Skip to content

feat: zero copy weight bridge - #60

Merged
Flink-ddd merged 7 commits into
mainfrom
issue-13-zero-copy-weight-bridge-rebased
Jun 3, 2026
Merged

Flink-ddd merged 7 commits into
mainfrom
issue-13-zero-copy-weight-bridge-rebased

Conversation

@inaniloquentee

@inaniloquentee inaniloquentee commented Jun 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Rebase issue [FEAT][executors]: design zero-copy weight synchronization bridge #13 on top of the baseline that includes issue 55 native logp API parity.
  • Replace the guarded legacy CUDA IPC placeholder with a real PyTorch CUDA IPC manifest bridge, lifecycle tracking, handle reuse, and validation.
  • Add production-aligned smoke coverage for legacy CUDA IPC and vLLM IPC hot-weight update through vLLM's public IPC update path.

Validation

  • pre-commit run --all-files
  • py -3.13 -m mypy --ignore-missing-imports rl_engine/
  • py -3.13 -m pytest rl_engine/tests/test_dispatch.py -v
  • py -3.13 -m pytest tests\test_op_accuracy.py tests\test_weight_sync_bridge.py -q
  • py -3.13 -m compileall rl_engine tests benchmarks
  • py -3.13 -m mkdocs build --strict -f mkdocs.yaml

Runtime Smoke Results

  • WSL2 legacy CUDA IPC smoke reached PyTorch CUDA IPC handle reconstruction in a spawned child process, then blocked with torch.AcceleratorError: CUDA error: invalid resource handle from torch.storage._new_shared_cuda.
  • WSL2 vLLM IPC hot-weight update reached vLLM 0.18.1 IPCWeightTransferEngine.receive_weights through LLM.update_weights(request), then blocked on the same PyTorch _new_shared_cuda invalid resource handle.
  • A PyTorch-native mp.Queue CUDA tensor control experiment on the same WSL2 runtime failed at the same _new_shared_cuda invalid resource handle, so the remaining blocker is the current WSL2/driver/runtime legacy CUDA IPC path rather than the bridge/request implementation.

@inaniloquentee inaniloquentee changed the title Issue 13 zero copy weight bridge feat: zero copy weight bridge Jun 2, 2026
@Flink-ddd

Copy link
Copy Markdown
Collaborator

Excellent work! can you add related doc and explain this function and user guide. meanwile, cc @bitborne @EthanZero2Hero PTAL

@inaniloquentee
inaniloquentee force-pushed the issue-13-zero-copy-weight-bridge-rebased branch from b63c005 to ffa3da7 Compare June 2, 2026 12:44
@inaniloquentee

Copy link
Copy Markdown
Collaborator Author

Excellent work! can you add related doc and explain this function and user guide. meanwile, cc @bitborne @EthanZero2Hero PTAL

Thanks for the feedback! Added related docs.

Comment thread rl_engine/executors/bridge.py Outdated
)

def synchronize(self) -> None:
self._check(self.lib.cuCtxSynchronize(), "cuCtxSynchronize")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace the blocking cuCtxSynchronize() with fine-grained stream synchronization using torch.cuda.Event(interprocess=True) to prevent stalling the entire GPU context and introducing severe pipeline bubbles.

Example Implementation:

# Publisher side Training
sync_event = torch.cuda.Event(interprocess=True)
sync_event.record(torch.cuda.current_stream())
ipc_event_handle = sync_event.ipc_handle()

# (Include `ipc_event_handle` in the manifest's metadata)

# Consumer side Inference
remote_event = torch.cuda.Event.from_ipc_handle(
    device=self.device_index, 
    handle=manifest.metadata["ipc_event_handle"]
)
remote_event.wait(torch.cuda.current_stream())

Comment thread rl_engine/executors/bridge.py Outdated
"SharedMemoryTensorBridge currently supports CPU tensors only. "
f"Tensor {name} is on {tensor.device}."
)
snapshot = tensor.detach().clone(memory_format=torch.preserve_format)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using .clone() here doubles the VRAM usage during the weight publishing phase. For a 70B-class model, this means an instant ~140GB spike, which will easily cause an OOM and defeats the core purpose of a "Zero-Copy" bridge.

To maintain true Zero-Copy semantics, we should strictly rely on tensor.detach().contiguous() and use the IPC Event synchronization (discussed above) to ensure that the read/write lifecycles are safe, completely avoiding the memory penalty of deep copying.

Suggested fix:

# Remove the expensive .clone()
snapshot = tensor.detach().contiguous()

for name, info in ipc_handles.items():
cuda_float_tensor = cast(Any, torch.cuda).FloatTensor
storage = cuda_float_tensor._new_shared_cuda(info["handle"])
tensor = cuda_float_tensor(storage).view(info["shape"])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a guaranteed NameError in IPCWeightBridge.import_model_weights.

The variable remote_weights is being assigned to here, but it is never initialized in this scope. Since you are collecting tensors into the tensors dictionary below, this line should be completely removed to prevent a runtime crash.

@inaniloquentee
inaniloquentee force-pushed the issue-13-zero-copy-weight-bridge-rebased branch from 609c18b to b66ffe8 Compare June 3, 2026 13:12
@Flink-ddd

Copy link
Copy Markdown
Collaborator

LGTM, Let's merge this PR first, if has issue in the furture, then focus handle it.

@Flink-ddd
Flink-ddd merged commit ef780e7 into main Jun 3, 2026
4 checks passed
ryankert01 added a commit to ryankert01/RL-Kernel that referenced this pull request Sep 18, 2026
…5 oracle

P5-1 (RL-Align#60): MXFP8 activation quantization (OCP E4M3 elements, block-32 E8M0
shared scales) plus the straight-through backward, in both CUDA and Triton.

Both backends reproduce rl_engine.moe.mx_format.mx_quantize byte for byte, so
they keep the oracle numeric profile (oracle-fp32-serial-v1) instead of
registering a relaxed one:

  the block amax is an integer max over |x| bit patterns, floor(log2) is read
  off the FP32 exponent field after the FLT_MIN clamp (exact integer
  equivalent of the oracle's frexp path), the scale is assembled from its bit
  pattern so code 0 lands on the subnormal 2**-127, and the division is an
  explicit non-.ftz div.rn.f32 / tl.fdiv(ieee_rounding=True). Nothing on the
  subnormal-sensitive path can be flushed by --use_fast_math; a fast-math build
  is a regression test. E4M3 encoding is clamp-then-RNE (start-kit decision
  D1), which is what cvt.rn.satfinite.e4m3 does in hardware.

Determinism: the amax reduction is row-local over a fixed 32-element window and
every element is then transformed independently, so the bytes do not depend on
grid shape, block size, or how many rows are in flight.

Non-finite input is fail-closed in both backends and in the providers (RL-Align#60 s4);
the STE backward accepts any floating dtype (RL-Align#60 s2).

Providers live in rl_engine/moe/backends/mxfp8_act_quant.py, following the
backends package P5-5 introduced. scripts/check_p5.py --device cuda passes
byte-equal on every boundary for both {Cuda,Triton}MXFP8ActQuantProvider,
with act_quant1.*/act_quant2.* hashed and act_quant_bwd noted as "ste".

Two outside MX implementations are byte-identical to these kernels and are
asserted as tests when installed: torchao's to_mx (FLOOR) and the Triton repo's
triton_kernels.downcast_to_mxfp (ROUND_DOWN), the quantizer vLLM runs for its
MXFP4 path. The benchmark measures against both, against vLLM's production
per_token_group_quant_fp8 CUDA kernel (a different format: group 128, FP32
scale), and against a plain fp8 cast as the memory-bandwidth lower bound. On
H100 the kernels reach 2.99 TB/s at 16384x7168, at that bound.

The CUDA wrapper prefers the compiled rl_engine._C symbols and otherwise
JIT-builds the single .cu file, so the tests and the benchmark run in a source
tree without a full extension build (RL_KERNEL_P5_DISABLE_JIT=1 to require AOT).

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Kv8HMu6Y1ZY5FKbuiFTrW3
Signed-off-by: Ryan Huang <[email protected]>
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.

2 participants