deepblue
All Posts

Finetune Mistral 7b on Colab Free Version

AuthorsVinod Thomas
Published
Organizationdeepblue Research
CategoryTechnology, Deep Learning
rainbow in front of a green cliff and a waterfall.

Overview

The article discusses working with Mistral 7B in the free Google Colab environment, described in the post as providing a single T4 GPU with 15 GB of memory. It argues that the full model is too large for straightforward training in that environment and uses a quantized checkpoint to reduce memory requirements.

The selected checkpoint is:

1TheBloke/Mistral-7B-Instruct-v0.2-GPTQ

GPTQ quantization reduces the number of bits used to represent model weights. This can reduce memory use and make model loading and inference more practical on limited hardware.

Download and load the model

1model_name = "TheBloke/Mistral-7B-Instruct-v0.2-GPTQ"
2
3model = AutoModelForCausalLM.from_pretrained(
4 model_name,
5 device_map="auto",
6 trust_remote_code=False,
7 revision="main"
8)

The article then addresses missing imports and dependencies:

1from transformers import AutoModelForCausalLM

It identifies Accelerate as a requirement for automatic device mapping:

1pip install accelerate

It also identifies Hugging Face Optimum and auto-gptq as dependencies for GPTQ support:

1pip install optimum
2pip install auto-gptq

Compatibility note: These commands reflect the 2024 post. GPTQ package support and installation requirements have changed, so they may need updating in a current environment.

Save the model

1model.save_pretrained("/location/where/you/want/to/save/model")

The article describes three files in the saved directory:

  • model.safetensors --- model weights (the post reports approximately 3.9 GB).
  • generation_config.json --- generation settings.
  • config.json --- model architecture and configuration.

Reload the model

1model = AutoModelForCausalLM.from_pretrained(
2 "/location/where/you/saved/the/model",
3 device_map="auto"
4)

Load the tokenizer

1from transformers import AutoTokenizer
2
3tokenizer = AutoTokenizer.from_pretrained(
4 "/location/where/you/saved/the/model"
5)

The tokenizer should correspond to the selected model.

Format an instruction prompt

The article uses the Mistral Instruct format:

1prompt = "[INST]Can you give me the recipe chicken curry[/INST]"

The [INST] and [/INST] markers delimit the instruction.

Tokenize the prompt

1inputs = tokenizer(prompt, return_tensors="pt")

The resulting structure contains:

  • input_ids: token IDs representing the prompt.
  • attention_mask: indicators for which tokens should be attended to.

The article includes an example tensor and explains that each integer in input_ids maps to a token in the model vocabulary.

Inference example --- supplemental

The captured article excerpt ends while explaining tokenization. The following is a conventional illustrative completion, not code recovered from the original article:

1inputs = {key: value.to(model.device) for key, value in inputs.items()}
2
3outputs = model.generate(
4 **inputs,
5 max_new_tokens=200
6)
7
8print(tokenizer.decode(outputs[0], skip_special_tokens=True))

Workflow summary

  1. Select a GPTQ-quantized Mistral 7B Instruct checkpoint.
  2. Install the Transformers, Accelerate, Optimum, and AutoGPTQ dependencies described in the post.
  3. Load the model with automatic device placement.
  4. Save and reload the model.
  5. Load the matching tokenizer.
  6. Format an instruction using Mistral's [INST] ... [/INST] convention.
  7. Tokenize the prompt in preparation for inference.

Important distinction: Although the title refers to fine-tuning, the accessible portion of the article focuses on loading, saving, and preparing the quantized model for inference. The complete fine-tuning procedure was not present in the retrieved page excerpt.

Related Posts

a ravine originating at a mountain glacier running between two cliffs
TechnologyDeep Learning

Finetuning mistral 7b using Unsloth

The article uses Unsloth to fine-tune Mistral 7B in Google Colab. It introduces Unsloth as a way to fine-tune models such as Mistral and Gemma.

Read Post