Views
No views yet
Qwen/Qwen3-4B model fine-tuned on a 10% subset of the GSM8k dataset using GRPO (Grounded Reward Preference Optimization). The training was accelerated using the Unsloth library.Qwen/Qwen3-4Btrl.GRPOTrainer)openai/gsm8k.1import torch
2from unsloth import FastLanguageModel
3
4# Load the base model with Unsloth, using the same settings as training
5model, tokenizer = FastLanguageModel.from_pretrained(
6 model_name="Qwen/Qwen3-4B",
7 max_seq_length=2048,
8 load_in_4bit=True,
9 dtype=torch.bfloat16, # Use bfloat16 for inference
10)
11
12# Load the LoRA adapter from the Hub
13model = FastLanguageModel.from_pretrained(
14 model=model,
15 model_name="tahamajs/Qwen3-4B-GSM8k-GRPO-Unsloth", # Your Hub model ID
16)
17
18# --- Define Prompt and Generate ---
19SYSTEM_PROMPT = (
20 "You are a helpful assistant.\n"
21 "First think through the problem, then provide the answer.\n"
22 "Use this strict format:\n"
23 "<reasoning>\n"
24 "your step-by-step reasoning here\n"
25 "</reasoning>\n"
26 "<answer>\n"
27 "The final answer is [final_number].\n"
28 "</answer>\n"
29)
30
31question = "Natalia sold 48 liters of milk in the morning. In the afternoon, she sold 27 liters less than in the morning. In the evening, she sold 15 liters more than in the afternoon. How many liters of milk did she sell in total?"
32
33messages = [
34 {"role": "system", "content": SYSTEM_PROMPT},
35 {"role": "user", "content": question},
36]
37prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
38
39inputs = tokenizer([prompt], return_tensors="pt").to("cuda")
40outputs = model.generate(**inputs, max_new_tokens=512, use_cache=True)
41response = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
42
43# Extract only the generated part
44generated_part = response.split("<|im_start|>assistant\n")[-1]
45print(generated_part)trl's GRPOTrainer.r_correctness) with a weight of 2.0.β): 0.01r): 160.00059k): 3