책 『밑바닥부터 만드는 추론 엔진: Rust로 배우는 AI 인퍼런스 엔지니어링』 (Building an Inference Engine from Scratch) 9장부터 13장까지 쓰는 정답 파일입니다. 모델의 가중치는 들어 있지 않습니다.
PyTorch reference activations for
HuggingFaceTB/SmolLM2-135M-Instruct,
used to verify the book's inference engine, badak, layer by layer. This
repository contains no model weights.
reference.safetensors는 글 하나(The capital of France is Paris. 추론 엔진,
토큰 19개)를 모델에 넣었을 때의 계산 결과입니다.
| 이름 | 형상 | 내용 |
|---|---|---|
ids |
(19) |
토큰 번호 |
embed |
(19, 576) |
임베딩 |
l0.norm1, l0.q, l0.k, l0.v |
첫 블록의 정규화와 질의·키·값 | |
l0.attn_weights |
(9, 19, 19) |
첫 블록의 어텐션 비중 |
l0.attn, l0.norm2, l0.mlp, l0.out |
(19, 576) |
첫 블록의 나머지 부품 |
hidden_states |
(31, 19, 576) |
임베딩과 블록 각각의 출력. 마지막 것만은 마지막 정규화까지 거친 값(final_norm과 같음) |
final_norm |
(19, 576) |
마지막 정규화 |
logits |
(19, 49152) |
최종 점수 |
tokenizer_vectors.json은 원본 모델의 tokenizer.json을 Hugging Face
tokenizers로 읽어 여러 글을 토큰 번호로 바꾼 결과입니다(10장). 글의 시작을
알리는 토큰은 붙이지 않았습니다. reference/ch10_tokenizer_vectors.py smollm2_135m로
만들었습니다.
generation.json은 PyTorch로 글 두 개를 이어 쓴 결과입니다(11장). 매번 점수가
가장 높은 토큰을 골랐습니다. 넣은 토큰, 새로 뽑힌 토큰, 뽑을 때마다의 1위와
2위의 점수 차이가 들어 있습니다. reference/ch11_generation_reference.py smollm2_135m로
만들었습니다.
generation.json holds greedy continuations produced with PyTorch: the prompt
ids, the generated ids, and the top-1/top-2 logit margin at each step.
sampling.safetensors는 샘플링 설정 일곱 가지에 대해, 점수를 손질한 뒤의
확률분포를 transformers의 구현으로 계산한 것입니다(12장). 입력은
reference.safetensors의 마지막 줄의 점수이고 계산은 f64로 했습니다. 설정은
메타데이터의 cases에 JSON으로 적혀 있습니다.
reference/ch12_sampling_reference.py smollm2_135m로 만들었습니다.
sampling.safetensors holds next-token distributions after repetition
penalty, temperature, top-k and top-p, computed in float64 with the
transformers logits processors for the seven settings listed in its metadata.
logits.safetensors는 여러 엔진의 점수를 견줄 기준입니다(13장). 글
The capital of France is의 마지막 토큰 다음 점수를 가중치를 f64로 바꿔
계산했습니다. f64를 f32 둘(truth_hi + truth_lo)로 나누어 담았고, 평소처럼
f32로 계산한 값(pytorch_f32)도 함께 들어 있습니다.
reference/ch13_logits_reference.py smollm2_135m로 만들었습니다.
logits.safetensors holds next-token logits computed in float64 (split into
two float32 tensors, truth_hi + truth_lo) as ground truth for comparing
engines, together with the usual float32 result.
Note that the last entry of hidden_states has the final normalization
applied, as transformers returns it. tokenizer_vectors.json holds token ids
produced by the original tokenizer for a set of test strings.
저장소의 reference/ch09_llama_reference.py로 만들었습니다. transformers
5.23.0, torch 2.15.0, 가중치를 f32로 펼쳐 CPU에서 계산했습니다.
sh scripts/download_smollm2.sh
python reference/ch09_llama_reference.py smollm2_135m
원본 모델 SmolLM2-135M-Instruct는 Hugging Face TB가 Apache License 2.0으로 공개했습니다. 이 저장소의 파일은 그 모델로 계산한 값이며 같은 라이선스를 따릅니다.
The values were computed with SmolLM2-135M-Instruct, released by Hugging Face TB under the Apache License 2.0, and are distributed under the same license.
reference.safetensors 5 MBsampling.safetensors 1 MBlogits.safetensors 590.2 kB