Skip to content

feat: grpo single gpu example - #80

Merged
Flink-ddd merged 2 commits into
mainfrom
feat/grpo-single-gpu-example
Jun 4, 2026
Merged

Flink-ddd merged 2 commits into
mainfrom
feat/grpo-single-gpu-example

Conversation

@inaniloquentee

Copy link
Copy Markdown
Collaborator

Summary

Closes #76.

This PR adds a minimal, reproducible single-GPU GRPO training example under examples/. The repository previously had no runnable examples in that directory; this change introduces a self-contained script that demonstrates the RL-Kernel GRPO training path on one device without multi-GPU infrastructure.

What users get

  • examples/grpo_single_gpu.py — end-to-end toy GRPO loop on a single GPU (or CPU for smoke tests)
  • docs/usage/grpo-single-gpu-example.md — run instructions, validation notes, and test commands
  • tests/test_grpo_single_gpu_example.py — subprocess smoke tests suitable for CI

What this PR does not include

  • No external dataset downloads
  • No Hugging Face / vLLM / Ray / DeepSpeed dependencies
  • No distributed launcher or multi-process training
  • No changes to core kernel implementations

Motivation

Issue #76 requests expanding examples/ with a script that:

  1. Runs end-to-end on a single GPU (e.g. one A100)
  2. Uses a toy dataset (synthetic, reproducible)
  3. Demonstrates the full RL-Kernel pipeline in a simplified single-node environment

This example is intentionally small so new users and CI can validate the workflow without standing up rollout services or large models.


Implementation overview

End-to-end pipeline

make_synthetic_rl_kernel_batch()
        │
        ▼
group-normalized rewards + GRPO advantages
        │
        ▼
TinyPolicy (embedding + linear head) ──► logits
        │
        ├── kernel_registry.get_op("logp")  [CUDA]
        │   or NativeLogpOp               [CPU]
        │
        ├── reference selected logprobs (validation / autograd)
        │
        ▼
GRPO clipped policy loss + reference KL
        │
        ▼
AdamW optimizer step (finite-loss guard)

Key design choices

Topic Choice Rationale
Data make_synthetic_rl_kernel_batch() Deterministic, no downloads, matches existing test fixtures
Policy Tiny embedding + linear projection Minimal trainable model; keeps VRAM and runtime low
Advantages Per-prompt group normalization over samples_per_prompt Aligns with GRPO group-relative reward idea
Logp backend kernel_registry on CUDA, NativeLogpOp on CPU Exercises RL-Kernel dispatch without extra services
Strict mode --require-fused-logp Fails fast if CUDA falls back to non-fused backend
Autograd Reference logprobs when fused op is forward-only Allows real loss.backward() while still validating fused output drift

CLI highlights

python examples/grpo_single_gpu.py \
  --device {auto,cuda,cpu} \
  --steps 4 \
  --num-prompts 2 \
  --samples-per-prompt 4 \
  --require-fused-logp   # optional: enforce fused CUDA backend

Per-step logs include: loss, policy_loss, kl, train_logp_source, kernel_max_abs_error, and active token count.

Issue alignment checklist

Issue requirement How this PR satisfies it
New file at examples/grpo_single_gpu.py Added
Minimal reproducible GRPO training Synthetic batch + clipped loss + optimizer steps
Single GPU (e.g. A100) --device auto / --device cuda; validated on A100
Toy dataset make_synthetic_rl_kernel_batch, no external data
Full pipeline demonstration Batch → advantages → logp dispatch → GRPO loss → backward/step
No multi-GPU setup Single device only; no launchers

How to run

1. CPU smoke test (CI-friendly)

python examples/grpo_single_gpu.py \
  --device cpu \
  --steps 2 \
  --num-prompts 1 \
  --samples-per-prompt 2 \
  --prompt-len 2 \
  --completion-len 3 \
  --vocab-size 16 \
  --hidden-dim 8

Expected: prints completed grpo_single_gpu and device=cpu.

2. Single-GPU strict fused path (A100 / CUDA)

MAX_JOBS=2 python setup.py build_ext --inplace

python examples/grpo_single_gpu.py \
  --device cuda \
  --require-fused-logp \
  --steps 2 \
  --num-prompts 1 \
  --samples-per-prompt 2 \
  --prompt-len 2 \
  --completion-len 3 \
  --vocab-size 16 \
  --hidden-dim 8

Expected (with built _C extension):

backend=FusedLogpGenericOp
completed grpo_single_gpu steps=2 device=cuda backend=FusedLogpGenericOp

3. Example pytest

python -m pytest tests/test_grpo_single_gpu_example.py -v

Test plan

  • pre-commit run --all-files
  • python -m mypy --ignore-missing-imports rl_engine/
  • python -m pytest rl_engine/tests/test_dispatch.py -v
  • python -m pytest tests/test_grpo_single_gpu_example.py -v
  • mkdocs build --strict -f mkdocs.yaml
  • MAX_JOBS=2 python setup.py build_ext --inplace
  • python examples/grpo_single_gpu.py --device cuda --require-fused-logp ... on A100

Validation results (local)

Check Result
Pre-commit (trailing-whitespace, EOF, yaml, black, isort, flake8) Passed
MyPy (rl_engine/) Passed
Dispatch tests (rl_engine/tests/test_dispatch.py) 3 passed
New example tests 2 passed
Docs strict build Passed
A100 strict fused run FusedLogpGenericOp, 2 steps completed
Kernel drift vs reference kernel_max_abs_error ≈ 2.38e-07

Known limitations / reviewer notes

  1. Fused logp is forward-validated; autograd uses reference logprobs when the selected backend does not expose a differentiable path. This is documented in docs/usage/grpo-single-gpu-example.md and logged as train_logp_source=autograd_reference.
  2. --require-fused-logp requires a built CUDA extension (pip install -e . or python setup.py build_ext --inplace). Without _C, CUDA may fall back to NativeLogpOp and strict mode will fail by design.
  3. CI unit-tests job runs CPU-only PyTorch and mocked dispatch tests; the new subprocess tests align with that environment. Full fused CUDA validation is documented for manual / GPU runners.
  4. Script inserts repo root into sys.path so python examples/grpo_single_gpu.py works without prior pip install -e . (with # noqa: E402 on the deferred import).

inaniloquentee and others added 2 commits June 4, 2026 20:04
Provide a self-contained GRPO training script and validation docs so users can reproduce the RL-Kernel logp path on one GPU without distributed infrastructure.

Signed-off-by: inaniloquentee <[email protected]>
Co-authored-by: Cursor <[email protected]>
Drop plan.md because the user-facing documentation already lives in docs/usage/grpo-single-gpu-example.md.

Signed-off-by: inaniloquentee <[email protected]>
Co-authored-by: Cursor <[email protected]>
@Flink-ddd

Copy link
Copy Markdown
Collaborator

LGTM, Thank you for your contribution.

@Flink-ddd
Flink-ddd merged commit d0c8012 into main Jun 4, 2026
5 checks passed
@inaniloquentee
inaniloquentee deleted the feat/grpo-single-gpu-example branch September 20, 2026 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT][examples]: add minimal reproducible GRPO training script (single GPU)

2 participants