unsloth/gemma-4-E2B-it checkpoint, trains on an NVIDIA GPU, saves the adapter, checks every saved LoRA tensor, and returns JSON metrics before Compute terminates the machine.
gemma4_sudoku_rl.py is adapted from Unsloth’s Gemma 4 Sudoku notebook. This example is explicitly licensed LGPL-3.0-only; its SPDX header and source notice are part of the file. That license notice applies to this adapted example.Before you start
Install Compute, sign in, and add prepaid credit:Save the example
Download the complete script:gemma4_sudoku_rl.py::train. Its Compute image starts from CUDA PyTorch. The function installs pinned Unsloth packages, then applies the newer Transformers and TRL versions used by the upstream notebook. Installing inside the function puts dependency output in the run log. GPU-only imports happen afterward.
The core training setup is:
Inspect the payload
Run a local dry-run first:cuda_pytorch and the train entrypoint. A dry-run does not upload the file, request a quote, or create a machine.
Run one training step
An RTX 4090 has enough memory for the default E2B run. Vast.ai capacity is interruptible, so Compute may end the run if the provider reclaims the machine:--wait keeps the logs attached through training and teardown.
How the reward works
Each prompt asks Gemma to returndef strategy(board, initial) in a Python code block. The generated function gets three scores:
function_workschecks the exact signature and verifies that Unsloth can create a locked-down callable.no_cheatingrejects imports and gives them a heavy penalty.strategy_succeedsruns the function against a generated puzzle. A complete solution earns30.0; partial progress earns0.2per valid move.
Read the result
The final value is JSON. It includes the device and package versions, aggregatetrain_loss, per-step loss_history and reward_history, elapsed training time, and an adapter_verified flag. Inspect the histories instead of treating a successful exit as proof that the short training run improved. The exact metrics vary because generation and GPU training are stochastic.
The example writes the adapter to /tmp/gemma4-sudoku-lora and opens adapter_model.safetensors before returning. The run fails if the file is empty or any adapter tensor is all zero.
Train for longer
The upstream notebook uses 60 steps and 1,000 copies of the prompt. Run that shape on reserved capacity with a longer timeout:General reinforcement learning
Run a smaller GRPO example on MI300X.
CLI run reference
Pass arguments, choose a GPU, and control the timeout.
