Add python nested_tensor and as_nested_tensor constructors in torch.nested (#85593)

Remove `torch.nested_tensor` which has erroneous behavior wrt gradients (could be either leaf or not leaf). Introduce `torch.nested.nested_tensor` and `torch.nested.as_nested_tensor` in the vein of `torch.tensor` and `torch.as_tensor`. Done in nested `__init__.py` for now but can move to pybind in future (when we want to load from numpy/nested lists ).

Discussed offline with @cpuhrsch and pybind constructor (https://github.com/pytorch/pytorch/pull/85536) was more gnarly than expected, so we can move to that when we do need loading from numpy etc.

Differential Revision: [D39806622](https://our.internmc.facebook.com/intern/diff/D39806622)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/85593
Approved by: https://github.com/drisspg, https://github.com/cpuhrsch
This commit is contained in:
Mikayla Gawarecki
2022-09-28 17:38:29 +00:00
committed by PyTorch MergeBot
parent a876432aea
commit afaee00fec
17 changed files with 381 additions and 225 deletions

View File

@ -10,6 +10,6 @@
TEST(NestedTest, Nested) {
auto a = torch::randn({2, 3});
auto b = torch::randn({4, 5});
auto nt = torch::nested_tensor({a, b});
auto nt = torch::nested::nested_tensor({a, b});
torch::nested::to_padded_tensor(nt, 0);
}