Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

example: unsloth (llama 3.1 8B LoRA + inference) (#2263)

Akshay Agrawal342cd62e5429
diff --git a/examples/third_party/unsloth/llama_3_1_8b_2x_faster_finetuning.py b/examples/third_party/unsloth/llama_3_1_8b_2x_faster_finetuning.py
new file mode 100644
index 0000000000..8cd4a5d6ea
--- /dev/null
+++ b/examples/third_party/unsloth/llama_3_1_8b_2x_faster_finetuning.py
@@ -0,0 +1,404 @@
+# /// script
+# requires-python = ">=3.10"
+# dependencies = [
+# "accelerate",
+# "bitsandbytes",
+# "marimo",
+# "peft",
+# "torch",
+# "triton",
+# "trl",
+# "xformers",
+# ]
+# ///
+import marimo
+
+__generated_with = "0.8.13"
+app = marimo.App(width="full")
+
+
+def __():
+ import marimo as mo
+ return mo,
+
+
+def __():
+ from unsloth import FastLanguageModel
+ from transformers import TextStreamer
+
+ import torch
+ return FastLanguageModel, TextStreamer, torch
+
+
[email protected](hide_code=True)
+def __(mo):
+ mo.md(
+ """
+ <span class="align-center" style="display:flex; flex-direction:row; gap: 16px">
+ <a href="https://github.com/unslothai/unsloth"><img src="https://github.com/unslothai/unsloth/raw/main/images/unsloth%20new%20logo.png" width="115"></a>
+ <a href="https://discord.gg/u54VK8m8tk"><img src="https://github.com/unslothai/unsloth/raw/main/images/Discord button.png" width="145"></a>
+ <a href="https://ko-fi.com/unsloth"><img src="https://github.com/unslothai/unsloth/raw/main/images/Kofi button.png" width="145"></a></a>
+ </span>
+ This example shows how to use [Unsloth](https://github.com/unslothai/unsloth) to finetune Llama-3.1 8b. It uses
+ marimo's UI elements to make the experience interactive, so you don't need to edit any code!
+
+ _Join Discord if you need help + ⭐ Star us on <a href="https://github.com/unslothai/unsloth">Github</a>⭐_
+
+ To install Unsloth on your own computer, follow the installation instructions on our Github page [here](https://github.com/unslothai/unsloth?tab=readme-ov-file#-installation-instructions).
+
+ You will learn how to do [data prep](#Data), how to [train](#Train), how to [run the model](#Inference), & [how to save it](#Save) (eg for Llama.cpp).
+
+ [NEW] Llama-3.1 8b, 70b & 405b are trained on a crazy 15 trillion tokens with 128K long context lengths!
+
+ **[NEW] Try 2x faster inference in a free Colab for Llama-3.1 8b Instruct [here](https://colab.research.google.com/drive/1T-YBVfnphoVc8E2E854qF3jdia2Ll2W2?usp=sharing)**
+ """
+ )
+ return
+
+
[email protected](hide_code=True)
+def __(mo):
+ mo.md(
+ r"""
+ * We support Llama, Mistral, Phi-3, Gemma, Yi, DeepSeek, Qwen, TinyLlama, Vicuna, Open Hermes etc
+ * We support 16bit LoRA or 4bit QLoRA. Both 2x faster.
+ * `max_seq_length` can be set to anything, since we do automatic RoPE Scaling via [kaiokendev's](https://kaiokendev.github.io/til) method.
+ * [**NEW**] We make Gemma-2 9b / 27b **2x faster**! See our [Gemma-2 9b notebook](https://colab.research.google.com/drive/1vIrqH5uYDQwsJ4-OO3DErvuv4pBgVwk4?usp=sharing)
+ * [**NEW**] To finetune and auto export to Ollama, try our [Ollama notebook](https://colab.research.google.com/drive/1WZDi7APtQ9VsvOrQSSC5DDtxq159j8iZ?usp=sharing)
+ * [**NEW**] We make Mistral NeMo 12B 2x faster and fit in under 12GB of VRAM! [Mistral NeMo notebook](https://colab.research.google.com/drive/17d3U-CAIwzmbDRqbZ9NnpHxCkmXB6LZ0?usp=sharing)
+ """
+ )
+ return
+
+
+def __(mo):
+ mo.md("### Load the model")
+ return
+
+
[email protected](hide_code=True)
+def __(mo):
+ mo.md("""Start by choosing a couple of parameters:""")
+ return
+
+
[email protected](hide_code=True)
+def __(mo):
+ max_seq_length = mo.ui.number(
+ value=2048, start=1, stop=4096, label="Max sequence length"
+ )
+
+ load_in_4bit = mo.ui.checkbox(value=True, label="Load in 4-bit?")
+
+ mo.vstack([max_seq_length, load_in_4bit])
+ return load_in_4bit, max_seq_length
+
+
+def __():
+ dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
+ return dtype,
+
+
[email protected](hide_code=True)
+def __(mo):
+ mo.md(r"""We now add LoRA adapters so we only need to update 1 to 10% of all parameters!""")
+ return
+
+
+def __(FastLanguageModel, dtype, load_in_4bit, max_seq_length):
+ model, tokenizer = FastLanguageModel.from_pretrained(
+ model_name="unsloth/Meta-Llama-3.1-8B",
+ max_seq_length=max_seq_length.value,
+ dtype=dtype,
+ load_in_4bit=load_in_4bit.value,
+ # token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf
+ )
+ model = FastLanguageModel.get_peft_model(
+ model,
+ r=16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
+ target_modules=[
+ "q_proj",
+ "k_proj",
+ "v_proj",
+ "o_proj",
+ "gate_proj",
+ "up_proj",
+ "down_proj",
+ ],
+ lora_alpha=16,
+ lora_dropout=0, # Supports any, but = 0 is optimized
+ bias="none", # Supports any, but = "none" is optimized
+ # [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
+ use_gradient_checkpointing="unsloth", # True or "unsloth" for very long context
+ random_state=3407,
+ use_rslora=False, # We support rank stabilized LoRA
+ loftq_config=None, # And LoftQ
+ )
+ return model, tokenizer
+
+
[email protected](hide_code=True)
+def __(mo):
+ mo.md(
+ r"""
+ <a name="Data"></a>
+ ### Data Prep
+ We now use the Alpaca dataset from [yahma](https://huggingface.co/datasets/yahma/alpaca-cleaned), which is a filtered version of 52K of the original [Alpaca dataset](https://crfm.stanford.edu/2023/03/13/alpaca.html). You can replace this code section with your own data prep.
+
+ **[NOTE]** To train only on completions (ignoring the user's input) read TRL's docs [here](https://huggingface.co/docs/trl/sft_trainer#train-on-completions-only).
+
+ **[NOTE]** Remember to add the **EOS_TOKEN** to the tokenized output!! Otherwise you'll get infinite generations!
+
+ If you want to use the `llama-3` template for ShareGPT datasets, try our conversational [notebook](https://colab.research.google.com/drive/1XamvWYinY6FOSX9GLvnqSjjsNflxdhNc?usp=sharing).
+
+ For text completions like novel writing, try this [notebook](https://colab.research.google.com/drive/1ef-tab5bhkvWmBOObepl1WgJvfvSzn5Q?usp=sharing).
+ """
+ )
+ return
+
+
+def __(tokenizer):
+ alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
+
+ ### Instruction:
+ {}
+
+ ### Input:
+ {}
+
+ ### Response:
+ {}"""
+
+ EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN
+
+
+ def formatting_prompts_func(examples):
+ instructions = examples["instruction"]
+ inputs = examples["input"]
+ outputs = examples["output"]
+ texts = []
+ for instruction, input, output in zip(instructions, inputs, outputs):
+ # Must add EOS_TOKEN, otherwise your generation will go on forever!
+ text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN
+ texts.append(text)
+ return {
+ "text": texts,
+ }
+
+
+ pass
+
+ from datasets import load_dataset
+
+ dataset = load_dataset("yahma/alpaca-cleaned", split="train")
+ dataset = dataset.map(
+ formatting_prompts_func,
+ batched=True,
+ )
+ return (
+ EOS_TOKEN,
+ alpaca_prompt,
+ dataset,
+ formatting_prompts_func,
+ load_dataset,
+ )
+
+
[email protected](hide_code=True)
+def __(mo):
+ mo.md(
+ r"""
+ ### Train the model
+ Now let's use Huggingface TRL's `SFTTrainer`! More docs here: [TRL SFT docs](https://huggingface.co/docs/trl/sft_trainer). We do 60 steps to speed things up, but you can set `num_train_epochs=1` for a full run, and turn off `max_steps=None`. We also support TRL's `DPOTrainer`!
+ """
+ )
+ return
+
+
[email protected](hide_code=True)
+def __(dataset, max_seq_length, model, tokenizer):
+ from trl import SFTTrainer
+ from transformers import TrainingArguments
+ from unsloth import is_bfloat16_supported
+
+ trainer = SFTTrainer(
+ model=model,
+ tokenizer=tokenizer,
+ train_dataset=dataset,
+ dataset_text_field="text",
+ max_seq_length=max_seq_length,
+ dataset_num_proc=2,
+ packing=False, # Can make training 5x faster for short sequences.
+ args=TrainingArguments(
+ per_device_train_batch_size=2,
+ gradient_accumulation_steps=4,
+ warmup_steps=5,
+ # num_train_epochs = 1, # Set this for 1 full training run.
+ max_steps=60,
+ learning_rate=2e-4,
+ fp16=not is_bfloat16_supported(),
+ bf16=is_bfloat16_supported(),
+ logging_steps=1,
+ optim="adamw_8bit",
+ weight_decay=0.01,
+ lr_scheduler_type="linear",
+ seed=3407,
+ output_dir="outputs",
+ ),
+ )
+ return SFTTrainer, TrainingArguments, is_bfloat16_supported, trainer
+
+
[email protected](hide_code=True)
+def __(torch):
+ # @title Show current memory stats
+ gpu_stats = torch.cuda.get_device_properties(0)
+ start_gpu_memory = round(
+ torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, 3
+ )
+ max_memory = round(gpu_stats.total_memory / 1024 / 1024 / 1024, 3)
+ print(f"GPU = {gpu_stats.name}. Max memory = {max_memory} GB.")
+ print(f"{start_gpu_memory} GB of memory reserved.")
+ return gpu_stats, max_memory, start_gpu_memory
+
+
+def __(mo):
+ train_button = mo.ui.run_button(label="Click to train!", kind="danger"); train_button.center()
+ return train_button,
+
+
+def __(train_button, trainer):
+ if train_button.value:
+ trainer_stats = trainer.train()
+ else:
+ trainer_stats = None
+ return trainer_stats,
+
+
+def __(max_memory, mo, start_gpu_memory, torch, trainer_stats):
+ mo.stop(trainer_stats is None, mo.md("Train the model 👆"))
+
+ # Show final memory and time stats
+ used_memory = round(torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, 3)
+ used_memory_for_lora = round(used_memory - start_gpu_memory, 3)
+ used_percentage = round(used_memory / max_memory * 100, 3)
+ lora_percentage = round(used_memory_for_lora / max_memory * 100, 3)
+ print(f"{trainer_stats.metrics['train_runtime']} seconds used for training.")
+ print(
+ f"{round(trainer_stats.metrics['train_runtime']/60, 2)} minutes used for training."
+ )
+ print(f"Peak reserved memory = {used_memory} GB.")
+ print(f"Peak reserved memory for training = {used_memory_for_lora} GB.")
+ print(f"Peak reserved memory % of max memory = {used_percentage} %.")
+ print(
+ f"Peak reserved memory for training % of max memory = {lora_percentage} %."
+ )
+ return (
+ lora_percentage,
+ used_memory,
+ used_memory_for_lora,
+ used_percentage,
+ )
+
+
[email protected](hide_code=True)
+def __(mo):
+ mo.md(
+ r"""
+ <a name="Inference"></a>
+ ### Inference
+ Let's run the model! You can change the instruction and input - leave the output blank!
+
+ **[NEW] Try 2x faster inference in a free Colab for Llama-3.1 8b Instruct [here](https://colab.research.google.com/drive/1T-YBVfnphoVc8E2E854qF3jdia2Ll2W2?usp=sharing)**
+ """
+ )
+ return
+
+
+def __(mo):
+ inf_instr_inp = mo.md(
+ """
+ {instr}
+
+ {inp}
+ """
+ ).batch(
+ instr=mo.ui.text_area(placeholder="Your instruction ..."),
+ inp=mo.ui.text_area(placeholder="Your input ..."),
+ ).form()
+
+
+ inf_instr_inp
+ return inf_instr_inp,
+
+
+def __(
+ FastLanguageModel,
+ alpaca_prompt,
+ inf_instr_inp,
+ mo,
+ model,
+ tokenizer,
+):
+ mo.stop(inf_instr_inp.value is None)
+
+ FastLanguageModel.for_inference(model) # Enable native 2x faster inference
+ _inputs = tokenizer(
+ [
+ alpaca_prompt.format(
+ inf_instr_inp.value["instr"],
+ inf_instr_inp.value["inp"],
+ "", # output - leave this blank for generation!
+ )
+ ],
+ return_tensors="pt",
+ ).to("cuda")
+
+ outputs = model.generate(**_inputs, max_new_tokens=64, use_cache=True)
+ tokenizer.batch_decode(outputs)
+ return outputs,
+
+
[email protected](hide_code=True)
+def __(mo):
+ mo.md(
+ r"""
+ And we're done! If you have any questions on Unsloth, we have a [Discord](https://discord.gg/u54VK8m8tk) 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!
+
+ Some other links:
+ 1. Zephyr DPO 2x faster [free Colab](https://colab.research.google.com/drive/15vttTpzzVXv_tJwEk-hIcQ0S9FcEWvwP?usp=sharing)
+ 2. Llama 7b 2x faster [free Colab](https://colab.research.google.com/drive/1lBzz5KeZJKXjvivbYvmGarix9Ao6Wxe5?usp=sharing)
+ 3. TinyLlama 4x faster full Alpaca 52K in 1 hour [free Colab](https://colab.research.google.com/drive/1AZghoNBQaMDgWJpi4RbffGM1h6raLUj9?usp=sharing)
+ 4. CodeLlama 34b 2x faster [A100 on Colab](https://colab.research.google.com/drive/1y7A0AxE3y8gdj4AVkl2aZX47Xu3P1wJT?usp=sharing)
+ 5. Mistral 7b [free Kaggle version](https://www.kaggle.com/code/danielhanchen/kaggle-mistral-7b-unsloth-notebook)
+ 6. We also did a [blog](https://huggingface.co/blog/unsloth-trl) with 🤗 HuggingFace, and we're in the TRL [docs](https://huggingface.co/docs/trl/main/en/sft_trainer#accelerate-fine-tuning-2x-using-unsloth)!
+ 7. `ChatML` for ShareGPT datasets, [conversational notebook](https://colab.research.google.com/drive/1Aau3lgPzeZKQ-98h69CCu1UJcvIBLmy2?usp=sharing)
+ 8. Text completions like novel writing [notebook](https://colab.research.google.com/drive/1ef-tab5bhkvWmBOObepl1WgJvfvSzn5Q?usp=sharing)
+ 9. [**NEW**] We make Phi-3 Medium / Mini **2x faster**! See our [Phi-3 Medium notebook](https://colab.research.google.com/drive/1hhdhBa1j_hsymiW9m-WzxQtgqTH_NHqi?usp=sharing)
+ 10. [**NEW**] We make Gemma-2 9b / 27b **2x faster**! See our [Gemma-2 9b notebook](https://colab.research.google.com/drive/1vIrqH5uYDQwsJ4-OO3DErvuv4pBgVwk4?usp=sharing)
+ 11. [**NEW**] To finetune and auto export to Ollama, try our [Ollama notebook](https://colab.research.google.com/drive/1WZDi7APtQ9VsvOrQSSC5DDtxq159j8iZ?usp=sharing)
+ 12. [**NEW**] We make Mistral NeMo 12B 2x faster and fit in under 12GB of VRAM! [Mistral NeMo notebook](https://colab.research.google.com/drive/17d3U-CAIwzmbDRqbZ9NnpHxCkmXB6LZ0?usp=sharing)
+
+ <div class="align-center">
+ <a href="https://github.com/unslothai/unsloth"><img src="https://github.com/unslothai/unsloth/raw/main/images/unsloth%20new%20logo.png" width="115"></a>
+ <a href="https://discord.gg/u54VK8m8tk"><img src="https://github.com/unslothai/unsloth/raw/main/images/Discord.png" width="145"></a>
+ <a href="https://ko-fi.com/unsloth"><img src="https://github.com/unslothai/unsloth/raw/main/images/Kofi button.png" width="145"></a></a> Support our work if you can! Thanks!
+ </div>
+ """
+ )
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/examples/third_party/unsloth/requirements.txt b/examples/third_party/unsloth/requirements.txt
new file mode 100644
index 0000000000..be8a71815c
--- /dev/null
+++ b/examples/third_party/unsloth/requirements.txt
@@ -0,0 +1,87 @@
+accelerate==0.34.2
+aiohappyeyeballs==2.4.0
+aiohttp==3.10.5
+aiosignal==1.3.1
+anyio==4.4.0
+async-timeout==4.0.3
+attrs==24.2.0
+bitsandbytes==0.43.3
+certifi==2024.8.30
+charset-normalizer==3.3.2
+click==8.1.7
+datasets==2.21.0
+dill==0.3.8
+docstring_parser==0.16
+docutils==0.21.2
+exceptiongroup==1.2.2
+filelock==3.16.0
+frozenlist==1.4.1
+fsspec==2024.6.1
+h11==0.14.0
+hf_transfer==0.1.8
+huggingface-hub==0.24.6
+idna==3.8
+itsdangerous==2.2.0
+jedi==0.19.1
+Jinja2==3.1.4
+marimo==0.8.13
+Markdown==3.7
+markdown-it-py==3.0.0
+MarkupSafe==2.1.5
+mdurl==0.1.2
+mpmath==1.3.0
+multidict==6.0.5
+multiprocess==0.70.16
+networkx==3.3
+numpy==2.1.1
+nvidia-cublas-cu12==12.1.3.1
+nvidia-cuda-cupti-cu12==12.1.105
+nvidia-cuda-nvrtc-cu12==12.1.105
+nvidia-cuda-runtime-cu12==12.1.105
+nvidia-cudnn-cu12==9.1.0.70
+nvidia-cufft-cu12==11.0.2.54
+nvidia-curand-cu12==10.3.2.106
+nvidia-cusolver-cu12==11.4.5.107
+nvidia-cusparse-cu12==12.1.0.106
+nvidia-nccl-cu12==2.20.5
+nvidia-nvjitlink-cu12==12.6.68
+nvidia-nvtx-cu12==12.1.105
+packaging==24.1
+pandas==2.2.2
+parso==0.8.4
+peft==0.12.0
+protobuf==3.20.3
+psutil==6.0.0
+pyarrow==17.0.0
+Pygments==2.18.0
+pymdown-extensions==10.9
+python-dateutil==2.9.0.post0
+pytz==2024.1
+PyYAML==6.0.2
+regex==2024.7.24
+requests==2.32.3
+rich==13.8.0
+ruff==0.6.4
+safetensors==0.4.5
+sentencepiece==0.2.0
+shtab==1.7.1
+six==1.16.0
+sniffio==1.3.1
+starlette==0.38.5
+sympy==1.13.2
+tokenizers==0.19.1
+tomlkit==0.13.2
+torch==2.4.1
+tqdm==4.66.5
+transformers==4.44.2
+triton==3.0.0
+trl==0.10.1
+typing_extensions==4.12.2
+tyro==0.8.10
+tzdata==2024.1
+unsloth==2024.8
+urllib3==2.2.2
+uvicorn==0.30.6
+websockets==12.0
+xxhash==3.5.0
+yarl==1.11.0