Repository navigation
feat: add native fused logp RL kernel variants - #33
Conversation
|
Thank you for your contribution this massive and import PR, I will make some review, Thanks. |
Flink-ddd
left a comment
There was a problem hiding this comment.
Amazing work on expanding the kernel variants! The progression from Dense to Online-Indexed is exactly what we need to minimize VRAM overhead for RL workloads like GRPO.
However, since this PR didn't modify our dispatcher, these new kernels are currently disconnected from our infra. To merge this smoothly, we need to wire these variants into our new dynamic dispatch system (KernelRegistry).
Action Required: Register Variants in rl_engine/kernels/registry.py
Since registry.py wasn't modified in this PR, please pull the latest main and edit it. We need to register your new workloads (e.g., "logp_indexed", "logp_online") so the RL rollout engine can fetch them dynamically based on mask density, rather than hardcoding static methods.
Please add your new variants to the _priority_map under the CUDA backend (sharing the generic fallback for now, as I will handle the broader Hardware Abstraction Layer folder restructuring later).
Example of what to add:
# rl_engine/kernels/registry.py
class KernelRegistry:
def __init__(self):
# ...
self._priority_map = {
"cuda": {
"logp": [OpBackend.CUDA_FUSED_LOGP_GENERIC, OpBackend.PYTORCH_NATIVE],
# Please register the new RL workload variants here:
"logp_indexed": [OpBackend.CUDA_FUSED_LOGP_GENERIC, OpBackend.PYTORCH_NATIVE],
"logp_online": [OpBackend.CUDA_FUSED_LOGP_GENERIC, OpBackend.PYTORCH_NATIVE],
"logp_online_indexed": [OpBackend.CUDA_FUSED_LOGP_GENERIC, OpBackend.PYTORCH_NATIVE],
"attn": [OpBackend.FLASH_ATTN, OpBackend.TRITON_GENERIC, OpBackend.PYTORCH_NATIVE],
},
# ...
}
Once you integrate these into the registry and resolve the inline comments on setup.py, ops.cpp, and the Python wrappers, I will gladly approve this. Incredible optimization!
Fixes #5 Signed-off-by: inaniloquentee <[email protected]>
Signed-off-by: inaniloquentee <[email protected]>
|
LGTM now. Thank you for update code, but please resolve these conflicts before merge |
f705111 to
409a32e
Compare
Thanks for the review and LGTM! I have rebased this PR onto the latest I also re-ran the checks after resolving the conflicts:
The PR branch has been force-updated with the resolved rebase. |
Summary
This PR adds native fused-logp variants for RL selected-logprob workloads.
The main RL-facing optimization is valid-row indexed output. It lets RL infra compute selected logprobs only for valid completion rows instead of every padded/inactive row.
The later iterations then improve the per-row vocab scan on top of that indexed path.
Fixes #5
Iteration Progression
Iteration 1: Baseline Dense Two-Pass
The starting point is the original dense fused-logp path.
It computes selected-token log probability for every row with a two-pass log-sum-exp kernel:
Role: baseline correctness and performance reference.
Decision: keep as the default/simple dense path.
Iteration 2: Preallocated And FP32 Output
This iteration adds RL infra-friendly output APIs.
Added:
Why:
Decision: promoted.
Iteration 3: Valid-Row Indexed Output
This is the main RL workload-shape optimization.
It adds indexed-row variants so the kernel can compute only valid completion rows, using row indices derived from the RL completion mask.
Why:
Decision: conditionally promoted.
Use indexed paths when valid-token density is meaningfully below full density. Keep dense paths for full-density batches.
Iteration 4: Online Log-Sum-Exp
This iteration optimizes the per-row vocab scan.
Instead of two streaming passes over vocab, it computes log-sum-exp online in one pass.
Why:
Initial online log-sum-exp was not always faster, so it was re-evaluated with block-size-controlled benchmarks and NCU. The final policy keeps:
Decision: promoted conditionally with a small launch policy.
Iteration 5: Static Online Launch Variants
This iteration keeps the launch policy from Iteration 4 but improves code generation.
Instead of one online kernel using runtime
blockDim.x, it compiles static online variants byBlockSize.Why:
Decision: promoted.
Performance Summary
The main RL-facing gain is from Iteration 3: exposing valid-row indexing so RL infra can skip inactive completion tokens.
Iterations 4 and 5 then improve the per-valid-row vocab scan.
Small-Vocab RL Shape
vocab=4096,num_prompts=8,samples_per_prompt=8,completion_len=128Speedup vs dense two-pass:
Interpretation:
Large-Vocab RL Shape
num_prompts=4,samples_per_prompt=4,completion_len=64Interpretation:
NCU Evidence For Iteration 5
Profiled shape:
vocab=32768,mask_density=0.5, indexed path.Iteration 5 improves codegen after Iteration 4 chooses the right online launch policy. It should be viewed as the final per-row implementation improvement, while Iteration 3 is the main RL workload-shape improvement.
Final Implementation
This PR adds:
fused_logp_outfused_logp_fp32fused_logp_indexed_outfused_logp_indexed_fp32fused_logp_online_outfused_logp_online_fp32fused_logp_online_indexed_outfused_logp_online_indexed_fp32Python wrapper methods:
FusedLogp.outFusedLogp.apply_fp32FusedLogp.indexed_outFusedLogp.indexed_fp32FusedLogp.online_outFusedLogp.online_fp32FusedLogp.online_indexed_outFusedLogp.online_indexed_fp32Build-time CUDA knobs:
FUSED_LOGP_TWOPASS_BLOCK_SIZEFUSED_LOGP_ONLINE_BLOCK_SIZEFUSED_LOGP_ONLINE_SPARSE_LARGE_VOCAB_BLOCK_SIZEFUSED_LOGP_ONLINE_LARGE_ROW_BYTES_THRESHOLDFUSED_LOGP_ONLINE_SPARSE_DENSITY_NUMERATORFUSED_LOGP_ONLINE_SPARSE_DENSITY_DENOMINATORFUSED_LOGP_ONLINE_MIN_BLOCKS_PER_SMKERNEL_ALIGN_NCU_LINEINFO=1Validation
Ran: