Repository navigation
feat: zero copy weight bridge - #60
Conversation
|
Excellent work! can you add related doc and explain this function and user guide. meanwile, cc @bitborne @EthanZero2Hero PTAL |
Signed-off-by: inaniloquentee <[email protected]>
Signed-off-by: inaniloquentee <[email protected]>
Signed-off-by: inaniloquentee <[email protected]>
Signed-off-by: inaniloquentee <[email protected]>
b63c005 to
ffa3da7
Compare
Signed-off-by: inaniloquentee <[email protected]>
Thanks for the feedback! Added related docs. |
| ) | ||
|
|
||
| def synchronize(self) -> None: | ||
| self._check(self.lib.cuCtxSynchronize(), "cuCtxSynchronize") |
There was a problem hiding this comment.
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())
| "SharedMemoryTensorBridge currently supports CPU tensors only. " | ||
| f"Tensor {name} is on {tensor.device}." | ||
| ) | ||
| snapshot = tensor.detach().clone(memory_format=torch.preserve_format) |
There was a problem hiding this comment.
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"]) |
There was a problem hiding this comment.
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.
Signed-off-by: inaniloquentee <[email protected]>
609c18b to
b66ffe8
Compare
…o-copy-weight-bridge Signed-off-by: inaniloquentee <[email protected]>
|
LGTM, Let's merge this PR first, if has issue in the furture, then focus handle it. |
…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]>
Summary
Validation
pre-commit run --all-filespy -3.13 -m mypy --ignore-missing-imports rl_engine/py -3.13 -m pytest rl_engine/tests/test_dispatch.py -vpy -3.13 -m pytest tests\test_op_accuracy.py tests\test_weight_sync_bridge.py -qpy -3.13 -m compileall rl_engine tests benchmarkspy -3.13 -m mkdocs build --strict -f mkdocs.yamlRuntime Smoke Results
torch.AcceleratorError: CUDA error: invalid resource handlefromtorch.storage._new_shared_cuda.IPCWeightTransferEngine.receive_weightsthroughLLM.update_weights(request), then blocked on the same PyTorch_new_shared_cudainvalid resource handle.mp.QueueCUDA tensor control experiment on the same WSL2 runtime failed at the same_new_shared_cudainvalid resource handle, so the remaining blocker is the current WSL2/driver/runtime legacy CUDA IPC path rather than the bridge/request implementation.