fix: handle non-contiguous tensors in IPC weight refit#2418
Merged
terrykong merged 1 commit intoMay 28, 2026
Conversation
Contributor
|
/ok to test 8384171 |
youngeunkwon0405
approved these changes
May 8, 2026
youngeunkwon0405
left a comment
Contributor
There was a problem hiding this comment.
LGTM
@terrykong, I think I had a similar issue when I tested the qwen3.5 model, and the fix was pretty much similar to this one at that time.
pack_tensor in stream_weights_via_ipc_zmq_impl and packed_broadcast_producer called view(-1) on tensors yielded by megatron_bridge.export_hf_weights, which can be non-contiguous for the Qwen3ForCausalLM converter (the bridge's split_qkv_weights finishes with reshape(-1, hidden_size), and reshape may return a view with strides that are valid for reshape but not view). Use reshape(-1) in pack_tensor, and add .contiguous() before view(torch.uint8).view(-1) in packed_broadcast_producer. Both are no-ops in the contiguous case. Add a regression test exercising the IPC roundtrip with transposed, strided-slice, and permuted tensors. Fixes NVIDIA-NeMo#2417 Signed-off-by: Jose Luis <jlcanta02@gmail.com>
youngeunkwon0405
force-pushed
the
fix/pack-tensor-non-contiguous
branch
from
May 22, 2026 00:36
8384171 to
e1d8e95
Compare
Contributor
|
/okay to test e1d8e95 |
Contributor
|
@terrykong, I believe, to run QWEN3.5 we need this fix. It is almost one line change, could you please help reviewing? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do ?
Make
pack_tensorandpacked_broadcast_produceraccept non-contiguous tensors yielded by the params iterator, instead of crashing withRuntimeError: view size is not compatible with input tensor's size and stride.Issues
Fixes #2417
Usage
No user-facing API change. Internal helpers (
stream_weights_via_ipc_zmq_impl.pack_tensorandpacked_broadcast_producer) become robust to non-contiguous inputs:Before your PR is "Ready for review"
Pre checks:
tests/unit/models/policy/test_utils.py::TestStreamWeightsViaIPC::test_stream_weights_via_ipc_zmq_impl_non_contiguousruff checkandruff format --checkon the touched files; the unit test requires CUDA which I don't have locallyAdditional Information
Root cause.
stream_weights_via_ipc_zmq_implconsumes tensors frommegatron_bridge.export_hf_weights. For theQwen3ForCausalLMconverter,QKVMapping.megatron_to_hf → split_qkv_weightsfinishes withq.reshape(-1, hidden_size)(and same for k, v).Tensor.reshapeis allowed to return a view with strides that are compatible withreshapebut not withview. The next step in NeMo-RL wastensor.data.view(-1), which raises.Fix.
nemo_rl/models/policy/utils.py—pack_tensornow usesreshape(-1)instead ofview(-1).reshapereturns the same view in the contiguous case (zero copy) and falls back to a copy when not — and since the next operation is.copy_()into a buffer, the extra copy in the rare non-contiguous case has no extra cost.nemo_rl/utils/packed_tensor.py—packed_broadcast_produceradds.contiguous()before.view(torch.uint8).view(-1)(defense in depth — same root cause).tests/unit/models/policy/test_utils.py— refactored the existing IPC roundtrip test into a reusable helper_run_stream_weights_roundtrip, and addedtest_stream_weights_via_ipc_zmq_impl_non_contiguousthat exercises the helper with three kinds of non-contiguous inputs (transpose, strided slicing,permute). Without the fix, these new inputs trigger the originalRuntimeError.Tested manually.
converter_typeQwen/Qwen3-1.7BQwen2ForCausalLMQwen/Qwen3.5-2BQwen3ForCausalLMpack_tensorCompatibility. Strictly additive — when the input tensor is already contiguous (the common path) the behaviour is identical to before.