pocket-math-ops / code /README.md
aafimalek2032's picture
code ca5ec3214eaa
b16b327 verified
|
Raw History Blame Contribute Delete
9.65 kB

Pocket Math

Pocket Math trains VibeThinker-1.5B into the strongest laptop-sized math model we can make on free Kaggle T4s. It uses on-policy distillation from a stronger teacher, then GRPO, then voting with a Python tool. docs/PLAN.md has the goals and the schedule. docs/DECISIONS.md records every design decision and why. docs/PAPER_NOTES.md records every number that may reach the paper.

How a job runs

Each training run, evaluation and check is a job in a queue. A Kaggle session runs whichever job is next, for up to 11 h 15 min. It saves everything to Hugging Face and stops cleanly before Kaggle's 12-hour limit. The next session, on any account, continues from the last save.

queue.yaml (ops repo) ──> Kaggle session picks the first runnable job
                              │ claims the job's lease (only one session writes a job)
                              ▼
         one round = generate (vLLM) → score (teacher) or grade (Math-Verify) → train (LoRA, 2 GPUs)
                              │ each finished phase is one commit to the job's own HF repo
                              ▼
         pm-<job> repo: state.json, ckpt/, metrics.jsonl, round/, milestones/, final/, results/

A crash or a Kaggle timeout loses at most the phase that was running. Two sessions can never write the same job: the lease is a compare-and-swap on the repo's head commit, so a session whose lease expired fails its next write and stops.

Who does what

Who Does what How often
You Create the HF organisation, invite friends, review every number before it goes in the paper Once, then at each gate
Friends Open their copy of the runner notebook and click Save Version When a session ends, about twice a day per running chain
Claude Queues jobs, publishes code, launches sessions on your account, reads results, makes the gate decisions in docs/PLAN.md, keeps docs/DECISIONS.md current Throughout

Set up once

  1. Create a Hugging Face organisation. Ours is Palaestra.
  2. Invite each friend to the organisation with the write role. A write member can write to every repo in the organisation, so any friend can continue a job another friend started.
  3. Give Claude a classic Write token through the secret prompt. It is kept at ~/.pocket-math-secrets/hf_token, outside the repo. A fine-grained token can't write to repos in a new organisation.
  4. Tell Claude the organisation name and which Kaggle account is yours.

Claude then runs the commands in the next section.

Start the project

These are the first commands, in order. PM_OPS_REPO saves typing.

cd projects/pocket-math
export PM_OPS_REPO=Palaestra/pocket-math-ops HF_TOKEN=$(cat ~/.pocket-math-secrets/hf_token)
python -m pocket_math ops init --org Palaestra              # creates the ops and data repos
python -m pocket_math ops publish-code                        # code snapshot, tagged with the git SHA
python kaggle/make_notebook.py --ops-repo $PM_OPS_REPO        # the notebook friends copy
python -m pocket_math ops add data-build data-build.yaml      # dev, fast-dev and the pool, built on a GPU
python -m pocket_math ops add smoke smoke.yaml
python -m pocket_math ops add opd-smoke opd-smoke.yaml --needs smoke
python -m pocket_math ops add grpo-smoke grpo-smoke.yaml --needs smoke
python kaggle/launch_own.py secrets                           # once: token to a private Kaggle dataset
python kaggle/launch_own.py push --ops-repo $PM_OPS_REPO

The data-build job publishes dev.jsonl, fastdev.jsonl, pool.jsonl and the decontamination report to <org>/pocket-math-data. Its manifest (row counts, sha256 values, dataset revisions) is in the job repo under results/manifest.json, and the sha256 values go in docs/PAPER_NOTES.md. To build on a laptop instead, run python -m pocket_math.data build --out data_out --embed-limit 20000 and ops upload-data.

The SFT trace sets for steps 3.1 and 3.4 are built on a laptop (no GPU, about 2 hours, mostly downloads and answer checks) and uploaded where sft-warmup.yaml and sft-tool.yaml read them. Their row counts, filters and sha256 values are in data_out/sft/manifest.json:

python -m pocket_math.traces build --out data_out/sft
python -m pocket_math ops upload-data data_out/sft/warmup.jsonl data_out/sft/tool.jsonl data_out/sft/manifest.json --prefix sft/

After the smoke jobs, Gate 0 picks the teacher. Then the baselines and the training chain go in the queue:

python -m pocket_math ops add eval-vt15-dev eval.yaml --set k=4
python -m pocket_math ops add opd-8k opd-8k.yaml --needs opd-smoke
python -m pocket_math ops add opd-16k opd-16k.yaml --needs opd-8k
python -m pocket_math ops add grpo-16k grpo-16k.yaml --needs opd-16k

Run and watch jobs

python -m pocket_math ops status                       # every job: status, round, who holds it, last error
python -m pocket_math ops compare eval-a eval-b --cap 32768   # paired bootstrap between two eval jobs
python -m pocket_math ops reset opd-8k                 # after fixing the cause of a failed job
python -m pocket_math ops set opd-8k optim.lr=3e-5     # change a started job's config (only while no session holds it)

A job that fails twice in a row is marked failed, so no session wastes quota on it. Its traceback is in logs/last_error.txt in the job repo.

To evaluate a training checkpoint while training continues, point an eval job at a milestone:

python -m pocket_math ops add eval-opd8k-r40 eval.yaml \
    --set model=Palaestra/pm-opd-8k:milestones/r0040 --set problems=Palaestra/pocket-math-data:fastdev.jsonl

To split a long evaluation across accounts, queue one job per shard with --set shard=[0,4], --set shard=[1,4], and so on.

Add a friend

Send them docs/FRIENDS.md. It takes about 10 minutes once, then one click per session.

Reproduce a result

Every job repo holds what you need to rerun it:

  • config.json is the exact config, frozen when the job first started. Changes made with ops set are listed in state.json under config_changes.
  • Every row of metrics.jsonl and every state.json records code_sha, the git commit of the code that wrote it.
  • env/<session>/ holds pip freeze for both Python environments and nvidia-smi for every session that ran the job.
  • Prompts for round r are a pure function of seed, epoch and cursor in state.json. Rerunning a round picks the same problems.

The data files are identified by the sha256 values in docs/PAPER_NOTES.md.

Run the tests

pip install -e ".[train,data,dev]"
python -m pytest -q                      # offline tests, about 5 minutes on a laptop CPU
python -m pytest -q -m network           # also downloads dev sets and real tokenizers
ruff check pocket_math tests kaggle

The CPU tests run complete jobs through the real runner with tiny random models: distillation to a final model, a crash in the middle of a round followed by a resume in a new session, and GRPO. They don't cover vLLM, fp16 on a T4 or two-GPU training. The smoke, opd-smoke and grpo-smoke jobs cover those on Kaggle.

Files

  • pocket_math/runner.py picks a job, holds its lease and runs it. context.py gives each job its config, repo writer, work directory and deadline.
  • pocket_math/hub.py holds the job repo store (Hugging Face or local), the lease and the heartbeat. queue.py reads queue.yaml and decides what can run.
  • pocket_math/rounds.py runs the training jobs (opd, grpo, sft, loopfix). phases.py turns each phase into one subprocess per GPU.
  • pocket_math/generate.py (vLLM or HF, with the Python-tool loop), score.py (teacher log-probs) and train.py (one LoRA step under torchrun) are those subprocesses.
  • pocket_math/losses.py and modeling.py hold the loss and the chunked log-probs.
  • pocket_math/evaluate.py runs eval jobs. system.py runs the voting system. smoke.py and bench.py run the Phase 0 checks.
  • pocket_math/grading.py, data.py, decontam.py, voting.py, loops.py, stats.py and sandbox.py are the leaf modules.
  • pocket_math/traces.py builds the SFT trace sets: it converts each source's traces to our response and tool-call format and filters them. sft_data.py turns those rows into token rows for training.
  • configs/models.yaml holds how to prompt and sample each model. configs/jobs/*.yaml holds one file per job type.
  • kaggle/bootstrap.py is the notebook's only code. session.py sets up a session. make_notebook.py writes runner.ipynb. launch_own.py starts sessions on your account.

When something goes wrong

  • A job shows failed. Read logs/last_error.txt in its repo, fix the cause, publish the code, then run ops reset <job>.
  • A job stays running but nobody runs it. The session died without releasing the lease. The lease expires 45 minutes after the last heartbeat, and then any session can take the job.
  • vLLM does not start on a T4. The generation phase tries four attention-backend settings in turn and records the one that works in the session log. If all four fail, set PM_VLLM_SPEC (for example vllm==0.11.0) in kaggle/session.py's environment and rerun the smoke job.
  • A session runs out of time. This is normal. The job pauses after its last finished phase, and the next session continues it.