|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +import torch |
| 4 | + |
| 5 | +from executorch.backends.nxp.backend.edge_program_converter import ( |
| 6 | + EdgeProgramToIRConverter, |
| 7 | +) |
| 8 | +from executorch.backends.nxp.backend.ir.converter.node_converters.ops_converters.hardtanh_converter import ( |
| 9 | + HardTanhConverter, |
| 10 | +) |
| 11 | +from executorch.backends.nxp.tests.executorch_pipeline import to_quantized_edge_program |
| 12 | +from executorch.backends.nxp.tests.executors import ( |
| 13 | + convert_run_compare, |
| 14 | + graph_contains_any_of_ops, |
| 15 | + ToNCHWPreprocess, |
| 16 | + ToNHWCPreprocess, |
| 17 | +) |
| 18 | +from executorch.exir.dialects._ops import ops as exir_ops |
| 19 | +from torch.export import ExportedProgram |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture(autouse=True) |
| 23 | +def reseed_model_per_test_run(): |
| 24 | + torch.manual_seed(23) |
| 25 | + np.random.seed(23) |
| 26 | + |
| 27 | + |
| 28 | +class Relu6ConvBlock(torch.nn.Module): |
| 29 | + def __init__(self, conv_in_channels: int = 3, inplace: bool = False): |
| 30 | + super().__init__() |
| 31 | + self.block = torch.nn.Sequential( |
| 32 | + torch.nn.Conv2d( |
| 33 | + in_channels=conv_in_channels, out_channels=64, kernel_size=(4, 4) |
| 34 | + ), |
| 35 | + torch.nn.ReLU6(inplace=inplace), |
| 36 | + ) |
| 37 | + |
| 38 | + def forward(self, x): |
| 39 | + return self.block(x) |
| 40 | + |
| 41 | + |
| 42 | +class CustomHardTanhBlock(torch.nn.Module): |
| 43 | + def __init__( |
| 44 | + self, |
| 45 | + conv_in_channels: int = 3, |
| 46 | + min_act_val: float = -1.0, |
| 47 | + max_act_val: float = 1.0, |
| 48 | + inplace: bool = False, |
| 49 | + ): |
| 50 | + super().__init__() |
| 51 | + self.block = torch.nn.Sequential( |
| 52 | + torch.nn.Conv2d( |
| 53 | + in_channels=conv_in_channels, out_channels=64, kernel_size=(4, 4) |
| 54 | + ), |
| 55 | + torch.nn.Hardtanh( |
| 56 | + min_val=min_act_val, max_val=max_act_val, inplace=inplace |
| 57 | + ), |
| 58 | + ) |
| 59 | + |
| 60 | + def forward(self, x): |
| 61 | + return self.block(x) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize("input_shape", [(1, 3, 128, 128), (1, 3, 256, 256)]) |
| 65 | +@pytest.mark.parametrize("inplace", [True, False]) |
| 66 | +def test_relu6_quant(mocker, input_shape: tuple[int], inplace: bool): |
| 67 | + # The torch.nn.Relu6 inherits from torch.nn.Hardtanh, and hence represented as HardTanh in ATen. |
| 68 | + # Testing the hardtanh originated from torch.nn.Relu6 op. |
| 69 | + model = Relu6ConvBlock(conv_in_channels=input_shape[1], inplace=inplace) |
| 70 | + |
| 71 | + converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 72 | + |
| 73 | + quantized_program = to_quantized_edge_program(model, input_shape).exported_program() |
| 74 | + |
| 75 | + tflite_flatbuffers_model, io_formats = converter_spy.spy_return |
| 76 | + exported_program: ExportedProgram = converter_spy.call_args.args[1] |
| 77 | + |
| 78 | + ops = [exir_ops.edge.aten.hardtanh.default, exir_ops.edge.aten.hardtanh_.default] |
| 79 | + assert not graph_contains_any_of_ops(graph=quantized_program.graph, ops=ops) |
| 80 | + |
| 81 | + input_data = (np.random.random(input_shape) * 50).astype(np.int8) |
| 82 | + convert_run_compare( |
| 83 | + exported_program, |
| 84 | + tfl_model=tflite_flatbuffers_model, |
| 85 | + tflite_input_preprocess=ToNHWCPreprocess(), |
| 86 | + tflite_output_preprocess=ToNCHWPreprocess(), |
| 87 | + input_data=input_data, |
| 88 | + atol=1.0, |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +@pytest.mark.parametrize("input_shape", [(1, 3, 128, 128), (1, 3, 256, 256)]) |
| 93 | +@pytest.mark.parametrize( |
| 94 | + "activation_range", list(HardTanhConverter.supported_modes_map.keys()) |
| 95 | +) |
| 96 | +@pytest.mark.parametrize("inplace", [True, False]) |
| 97 | +def test_custom_hardtanh_quant( |
| 98 | + mocker, input_shape: tuple[int], activation_range: tuple[int, int], inplace: bool |
| 99 | +): |
| 100 | + min_val, max_val = activation_range |
| 101 | + model = CustomHardTanhBlock( |
| 102 | + conv_in_channels=input_shape[1], |
| 103 | + min_act_val=min_val, |
| 104 | + max_act_val=max_val, |
| 105 | + inplace=inplace, |
| 106 | + ) |
| 107 | + |
| 108 | + converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 109 | + |
| 110 | + quantized_program = to_quantized_edge_program(model, input_shape).exported_program() |
| 111 | + |
| 112 | + tflite_flatbuffers_model, io_formats = converter_spy.spy_return |
| 113 | + exported_program: ExportedProgram = converter_spy.call_args.args[1] |
| 114 | + |
| 115 | + ops = [exir_ops.edge.aten.hardtanh.default, exir_ops.edge.aten.hardtanh_.default] |
| 116 | + assert not graph_contains_any_of_ops(graph=quantized_program.graph, ops=ops) |
| 117 | + |
| 118 | + input_data = (np.random.random(input_shape) * 50).astype(np.int8) |
| 119 | + convert_run_compare( |
| 120 | + exported_program, |
| 121 | + tfl_model=tflite_flatbuffers_model, |
| 122 | + tflite_input_preprocess=ToNHWCPreprocess(), |
| 123 | + tflite_output_preprocess=ToNCHWPreprocess(), |
| 124 | + input_data=input_data, |
| 125 | + atol=1.0, |
| 126 | + ) |
0 commit comments