Coaching Gemma-3 for Structured Mathematical Reasoning with Tunix GRPO, LoRA Adapters, and GSM8K Rewards

Coaching Gemma-3 for Structured Mathematical Reasoning with Tunix GRPO, LoRA Adapters, and GSM8K Rewards


import importlib.util, os, shutil as _sh
if importlib.util.find_spec("tunix") is None:
   print("Putting in Tunix + JAX ecosystem — this takes ~5-8 min…")
   %pip set up -q ipywidgets tensorboardX transformers grain nest_asyncio
   %pip set up -q datasets huggingface_hub "numpy>2"
   %pip set up -q tensorflow tensorflow_datasets
   %pip set up -q git+https://github.com/jax-ml/jax
   %pip set up -q git+https://github.com/google/tunix
   %pip set up -q git+https://github.com/google/qwix
   %pip uninstall -q flax -y
   %pip set up -q git+https://github.com/google/flax
   %pip uninstall -q wandb -y
   print("nn✅ Set up accomplished. The runtime will RESTART now.")
   print("👉  After it restarts, RUN THIS CELL AGAIN to begin coaching.n")
   os.kill(os.getpid(), 9)
import getpass, functools, json, re
import numpy as np
os.environ["WANDB_MODE"] = "disabled"
os.environ["TOKENIZERS_PARALLELISM"] = "false"
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"
HF_TOKEN = os.environ.get("HF_TOKEN")
if not HF_TOKEN:
   strive:
       from google.colab import userdata
       HF_TOKEN = userdata.get("HF_TOKEN")
   besides Exception:
       HF_TOKEN = getpass.getpass("Hugging Face token (wants Gemma license entry): ")
os.environ["HF_TOKEN"] = HF_TOKEN or ""
import nest_asyncio; nest_asyncio.apply()
import tensorflow as tf; tf.config.set_visible_devices([], "GPU")
import jax, jax.numpy as jnp, optax, grain, qwix
from flax import nnx
from orbax import checkpoint as ocp
from huggingface_hub import snapshot_download, login
from datasets import load_dataset
from tunix.generate import sampler as sampler_lib
from tunix.generate import tokenizer_adapter as tokenizer_lib
from tunix.fashions.gemma3 import mannequin as gemma_lib
from tunix.fashions.gemma3 import params_safetensors as params_safetensors_lib
from tunix.fashions.gemma3 import params as gemma_params
from tunix.rl import rl_cluster as rl_cluster_lib
from tunix.rl.grpo.grpo_learner import GRPOConfig, GRPOLearner
from tunix.rl.rollout import base_rollout
from tunix.sft import metrics_logger
if HF_TOKEN:
   login(token=HF_TOKEN)
gadgets = jax.gadgets()
print("JAX backend :", jax.default_backend(), "|", len(gadgets), "machine(s):", gadgets)
IS_GPU = _sh.which("nvidia-smi") shouldn't be None
if IS_GPU and jax.default_backend() == "cpu":
   print("⚠️  GPU runtime however JAX sees CPU solely. Re-run `pip set up -U "jax[cuda12]"` "
         "and restart, or swap to a TPU runtime.")
MODEL_ID = "google/gemma-3-1b-it"
RANK, ALPHA = 32, 32.0
MAX_PROMPT_LENGTH      = 256
TOTAL_GENERATION_STEPS = 512
NUM_GENERATIONS        = 2
NUM_ITERATIONS         = 1
BETA                   = 0.08
EPSILON                = 0.2
TEMPERATURE, TOP_P, TOP_K = 0.9, 1.0, 50
MAX_STEPS   = 100
TRAIN_LIMIT = MAX_STEPS
NUM_TEST    = 16
LEARNING_RATE, B1, B2, WEIGHT_DECAY = 3e-6, 0.9, 0.99, 0.1
WARMUP_STEPS, MAX_GRAD_NORM = int(0.1 * MAX_STEPS), 0.1
CKPT_DIR, TB_DIR = "/content material/ckpts/", "/content material/tb/grpo"
N = jax.device_count()
MESH = [(N, 1), ("fsdp", "tp")]
mesh = jax.make_mesh(*MESH, axis_types=(jax.sharding.AxisType.Auto,) * 2)



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *