Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Contiguous stacking of matching specs #960

Merged
merged 5 commits into from
Mar 13, 2023

Conversation

tcbegley
Copy link
Contributor

@tcbegley tcbegley commented Mar 9, 2023

Description

This PR changes the behaviour of torch.stack when the arguments are TensorSpec or CompositeSpec variants. It checks for equality among the specs being stacked, and if they all match, it actually performs the stack (rather than returning a lazy stacked spec).

To facilitate this, I have added squeeze and unsqueeze methods to all specs. Stacking is done by computing the new shape and then calling something like spec.clone().unsqueeze(stack_dim).expand(new_shape). I added squeeze since you can't have unsqueeze without squeeze 😄

Some of the tests probably need rethinking. I've made small changes to get them to pass, but there is new behaviour here which ideally would be tested for explicitly.

@facebook-github-bot facebook-github-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 Mar 9, 2023
test/test_specs.py Show resolved Hide resolved
test/test_specs.py Show resolved Hide resolved
test/test_specs.py Show resolved Hide resolved
Copy link
Contributor

@vmoens vmoens left a comment

Choose a reason for hiding this comment

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

LGTM
We can merge this as is and add the missing tests later!

test/test_specs.py Show resolved Hide resolved
torchrl/data/tensor_specs.py Show resolved Hide resolved
@vmoens
Copy link
Contributor

vmoens commented Mar 9, 2023

We have some failing tests due to the device changes

@vmoens vmoens merged commit c1acefd into pytorch:stack_specs Mar 13, 2023
@tcbegley tcbegley deleted the stack_specs branch March 13, 2023 16:12
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants