|
14 | 14 | from torch.testing._internal.common_utils import ( |
15 | 15 | install_cpp_extension, |
16 | 16 | IS_WINDOWS, |
| 17 | + parametrize, |
17 | 18 | run_tests, |
18 | 19 | skipIfTorchDynamo, |
19 | 20 | TestCase, |
@@ -618,43 +619,92 @@ def test_get_num_threads(self, device): |
618 | 619 | self.assertEqual(num_threads, expected_num_threads) |
619 | 620 |
|
620 | 621 | @skipIfTorchVersionLessThan(2, 10) |
621 | | - def test_my_empty(self, device): |
| 622 | + @parametrize("layout", [None, torch.strided, torch.sparse_coo]) |
| 623 | + @parametrize( |
| 624 | + "memory_format", [None, torch.channels_last, torch.contiguous_format] |
| 625 | + ) |
| 626 | + def test_my_empty(self, device, layout, memory_format): |
622 | 627 | import libtorch_agnostic_2_10 as libtorch_agnostic |
623 | 628 |
|
624 | 629 | deterministic = torch.are_deterministic_algorithms_enabled() |
625 | 630 | try: |
626 | 631 | # set use_deterministic_algorithms to fill uninitialized memory |
627 | 632 | torch.use_deterministic_algorithms(True) |
628 | 633 |
|
629 | | - size = [2, 3] |
630 | | - result = libtorch_agnostic.ops.my_empty(size, None, None, None) |
631 | | - expected = torch.empty(size) |
632 | | - self.assertEqual(result, expected, exact_device=True) |
| 634 | + # Use 4D size for channels_last, 2D otherwise |
| 635 | + size = [2, 3, 4, 5] if memory_format == torch.channels_last else [2, 3] |
| 636 | + |
| 637 | + # sparse_coo layout doesn't support memory_format parameter |
| 638 | + if layout == torch.sparse_coo and memory_format is not None: |
| 639 | + return |
| 640 | + |
| 641 | + # Test default parameters |
| 642 | + result = libtorch_agnostic.ops.my_empty( |
| 643 | + size, None, layout, None, None, memory_format |
| 644 | + ) |
| 645 | + expected = torch.empty(size, layout=layout, memory_format=memory_format) |
| 646 | + self.assertEqual(result, expected, exact_device=True, exact_layout=True) |
633 | 647 |
|
| 648 | + # Test with dtype |
634 | 649 | result_float = libtorch_agnostic.ops.my_empty( |
635 | | - size, torch.float32, None, None |
| 650 | + size, torch.float32, layout, None, None, memory_format |
| 651 | + ) |
| 652 | + expected_float = torch.empty( |
| 653 | + size, |
| 654 | + dtype=torch.float32, |
| 655 | + layout=layout, |
| 656 | + memory_format=memory_format, |
| 657 | + ) |
| 658 | + self.assertEqual( |
| 659 | + result_float, expected_float, exact_device=True, exact_layout=True |
636 | 660 | ) |
637 | | - expected_float = torch.empty(size, dtype=torch.float32) |
638 | | - self.assertEqual(result_float, expected_float, exact_device=True) |
639 | 661 |
|
| 662 | + # Test with dtype and device |
640 | 663 | result_with_device = libtorch_agnostic.ops.my_empty( |
641 | | - size, torch.float64, device, None |
| 664 | + size, torch.float64, layout, device, None, memory_format |
642 | 665 | ) |
643 | 666 | expected_with_device = torch.empty( |
644 | | - size, dtype=torch.float64, device=device |
| 667 | + size, |
| 668 | + dtype=torch.float64, |
| 669 | + layout=layout, |
| 670 | + device=device, |
| 671 | + memory_format=memory_format, |
645 | 672 | ) |
646 | 673 | self.assertEqual( |
647 | | - result_with_device, expected_with_device, exact_device=True |
| 674 | + result_with_device, |
| 675 | + expected_with_device, |
| 676 | + exact_device=True, |
| 677 | + exact_layout=True, |
648 | 678 | ) |
649 | 679 |
|
650 | | - if device == "cuda": |
| 680 | + # Verify layout if specified |
| 681 | + if layout is not None: |
| 682 | + self.assertEqual(result_with_device.layout, layout) |
| 683 | + |
| 684 | + # Verify memory format if specified |
| 685 | + if memory_format == torch.channels_last: |
| 686 | + self.assertTrue( |
| 687 | + result_with_device.is_contiguous( |
| 688 | + memory_format=torch.channels_last |
| 689 | + ) |
| 690 | + ) |
| 691 | + elif memory_format == torch.contiguous_format: |
| 692 | + self.assertTrue(result_with_device.is_contiguous()) |
| 693 | + |
| 694 | + # Test pin_memory on CUDA (only once, not for every parameter combination) |
| 695 | + if device == "cuda" and layout is None and memory_format is None: |
651 | 696 | result_pinned = libtorch_agnostic.ops.my_empty( |
652 | | - size, torch.float32, "cpu", True |
| 697 | + [2, 3], torch.float32, None, "cpu", True, None |
653 | 698 | ) |
654 | 699 | expected_pinned = torch.empty( |
655 | | - size, dtype=torch.float32, device="cpu", pin_memory=True |
| 700 | + [2, 3], dtype=torch.float32, device="cpu", pin_memory=True |
| 701 | + ) |
| 702 | + self.assertEqual( |
| 703 | + result_pinned, |
| 704 | + expected_pinned, |
| 705 | + exact_device=True, |
| 706 | + exact_layout=True, |
656 | 707 | ) |
657 | | - self.assertEqual(result_pinned, expected_pinned) |
658 | 708 | self.assertTrue(result_pinned.is_pinned()) |
659 | 709 | finally: |
660 | 710 | torch.use_deterministic_algorithms(deterministic) |
|
0 commit comments