# anujpal1/nanomind-slm architecture

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

- Interactive diagram: https://gitdiagram.com/anujpal1/nanomind-slm
- Repository: https://github.com/anujpal1/nanomind-slm

## Overview

NanoMind-SLM is a research-style ML repository organized around a single end-to-end pipeline: Python code data collection/cleaning, tokenizer training, tokenized dataset construction, decoder-only Transformer training, and experiment reporting.

1. Core architecture goal  
The project builds a compact Llama-style decoder-only Transformer for Python code, with a fixed model size, context length, and token budget. The repository is designed to compare two training-data conditions: a minimally filtered “raw” corpus versus an AST-validated, deduplicated “filtered” corpus.

2. Main package boundary  
All production code lives under `src/nanomind_slm`, split into four subsystems:
- `data`: corpus preparation and tokenized dataset creation
- `model`: Transformer architecture definition
- `training`: optimization, loss, and training loops
- `utils`: shared helpers such as seeding

3. Data pipeline  
The `data` package forms the first major boundary in the system. It includes:
- `filtering.py` for syntax/AST-based filtering
- `sampling.py` for dataset selection and balancing
- `packing.py` for packing token sequences to the target context length
- `token_dataset.py` for dataset objects used by training
- `build_tokenizer_corpora.py`, `train_tokenizers.py`, and `build_tokenized_data.py` for tokenizer and tokenized-corpus generation

This implies a staged flow: source Python code → filtered/sampled corpus → tokenizer training → tokenized training/validation sets.

4. Tokenization subsystem  
The README states the model uses a custom byte-level BPE tokenizer with an 8,000-token vocabulary. Tokenization is therefore a separate architectural layer from the model itself, not embedded in training. The tokenizer corpora and tokenized data builders are the bridge between raw code and model-ready sequences.

5. Model subsystem  
The `model` package contains the Transformer implementation:
- `attention.py` for attention primitives
- `layers.py` for higher-level blocks
- `config.py` for model hyperparameters
- `model.py` for the assembled decoder-only network

This is the central inference/training core and should be shown as consuming token IDs and emitting next-token logits.

6. Training subsystem  
The `training` package manages optimization and experiment execution:
- `loss.py` defines the training objective
- `schedule.py` handles learning-rate or step scheduling
- `train.py` is the main training entry point
- `smoke_train.py` is a lightweight sanity-check path

Training consumes tokenized datasets plus model configuration, produces checkpoints/metrics, and is the only place where the model is updated.

7. Experiment configuration  
The `configs` directory holds YAML files for base settings plus separate data, model, tokenization, and training configuration. This indicates configuration-driven experiment setup, where the same codebase can switch between raw/filtered runs and different training budgets without code changes.

8. Validation, auditing, and reproducibility  
The `scripts` directory contains repo-level checks for data quality and environment readiness:
- dataset stream auditing
- filtering audit
- dataset source verification
- hardware checks

This shows that dataset provenance and compute availability are treated as first-class concerns, which is important for a small-scale research project with strict token and GPU constraints.

9. Outputs and research artifacts  
The `reports` directory stores final metrics, syntax results, figures, and a research log. These are the experiment outputs rather than application runtime artifacts, reinforcing that the repository is research/benchmark oriented rather than serving an end-user product.

10. Test coverage and boundaries  
The `tests` directory maps closely to the package structure: attention, model, layers, filtering, sampling, packing, token dataset, seed, loss, and schedule. This indicates the repo’s main boundaries are meant to be independently verifiable: data correctness, model correctness, and training correctness.

11. Key architectural data flow for a diagram  
A useful diagram would show:
raw Python sources → filtering/deduplication/sampling → tokenizer training → tokenized/packed datasets → decoder-only Transformer training → evaluation/metrics reports  
with a parallel branch for the raw versus filtered dataset experiments sharing the same model and training stack.

12. Important repo-specific constraint  
The entire experiment is controlled by a fixed training-token budget and a compact model size (about 18.8M parameters). That constraint is central to the architecture: most subsystems exist to ensure a fair comparison between data-quality variants rather than to scale the model itself.

Main entry points: [`src/nanomind_slm/training/train.py`](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/training/train.py) (Train), [`src/nanomind_slm/data/build_tokenized_data.py`](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/build_tokenized_data.py) (Tokenized data), [`src/nanomind_slm/data/filtering.py`](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/filtering.py) (Filtering), [`src/nanomind_slm/model/layers.py`](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/model/layers.py) (Transformer blocks)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_data["Data pipeline"]
  node_node_raw_sources[("Python corpus<br/>source data")]
  node_node_filtering["Filtering<br/>AST filter<br/>[filtering.py]"]
  node_node_sampling["Sampling<br/>data selection<br/>[sampling.py]"]
  node_node_tokenizer_corpora["Tokenizer corpora<br/>prep step"]
  node_node_train_tokenizers["Tokenizer training<br/>BPE trainer"]
  node_node_tokenized_data[("Tokenized data<br/>dataset build")]
  node_node_packing["Packing<br/>sequence packer<br/>[packing.py]"]
  node_node_token_dataset[("Token dataset<br/>dataset API<br/>[token_dataset.py]")]
end

subgraph group_group_model["Model core"]
  node_node_model_config["Model config<br/>hyperparams<br/>[config.py]"]
  node_node_attention["Attention<br/>[attention.py]"]
  node_node_layers["Transformer blocks<br/>block stack<br/>[layers.py]"]
  node_node_decoder{{"Decoder LM<br/>transformer model<br/>[model.py]"}}
end

subgraph group_group_training["Training"]
  node_node_loss["Loss<br/>objective<br/>[loss.py]"]
  node_node_schedule["Schedule<br/>[schedule.py]"]
  node_node_train{{"Train<br/>main loop<br/>[train.py]"}}
  node_node_smoke_train["Smoke train<br/>sanity run<br/>[smoke_train.py]"]
end

subgraph group_group_ops["Ops and validation"]
  node_node_seed["Seeding<br/>repro helper<br/>[seed.py]"]
  node_node_audits["Audits<br/>repo checks"]
  node_node_reports["Reports<br/>experiment outputs"]
end

node_node_raw_sources -->|"clean"| node_node_filtering
node_node_filtering -->|"subset"| node_node_sampling
node_node_sampling -->|"prepare"| node_node_tokenizer_corpora
node_node_tokenizer_corpora -->|"train"| node_node_train_tokenizers
node_node_train_tokenizers -->|"encode"| node_node_tokenized_data
node_node_tokenized_data -->|"pack"| node_node_packing
node_node_packing -->|"serve"| node_node_token_dataset
node_node_model_config -->|"configures"| node_node_layers
node_node_attention -->|"builds"| node_node_layers
node_node_layers -->|"assembles"| node_node_decoder
node_node_token_dataset -->|"inputs"| node_node_train
node_node_decoder -->|"optimizes"| node_node_train
node_node_loss -->|"objective"| node_node_train
node_node_schedule -->|"controls"| node_node_train
node_node_seed -->|"reproducibility"| node_node_train
node_node_smoke_train -.->|"variant"| node_node_train
node_node_train -->|"outputs"| node_node_reports
node_node_audits -.->|"checks"| node_node_filtering
node_node_audits -.->|"checks"| node_node_tokenized_data
node_node_audits -.->|"checks"| node_node_train
node_node_tokenized_data -->|"feeds"| node_node_decoder

click node_node_filtering "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/filtering.py"
click node_node_sampling "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/sampling.py"
click node_node_tokenizer_corpora "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/build_tokenizer_corpora.py"
click node_node_train_tokenizers "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/train_tokenizers.py"
click node_node_tokenized_data "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/build_tokenized_data.py"
click node_node_packing "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/packing.py"
click node_node_token_dataset "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/token_dataset.py"
click node_node_model_config "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/model/config.py"
click node_node_attention "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/model/attention.py"
click node_node_layers "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/model/layers.py"
click node_node_decoder "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/model/model.py"
click node_node_loss "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/training/loss.py"
click node_node_schedule "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/training/schedule.py"
click node_node_train "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/training/train.py"
click node_node_smoke_train "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/training/smoke_train.py"
click node_node_seed "https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/utils/seed.py"
click node_node_reports "https://github.com/anujpal1/nanomind-slm/tree/main/reports"

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_node_raw_sources,node_node_filtering,node_node_sampling,node_node_tokenizer_corpora,node_node_train_tokenizers,node_node_tokenized_data,node_node_packing,node_node_token_dataset toneBlue
class node_node_model_config,node_node_attention,node_node_layers,node_node_decoder toneAmber
class node_node_loss,node_node_schedule,node_node_train,node_node_smoke_train toneMint
class node_node_seed,node_node_audits,node_node_reports toneRose
```

## Components

### Data pipeline

Corpus preparation, tokenizer training, and tokenized dataset construction.

- **Python corpus**: source data · Raw Python code inputs used to build the training corpus.
- [Filtering](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/filtering.py): AST filter · Syntax and AST-based validation and deduplication of candidate code. · `src/nanomind_slm/data/filtering.py`
- [Sampling](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/sampling.py): data selection · Corpus selection and balancing for experiment variants. · `src/nanomind_slm/data/sampling.py`
- [Tokenizer corpora](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/build_tokenizer_corpora.py): prep step · Builds corpora used to train the custom tokenizer. · `src/nanomind_slm/data/build_tokenizer_corpora.py`
- [Tokenizer training](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/train_tokenizers.py): BPE trainer · Trains the byte-level BPE tokenizer with the target vocabulary size. · `src/nanomind_slm/data/train_tokenizers.py`
- [Tokenized data](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/build_tokenized_data.py): dataset build · Converts corpora into model-ready tokenized splits. · `src/nanomind_slm/data/build_tokenized_data.py`
- [Packing](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/packing.py): sequence packer · Packs token sequences to the target context length. · `src/nanomind_slm/data/packing.py`
- [Token dataset](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/data/token_dataset.py): dataset API · Dataset objects consumed by training. · `src/nanomind_slm/data/token_dataset.py`

### Model core

Decoder-only Transformer implementation and configuration.

- [Model config](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/model/config.py): hyperparams · Model architecture and size settings. · `src/nanomind_slm/model/config.py`
- [Attention](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/model/attention.py): attention core · Self-attention primitives used by the Transformer blocks. · `src/nanomind_slm/model/attention.py`
- [Transformer blocks](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/model/layers.py): block stack · Higher-level decoder block composition. · `src/nanomind_slm/model/layers.py`
- [Decoder LM](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/model/model.py): transformer model · Assembled decoder-only Transformer that maps token IDs to next-token logits. · `src/nanomind_slm/model/model.py`

### Training

Optimization, loss, scheduling, and entrypoints for experiment runs.

- [Loss](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/training/loss.py): objective · Training objective for next-token prediction. · `src/nanomind_slm/training/loss.py`
- [Schedule](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/training/schedule.py): lr schedule · Learning-rate or step scheduling utilities. · `src/nanomind_slm/training/schedule.py`
- [Train](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/training/train.py): main loop · Primary training entrypoint that updates model weights and emits metrics/checkpoints. · `src/nanomind_slm/training/train.py`
- [Smoke train](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/training/smoke_train.py): sanity run · Lightweight training path for quick validation. · `src/nanomind_slm/training/smoke_train.py`

### Ops and validation

Repo-level audits, environment checks, and experiment artifacts.

- [Seeding](https://github.com/anujpal1/nanomind-slm/blob/main/src/nanomind_slm/utils/seed.py): repro helper · Shared reproducibility helpers. · `src/nanomind_slm/utils/seed.py`
- **Audits**: repo checks · Dataset stream, filtering, source verification, and hardware readiness checks.
- [Reports](https://github.com/anujpal1/nanomind-slm/tree/main/reports): experiment outputs · Final metrics, syntax results, figures, and research log artifacts. · `reports`

## Connections

- Python corpus → Filtering: clean · Raw code is validated and deduplicated before inclusion.
- Filtering → Sampling: subset · Filtered code is sampled and balanced for experiment variants.
- Sampling → Tokenizer corpora: prepare · Selected corpus is converted into tokenizer training corpora.
- Tokenizer corpora → Tokenizer training: train · Tokenizer corpora are used to fit the byte-level BPE tokenizer.
- Tokenizer training → Tokenized data: encode · Trained tokenizer is used to build tokenized splits.
- Tokenized data → Packing: pack · Token sequences are packed to the target context length.
- Packing → Token dataset: serve · Packed sequences are exposed through dataset objects.
- Model config → Transformer blocks: configures · Hyperparameters shape the Transformer block stack.
- Attention → Transformer blocks: builds · Attention primitives are composed into blocks.
- Transformer blocks → Decoder LM: assembles · Blocks form the full decoder-only language model.
- Token dataset → Train: inputs · Training consumes tokenized datasets.
- Decoder LM → Train: optimizes · The model is updated by the training loop.
- Loss → Train: objective · Training uses the next-token loss.
- Schedule → Train: controls · Scheduling shapes optimization dynamics.
- Seeding → Train: reproducibility · Shared seeding supports repeatable runs.
- Smoke train → Train: variant · Smoke training is a lightweight path through the same stack.
- Train → Reports: outputs · Training produces metrics and research artifacts.
- Audits → Filtering: checks · Audit scripts validate filtering behavior and data provenance.
- Audits → Tokenized data: checks · Audit scripts validate the dataset stream.
- Audits → Train: checks · Hardware checks gate training readiness.
- Tokenized data → Decoder LM: feeds · Tokenized inputs are consumed by the model during training and evaluation.
