|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import unittest |
| 8 | + |
| 9 | +# Import the registry to register the ops |
| 10 | +import executorch.exir.passes._device_copy_ops_registry # noqa: F401 |
| 11 | + |
| 12 | +import torch |
| 13 | + |
| 14 | + |
| 15 | +class DeviceCopyOpsRegistryTest(unittest.TestCase): |
| 16 | + """Tests that et_copy._h2d_copy and et_copy._d2h_copy ops are correctly |
| 17 | + registered and produce expected outputs during tracing (CPU-only).""" |
| 18 | + |
| 19 | + def test_h2d_copy_functional(self): |
| 20 | + """_h2d_copy should return a clone of the input tensor.""" |
| 21 | + x = torch.randn(2, 3) |
| 22 | + result = torch.ops.et_copy._h2d_copy(x) |
| 23 | + self.assertEqual(result.shape, x.shape) |
| 24 | + self.assertEqual(result.dtype, x.dtype) |
| 25 | + self.assertTrue(torch.equal(result, x)) |
| 26 | + # Should be a new tensor, not the same object |
| 27 | + self.assertFalse(result.data_ptr() == x.data_ptr()) |
| 28 | + |
| 29 | + def test_d2h_copy_functional(self): |
| 30 | + """_d2h_copy should return a clone of the input tensor.""" |
| 31 | + x = torch.randn(4, 5) |
| 32 | + result = torch.ops.et_copy._d2h_copy(x) |
| 33 | + self.assertEqual(result.shape, x.shape) |
| 34 | + self.assertEqual(result.dtype, x.dtype) |
| 35 | + self.assertTrue(torch.equal(result, x)) |
| 36 | + self.assertFalse(result.data_ptr() == x.data_ptr()) |
| 37 | + |
| 38 | + def test_h2d_copy_out_variant(self): |
| 39 | + """_h2d_copy.out should copy data into the provided out tensor.""" |
| 40 | + x = torch.randn(3, 3) |
| 41 | + out = torch.empty(3, 3) |
| 42 | + result = torch.ops.et_copy._h2d_copy.out(x, out=out) |
| 43 | + self.assertTrue(result is out) |
| 44 | + self.assertTrue(torch.equal(out, x)) |
| 45 | + |
| 46 | + def test_d2h_copy_out_variant(self): |
| 47 | + """_d2h_copy.out should copy data into the provided out tensor.""" |
| 48 | + x = torch.randn(2, 4) |
| 49 | + out = torch.empty(2, 4) |
| 50 | + result = torch.ops.et_copy._d2h_copy.out(x, out=out) |
| 51 | + self.assertTrue(result is out) |
| 52 | + self.assertTrue(torch.equal(out, x)) |
| 53 | + |
| 54 | + def test_h2d_copy_preserves_dtype(self): |
| 55 | + """_h2d_copy should work with various dtypes.""" |
| 56 | + for dtype in [torch.float32, torch.float16, torch.int32, torch.int64]: |
| 57 | + x = torch.ones(2, 2, dtype=dtype) |
| 58 | + result = torch.ops.et_copy._h2d_copy(x) |
| 59 | + self.assertEqual(result.dtype, dtype) |
| 60 | + self.assertTrue(torch.equal(result, x)) |
| 61 | + |
| 62 | + def test_h2d_copy_scalar_tensor(self): |
| 63 | + """_h2d_copy should handle 0-dim tensors.""" |
| 64 | + x = torch.tensor(3.14) |
| 65 | + result = torch.ops.et_copy._h2d_copy(x) |
| 66 | + self.assertEqual(result.shape, torch.Size([])) |
| 67 | + self.assertTrue(torch.equal(result, x)) |
| 68 | + |
| 69 | + def test_d2h_copy_empty_tensor(self): |
| 70 | + """_d2h_copy should handle empty tensors.""" |
| 71 | + x = torch.empty(0, 3) |
| 72 | + result = torch.ops.et_copy._d2h_copy(x) |
| 73 | + self.assertEqual(result.shape, torch.Size([0, 3])) |
0 commit comments