Llama-3.2-1B GSM8K SFT + RL (COMS 4705 A4)
Built with Llama. A model trained by Jieqi Li (jl7701) for Assignment 4. This repository contains the merged model and tokenizer, so loading it does not depend on a local Colab checkpoint.
The course-provided Llama-3.2-1B was first fine-tuned on GSM8K for two epochs using response-only cross-entropy. The SFT adapter was merged before REINFORCE training. Both stages used rank-8 LoRA on the query, key, value, and output attention projections, with alpha 32 and dropout 0.05.
RL used the assignment's numeric-answer reward and a minibatch-mean baseline, with a learning rate of 1e-6, an effective batch size of 64, and KL coefficient 0.1 against the frozen SFT model. Sampling used top-p 0.9 and a 300-token limit. The Tesla T4 run completed 12 Trainer steps (about 0.10 epoch) in 33.7 minutes. FP16 loss scaling skipped an overflowed update at step 2; gradient norms were finite afterward, and the saved adapter weights were verified to be finite.
On the first 200 GSM8K test questions, greedy decoding with a 300-token cap obtained 38/200 exact matches (19.0%), the same as SFT. The set of correct questions was unchanged; 9 generations differed and 4 parsed answers changed, all remaining incorrect. Answers were parseable for 191/200 questions. This is a subset result, not an evaluation on the full test split.
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
repo = "Jieqi-lab/Llama-3.2-1B-gsm8k-rl-jl7701"
tokenizer = AutoTokenizer.from_pretrained(repo)
model = AutoModelForCausalLM.from_pretrained(repo, dtype=torch.float16, device_map="auto")
model.eval()
question = "A robe takes 2 bolts of blue fiber and half that much white fiber. How many bolts in total does it take?"
prompt = (
"Solve the math problem step by step. End your solution with a line of the form "
"#### <ANSWER>, where <ANSWER> is the final number.\nProblem:\n"
+ question + "\nSolution:\n"
)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.inference_mode():
output = model.generate(**inputs, max_new_tokens=300, do_sample=False,
return_dict_in_generate=False,
eos_token_id=tokenizer.eos_token_id,
pad_token_id=tokenizer.eos_token_id)
print(tokenizer.decode(output[0, inputs.input_ids.shape[1]:], skip_special_tokens=True))
The notebook uses Transformers 5.22.2 and PEFT 0.22.0. The model follows the
Llama 3.2 Community License; the original attribution is retained in NOTICE.