Repository navigation
Closed
Description
Summary
Add PyTorch reference operators that define the semantics expected from Kernel-Align RL kernels. These references should be slow but clear, deterministic, and reusable across kernel tests.
selected_logprobs_reference should be the first reference because issue #5 needs it, but the module should be organized as a generic RL kernel reference layer.
Why This Issue Exists
CUDA kernels need a precise semantic contract before they are optimized. In RL post-training, small numerical or masking errors can affect:
- policy ratio;
- KL penalty;
- clipping behavior;
- active-token normalization;
- sampling/training logprob consistency;
- reward or advantage weighting.
A generic reference operator layer lets kernel tests compare against one trusted source instead of duplicating ad hoc PyTorch snippets.
Scope
Create a reference module, for example:
rl_engine/testing/reference_ops.py, preferred for reuse by tests and benchmarks; ortests/reference/rl_ops.py, if it should stay test-only at first.
Initial reference operators:
- selected-token logprobs:
log_softmax(logits.float(), dim=-1).gather(...)- optional completion mask;
- optional temperature;
- explicit output dtype, with fp32 preferred for RL metrics.
- masked reductions:
- masked mean;
- masked sum;
- active-token count.
- GRPO/PPO-style metrics:
ratio = exp(current_logps - old_logps)- KL approximation against reference logps;
- ratio drift / KL drift helpers.
- optional sampling reference hooks:
- top-k/top-p semantics can be added later only when the trainer contract defines expected behavior.
Non-Goals
- Do not optimize CUDA code.
- Do not implement a full trainer.
- Do not add external RL framework dependencies.
- Do not define production top-k/top-p filtered logprob semantics until rollout/training contracts require it.
Suggested Implementation
Keep references explicit and readable:
def selected_logprobs_reference(
logits: torch.Tensor,
token_ids: torch.Tensor,
mask: torch.Tensor | None = None,
temperature: float = 1.0,
output_dtype: torch.dtype = torch.float32,
) -> torch.Tensor:
scaled_logits = logits.float() / temperature
log_probs = torch.log_softmax(scaled_logits, dim=-1)
selected = torch.gather(log_probs, dim=-1, index=token_ids.unsqueeze(-1)).squeeze(-1)
if mask is not None:
selected = selected.masked_fill(~mask, 0.0)
return selected.to(output_dtype)Add helpers such as:
def masked_mean(values, mask, eps=1e-8): ...
def compute_policy_ratio(current_logps, old_logps, mask=None): ...
def compute_reference_kl(current_logps, ref_logps, mask=None): ...
def summarize_kernel_drift(candidate, reference, mask=None): ...Acceptance Criteria
- Reference tests cover fp32, fp16, and bfloat16 logits where supported.
- Reference tests cover dense and masked completion tokens.
- Reference tests cover temperature values such as
1.0and0.7. - Helpers report logprob error, ratio drift, KL drift, and active-token count.
fused_logptests can import the selected-logprob reference.- The module is named and organized so future kernels can add reference functions without creating a logprob-only dependency.
Dependencies
- Generic RL kernel batch schema and fixtures.
Follow-Up Users
- RL-shaped kernel benchmark suite.
- Minimal RL loss-step kernel test.
- Native fused logp CUDA optimization candidates.
- Future sampling, attention, masked reduction, and loss kernel tests.
Metadata
Metadata
Assignees
Labels
No labels
Activity
inaniloquentee commentedon May 30, 2026
This is the PR: #35