How anujpal1/nanomind-slm works
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: train.py Train · build_tokenized_data.py Tokenized data · filtering.py Filtering · layers.py Transformer blocks
Components 19
Data pipeline
Corpus preparation, tokenizer training, and tokenized dataset construction.
- Python corpus · Raw Python code inputs used to build the training corpus.
- Filtering · Syntax and AST-based validation and deduplication of candidate code.
src/nanomind_slm/data/filtering.py - Sampling · Corpus selection and balancing for experiment variants.
src/nanomind_slm/data/sampling.py - Tokenizer corpora · Builds corpora used to train the custom tokenizer.
src/nanomind_slm/data/build_tokenizer_corpora.py - Tokenizer training · Trains the byte-level BPE tokenizer with the target vocabulary size.
src/nanomind_slm/data/train_tokenizers.py - Tokenized data · Converts corpora into model-ready tokenized splits.
src/nanomind_slm/data/build_tokenized_data.py - Packing · Packs token sequences to the target context length.
src/nanomind_slm/data/packing.py - Token dataset · Dataset objects consumed by training.
src/nanomind_slm/data/token_dataset.py
Model core
Decoder-only Transformer implementation and configuration.
- Model config · Model architecture and size settings.
src/nanomind_slm/model/config.py - Attention · Self-attention primitives used by the Transformer blocks.
src/nanomind_slm/model/attention.py - Transformer blocks · Higher-level decoder block composition.
src/nanomind_slm/model/layers.py - Decoder LM · 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 · Training objective for next-token prediction.
src/nanomind_slm/training/loss.py - Schedule · Learning-rate or step scheduling utilities.
src/nanomind_slm/training/schedule.py - Train · Primary training entrypoint that updates model weights and emits metrics/checkpoints.
src/nanomind_slm/training/train.py - Smoke train · Lightweight training path for quick validation.
src/nanomind_slm/training/smoke_train.py
Ops and validation
Repo-level audits, environment checks, and experiment artifacts.
Connections 21
- 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.