Follow the HuggingFace Models Tutorial to download the LLaMA3.2 1B model, which can be run on volta32gb GPUs.
Once you have the model in your local path, (e.g.` /models/Llama-3.2-1B/original/consolidated.00.pth),
you need to register the model in a YAML card so that fairseq2 will know from where to pull the model
(read more about Assets). To do that:
Create a YAML file (e.g. my_llama3_2_1b.yaml) with the following content:
Follow the HuggingFace Datasets Tutorial to download the gsm8k data, (formatted with fairseq2 flavor) to your local path (e.g. /datasets/facebook/fairseq2-lm-gsm8k/).
We will use the sft/train.jsonl to fine-tune the model and use the test/test.jsonl for evaluation.
# /configs/example.yamldataset:/datasets/facebook/fairseq2-lm-gsm8k/sftmodel:llama3_2_1bmax_num_tokens:4096max_seq_len:4096max_num_steps:1000max_num_data_epochs:20checkpoint_every_n_steps:1000keep_last_n_checkpoints:1keep_last_n_models:1publish_metrics_every_n_steps:5dtype:float16# volta32gb gpus do not support bfloat16
Sometimes you may want to continue fine-tuning from a previously trained checkpoint, either to:
Resume interrupted training
Fine-tune on additional data
Perform iterative fine-tuning with different hyperparameters
fairseq2 provides a clean way to handle this through the checkpoint system (learn more about Checkpoint Management):
fairseq2lminstruction_finetune$OUTPUT_DIR--config\resume_checkpoint_dir=/path/to/checkpoint\model="last_checkpoint"\ # this will pick up the last checkpointdataset=/path/to/data
To pick up a specific checkpoint
CKPT_PATH="/checkpoint/user/experiments/run_0/checkpoints/step_1000"# this is the path to the checkpointCKPT_DIR=$(dirname"$CKPT_PATH")# e.g., /checkpoint/user/experiments/run_0/checkpointsCKPT="checkpoint_$(basename"$CKPT_DIR")"# e.g., checkpoint_step_1000
fairseq2lminstruction_finetune$OUTPUT_DIR--config\resume_checkpoint_dir=$CKPT_DIR\model=$CKPT\ # Must match the checkpoint stepdataset=/path/to/new/data\max_num_tokens=4096\dtype=float16
Note
If you want to pick a specific checkpoint instead of the last checkpoint, the model parameter must be set to checkpoint_step_X where X matches the step number of the checkpoint you want to load.
A more detailed example
For iterative fine-tuning across different datasets or with different hyperparameters:
# config.yaml# First stage - train on dataset Adataset:/path/to/dataset_Amodel:llama3_2_1bmax_num_steps:1000learning_rate:1e-5# ... other config
Then run the following commands in bash:
# First stage
fairseq2lminstruction_finetunerun1_output--config-fileconfig.yaml
# Second stage - continue from first stage checkpoint
fairseq2lminstruction_finetunerun2_output--config\resume_checkpoint_dir=run1_output/checkpoints\model=checkpoint_step_1000\dataset=/path/to/dataset_B\learning_rate=5e-6# Lower learning rate for second stagemax_num_steps=500
Tip
When doing iterative fine-tuning:
Generally use a lower learning rate in later stages
Consider reducing the number of steps for later stages
You may want to adjust the validation frequency
Make sure to track metrics to compare performance across stages
Once we have finished the training, we can find in the $OUTPUT_DIR the model checkpoints in $OUTPUT_DIR/checkpoints. With that, we can now generate over the test dataset!
To accelerate the inference process, we can convert fairseq2 checkpoints to HuggingFace checkpoints, which can be deployed with VLLM. This takes 2 steps:
Step 1: Convert fairseq2 checkpoint to XLFormer checkpoint
The first step is to use the fairseq2 command-line ( CLI) tool to convert the fairseq2 checkpoint to an XLF checkpoint. The command structure is as follows:
Once you generated the output, it is relatively trivial to compute the accuracy. Overall, you just need to:
Load the generated dataset
Load the original test dataset as ground truth
Compare and count the number of correct items
Some example utils functions
importreANS_RE=re.compile(r"#### (\-?[0-9\.\,]+)")INVALID_ANS="[invalid]"defextract_answer(completion:str)->str:""" Extract the answer from the completion. :param completion: The completion. :return: The answer. """globalANS_RE,INVALID_ANSmatch=ANS_RE.search(completion)ifmatch:match_str=match.group(1).strip()match_str=match_str.replace(",","")returnmatch_strelse:returnINVALID_ANSdefis_correct(model_completion:str,gt_example:str)->bool:""" Check if the model completion is correct. :param model_completion: The model completion. :param gt_example: The ground truth example. :return: True if the model completion is correct, False otherwise. """gt_answer=extract_answer(gt_example)assertgt_answer!=INVALID_ANSreturnextract_answer(model_completion)==gt_answer
That’s pretty much it to get you started. But you can do a lot more. fairseq2 is a powerful tool to help you accelerate and scale up your research. It allows:
Experiment with different hyper-parameter configurations;
Compare performance across various datasets or model architectures;
Profile resource usage and optimize training workflows;
Connect to your WanDB and monitor your experiments in real-time;