Repository navigation
Use the library sandbox helper in the matmul GRPO notebooks - #364
Merged
Merged
Conversation
The 2048 and Sudoku RL notebooks already do
from unsloth import check_python_modules, create_locked_down_function
but the gpt-oss and Gemma4 matmul GRPO family still carries its own inline
copy of the helper:
def create_locked_down_function(function):
output_function = {}
exec(function, {}, output_function)
new_matmul = output_function["matmul"]
new_matmul = types.FunctionType(new_matmul.__code__, {})
return new_matmul
That copy runs the model's generated Python with full builtins. The
types.FunctionType(..., {}) line looks like it locks things down but does
not: CPython injects the real builtins module into any globals mapping with
no __builtins__ key. check_only_stdlib_imports does not help either, since
os, subprocess and socket are all stdlib, and its ok flag is computed and
then discarded by the reward functions.
The cell had spread to 35 files by copy paste. This switches the four
sources of truth to the library helper and regenerates the rest, so all of
them pick up the hardening in unslothai/unsloth-zoo#1105.
Sources of truth edited:
- original_template/gpt-oss-(20B)-GRPO.ipynb
- original_template/gpt-oss-(20B)_A100-GRPO.ipynb
- original_template/gpt_oss_(20B)_GRPO_BF16.ipynb
- nb/Gemma4_(E2B)_GRPO.ipynb, which has no original_template counterpart and
is therefore its own source
The remaining 31 files are regenerated output from update_all_notebooks.py
and update_all_notebooks.py --amd.
check_only_stdlib_imports and the surrounding explanation are left alone.
They still drive the no_cheating reward, which is what they were written
for. The demo cells that exec a hardcoded sample string are also untouched,
since that is a literal rather than model output.
Repo wide grep for "exec(function" now returns nothing.
tests/test_python_scripts_regen_parity.py passes 888/888.
One note for whoever regenerates next: the molab step fails silently if
marimo is not installed. It prints a warning and the generator still exits
0, which leaves the four molab files stale while the run looks like it
succeeded.
|
Codex Review: Didn't find any major issues. Breezy! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
The molab step in update_all_notebooks.py fails silently when marimo is missing: it prints a warning and the run still exits 0. I installed marimo to get past that, took the current release, and regenerated with 0.24.0 while the workflow pins 0.23.8. marimo stamps its version into __generated_with, which the workflow comment already warns about, so all four files carried "0.24.0" against a repo whose every other molab file says "0.23.8". test_molab_generation_parity regenerates with the pinned version and compares, so this was a parity break waiting for CI to reach it. Regenerated with marimo==0.23.8. Measured against a clean worktree of main rather than assuming: main fails 125 of these, this branch failed 126, and the one extra was Gemma4_(E2B)_GRPO. It is now 122, so the branch introduces no parity regression and repairs three of the pre-existing ones as a side effect, those being the other molab files it regenerates. The remaining 122 are pre-existing drift on main, untouched here.
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Companion to unslothai/unsloth-zoo#1105.
The 2048 and Sudoku RL notebooks already pull the sandbox helper from the library:
The gpt-oss and Gemma4 matmul GRPO family does not. It still carries its own inline copy:
That copy executes the model's generated Python with full builtins available. The
types.FunctionType(..., {})line reads as if it locks things down, but CPython injects the real builtins module into any globals mapping that has no__builtins__key, so it does not.check_only_stdlib_importsdoes not cover the gap either:os,subprocessandsocketare all stdlib and pass it, and theokflag it returns is computed and then discarded by the reward functions, which only testif function is None or "error" in info.So a completion of this shape runs on the user's Colab or Kaggle box:
The cell had spread to 35 files by copy paste. This PR switches the four sources of truth over to the library helper and regenerates the rest, so every one of them picks up the hardening in unslothai/unsloth-zoo#1105.
Sources of truth edited
original_template/gpt-oss-(20B)-GRPO.ipynboriginal_template/gpt-oss-(20B)_A100-GRPO.ipynboriginal_template/gpt_oss_(20B)_GRPO_BF16.ipynbnb/Gemma4_(E2B)_GRPO.ipynb, which has nooriginal_templatecounterpart and is therefore its own sourceThe other 31 files in the diff are regenerated output from
update_all_notebooks.pyandupdate_all_notebooks.py --amd.What is deliberately left alone
check_only_stdlib_importsand the explanation around it stay exactly as they are. They still drive theno_cheatingreward, which is what they were written for, and the teaching narrative about reward hacking is unchanged. The demo cells thatexeca hardcodedsamplestring are also untouched, since that is a literal rather than model output.No user facing change: same cells, same flow, same outputs.
Verification
exec(functionreturns nothing.tests/test_python_scripts_regen_parity.pypasses 888/888.import os,__import__,open,eval,getattrand().__class__.__bases__[0].__subclasses__()are blocked, while the 2048 sample usingimport mathandfrom typing import Callable, the matmul sample from this notebook, and the Sudoku sample all still run.One note for whoever regenerates next
The molab step fails silently when
marimois not installed. It prints a warning and the generator still exits 0, which leaves the four molab files stale while the run looks like it succeeded. I only caught it by grepping for the old cell afterwards. Might be worth making that step fail loudly.A full regeneration also produces a fair amount of drift unrelated to any given change: a marimo version bump, the announcement cell switching from Studio to Desktop, README rows for notebooks missing from the table,
scripts/model_created_at.csv, and a handful of AMD and molab variants that had never been generated. I reverted all of that here to keep this diff to the security fix. The four molab files are the exception, since marimo rewrites whole files and the version bump cannot be separated out.