|
| 1 | +# Copyright (c) 2026 Software Mansion |
| 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 torch |
| 10 | +from executorch.backends.xnnpack.test.tester import Tester |
| 11 | + |
| 12 | + |
| 13 | +class TestToCopyDtype(unittest.TestCase): |
| 14 | + """fp32 <-> fp16 casts are lowered to XNNPACK convert nodes.""" |
| 15 | + |
| 16 | + def setUp(self): |
| 17 | + torch._dynamo.reset() |
| 18 | + |
| 19 | + class Fp16Inner(torch.nn.Module): |
| 20 | + """fp32 I/O contract around an fp16 model: the common deployment shape.""" |
| 21 | + |
| 22 | + def __init__(self): |
| 23 | + super().__init__() |
| 24 | + self.conv = torch.nn.Conv2d(3, 8, 3, padding=1).half() |
| 25 | + |
| 26 | + def forward(self, x): |
| 27 | + return self.conv(x.to(torch.float16)).to(torch.float32) |
| 28 | + |
| 29 | + class CastOnly(torch.nn.Module): |
| 30 | + def forward(self, x): |
| 31 | + return (x.to(torch.float16) * 2.0).to(torch.float32) |
| 32 | + |
| 33 | + cast_ops = [ |
| 34 | + "executorch_exir_dialects_edge__ops_dim_order_ops__to_dim_order_copy_default", |
| 35 | + "executorch_exir_dialects_edge__ops_aten__to_copy_default", |
| 36 | + ] |
| 37 | + |
| 38 | + def _run(self, module, inputs): |
| 39 | + ( |
| 40 | + Tester(module.eval(), inputs) |
| 41 | + .export() |
| 42 | + .to_edge_transform_and_lower() |
| 43 | + .check_not(self.cast_ops) |
| 44 | + .check_count({"torch.ops.higher_order.executorch_call_delegate": 1}) |
| 45 | + .to_executorch() |
| 46 | + .serialize() |
| 47 | + .run_method_and_compare_outputs(atol=2e-2, rtol=2e-2) |
| 48 | + ) |
| 49 | + |
| 50 | + def test_fp32_io_around_fp16_conv(self): |
| 51 | + self._run(self.Fp16Inner(), (torch.randn(1, 3, 8, 8),)) |
| 52 | + |
| 53 | + def test_cast_only(self): |
| 54 | + self._run(self.CastOnly(), (torch.randn(2, 5),)) |
| 55 | + |
| 56 | + def test_int_cast_not_partitioned(self): |
| 57 | + class ToInt(torch.nn.Module): |
| 58 | + def forward(self, x): |
| 59 | + return x.to(torch.int32) |
| 60 | + |
| 61 | + ( |
| 62 | + Tester(ToInt().eval(), (torch.randn(2, 5),)) |
| 63 | + .export() |
| 64 | + .to_edge_transform_and_lower() |
| 65 | + .check_not(["torch.ops.higher_order.executorch_call_delegate"]) |
| 66 | + ) |
| 67 | + |
| 68 | + def test_dtype_cast_with_dim_order_change_not_partitioned(self): |
| 69 | + class ToHalfChannelsLast(torch.nn.Module): |
| 70 | + def forward(self, x): |
| 71 | + return x.to(torch.float16, memory_format=torch.channels_last) |
| 72 | + |
| 73 | + ( |
| 74 | + Tester(ToHalfChannelsLast().eval(), (torch.randn(1, 3, 4, 4),)) |
| 75 | + .export() |
| 76 | + .to_edge_transform_and_lower() |
| 77 | + .check_not(["torch.ops.higher_order.executorch_call_delegate"]) |
| 78 | + .check_count({self.cast_ops[0]: 1}) |
| 79 | + ) |
| 80 | + |
| 81 | + def test_dtype_cast_to_contiguous_from_channels_last_not_partitioned(self): |
| 82 | + class ChannelsLastToHalfContiguous(torch.nn.Module): |
| 83 | + def forward(self, x): |
| 84 | + x = x.to(memory_format=torch.channels_last) |
| 85 | + return x.to(torch.float16, memory_format=torch.contiguous_format) |
| 86 | + |
| 87 | + ( |
| 88 | + Tester(ChannelsLastToHalfContiguous().eval(), (torch.randn(1, 3, 4, 4),)) |
| 89 | + .export() |
| 90 | + .to_edge_transform_and_lower() |
| 91 | + .check_count({self.cast_ops[0]: 1}) |
| 92 | + .to_executorch() |
| 93 | + .serialize() |
| 94 | + .run_method_and_compare_outputs(atol=2e-2, rtol=2e-2) |
| 95 | + ) |
| 96 | + |
| 97 | + def test_dtype_cast_preserving_channels_last_partitioned(self): |
| 98 | + class ChannelsLastToHalf(torch.nn.Module): |
| 99 | + def forward(self, x): |
| 100 | + return x.to(memory_format=torch.channels_last).to(torch.float16) |
| 101 | + |
| 102 | + self._run(ChannelsLastToHalf(), (torch.randn(1, 3, 4, 4),)) |
0 commit comments