Skip to content

[Feature] add key-level skip_existing - #1771

Merged
vmoens merged 1 commit into
pytorch:mainfrom
gtnv:fix-352-skip-existing-keys
Aug 25, 2026
Merged

[Feature] add key-level skip_existing#1771
vmoens merged 1 commit into
pytorch:mainfrom
gtnv:fix-352-skip-existing-keys

Conversation

@gtnv

@gtnv gtnv commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

TLDR

set_skip_existing(["value"]) used list truthiness, so any non-empty key list behaved like True and could skip unrelated recurrent-state updates.

This normalizes the selected keys and skips a module only when all of its outputs are selected and already present. Cached outputs stay untouched, while modules with unselected outputs still run.

Closes #352

Testing

Reproduction

import torch
from tensordict import TensorDict
from tensordict.nn import TensorDictModule, TensorDictSequential, set_skip_existing

model = TensorDictSequential(
    TensorDictModule(
        lambda value: value + 1,
        in_keys=["input"],
        out_keys=["value"],
    ),
    TensorDictModule(
        lambda memory: memory + 1,
        in_keys=["memory"],
        out_keys=[("next", "memory")],
    ),
)
data = TensorDict(
    {
        "input": torch.tensor(0),
        "value": torch.tensor(10),
        "memory": torch.tensor(0),
        "next": {"memory": torch.tensor(10)},
    },
    [],
)

with set_skip_existing(["value"]):
    model(data)

assert data["value"].item() == 10
assert data["next", "memory"].item() == 1

Before this change, the final assertion fails because ("next", "memory") remains 10.

python -m pytest test/nn/test_nn.py::TestSkipExisting -q
python -m pytest test/nn -q
pre-commit run --files tensordict/nn/utils.py test/nn/test_nn.py

Results:

  • 8 passed in the focused suite.
  • 611 passed, 1 skipped in test/nn; ONNX was unavailable.
  • All configured formatting and lint hooks pass.

@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 Aug 25, 2026

@vmoens vmoens 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.

LGTM thanks!

@vmoens
vmoens merged commit 9a3e006 into pytorch:main Aug 25, 2026
70 of 71 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Feature New feature nn Test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] key-level granularity in skip_existing

2 participants