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

[Refactoring] Import at root to enable vmap monkey-patching #500

Merged
merged 8 commits into from
Sep 30, 2022

Conversation

vmoens
Copy link
Contributor

@vmoens vmoens commented Sep 30, 2022

Description

  • monkey-patches a few more functorch functions
  • implements tests for nn.Module vmap with tensordicts

Following suggestions from @zou3519 we remove and add a batch dimension when vmapping over a tensordict.
Some commended code shows attempts to register TensorDIct in pytree but that failed + it requires to decompose and recompose the tensordict multiple times, while doing complex operations to keep track of the modified batch size. Besides the fact that it did not work, it also seemed suboptimal from a compute time perspective.

@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 Sep 30, 2022
@vmoens
Copy link
Contributor Author

vmoens commented Sep 30, 2022

cc @nicolas-dufour
allows for this kind of behaviour

        class MyModule(torch.nn.Module):
            def forward(self, tensordict, tensor):
                a = tensordict["a"]
                return (
                    TensorDict(
                        {"a": a}, tensordict.batch_size, device=tensordict.device
                    ),
                    tensor,
                )

        tensor = torch.randn(3).expand(4, 3)
        tensordict = TensorDict({"a": torch.randn(3, 1)}, [3])
        out = vmap(MyModule(), (None, 0))(tensordict, tensor)

        assert out[0].shape == torch.Size([4, 3])
        assert out[1].shape == torch.Size([4, 3])
        assert out[0]["a"].shape == torch.Size([4, 3, 1])

allowing for nn.Module with arbitrary TensorDict inputs

@vmoens vmoens merged commit c644a34 into main Sep 30, 2022
@vmoens vmoens deleted the change_imports branch December 14, 2022 15:07
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.

2 participants