Skip to content

Commit e217b30

Browse files
mikaylagawareckipytorchmergebot
authored andcommitted
Add torch.nested namespace (pytorch#84102)
First step towards pytorch#83775 - only `to_padded_tensor` is moved to the nested namespace for now - following the schema used for `special`, `fft`, `linalg` and other namespaces, nested functions are registered in native_functions.yaml as `nested_{function_name}` and are bound to the desired Python name in `torch/nested/__init__.py`, and the desired C++ name in `torch/csrc/api/include/torch/nested.h`. ~~**Question**: should we keep the documentation for `Tensor.to_padded_tensor` or can this deleted since it is shared by `torch.nested.to_padded_tensor`?~~ [generated nested docs](https://docs-preview.pytorch.org/84102/nested.html?highlight=nested#module-torch.nested) Differential Revision: [D39361148](https://our.internmc.facebook.com/intern/diff/D39361148) Pull Request resolved: pytorch#84102 Approved by: https://github.com/drisspg
1 parent 9c78f59 commit e217b30

30 files changed

+309
-130
lines changed

aten/src/ATen/native/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,8 @@ the torch._C._nn (marked with `python_module: nn`),
628628
torch._C._fft (marked with `python_module: fft`),
629629
torch._C._linalg (marked with `python_module: linalg`) objects,
630630
torch._C._sparse (marked with `python_module: sparse`) objects,
631-
or torch._C._special (marked with `python_module: special`) objects.
631+
torch._C._special (marked with `python_module: special`) objects,
632+
or torch._C._nested (marked with `python_module: nested`) objects.
632633
633634
### Undefined tensor conventions
634635

aten/src/ATen/native/native_functions.yaml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12558,6 +12558,21 @@
1255812558
- func: linalg_multi_dot.out(Tensor[] tensors, *, Tensor(a!) out) -> Tensor(a!)
1255912559
python_module: linalg
1256012560

12561+
## Functions related to the `torch.nested` namespace
12562+
# Note [nested namespace binding]
12563+
# Functions in the nested python module should have their names start with
12564+
# "nested_" underscore and be bound to the desired Python name in
12565+
# torch/nested/__init__.py, and the desired C++ name in torch/csrc/api/include/torch/nested.h.
12566+
# The "nested_" names should be hidden from the user and not documented.
12567+
12568+
- func: nested_to_padded_tensor(Tensor self, float padding, int[]? output_size=None) -> Tensor
12569+
python_module: nested
12570+
variants: function
12571+
dispatch:
12572+
NestedTensorCPU: NestedTensor_to_padded_tensor_generic
12573+
NestedTensorCUDA: NestedTensor_to_padded_tensor_cuda
12574+
autogen: nested_to_padded_tensor.out
12575+
1256112576
## Functions that are only for testing
1256212577
# It is undocumented and should not be used outside of tests.
1256312578
- func: _test_serialization_subcmul(Tensor self, Tensor other, Scalar alpha=1) -> Tensor
@@ -13068,13 +13083,6 @@
1306813083
dispatch:
1306913084
CompositeExplicitAutograd: alias_copy_out
1307013085

13071-
- func: to_padded_tensor(Tensor self, float padding, int[]? output_size=None) -> Tensor
13072-
variants: method
13073-
dispatch:
13074-
NestedTensorCPU: NestedTensor_to_padded_tensor_generic
13075-
NestedTensorCUDA: NestedTensor_to_padded_tensor_cuda
13076-
autogen: to_padded_tensor.out
13077-
1307813086
- func: _nested_tensor_softmax_with_shape(Tensor self, Tensor query) -> Tensor
1307913087
dispatch:
1308013088
NestedTensorCPU: NestedTensor_softmax_dropout

aten/src/ATen/native/transformers/attention.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ std::tuple<Tensor, Tensor, Tensor> transform_bias_rescale_qkv_cpu(
262262
const Tensor& qkv_bias,
263263
const int64_t num_head) {
264264
auto qkv_ = qkv.is_nested()
265-
? c10::MaybeOwned<Tensor>::owned(qkv.to_padded_tensor(0))
265+
? c10::MaybeOwned<Tensor>::owned(nested_to_padded_tensor(qkv, 0))
266266
: c10::MaybeOwned<Tensor>::borrowed(qkv);
267267
auto B = qkv_->size(0);
268268
auto T = qkv_->size(1);

build.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ _GENERATED_AUTOGRAD_PYTHON_CPP = [
257257
"torch/csrc/autograd/generated/python_functions_3.cpp",
258258
"torch/csrc/autograd/generated/python_functions_4.cpp",
259259
"torch/csrc/autograd/generated/python_nn_functions.cpp",
260+
"torch/csrc/autograd/generated/python_nested_functions.cpp",
260261
"torch/csrc/autograd/generated/python_fft_functions.cpp",
261262
"torch/csrc/autograd/generated/python_linalg_functions.cpp",
262263
"torch/csrc/autograd/generated/python_return_types.cpp",

build_variables.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,7 @@ def glob_libtorch_python_sources(gencode_pattern = ":generate-code[{}]"):
986986
"torch/csrc/autograd/generated/python_functions_2.cpp",
987987
"torch/csrc/autograd/generated/python_functions_3.cpp",
988988
"torch/csrc/autograd/generated/python_functions_4.cpp",
989+
"torch/csrc/autograd/generated/python_nested_functions.cpp",
989990
"torch/csrc/autograd/generated/python_nn_functions.cpp",
990991
"torch/csrc/autograd/generated/python_fft_functions.cpp",
991992
"torch/csrc/autograd/generated/python_linalg_functions.cpp",

caffe2/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ set(GENERATED_CXX_PYTHON
395395
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_nn_functions.cpp"
396396
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_fft_functions.cpp"
397397
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_linalg_functions.cpp"
398+
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_nested_functions.cpp"
398399
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_sparse_functions.cpp"
399400
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_special_functions.cpp"
400401
"${TORCH_SRC_DIR}/csrc/autograd/generated/python_return_types.cpp"

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ Features described in this documentation are classified by release status:
9292
rpc
9393
torch.random <random>
9494
masked
95-
nested
95+
torch.nested <nested>
9696
sparse
9797
storage
9898
torch.testing <testing>

docs/source/nested.rst

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,11 @@ nested_tensor([
142142

143143
Note that nt.unbind()[0] is not a, but rather a slice of the underlying memory, which represents the first entry or constituent of the NestedTensor.
144144

145-
Nested tensor methods
145+
Nested tensor functions
146146
+++++++++++++++++++++++++
147147

148-
The following Tensor methods are related to nested tensors:
148+
The following functions are related to nested tensors:
149149

150-
.. currentmodule:: torch
151-
.. autosummary::
152-
:toctree: generated
153-
:nosignatures:
150+
.. currentmodule:: torch.nested
154151

155-
Tensor.to_padded_tensor
152+
.. autofunction:: to_padded_tensor

test/allowlist_for_publicAPI.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,9 @@
12161216
"torch.multiprocessing.spawn": [
12171217
"Optional"
12181218
],
1219+
"torch.nested": [
1220+
"to_padded_tensor"
1221+
],
12191222
"torch.nn.common_types": [
12201223
"Optional",
12211224
"Tensor",

test/cpp/api/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(TORCH_API_TEST_SOURCES
1818
${TORCH_API_TEST_DIR}/moduledict.cpp
1919
${TORCH_API_TEST_DIR}/modulelist.cpp
2020
${TORCH_API_TEST_DIR}/modules.cpp
21+
${TORCH_API_TEST_DIR}/nested.cpp
2122
${TORCH_API_TEST_DIR}/parameterdict.cpp
2223
${TORCH_API_TEST_DIR}/parameterlist.cpp
2324
${TORCH_API_TEST_DIR}/namespace.cpp

test/cpp/api/nested.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <gtest/gtest.h>
2+
3+
#include <torch/nested.h>
4+
#include <torch/torch.h>
5+
6+
#include <test/cpp/api/support.h>
7+
8+
// Simple test that verifies the nested namespace is registered properly
9+
// properly in C++
10+
TEST(NestedTest, Nested) {
11+
auto a = torch::randn({2, 3});
12+
auto b = torch::randn({4, 5});
13+
auto nt = torch::nested_tensor({a, b});
14+
torch::nested::to_padded_tensor(nt, 0);
15+
}

test/forward_backward_compatibility/check_forward_backward_compatibility.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@
278278
# Distributed c10d ops are all going to be updated
279279
("c10d::.*", datetime.date(2022, 10, 31)),
280280
("c10d::allgather_", datetime.date(2022, 10, 1)),
281+
("aten::to_padded_tensor", datetime.date(2022, 10, 1)),
281282
]
282283

283284
ALLOW_LIST_COMPILED = [

test/test_native_mha.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def forward(self, q, k, v, key_padding_mask):
213213
q, k, v, key_padding_mask=mask if use_padding and not use_nt else None
214214
)
215215
if use_nt:
216-
ynpt = ynpt.to_padded_tensor(0)
216+
ynpt = torch.nested.to_padded_tensor(ynpt, 0)
217217
if pad_all:
218218
ynpt_final = torch.zeros_like(ypt)
219219
ynpt_final[:, :ynpt.shape[1], :] = ynpt

0 commit comments

Comments
 (0)