Views
No views yet
TL;DR: Plug this adapter intounsloth/qwen3-4b-unsloth-bnb-4bit(or any compatible Qwen3-4B checkpoint), and you get a GRPO-tuned reasoning model that runs comfortably on a single consumer GPU.
unsloth/qwen3-4b-unsloth-bnb-4bit (Qwen3-4B with 4-bit quantization for faster/cheaper training & inference)unsloth/qwen3-4b-unsloth-bnb-4bit
License: Inherits the base Qwen3 license; add an adapter license (e.g., Apache-2.0) if desiredSafety note: Always apply your own content filters and human review in production settings.
1from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
2from peft import PeftModel
3import torch
4
5base_id = "unsloth/qwen3-4b-unsloth-bnb-4bit" # Base 4-bit model
6adapter_id = "your-username/your-adapter-repo" # <- replace with this repo id
7
8bnb_config = BitsAndBytesConfig(
9 load_in_4bit=True,
10 bnb_4bit_use_double_quant=True,
11 bnb_4bit_quant_type="nf4",
12 bnb_4bit_compute_dtype=torch.bfloat16,
13)
14
15tok = AutoTokenizer.from_pretrained(base_id, use_fast=True)
16base = AutoModelForCausalLM.from_pretrained(
17 base_id,
18 quantization_config=bnb_config,
19 device_map="auto"
20)
21
22model = PeftModel.from_pretrained(base, adapter_id).eval()
23
24prompt = "Explain why the derivative of x^2 is 2x, step by step."
25inputs = tok(prompt, return_tensors="pt").to(model.device)
26with torch.no_grad():
27 out = model.generate(
28 **inputs,
29 max_new_tokens=256,
30 temperature=0.7,
31 top_p=0.9,
32 do_sample=True
33 )
34print(tok.decode(out[0], skip_special_tokens=True))1from unsloth import FastLanguageModel
2import torch
3
4base_id = "unsloth/qwen3-4b-unsloth-bnb-4bit"
5adapter_id = "your-username/your-adapter-repo"
6
7model, tokenizer = FastLanguageModel.from_pretrained(
8 model_name=base_id,
9 max_seq_length=2048,
10 load_in_4bit=True,
11 dtype=torch.bfloat16,
12)
13model = FastLanguageModel.from_pretrained(model=model, model_name=adapter_id) # attach LoRA
14model.eval()
15
16prompt = "List three key differences between GRPO and PPO."
17inp = tokenizer(prompt, return_tensors="pt").to(model.device)
18with torch.no_grad():
19 out = model.generate(**inp, max_new_tokens=256, temperature=0.7, top_p=0.9, do_sample=True)
20print(tokenizer.decode(out[0], skip_special_tokens=True))1from transformers import AutoTokenizer, AutoModelForCausalLM
2from peft import PeftModel
3import torch
4
5base_id = "unsloth/qwen3-4b-unsloth-bnb-4bit"
6adapter_id = "your-username/your-adapter-repo"
7
8tok = AutoTokenizer.from_pretrained(base_id)
9base = AutoModelForCausalLM.from_pretrained(base_id, device_map="auto", load_in_4bit=True)
10model = PeftModel.from_pretrained(base, adapter_id).eval()
11
12messages = [
13 {"role": "system", "content": "You are a helpful AI assistant specialized in step-by-step reasoning."},
14 {"role": "user", "content": "Solve: If x + y = 10 and x - y = 2, what is x * y? Show the steps."},
15]
16text = tok.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
17inputs = tok([text], return_tensors="pt").to(model.device)
18
19with torch.no_grad():
20 outputs = model.generate(**inputs, max_new_tokens=256, temperature=0.7, top_p=0.9, do_sample=True)
21print(tok.decode(outputs[0], skip_special_tokens=True))HuggingFaceH4/aime_2024 for math-style promptsr = 16–64, alpha = 16–64, dropout = 0.05q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj (adjust to taste)Tip: If loss oscillates, lower LR, increase group size, and ensure reward normalization/stability in your GRPO config.
1from trl import GRPOConfig, GRPOTrainer
2from peft import LoraConfig
3from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
4import torch
5
6base_id = "unsloth/qwen3-4b-unsloth-bnb-4bit"
7
8bnb = BitsAndBytesConfig(
9 load_in_4bit=True, bnb_4bit_use_double_quant=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.bfloat16
10)
11
12model = AutoModelForCausalLM.from_pretrained(base_id, quantization_config=bnb, device_map="auto")
13tokenizer = AutoTokenizer.from_pretrained(base_id, use_fast=True)
14
15peft_config = LoraConfig(
16 r=32, lora_alpha=32, lora_dropout=0.05, bias="none",
17 task_type="CAUSAL_LM",
18 target_modules=["q_proj","k_proj","v_proj","o_proj","gate_proj","up_proj","down_proj"],
19)
20
21grpo_config = GRPOConfig(
22 learning_rate=1e-5,
23 beta=0.01,
24 group_size=4,
25 per_device_train_batch_size=12,
26 gradient_accumulation_steps=2,
27 num_train_epochs=3,
28 logging_steps=10,
29)
30
31trainer = GRPOTrainer(
32 model=model,
33 args=grpo_config,
34 processing_class=tokenizer,
35 peft_config=peft_config,
36 train_dataset=your_train_dataset, # replace
37 reward_funcs=[your_reward_fn], # replace
38)
39
40trainer.train()
41trainer.save_model("grpo-lora-adapter")1@software{qwen3_2024,
2 title={Qwen3 Language Models},
3 author={Qwen Team},
4 year={2024},
5 url={https://huggingface.co/Qwen}
6}
7
8@misc{trl_library,
9 title={{TRL}: Transformer Reinforcement Learning},
10 author={von Werra, L. and others},
11 year={2023},
12 howpublished={\url{https://github.com/huggingface/trl}}
13}LICENSE file if you want a distinct license for the adapter.