Llama-3.1-8B-Thinking-SFT
Llama-3.1-8B-Instruct fine-tuned with thinking-style SFT (~8.03B params, BF16, single 16 GB safetensors). The model opens assistant turns with a <think> … </think> reasoning block, then writes the final answer.
why Llama-3.1-8B
- this model isn't as good as modern models, but i like Llama. that is why i wanna finetune it. what i did is training 2 new tokens to teach model to think.
What changed vs. the base model
| | Base | This model |
|---|---|---|
| Thinking tokens | — | `<think>` = id **128002**, `</think>` = id **128003** |
| Token source | `<|reserved_special_token_0|>` / `<|reserved_special_token_1|>` | renamed in place — Qwen-style display names, token ids unchanged |
| Vocab size | 128,256 | 128,256 (**unchanged, no embedding resize**) |
| EOS | `<|eot_id|>` (128009) | unchanged — `</think>` does **not** end generation |
| PAD | `<|finetune_right_pad_id|>` (128004) | unchanged |
Because the two ids reuse the tokenizer's reserved special slots, this model is a drop-in replacement: same architecture, same config.json — any runtime that runs Llama 3.1 can run it, as long as it picks up this repo's tokenizer files and chat template (otherwise the model degrades to emitting literal <think> strings instead of the special tokens).
Chat template behavior
The shipped chat_template.jinja is the Llama 3.1 template with two additions:
- Thinking is on by default. With
add_generation_prompt=Truethe template prefills<think>after the assistant header, so the model only writes the closing</think>itself:apply_chat_template(..., add_generation_prompt=True)→…<|start_header_id|>assistant<|end_header_id|>\n\n<think> enable_thinking=Falseremoves the prefill — the model then decides on its own whether to open a thinking block.reasoning_contentsupport. Assistant messages carrying an OpenAI-stylereasoning_contentfield are rendered wrapped in<think>…</think>— handy for replaying teacher traces in multi-turn conversations.
To hard-disable thinking at inference time, suppress the open-token logit: logits[:, 128002] = -float("inf").
Usage
transformers
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
tok = AutoTokenizer.from_pretrained("wesjos/Llama-3.1-8B-Thinking-SFT")
model = AutoModelForCausalLM.from_pretrained(
"wesjos/Llama-3.1-8B-Thinking-SFT", dtype=torch.bfloat16, device_map="auto"
)
messages = [{"role": "user", "content": "what is 89x12"}]
text = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
# The rendered template already contains BOS — do not add special tokens again.
inputs = tok(text, return_tensors="pt", add_special_tokens=False).to(model.device)
out = model.generate(**inputs, max_new_tokens=1024, do_sample=True,
temperature=0.6, top_p=0.95, repetition_penalty=1.05)
print(tok.decode(out[0], skip_special_tokens=False))
Prompt / output shape (real sample, truncated):
<|begin_of_text|><|begin_of_text|><|start_header_id|>system<|end_header_id|>
Cutting Knowledge Date: December 2023
Today Date: 26 Jul 2024
you are a helpful assistant.<|eot_id|><|start_header_id|>user<|end_header_id|>
what do you know about yourself<|eot_id|><|start_header_id|>assistant<|end_header_id|>
<think>The user is asking what I know about myself. This is a meta-question about my own knowledge and capabilities. I should be transparent about what I can tell them.</think>I appreciate the question! Here's what I can share:
## What I Know About Myself
### Core Abilities
- **Language**: I understand natural language, generate text, and respond in many languages.
- **Knowledge**: I have been trained on a vast range of topics — history, science, technology, philosophy, culture, literature, and more.
- **Reasoning & Reasoning**: I can reason through logical steps, draw analogies, and make simple mathematical calculations.
### What I Don't Know (or struggle with)
- **Personal experiences**: I don't have feelings, memories, or a personal history.
- **Domain-specific expertise**: While broad, my knowledge has limits (especially very recent events or highly specialized fields like legal or medical practice without context).
- **Common sense in rare cases**: I may not always understand everyday situations that rely heavily on shared human experience (humor, nuanced social norms, etc.).
### How I Work
- I process input as text only.
- I generate responses based on patterns learned from training data.
- I can explain concepts, answer questions, write, code, summarize, translate, and more.
### Boundaries
- I'm an AI assistant, so I can make mistakes or not fully understand context.
- I don't have access to the internet in real-time after training.
- I should not be used for tasks requiring personal judgment or expertise beyond what I've been trained on.
Would you like me to help with something specific?<|eot_id|>
Serving notes
- vLLM / SGLang / llama.cpp: serve from this repo so the runtime picks up its
tokenizer.json/tokenizer_config.json/chat_template.jinja. With the stock Llama-3.1 tokenizer files the model outputs literal<think>text — the thinking tokens only exist in this repo's tokenizer. - Budget
max_new_tokensfor the reasoning block; the answer follows after</think>.
Sampling defaults (generation_config.json): temperature=0.6 · top_p=0.95 · top_k=0 (disabled) · repetition_penalty=1.05.
Training data
SFT only (no RL stage). The corpus is a chat-only cleaning of saidutta69/qwen-glm-kimi-distillation-clean, a teacher-distilled conversational corpus (Qwen3.8-Max / Kimi-K3):
| Traces | 57,064 → 51,150 after removing all tool-calling rows (−10.4%); original splits kept — train 46,077 / validation 2,558 / test 2,515 |
| Teachers (train split) | qwen3.8-max-preview 42,942 · kimi-code/k3 3,135 |
| Domains (train) | math 12.9k · code 12.7k · reasoning 9.4k · instruction 6.5k · other 4.5k |
| Chain-of-thought | ~82% of assistant turns carry an explicit reasoning trace, trained inside <think>…</think> |
Trained with Unsloth (the released config.json carries the Unsloth save markers).
Evaluation (author-run, same protocol for both models)
| Benchmark | Llama-3.1-8B-Instruct | This model |
|---|---|---|
| GSM8K (full test, 1,319 problems) | 75.94% | 84.38% |
| Hard-453 (453-problem hard set) | 72.63% | 72.63% |
model learned how to use thinking tokens but doesnt really improve in hard questions. maybe DPO or RL would help but i didnt try yet.
Limitations
- Task-dependent gains: no improvement on Hard-453; thinking mainly helped math word problems in this run.
- English-centric training corpus.
- Reasoning consumes token budget — size
max_new_tokensaccordingly. - Inherits all limitations of Llama-3.1-8B-Instruct (knowledge cutoff December 2023, hallucination, no real-time knowledge).
License & attribution
- Weights: Llama 3.1 Community License, inherited from
meta-llama/Llama-3.1-8B-Instruct. (Training started from theunsloth/Meta-Llama-3.1-8B-Instructrepack — identical weights, different default chat template.) - Training data: the upstream distillation corpus mixes non-commercial sources (CC BY-NC-SA 4.0, CC BY-NC 3.0, CC BY-SA 3.0, CC BY 4.0, MIT, Apache-2.0, ODC-BY) plus Alibaba Cloud Model Studio terms. To the extent this model derives from that data lineage, non-commercial research use only.