{
"cells": [
{
"cell_type": "markdown",
"id": "cell-0",
"metadata": {},
"source": [
"# Continued Pre-training (CPT) with Unsloth for Translation\n",
"\n",
"Fine-tuning requires a GPU. If you don't have one locally, you can run this notebook for free on [Google Colab](https://colab.research.google.com/github/Liquid4All/cookbook/blob/main/finetuning/notebooks/cpt_translation_with_unsloth.ipynb) using a free NVIDIA T4 GPU instance.\n",
"\n",
"[](https://colab.research.google.com/github/Liquid4All/cookbook/blob/main/finetuning/notebooks/cpt_translation_with_unsloth.ipynb)\n",
"\n",
"### What's in this notebook?\n",
"\n",
"In this notebook you will learn how to use Continued Pre-training (CPT) with Unsloth to adapt a language model for translation tasks.\n",
"We will use the [LFM2.5-1.2B-Base](https://docs.liquid.ai/docs/models/lfm25-1.2b-base) model and perform continued pre-training on Korean Wikipedia data, followed by instruction fine-tuning on Korean translation examples. This approach is ideal for adapting models to specific languages or translation domains.\n",
"\n",
"We will cover\n",
"- Environment setup\n",
"- Data preparation for translation\n",
"- Continued pre-training on domain-specific data\n",
"- Instruction fine-tuning for translation tasks\n",
"- Local inference with your new model\n",
"- Model saving and exporting it into the format you need for **deployment**.\n",
"\n",
"### Deployment options\n",
"\n",
"LFM2.5 models are small and efficient, enabling deployment across a wide range of platforms:\n",
"\n",
"
\n",
" \n",
" | Deployment Target | \n",
" Use Case | \n",
"
\n",
" \n",
" | π± Android | \n",
" Mobile apps on Android devices | \n",
"
\n",
" \n",
" | π± iOS | \n",
" Mobile apps on iPhone/iPad | \n",
"
\n",
" \n",
" | π Apple Silicon Mac | \n",
" Local inference on Mac with MLX | \n",
"
\n",
" \n",
" | π¦ llama.cpp | \n",
" Local deployments on any hardware | \n",
"
\n",
" \n",
" | π¦ Ollama | \n",
" Local inference with easy setup | \n",
"
\n",
" \n",
" | π₯οΈ LM Studio | \n",
" Desktop app for local inference | \n",
"
\n",
" \n",
" | β‘ vLLM | \n",
" Cloud deployments with high throughput | \n",
"
\n",
" \n",
" | βοΈ Modal | \n",
" Serverless cloud deployment | \n",
"
\n",
" \n",
" | ποΈ Baseten | \n",
" Production ML infrastructure | \n",
"
\n",
" \n",
" | π Fal | \n",
" Fast inference API | \n",
"
\n",
"
"
]
},
{
"cell_type": "markdown",
"id": "cell-0b",
"metadata": {},
"source": [
"### Need help building with our models and tools?\n",
"Join the Liquid AI Discord Community and ask.\n",
"\n",
"
\n",
"\n",
"And now, let the fine tune begin!"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bRlUoyrp_3sE"
},
"source": [
"### Installation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "IJ9jTPoO_3sE"
},
"outputs": [],
"source": [
"%%capture\n",
"import os, re\n",
"if \"COLAB_\" not in \"\".join(os.environ.keys()):\n",
" !pip install unsloth\n",
"else:\n",
" # Do this only in Colab notebooks! Otherwise use pip install unsloth\n",
" import torch; v = re.match(r\"[0-9]{1,}\\.[0-9]{1,}\", str(torch.__version__)).group(0)\n",
" xformers = \"xformers==\" + (\"0.0.33.post1\" if v==\"2.9\" else \"0.0.32.post2\" if v==\"2.8\" 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==4.3.0\" \"huggingface_hub>=0.34.0\" hf_transfer\n",
" !pip install --no-deps unsloth\n",
"!pip install transformers==4.57.3\n",
"!pip install --no-deps trl==0.22.2"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "5yQ-92KP_3sF"
},
"source": [
"### Unsloth"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lvisH6xv_3sG",
"outputId": "2849c6e1-d963-498d-aebc-4430c9bd3592"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"env: UNSLOTH_RETURN_LOGITS=1 # Run this to disable CCE since it is not supported for CPT\n"
]
}
],
"source": [
"%env UNSLOTH_RETURN_LOGITS=1 # Run this to disable CCE since it is not supported for CPT"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 365,
"referenced_widgets": [
"1e9838e93bc64d379d033db4ac7470af",
"2b3410f4178d49ef99235b1bd311f2d2",
"0fe3527f3dca4d5692a3aea95be00002",
"478ab8860b87499cac266fa3d8e2eec4",
"a01dc10c20444b6b8d2df5d06ffbdc97",
"8fe7917c02fd44c2bd6dbcbff4a7b9d4",
"6125017f831e4d0a9963f5cd67bd94ae",
"6c083fa23cf545dca7da83f27f89e630",
"78378870887444f5a0a6ca85b8bc121b",
"b7900cea9abb4876b1673e90947cf3e0",
"c42006a113c14e30be647f5ac90a96d6",
"c6df60f6e23e4deabaff94db0d8da9a7",
"4aa1a4dc18b2439ebb4b422bdbfe24af",
"fefbcc48c750472bbaa22b6a15bd95a3",
"fca6ef25fe7c463a8df2287ed20c4747",
"e32b00d3aa87460184fabf679b66f028",
"630637f7b2794500a73ccf6066e34d69",
"dc37abf624524453822c81b2a7299de0",
"ee7d7a5e3214472f8b88b6c6ac60caed",
"e723fe45c07f48b1993c295bdb1fbd39",
"e73c451a9b1c497aaaeadd75daa46033",
"e3daa8480cb948e48d4b3be8fcebc7e6",
"25f6ec1e5d6846649c995b78a4415bd6",
"cdf1c29ab64f427ca52405b567a232a7",
"3eacec872a9f4f7ea0340d3f4384de19",
"1230bc1efb9b4eb4a9e87e3750522767",
"0d11451ccb11433d8226454bef80124a",
"99d3f5f9076c4403a7a818470246b781",
"ca07426d2b184bdd9cedf632f3360d5d",
"99aea5e1d8f545d9bd8aa590a294cf72",
"dd3b1237daf9409199e32e2bf5edfa0e",
"b2ac43f92608404587f97291b8d46605",
"7c5a3e375dca4d4aa4569770f9466444",
"d5a3584e1a20488ea05da8c987a8dbc5",
"591fd740d9ee4342a609e95aee551228",
"3093b5bbc1bf463f87c1a8c10753f873",
"f81bef48fb114a4bb8747bdff9ff8e97",
"9425098593b3498194880685b66ba961",
"80bb8f6df4234cdfb495e1e7a9a20f3d",
"290bc6f9017b46cbbf33050037c7ca08",
"747eb593f06a498c9e3418cde3b73740",
"4ab6ff264a2b47d7aade4c6af575d04a",
"8bdbe2c5554f4530bb3547145e50f87a",
"38fc904747a74710a64b0a8eba963f16",
"31e2f1e45309415aa1f65ef24078d272",
"e6149c236a8a428f91f9d8ec861f91d1",
"4df019ea3dd947a8ae900ee77b412b23",
"4032b1215c86453296ee6906878fd948",
"64194788328046019214a5ffa399825a",
"81a367e4d7094825914166d739632e8c",
"70807ae9bab24a90a8f00e9daf4080f1",
"e92ac587c8024897bc22911d4b752747",
"ce5f13d1954d46e9af7a23b3f6358ff5",
"bff9140a1d5a45afacef0720062d132d",
"fea6e68d12714c5b84cea87c3a5702d7",
"a60ae73e1700412d856d24c5bbf7ff67",
"b4cf337b53fc443686aca22196e94d61",
"3ca8fb2d038c42129943188ee901040d",
"fe9deaf629c4418a8de41bc04ddf03d9",
"d8533ab7c97443c4b7644a00be833f6a",
"360425030db643b6b0abb1dd5a933212",
"543288c7075a46f68f7842843014729f",
"ce9c3b4e68f649239468aefb799240fd",
"4ebafb1c908b4407ab435f80479f2a3f",
"d036c20bd50f4549b90e4b5c75661189",
"f68b9da3d12d47f88fb94351411afdc0"
]
},
"id": "QmUBVEnvCDJv",
"outputId": "47b00b51-ccf0-4472-f1c4-cfe6f8dfeaa4"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"π¦₯ Unsloth: Will patch your computer to enable 2x faster free finetuning.\n",
"π¦₯ Unsloth Zoo will now patch everything to make training faster!\n",
"==((====))== Unsloth 2026.1.2: Fast Lfm2 patching. Transformers: 4.57.3.\n",
" \\\\ /| NVIDIA L4. Num GPUs = 1. Max memory: 22.161 GB. Platform: Linux.\n",
"O^O/ \\_/ \\ Torch: 2.9.0+cu126. CUDA: 8.9. CUDA Toolkit: 12.6. Triton: 3.5.0\n",
"\\ / Bfloat16 = TRUE. FA [Xformers = 0.0.33.post1. FA2 = False]\n",
" \"-____-\" Free license: http://github.com/unslothai/unsloth\n",
"Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored!\n",
"Unsloth: QLoRA and full finetuning all not selected. Switching to 16bit LoRA.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "1e9838e93bc64d379d033db4ac7470af",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"model.safetensors: 0%| | 0.00/2.34G [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "c6df60f6e23e4deabaff94db0d8da9a7",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"generation_config.json: 0%| | 0.00/132 [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "25f6ec1e5d6846649c995b78a4415bd6",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"tokenizer_config.json: 0.00B [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d5a3584e1a20488ea05da8c987a8dbc5",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"tokenizer.json: 0.00B [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "31e2f1e45309415aa1f65ef24078d272",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"special_tokens_map.json: 0%| | 0.00/434 [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a60ae73e1700412d856d24c5bbf7ff67",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"chat_template.jinja: 0.00B [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from unsloth import FastLanguageModel\n",
"import torch\n",
"max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!\n",
"dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+\n",
"load_in_4bit = False # Use 4bit quantization to reduce memory usage. Can be False.\n",
"\n",
"model, tokenizer = FastLanguageModel.from_pretrained(\n",
" model_name = \"LiquidAI/LFM2.5-1.2B-Base\", # Choose ANY! eg teknium/OpenHermes-2.5-Mistral-7B\n",
" max_seq_length = max_seq_length,\n",
" dtype = dtype,\n",
" load_in_4bit = load_in_4bit,\n",
" # token = \"hf_...\", # use one if using gated models like meta-llama/Llama-2-7b-hf\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SXd9bTZd1aaL"
},
"source": [
"We now add LoRA adapters so we only need to update 1 to 10% of all parameters!\n",
"\n",
"We also add `embed_tokens` and `lm_head` to allow the model to learn out of distribution data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "6bZsfBuZDeCL",
"outputId": "e295ce25-f271-4cb4-f330-5cbd05057459"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.12/dist-packages/peft/tuners/tuners_utils.py:916: UserWarning: Model with `tie_word_embeddings=True` and the tied_target_modules=['model.embed_tokens', 'lm_head'] are part of the adapter. This can lead to complications, for example when merging the adapter or converting your model to formats other than safetensors. See for example https://github.com/huggingface/peft/issues/2018.\n",
" warnings.warn(\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Unsloth: Making `model.base_model.model.model.embed_tokens` require gradients\n"
]
}
],
"source": [
"model = FastLanguageModel.get_peft_model(\n",
" model,\n",
" r = 128, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128\n",
" target_modules = [\"q_proj\", \"k_proj\", \"v_proj\", \"out_proj\", \"in_proj\",\n",
" \"w1\", \"w2\", \"w3\",\n",
" \"embed_tokens\", \"lm_head\",], # Add for continual pretraining\n",
" lora_alpha = 32,\n",
" lora_dropout = 0, # Supports any, but = 0 is optimized\n",
" bias = \"none\", # Supports any, but = \"none\" is optimized\n",
" # [NEW] \"unsloth\" uses 30% less VRAM, fits 2x larger batch sizes!\n",
" use_gradient_checkpointing = \"unsloth\", # True or \"unsloth\" for very long context\n",
" random_state = 3407,\n",
" use_rslora = True, # We support rank stabilized LoRA\n",
" loftq_config = None, # And LoftQ\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vITh0KVJ10qX"
},
"source": [
"\n",
"### Data Prep\n",
"We now use the Korean subset of the [Wikipedia dataset](https://huggingface.co/datasets/wikimedia/wikipedia) to first continually pretrain the model. You can use **any language** you like! Go to [Wikipedia's List of Languages](https://en.wikipedia.org/wiki/List_of_Wikipedias) to find your own language!\n",
"\n",
"**[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).\n",
"\n",
"**[NOTE]** Remember to add the **EOS_TOKEN** to the tokenized output!! Otherwise you'll get infinite generations!\n",
"\n",
"If you want to use the `llama-3` template for ShareGPT datasets, try our conversational [notebook](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Mistral_v0.3_(7B)-Conversational.ipynb)\n",
"\n",
"For text completions like novel writing, try this [notebook](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Mistral_(7B)-Text_Completion.ipynb).\n",
"\n",
"**[NOTE]** Use https://translate.google.com to translate from English to Korean!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "LjY75GoYUCB8"
},
"outputs": [],
"source": [
"# Wikipedia provides a title and an article text.\n",
"# Use https://translate.google.com!\n",
"_wikipedia_prompt = \"\"\"Wikipedia Article\n",
"### Title: {}\n",
"\n",
"### Article:\n",
"{}\"\"\"\n",
"# becomes:\n",
"wikipedia_prompt = \"\"\"μν€νΌλμ κΈ°μ¬\n",
"### μ λͺ©: {}\n",
"\n",
"### κΈ°μ¬:\n",
"{}\"\"\"\n",
"\n",
"EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN\n",
"def formatting_prompts_func(examples):\n",
" titles = examples[\"title\"]\n",
" texts = examples[\"text\"]\n",
" outputs = []\n",
" for title, text in zip(titles, texts):\n",
" # Must add EOS_TOKEN, otherwise your generation will go on forever!\n",
" text = wikipedia_prompt.format(title, text) + EOS_TOKEN\n",
" outputs.append(text)\n",
" return { \"text\" : outputs, }\n",
"pass"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rw0tuebw4Ppe"
},
"source": [
"We only use 1% of the dataset to speed things up! Use more for longer runs!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 209,
"referenced_widgets": [
"f5b44d7628844194998d17f439a476ae",
"c33f45386cb7422a8381eb06257e63ec",
"22a6352b39294acfa236bfca33571487",
"a3a9a21062b94af4b0d8c0a126e40369",
"eba4108943444257ac4b67570bf047f4",
"1930d3069ad14a278cd560cb01a041d4",
"4912737e9232414db7ef5849e23a1394",
"041a8eced75447fead96020e41af922b",
"2ffcd689e2d446c6bd1df4cb6f961233",
"d4222317566e471ba7a62f7ccba2596c",
"61a98e57d9a8437aa8edfa4eba74a292",
"10f8bb92c9744c1fa37108d2e53957e1",
"be2eec5442a747dcb83e977edd86b041",
"9839d96f3b9e4512a9bdf34f5ea39c24",
"458b31289c27479e84199fde3f69b897",
"b6d3e5d3686b4c93a5ac89169134b1ea",
"1514149c62864a2982ce7abfaf787007",
"0443ae929b344ee39956683567b7e1bc",
"66ff2b8c29aa4ed3858d1b857afe62f0",
"4375bc7686374a90aa2cd4f3521fdd0a",
"e18a6ee78cf347bebc89c1a420ef41fb",
"5332b07c1d38442f935fcdc57ef34cc4",
"67d9b8410f954e9ab3eabedb8a37cbc1",
"4f30a0e4491148cea0fcdc86a21bb413",
"5d8cffb6c95c4b3ab51ebe696cf1bb4e",
"e323bdfbb8ca4b79ba0c71bf6f162088",
"1453de4e4cb7420cbb7d05a77f9b228c",
"6bf957a8e542432a957a263857079ed6",
"c89ec2fe239542cf9b13c8026a656e21",
"d88366d41b3049d98fcaf401ca4851a6",
"55f4c28aca454dc5bc72c0ece4e150f6",
"92a5d1516a8345f2ad8f0d9aa632efb3",
"feee83c3474949aea23ad351785318de",
"9d1cf038ff83480cba57a34c779a923a",
"0f7dec72dc844270ae02af80198d8cb1",
"177ff3a7542647ccaa7224229dba32b9",
"b011c215f3c149199dade06d21f24283",
"f582a4a7ab1e49ec9cf7a367762bdc07",
"55cee5f842da437c8bbdd5bd88338d6d",
"397b04da588a4b1f90008164e7dc8d61",
"9c6ffe60f9614613ac92b595225a464b",
"70dfafe774cd44b89619a092a177ddb7",
"ce80a0d62951437d8559ddf7166e8c44",
"f0950c9d68eb484e816429f2d59ee782",
"805a17b0d37b4702b99c19e275c091d3",
"9f5b2d673d2541d59e68ba19d130b21c",
"5b13ab52b84245c9a5f2c59a64663054",
"48424fffbe4b4f93b6709142e971d43f",
"c7312c23f9fc4cfa94128a04b408f092",
"17b0280177fa48bbadb40a0b934eff28",
"13dce43730a6413fa5fb3f0a35ffef6d",
"dae461c0f0124b90b1d6f0e6f8ddde8b",
"9b7210bdee304d3ea153322dfb47fdcd",
"32d5006f3b6a43378973f286ef0dea41",
"16f71874f4114ee4ab51aea4229af5d9",
"848938d420a1454e91e25ddf08a6da29",
"6dc0a3c7de7f4f9aada821bb6da4312c",
"c47feedc8145408ba2c32a0f90da141b",
"5358e774e4de4a3a99aeb38ea2f70639",
"748c5974e93a4aec99ed4148c6342189",
"c39437fe2ad04a71864936aa51e39e74",
"ae3153cb80d4432bbe3b5114a17828b3",
"31d8a1ccf7f848b5bbc652cb9068f431",
"4b96c1019afd46449cfb11ecc0b44b12",
"04ab4d3f01bf4aadb0bfb1bf79782956",
"97e494c8734c49f5823240f1013fb21f"
]
},
"id": "EsKrpkza3VB3",
"outputId": "0e4bd26d-f036-4189-9caa-0e448618229a"
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "f5b44d7628844194998d17f439a476ae",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"README.md: 0.00B [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "10f8bb92c9744c1fa37108d2e53957e1",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"20231101.ko/train-00000-of-00003.parquet: 0%| | 0.00/400M [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "67d9b8410f954e9ab3eabedb8a37cbc1",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"20231101.ko/train-00001-of-00003.parquet: 0%| | 0.00/205M [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "9d1cf038ff83480cba57a34c779a923a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"20231101.ko/train-00002-of-00003.parquet: 0%| | 0.00/177M [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "805a17b0d37b4702b99c19e275c091d3",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Generating train split: 0%| | 0/647897 [00:00, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "848938d420a1454e91e25ddf08a6da29",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/6478 [00:00, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from datasets import load_dataset\n",
"\n",
"dataset = load_dataset(\"wikimedia/wikipedia\", \"20231101.ko\", split = \"train\",)\n",
"\n",
"# We select 1% of the data to make training faster!\n",
"dataset = dataset.train_test_split(train_size = 0.01)[\"train\"]\n",
"\n",
"dataset = dataset.map(formatting_prompts_func, batched = True,)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "idAEIeSQ3xdS"
},
"source": [
"\n",
"### Continued Pretraining\n",
"Now let's use Unsloth's `UnslothTrainer`! More docs here: [TRL SFT docs](https://huggingface.co/docs/trl/sft_trainer). We do 20 steps to speed things up, but you can set `num_train_epochs=1` for a full run, and turn off `max_steps=None`.\n",
"\n",
"Also set `embedding_learning_rate` to be a learning rate at least 2x or 10x smaller than `learning_rate` to make continual pretraining work!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 66,
"referenced_widgets": [
"2c7d67c976654d5aba5ce2572b88fb49",
"35fab26ac532431891df67a33f2fbe87",
"35b4a7320d124f899df6fd52a9dbc7b4",
"aa2716d36e254d3c9fd0b2ea5992a67a",
"995bdd86d03243f4afb392b09d21b480",
"4fbd255ab0c447dba8107e051e59adcb",
"88ea19a2b9574bd592188fdcf562c842",
"5fb2f4ece1e84f67adee65ca97009cd2",
"cfbd4ab591744939b046f8ff06b0d858",
"8f59572010d5452f90da2ab94c4c2f6d",
"59e2286c7b1f4f33a4e257cbc295bac7"
]
},
"id": "95_Nn-89DhsL",
"outputId": "9673580b-4c1b-4f2e-ee4a-9b9bf2e48112"
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "2c7d67c976654d5aba5ce2572b88fb49",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Unsloth: Tokenizing [\"text\"] (num_proc=16): 0%| | 0/6478 [00:00, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"π¦₯ Unsloth: Padding-free auto-enabled, enabling faster training.\n"
]
}
],
"source": [
"from transformers import TrainingArguments\n",
"from unsloth import UnslothTrainer, UnslothTrainingArguments\n",
"\n",
"trainer = UnslothTrainer(\n",
" model = model,\n",
" tokenizer = tokenizer,\n",
" train_dataset = dataset,\n",
" dataset_text_field = \"text\",\n",
" max_seq_length = max_seq_length,\n",
" dataset_num_proc = 4,\n",
"\n",
" args = UnslothTrainingArguments(\n",
" per_device_train_batch_size = 2,\n",
" gradient_accumulation_steps = 8,\n",
"\n",
" # Use warmup_ratio and num_train_epochs for longer runs!\n",
" max_steps = 120,\n",
" warmup_steps = 10,\n",
" # warmup_ratio = 0.1,\n",
" # num_train_epochs = 1,\n",
"\n",
" # Select a 2 to 10x smaller learning rate for the embedding matrices!\n",
" learning_rate = 5e-5,\n",
" embedding_learning_rate = 1e-5,\n",
"\n",
" logging_steps = 1,\n",
" optim = \"adamw_8bit\",\n",
" weight_decay = 0.001,\n",
" lr_scheduler_type = \"linear\",\n",
" seed = 3407,\n",
" output_dir = \"outputs\",\n",
" report_to = \"none\", # Use TrackIO/WandB etc\n",
" ),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2ejIt2xSNKKp",
"outputId": "4815f0c5-bee0-467e-c1e0-94ab4810a0c3"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"GPU = NVIDIA L4. Max memory = 22.161 GB.\n",
"2.598 GB of memory reserved.\n"
]
}
],
"source": [
"# @title Show current memory stats\n",
"gpu_stats = torch.cuda.get_device_properties(0)\n",
"start_gpu_memory = round(torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, 3)\n",
"max_memory = round(gpu_stats.total_memory / 1024 / 1024 / 1024, 3)\n",
"print(f\"GPU = {gpu_stats.name}. Max memory = {max_memory} GB.\")\n",
"print(f\"{start_gpu_memory} GB of memory reserved.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "yqxqAZ7KJ4oL",
"outputId": "d00f97dd-5e1d-48cd-b8bc-58f8de123f06"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"The model is already on multiple devices. Skipping the move to device specified in `args`.\n",
"==((====))== Unsloth - 2x faster free finetuning | Num GPUs used = 1\n",
" \\\\ /| Num examples = 6,478 | Num Epochs = 1 | Total steps = 120\n",
"O^O/ \\_/ \\ Batch size per device = 2 | Gradient accumulation steps = 8\n",
"\\ / Data Parallel GPUs = 1 | Total batch size (2 x 8 x 1) = 16\n",
" \"-____-\" Trainable parameters = 97,517,568 of 1,276,508,928 (7.64% trained)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Unsloth: Will smartly offload gradients to save VRAM!\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
"
\n",
" [120/120 04:24, Epoch 0/1]\n",
"
\n",
" \n",
" \n",
" \n",
" | Step | \n",
" Training Loss | \n",
"
\n",
" \n",
" \n",
" \n",
" | 1 | \n",
" 2.204100 | \n",
"
\n",
" \n",
" | 2 | \n",
" 1.917100 | \n",
"
\n",
" \n",
" | 3 | \n",
" 2.075200 | \n",
"
\n",
" \n",
" | 4 | \n",
" 1.892900 | \n",
"
\n",
" \n",
" | 5 | \n",
" 2.122100 | \n",
"
\n",
" \n",
" | 6 | \n",
" 2.040400 | \n",
"
\n",
" \n",
" | 7 | \n",
" 1.662600 | \n",
"
\n",
" \n",
" | 8 | \n",
" 1.794300 | \n",
"
\n",
" \n",
" | 9 | \n",
" 1.842000 | \n",
"
\n",
" \n",
" | 10 | \n",
" 1.900900 | \n",
"
\n",
" \n",
" | 11 | \n",
" 1.580900 | \n",
"
\n",
" \n",
" | 12 | \n",
" 1.880000 | \n",
"
\n",
" \n",
" | 13 | \n",
" 1.626500 | \n",
"
\n",
" \n",
" | 14 | \n",
" 1.779500 | \n",
"
\n",
" \n",
" | 15 | \n",
" 1.831500 | \n",
"
\n",
" \n",
" | 16 | \n",
" 1.751900 | \n",
"
\n",
" \n",
" | 17 | \n",
" 1.732000 | \n",
"
\n",
" \n",
" | 18 | \n",
" 1.599300 | \n",
"
\n",
" \n",
" | 19 | \n",
" 1.661500 | \n",
"
\n",
" \n",
" | 20 | \n",
" 1.886500 | \n",
"
\n",
" \n",
" | 21 | \n",
" 1.524300 | \n",
"
\n",
" \n",
" | 22 | \n",
" 1.635700 | \n",
"
\n",
" \n",
" | 23 | \n",
" 1.810400 | \n",
"
\n",
" \n",
" | 24 | \n",
" 1.848200 | \n",
"
\n",
" \n",
" | 25 | \n",
" 1.813500 | \n",
"
\n",
" \n",
" | 26 | \n",
" 1.709500 | \n",
"
\n",
" \n",
" | 27 | \n",
" 1.761600 | \n",
"
\n",
" \n",
" | 28 | \n",
" 1.427200 | \n",
"
\n",
" \n",
" | 29 | \n",
" 1.798000 | \n",
"
\n",
" \n",
" | 30 | \n",
" 1.862400 | \n",
"
\n",
" \n",
" | 31 | \n",
" 1.655300 | \n",
"
\n",
" \n",
" | 32 | \n",
" 1.434500 | \n",
"
\n",
" \n",
" | 33 | \n",
" 1.819100 | \n",
"
\n",
" \n",
" | 34 | \n",
" 1.528400 | \n",
"
\n",
" \n",
" | 35 | \n",
" 1.745900 | \n",
"
\n",
" \n",
" | 36 | \n",
" 1.718900 | \n",
"
\n",
" \n",
" | 37 | \n",
" 1.842900 | \n",
"
\n",
" \n",
" | 38 | \n",
" 1.525800 | \n",
"
\n",
" \n",
" | 39 | \n",
" 1.917400 | \n",
"
\n",
" \n",
" | 40 | \n",
" 1.876700 | \n",
"
\n",
" \n",
" | 41 | \n",
" 1.869100 | \n",
"
\n",
" \n",
" | 42 | \n",
" 1.877200 | \n",
"
\n",
" \n",
" | 43 | \n",
" 1.762600 | \n",
"
\n",
" \n",
" | 44 | \n",
" 1.318200 | \n",
"
\n",
" \n",
" | 45 | \n",
" 1.580500 | \n",
"
\n",
" \n",
" | 46 | \n",
" 1.934600 | \n",
"
\n",
" \n",
" | 47 | \n",
" 1.865300 | \n",
"
\n",
" \n",
" | 48 | \n",
" 1.860000 | \n",
"
\n",
" \n",
" | 49 | \n",
" 1.902900 | \n",
"
\n",
" \n",
" | 50 | \n",
" 1.488700 | \n",
"
\n",
" \n",
" | 51 | \n",
" 1.805500 | \n",
"
\n",
" \n",
" | 52 | \n",
" 1.696800 | \n",
"
\n",
" \n",
" | 53 | \n",
" 1.806300 | \n",
"
\n",
" \n",
" | 54 | \n",
" 1.810700 | \n",
"
\n",
" \n",
" | 55 | \n",
" 1.887900 | \n",
"
\n",
" \n",
" | 56 | \n",
" 1.895300 | \n",
"
\n",
" \n",
" | 57 | \n",
" 1.495600 | \n",
"
\n",
" \n",
" | 58 | \n",
" 1.696500 | \n",
"
\n",
" \n",
" | 59 | \n",
" 1.908500 | \n",
"
\n",
" \n",
" | 60 | \n",
" 1.488400 | \n",
"
\n",
" \n",
" | 61 | \n",
" 1.594100 | \n",
"
\n",
" \n",
" | 62 | \n",
" 2.020000 | \n",
"
\n",
" \n",
" | 63 | \n",
" 1.512200 | \n",
"
\n",
" \n",
" | 64 | \n",
" 1.759700 | \n",
"
\n",
" \n",
" | 65 | \n",
" 1.668500 | \n",
"
\n",
" \n",
" | 66 | \n",
" 1.796000 | \n",
"
\n",
" \n",
" | 67 | \n",
" 1.812900 | \n",
"
\n",
" \n",
" | 68 | \n",
" 1.606400 | \n",
"
\n",
" \n",
" | 69 | \n",
" 2.003000 | \n",
"
\n",
" \n",
" | 70 | \n",
" 1.744600 | \n",
"
\n",
" \n",
" | 71 | \n",
" 1.623500 | \n",
"
\n",
" \n",
" | 72 | \n",
" 2.004400 | \n",
"
\n",
" \n",
" | 73 | \n",
" 1.898900 | \n",
"
\n",
" \n",
" | 74 | \n",
" 1.629500 | \n",
"
\n",
" \n",
" | 75 | \n",
" 1.756600 | \n",
"
\n",
" \n",
" | 76 | \n",
" 1.897300 | \n",
"
\n",
" \n",
" | 77 | \n",
" 1.776500 | \n",
"
\n",
" \n",
" | 78 | \n",
" 1.794000 | \n",
"
\n",
" \n",
" | 79 | \n",
" 1.913500 | \n",
"
\n",
" \n",
" | 80 | \n",
" 1.813300 | \n",
"
\n",
" \n",
" | 81 | \n",
" 1.990800 | \n",
"
\n",
" \n",
" | 82 | \n",
" 1.550800 | \n",
"
\n",
" \n",
" | 83 | \n",
" 1.600400 | \n",
"
\n",
" \n",
" | 84 | \n",
" 1.710200 | \n",
"
\n",
" \n",
" | 85 | \n",
" 1.687600 | \n",
"
\n",
" \n",
" | 86 | \n",
" 1.487900 | \n",
"
\n",
" \n",
" | 87 | \n",
" 1.694200 | \n",
"
\n",
" \n",
" | 88 | \n",
" 1.681300 | \n",
"
\n",
" \n",
" | 89 | \n",
" 1.636100 | \n",
"
\n",
" \n",
" | 90 | \n",
" 1.651300 | \n",
"
\n",
" \n",
" | 91 | \n",
" 1.599500 | \n",
"
\n",
" \n",
" | 92 | \n",
" 2.039100 | \n",
"
\n",
" \n",
" | 93 | \n",
" 1.700700 | \n",
"
\n",
" \n",
" | 94 | \n",
" 1.595400 | \n",
"
\n",
" \n",
" | 95 | \n",
" 1.803100 | \n",
"
\n",
" \n",
" | 96 | \n",
" 1.763100 | \n",
"
\n",
" \n",
" | 97 | \n",
" 2.054900 | \n",
"
\n",
" \n",
" | 98 | \n",
" 1.769400 | \n",
"
\n",
" \n",
" | 99 | \n",
" 1.730800 | \n",
"
\n",
" \n",
" | 100 | \n",
" 1.728000 | \n",
"
\n",
" \n",
" | 101 | \n",
" 1.551600 | \n",
"
\n",
" \n",
" | 102 | \n",
" 2.047700 | \n",
"
\n",
" \n",
" | 103 | \n",
" 1.739400 | \n",
"
\n",
" \n",
" | 104 | \n",
" 1.587400 | \n",
"
\n",
" \n",
" | 105 | \n",
" 1.699400 | \n",
"
\n",
" \n",
" | 106 | \n",
" 1.753300 | \n",
"
\n",
" \n",
" | 107 | \n",
" 1.607900 | \n",
"
\n",
" \n",
" | 108 | \n",
" 1.621000 | \n",
"
\n",
" \n",
" | 109 | \n",
" 1.658300 | \n",
"
\n",
" \n",
" | 110 | \n",
" 1.780900 | \n",
"
\n",
" \n",
" | 111 | \n",
" 1.531200 | \n",
"
\n",
" \n",
" | 112 | \n",
" 1.856400 | \n",
"
\n",
" \n",
" | 113 | \n",
" 1.739500 | \n",
"
\n",
" \n",
" | 114 | \n",
" 1.693600 | \n",
"
\n",
" \n",
" | 115 | \n",
" 1.576500 | \n",
"
\n",
" \n",
" | 116 | \n",
" 1.738700 | \n",
"
\n",
" \n",
" | 117 | \n",
" 1.593500 | \n",
"
\n",
" \n",
" | 118 | \n",
" 1.719200 | \n",
"
\n",
" \n",
" | 119 | \n",
" 1.768000 | \n",
"
\n",
" \n",
" | 120 | \n",
" 1.714000 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.12/dist-packages/peft/utils/save_and_load.py:279: UserWarning: Setting `save_embedding_layers` to `True` as embedding layers found in `target_modules`.\n",
" warnings.warn(\"Setting `save_embedding_layers` to `True` as embedding layers found in `target_modules`.\")\n"
]
}
],
"source": [
"trainer_stats = trainer.train()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "R9dRBJZulavZ"
},
"source": [
"### Instruction Finetuning\n",
"\n",
"We now use the [Alpaca in GPT4 Dataset](https://huggingface.co/datasets/FreedomIntelligence/alpaca-gpt4-korean) but translated in Korean!\n",
"\n",
"Go to [vicgalle/alpaca-gpt4](https://huggingface.co/datasets/vicgalle/alpaca-gpt4) for the original GPT4 dataset for Alpaca or [MultilingualSIFT project](https://github.com/FreedomIntelligence/MultilingualSIFT) for other translations of the Alpaca dataset."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 148,
"referenced_widgets": [
"7500cd3fa2d94c4c9716871e55bcba47",
"0d642cf4768f4502aa20deb268fddc91",
"10cae01e7ebf42f3be74eb3134c5ad85",
"11bb05e49f32426cb5894748ae0ad29f",
"2719225c8dc34dca8ab99bc3e7c1806e",
"a62cb7b45f7f4b29a0a985cc229f1c5b",
"9289fe1169b94b258c4277aec164993d",
"120c06068732444f9da6c0378f5010d3",
"f0a952e6c8ca4da29fcdc67750e21ad4",
"801074afc4d84b81a9fd2a4a8ecb9a5d",
"05cd421d5e4746d2a6072ba65ebc2015",
"25cbe6b8060148ee93ad3b95f8416211",
"6169416a06b9464fae20e4c3e3425334",
"e927bdfcedb7493dba81c01fad134ead",
"bd597e8c08bb482292c4ea2c3f7dd4b5",
"3c61eaa8d975477ea88b54ce84743bea",
"e455c7b6f019456b880c627e7ef89e95",
"bde7a4ca1216440587f46d8a11426fb7",
"d3b8ba12821848faad8900f5ca37640c",
"0021f7a8cbb949918cc8575b5cfbf4a0",
"3eeb37932f314347b8a10567c5754111",
"05e327a56e11466c921298b94ce5743a",
"97ce6ebdb1e34829acf837f9a6d3221a",
"ad18ae3a29a3420a836b39f8a0cb32e4",
"84ce9b8487e34f4d835e337eeaeda4ed",
"cecd13882474452e948fbc0aebdde72e",
"8038a74c4c774b5fa0be882efafcb5ef",
"dd218d77959c442794d736d238d3ca2f",
"385d2e0325744086bd6f932ebee66f2e",
"224e551aeb5e4234ad59df76c93a67e7",
"625bd000f8094f04964054ee577ffcaa",
"9612eac692ae494fbbe995361384cdac",
"077ccd2208374dd4b9229270299fd563"
]
},
"id": "1oNjUwxOyG8C",
"outputId": "b92bedc5-cf8d-4be0-8f7a-fb0c533013ab"
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7500cd3fa2d94c4c9716871e55bcba47",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"README.md: 0%| | 0.00/124 [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Repo card metadata block was not found. Setting CardData to empty.\n",
"WARNING:huggingface_hub.repocard:Repo card metadata block was not found. Setting CardData to empty.\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "25cbe6b8060148ee93ad3b95f8416211",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"alpaca-gpt4-korean.json: 0%| | 0.00/51.6M [00:00, ?B/s]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "97ce6ebdb1e34829acf837f9a6d3221a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Generating train split: 0%| | 0/49969 [00:00, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from datasets import load_dataset\n",
"\n",
"alpaca_dataset = load_dataset(\"FreedomIntelligence/alpaca-gpt4-korean\", split=\"train\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kmFG41nFytgi"
},
"source": [
"We print 1 example:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "QvTfIKaUQxQ5",
"outputId": "271133b2-4f4f-45c7-b629-d87d407950a6"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'conversations': [{'from': 'human', 'value': 'μ¬νμ© μΊ νμΈ μ¬λ‘건μ μ μνμΈμ.\\n'}, {'from': 'gpt', 'value': '1. \"λμ± λ
Ήμ λ―Έλλ₯Ό μν΄ ν¨κ» μ€μ΄κ³ , μ¬μ¬μ©νκ³ , μ¬νμ©νμΈμ.\"\\n2. \"λ λμ λ΄μΌμ μν΄ μ€λ λ°λ‘ μ¬νμ©νμΈμ.\"\\n3. \"μ°λ κΈ°λ₯Ό λ³΄λ¬Όλ‘ λ§λλ λ² - μ¬νμ©!\"\\n4. \"μΈμμ μνμ μν΄ μ¬νμ©νμΈμ.\"\\n5. \"μμμ μλΌκ³ λ λ§μ΄ μ¬νμ©νμΈμ.\"'}], 'id': '23712'}\n"
]
}
],
"source": [
"print(alpaca_dataset[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OO8UY34ql2vJ"
},
"source": [
"We again use https://translate.google.com/ to translate the Alpaca format into Korean"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 49,
"referenced_widgets": [
"fb9358899cbb4750bf4bb26c7f6d628f",
"d432ce98c51e4431b7623f8d2156a1bd",
"ca6f279f659f4ca9ae6174c028c2edeb",
"6593091e611b41d98df969ec5675618e",
"f11429abcb1c45b7894fccb03a68430a",
"e95916e6ae3144d9901be996483b2bcf",
"9560e00e3dfb4741a49a0506fed3e2ff",
"b17d04e5332c43bfada34f38a4109d67",
"8cc9ff81f3da4b2b9e078cf0d2ec7b2c",
"4753517a38464026a1e76072c5219715",
"7069e2a730b24bb2bae8c013ec4cf5d1"
]
},
"id": "B2tv8AEhPTu6",
"outputId": "7d50b879-1b1b-46ed-b69d-31689ce16fbe"
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "fb9358899cbb4750bf4bb26c7f6d628f",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map: 0%| | 0/49969 [00:00, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"_alpaca_prompt = \"\"\"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n",
"\n",
"### Instruction:\n",
"{}\n",
"\n",
"### Response:\n",
"{}\"\"\"\n",
"# Becomes:\n",
"alpaca_prompt = \"\"\"λ€μμ μμ
μ μ€λͺ
νλ λͺ
λ Ήμ
λλ€. μμ²μ μ μ νκ² μλ£νλ μλ΅μ μμ±νμΈμ.\n",
"\n",
"### μ§μΉ¨:\n",
"{}\n",
"\n",
"### μλ΅:\n",
"{}\"\"\"\n",
"\n",
"EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN\n",
"def formatting_prompts_func(conversations):\n",
" texts = []\n",
" conversations = conversations[\"conversations\"]\n",
" for convo in conversations:\n",
" # Must add EOS_TOKEN, otherwise your generation will go on forever!\n",
" text = alpaca_prompt.format(convo[0][\"value\"], convo[1][\"value\"]) + EOS_TOKEN\n",
" texts.append(text)\n",
" return { \"text\" : texts, }\n",
"\n",
"alpaca_dataset = alpaca_dataset.map(formatting_prompts_func, batched = True,)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "mxWWh8xsl9XT"
},
"source": [
"We again employ `UnslothTrainer` and do instruction finetuning!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 49,
"referenced_widgets": [
"91f1d1c21b914478b5b2528af71cf7e1",
"2e35a2b486f34132be95cee168b1efa4",
"c70e2f8902c8476f9579676433e4158d",
"3ac6446d477a4bf8be0a4898fa1da4e6",
"79111526e9c048c897956ced33acaa3e",
"d7d6ffd04ba74ac4b379cb3bea754a91",
"268290f79f234522b54beb9897484958",
"b548fe7afdcb4a6ba521a8829f9c3064",
"5a1ae9b545fc4f408642f6a774f46143",
"31db35d41e4d4840917a4950aee4f5c0",
"bc9ce7f54ab7467c8203553bac628efc"
]
},
"id": "1Zul21NSRRLP",
"outputId": "5ca85dbb-d8b5-4d65-89d9-cb6fd4b279a5"
},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "91f1d1c21b914478b5b2528af71cf7e1",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Unsloth: Tokenizing [\"text\"] (num_proc=16): 0%| | 0/49969 [00:00, ? examples/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from transformers import TrainingArguments\n",
"from unsloth import UnslothTrainer, UnslothTrainingArguments\n",
"\n",
"trainer = UnslothTrainer(\n",
" model = model,\n",
" tokenizer = tokenizer,\n",
" train_dataset = alpaca_dataset,\n",
" dataset_text_field = \"text\",\n",
" max_seq_length = max_seq_length,\n",
" dataset_num_proc = 8,\n",
"\n",
" args = UnslothTrainingArguments(\n",
" per_device_train_batch_size = 2,\n",
" gradient_accumulation_steps = 8,\n",
"\n",
" # Use num_train_epochs and warmup_ratio for longer runs!\n",
" max_steps = 120,\n",
" warmup_steps = 10,\n",
" # warmup_ratio = 0.1,\n",
" # num_train_epochs = 1,\n",
"\n",
" # Select a 2 to 10x smaller learning rate for the embedding matrices!\n",
" learning_rate = 5e-5,\n",
" embedding_learning_rate = 1e-5,\n",
"\n",
" logging_steps = 1,\n",
" optim = \"adamw_8bit\",\n",
" weight_decay = 0.00,\n",
" lr_scheduler_type = \"linear\",\n",
" seed = 3407,\n",
" output_dir = \"outputs\",\n",
" report_to = \"none\", # Use TrackIO/WandB etc\n",
" ),\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "DIO7c1FoRe-X",
"outputId": "32b21474-cc2a-43b9-8da5-fd3b71ec05e2"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"The model is already on multiple devices. Skipping the move to device specified in `args`.\n",
"==((====))== Unsloth - 2x faster free finetuning | Num GPUs used = 1\n",
" \\\\ /| Num examples = 49,969 | Num Epochs = 1 | Total steps = 120\n",
"O^O/ \\_/ \\ Batch size per device = 2 | Gradient accumulation steps = 8\n",
"\\ / Data Parallel GPUs = 1 | Total batch size (2 x 8 x 1) = 16\n",
" \"-____-\" Trainable parameters = 97,517,568 of 1,276,508,928 (7.64% trained)\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Unsloth: Will smartly offload gradients to save VRAM!\n"
]
},
{
"data": {
"text/html": [
"\n",
" \n",
" \n",
"
\n",
" [120/120 03:21, Epoch 0/1]\n",
"
\n",
" \n",
" \n",
" \n",
" | Step | \n",
" Training Loss | \n",
"
\n",
" \n",
" \n",
" \n",
" | 1 | \n",
" 1.960100 | \n",
"
\n",
" \n",
" | 2 | \n",
" 1.916300 | \n",
"
\n",
" \n",
" | 3 | \n",
" 1.732600 | \n",
"
\n",
" \n",
" | 4 | \n",
" 1.592300 | \n",
"
\n",
" \n",
" | 5 | \n",
" 1.505100 | \n",
"
\n",
" \n",
" | 6 | \n",
" 1.324400 | \n",
"
\n",
" \n",
" | 7 | \n",
" 1.380100 | \n",
"
\n",
" \n",
" | 8 | \n",
" 1.478100 | \n",
"
\n",
" \n",
" | 9 | \n",
" 1.302900 | \n",
"
\n",
" \n",
" | 10 | \n",
" 1.327200 | \n",
"
\n",
" \n",
" | 11 | \n",
" 1.174000 | \n",
"
\n",
" \n",
" | 12 | \n",
" 1.323000 | \n",
"
\n",
" \n",
" | 13 | \n",
" 1.115200 | \n",
"
\n",
" \n",
" | 14 | \n",
" 1.341500 | \n",
"
\n",
" \n",
" | 15 | \n",
" 1.231500 | \n",
"
\n",
" \n",
" | 16 | \n",
" 1.223500 | \n",
"
\n",
" \n",
" | 17 | \n",
" 1.229800 | \n",
"
\n",
" \n",
" | 18 | \n",
" 1.279700 | \n",
"
\n",
" \n",
" | 19 | \n",
" 1.291400 | \n",
"
\n",
" \n",
" | 20 | \n",
" 1.158600 | \n",
"
\n",
" \n",
" | 21 | \n",
" 1.189800 | \n",
"
\n",
" \n",
" | 22 | \n",
" 1.202100 | \n",
"
\n",
" \n",
" | 23 | \n",
" 1.075500 | \n",
"
\n",
" \n",
" | 24 | \n",
" 1.153000 | \n",
"
\n",
" \n",
" | 25 | \n",
" 1.246700 | \n",
"
\n",
" \n",
" | 26 | \n",
" 1.095900 | \n",
"
\n",
" \n",
" | 27 | \n",
" 1.167800 | \n",
"
\n",
" \n",
" | 28 | \n",
" 1.235900 | \n",
"
\n",
" \n",
" | 29 | \n",
" 1.246900 | \n",
"
\n",
" \n",
" | 30 | \n",
" 1.001400 | \n",
"
\n",
" \n",
" | 31 | \n",
" 1.288600 | \n",
"
\n",
" \n",
" | 32 | \n",
" 1.111600 | \n",
"
\n",
" \n",
" | 33 | \n",
" 1.261200 | \n",
"
\n",
" \n",
" | 34 | \n",
" 1.193700 | \n",
"
\n",
" \n",
" | 35 | \n",
" 1.110400 | \n",
"
\n",
" \n",
" | 36 | \n",
" 1.192600 | \n",
"
\n",
" \n",
" | 37 | \n",
" 1.220400 | \n",
"
\n",
" \n",
" | 38 | \n",
" 1.277400 | \n",
"
\n",
" \n",
" | 39 | \n",
" 1.126100 | \n",
"
\n",
" \n",
" | 40 | \n",
" 1.205600 | \n",
"
\n",
" \n",
" | 41 | \n",
" 1.148900 | \n",
"
\n",
" \n",
" | 42 | \n",
" 1.144600 | \n",
"
\n",
" \n",
" | 43 | \n",
" 1.163200 | \n",
"
\n",
" \n",
" | 44 | \n",
" 1.305500 | \n",
"
\n",
" \n",
" | 45 | \n",
" 1.218100 | \n",
"
\n",
" \n",
" | 46 | \n",
" 1.024800 | \n",
"
\n",
" \n",
" | 47 | \n",
" 1.186500 | \n",
"
\n",
" \n",
" | 48 | \n",
" 1.210500 | \n",
"
\n",
" \n",
" | 49 | \n",
" 1.189400 | \n",
"
\n",
" \n",
" | 50 | \n",
" 1.102300 | \n",
"
\n",
" \n",
" | 51 | \n",
" 1.130700 | \n",
"
\n",
" \n",
" | 52 | \n",
" 0.952200 | \n",
"
\n",
" \n",
" | 53 | \n",
" 1.134600 | \n",
"
\n",
" \n",
" | 54 | \n",
" 1.204000 | \n",
"
\n",
" \n",
" | 55 | \n",
" 1.158900 | \n",
"
\n",
" \n",
" | 56 | \n",
" 1.324500 | \n",
"
\n",
" \n",
" | 57 | \n",
" 1.208600 | \n",
"
\n",
" \n",
" | 58 | \n",
" 1.189000 | \n",
"
\n",
" \n",
" | 59 | \n",
" 1.114100 | \n",
"
\n",
" \n",
" | 60 | \n",
" 1.131300 | \n",
"
\n",
" \n",
" | 61 | \n",
" 1.238700 | \n",
"
\n",
" \n",
" | 62 | \n",
" 1.178100 | \n",
"
\n",
" \n",
" | 63 | \n",
" 1.155200 | \n",
"
\n",
" \n",
" | 64 | \n",
" 1.088200 | \n",
"
\n",
" \n",
" | 65 | \n",
" 1.347600 | \n",
"
\n",
" \n",
" | 66 | \n",
" 1.097300 | \n",
"
\n",
" \n",
" | 67 | \n",
" 1.145100 | \n",
"
\n",
" \n",
" | 68 | \n",
" 1.058100 | \n",
"
\n",
" \n",
" | 69 | \n",
" 1.250400 | \n",
"
\n",
" \n",
" | 70 | \n",
" 1.258900 | \n",
"
\n",
" \n",
" | 71 | \n",
" 1.127400 | \n",
"
\n",
" \n",
" | 72 | \n",
" 1.161100 | \n",
"
\n",
" \n",
" | 73 | \n",
" 1.088800 | \n",
"
\n",
" \n",
" | 74 | \n",
" 1.147600 | \n",
"
\n",
" \n",
" | 75 | \n",
" 1.229800 | \n",
"
\n",
" \n",
" | 76 | \n",
" 1.093800 | \n",
"
\n",
" \n",
" | 77 | \n",
" 1.114500 | \n",
"
\n",
" \n",
" | 78 | \n",
" 1.188100 | \n",
"
\n",
" \n",
" | 79 | \n",
" 1.110300 | \n",
"
\n",
" \n",
" | 80 | \n",
" 1.132600 | \n",
"
\n",
" \n",
" | 81 | \n",
" 1.358300 | \n",
"
\n",
" \n",
" | 82 | \n",
" 1.215700 | \n",
"
\n",
" \n",
" | 83 | \n",
" 1.105100 | \n",
"
\n",
" \n",
" | 84 | \n",
" 1.052100 | \n",
"
\n",
" \n",
" | 85 | \n",
" 1.343200 | \n",
"
\n",
" \n",
" | 86 | \n",
" 1.124700 | \n",
"
\n",
" \n",
" | 87 | \n",
" 1.340600 | \n",
"
\n",
" \n",
" | 88 | \n",
" 1.115500 | \n",
"
\n",
" \n",
" | 89 | \n",
" 1.048400 | \n",
"
\n",
" \n",
" | 90 | \n",
" 1.184300 | \n",
"
\n",
" \n",
" | 91 | \n",
" 1.201300 | \n",
"
\n",
" \n",
" | 92 | \n",
" 1.095200 | \n",
"
\n",
" \n",
" | 93 | \n",
" 1.058900 | \n",
"
\n",
" \n",
" | 94 | \n",
" 1.347200 | \n",
"
\n",
" \n",
" | 95 | \n",
" 1.024100 | \n",
"
\n",
" \n",
" | 96 | \n",
" 1.127000 | \n",
"
\n",
" \n",
" | 97 | \n",
" 1.293200 | \n",
"
\n",
" \n",
" | 98 | \n",
" 1.106900 | \n",
"
\n",
" \n",
" | 99 | \n",
" 1.041000 | \n",
"
\n",
" \n",
" | 100 | \n",
" 1.286100 | \n",
"
\n",
" \n",
" | 101 | \n",
" 1.065200 | \n",
"
\n",
" \n",
" | 102 | \n",
" 1.133100 | \n",
"
\n",
" \n",
" | 103 | \n",
" 1.272900 | \n",
"
\n",
" \n",
" | 104 | \n",
" 1.213900 | \n",
"
\n",
" \n",
" | 105 | \n",
" 0.994700 | \n",
"
\n",
" \n",
" | 106 | \n",
" 1.186600 | \n",
"
\n",
" \n",
" | 107 | \n",
" 1.189600 | \n",
"
\n",
" \n",
" | 108 | \n",
" 1.234500 | \n",
"
\n",
" \n",
" | 109 | \n",
" 1.165800 | \n",
"
\n",
" \n",
" | 110 | \n",
" 1.124100 | \n",
"
\n",
" \n",
" | 111 | \n",
" 1.241200 | \n",
"
\n",
" \n",
" | 112 | \n",
" 1.092300 | \n",
"
\n",
" \n",
" | 113 | \n",
" 1.201000 | \n",
"
\n",
" \n",
" | 114 | \n",
" 1.161800 | \n",
"
\n",
" \n",
" | 115 | \n",
" 1.299800 | \n",
"
\n",
" \n",
" | 116 | \n",
" 1.137100 | \n",
"
\n",
" \n",
" | 117 | \n",
" 1.119700 | \n",
"
\n",
" \n",
" | 118 | \n",
" 1.006000 | \n",
"
\n",
" \n",
" | 119 | \n",
" 1.280800 | \n",
"
\n",
" \n",
" | 120 | \n",
" 1.066700 | \n",
"
\n",
" \n",
"
"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.12/dist-packages/peft/utils/save_and_load.py:279: UserWarning: Setting `save_embedding_layers` to `True` as embedding layers found in `target_modules`.\n",
" warnings.warn(\"Setting `save_embedding_layers` to `True` as embedding layers found in `target_modules`.\")\n"
]
}
],
"source": [
"trainer_stats = trainer.train()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pCqnaKmlO1U9",
"outputId": "05d48d2a-7f23-466f-f7ad-52c57c5fe5c1"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"226.8895 seconds used for training.\n",
"3.78 minutes used for training.\n",
"Peak reserved memory = 4.361 GB.\n",
"Peak reserved memory for training = 1.763 GB.\n",
"Peak reserved memory % of max memory = 19.679 %.\n",
"Peak reserved memory for training % of max memory = 7.955 %.\n"
]
}
],
"source": [
"# @title Show final memory and time stats\n",
"used_memory = round(torch.cuda.max_memory_reserved() / 1024 / 1024 / 1024, 3)\n",
"used_memory_for_lora = round(used_memory - start_gpu_memory, 3)\n",
"used_percentage = round(used_memory / max_memory * 100, 3)\n",
"lora_percentage = round(used_memory_for_lora / max_memory * 100, 3)\n",
"print(f\"{trainer_stats.metrics['train_runtime']} seconds used for training.\")\n",
"print(\n",
" f\"{round(trainer_stats.metrics['train_runtime']/60, 2)} minutes used for training.\"\n",
")\n",
"print(f\"Peak reserved memory = {used_memory} GB.\")\n",
"print(f\"Peak reserved memory for training = {used_memory_for_lora} GB.\")\n",
"print(f\"Peak reserved memory % of max memory = {used_percentage} %.\")\n",
"print(f\"Peak reserved memory for training % of max memory = {lora_percentage} %.\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ekOmTR1hSNcr"
},
"source": [
"\n",
"### Inference\n",
"Let's run the model! You can change the instruction and input - leave the output blank!\n",
"\n",
"Remember to use https://translate.google.com/!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "B_GRpqmUiFRj",
"outputId": "ce57b1e6-6aae-4878-97d8-8aed766387ea"
},
"outputs": [
{
"data": {
"text/plain": [
"['<|startoftext|>λ€μμ μμ
μ μ€λͺ
νλ λͺ
λ Ήμ
λλ€. μμ²μ μ μ νκ² μλ£νλ μλ΅μ μμ±νμΈμ.\\n\\n### μ§μΉ¨:\\nνΌλ³΄λμΉ μμ΄μ κ³μνμΈμ: 1, 1, 2, 3, 5, 8,\\n\\n### μλ΅:\\nνΌλ³΄λμΉ μμ΄μ κ° μ«μκ° μμ λ μ«μμ ν©μΈ μμ΄μ
λλ€. μ΄ μμ΄μ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ']"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# alpaca_prompt = Copied from above\n",
"FastLanguageModel.for_inference(model) # Enable native 2x faster inference\n",
"inputs = tokenizer(\n",
"[\n",
" alpaca_prompt.format(\n",
" # \"Continue the fibonacci sequence: 1, 1, 2, 3, 5, 8,\", # instruction\n",
" \"νΌλ³΄λμΉ μμ΄μ κ³μνμΈμ: 1, 1, 2, 3, 5, 8,\", # instruction\n",
" \"\", # output - leave this blank for generation!\n",
" )\n",
"], return_tensors = \"pt\").to(\"cuda\")\n",
"\n",
"outputs = model.generate(**inputs, max_new_tokens = 64, use_cache = True)\n",
"tokenizer.batch_decode(outputs)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "CrSvZObor0lY"
},
"source": [
" You can also use a `TextStreamer` for continuous inference - so you can see the generation token by token, instead of waiting the whole time!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "e2pEuRb1r2Vg",
"outputId": "e8962db0-d3b8-4177-9946-ff36a0fd0130"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<|startoftext|>λ€μμ μμ
μ μ€λͺ
νλ λͺ
λ Ήμ
λλ€. μμ²μ μ μ νκ² μλ£νλ μλ΅μ μμ±νμΈμ.\n",
"\n",
"### μ§μΉ¨:\n",
"νκ΅μμ
μ μ΄λ€κ°μ?\n",
"\n",
"### μλ΅:\n",
"νκ΅μμ
μ μ ν΅ μμ
κ³Ό νλ μμ
μ ν¬ν¨ν λ€μν μ₯λ₯΄λ₯Ό ν¬ν¨ν©λλ€. μ ν΅ μμ
μ μ’
μ’
μ
κΈ°μ λ
Έλλ₯Ό μ¬μ©νμ¬ νκ΅μ μμ¬μ λ¬Ένλ₯Ό λ°μν©λλ€. νλ μμ
μ μ ν΅ μμ
μ μν₯μ λ°μ λ°μ ν΄μμΌλ©°, μ μ μμ
, λ‘, ν λ± λ€μν μ₯λ₯΄κ° μμ΅λλ€. νκ΅ μμ
μ μ μΈκ³μ μΌλ‘ μΈκΈ°λ₯Ό μ»κ³ μμΌλ©°, λ§μ μν°μ€νΈλ€μ΄ κ΅μ μ μΈ μ±κ³΅μ κ±°λμμ΅λλ€.<|im_end|>\n"
]
}
],
"source": [
"# alpaca_prompt = Copied from above\n",
"FastLanguageModel.for_inference(model) # Enable native 2x faster inference\n",
"inputs = tokenizer(\n",
"[\n",
" alpaca_prompt.format(\n",
" # \"What is Korean music like?\"\n",
" \"νκ΅μμ
μ μ΄λ€κ°μ?\", # instruction\n",
" \"\", # output - leave this blank for generation!\n",
" )\n",
"], return_tensors = \"pt\").to(\"cuda\")\n",
"\n",
"from transformers import TextStreamer\n",
"text_streamer = TextStreamer(tokenizer)\n",
"_ = model.generate(**inputs, streamer = text_streamer, max_new_tokens = 128)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "acKgU7NMauCp"
},
"source": [
"By using https://translate.google.com/ we get\n",
"```\n",
"Korean music is classified into many types of music genres.\n",
"\n",
"This genre is classified into different music genres such as pop songs,\n",
"\n",
"rock songs, classical songs and pop songs, music groups consisting of drums, fans, instruments and singers\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uMuVrWbjAzhc"
},
"source": [
"\n",
"### Saving, loading finetuned models\n",
"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.\n",
"\n",
"**[NOTE]** This ONLY saves the LoRA adapters, and not the full model. To save to 16bit or GGUF, scroll down!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "upcOlWe7A1vc",
"outputId": "6ce0bfd6-9fd7-4993-8733-d1f749ac3af3"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.12/dist-packages/peft/utils/save_and_load.py:279: UserWarning: Setting `save_embedding_layers` to `True` as embedding layers found in `target_modules`.\n",
" warnings.warn(\"Setting `save_embedding_layers` to `True` as embedding layers found in `target_modules`.\")\n"
]
},
{
"data": {
"text/plain": [
"('lora_model/tokenizer_config.json',\n",
" 'lora_model/special_tokens_map.json',\n",
" 'lora_model/chat_template.jinja',\n",
" 'lora_model/tokenizer.json')"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.save_pretrained(\"lora_model\") # Local saving\n",
"tokenizer.save_pretrained(\"lora_model\")\n",
"# model.push_to_hub(\"your_name/lora_model\", token = \"...\") # Online saving\n",
"# tokenizer.push_to_hub(\"your_name/lora_model\", token = \"...\") # Online saving"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "AEEcJ4qfC7Lp"
},
"source": [
"Now if you want to load the LoRA adapters we just saved for inference, set `False` to `True`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "MKX_XKs_BNZR",
"outputId": "cccb4070-83d5-42d0-c23e-cfb9dd57b73c"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<|startoftext|>λ€μμ μμ
μ μ€λͺ
νλ λͺ
λ Ήμ
λλ€. μμ²μ μ μ νκ² μλ£νλ μλ΅μ μμ±νμΈμ.\n",
"\n",
"### μ§μΉ¨:\n",
"μ§κ΅¬λ₯Ό κ΄λ²μνκ² μ€λͺ
νμΈμ.\n",
"\n",
"### μλ΅:\n",
"μ§κ΅¬λ₯Ό κ΄λ²μνκ² μ€λͺ
νμΈμ.\n",
"\n",
"μ§κ΅¬λ₯Ό κ΄λ²μνκ² μ€λͺ
νμΈμ.\n",
"\n",
"μ§κ΅¬λ₯Ό κ΄λ²μνκ² μ€λͺ
νμΈμ.\n",
"\n",
"μ§κ΅¬λ₯Ό κ΄λ²μνκ² μ€λͺ
νμΈμ.\n",
"\n",
"μ§κ΅¬λ₯Ό κ΄λ²μνκ² μ€λͺ
νμΈμ.\n",
"\n",
"μ§κ΅¬λ₯Ό κ΄λ²μνκ² μ€λͺ
νμΈμ.\n",
"\n",
"μ§κ΅¬λ₯Ό κ΄λ²μνκ² μ€λͺ
νμΈμ.\n",
"\n",
"μ§κ΅¬λ₯Ό κ΄λ²μνκ² μ€λͺ
νμΈμ.\n",
"\n",
"μ§κ΅¬λ₯Ό κ΄λ²μνκ² μ€λͺ
νμΈμ.\n",
"\n",
"μ§κ΅¬λ₯Ό κ΄λ²μνκ² μ€λͺ
νμΈμ.\n",
"\n",
"μ§κ΅¬λ₯Ό κ΄λ²μνκ² μ€λͺ
\n"
]
}
],
"source": [
"if False:\n",
" from unsloth import FastLanguageModel\n",
"\n",
" model, tokenizer = FastLanguageModel.from_pretrained(\n",
" model_name=\"lora_model\", # YOUR MODEL YOU USED FOR TRAINING\n",
" max_seq_length=max_seq_length,\n",
" dtype=dtype,\n",
" load_in_4bit=load_in_4bit,\n",
" )\n",
" FastLanguageModel.for_inference(model) # Enable native 2x faster inference\n",
"\n",
"# alpaca_prompt = You MUST copy from above!\n",
"\n",
"inputs = tokenizer(\n",
" [\n",
" alpaca_prompt.format(\n",
" # \"Describe the planet Earth extensively.\", # instruction\n",
" \"μ§κ΅¬λ₯Ό κ΄λ²μνκ² μ€λͺ
νμΈμ.\",\n",
" \"\", # output - leave this blank for generation!\n",
" ),\n",
" ],\n",
" return_tensors=\"pt\",\n",
").to(\"cuda\")\n",
"\n",
"\n",
"from transformers import TextStreamer\n",
"\n",
"text_streamer = TextStreamer(tokenizer)\n",
"_ = model.generate(\n",
" **inputs, streamer=text_streamer, max_new_tokens=128, repetition_penalty=0.1\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "twNf4NXhmLqj"
},
"source": [
"By using https://translate.google.com/ we get\n",
"```\n",
"Earth refers to all things including natural disasters such as local derailment\n",
"\n",
"and local depletion that occur in one space along with the suppression of water, gases, and living things.\n",
"\n",
"Most of the Earth's water comes from oceans, atmospheric water, underground water layers, and rivers and rivers.\n",
"```\n",
"\n",
"Yikes the language model is a bit whacky! Change the temperature and using sampling will definitely make the output much better!"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "QQMjaNrjsU5_"
},
"source": [
"You can also use Hugging Face's `AutoModelForPeftCausalLM`. Only use this if you do not have `unsloth` installed. It can be hopelessly slow, since `4bit` model downloading is not supported, and Unsloth's **inference is 2x faster**."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "yFfaXG0WsQuE"
},
"outputs": [],
"source": [
"if False:\n",
" # I highly do NOT suggest - use Unsloth if possible\n",
" from peft import AutoPeftModelForCausalLM\n",
" from transformers import AutoTokenizer\n",
"\n",
" model = AutoPeftModelForCausalLM.from_pretrained(\n",
" \"lora_model\", # YOUR MODEL YOU USED FOR TRAINING\n",
" load_in_4bit=load_in_4bit,\n",
" )\n",
" tokenizer = AutoTokenizer.from_pretrained(\"lora_model\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "f422JgM9sdVT"
},
"source": [
"### Saving to float16 for VLLM\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "iHjt_SMYsd3P"
},
"outputs": [],
"source": [
"# Merge to 16bit\n",
"if False: model.save_pretrained_merged(\"model\", tokenizer, save_method = \"merged_16bit\",)\n",
"if False: model.push_to_hub_merged(\"hf/model\", tokenizer, save_method = \"merged_16bit\", token = \"\")\n",
"\n",
"# Merge to 4bit\n",
"if False: model.save_pretrained_merged(\"model\", tokenizer, save_method = \"merged_4bit\",)\n",
"if False: model.push_to_hub_merged(\"hf/model\", tokenizer, save_method = \"merged_4bit\", token = \"\")\n",
"\n",
"# Just LoRA adapters\n",
"if False:\n",
" model.save_pretrained(\"model\")\n",
" tokenizer.save_pretrained(\"model\")\n",
"if False:\n",
" model.push_to_hub(\"hf/model\", token = \"\")\n",
" tokenizer.push_to_hub(\"hf/model\", token = \"\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "TCv4vXHd61i7"
},
"source": [
"### GGUF / llama.cpp Conversion\n",
"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.\n",
"\n",
"Some supported quant methods (full list on our [Wiki page](https://github.com/unslothai/unsloth/wiki#gguf-quantization-options)):\n",
"* `q8_0` - Fast conversion. High resource use, but generally acceptable.\n",
"* `q4_k_m` - Recommended. Uses Q6_K for half of the attention.wv and feed_forward.w2 tensors, else Q4_K.\n",
"* `q5_k_m` - Recommended. Uses Q6_K for half of the attention.wv and feed_forward.w2 tensors, else Q5_K."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FqfebeAdT073"
},
"outputs": [],
"source": [
"# Save to 8bit Q8_0\n",
"if False: model.save_pretrained_gguf(\"model\", tokenizer,)\n",
"if False: model.push_to_hub_gguf(\"hf/model\", tokenizer, token = \"\")\n",
"\n",
"# Save to 16bit GGUF\n",
"if False: model.save_pretrained_gguf(\"model\", tokenizer, quantization_method = \"f16\")\n",
"if False: model.push_to_hub_gguf(\"hf/model\", tokenizer, quantization_method = \"f16\", token = \"\")\n",
"\n",
"# Save to q4_k_m GGUF\n",
"if False: model.save_pretrained_gguf(\"model\", tokenizer, quantization_method = \"q4_k_m\")\n",
"if False: model.push_to_hub_gguf(\"hf/model\", tokenizer, quantization_method = \"q4_k_m\", token = \"\")\n",
"if False: model.push_to_hub_gguf(\"hf/model\", tokenizer, quantization_method = \"q5_k_m\", token = \"\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gCB7r0i2_3sS"
},
"source": [
"Now, use the `model-unsloth.gguf` file or `model-unsloth-Q4_K_M.gguf` file in llama.cpp.\n",
"\n",
"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!\n",
"\n",
"Some other links:\n",
"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)\n",
"2. Saving finetunes to Ollama. [Free notebook](https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Llama3_(8B)-Ollama.ipynb)\n",
"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)\n",
"6. See notebooks for DPO, ORPO, Continued pretraining, conversational finetuning and more on our [documentation](https://docs.unsloth.ai/get-started/unsloth-notebooks)!\n",
"\n",
"\n",
"

\n",
"

\n",
"

\n",
"\n",
" Join Discord if you need help + βοΈ
Star us on Github βοΈ\n",
"\n",
" This notebook and all Unsloth notebooks are licensed [LGPL-3.0](https://github.com/unslothai/notebooks?tab=LGPL-3.0-1-ov-file#readme).\n",
"
\n"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"cell_execution_strategy": "setup",
"gpuType": "L4",
"machine_shape": "hm",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}