adamjen commited on
Commit
e3c7b25
Β·
verified Β·
1 Parent(s): 27f5f5d

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +161 -3
README.md CHANGED
@@ -1,3 +1,161 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ base_model: mistralai/Devstral-Small-2-24B-Instruct-2512
4
+ datasets:
5
+ - nohurry/Opus-4.6-Reasoning-3000x-filtered
6
+ language:
7
+ - en
8
+ tags:
9
+ - mistral
10
+ - ministral3
11
+ - code
12
+ - reasoning
13
+ - lora
14
+ - gguf
15
+ - unsloth
16
+ - knowledge-distillation
17
+ pipeline_tag: text-generation
18
+ ---
19
+
20
+ # Devstral-Small-2-24B Opus Reasoning
21
+
22
+ A LoRA fine-tune of [Devstral-Small-2-24B](https://huggingface.co/mistralai/Devstral-Small-2-24B-Instruct-2512) distilled on Claude 4.6 Opus `<think>...</think>` reasoning traces. The goal: give Devstral's strong coding foundation explicit chain-of-thought reasoning before it writes code.
23
+
24
+ ## Model Details
25
+
26
+ | | |
27
+ |---|---|
28
+ | **Base model** | mistralai/Devstral-Small-2-24B-Instruct-2512 |
29
+ | **Fine-tune type** | QLoRA (4-bit NF4 base + BF16 LoRA adapters) |
30
+ | **LoRA rank** | r=16, alpha=16 |
31
+ | **Target modules** | q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj |
32
+ | **Training data** | nohurry/Opus-4.6-Reasoning-3000x-filtered (2,322 samples) |
33
+ | **Checkpoint used** | checkpoint-1200 (end of epoch 2 β€” best generalisation) |
34
+ | **Hardware** | RTX 3090 24GB VRAM |
35
+ | **Framework** | Unsloth 2026.3.10 + TRL SFTTrainer |
36
+ | **Sequence length** | 2048 |
37
+
38
+ ## Files
39
+
40
+ | File | Description |
41
+ |---|---|
42
+ | `adapter_model.safetensors` | LoRA adapter weights (~400MB) |
43
+ | `adapter_config.json` | LoRA config (rank, target modules, base model path) |
44
+ | `Devstral-Small-2-24B-Opus-Reasoning.Q4_K_M.gguf` | Quantised GGUF β€” ready for llama.cpp / Ollama / llama-swap |
45
+ | `Devstral-Small-2-24B-Opus-Reasoning.Q5_K_M.gguf` | Higher quality GGUF β€” recommended for local use |
46
+
47
+ ## Training Data
48
+
49
+ [nohurry/Opus-4.6-Reasoning-3000x-filtered](https://huggingface.co/datasets/nohurry/Opus-4.6-Reasoning-3000x-filtered) β€” 2,324 problems with Claude 4.6 Opus `<think>` reasoning traces and solutions, filtered to < 20,000 characters combined length.
50
+
51
+ Each sample was formatted as:
52
+ ```
53
+ [INST] {problem} [/INST]<think>
54
+ {thinking}
55
+ </think>
56
+
57
+ {solution}
58
+ ```
59
+
60
+ Loss was computed on the assistant turn only (`train_on_responses_only`).
61
+
62
+ ## Training Loss
63
+
64
+ | Step | Epoch | Loss |
65
+ |------|-------|------|
66
+ | 5 | 0.01 | 0.7949 |
67
+ | 100 | 0.17 | 0.5708 |
68
+ | 300 | 0.52 | 0.5800 |
69
+ | 600 | 1.03 | 0.3559 |
70
+ | 900 | 1.55 | 0.3858 |
71
+ | 1100 | 1.89 | 0.3469 |
72
+ | 1160 | 2.00 | 0.3752 |
73
+ | **1200** | **2.07** | **0.1493** |
74
+
75
+ Checkpoint 1200 (end of epoch 2) was selected over the full epoch 3 run β€” for reasoning distillation tasks, epoch 3 typically overfits to the trace style while epoch 2 gives the best generalisation.
76
+
77
+ ## Usage
78
+
79
+ ### GGUF (llama.cpp / Ollama / llama-swap)
80
+
81
+ Download `Devstral-Small-2-24B-Opus-Reasoning.Q5_K_M.gguf` for best quality, or `Devstral-Small-2-24B-Opus-Reasoning.Q4_K_M.gguf` if VRAM is tight.
82
+
83
+ ```bash
84
+ # llama.cpp
85
+ ./llama-cli -m unsloth.Q5_K_M.gguf \
86
+ --chat-template mistral \
87
+ -p "[INST] Write a Python function to find all prime numbers up to n using a sieve. [/INST]"
88
+ ```
89
+
90
+ ### LoRA Adapter (Python)
91
+
92
+ Requires the base model. Because Devstral is a VLM (Pixtral vision encoder), the easiest path is the text-only extracted weights β€” see the technical notes below.
93
+
94
+ ```python
95
+ import torch
96
+ from unsloth import FastLanguageModel
97
+ from peft import PeftModel
98
+
99
+ base_model_path = "path/to/Devstral-Small-2-24B-textonly" # see notes
100
+ adapter_path = "adamjen/Devstral-Small-2-24B-Opus-Reasoning"
101
+
102
+ model, tokenizer = FastLanguageModel.from_pretrained(
103
+ model_name = base_model_path,
104
+ max_seq_length = 2048,
105
+ dtype = torch.bfloat16,
106
+ load_in_4bit = True,
107
+ )
108
+ model = PeftModel.from_pretrained(model, adapter_path)
109
+
110
+ messages = [{"role": "user", "content": "Write a binary search in Python."}]
111
+ text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
112
+ inputs = tokenizer(text, return_tensors="pt").to("cuda")
113
+
114
+ outputs = model.generate(**inputs, max_new_tokens=2048, temperature=0.7)
115
+ print(tokenizer.decode(outputs[0], skip_special_tokens=True))
116
+ ```
117
+
118
+ ### Chat Template
119
+
120
+ This model uses Mistral's `[INST]...[/INST]` format. The model will produce a `<think>...</think>` block before its response.
121
+
122
+ ```
123
+ [INST] Your question here [/INST]<think>
124
+ ... reasoning ...
125
+ </think>
126
+
127
+ ... answer ...
128
+ ```
129
+
130
+ ## Technical Notes: The Devstral Extraction Problem
131
+
132
+ Devstral-Small-2-24B ships as a `Mistral3ForConditionalGeneration` (VLM) with a Pixtral vision encoder. Training it as a text-only model on a single 24GB GPU hits several problems:
133
+
134
+ - **FP8 weights**: The official instruct release uses FP8 quantisation, which requires compute capability β‰₯ 8.9. RTX 3090 is 8.6 β€” incompatible. Requires dequantising to BF16 first.
135
+ - **Vision encoder VRAM**: The Pixtral encoder consumes ~4GB VRAM, leaving insufficient headroom for 4-bit QLoRA + gradients.
136
+ - **Device map splitting**: With a VLM loaded via `device_map="auto"`, accelerate splits layers across GPU/CPU, breaking distributed training mode.
137
+ - **transformers 5.x concurrent loader**: The async tensor loader materialises all BF16 tensors simultaneously before quantisation β†’ OOM. Fix: `HF_DEACTIVATE_ASYNC_LOAD=1`.
138
+
139
+ **Solution**: Extract the `Ministral3ForCausalLM` language layers into a standalone text-only model directory (stripping `vision_tower.*` and `multi_modal_projector.*`, renaming `language_model.model.*` β†’ `model.*`). This produces a clean 23B causal LM loadable by `FastLanguageModel`.
140
+
141
+ Full write-up with all fixes: [Fine-tuning Devstral on an RTX 3090](https://adamjenner.com.au)
142
+
143
+ ## Hardware Requirements
144
+
145
+ | Format | Min VRAM |
146
+ |---|---|
147
+ | Q4_K_M GGUF | ~16GB |
148
+ | Q5_K_M GGUF | ~18GB |
149
+ | LoRA inference (4-bit) | ~20GB |
150
+ | LoRA training (QLoRA) | 24GB |
151
+
152
+ ## Limitations
153
+
154
+ - Trained on 2,322 samples β€” a small dataset. Performance gains on reasoning are real but limited in breadth.
155
+ - Max sequence length 2048 tokens (training constraint). Longer contexts may degrade quality.
156
+ - The `<think>` block reasoning style is inherited from Claude Opus traces β€” the model may produce verbose reasoning.
157
+ - Not evaluated on formal benchmarks.
158
+
159
+ ## Author
160
+
161
+ Adam Jenner β€” [adamjenner.com.au](https://adamjenner.com.au)