Notebooks
U
Unsloth
EmbeddingGemma (300M)

EmbeddingGemma (300M)

unsloth-notebooksunslothnb

To run this, press "Runtime" and press "Run all" on a free Tesla T4 Google Colab instance!

Join Discord if you need help + ⭐ Star us on Github

To install Unsloth on your local device, follow our guide. This notebook is licensed LGPL-3.0.

You will learn how to do data prep, how to train, how to run the model, & how to save it

News

Train MoEs - DeepSeek, GLM, Qwen and gpt-oss 12x faster with 35% less VRAM. Blog

You can now train embedding models 1.8-3.3x faster with 20% less VRAM. Blog

Ultra Long-Context Reinforcement Learning is here with 7x more context windows! Blog

3x faster LLM training with 30% less VRAM and 500K context. 3x faster500K Context

New in Reinforcement Learning: FP8 RLVision RLStandbygpt-oss RL

Visit our docs for all our model uploads and notebooks.

Installation

[ ]

Unsloth

[ ]
🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.
🦥 Unsloth Zoo will now patch everything to make training faster!
==((====))==  Unsloth 2025.12.8: Fast Gemma3 patching. Transformers: 4.57.3.
   \\   /|    Tesla T4. Num GPUs = 1. Max memory: 14.741 GB. Platform: Linux.
O^O/ \_/ \    Torch: 2.9.0+cu126. CUDA: 7.5. CUDA Toolkit: 12.6. Triton: 3.5.0
\        /    Bfloat16 = FALSE. FA [Xformers = 0.0.33.post1. FA2 = False]
 "-____-"     Free license: http://github.com/unslothai/unsloth
Unsloth: Fast downloading is enabled - ignore downloading bars which are red colored!
Unsloth: Using float16 precision for gemma3 won't work! Using float32.
Unsloth: Gemma3 does not support SDPA - switching to fast eager.

We now add LoRA adapters so we only need to update a small amount of parameters!

[2]
Unsloth: Making `model.base_model.model` require gradients

Data Prep

We now use the tomaarsen/miriad-4.4M-split dataset, a large-scale collection of 4.4 million medical question-answer pairs distilled from peer-reviewed biomedical literature. To maintain efficiency, we use data streaming to ingest a subset of 10,000 training samples and 2,000 evaluation samples.

[3]
Resolving data files:   0%|          | 0/42 [00:00<?, ?it/s]
Resolving data files:   0%|          | 0/42 [00:00<?, ?it/s]
Resolving data files:   0%|          | 0/42 [00:00<?, ?it/s]
Resolving data files:   0%|          | 0/42 [00:00<?, ?it/s]

Let's take a look at the dataset structure:

[4]
{'question': 'What factors may contribute to increased pulmonary conduit durability in patients who undergo the Ross operation compared to those with right ventricular outflow tract obstruction?\n',
, 'passage_text': "I n 1966, Ross and Somerville 1 reported the first use of an aortic homograft to establish right ventricle-to-pulmonary artery continuity in a patient with tetralogy of Fallot and pulmonary atresia. Since that time, pulmonary position homografts have been used in a variety of right-sided congenital heart lesions. Actuarial 5-year homograft survivals for cryopreserved homografts are reported to range between 55% and 94%, with the shortest durability noted in patients less than 2 years of age. 4 Pulmonary position homografts also are used to replace pulmonary autografts explanted to repair left-sided outflow disease (the Ross operation). Several factors may be likely to favor increased pulmonary conduit durability in Ross patients compared with those with right ventricular outflow tract obstruction, including later age at operation (allowing for larger homografts), more normal pulmonary artery architecture, absence of severe right ventricular hypertrophy, and more natural positioning of the homograft. However, this concept has not been systematically studied. Only a small number of Ross and non-Ross patients have been compared, and these were in the context of a broad study of cryopreserved homografts in the pulmonary position. 5 The present study directly compares Ross versus non-Ross homograft survival in pediatric patients followed serially after surgical intervention during the first decade of life.\n\n The hospital records of all patients less than 10 years of age receiving primary cryopreserved right ventricle-to-pulmonary artery homografts at Children's Hospital of New York from July 1989 through October 2003 were reviewed. Cryopreserved homografts were obtained from Cryolife, Inc (Kennesaw, Ga). All patients who were followed up for longer than 4 months were included in the study unless graft failure occurred earlier (n ϭ 5). Hospital records, including operative reports, catheterization data, and echocardiographic studies, were retrospectively reviewed. The study protocol was reviewed and approved by the institutional review board. Ninety-eight consecutive patients were included in the study.\n\n Homograft failure was defined as need for surgical replacement or catheter balloon dilatation and/or stent implantation because of right ventricular outflow tract obstruction. Indications for intervention were determined by the primary cardiologist on the basis of the presence of right ventricular hypertrophy and 2-dimensional and Doppler echocardiographic evidence of significant outflow tract obstruction. In addition to Ross versus non-Ross comparisons, age at operation, length of follow-up, type of operation, homograft type, and size were analyzed as other potential risk factors for homograft failure.\n\n Statistical analysis was performed with the SAS 8.2 software (SAS Institute). Continuous variables were compared between subjects with and without graft failure by using unpaired t test. Categorical variables were compared by the Fisher's exact test. Kaplan-Meier curves were constructed for graft survival, and the effect of Ross versus non-Ross operation, as well as other potential covariates, was assessed by Cox proportional hazards models. Variable selection was performed by backwards elimination for multivariate modeling. Values are presented as means Ϯ SD.\n\n Ninety-eight patients were included in the study (Table 1) . Twenty-six patients underwent the Ross procedure for left-sided heart disease. Seventy-two patients with right ventricular outflow tract obstruction (non-Ross group) were studied.\n\n The mean follow-up time was 5.1 years (range, 1.25 months-14.7 years) for all patients ( Table 1) Table 1 ). Figure 1 demonstrates the age distribution, which was not statistically different between the Ross and non-Ross groups. The homograft size was greater in the Ross group (19.2 Ϯ 3.9 vs 16.5 Ϯ 4.8 mm, P ϭ .02), and more pulmonary type homografts were used in the Ross group (73% vs 40%, P ϭ .01; Table 1 ).\n\n The non-Ross group consisted of 3 major groups (Table  1) : patients undergoing homograft placement as a component of the Rastelli procedure (n ϭ 23), variants of tetralogy of Fallot (n ϭ 37), or truncus arteriosus (n ϭ 11) repair. Patients with truncus arteriosus were the youngest in this group (0.09 Ϯ 0.3 years), and these patients had smaller homografts placed (13.0 Ϯ 5.5 mm).\n\n The characteristics of patients with homograft failure are listed in Table 2 ."}

Baseline Performance

Now after fine-tuning lets evaluate the model!

[5]
{'cosine_accuracy@1': 0.8715,
, 'cosine_accuracy@3': 0.947,
, 'cosine_accuracy@5': 0.9605,
, 'cosine_accuracy@10': 0.975,
, 'cosine_precision@1': 0.8715,
, 'cosine_precision@3': 0.31566666666666665,
, 'cosine_precision@5': 0.19210000000000002,
, 'cosine_precision@10': 0.0975,
, 'cosine_recall@1': 0.8715,
, 'cosine_recall@3': 0.947,
, 'cosine_recall@5': 0.9605,
, 'cosine_recall@10': 0.975,
, 'cosine_ndcg@10': 0.926691286916503,
, 'cosine_mrr@10': 0.9108168650793647,
, 'cosine_map@100': 0.9117156470992975}

Train the model

Now let's train our model. We use MultipleNegativesRankingLoss

This loss function uses other positives in the same batch as negative examples, which is efficient for contrastive learning.

We do 30 steps to speed things up, but you can set num_train_epochs=1 for a full run, and turn off max_steps=None.

[6]
Computing widget examples:   0%|          | 0/1 [00:00<?, ?example/s]
[7]
GPU = Tesla T4. Max memory = 14.741 GB.
7.74 GB of memory reserved.

Let's train the model! To resume a training run, set trainer.train(resume_from_checkpoint = True)

[8]
==((====))==  Unsloth - 2x faster free finetuning | Num GPUs used = 1
   \\   /|    Num examples = 10,000 | Num Epochs = 1 | Total steps = 30
O^O/ \_/ \    Batch size per device = 64 | Gradient accumulation steps = 2
\        /    Data Parallel GPUs = 1 | Total batch size (64 x 2 x 1) = 128
 "-____-"     Trainable parameters = 13,074,432 of 315,937,536 (4.14% trained)
[9]
2769.2485 seconds used for training.
46.15 minutes used for training.
Peak reserved memory = 11.287 GB.
Peak reserved memory for training = 3.547 GB.
Peak reserved memory % of max memory = 76.569 %.
Peak reserved memory for training % of max memory = 24.062 %.

Now after fine-tuning lets evaluate the model!

[15]
{'cosine_accuracy@1': 0.883,
, 'cosine_accuracy@3': 0.9555,
, 'cosine_accuracy@5': 0.967,
, 'cosine_accuracy@10': 0.9795,
, 'cosine_precision@1': 0.883,
, 'cosine_precision@3': 0.31849999999999995,
, 'cosine_precision@5': 0.19340000000000004,
, 'cosine_precision@10': 0.09795000000000002,
, 'cosine_recall@1': 0.883,
, 'cosine_recall@3': 0.9555,
, 'cosine_recall@5': 0.967,
, 'cosine_recall@10': 0.9795,
, 'cosine_ndcg@10': 0.9350705550252689,
, 'cosine_mrr@10': 0.9203962301587296,
, 'cosine_map@100': 0.9213114362986002}

In just 30 steps, the model's Accuracy@1 increased from 0.871 to 0.883, demonstrating how quickly the model adapts to the specific dataset.

Inference

Let's run the model after training to see the improvements!

[22]
0.7012 | Acute Pericarditis often involves pleuritic chest pain relieved by sitting up and leaning forward.
0.5854 | Pneumothorax is characterized by sudden onset shortness of breath and unilateral chest pain.
0.5420 | Myocardial Infarction typically presents with crushing substernal pressure and radiation to the left arm.
0.4211 | Gastroesophageal Reflux Disease (GERD) causes burning retrosternal pain usually after meals.

Saving, loading finetuned models

To save the final model as LoRA adapters, either use Hugging Face's push_to_hub for an online save or save_pretrained for a local save.

[NOTE] This ONLY saves the LoRA adapters, and not the full model. To save to 16bit, scroll down!

[17]

Now if you want to load the LoRA adapters we just saved for inference, set False to True:

[18]

Saving to float16 for VLLM

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. See our docs for more deployment options.

[19]

GGUF / llama.cpp Conversion

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.

Some supported quant methods (full list on our docs page):

  • q8_0 - Fast conversion. High resource use, but generally acceptable.
  • q4_k_m - Recommended. Uses Q6_K for half of the attention.wv and feed_forward.w2 tensors, else Q4_K.
  • q5_k_m - Recommended. Uses Q6_K for half of the attention.wv and feed_forward.w2 tensors, else Q5_K.
[ ]

And we're done! If you have any questions on Unsloth, we have a Discord 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 resources:

  1. Looking to use Unsloth locally? Read our Installation Guide for details on installing Unsloth on Windows, Docker, AMD, Intel GPUs.
  2. Learn how to do Reinforcement Learning with our RL Guide and notebooks.
  3. Read our guides and notebooks for Text-to-speech (TTS) and vision model support.
  4. Explore our LLM Tutorials Directory to find dedicated guides for each model.
  5. Need help with Inference? Read our Inference & Deployment page for details on using vLLM, llama.cpp, Ollama etc.

Join Discord if you need help + ⭐️ Star us on Github ⭐️

This notebook and all Unsloth notebooks are licensed LGPL-3.0