Skip to content

fix(data): handle NestedTensor values from TransferQueue - #286

Merged
NINGBENZHE merged 3 commits into
redai-studio:mainfrom
Echo-Nie:fix/transferqueue-nestedtensor-conversion
Aug 26, 2026
Merged

NINGBENZHE merged 3 commits into
redai-studio:mainfrom
Echo-Nie:fix/transferqueue-nestedtensor-conversion

Conversation

@Echo-Nie

Copy link
Copy Markdown
Member

thx for building and open-sourcing Relax — I've been using it to get the fully async training path running, and ran into this while looking into #283.

What

This PR fixes the TransferQueue consumer-side conversion when length/reward fields are restored as jagged NestedTensors.

It also adds regression coverage for:

  • dense tensor conversion;
  • jagged NestedTensor rows;
  • singleton nested rows that should become Python scalars;
  • mixed singleton / multi-element nested rows;
  • the actual get_data_from_transfer_queue() length/reward conversion path.

Why

Fixes #283.

get_data_from_transfer_queue() currently assumes length/reward-style fields are always regular dense tensors:

if "lengths" in k or "reward" in k:
    new_rollout_data[k] = v.tolist()

TransferQueue may restore sampled tensor fields as jagged NestedTensors. Calling .tolist() on the whole NestedTensor isn't supported by PyTorch, so the consumer fails while reconstructing the rollout batch.

Before the fix, the regression test reproduces the same failure reported in #283:

relax/utils/data/stream_dataloader.py:888: in get_data_from_transfer_queue
    new_rollout_data[k] = v.tolist()

RuntimeError: .tolist() is not supported for tensor subclasses, got NestedTensor

Simply doing unbind() + sample.tolist() isn't quite enough either, since scalar-per-sample fields such as response_lengths would become:

[[512], [135]]

instead of:

[512, 135]

which breaks the downstream length semantics.

How

I added a small conversion helper at the TransferQueue consumer boundary.

For a regular dense tensor, it keeps the existing .tolist() behavior.

For a nested tensor, it iterates over the rows with unbind():

  • singleton rows use .item() and become Python scalars;
  • multi-element rows use .tolist() and keep their row boundary.

So, for example:

[tensor([512]), tensor([135])]

is restored as:

[512, 135]

while a genuinely ragged value such as:

[tensor([1, 2]), tensor([3, 4, 5])]

stays:

[[1, 2], [3, 4, 5]]

I kept this PR intentionally small and limited to the existing lengths / reward conversion path. A field-schema/normalizer approach would probably be a nicer long-term hardening, but IMO that's better handled separately rather than mixing a broader refactor into this bug fix.

Testing

Before the fix

The consumer-boundary regression test fails with the original #283 error:

pytest -q \
  tests/utils/data/test_streaming_tq_iterator.py::test_get_data_from_transfer_queue_converts_nested_length_and_reward_fields

Result:

FAILED tests/utils/data/test_streaming_tq_iterator.py::test_get_data_from_transfer_queue_converts_nested_length_and_reward_fields

relax/utils/data/stream_dataloader.py:888: in get_data_from_transfer_queue
    new_rollout_data[k] = v.tolist()

RuntimeError: .tolist() is not supported for tensor subclasses, got NestedTensor

1 failed in 2.89s

After the fix

Same regression test:

pytest -q \
  tests/utils/data/test_streaming_tq_iterator.py::test_get_data_from_transfer_queue_converts_nested_length_and_reward_fields
1 passed in 2.83s

Focused regression coverage:

pytest -q \
  tests/utils/data/test_streaming_tq_iterator.py \
  -k "tensor_to_python_values or converts_nested_length_and_reward_fields"
4 passed, 6 deselected in 2.89s

Full StreamingTQIterator test file:

pytest -q tests/utils/data/test_streaming_tq_iterator.py
10 passed in 2.91s

All data utility tests:

pytest -q tests/utils/data/
81 passed, 4 skipped in 3.38s
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
1466 passed, 15 skipped, 74 warnings in 321.56s (0:05:21)
(RewardWorker pid=12193) 2026-08-20 11:26:30 | INFO | relax.utils.reload_utils:267 Reloaded Python module: tests.engine.rewards.custom_reward_fixtures [repeated 2x across cluster] (Ray deduplicates logs by default. Set RAY_DEDUP_LOGS=0 to disable log deduplication, or see https://docs.ray.io/en/master/ray-observability/user-guides/configure-logging.html#log-deduplication for more options.)
  • pre-commit run --all-files passes
  • Tests pass (pytest tests/)
  • New tests added (if applicable)
  • Documentation updated (if applicable)

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • CI/CD or build changes

Screenshots / Logs

The relevant before/after failure and test results are included above.

@Echo-Nie

Copy link
Copy Markdown
Member Author

@Yangruipis @NINGBENZHE The PR is ready for review, all CI checks have passed. thx!

@overloadedHenry

Copy link
Copy Markdown
Contributor

Thank you for your attention and contribution to this issue. Are you a beginner in Infra or have you been deeply researching it for some time? Perhaps we can have an opportunity to exchange ideas.

@Echo-Nie

Copy link
Copy Markdown
Member Author

Thank you for your attention and contribution to this issue. Are you a beginner in Infra or have you been deeply researching it for some time? Perhaps we can have an opportunity to exchange ideas.

Thx! I’m still new to infra, but I’ve been spending quite a bit of time recently on LLM systems and training/inference infra. Would be happy to exchange ideas sometime!

Comment thread relax/utils/data/stream_dataloader.py Resolved
@Echo-Nie
Echo-Nie requested a review from NINGBENZHE August 25, 2026 12:40

@NINGBENZHE NINGBENZHE left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@NINGBENZHE
NINGBENZHE merged commit b0f115e into redai-studio:main Aug 26, 2026
5 checks passed
@Echo-Nie
Echo-Nie deleted the fix/transferqueue-nestedtensor-conversion branch August 26, 2026 10:22
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.

[Bug] TransferQueue 恢复出的 NestedTensor 导致 fully async training 失败

3 participants