Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Local Fine Tuning

This notebook is part of the Fine Tuning in 5, 15, 50 minutes guide as part of the https://ravinkumar.com/GenAiGuidebook/

This notebook was run on a local desktop with an RTX 4090 GPU.

Download model

This grabs the weights from the Hugging Face server.

Set quantization configuration

To tune a model with a 4090 it won’t fit in memory with higher precision. So we reduce the size of the model in memory with 4 bit quantization.

Loading...

Initial Generation

Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.
What is a recipe for eggs?

A basic recipe for cooking eggs could be:

1. Heat a small amount of oil or butter in a pan over medium heat.
2. Crack one or more eggs into the pan.
3. Cook until the whites are set but the yolks are still runny, or cook until the eggs are fully set to your liking.
4. Season with salt, pepper, or other desired spices.
5. Serve and enjoy!

There are many variations of this basic recipe, such as making an omelette, scrambling the eggs, or making a poached or fried egg. The cooking method and additional ingredients can be adjusted to suit personal preferences.

Load Training Data

These are the examples generated using the distillation notebook.

DatasetDict({ train: Dataset({ features: ['id', 'food', 'prompt', 'rewrite_prompt', 'completion'], num_rows: 20 }) })
'What is a recipe for Apple Slices with Cinnamon?'
**Ingredients:**

```json
{
  "apples": 2,
  "cinnamon": 1/2 teaspoon,
  "butter": 1/4 cup,
  "sugar": 1/2 cup,
  "lemon juice": 1 tablespoon
}
```

**Instructions:**

"Gather thy apples, fair and ripe,
And sprinkle cinnamon upon their slice,
With butter soft and golden spread,
And sugar sweet to sweeten the spread.
Add lemon juice, a touch of zest,
And bake in oven, till they rest
In golden glory, a delight,
To fill thy mouth with pure delight."

Perform Supervised Fine Tuning

We need to first format the distillation examples to fit the Mistral format. After that we can set the LORA config and other parameters. In particular the learning rate and max steps are critical.

['<|system|></s><|user|>What is a recipe for Apple Slices with Cinnamon?<|assistant|>**Ingredients:**\n\n```json\n{\n "apples": 2,\n "cinnamon": 1/2 teaspoon,\n "butter": 1/4 cup,\n "sugar": 1/2 cup,\n "lemon juice": 1 tablespoon\n}\n```\n\n**Instructions:**\n\n"Gather thy apples, fair and ripe,\nAnd sprinkle cinnamon upon their slice,\nWith butter soft and golden spread,\nAnd sugar sweet to sweeten the spread.\nAdd lemon juice, a touch of zest,\nAnd bake in oven, till they rest\nIn golden glory, a delight,\nTo fill thy mouth with pure delight."']
Loading...

Final Generation

After finetuning here’s what the final generation looks like.

Setting `pad_token_id` to `eos_token_id`:2 for open-end generation.
What is a recipe for eggs?

Ingredients:

```json
{
  "ingredients": [
    "2 eggs",
    "Salt to taste",
    "Pepper to taste",
    "Butter for frying"
  ]
}
```

Instructions:

```
To craft a dish of eggs,
Gather the ingredients, with care,
Two eggs, salt, pepper, and butter,
And a pan for frying, hot as a furnace.

Crack the eggs into a bowl,
And whisk them with a swift hand,
Add the salt and pepper,
To give them flavor, bold.

Heat the pan with the butter,
Until it sizzles and shines,
Pour in the eggs, and let them cook,
Until they are golden and bright.

Flip them over, with a gentle touch,
And cook the other side,
Until they are done to perfection,
A feast fit for a king.

Serve the eggs, hot and steaming,
With a slice of bread,
And enjoy the deliciousness,
Of this humble, yet divine, dish.
```

May your eggs be cooked with love,
And bring joy to your table,
For in their simplicity,
Lies the flavor of home.