# aliutkus/spe architecture

> Architecture diagram of the GitHub repository aliutkus/spe, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aliutkus/spe
- Repository: https://github.com/aliutkus/spe

## Overview

#### Purpose and layout
This research repository implements Stochastic Positional Encoding (SPE), a relative-position-compatible encoding for linear-complexity Transformers. It contains reusable PyTorch and JAX packages plus three independent experiment stacks: Long Range Arena (LRA), pop-piano generation, and groove continuation. It is not a unified application or service.

#### Core SPE libraries
The primary reusable entry points are framework-specific packages:
- PyTorch: `src/pytorch/spe/spe.py`, packaged by `src/pytorch/setup.py`
- JAX/Flax: `src/jax/jax_spe/spe.py`, packaged by `src/jax/setup.py`

These form the positional-encoding boundary: downstream Transformer models consume their generated positional representations while retaining framework-specific tensor/runtime conventions.

#### LRA benchmark pipeline
The LRA experiment compares attention/model variants across AAN, CIFAR-10, ListOps, pathfinder, and text classification through shell launchers:
- `experiments/lra/run_aan.sh`
- `experiments/lra/run_cifar10.sh`
- `experiments/lra/run_listops.sh`

Model/run configuration and reported outputs are persisted per architecture, task, and repeat under `experiments/lra/models/gpu_16g/`, e.g. `experiments/lra/models/gpu_16g/performer_softmax/aan/r1/config.py` and `experiments/lra/models/gpu_16g/performer_softmax/aan/r1/results.json`. This directory is primarily an experiment-record boundary rather than shared executable model code.

#### Linear-attention dependency boundary
LRA vendors a fast-attention implementation as a separately packaged component:
- `experiments/lra/fast_attention/fast_self_attention/fast_self_attention.py`
- `experiments/lra/fast_attention/setup.py`
- `experiments/lra/long-range-arena`

The `long-range-arena` path is a submodule dependency, as implied by `.gitmodules`; cloning recursively is required. Fast self-attention is the key mechanism that makes SPE experiments compatible with long sequences.

#### Pop-piano generation
This is a standalone PyTorch training/inference workflow. Entrypoints are `experiments/pop_piano/train.py`, `experiments/pop_piano/inference.py`, and `experiments/pop_piano/eval.py`. Serialized REMI vocabulary and dataset splits enter through `experiments/pop_piano/pickles/remi_vocab.pkl`, `experiments/pop_piano/pickles/train_pieces.pkl`, and `experiments/pop_piano/dataloader.py`.

Token sequences flow into APE or SPE Performer model variants, then generation utilities produce continuations; inference output can be converted to MIDI via `experiments/pop_piano/convert2midi.py`. Architecture selection and training/inference parameters are separated into YAML configs, notably `experiments/pop_piano/configs/train/convspe_default.yaml` and `experiments/pop_piano/configs/inference/default.yaml`.

#### Groove continuation
Groove uses an installable `spe_music` package and a notebook-oriented data/evaluation workflow:
- Training: `experiments/groove/src/spe_music/train_performer_grv2grv.py`
- Event representation: `experiments/groove/src/spe_music/event_processor.py`
- Models: `experiments/groove/src/spe_music/model/music_performer.py`, `experiments/groove/src/spe_music/model/fast_transformer_decoder.py`

Prepared groove data feeds event processing, Performer decoding, and trained experiment configurations such as `experiments/groove/exp/trio_performer_softmax_convspe_l512_v01/config.yaml`. Style evaluation is a separate post-generation boundary using `experiments/groove/src/spe_music/style_eval/style_profiles.py` and `experiments/groove/src/spe_music/style_eval/note_features.py`.

#### Runtime and infrastructure
The code is Python-based, with PyTorch for music and likely LRA model execution, plus JAX/Flax for the standalone SPE implementation. Workloads are GPU-oriented, evidenced by `experiments/lra/models/gpu_16g/`. There are no deployed services or network APIs; external dependencies are Python packages, benchmark/data assets, a vendored/submodule LRA dependency, and MIDI-oriented local artifacts.

Main entry points: [`src/pytorch/spe/spe.py`](https://github.com/aliutkus/spe/blob/main/src/pytorch/spe/spe.py) (PyTorch SPE), [`experiments/lra/fast_attention/fast_self_attention/fast_self_attention.py`](https://github.com/aliutkus/spe/blob/main/experiments/lra/fast_attention/fast_self_attention/fast_self_attention.py) (Fast self-attention), [`experiments/pop_piano/inference.py`](https://github.com/aliutkus/spe/blob/main/experiments/pop_piano/inference.py) (Piano inference), [`experiments/groove/src/spe_music/model/music_performer.py`](https://github.com/aliutkus/spe/blob/main/experiments/groove/src/spe_music/model/music_performer.py) (Performer decoder)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_core["SPE libraries"]
  node_torch_spe["PyTorch SPE<br/>positional encoding<br/>[spe.py]"]
  node_jax_spe["JAX/Flax SPE<br/>positional encoding<br/>[spe.py]"]
end

subgraph group_lra["LRA benchmarks"]
  node_lra_launchers["LRA launchers<br/>shell entrypoints<br/>[run_aan.sh]"]
  node_fast_attention["Fast self-attention<br/>linear attention"]
  node_lra_submodule{{"Long Range Arena<br/>git submodule"}}
  node_lra_records["LRA run records<br/>experiment artifacts<br/>[results.json]"]
end

subgraph group_piano["Pop-piano"]
  node_piano_configs["Piano YAML configs<br/>configuration"]
  node_piano_data[("REMI data and vocabulary<br/>serialized dataset<br/>[remi_vocab.pkl]")]
  node_piano_loader["Piano dataloader<br/>data pipeline<br/>[dataloader.py]"]
  node_piano_models["APE/SPE Performer<br/>music model"]
  node_piano_train["Piano training<br/>entrypoint<br/>[train.py]"]
  node_piano_inference["Piano inference<br/>entrypoint<br/>[inference.py]"]
  node_midi_output["MIDI conversion<br/>local artifact export<br/>[convert2midi.py]"]
end

subgraph group_groove["Groove continuation"]
  node_groove_data[("Prepared groove data<br/>dataset")]
  node_event_processor["Event processor<br/>music representation<br/>[event_processor.py]"]
  node_groove_decoder["Performer decoder<br/>music model<br/>[music_performer.py]"]
  node_groove_train["Groove training<br/>entrypoint"]
  node_groove_config["Groove experiment config<br/>configuration<br/>[config.yaml]"]
  node_style_eval["Style evaluation<br/>post-generation evaluation<br/>[style_profiles.py]"]
end

node_lra_launchers -->|"runs benchmarks"| node_lra_submodule
node_lra_launchers -->|"selects attention"| node_fast_attention
node_torch_spe -->|"SPE-compatible attention"| node_fast_attention
node_fast_attention -->|"records runs"| node_lra_records
node_piano_configs -->|"configures"| node_piano_train
node_piano_configs -->|"configures"| node_piano_inference
node_piano_data -->|"loads"| node_piano_loader
node_piano_loader -->|"token sequences"| node_piano_models
node_torch_spe -->|"SPE variant"| node_piano_models
node_piano_models -->|"optimizes"| node_piano_train
node_piano_models -->|"generates"| node_piano_inference
node_piano_inference -->|"exports MIDI"| node_midi_output
node_groove_data -->|"events"| node_event_processor
node_event_processor -->|"encoded sequences"| node_groove_decoder
node_torch_spe -->|"SPE variant"| node_groove_decoder
node_groove_config -->|"configures"| node_groove_train
node_groove_decoder -->|"trains"| node_groove_train
node_groove_train -->|"generated continuations"| node_style_eval

click node_torch_spe "https://github.com/aliutkus/spe/blob/main/src/pytorch/spe/spe.py"
click node_jax_spe "https://github.com/aliutkus/spe/blob/main/src/jax/jax_spe/spe.py"
click node_lra_launchers "https://github.com/aliutkus/spe/blob/main/experiments/lra/run_aan.sh"
click node_fast_attention "https://github.com/aliutkus/spe/blob/main/experiments/lra/fast_attention/fast_self_attention/fast_self_attention.py"
click node_lra_submodule "https://github.com/aliutkus/spe/tree/main/experiments/lra/long-range-arena"
click node_lra_records "https://github.com/aliutkus/spe/blob/main/experiments/lra/models/gpu_16g/performer_softmax/aan/r1/results.json"
click node_piano_configs "https://github.com/aliutkus/spe/blob/main/experiments/pop_piano/configs/train/convspe_default.yaml"
click node_piano_data "https://github.com/aliutkus/spe/blob/main/experiments/pop_piano/pickles/remi_vocab.pkl"
click node_piano_loader "https://github.com/aliutkus/spe/blob/main/experiments/pop_piano/dataloader.py"
click node_piano_train "https://github.com/aliutkus/spe/blob/main/experiments/pop_piano/train.py"
click node_piano_inference "https://github.com/aliutkus/spe/blob/main/experiments/pop_piano/inference.py"
click node_midi_output "https://github.com/aliutkus/spe/blob/main/experiments/pop_piano/convert2midi.py"
click node_event_processor "https://github.com/aliutkus/spe/blob/main/experiments/groove/src/spe_music/event_processor.py"
click node_groove_decoder "https://github.com/aliutkus/spe/blob/main/experiments/groove/src/spe_music/model/music_performer.py"
click node_groove_train "https://github.com/aliutkus/spe/blob/main/experiments/groove/src/spe_music/train_performer_grv2grv.py"
click node_groove_config "https://github.com/aliutkus/spe/blob/main/experiments/groove/exp/trio_performer_softmax_convspe_l512_v01/config.yaml"
click node_style_eval "https://github.com/aliutkus/spe/blob/main/experiments/groove/src/spe_music/style_eval/style_profiles.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_torch_spe,node_jax_spe toneBlue
class node_lra_launchers,node_fast_attention,node_lra_submodule,node_lra_records toneAmber
class node_piano_configs,node_piano_data,node_piano_loader,node_piano_models,node_piano_train,node_piano_inference,node_midi_output toneMint
class node_groove_data,node_event_processor,node_groove_decoder,node_groove_train,node_groove_config,node_style_eval toneRose
```

## Components

### SPE libraries

Framework-specific reusable positional encoding packages.

- [PyTorch SPE](https://github.com/aliutkus/spe/blob/main/src/pytorch/spe/spe.py): positional encoding · `src/pytorch/spe/spe.py`
- [JAX/Flax SPE](https://github.com/aliutkus/spe/blob/main/src/jax/jax_spe/spe.py): positional encoding · `src/jax/jax_spe/spe.py`

### LRA benchmarks

Long-sequence attention experiments and recorded runs.

- [LRA launchers](https://github.com/aliutkus/spe/blob/main/experiments/lra/run_aan.sh): shell entrypoints · Launch task-specific benchmark runs. · `experiments/lra/run_aan.sh`
- [Fast self-attention](https://github.com/aliutkus/spe/blob/main/experiments/lra/fast_attention/fast_self_attention/fast_self_attention.py): linear attention · Vendored long-sequence attention mechanism. · `experiments/lra/fast_attention/fast_self_attention/fast_self_attention.py`
- [Long Range Arena](https://github.com/aliutkus/spe/tree/main/experiments/lra/long-range-arena): git submodule · External benchmark dependency requiring recursive clone. · `experiments/lra/long-range-arena`
- [LRA run records](https://github.com/aliutkus/spe/blob/main/experiments/lra/models/gpu_16g/performer_softmax/aan/r1/results.json): experiment artifacts · Configurations and results are stored per architecture, task, and repeat. · `experiments/lra/models/gpu_16g/performer_softmax/aan/r1/results.json`

### Pop-piano

Standalone PyTorch generation workflow.

- [Piano YAML configs](https://github.com/aliutkus/spe/blob/main/experiments/pop_piano/configs/train/convspe_default.yaml): configuration · Selects architecture and training or inference parameters. · `experiments/pop_piano/configs/train/convspe_default.yaml`
- [REMI data and vocabulary](https://github.com/aliutkus/spe/blob/main/experiments/pop_piano/pickles/remi_vocab.pkl): serialized dataset · Vocabulary and split artifacts feed the dataloader. · `experiments/pop_piano/pickles/remi_vocab.pkl`
- [Piano dataloader](https://github.com/aliutkus/spe/blob/main/experiments/pop_piano/dataloader.py): data pipeline · `experiments/pop_piano/dataloader.py`
- **APE/SPE Performer**: music model · Performer variants consume token sequences for training and generation.
- [Piano training](https://github.com/aliutkus/spe/blob/main/experiments/pop_piano/train.py): entrypoint · `experiments/pop_piano/train.py`
- [Piano inference](https://github.com/aliutkus/spe/blob/main/experiments/pop_piano/inference.py): entrypoint · Generates continuations. · `experiments/pop_piano/inference.py`
- [MIDI conversion](https://github.com/aliutkus/spe/blob/main/experiments/pop_piano/convert2midi.py): local artifact export · `experiments/pop_piano/convert2midi.py`

### Groove continuation

Installable music package with notebook-oriented evaluation.

- **Prepared groove data**: dataset
- [Event processor](https://github.com/aliutkus/spe/blob/main/experiments/groove/src/spe_music/event_processor.py): music representation · `experiments/groove/src/spe_music/event_processor.py`
- [Performer decoder](https://github.com/aliutkus/spe/blob/main/experiments/groove/src/spe_music/model/music_performer.py): music model · `experiments/groove/src/spe_music/model/music_performer.py`
- [Groove training](https://github.com/aliutkus/spe/blob/main/experiments/groove/src/spe_music/train_performer_grv2grv.py): entrypoint · `experiments/groove/src/spe_music/train_performer_grv2grv.py`
- [Groove experiment config](https://github.com/aliutkus/spe/blob/main/experiments/groove/exp/trio_performer_softmax_convspe_l512_v01/config.yaml): configuration · `experiments/groove/exp/trio_performer_softmax_convspe_l512_v01/config.yaml`
- [Style evaluation](https://github.com/aliutkus/spe/blob/main/experiments/groove/src/spe_music/style_eval/style_profiles.py): post-generation evaluation · Evaluates generated music using style profiles and note features. · `experiments/groove/src/spe_music/style_eval/style_profiles.py`

## Connections

- LRA launchers → Long Range Arena: runs benchmarks
- LRA launchers → Fast self-attention: selects attention
- PyTorch SPE → Fast self-attention: SPE-compatible attention
- Fast self-attention → LRA run records: records runs
- Piano YAML configs → Piano training: configures
- Piano YAML configs → Piano inference: configures
- REMI data and vocabulary → Piano dataloader: loads
- Piano dataloader → APE/SPE Performer: token sequences
- PyTorch SPE → APE/SPE Performer: SPE variant
- APE/SPE Performer → Piano training: optimizes
- APE/SPE Performer → Piano inference: generates
- Piano inference → MIDI conversion: exports MIDI
- Prepared groove data → Event processor: events
- Event processor → Performer decoder: encoded sequences
- PyTorch SPE → Performer decoder: SPE variant
- Groove experiment config → Groove training: configures
- Performer decoder → Groove training: trains
- Groove training → Style evaluation: generated continuations
