Downloads · 30 days
15
9% of all-time downloads
Nirav-Madhani/gemma3-270m-grpo-math
gemma3-270m-grpo-math is a text generation model from Nirav-Madhani. Use it when you need the model to write or continue text. It is set up for transformers. The card lists the license as gemma.
<div style="border-left:6px solid facc15; background:fffbe6; padding:12px; border-radius:6px; margin:12px 0;" <strong⚠️ Experimental training run.</strong This is a personal experiment and <strongnot production-ready<…
Downloads · 30 days
15
9% of all-time downloads
All-time downloads
162
Public
Parameters
268M
1.1 GB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors1.1 GB · 97%
From the Hugging Face model README
Small reasoning-tuned variant of Google’s Gemma-3-270M.
Two-stage recipe: SFT on math prompts with hidden <think>…</think> reasoning, then GRPO to reinforce correct final answers while discouraging overly long hidden reasoning.
Note: there is no <final> tag in this project. The final answer is emitted as \boxed{...} after the <think> block.
The model can emit
<think>…</think>tokens. Examples below strip this by default.
google/gemma-3-270m= 1.0 if the boxed answer matches ground truth (numeric or sympy-equivalent), else 0.0, minus a small penalty proportional to tokens inside <think>…</think>. KL regularization to the SFT reference.Training/eval wrapper (no <final> tag):
<prompt>
<YOUR_QUESTION_HERE>
</prompt>
<think>
…(internal scratch work)…
</think>
\boxed{FINAL_ANSWER}
format_sft_example(question, reasoning, final_answer)<prompt>…</prompt>\n<think>\n and expect the model to write reasoning + \boxed{...}.from transformers import AutoTokenizer, AutoModelForCausalLM
import torch, re
MODEL_ID = "nirav-madhani/gemma3-270m-grpo-math" # change if different
tok = AutoTokenizer.from_pretrained(MODEL_ID, use_fast=True)
if tok.pad_token is None:
tok.pad_token = tok.eos_token
device = "cuda" if torch.cuda.is_available() else "cpu"
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
torch_dtype=torch.float32,
low_cpu_mem_usage=True,
attn_implementation="eager",
).to(device).eval()
BOX_RE = re.compile(r"\\boxed\\{([^{}]+)\\}")
def generate(question, max_new_tokens=160, temperature=0.2, top_p=0.95, return_boxed=True, show_think=False):
prompt = f"<prompt>\n{question}\n</prompt>\n<think>\n"
inputs = tok(prompt, return_tensors="pt").to(device)
with torch.no_grad():
out = model.generate(
**inputs,
max_new_tokens=max_new_tokens,
do_sample=temperature > 0,
temperature=temperature,
top_p=top_p,
eos_token_id=tok.eos_token_id,
pad_token_id=tok.pad_token_id,
use_cache=True,
)
text = tok.decode(out[0], skip_special_tokens=True)
if not show_think:
text = re.sub(r"<think>.*?</think>\s*", "", text, flags=re.S)
if return_boxed:
m = BOX_RE.search(text)
return m.group(1).strip() if m else text.strip()
return text
print(generate("If 3x + 5 = 17, what is x?"))
Stage 1 — SFT
transformers==4.55.x, trl==0.21.0pad_token = eos_tokenTrainer + DataCollatorForLanguageModelingmax_seq_length = prompt_len + completion_len; small per-device batch with grad accumulation; BF16/FP16 on Ampere, else FP32.Stage 2 — RL (GRPO)
trl==0.21.0)+1.0 if \boxed{...} equals ground truth (float tolerance or sympy equivalence)−λ * (#tokens inside <think>…</think>)num_generations (K): 2–8per_device_train_batch_size: 1–2gradient_accumulation_steps: 4–8batch * accum * world_size is divisible by K)max_prompt_length: ~160–256max_completion_length: ~128–192beta (KL): ~0.02attn_implementation="eager"; enable use_cache=True if you have headroomCheckpointing
/kaggle/working.checkpoint-XXXX/, else RL root → SFT → base.Early-stage. Evaluate on your math split or GSM-style test by extracting \boxed{…} and checking numeric or sympy-equivalence. Track:
license: gemma. Review Gemma’s terms before commercial use/redistribution.attn_implementation="eager" to avoid FA mismatches.0.0 — monitor reward/KL/length.TRANSFORMERS_NO_TORCHVISION=1,numpy/scikit-learn on Kaggle if transformers.generation pulls sklearn.google/gemma-3-270mtrl==0.21.0)@software{nirav_gemma3_270m_grpo_math_2025,
title = {Gemma-3-270M GRPO (math + CoT)},
author = {Nirav Madhani},
year = {2025},
url = {https://huggingface.co/nirav-madhani/gemma3-270m-grpo-math}
}