已合并
test(jit):add test fot ignore #39824
wei-pengfei22创建于 7月2日
test(jit):add test fot ignore #39824
已合并
共 1 个文件变更+161-0
| @@ -14,8 +14,12 @@ torch.jit.ScriptModule.register_state_dict_post_hook | |||
| 14 | (extendable) | 14 | (extendable) |
| 15 | """ | 15 | """ |
| 16 | 16 | ||
| 17 | +import warnings | ||
| 18 | +import io | ||
| 19 | +import numpy as np | ||
| 17 | import torch | 20 | import torch |
| 18 | import torch.nn as nn | 21 | import torch.nn as nn |
| 22 | +from torch.testing._internal.jit_utils import JitTestCase | ||
| 19 | from torch.testing._internal.common_utils import run_tests, TestCase | 23 | from torch.testing._internal.common_utils import run_tests, TestCase |
| 20 | 24 | ||
| 21 | 25 | ||
| @@ -521,6 +525,163 @@ class TestScriptModuleHooks(TestCase): | |||
| 521 | model.state_dict() | 525 | model.state_dict() |
| 522 | self.assertEqual(len(called), 1) | 526 | self.assertEqual(len(called), 1) |
| 523 | 527 | ||
| 528 | +class TestJitIgnoreNPU(JitTestCase): | ||
| 529 | + | ||
| 530 | + def getExportImportCopy(self, mod): | ||
| 531 | + buffer = io.BytesIO() | ||
| 532 | + torch.jit.save(mod, buffer) | ||
| 533 | + buffer.seek(0) | ||
| 534 | + return torch.jit.load(buffer) | ||
| 535 | + | ||
| 536 | + def test_ignore_decorator(self): | ||
| 537 | + with warnings.catch_warnings(record=True) as warns: | ||
| 538 | + warnings.simplefilter("always") | ||
| 539 | + | ||
| 540 | + class M(nn.Module): | ||
| 541 | + def __init__(self) -> None: | ||
| 542 | + super().__init__() | ||
| 543 | + self.state = nn.Buffer(torch.zeros(1).to(device_type)) | ||
| 544 | + | ||
| 545 | + def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
| 546 | + return x | ||
| 547 | + | ||
| 548 | + | ||
| 549 | + def ignored_func(self, x: torch.Tensor) -> torch.Tensor: | ||
| 550 | + self.state = torch.tensor([999.0]).to(device_type) | ||
| 551 | + return x * 10 | ||
| 552 | + | ||
| 553 | + raw_m = M().to(device_type) | ||
| 554 | + m = torch.jit.script(raw_m) | ||
| 555 | + | ||
| 556 | + target_warns = [w for w in warns if "TorchScript will now drop the function" in str(w.message)] | ||
| 557 | + self.assertEqual(len(target_warns), 1) | ||
| 558 | + warn_msg = str(target_warns[0].message) | ||
| 559 | + self.assertIn("TorchScript will now drop the function", warn_msg) | ||
| 560 | + | ||
| 561 | + x = torch.tensor(2.0).to(device_type) | ||
| 562 | + eager_out = m(x) | ||
| 563 | + self.assertEqual(eager_out, x) | ||
| 564 | + | ||
| 565 | + m.ignored_func(x) | ||
| 566 | + self.assertEqual(m.state, torch.tensor([999.0], device=device_type)) | ||
| 567 | + self.assertEqual(m.ignored_func(torch.tensor(3, device=device_type)), torch.tensor(30, device=device_type)) | ||
| 568 | + | ||
| 569 | + m_export = self.getExportImportCopy(m) | ||
| 570 | + | ||
| 571 | + with self.assertRaises(AttributeError): | ||
| 572 | + _ = m_export.ignored_func | ||
| 573 | + | ||
| 574 | + self.assertTrue(hasattr(m, "ignored_func")) | ||
| 575 | + self.assertFalse(hasattr(m_export, "ignored_func")) | ||
| 576 | + self.assertNotIn("ignored_func", dir(m_export)) | ||
| 577 | + | ||
| 578 | + export_out = m_export(x) | ||
| 579 | + self.assertEqual(export_out, x) | ||
| 580 | + self.assertEqual(m_export.state, torch.tensor([999.0], device=device_type)) | ||
| 581 | + | ||
| 582 | + def test_ignored_props(self): | ||
| 583 | + class A(nn.Module): | ||
| 584 | + __jit_ignored_attributes__ = ["ignored", "ignored_return_val"] | ||
| 585 | + | ||
| 586 | + | ||
| 587 | + def ignored(self): | ||
| 588 | + raise ValueError("shouldn't be called") | ||
| 589 | + | ||
| 590 | + | ||
| 591 | + def ignored_return_val(self): | ||
| 592 | + return 1 | ||
| 593 | + | ||
| 594 | + | ||
| 595 | + def call(self): | ||
| 596 | + return self.ignored_return_val | ||
| 597 | + | ||
| 598 | + f = torch.jit.script(A()) | ||
| 599 | + # jank way to test if there is no error | ||
| 600 | + self.assertTrue(isinstance(f, torch.jit.ScriptModule)) | ||
| 601 | + self.assertTrue(isinstance(f.call(), property)) | ||
| 602 | + | ||
| 603 | + def test_torch_ignore_conversion_to_none(self): | ||
| 604 | + class A(torch.nn.Module): | ||
| 605 | + | ||
| 606 | + def ignored(self, a: int) -> None: | ||
| 607 | + l: int = len([2 for i in range(a) if i > 2]) | ||
| 608 | + return | ||
| 609 | + | ||
| 610 | + def forward(self) -> int: | ||
| 611 | + a: int = 4 | ||
| 612 | + b: int = 5 | ||
| 613 | + self.ignored(a) | ||
| 614 | + return a + b | ||
| 615 | + | ||
| 616 | + class B(torch.nn.Module): | ||
| 617 | + | ||
| 618 | + def ignored(self, a: int): | ||
| 619 | + l: int = len([2 for i in range(a) if i > 2]) | ||
| 620 | + return | ||
| 621 | + | ||
| 622 | + def forward(self) -> int: | ||
| 623 | + a: int = 4 | ||
| 624 | + b: int = 5 | ||
| 625 | + self.ignored(a) | ||
| 626 | + return a + b | ||
| 627 | + | ||
| 628 | + modelA = torch.jit.script(A()) | ||
| 629 | + self.assertEqual(modelA(), 9) | ||
| 630 | + | ||
| 631 | + modelB = torch.jit.script(B()) | ||
| 632 | + self.assertEqual(modelB(), 9) | ||
| 633 | + | ||
| 634 | + def test_comment_ignore_indent(self): | ||
| 635 | + class Model(torch.nn.Module): | ||
| 636 | + def __init__(self) -> None: | ||
| 637 | + # useless comment that is not indented correctly # noqa: E115 | ||
| 638 | + super().__init__() | ||
| 639 | + | ||
| 640 | + def forward(self): | ||
| 641 | + return 5 | ||
| 642 | + | ||
| 643 | + # should compile without an error | ||
| 644 | + self.checkModule(Model(), ()) | ||
| 645 | + | ||
| 646 | + def test_ignored_method_binding(self): | ||
| 647 | + class Bar(torch.nn.Module): | ||
| 648 | + def __init__(self) -> None: | ||
| 649 | + super().__init__() | ||
| 650 | + self.x : int = 0 | ||
| 651 | + | ||
| 652 | + | ||
| 653 | + def setx(self, x : int): | ||
| 654 | + self.x = x | ||
| 655 | + | ||
| 656 | + | ||
| 657 | + def getx(self): | ||
| 658 | + return self.x | ||
| 659 | + | ||
| 660 | + | ||
| 661 | + def ignored_getx(self): | ||
| 662 | + return self.x | ||
| 663 | + | ||
| 664 | + b = Bar() | ||
| 665 | + b.setx(123) | ||
| 666 | + sb = torch.jit.script(b) | ||
| 667 | + self.assertEqual(sb.getx(), 123) | ||
| 668 | + self.assertEqual(sb.ignored_getx(), 123) | ||
| 669 | + | ||
| 670 | + sb.setx(456) | ||
| 671 | + self.assertEqual(sb.getx(), 456) | ||
| 672 | + self.assertEqual(sb.ignored_getx(), 456) | ||
| 673 | + | ||
| 674 | + def test_no_self_arg_ignore_function(self): | ||
| 675 | + class MyModule(nn.Module): | ||
| 676 | + | ||
| 677 | + def call_np(): | ||
| 678 | + return np.random.choice(2, p=[.95, .05]) | ||
| 679 | + | ||
| 680 | + def forward(self): | ||
| 681 | + return self.call_np() | ||
| 682 | + | ||
| 683 | + with self.assertRaisesRegex(Exception, "does not have a self argument"): | ||
| 684 | + torch.jit.script(MyModule()) | ||
| 524 | 685 | ||
| 525 | if __name__ == "__main__": | 686 | if __name__ == "__main__": |
| 526 | run_tests() | 687 | run_tests() |