From 41c336165fb013a9ee49cda39266c7f7ab68b472 Mon Sep 17 00:00:00 2001 From: Niels Geens Date: Wed, 23 Apr 2025 16:37:10 +0000 Subject: [PATCH] Updated notebooks to support 4-bit quantization --- sia/llm_engine/qwq_llm_engine.ipynb | 116 +++++-- tools/train/train/qwq.ipynb | 519 ++++++++++++++++++++++++++-- 2 files changed, 589 insertions(+), 46 deletions(-) diff --git a/sia/llm_engine/qwq_llm_engine.ipynb b/sia/llm_engine/qwq_llm_engine.ipynb index 3667e1d..6d27632 100644 --- a/sia/llm_engine/qwq_llm_engine.ipynb +++ b/sia/llm_engine/qwq_llm_engine.ipynb @@ -2,9 +2,20 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.\n", + "Unsloth: Failed to patch Gemma3ForConditionalGeneration.\n", + "🦥 Unsloth Zoo will now patch everything to make training faster!\n", + "INFO 04-23 16:36:10 [__init__.py:239] Automatically detected platform cuda.\n" + ] + } + ], "source": [ "# Unsloth should be imported before transformers to ensure all optimizations are applied.\n", "from unsloth import FastLanguageModel" @@ -12,7 +23,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -24,18 +35,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "temperature = 0.6\n", - "model_path = \"/root/models/notebook\"\n", + "model_path = \"/root/models/notebook_merged_4bit\"\n", "xml_schema_text = Path(\"/root/sia/action_schema.xsd\").read_text()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ @@ -47,24 +58,74 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "==((====))== Unsloth 2025.3.19: Fast Qwen2 patching. Transformers: 4.51.3. vLLM: 0.8.2.\n", + " \\\\ /| NVIDIA RTX 6000 Ada Generation. Num GPUs = 1. Max memory: 47.5 GB. Platform: Linux.\n", + "O^O/ \\_/ \\ Torch: 2.6.0+cu124. CUDA: 8.9. CUDA Toolkit: 12.4. Triton: 3.2.0\n", + "\\ / Bfloat16 = TRUE. FA [Xformers = 0.0.29.post2. FA2 = False]\n", + " \"-____-\" Free license: http://github.com/unslothai/unsloth\n", + "Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored!\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Sliding Window Attention is enabled but not implemented for `eager`; unexpected results may be encountered.\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "validate_env\n", + "device_map sequential\n", + "validate_env\n", + "device_map OrderedDict([('', 0)])\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "a99acfd42fa645abae77144125a734b7", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Loading checkpoint shards: 0%| | 0/4 [00:00\n", + "Hello! I'm just a large language model, so I don't have feelings, but thank you for asking. How can I assist you today?<|im_end|>" + ] + } + ], "source": [ "for text in streamer:\n", " print(text, end=\"\")" @@ -172,7 +242,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -185,7 +255,7 @@ "kernelspec": { "display_name": "sia", "language": "python", - "name": "sia" + "name": "python3" }, "language_info": { "codemirror_mode": { diff --git a/tools/train/train/qwq.ipynb b/tools/train/train/qwq.ipynb index c88dd5e..932100f 100644 --- a/tools/train/train/qwq.ipynb +++ b/tools/train/train/qwq.ipynb @@ -2,9 +2,20 @@ "cells": [ { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.\n", + "Unsloth: Failed to patch Gemma3ForConditionalGeneration.\n", + "🦥 Unsloth Zoo will now patch everything to make training faster!\n", + "INFO 04-23 16:23:47 [__init__.py:239] Automatically detected platform cuda.\n" + ] + } + ], "source": [ "# Unsloth should be imported before transformers to ensure all optimizations are applied.\n", "from unsloth import FastLanguageModel, is_bfloat16_supported" @@ -12,7 +23,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -28,7 +39,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -37,18 +48,67 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "args = qwq.Args([\"--output-dir\", \"/root/models/notebook\"])" + "args = qwq.Args([\"--output-dir\", \"/root/models/notebook\", \"--base-model\", \"unsloth/QwQ-32B-bnb-4bit\"])" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Validating 20 XML files...\n", + "file: /root/sia/training/clean_start/iteration_20250116_134549_655.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/clean_start/iteration_20250116_134549_655.xml\n", + "file: /root/sia/training/clean_start/iteration_20250116_134555_680.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/clean_start/iteration_20250116_134555_680.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141241_092.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141241_092.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141252_317.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141252_317.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141302_940.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141302_940.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141329_886.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141329_886.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141343_416.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141343_416.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141357_412.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141357_412.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141410_965.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141410_965.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141428_204.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141428_204.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141441_443.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141441_443.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141447_231.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141447_231.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141454_509.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141454_509.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141458_495.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141458_495.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141503_889.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141503_889.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141516_718.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141516_718.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141533_231.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141533_231.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141603_549.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141603_549.xml\n", + "file: /root/sia/training/delete_indicated_entries/iteration_20250116_141633_083.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/delete_indicated_entries/iteration_20250116_141633_083.xml\n", + "file: /root/sia/training/list_entries_to_delete/iteration_20250116_141227_271.xml\n", + "WARNING: System prompt hash mismatch in /root/sia/training/list_entries_to_delete/iteration_20250116_141227_271.xml\n", + "Validation complete. Found 20 valid files.\n" + ] + } + ], "source": [ "dataset = qwq.Dataset(args.config_path)\n", "dataset.validate()" @@ -56,9 +116,94 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "b72535c55d214b9da158b90ab0d3e65a", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "tokenizer_config.json: 0%| | 0.00/8.14k [00:00\n", + " \n", + " \n", + " [3/3 01:48, Epoch 1/3]\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
StepTraining Loss

" + ], + "text/plain": [ + "" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "TrainOutput(global_step=3, training_loss=1.7701025009155273, metrics={'train_runtime': 187.628, 'train_samples_per_second': 0.32, 'train_steps_per_second': 0.016, 'total_flos': 1.41437049721344e+16, 'train_loss': 1.7701025009155273})" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "trainer.train()" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Unsloth: Merging 4bit and LoRA weights to 4bit...\n", + "This might take 5 minutes...\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/root/venvs/train/lib/python3.10/site-packages/peft/tuners/lora/bnb.py:351: UserWarning: Merge lora module to 4-bit linear may get different generations due to rounding errors.\n", + " warnings.warn(\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Done.\n", + "Unsloth: Saving tokenizer... Done.\n", + "Unsloth: Saving model... This might take 10 minutes for Llama-7b... Done.\n" + ] + } + ], "source": [ "model.save_pretrained_merged(\n", - " str(args.output_dir), \n", + " str(args.output_dir) + \"_merged_4bit\", \n", " tokenizer=tokenizer,\n", " save_method=\"merged_4bit_forced\"\n", ")"