Skip to content

Commit 43d3dc1

Browse files
Merge pull request #136 from unslothai/fix-magistral-dataset-mapping
fix dataset formatting and mapping for Magistral reasoning
2 parents dc6acd0 + e1b249d commit 43d3dc1

File tree

4 files changed

+69
-78
lines changed

4 files changed

+69
-78
lines changed

‎nb/Kaggle-Magistral_(24B)-Reasoning-Conversational.ipynb‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -575,16 +575,14 @@
575575
},
576576
"outputs": [],
577577
"source": [
578-
"def generate_conversation(examples):\n",
579-
" problems = examples[\"problem\"]\n",
580-
" solutions = examples[\"generated_solution\"]\n",
581-
" conversations = []\n",
582-
" for problem, solution in zip(problems, solutions):\n",
583-
" conversations.append([\n",
578+
"def generate_conversation(example):\n",
579+
" problem = example[\"problem\"]\n",
580+
" solution = example[\"generated_solution\"]\n",
581+
" conversation = [\n",
584582
" {\"role\" : \"user\", \"content\" : problem},\n",
585583
" {\"role\" : \"assistant\", \"content\" : solution},\n",
586-
" ])\n",
587-
" return { \"conversations\": conversations, }"
584+
" ]\n",
585+
" return { \"conversations\": conversation, }"
588586
]
589587
},
590588
{
@@ -628,10 +626,10 @@
628626
}
629627
],
630628
"source": [
631-
"reasoning_conversations = tokenizer.apply_chat_template(\n",
632-
" reasoning_dataset.map(generate_conversation, batched = True)[\"conversations\"],\n",
633-
" tokenize = False,\n",
634-
")"
629+
"reasoning_conversations = [tokenizer.apply_chat_template(\n",
630+
" conv[\"conversations\"],tokenize = False,)\n",
631+
" for conv in reasoning_dataset.map(generate_conversation)\n",
632+
"]"
635633
]
636634
},
637635
{
@@ -6420,4 +6418,4 @@
64206418
},
64216419
"nbformat": 4,
64226420
"nbformat_minor": 0
6423-
}
6421+
}

‎nb/Magistral_(24B)-Reasoning-Conversational.ipynb‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -575,16 +575,14 @@
575575
},
576576
"outputs": [],
577577
"source": [
578-
"def generate_conversation(examples):\n",
579-
" problems = examples[\"problem\"]\n",
580-
" solutions = examples[\"generated_solution\"]\n",
581-
" conversations = []\n",
582-
" for problem, solution in zip(problems, solutions):\n",
583-
" conversations.append([\n",
578+
"def generate_conversation(example):\n",
579+
" problem = example[\"problem\"]\n",
580+
" solution = example[\"generated_solution\"]\n",
581+
" conversation = [\n",
584582
" {\"role\" : \"user\", \"content\" : problem},\n",
585583
" {\"role\" : \"assistant\", \"content\" : solution},\n",
586-
" ])\n",
587-
" return { \"conversations\": conversations, }"
584+
" ]\n",
585+
" return { \"conversations\": conversation, }"
588586
]
589587
},
590588
{
@@ -628,10 +626,10 @@
628626
}
629627
],
630628
"source": [
631-
"reasoning_conversations = tokenizer.apply_chat_template(\n",
632-
" reasoning_dataset.map(generate_conversation, batched = True)[\"conversations\"],\n",
633-
" tokenize = False,\n",
634-
")"
629+
"reasoning_conversations = [tokenizer.apply_chat_template(\n",
630+
" conv[\"conversations\"],tokenize = False,)\n",
631+
" for conv in reasoning_dataset.map(generate_conversation)\n",
632+
"]"
635633
]
636634
},
637635
{
@@ -6420,4 +6418,4 @@
64206418
},
64216419
"nbformat": 4,
64226420
"nbformat_minor": 0
6423-
}
6421+
}

‎original_template/Magistral_(24B)-Reasoning-Conversational.ipynb‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -552,17 +552,15 @@
552552
},
553553
"outputs": [],
554554
"source": [
555-
"def generate_conversation(examples):\n",
556-
" problems = examples[\"problem\"]\n",
557-
" solutions = examples[\"generated_solution\"]\n",
558-
" conversations = []\n",
559-
" for problem, solution in zip(problems, solutions):\n",
560-
" conversations.append([\n",
561-
" {\"role\" : \"user\", \"content\" : problem},\n",
562-
" {\"role\" : \"assistant\", \"content\" : solution},\n",
563-
" ])\n",
564-
" return { \"conversations\": conversations, }"
565-
]
555+
"def generate_conversation(example):\n",
556+
" problem = example[\"problem\"]\n",
557+
" solution = example[\"generated_solution\"]\n",
558+
" conversation = [\n",
559+
" {\"role\" : \"user\", \"content\" : problem},\n",
560+
" {\"role\" : \"assistant\", \"content\" : solution},\n",
561+
" ]\n",
562+
" return { \"conversations\": conversation, }"
563+
]
566564
},
567565
{
568566
"cell_type": "code",
@@ -605,11 +603,11 @@
605603
}
606604
],
607605
"source": [
608-
"reasoning_conversations = tokenizer.apply_chat_template(\n",
609-
" reasoning_dataset.map(generate_conversation, batched = True)[\"conversations\"],\n",
610-
" tokenize = False,\n",
611-
")"
612-
]
606+
"reasoning_conversations = [tokenizer.apply_chat_template(\n",
607+
" conv[\"conversations\"],tokenize = False,)\n",
608+
" for conv in reasoning_dataset.map(generate_conversation)\n",
609+
"]"
610+
]
613611
},
614612
{
615613
"cell_type": "markdown",

‎python_scripts/Magistral_(24B)-Reasoning-Conversational.py‎

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@
77
# <a href="https://discord.gg/unsloth"><img src="https://github.com/unslothai/unsloth/raw/main/images/Discord button.png" width="145"></a>
88
# <a href="https://docs.unsloth.ai/"><img src="https://github.com/unslothai/unsloth/blob/main/images/documentation%20green%20button.png?raw=true" width="125"></a></a> Join Discord if you need help + ⭐ <i>Star us on <a href="https://github.com/unslothai/unsloth">Github</a> </i> ⭐
99
# </div>
10-
#
10+
#
1111
# To install Unsloth your local device, follow [our guide](https://docs.unsloth.ai/get-started/install-and-update). This notebook is licensed [LGPL-3.0](https://github.com/unslothai/notebooks?tab=LGPL-3.0-1-ov-file#readme).
12-
#
12+
#
1313
# You will learn how to do [data prep](#Data), how to [train](#Train), how to [run the model](#Inference), & [how to save it](#Save)
14-
#
14+
#
1515

1616
# ### News
1717

18-
#
18+
#
1919
# Unsloth's [Docker image](https://hub.docker.com/r/unsloth/unsloth) is here! Start training with no setup & environment issues. [Read our Guide](https://docs.unsloth.ai/new/how-to-train-llms-with-unsloth-and-docker).
20-
#
20+
#
2121
# [gpt-oss RL](https://docs.unsloth.ai/new/gpt-oss-reinforcement-learning) is now supported with the fastest inference & lowest VRAM. Try our [new notebook](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/gpt-oss-(20B)-GRPO.ipynb) which creates kernels!
22-
#
22+
#
2323
# Introducing [Vision](https://docs.unsloth.ai/new/vision-reinforcement-learning-vlm-rl) and [Standby](https://docs.unsloth.ai/basics/memory-efficient-rl) for RL! Train Qwen, Gemma etc. VLMs with GSPO - even faster with less VRAM.
24-
#
24+
#
2525
# Unsloth now supports Text-to-Speech (TTS) models. Read our [guide here](https://docs.unsloth.ai/basics/text-to-speech-tts-fine-tuning).
26-
#
26+
#
2727
# Visit our docs for all our [model uploads](https://docs.unsloth.ai/get-started/all-our-models) and [notebooks](https://docs.unsloth.ai/get-started/unsloth-notebooks).
28-
#
28+
#
2929

3030
# # ### Installation
31-
#
31+
#
3232
# # In[ ]:
33-
#
34-
#
33+
#
34+
#
3535
# get_ipython().run_cell_magic('capture', '', 'import os, re\nif "COLAB_" not in "".join(os.environ.keys()):\n !pip install unsloth\nelse:\n # Do this only in Colab notebooks! Otherwise use pip install unsloth\n import torch; v = re.match(r"[0-9\\.]{3,}", str(torch.__version__)).group(0)\n xformers = "xformers==" + ("0.0.32.post2" if v == "2.8.0" else "0.0.29.post3")\n !pip install --no-deps bitsandbytes accelerate {xformers} peft trl triton cut_cross_entropy unsloth_zoo\n !pip install sentencepiece protobuf "datasets>=3.4.1,<4.0.0" "huggingface_hub>=0.34.0" hf_transfer\n !pip install --no-deps unsloth\n!pip install transformers==4.56.2\n!pip install --no-deps trl==0.22.2\n')
36-
#
37-
#
36+
#
37+
#
3838
# # ### Unsloth
3939

4040
# In[ ]:
@@ -114,25 +114,22 @@
114114
# In[8]:
115115

116116

117-
def generate_conversation(examples):
118-
problems = examples["problem"]
119-
solutions = examples["generated_solution"]
120-
conversations = []
121-
for problem, solution in zip(problems, solutions):
122-
conversations.append([
117+
def generate_conversation(example):
118+
problem = example["problem"]
119+
solution = example["generated_solution"]
120+
conversation = [
123121
{"role" : "user", "content" : problem},
124122
{"role" : "assistant", "content" : solution},
125-
])
126-
return { "conversations": conversations, }
123+
]
124+
return { "conversations": conversation }
127125

128126

129127
# In[9]:
130128

131129

132-
reasoning_conversations = tokenizer.apply_chat_template(
133-
reasoning_dataset.map(generate_conversation, batched = True)["conversations"],
134-
tokenize = False,
135-
)
130+
reasoning_conversations = [
131+
tokenizer.apply_chat_template(conv["conversations"],tokenize = False,) for conv in reasoning_dataset.map(generate_conversation)
132+
]
136133

137134

138135
# Let's see the first transformed row:
@@ -261,7 +258,7 @@ def generate_conversation(examples):
261258
# <a name="Save"></a>
262259
# ### Saving, loading finetuned models
263260
# To save the final model as LoRA adapters, either use Huggingface's `push_to_hub` for an online save or `save_pretrained` for a local save.
264-
#
261+
#
265262
# **[NOTE]** This ONLY saves the LoRA adapters, and not the full model. To save to 16bit or GGUF, scroll down!
266263

267264
# In[21]:
@@ -288,7 +285,7 @@ def generate_conversation(examples):
288285

289286

290287
# ### Saving to float16 for VLLM
291-
#
288+
#
292289
# We also support saving to `float16` directly. Select `merged_16bit` for float16 or `merged_4bit` for int4. We also allow `lora` adapters as a fallback. Use `push_to_hub_merged` to upload to your Hugging Face account! You can go to https://huggingface.co/settings/tokens for your personal tokens.
293290

294291
# In[ ]:
@@ -317,12 +314,12 @@ def generate_conversation(examples):
317314

318315
# ### GGUF / llama.cpp Conversion
319316
# To save to `GGUF` / `llama.cpp`, we support it natively now! We clone `llama.cpp` and we default save it to `q8_0`. We allow all methods like `q4_k_m`. Use `save_pretrained_gguf` for local saving and `push_to_hub_gguf` for uploading to HF.
320-
#
317+
#
321318
# Some supported quant methods (full list on our [Wiki page](https://github.com/unslothai/unsloth/wiki#gguf-quantization-options)):
322319
# * `q8_0` - Fast conversion. High resource use, but generally acceptable.
323320
# * `q4_k_m` - Recommended. Uses Q6_K for half of the attention.wv and feed_forward.w2 tensors, else Q4_K.
324321
# * `q5_k_m` - Recommended. Uses Q6_K for half of the attention.wv and feed_forward.w2 tensors, else Q5_K.
325-
#
322+
#
326323
# [**NEW**] To finetune and auto export to Ollama, try our [Ollama notebook](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Llama3_(8B)-Ollama.ipynb)
327324

328325
# In[24]:
@@ -359,22 +356,22 @@ def generate_conversation(examples):
359356

360357

361358
# Now, use the `model-unsloth.gguf` file or `model-unsloth-Q4_K_M.gguf` file in llama.cpp.
362-
#
359+
#
363360
# And we're done! If you have any questions on Unsloth, we have a [Discord](https://discord.gg/unsloth) channel! If you find any bugs or want to keep updated with the latest LLM stuff, or need help, join projects etc, feel free to join our Discord!
364-
#
361+
#
365362
# Some other links:
366363
# 1. Train your own reasoning model - Llama GRPO notebook [Free Colab](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Llama3.1_(8B)-GRPO.ipynb)
367364
# 2. Saving finetunes to Ollama. [Free notebook](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Llama3_(8B)-Ollama.ipynb)
368365
# 3. Llama 3.2 Vision finetuning - Radiography use case. [Free Colab](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Llama3.2_(11B)-Vision.ipynb)
369366
# 6. See notebooks for DPO, ORPO, Continued pretraining, conversational finetuning and more on our [documentation](https://docs.unsloth.ai/get-started/unsloth-notebooks)!
370-
#
367+
#
371368
# <div class="align-center">
372369
# <a href="https://unsloth.ai"><img src="https://github.com/unslothai/unsloth/raw/main/images/unsloth%20new%20logo.png" width="115"></a>
373370
# <a href="https://discord.gg/unsloth"><img src="https://github.com/unslothai/unsloth/raw/main/images/Discord.png" width="145"></a>
374371
# <a href="https://docs.unsloth.ai/"><img src="https://github.com/unslothai/unsloth/blob/main/images/documentation%20green%20button.png?raw=true" width="125"></a>
375-
#
372+
#
376373
# Join Discord if you need help + ⭐️ <i>Star us on <a href="https://github.com/unslothai/unsloth">Github</a> </i> ⭐️
377374
# </div>
378-
#
375+
#
379376
# This notebook and all Unsloth notebooks are licensed [LGPL-3.0](https://github.com/unslothai/notebooks?tab=LGPL-3.0-1-ov-file#readme).
380-
#
377+
#

0 commit comments

Comments
 (0)