Skip to content
LinJiaShenPublic

About

A reproducible LLM engineering research lab covering Transformer training, LoRA adaptation, distribution shift, RAG, mixed precision, and KV-cache inference.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

TrainLLM

CPU verification MIT license

TrainLLM is a reproducible language-model engineering lab. Phase I builds a causal Transformer and LoRA from PyTorch primitives. Phase II examines where adaptation fails under distribution shift, whether CUDA helps a small workload, and how to measure retrieval and cached inference independently of answer quality.

Architecture

flowchart LR
    D["Synthetic corpora"] --> S["Document splits and tokenization"]
    S --> T["Bigram and causal decoders"]
    T --> A["Independent LoRA"]
    T --> K["Full, concatenated and preallocated decoding"]
    P["Pinned public checkpoints"] --> L["PEFT adaptation"]
    A --> E["Shift and counterfactual evaluation"]
    L --> E
    D --> R["BM25, dense and hybrid retrieval"]
    R --> G["Local generators and schema-specific parser"]
    E --> M["Raw metrics, manifests and figures"]
    K --> M
    G --> M
Loading

Phase I · Train and verify

The decoder uses token and learned absolute position embeddings, explicit multi-head causal attention, pre-normalization, residual connections and a GELU feed-forward block. The three models saw the same 183,328 non-padding targets from 480 training log documents, with 60 validation and 60 test documents. AdamW training uses token-weighted cross-entropy, gradient clipping and resumable checkpoints.

Model Parameters Validation perplexity Test perplexity
Bigram 1,444 18.734 18.753
Small Transformer 109,056 1.325 1.327
Wider Transformer 349,152 1.324 1.328

Phase II reproduced the three unrounded test perplexities exactly on the tested host. This establishes metric reproducibility for the run. Checkpoint identity was not checked.

Phase I training loss and validation perplexity

The independent LoRA layer freezes the base map and trains a scaled low-rank update. Tests verify initialization, gradient isolation, merge, state reload and FP64 agreement with PEFT. Architecture · Mathematics · Phase I report

Phase II · Measure transfer and performance

Adaptation. The original local rank-2 accuracy ranged from 50.0% to 95.8% across three seeds. Both Pythia adapters classified all 96 matched examples correctly in the training layout, then reached 50% in the held-out layout by predicting one class throughout. On a separate arithmetic task with six 48-example test groups, diverse training layouts raised mean combined-shift accuracy from 45.8% to 50.7% and both-correct numerical counterfactual accuracy from 20.8% to 23.6%, while in-domain accuracy fell from 75.0% to 70.1%. SmolLM2 LoRA lowered validation NLL from 2.218 to 0.737 and remained at 50% accuracy on every held-out group.

Local LoRA seed sensitivity and Pythia prediction collapse across layouts

Training hardware. On a Ryzen 7 5700X3D and RTX 5060 Ti, a matched 120-step, batch-16 run reached 45,222 targets/s on CPU FP32 versus 39,675 / 40,375 / 41,818 on CUDA FP32 / BF16 / FP16. All validation losses rounded to 0.2908. The GPU used PyTorch 2.7.1+cu128, with synchronized timings. This result applies to the measured small workload.

CPU and CUDA throughput, convergence and CUDA allocations

Retrieval and generation. On Pump Register v2 (36 records, 16 held-out queries), BM25 Recall@3 was 100% in each answerable category. Mean-pooled Pythia scores were 75% / 25% / 12.5% for exact, paraphrased and multi-record queries. MiniLM and fixed rank fusion each reached 100% / 75% / 100%. With matching evidence, Pythia and SmolLM2 produced 0% exact match even with oracle records, while the schema-specific parser achieved 100% with retrieved or oracle records.

Retrieval by query category and generation with matched evidence

KV cache. Recomputation, concatenation and preallocation share absolute-position and rollover semantics. Numerical comparisons and greedy output checks passed. At batch 1 with 48 prompt tokens on CPU, median inter-token latency was 0.940 / 0.631 / 0.701 ms, respectively. Preallocation showed no consistent speedup. Phase II report · All 15 figures

Quick start and verification

Use Python 3.12.4 in an active virtual environment.

python -m pip install -r requirements.txt
python -m pip install --no-build-isolation -e .
python -m pytest -q
python -m trainllm.study --mode smoke --steps 3
python -m trainllm.verify runs/metrics.json

The CPU workflow installs dependencies on a standard runner, runs these checks, and validates both committed manifests. The PEFT comparison and CUDA regression skip when their optional requirements are absent. The smoke run uses a tiny synthetic dataset and needs no model downloads or credentials. Hosted CPU checks · Numerical verification

Reproduction Requirements
Full Phase I CPU from-scratch study. Optional pretrained experiments download pinned Pythia weights.
Full Phase II CPU generalization and cache studies, optional pretrained experiments, and a separate CUDA environment for accelerator measurements.

Phase I raw results and manifest · Phase II raw results and manifest

Scope and attribution

The datasets are deterministic synthetic engineering fixtures. The seed counts, prompt families and hardware limit generalization. Model-level prompt-injection resistance was outside the evaluated scope. The security evidence covers fixed schema and authorization fixtures. Limitations and negative results

Independent project source and original synthetic datasets are MIT. Pythia-14M, SmolLM2-135M-Instruct and all-MiniLM-L6-v2 retain their upstream Apache-2.0 terms. The repository includes the pinned model and tokenizer revisions and distributes no weights. License · Upstream attribution

About

A reproducible LLM engineering research lab covering Transformer training, LoRA adaptation, distribution shift, RAG, mixed precision, and KV-cache inference.

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages