[CPP FakeTensor] Skip unsupported nested tensor tests (#205364)

## Linked issue or supporting maintainer

Part of #205094.

## BC-breaking?

> No.

## Summary (human written only)

> can you split out the nested tensor skips into its own pr before [CPP FakeTensor] Enable full FakeTensor and compiler CI coverage #205094? submit it as ready for review

## Agent details (AI-generated)

Authored with Codex assistance. Exclude nested-tensor cases when C++ FakeTensor is selected because that backend does not support nested tensors. Preserve Python nested-tensor coverage and isolate nested portions of mixed tests so their dense cases still run in both modes.

Move the existing exclusions in 21 test files ahead of full CI enablement. The split preserves their exact contents and leaves the assembled stack unchanged.

### Test Plan

Lint and independent source review passed. Verified the extracted files match the previous CI commit and the split preserves the stack's final tree. No new runtime tests or build were run for this move.

The staged C++ manifest at this checkpoint does not select these files. Their full C++ CI coverage is enabled by #205094 above this PR; Python CI selection is unchanged.

```bash
python ../sparse-guard-and-nested-20261117/lint_nested.py
python ../sparse-guard-and-nested-20261117/verify_split.py
```
Pull Request resolved: https://github.com/pytorch/pytorch/pull/205364
Approved by: https://github.com/pytorchgreenlight, https://github.com/ezyang
ghstack dependencies: #204939, #204940, #204957
This commit is contained in:
Angel Li
2026-11-17 22:27:49 -08:00
committed by PyTorch MergeBot
parent 2ec8123e7d
commit d0f3e6fa71
26 changed files with 263 additions and 131 deletions
+4
View File
@@ -81,6 +81,7 @@ from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
IS_FBCODE,
parametrize,
skipIfCppFakeTensor,
)
from torch.testing._internal.inductor_utils import GPU_TYPE, HAS_GPU
from torch.testing._internal.two_tensor import TwoTensor
@@ -5198,6 +5199,7 @@ from user code:
self.assertNotIn("SystemError", warned)
self.assertEqual(len(logs.output), 2, warned)
@skipIfCppFakeTensor("no nested tensor support")
def test_aot_compile_module_restores_torch_function_after_a_throw(self):
# RootGuardManager disables TorchFunction while its accessors run and
# restores it on scope exit, so a tree that THROWS out of C++ leaves the
@@ -5271,6 +5273,7 @@ from user code:
)
self.assertEqual(torch._C._get_torch_function_state(), state)
@skipIfCppFakeTensor("no nested tensor support")
def test_aot_compile_function_restores_torch_function_after_a_throw(self):
# load_compiled_function returns an AOTCompiledFunction, whose guard
# check evaluates the same kind of tree, so a C++ throw without the
@@ -5352,6 +5355,7 @@ from user code:
self.assertTrue(advice.endswith(caveat), advice)
self.assertNotIn("Every guard tree raised", message)
@skipIfCppFakeTensor("no nested tensor support")
def test_no_match_message_qualifies_a_rejection_that_followed_a_cpp_throw(self):
# The stub above raises from Python; this is the raise the caveat's clause
# is about, out of a real tree. TENSOR_MATCH on a strided nested tensor
+2
View File
@@ -15,6 +15,7 @@ from torch._dynamo.testing import AotEagerAndRecordGraphs
from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
skipIfCppFakeTensor,
)
from torch.testing._internal.triton_utils import HAS_GPU, requires_gpu
from torch.testing._internal.two_tensor import TwoTensor
@@ -3995,6 +3996,7 @@ class AutogradFunctionFunctorchTests(torch._dynamo.test_case.TestCase):
self.assertEqual(fn(x, dx), torch.zeros(3, 4))
self.assertEqual(torch.compile(fn, backend="aot_eager")(x, dx), fn(x, dx))
@skipIfCppFakeTensor("no nested tensor support")
def test_custom_jvp_on_subclass_without_alias(self):
"""A tensor that cannot be unpacked cannot hold a tangent, so it neither
crashes the tangent check nor fails its own guard."""
+2
View File
@@ -26,6 +26,7 @@ from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
set_default_dtype,
skipIfCppFakeTensor,
TEST_WITH_ASAN,
TEST_WITH_TSAN,
)
@@ -815,6 +816,7 @@ user_stack=None)
"tensor 'x' stride mismatch" in debug_info.verbose_code_parts[0]
)
@skipIfCppFakeTensor("no nested tensor support")
def test_tensor_match_guard_throw_restores_torch_function(self):
# The root disables the TorchFunction TLS while its accessors run and
# restores it on scope exit, so a C++ throw out of an accessor leaves
+6 -1
View File
@@ -18,7 +18,11 @@ from torch._dynamo.testing import (
)
from torch._inductor.cpp_builder import normalize_path_separator
from torch._inductor.utils import clear_caches, fresh_cache
from torch.testing._internal.common_utils import HardwareClassification, IS_WINDOWS
from torch.testing._internal.common_utils import (
HardwareClassification,
IS_WINDOWS,
skipIfCppFakeTensor,
)
class PgoTest(torch._dynamo.test_case.TestCase):
@@ -254,6 +258,7 @@ class PgoTest(torch._dynamo.test_case.TestCase):
run()
self.assertEqual(cnts.frame_count, 1)
@skipIfCppFakeTensor("no nested tensor support")
def test_njt(self):
cnts = CompileCounter()
+2
View File
@@ -71,6 +71,7 @@ from torch.testing._internal.common_utils import (
markDynamoStrictTest,
parametrize,
run_tests,
skipIfCppFakeTensor,
skipIfTorchDynamo,
subtest,
TEST_WITH_ROCM,
@@ -6645,6 +6646,7 @@ class TestVmapDevice(Namespace.TestVmapBase):
check_vmap_fallback(self, test, torch._test_check_tensor)
@skipIfCppFakeTensor("no nested tensor support")
@markDynamoStrictTest
class TestVmapNestedTensorDevice(Namespace.TestVmapBase):
hw_classification = HardwareClassification.ACCELERATOR
+2
View File
@@ -108,6 +108,7 @@ from torch.testing._internal.common_utils import (
runOnRocm,
set_cwd,
skipIfCachingAllocatorDisabled,
skipIfCppFakeTensor,
skipIfWindows,
skipIfWindowsXPU,
skipIfXpu,
@@ -7720,6 +7721,7 @@ class AOTInductorTestsTemplate:
example_inputs = (torch.randn(3, 3, device=self.device), [3, 4])
self.check_model(Model(), example_inputs)
@skipIfCppFakeTensor("no nested tensor support")
def test_nested_tensor_from_jagged(self):
class Model(nn.Module):
def __init__(self) -> None:
+2
View File
@@ -46,6 +46,7 @@ from torch.testing._internal.common_utils import (
noncontiguous_like,
parametrize,
scoped_load_inline,
skipIfCppFakeTensor,
skipIfWindows,
skipIfXpu,
TEST_WITH_ROCM,
@@ -3158,6 +3159,7 @@ main()
"""
self.run_as_subprocess(script)
@skipIfCppFakeTensor("no nested tensor support")
@unittest.skipIf(not HAS_GPU, "requires gpu")
def test_free_activation_memory_subclass(self):
# cover the case when aot inputs have subclasses, resulting in a different runtime wrapper
+2
View File
@@ -84,6 +84,7 @@ from torch.testing._internal.common_utils import (
isRocmArchAnyOf,
MI200_ARCH,
serialTest,
skipIfCppFakeTensor,
skipIfRocm,
skipIfRocmArch,
TEST_MULTIACCELERATOR,
@@ -7915,6 +7916,7 @@ class GraphModule(torch.nn.Module):
):
attention(query, key, value, return_lse=True)
@skipIfCppFakeTensor("no nested tensor support")
@supported_platform
@skip_on_cpu
@skip_on_xpu
+2
View File
@@ -19,6 +19,7 @@ from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize as parametrize_test,
run_tests,
skipIfCppFakeTensor,
TEST_NUMPY,
TEST_WITH_CROSSREF,
xfailIfNoAcceleratorTriton,
@@ -693,6 +694,7 @@ class TestMultiheadAttentionNN(NNTestCase):
attn_mask=torch.randn(5, 4, 4).bernoulli_().to(torch.bool),
)
@skipIfCppFakeTensor("no nested tensor support")
def test_multihead_attn_nested_tensor_outside_fast_path(self):
mha = torch.nn.MultiheadAttention(4, 4, batch_first=True).eval()
nt = torch.nested.nested_tensor([torch.randn(4, 4)])
+2
View File
@@ -32,6 +32,7 @@ from torch.testing._internal.common_device_type import (
from torch.testing._internal.common_utils import (
IS_WINDOWS,
run_tests,
skipIfCppFakeTensor,
skipIfHpu,
skipIfTorchDynamo,
TemporaryFileName,
@@ -721,6 +722,7 @@ class TestExecutionTrace(TestCase):
if not found_root_node:
raise AssertionError("Expected to find root node")
@skipIfCppFakeTensor("no nested tensor support")
@skipIfTorchDynamo("https://github.com/pytorch/pytorch/issues/124500")
def test_execution_trace_nested_tensor(self):
def fn(nt):
+13 -4
View File
@@ -69,6 +69,7 @@ from torch.testing._internal.common_utils import (
parametrize,
run_tests,
serialTest,
skipIfCppFakeTensor,
skipIfRocm,
skipIfTorchDynamo,
TemporaryFileName,
@@ -1431,6 +1432,7 @@ torch.ones(10) * 2
id_uniqueness_set.add(corr_id)
self.assertTrue(corr_id < uint32_max)
@skipIfCppFakeTensor("no nested tensor support")
def test_nested_tensor_with_shapes(self):
a = torch.randn(4, 4)
b = torch.randn(4, 4)
@@ -1445,6 +1447,17 @@ torch.ones(10) * 2
self.assertTrue(len(e.input_shapes) > 0)
self.assertTrue(len(e.input_shapes[0]) > 0)
@skipIfCppFakeTensor("no nested tensor support")
def test_flops_nested(self):
nested_tensor = torch.nested.nested_tensor(
[torch.randn((2, 5)), torch.randn((3, 5))], layout=torch.jagged
)
with _profile(
record_shapes=True, with_flops=True, use_kineto=kineto_available()
) as prof:
nested_tensor = nested_tensor + nested_tensor
prof.key_averages(group_by_input_shape=True).table()
def test_custom_autograd_function_input_shapes(self):
class CustomRecordShapes(torch.autograd.Function):
@staticmethod
@@ -2875,14 +2888,10 @@ class TestProfilerDevice(TestCase):
nn.ReLU(),
)
inputs = torch.randn(40, 16, 18, 260)
nested_tensor = torch.nested.nested_tensor(
[torch.randn((2, 5)), torch.randn((3, 5))], layout=torch.jagged
)
with _profile(
record_shapes=True, with_flops=True, use_kineto=kineto_available()
) as prof:
model(inputs)
nested_tensor = nested_tensor + nested_tensor
profiler_output = prof.key_averages(group_by_input_shape=True).table(
sort_by="cpu_time_total", row_limit=10
)
+75 -60
View File
@@ -81,6 +81,7 @@ from torch.testing._internal.common_utils import (
scoped_load_inline,
set_warn_always_context,
skipCUDANonDefaultStreamIf,
skipIfCppFakeTensor,
skipIfNoLapack,
skipIfSlowGradcheckEnv,
skipIfTorchDynamo,
@@ -1308,6 +1309,22 @@ class TestAutograd(TestCase):
self.assertEqual(metadata_0.shape, (3,))
self.assertEqual(metadata_1.shape, (3,))
class Test(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
return x
@staticmethod
def backward(ctx, grad_output):
return grad_output
x = torch.randn(3, 3, requires_grad=True)
x = Test.apply(x)
metadata = x.grad_fn._input_metadata[0]
self.assertEqual(metadata.shape, (3, 3))
@skipIfCppFakeTensor("no nested tensor support")
def test_grad_fn_input_metadata_nested(self):
# Preserves symints
nt = torch.nested.nested_tensor(
[torch.randn(3, 2), torch.randn(2, 2)],
@@ -1323,20 +1340,6 @@ class TestAutograd(TestCase):
self.assertFalse(nt_metadata.is_cpp_nested_tensor)
self.assertEqual(nt_metadata.dtype, nt.dtype)
class Test(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
return x
@staticmethod
def backward(ctx, grad_output):
return grad_output
x = torch.randn(3, 3, requires_grad=True)
x = Test.apply(x)
metadata = x.grad_fn._input_metadata[0]
self.assertEqual(metadata.shape, (3, 3))
def test_gradient_edge_output(self):
x = torch.tensor([1.0, 2.0], requires_grad=True)
@@ -6342,6 +6345,8 @@ Done""",
if grad_shape != torch.Size([5, 10]):
raise AssertionError(f"expected grad_shape == (5, 10), got {grad_shape}")
@skipIfCppFakeTensor("no nested tensor support")
def test_calculate_shape_util_nested(self):
out = torch.nested.as_nested_tensor(
[
torch.randn(10, 5, requires_grad=True),
@@ -12247,42 +12252,43 @@ get_out().sum().backward()
)
self.assertTrue(err_msg in e.output.decode("utf-8"))
def _test_view_func_replay(self, fn, inp, *args, use_unsafe_view_func=False):
def _assert_match_metadata(a, b):
self.assertEqual(a.size(), b.size())
self.assertEqual(a.stride(), b.stride())
self.assertEqual(a.storage_offset(), b.storage_offset())
self.assertEqual(a.device, b.device)
self.assertEqual(a.dtype, b.dtype)
outs = fn(inp, *args)
# handle functions that return multiple views (e.g. split)
if isinstance(outs, torch.Tensor):
outs = [outs]
for out in outs:
self.assertTrue(out._is_view())
self.assertTrue(out._base is inp)
# forward view_func
new_inp = inp.clone()
_assert_match_metadata(new_inp, inp)
if use_unsafe_view_func:
new_out = out._view_func_unsafe(new_inp)
else:
new_out = out._view_func(new_inp)
_assert_match_metadata(new_out, out)
self.assertEqual(new_out, out)
# reverse view_func
new_out = out.detach()
new_inp = out._rev_view_func_unsafe(new_out)
_assert_match_metadata(new_inp, inp)
self.assertTrue(new_inp._is_view())
self.assertTrue(new_inp._base is new_out)
def test_view_func_replay(self):
with torch.autograd._force_original_view_tracking(True):
def _assert_match_metadata(a, b):
self.assertEqual(a.size(), b.size())
self.assertEqual(a.stride(), b.stride())
self.assertEqual(a.storage_offset(), b.storage_offset())
self.assertEqual(a.device, b.device)
self.assertEqual(a.dtype, b.dtype)
def _test_fn(fn, inp, *args, use_unsafe_view_func=False):
outs = fn(inp, *args)
# handle functions that return multiple views (e.g. split)
if isinstance(outs, torch.Tensor):
outs = [outs]
for out in outs:
self.assertTrue(out._is_view())
self.assertTrue(out._base is inp)
# forward view_func
new_inp = inp.clone()
_assert_match_metadata(new_inp, inp)
if use_unsafe_view_func:
new_out = out._view_func_unsafe(new_inp)
else:
new_out = out._view_func(new_inp)
_assert_match_metadata(new_out, out)
self.assertEqual(new_out, out)
# reverse view_func
new_out = out.detach()
new_inp = out._rev_view_func_unsafe(new_out)
_assert_match_metadata(new_inp, inp)
self.assertTrue(new_inp._is_view())
self.assertTrue(new_inp._base is new_out)
_test_fn = self._test_view_func_replay
# test individual view ops
_test_fn(torch.ops.aten.alias.default, torch.rand(2, 2))
@@ -12345,8 +12351,11 @@ get_out().sum().backward()
_test_fn(chain_with_only_current_view_func, torch.randn(2, 3, 4))
# TODO: Move this somewhere else
# test NT views
@skipIfCppFakeTensor("no nested tensor support")
def test_view_func_replay_nested(self):
with torch.autograd._force_original_view_tracking(True):
_test_fn = self._test_view_func_replay
from torch.nested._internal.nested_tensor import (
nested_view_from_values_offsets,
)
@@ -18077,6 +18086,8 @@ class TestAutogradMultipleDispatch(TestCase):
# bogus gradient registered for AutogradCUDA is grad * 2
self.assertEqual(t.grad, grad * 2)
@skipIfCppFakeTensor("no nested tensor support")
def test_autograd_multiple_dispatch_registrations_nested(self, device):
# test registered AutogradNestedTensor formula
a = (
torch.arange(6, dtype=torch.float, device=device)
@@ -18111,6 +18122,8 @@ class TestAutogradMultipleDispatch(TestCase):
# t.grad is just out.grad by composite op since _test_autograd_multiple_dispatch is just a clone
self.assertEqual(t.grad, grad)
@skipIfCppFakeTensor("no nested tensor support")
def test_autograd_composite_implicit_and_dispatch_registration_nested(self, device):
# test registered AutogradNestedTensor formula
a = (
torch.arange(6, dtype=torch.float, device=device)
@@ -18396,25 +18409,27 @@ class TestAutogradMultipleDispatchGeneric(TestCase):
expected[i, i] = 1.0
self.assertEqual(x.grad, expected)
def test_per_dispatch_key_input_saving(self):
# Tests that sum.dim_IntList's input is not saved for regular tensors but is saved for nested tensors
def foo(x):
# Don't modify the input inplace
x = x.clone()
res = x.sum(-1, keepdim=True)
x.add_(x)
return res
def _test_per_dispatch_key_input_saving(self, x):
x = x.clone()
res = x.sum(-1, keepdim=True)
x.add_(x)
return res
def test_per_dispatch_key_input_saving(self):
inp = torch.rand(2, requires_grad=True)
# sum's input is not saved for regular Tensors
foo(inp).backward()
self._test_per_dispatch_key_input_saving(inp).backward()
@skipIfCppFakeTensor("no nested tensor support")
def test_per_dispatch_key_input_saving_nested(self):
# sum's input is saved for Nested Tensors
nt = torch.nested.nested_tensor(
[torch.rand(2), torch.rand(2)], requires_grad=True
)
with self.assertRaisesRegex(RuntimeError, "modified by an inplace operation"):
foo(nt).backward(torch.nested.nested_tensor([torch.rand(1), torch.rand(1)]))
self._test_per_dispatch_key_input_saving(nt).backward(
torch.nested.nested_tensor([torch.rand(1), torch.rand(1)])
)
@skipIfTorchDynamo("tests eager C++ error paths that Dynamo does not reproduce")
+2
View File
@@ -37,6 +37,7 @@ from torch.testing._internal.common_utils import (
load_tests,
parametrize,
run_tests,
skipIfCppFakeTensor,
skipIfNoDill,
skipIfRocm,
skipIfXpu,
@@ -2979,6 +2980,7 @@ except RuntimeError as e:
class TestDataLoaderDevice(TestCase):
hw_classification = HardwareClassification.ACCELERATOR
@skipIfCppFakeTensor("no nested tensor support")
@parametrize(
"context",
[ctx for ctx in supported_multiprocessing_contexts if ctx is not None],
+2
View File
@@ -28,6 +28,7 @@ from torch.testing._internal.common_utils import (
instantiate_parametrized_tests,
parametrize,
run_tests,
skipIfCppFakeTensor,
subtest,
TEST_WITH_CROSSREF,
TEST_WITH_TORCHDYNAMO,
@@ -922,6 +923,7 @@ class TestFlopCounterDevice(TestCase):
self.assertEqual(gqa_flops, mha_flops)
self.assertTrue(gqa_flops > 0)
@skipIfCppFakeTensor("no nested tensor support")
@requires_capabilities(
Capability.attention.flash_attention,
Capability.attention.mem_efficient_attention,
+7
View File
@@ -12,9 +12,11 @@ from torch.testing._internal.common_device_type import (
skipMeta,
)
from torch.testing._internal.common_utils import (
decorateIf,
HardwareClassification,
parametrize,
run_tests,
skipIfCppFakeTensor,
TestCase,
)
from torch.nn.attention import SDPBackend
@@ -108,6 +110,7 @@ class TestMHADevice(TestCase):
device, dtype, use_nt=False, use_padding=use_padding
)
@skipIfCppFakeTensor("no nested tensor support")
@dtypesIfCUDA(torch.float)
@dtypesIfXPU(torch.float)
@dtypes(torch.float)
@@ -275,6 +278,10 @@ class TestMHADevice(TestCase):
else:
self.assertEqual(weight_pt, weight_npt)
@decorateIf(
skipIfCppFakeTensor("no nested tensor support"),
lambda p: p["use_nt"],
)
@dtypesIfCUDA(torch.float, torch.half)
@dtypesIfXPU(torch.float, torch.half)
@dtypes(torch.float)
+61 -49
View File
@@ -40,7 +40,7 @@ from torch.testing._internal.common_utils import dtype_name, freeze_rng_state, r
download_file, get_function_arglist, load_tests, skipIfMPS, IS_PPC, IS_ARM64, IS_MACOS, IS_WINDOWS, IS_CPU_CAPABILITY_SVE, IS_CPU_EXT_SVE_SUPPORTED, xfailIf, \
parametrize as parametrize_test, subtest, instantiate_parametrized_tests, \
skipIfTorchDynamo, gcIfJetson, set_default_dtype, skipIfNoCuteDSL, isRocmArchAnyOf, MI200_ARCH, \
TEST_WITH_TORCHDYNAMO, HardwareClassification, serialTest, decorateIf
TEST_WITH_TORCHDYNAMO, HardwareClassification, serialTest, decorateIf, skipIfCppFakeTensor
from torch.testing._internal.common_cuda import TEST_CUDA, TEST_CUDNN, \
SM80OrLater, SM90OrLater, has_device_side_assert
from torch.testing._internal.common_nn import NNTestCase, NewModuleTest, CriterionTest, \
@@ -2064,6 +2064,7 @@ tensor(..., device='meta', size=(1,), requires_grad=True)""")
self.assertRaises(RuntimeError, lambda: F.pad(inputs, (1, 1)))
self.assertRaises(RuntimeError, lambda: F.pad(inputs, (1,)))
@skipIfCppFakeTensor("no nested tensor support")
def test_nested_tensor_from_mask(self):
N, L, D = 10, 12, 14
@@ -2080,6 +2081,7 @@ tensor(..., device='meta', size=(1,), requires_grad=True)""")
self.assertEqual(input, input_convert)
@skipIfCppFakeTensor("no nested tensor support")
def test_nested_tensor_from_mask_error(self):
N, L, D = 10, 12, 14
@@ -8570,17 +8572,22 @@ class TestNNDeviceType(NNTestCase):
with torch.no_grad():
_test_module_empty_input(self, encoder_layer, input, check_size=False, inference=True)
if batch_first and not TEST_WITH_CROSSREF:
with torch.no_grad():
# A NestedTensor with no tensors inside it doesn't have dim 3 (or dim
# 2, for that matter) so it can't hit the fast path, nor can we give a
# result.
with self.assertRaisesRegex(
AssertionError, 'MultiheadAttention does not support NestedTensor outside'):
nt = torch.nested.nested_tensor([], device=device)
@skipIfCppFakeTensor("no nested tensor support")
def _test_nested():
with torch.no_grad():
# A NestedTensor with no tensors inside it doesn't have dim 3 (or dim
# 2, for that matter) so it can't hit the fast path, nor can we give a
# result.
with self.assertRaisesRegex(
AssertionError, 'MultiheadAttention does not support NestedTensor outside'):
nt = torch.nested.nested_tensor([], device=device)
_test_module_empty_input(self, encoder_layer, nt, check_size=False, inference=True)
nt = torch.nested.nested_tensor([torch.rand(0, 512, device=device, dtype=torch.double)], device=device)
_test_module_empty_input(self, encoder_layer, nt, check_size=False, inference=True)
nt = torch.nested.nested_tensor([torch.rand(0, 512, device=device, dtype=torch.double)], device=device)
_test_module_empty_input(self, encoder_layer, nt, check_size=False, inference=True)
with self.subTest(nested_tensor=True):
_test_nested()
else:
_test_module_empty_input(self, encoder_layer, input, check_size=False)
@@ -13554,49 +13561,54 @@ class TestNNDeviceType(NNTestCase):
# currently, which won't be used if training.
if (batch_first and not training and
('cuda' in str(device) or 'cpu' in str(device)) and not TEST_WITH_CROSSREF):
encoder_input[0][-1] = torch.zeros_like(encoder_input[0][1])
mask = torch.zeros(encoder_input.shape[:-1], device=device, dtype=torch.bool)
mask[0][-1] = True
@skipIfCppFakeTensor("no nested tensor support")
def _test_nested():
encoder_input[0][-1] = torch.zeros_like(encoder_input[0][1])
mask = torch.zeros(encoder_input.shape[:-1], device=device, dtype=torch.bool)
mask[0][-1] = True
nt = torch.nested.nested_tensor([encoder_input[0][:-1], encoder_input[1]], device=device)
result = model(nt)
ref_output = torch.tensor(
[
nt = torch.nested.nested_tensor([encoder_input[0][:-1], encoder_input[1]], device=device)
result = model(nt)
ref_output = torch.tensor(
[
[2.4268184, 0.02042419, -0.603311, -0.08476824],
[2.423306, 0.01889652, -0.6057701, -0.08519465],
[2.431538, 0.02078694, -0.5999354, -0.08746159],
[2.4348664, 0.02212971, -0.5975677, -0.08733892],
[2.423133, 0.02097577, -0.60594773, -0.08113337],
[
[2.4268184, 0.02042419, -0.603311, -0.08476824],
[2.423306, 0.01889652, -0.6057701, -0.08519465],
[2.431538, 0.02078694, -0.5999354, -0.08746159],
[2.4348664, 0.02212971, -0.5975677, -0.08733892],
[2.423133, 0.02097577, -0.60594773, -0.08113337],
],
[
[2.4279876, 0.02121329, -0.60249615, -0.08410317],
[2.4138637, 0.02221113, -0.6124869, -0.07249016],
[2.4251041, 0.01974815, -0.6045152, -0.08483928],
[2.4335563, 0.0218913, -0.59850943, -0.08683228],
[2.4229012, 0.02418739, -0.6061784, -0.07492948],
],
],
[
[2.4279876, 0.02121329, -0.60249615, -0.08410317],
[2.4138637, 0.02221113, -0.6124869, -0.07249016],
[2.4251041, 0.01974815, -0.6045152, -0.08483928],
[2.4335563, 0.0218913, -0.59850943, -0.08683228],
[2.4229012, 0.02418739, -0.6061784, -0.07492948],
],
],
device=device, dtype=dtype
)
result = result.to_padded_tensor(0)
ref_output[0][-1] = torch.zeros_like(
ref_output[0][-1], device=device, dtype=dtype
)
result[0][-1] = torch.zeros_like(
result[0][-1], device=device, dtype=dtype
)
self.assertEqual(tuple(result.shape), tuple(ref_output.shape))
if 'cuda' in device:
if dtype == torch.float:
atol = 2e-4
rtol = 4e-3
device=device, dtype=dtype
)
result = result.to_padded_tensor(0)
ref_output[0][-1] = torch.zeros_like(
ref_output[0][-1], device=device, dtype=dtype
)
result[0][-1] = torch.zeros_like(
result[0][-1], device=device, dtype=dtype
)
self.assertEqual(tuple(result.shape), tuple(ref_output.shape))
if 'cuda' in device:
if dtype == torch.float:
atol = 2e-4
rtol = 4e-3
else:
atol = 7e-4
rtol = 2e-2
torch.testing.assert_close(result, ref_output, atol=atol, rtol=rtol)
else:
atol = 7e-4
rtol = 2e-2
torch.testing.assert_close(result, ref_output, atol=atol, rtol=rtol)
else:
torch.testing.assert_close(result, ref_output)
torch.testing.assert_close(result, ref_output)
with self.subTest(nested_tensor=True):
_test_nested()
for batch_first in (True, False):
+2
View File
@@ -60,6 +60,7 @@ from torch.testing._internal.common_utils import (
IS_WINDOWS,
parametrize,
run_tests,
skipIfCppFakeTensor,
skipIfCrossRef,
skipIfTorchDynamo,
TEST_WITH_CROSSREF,
@@ -3200,6 +3201,7 @@ class TestPrecompile(TestCase):
self.assertIn("forward = _build_installed_forward()", python_code)
self.assertNotIn("_FRAMES = ", python_code)
@skipIfCppFakeTensor("no nested tensor support")
def test_nested_input_refused(self):
# A nested example input is refused up front with a named PrecompileError rather
# than the raw internal error one produces further down (a static capture has no
+15 -10
View File
@@ -65,6 +65,7 @@ from torch.testing._internal.common_utils import (
parametrize,
run_tests,
serialTest,
skipIfCppFakeTensor,
skipIfTorchDynamo,
TemporaryDirectoryName,
TemporaryFileName,
@@ -4551,21 +4552,23 @@ class TestSerialization(TestCase, SerializationMixin):
self.assertTrue(id(sd_loaded['t_view'].untyped_storage()) == id(sd_loaded['t'].untyped_storage()))
self.assertTrue(id(sd_loaded['t_slice'].untyped_storage()) == id(sd_loaded['t'].untyped_storage()))
def test_skip_data_serialization_error_cases(self):
def _save_load(t):
with BytesIOContext() as f:
with skip_data():
torch.save(t, f)
f.seek(0)
torch.load(f, weights_only=True)
def _skip_data_save_load(self, t):
with BytesIOContext() as f:
with skip_data():
torch.save(t, f)
f.seek(0)
torch.load(f, weights_only=True)
@skipIfCppFakeTensor("no nested tensor support")
def test_skip_data_serialization_error_nested(self):
nt = torch.nested.nested_tensor([torch.randn(2), torch.randn(3)])
t = torch.randn(2, 3, device="meta")
with self.assertRaisesRegex(RuntimeError, "Cannot serialize nested tensor under skip_data context manager"):
_save_load(nt)
self._skip_data_save_load(nt)
def test_skip_data_serialization_error_cases(self):
t = torch.randn(2, 3, device="meta")
with self.assertWarnsRegex(UserWarning, "meta device under skip_data context manager is a no-op"):
_save_load(t)
self._skip_data_save_load(t)
@parametrize("force_weights_only", (True, False))
def test_weights_only_env_variables(self, force_weights_only):
@@ -4662,6 +4665,7 @@ class TestSerialization(TestCase, SerializationMixin):
finally:
torch._weights_only_unpickler._get_allowed_globals = old_get_allowed_globals
@skipIfCppFakeTensor("no nested tensor support")
@parametrize("should_import", [False, True])
def test_load_njt_weights_only(self, should_import):
with TemporaryFileName() as filename:
@@ -4675,6 +4679,7 @@ class TestSerialization(TestCase, SerializationMixin):
)
self._attempt_load_from_subprocess(filename, import_string, err_msg)
@skipIfCppFakeTensor("no nested tensor support")
@serialTest()
def test_load_njt_weights_only_safe_globals(self):
from torch.nested._internal.nested_tensor import _rebuild_njt, NestedTensor
+4
View File
@@ -38,6 +38,7 @@ from torch.testing._internal.common_optimizers import (
from torch.testing._internal.common_utils import ( # type: ignore[attr-defined]
MI200_ARCH, TEST_WITH_TORCHINDUCTOR, TEST_WITH_ROCM, TEST_CUDA, TEST_CUDA_GRAPH, run_tests, IS_JETSON,
IS_FILESYSTEM_UTF8_ENCODING,
skipIfCppFakeTensor,
IS_SANDCASTLE, IS_FBCODE, IS_REMOTE_GPU, skipIfRocmArch, skipIfTorchInductor, load_tests, slowTest, slowTestIf,
skipIfCrossRef, TEST_WITH_CROSSREF, skipIfTorchDynamo, set_default_dtype,
skipCUDAMemoryLeakCheckIf, BytesIOContext,
@@ -8315,6 +8316,9 @@ class TestTorch(TestCase):
self.assertFalse(t1.is_same_size(t3))
self.assertTrue(t1.is_same_size(t4))
@skipIfCppFakeTensor("no nested tensor support")
def test_is_same_size_nested(self):
t1 = torch.empty(3, 4, 9, 10)
nt1 = torch.nested.nested_tensor([torch.ones(2, 4), torch.ones(3, 4), torch.ones(5, 4)])
nt2 = torch.nested.nested_tensor([torch.ones(2, 4), torch.ones(2, 4), torch.ones(2, 4)])
nt3 = torch.nested.nested_tensor([torch.ones(2, 4, 5), torch.ones(2, 6, 5)])
+54 -7
View File
@@ -40,6 +40,7 @@ from torch.testing._internal.common_utils import (
isRocmArchAnyOf,
MI350_ARCH,
NAVI4_ARCH,
skipIfCppFakeTensor,
skipIfRocm,
skipIfRocmArch,
skipIfTorchDynamo,
@@ -512,6 +513,11 @@ class TestTransformersGeneric(NNTestCase):
for call_args, _ in fastpath_mock.call_args_list:
self.assertEqual(call_args[0].is_nested, nested_tensors)
@skipIfCppFakeTensor("no nested tensor support")
def _test_nested_fastpath(model, key_padding_mask):
nt = torch.nested.nested_tensor([torch.ones((2, 2), dtype=torch.float)])
_test_fastpath(model, key_padding_mask, nt, nested_tensors=True)
encoder_layer = torch.nn.TransformerEncoderLayer(d_model=2, nhead=2, dim_feedforward=8, batch_first=True)
model = torch.nn.TransformerEncoder(encoder_layer, num_layers=2, enable_nested_tensor=True, mask_check=True)
@@ -520,11 +526,11 @@ class TestTransformersGeneric(NNTestCase):
aligned_key_padding_mask = torch.Tensor([[0, 0, 1]]).to(torch.bool)
not_aligned_key_padding_mask = torch.Tensor([[1, 0, 1]]).to(torch.bool)
attn_mask = torch.Tensor([[1, 0, 1], [0, 1, 0], [1, 0, 1]]).to(torch.bool)
nested_tensor_return_value = torch.nested.nested_tensor([torch.ones((2, 2), dtype=torch.float)])
tensor_return_value = torch.ones((1, 3, 2), dtype=torch.float)
# Left aligned mask results in sparsity fastpath
_test_fastpath(model, aligned_key_padding_mask, nested_tensor_return_value, nested_tensors=True)
with self.subTest(nested_tensor=True, mask_check=True):
_test_nested_fastpath(model, aligned_key_padding_mask)
# Not aligned mask results in fastpath
_test_fastpath(model, not_aligned_key_padding_mask, tensor_return_value, nested_tensors=False)
@@ -542,8 +548,9 @@ class TestTransformersGeneric(NNTestCase):
model.eval()
# Mask check disabled results in sparisty fastpath, independently of the mask
_test_fastpath(model, aligned_key_padding_mask, nested_tensor_return_value, nested_tensors=True)
_test_fastpath(model, not_aligned_key_padding_mask, nested_tensor_return_value, nested_tensors=True)
with self.subTest(nested_tensor=True, mask_check=False):
_test_nested_fastpath(model, aligned_key_padding_mask)
_test_nested_fastpath(model, not_aligned_key_padding_mask)
# Test failing MHA when bias was NoneType
def test_bias_is_none(self):
@@ -852,6 +859,12 @@ class TestTransformersAccelerator(NNTestCase):
maybe_autocast = torch.autocast("cuda", dtype=torch.float16) if use_autocast else contextlib.nullcontext()
with maybe_autocast:
if (
torch._dynamo.config.use_cpp_fake_tensor
and enable_nested_tensor
and not torch.is_autocast_enabled()
):
self.skipTest("no nested tensor support")
for input, src_key_padding_mask in input_mask_pairs:
with torch.no_grad():
fastpath_output = model(input, src_key_padding_mask=src_key_padding_mask)
@@ -997,9 +1010,17 @@ class TestTransformersAccelerator(NNTestCase):
# all 0
mask = torch.zeros([2, 5]).to(device) == 1
result = model(encoder_input, src_key_padding_mask=mask)
self.assertEqual(tuple(result.shape), tuple(ref_output.shape))
torch.testing.assert_close(result, ref_output, rtol=1e-7, atol=1e-5)
with self.subTest(mask="all_false"):
if (
torch._dynamo.config.use_cpp_fake_tensor
and enable_nested_tensor
and batch_first
and not training
):
self.skipTest("no nested tensor support")
result = model(encoder_input, src_key_padding_mask=mask)
self.assertEqual(tuple(result.shape), tuple(ref_output.shape))
torch.testing.assert_close(result, ref_output, rtol=1e-7, atol=1e-5)
mask[0, 1] = 1
mask[1, 3] = 1
@@ -1289,6 +1310,7 @@ class TestTransformersAccelerator(NNTestCase):
# CPU unit test has_torch_functions in test environment,
# preventing successful completion
@skipIfCppFakeTensor("no nested tensor support")
@onlyAccelerator
def test_with_nested_tensor_input(self, device):
encoder_layer = nn.TransformerEncoderLayer(
@@ -2215,6 +2237,7 @@ class TestSDPAFailureModes(NNTestCase):
value = torch.randn(shape, dtype=torch.float16, device=device)
self.assertRaises(RuntimeError, lambda: F.scaled_dot_product_attention(query, key, value))
@skipIfCppFakeTensor("no nested tensor support")
@onlyAccelerator
@unittest.skipIf(not PLATFORM_SUPPORTS_MEM_EFF_ATTENTION, "Fused SDPA was not built for this system")
def test_fused_kernels_nested_broadcasting_error_cases(self, device):
@@ -2237,6 +2260,7 @@ class TestSDPAFailureModes(NNTestCase):
torch.nn.functional.scaled_dot_product_attention(
query, key, value, attn_mask=None, dropout_p=0.0, is_causal=False)
@skipIfCppFakeTensor("no nested tensor support")
@onlyAccelerator
@skipXPUIf(not PLATFORM_SUPPORTS_FLASH_ATTENTION_XPU, "XPU Flash Attention is not supported")
@unittest.skipIf(not PLATFORM_SUPPORTS_FLASH_ATTENTION, "Fused SDPA was not built for this system")
@@ -2279,6 +2303,7 @@ class TestSDPAFailureModes(NNTestCase):
self.assertFalse(torch.isnan(out).any(), "Output should not contain NaNs!")
@skipIfCppFakeTensor("no nested tensor support")
@onlyAccelerator
@unittest.skipIf(not PLATFORM_SUPPORTS_FUSED_ATTENTION, "Fused SDPA was not built for this system")
@skipXPUIf(not PLATFORM_SUPPORTS_FLASH_ATTENTION_XPU, "XPU Flash Attention is not supported")
@@ -2307,6 +2332,7 @@ class TestSDPAFailureModes(NNTestCase):
torch.nn.functional.scaled_dot_product_attention(
query, key, value, attn_mask=None, dropout_p=0.0, is_causal=False)
@skipIfCppFakeTensor("no nested tensor support")
@onlyAccelerator
@skipXPUIf(not PLATFORM_SUPPORTS_FLASH_ATTENTION_XPU, "XPU Flash Attention is not supported")
@unittest.skipIf(not PLATFORM_SUPPORTS_FLASH_ATTENTION, "Fused SDPA was not built for this system")
@@ -3126,6 +3152,10 @@ class TestSDPACPU(NNTestCase):
""" Used to test CPU only functionality of scaled_dot_product_attention """
hw_classification = HardwareClassification.CPU
@decorateIf(
skipIfCppFakeTensor("no nested tensor support"),
lambda p: p["type"] == "nested",
)
@parametrize("type", ["dense", "nested"])
@parametrize("dropout", [0.0, 0.7])
@parametrize("dtype", [torch.float64, torch.float32, torch.bfloat16, torch.half])
@@ -5549,6 +5579,10 @@ class TestSDPAAccelerator(NNTestCase):
with torch.backends.cuda.sdp_kernel(enable_math=False, enable_flash=True, enable_mem_efficient=False):
scaled_dot_product_attention(query, key, value)
@decorateIf(
skipIfCppFakeTensor("no nested tensor support"),
lambda p: p["type"] == "nested",
)
@unittest.skipIf(not PLATFORM_SUPPORTS_MEM_EFF_ATTENTION, "Fused SDPA was not built for this system")
@parametrize("type", ["dense", "nested"])
@parametrize("is_contiguous", [True, False])
@@ -5587,6 +5621,7 @@ class TestSDPAAccelerator(NNTestCase):
self.assertEqual(actual.contiguous(), math_ref.contiguous(), atol=2e-3, rtol=1e-2)
@skipIfCppFakeTensor("no nested tensor support")
@unittest.skipIf(not PLATFORM_SUPPORTS_CUDNN_ATTENTION, "Fused SDPA was not built for this system")
@unittest.skipIf("TORCH_CUDNN_SDPA_NESTED_TENSOR_ENABLED" not in os.environ, "cuDNN Nested Tensor support not enabled")
@parametrize("type", ["nested"])
@@ -5619,6 +5654,10 @@ class TestSDPAAccelerator(NNTestCase):
attn_mask=None, dropout_p=0.0, is_causal=False)
self.assertEqual(actual.contiguous(), math_ref.contiguous(), atol=2e-3, rtol=1e-2)
@decorateIf(
skipIfCppFakeTensor("no nested tensor support"),
lambda p: p["type"] == "nested",
)
@skipXPUIf(not PLATFORM_SUPPORTS_FLASH_ATTENTION_XPU, "XPU Flash Attention is not supported")
@unittest.skipIf(not PLATFORM_SUPPORTS_FUSED_ATTENTION, "Fused SDPA was not built for this system")
@parametrize("type", ["dense",] if TEST_XPU else ["dense", "nested"]) # https://github.com/intel/torch-xpu-ops/issues/3132
@@ -5787,6 +5826,10 @@ class TestSDPAAccelerator(NNTestCase):
atol = 9e-4 if dtype == torch.float16 else 9e-3
self.assertEqual(qkv.grad, qkv_lp.grad.to(torch.float64), atol=atol, rtol=rtol)
@decorateIf(
skipIfCppFakeTensor("no nested tensor support"),
lambda p: p["type"] == "nested",
)
@unittest.skipIf(not PLATFORM_SUPPORTS_FUSED_ATTENTION, "Platform does not support fused SDPA")
@skipIfXpu(msg="XPU backend choice / NestedTensorXPU dispatch not aligned; tracked in torch-xpu-ops")
@parametrize("type", ["dense", "nested"])
@@ -6997,6 +7040,7 @@ class TestSDPAAccelerator(NNTestCase):
g_bwd.replay()
self.assertEqual(grads[:3], grads_ref[:3])
@skipIfCppFakeTensor("no nested tensor support")
@skipXPUIf(not PLATFORM_SUPPORTS_FLASH_ATTENTION_XPU, "XPU Flash Attention is not supported")
@unittest.skipIf(not PLATFORM_SUPPORTS_FUSED_ATTENTION, "Fused SDPA was not built for this system")
@parametrize("fused_kernel", [SDPBackend.FLASH_ATTENTION, SDPBackend.EFFICIENT_ATTENTION] if
@@ -7032,6 +7076,7 @@ class TestSDPAAccelerator(NNTestCase):
self.assertEqual(actual.contiguous(), math_ref.contiguous().to(torch.float16), atol=1e-3, rtol=1e-2)
@skipIfCppFakeTensor("no nested tensor support")
@skipXPUIf(not PLATFORM_SUPPORTS_FLASH_ATTENTION_XPU, "XPU Flash Attention is not supported")
@unittest.skipIf(not PLATFORM_SUPPORTS_FUSED_ATTENTION, "Fused SDPA was not built for this system")
@parametrize("kernel", [SDPBackend.FLASH_ATTENTION, SDPBackend.EFFICIENT_ATTENTION] if
@@ -7122,6 +7167,7 @@ class TestSDPAAccelerator(NNTestCase):
self.assertEqual(actual.contiguous(), math_ref.contiguous().to(dtype), atol=1.5e-3, rtol=1e-2)
@skipIfCppFakeTensor("no nested tensor support")
@unittest.skipIf(not PLATFORM_SUPPORTS_MEM_EFF_ATTENTION, "Fused SDPA was not built for this system")
@skipIfXpu(msg="XPU SDPA dispatch lacks nested broadcasting kernels; tracked in torch-xpu-ops")
def test_fused_kernels_nested_broadcasting_query_dense(self, device):
@@ -7157,6 +7203,7 @@ class TestSDPAAccelerator(NNTestCase):
self.assertEqual(actual.contiguous(), math_ref.contiguous(), atol=1e-3, rtol=1e-2)
@skipIfCppFakeTensor("no nested tensor support")
@skipXPUIf(not PLATFORM_SUPPORTS_FLASH_ATTENTION_XPU, "XPU Flash Attention is not supported")
@unittest.skipIf(not PLATFORM_SUPPORTS_FLASH_ATTENTION, "Does not support SDPA or pre-SM80 hardware")
@parametrize("batch_size", [8, 32])
+2
View File
@@ -47,6 +47,7 @@ from torch.testing._internal.common_utils import (
parametrize,
run_tests,
serialTest,
skipIfCppFakeTensor,
subtest,
suppress_warnings,
TEST_WITH_TSAN,
@@ -1061,6 +1062,7 @@ print(torch.xpu.is_initialized())
self.assertIs(result, t)
self.assertTrue(t.is_shared())
@skipIfCppFakeTensor("no nested tensor support")
def test_share_memory_nested(self):
a = torch.randn(3, 4, device="xpu")
b = torch.randn(5, 4, device="xpu")