Purpose
This project is a 25.7M parameter language model trained from random initialization, inspired by GLM-5.3-Flash, and improved after pretraining using executable-reward reinforcement learning (RL). It is aimed at developers and researchers who want to understand large model training workflows, reinforcement learning fine-tuning, and architecture design, providing a readable, reproducible educational implementation.
Main Capabilities
- Pretraining from scratch: Directly generates a small synthetic Python corpus on GPU, with no need to download a dataset. Pretraining for 400 steps consumes 1,381,760 tokens, takes 63.1 seconds, and peaks at 2.139 GiB of memory.
- Executable-reward RL: Uses RLOO policy gradient post-training, run on 96 prompts, sampling 16 completions per prompt. The reward is based only on executable results: passing all hidden tests gets 1, valid but incorrect gets 0, and invalid code gets -0.1. The final RL run uses 1,536 rollouts, performs 73 reward-bearing updates, takes 155.5 seconds, and peaks at 0.936 GiB of memory (RTX 3080 Ti).
- Multi-backend support: The same code supports NVIDIA CUDA, Apple Silicon MPS, and CPU execution, and the training script accepts
--device autoto automatically select the available backend. - Architecture implementation: The model adopts the released model's high-level linear, linear, linear, sparse rhythm. Its ELU feature linear attention is not GLM's exact KDA implementation; the local plus strided grouped attention is not DeepSeek Sparse Attention with an indexer; the learned flow mixer is not the full manifold-constrained Sinkhorn formulation. This is an educational implementation, not a reproduction.
- Byte-level tokenization: The project deliberately uses one token per UTF-8 byte, rather than the released tokenizer's BPE. The byte tokenizer requires no learned vocabulary, can represent arbitrary text exactly, and keeps the embedding and output matrices extremely small, at the cost of longer sequences. The model predicts the next byte, which is equivalent to the next character for ASCII codes, but not necessarily for multi-byte Unicode characters.
- Optional miniature vision extension: A teaching-scale implementation following the released GLM-5.3-Flash image topology. The production model uses 448x448 RGB input, 14x14 spatial patches, 24 vision blocks (width 1024), 16 heads, 2D rotary position, a 2x2 learned downsampler, and a gated projector to a 4096-wide language model. The miniature version uses fixed 32x32 images, width 24, four heads, two blocks, learned position embeddings, and no video or variable-resolution packing.
- Full 25.7M language model integration pilot: Attaches a GLM-like vision encoder to the full 12-layer language model (initialized from a local step-100 encoded checkpoint). All 12 language layers execute, and training is limited to the vision encoder, final LM block, final norm, and tied embedding/output matrix. Runs 40 updates, 400 generated training images, 37 seconds (Apple Silicon MPS). Weights are not saved.
Usage
- Environment requirements: Use Python 3.12, because the pinned PyTorch 2.5.1 does not provide Python 3.13 wheels. Works on macOS Apple Silicon, Linux NVIDIA CUDA, and pure CPU machines.
- Quick start: After completing the quick start, GPU experiments can be reproduced.
- Pretraining: Run
scripts/train_pretrain.pyfor random initialization and causal language model pretraining. - RL post-training: Run
scripts/train_rl.pyfor batched RLOO with executable rewards. - Evaluation: Use
scripts/evaluate.pyfor greedy pass@1 evaluation;scripts/evaluate_passk.pyConduct sampling strategy evaluation. - Visual training:
scripts/train_vision.pyConduct deterministic RGB training and held-out comparison. - Full-model visual pilot:
experiments/full_25m_vision_digit_pilot.pyConduct a bounded full language model visual integration test, never saving weights. - Course slides: The repository contains HTML slides, real before-and-after generation animations, and small chart assets. Run
slides/serve_slides.pythen openhttp://127.0.0.1:8765/slides.html. On macOS,slides/start-slides.commandcan automatically start the server and open the slides. - Reproduce experiments: The final raw results, charts, and reports are located in
artifacts/experiments. The analysis code is inscripts/analyze_rl_variants.pyandscripts/analyze_rl_group_confirmation.py.
Applicable scenarios
- Learn the complete pipeline of training a language model from scratch, pretraining data generation, checkpoint selection, and RL fine-tuning.
- Study the impact of executable reward reinforcement learning on narrow coding behavior, as well as ablation experiments on RL design choices (reward density, invalid code penalty, sampling temperature, rollout group size).
- Understand a teaching implementation of the high-level architectural rhythm of GLM-5.3-Flash, including hybrid attention, MoE, residual streams, and visual encoder integration.
- Reproduce small-scale training experiments on limited computational resources (a single RTX 3080 Ti or Apple Silicon).
Limitations
- This is an educational implementation, not a reproduction of GLM-5.3-Flash. The real GLM-5.3-Flash has 320B total parameters, 18B active parameters, 45 text layers, hybrid linear/sparse attention, MoE, four-stream mHC, IndexPool, million-token context, and native multimodality.
- The statistical results describe only synthetic benchmarks and cannot establish broad coding ability. The paired greedy comparison has 16 gains and 0 losses on the training-family confirmation task (exact McNemar/binomial p = 0.0000305), with a task-level bootstrap interval of +25.0 to +53.1 percentage points.
- RL makes increment and double reliable after these operation patterns have already appeared in pretraining, but it does not make even reliable, and sampling performance degrades on the untrained square and list_sum families. This interference is part of the result and is not hidden.
- The pretraining study uses a 248,412-parameter tiny model and 32 held-out expression structures, with each comparison repeated on 10 paired seeds. The strongest result is the ranking comparison: interleaving wins on all 10 paired seeds, but exact expression accuracy remains 0% under all conditions, so these results measure partial next-byte learning on synthetic code, not reliable program generation.
- RL design choice follow-up experiment: the screen appears to prefer group size 4, but all seven paired comparisons with the standard group 8 have a Holm-corrected p-value of 1.0. On the 40-task confirmation split, the task bootstrap 95% interval for group sizes 4 and 8 across three RL training seeds is -6.7 to +1.7 percentage points, crossing zero. The development set winner did not reproduce. The lower sampling temperature 0.20 remains the strongest unconfirmed clue and should be reproduced before spending more compute.
- Visual experiment: in the recorded single-seed run, the faithful tiny model improved from 0/200 to 119/200 held-out accuracy, with loss 5.5809 -> 1.1618; the smaller direct patch baseline improved from 0/200 to 189/200, with loss 5.5758 -> 0.9608. The baseline wins in this tiny short-run comparison. This does not indicate that the production topology is worse, but rather that adding structure under a very small model and training budget does not pay off.
- Full 25.7M visual integration pilot: the first 11 LM blocks are frozen, semantic digit classes are shared between training and evaluation, single seed, and no matched baseline. It proves that the integration can learn this synthetic rendering task, but it does not establish general visual ability.
- Model weights are intentionally excluded: rerunning both training stages takes about four minutes on the measured GPU.
- License: MIT. This repository is an independent educational implementation and is not affiliated with Z.ai.