Skip to content

[BugFix] Keep AsyncBatchedCollector's env_index a tensor with tensordict 0.17 - #4637

Merged
theap06 merged 1 commit into
pytorch:mainfrom
peterdsharpe:async-env-index-tensor
Nov 7, 2026
Merged

theap06 merged 1 commit into
pytorch:mainfrom
peterdsharpe:async-env-index-tensor

Conversation

@peterdsharpe

@peterdsharpe peterdsharpe commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Problem

In AsyncBatchedCollector's thread worker (_env_loop), pool.async_step_and_maybe_reset_recv returns a TensorDict where "env_index" is a NonTensorData(env_index) marker set by the pool, and the worker overwrites it with a Python int:

cur_td.set(_ENV_IDX_KEY, env_id)

In tensordict 0.15-0.16, writing a Python int over an existing NonTensorData entry converts it to a 0-D tensor and emits a FutureWarning. Starting in tensordict 0.17, set preserves the existing entry kind and keeps "env_index" as NonTensorData, which breaks downstream consumers that expect an integer tensor (such as ReplayBufferEnsemble routing and .unique()).

Fix

Write an explicit torch.long tensor matching cur_td.batch_size and cur_td.device, the same way the chunked process-worker path in _process_env_loop already does:

cur_td.set(
    _ENV_IDX_KEY,
    torch.full(cur_td.batch_size, env_id, dtype=torch.long, device=cur_td.device),
)

For an unbatched env slot this produces the same 0-D int64 tensor as today without triggering the deprecation warning.

Tests

Added TestAsyncBatchedCollector::test_thread_worker_env_index_is_long_tensor in test/test_inference_server.py, which runs a threaded AsyncBatchedCollector with the FutureWarning turned into an error and checks that "env_index" is a torch.long tensor.

Related: #4551, #4576, and #4638.

@pytorch-bot

pytorch-bot Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/4637

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure

As of commit 0ca7025 with merge base 6479319 (image):

NEW FAILURE - The following job has failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 28, 2026

@bsprenger bsprenger left a comment

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.

Looks great, thanks for fixing, and sorry for the delay in reviewing! Can you rebase + rerun CI before we merge?

…ict 0.17

Thread workers wrote an int over the NonTensorData env_index that the
AsyncEnvPool sets on each result. tensordict casts that int to a tensor
today with a FutureWarning, and keeps it non-tensor from 0.17. Store a
long tensor of the slot's batch size, as the chunked process workers do.
@peterdsharpe
peterdsharpe force-pushed the async-env-index-tensor branch from 39144dd to 0ca7025 Compare November 7, 2026 03:31
@theap06
theap06 merged commit 6b42e06 into pytorch:main Nov 7, 2026
122 of 123 checks passed
@peterdsharpe
peterdsharpe deleted the async-env-index-tensor branch November 7, 2026 11:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BugFix CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Collectors Integrations/torch_geometric Integrations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants