|
1 | 1 | # Owner(s): ["oncall: export"]
|
2 | 2 | import copy
|
| 3 | +from typing import List, Tuple |
3 | 4 |
|
4 | 5 | import torch
|
5 |
| -from torch.export import Dim |
| 6 | +from torch.export import Dim, export |
6 | 7 | from torch.export._draft_export import draft_export, FailureType
|
7 | 8 | from torch.testing import FileCheck
|
8 | 9 | from torch.testing._internal.common_utils import run_tests, TestCase
|
9 | 10 | from torch.testing._internal.torchbind_impls import (
|
10 | 11 | _empty_tensor_queue,
|
11 | 12 | init_torchbind_implementations,
|
12 | 13 | )
|
| 14 | +from torch.utils._pytree import tree_leaves |
13 | 15 |
|
14 | 16 |
|
15 | 17 | class TestDraftExport(TestCase):
|
@@ -271,6 +273,89 @@ def forward(self, tq, x):
|
271 | 273 | self.assertEqual(tq3.size(), 2)
|
272 | 274 | self.assertEqual(tq.size(), 2)
|
273 | 275 |
|
| 276 | + def test_override_size_and_dtype_mismatched_fake_kernels(self): |
| 277 | + class M(torch.nn.Module): |
| 278 | + def forward(self, a): |
| 279 | + return torch.ops.mylib.foo(a) |
| 280 | + |
| 281 | + @torch.library.custom_op("mylib::foo", mutates_args={}) |
| 282 | + def foo(a: torch.Tensor) -> List[torch.Tensor]: |
| 283 | + x = a * 2 |
| 284 | + y = a.repeat(2, 2) |
| 285 | + z = a.to(torch.bfloat16) |
| 286 | + return [x, y, z] |
| 287 | + |
| 288 | + @foo.register_fake |
| 289 | + def foo_fake_impl(a): |
| 290 | + x = torch.empty_like(a) # good |
| 291 | + y = torch.empty_like(a) # size mismatch |
| 292 | + z = torch.empty_like(a) # dtype mismatch |
| 293 | + return [x, y, z] |
| 294 | + |
| 295 | + mod = M() |
| 296 | + inputs = (torch.randn(3, 3),) |
| 297 | + with self.assertRaises(RuntimeError): |
| 298 | + with torch._functorch.config.patch(fake_tensor_propagate_real_tensors=True): |
| 299 | + export(mod, inputs) |
| 300 | + |
| 301 | + ep, report = draft_export(mod, inputs) |
| 302 | + for ep_out, eager_out in zip(ep.module()(*inputs), mod(*inputs)): |
| 303 | + self.assertTrue(torch.allclose(ep_out, eager_out)) |
| 304 | + self.assertEqual(ep_out.dtype, eager_out.dtype) |
| 305 | + |
| 306 | + self.assertEqual(len(report.failures), 2) |
| 307 | + self.assertEqual( |
| 308 | + report.failures[0].failure_type, FailureType.MISMATCHED_FAKE_KERNEL |
| 309 | + ) |
| 310 | + self.assertEqual( |
| 311 | + report.failures[1].failure_type, FailureType.MISMATCHED_FAKE_KERNEL |
| 312 | + ) |
| 313 | + self.assertEqual( |
| 314 | + sorted([f.data["reason"] for f in report.failures]), |
| 315 | + [ |
| 316 | + "Dtypes torch.bfloat16 and torch.float32 are not equal!", |
| 317 | + "mismatch between fake value 3 and real value 6 ", |
| 318 | + ], |
| 319 | + ) |
| 320 | + |
| 321 | + def test_override_incorrectly_aliasing_kernel(self): |
| 322 | + class M(torch.nn.Module): |
| 323 | + def forward(self, a): |
| 324 | + return torch.ops.mylib.foo(a) |
| 325 | + |
| 326 | + @torch.library.custom_op("mylib::foo", mutates_args={}) |
| 327 | + def foo(a: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]: |
| 328 | + return a * 2, a + 2 |
| 329 | + |
| 330 | + @foo.register_fake |
| 331 | + def foo_fake_impl(a): |
| 332 | + return a, torch.empty_like(a) # incorrectly aliasing |
| 333 | + |
| 334 | + mod = M() |
| 335 | + inputs = (torch.randn(3, 3),) |
| 336 | + with self.assertRaisesRegex( |
| 337 | + RuntimeError, |
| 338 | + "Real tensor propagation found an aliasing mismatch", |
| 339 | + ): |
| 340 | + with torch._functorch.config.patch(fake_tensor_propagate_real_tensors=True): |
| 341 | + export(mod, inputs) |
| 342 | + |
| 343 | + ep, report = draft_export(mod, inputs) |
| 344 | + for ep_out, eager_out in zip( |
| 345 | + tree_leaves(ep.module()(*inputs)), tree_leaves(mod(*inputs)) |
| 346 | + ): |
| 347 | + self.assertTrue(torch.allclose(ep_out, eager_out)) |
| 348 | + self.assertEqual(ep_out.dtype, eager_out.dtype) |
| 349 | + |
| 350 | + self.assertEqual(len(report.failures), 1) |
| 351 | + self.assertEqual( |
| 352 | + report.failures[0].failure_type, FailureType.MISMATCHED_FAKE_KERNEL |
| 353 | + ) |
| 354 | + self.assertTrue( |
| 355 | + "Mismatched aliasing spec between fake kernel and real kernel" |
| 356 | + in report.failures[0].data["reason"] |
| 357 | + ) |
| 358 | + |
274 | 359 |
|
275 | 360 | if __name__ == "__main__":
|
276 | 361 | run_tests()
|
0 commit comments