Skip to content

Add SmolLM2-135M-Instruct model (smollm2_infer) - #8

Merged
peterbjohnson merged 2 commits into
mainfrom
add-smollm2-model
Nov 9, 2026
Merged

peterbjohnson merged 2 commits into
mainfrom
add-smollm2-model

Conversation

@peterbjohnson

Copy link
Copy Markdown
Member

Adds SmolLM2-135M-Instruct next to GPT-2 so the course can compare them side by side: a similar parameter count, but newer training data, a newer architecture and instruction tuning. GPT-2's behaviour is unchanged.

Two commits, best reviewed separately:

  1. Extract shared Hugging Face model code from gpt2_infer (refactor only). Moves loading, sampling, parameter clamping and the error result into models/hf_lm.py, and renames scripts/download_gpt2.py to download_hf_model.py, which now takes the target directory and --model-id explicitly. For fixed seeds, GPT-2's output was identical before and after, including clamped and invalid parameters and an empty prompt.
  2. Add smollm2_infer.
    • The student's response is wrapped in the chat template (add_generation_prompt=True), and only the reply is returned. GPT-2 returns the prompt followed by its continuation. The difference is documented for teachers in docs/user.md.
    • It takes the same params as GPT-2. word_count defaults to 64 (max 256) for chat replies, and top_k is clamped to the 49152-token vocabulary. Temperature and top_k defaults match GPT-2's, so the comparison is fair.
    • The weights are loaded in float32 with no quantisation, and baked into their own image layer under SMOLLM2_MODEL_DIR, with the same safetensors chmod fix as GPT-2.
    • The generation tests cover both models and now run when RUN_HF_MODEL_TESTS=1 is set (previously RUN_GPT2_TESTS).

Testing

  • pytest: the CI-safe tests pass. With RUN_HF_MODEL_TESTS=1, the generation tests also pass for both models, loading both from baked directories and from the Hugging Face download fallback.
  • I built the Docker image (3.94GB) and ran both models inside it with --network none as a non-root user. Both load from their baked directories and generate. Peak memory with both loaded was about 1.5GB, against the Lambda's 2GB.
  • Not yet checked on staging. That should happen once this is merged and deployed, ideally with scripts/stress_test.py --model smollm2_infer after the model has been added to a question set.

🤖 Generated with Claude Code

peterbjohnson and others added 2 commits November 4, 2026 22:00
Pure refactor in preparation for a second Hugging Face model; GPT-2
output is unchanged (verified identical for fixed seeds).

- models/hf_lm.py: cached local-dir-or-download loading, sampling,
  param clamping and error result, previously private to gpt2_infer
- Load weights explicitly in float32 rather than relying on the
  transformers default
- Rename scripts/download_gpt2.py to download_hf_model.py, taking the
  target dir and model id explicitly

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Adds SmolLM2-135M-Instruct alongside GPT-2 for side-by-side comparison
in the course: similar parameter count, but newer data, architecture
and instruction tuning. GPT-2 is unchanged.

- smollm2_infer wraps the student's response in the chat template and
  returns only the model's reply (GPT-2 returns prompt + continuation);
  the difference is documented for teachers in docs/user.md
- Same params as gpt2_infer; word_count defaults to 64 (max 256) for
  chat replies and top_k is clamped to the 49152-token vocabulary
- Bake the weights into the image under SMOLLM2_MODEL_DIR, in their own
  layer, with the same safetensors permission fix as GPT-2
- Tests cover both models; generation tests now run under
  RUN_HF_MODEL_TESTS (was RUN_GPT2_TESTS)

Verified in the built image (offline, non-root): both models load from
the baked directories, with peak memory about 1.5GB once both are loaded.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@peterbjohnson
peterbjohnson merged commit 80ef10b into main Nov 9, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant