|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +import torch |
| 4 | +from torch.export import ExportedProgram |
| 5 | + |
| 6 | +from executorch.backends.nxp.backend.edge_program_converter import EdgeProgramToIRConverter |
| 7 | +from executorch.backends.nxp.tests.executorch_pipeline import to_quantized_edge_program |
| 8 | +from executorch.backends.nxp.tests.executors import convert_run_compare, ToChannelFirstPreprocess, \ |
| 9 | + ToChannelLastPreprocess |
| 10 | +from executorch.backends.nxp.tests.models import MeanDimConvModule, MeanDimLinearModule |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(autouse=True) |
| 14 | +def reseed_model_per_test_run(): |
| 15 | + torch.manual_seed(23) |
| 16 | + np.random.seed(23) |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.parametrize("input_shape, dim", [ |
| 20 | + pytest.param((1, 4, 8, 8), (-1, -2), id="Dim -1, -2."), |
| 21 | +]) |
| 22 | +def test_mean_dim_conv_quant_conversion(mocker, input_shape, dim, keeepdim=True): |
| 23 | + model = MeanDimConvModule(dim, keeepdim) |
| 24 | + |
| 25 | + converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 26 | + |
| 27 | + # Run conversion |
| 28 | + _ = to_quantized_edge_program(model, input_shape) |
| 29 | + |
| 30 | + # Capture generated model |
| 31 | + tflite_flatbuffers_model, io_formats = converter_spy.spy_return |
| 32 | + |
| 33 | + # Capture converted program |
| 34 | + exported_program: ExportedProgram = converter_spy.call_args.args[1] |
| 35 | + |
| 36 | + input_data = (np.random.random(input_shape).astype(np.float32) * 50).astype(np.int8) |
| 37 | + |
| 38 | + convert_run_compare(exported_program, tflite_input_preprocess=ToChannelLastPreprocess(), input_data=input_data, |
| 39 | + tflite_output_preprocess=ToChannelFirstPreprocess(), tfl_model=tflite_flatbuffers_model) |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize("input_shape, dim", [ |
| 43 | + pytest.param((1, 32), 0, id="Dim 0."), |
| 44 | + pytest.param((1, 32), 1, id="Dim 1."), |
| 45 | +]) |
| 46 | +@pytest.mark.parametrize("keeepdim", [ |
| 47 | + pytest.param(False, id="Don't keep dim."), |
| 48 | + pytest.param(True, id="Keep dim."), |
| 49 | +]) |
| 50 | +def test_mean_dim_linear_unsupported_quant_conversion(mocker, input_shape, dim, keeepdim): |
| 51 | + model = MeanDimLinearModule(dim, keeepdim) |
| 52 | + |
| 53 | + converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 54 | + |
| 55 | + # Run conversion |
| 56 | + edge_program = to_quantized_edge_program(model, input_shape).exported_program() |
| 57 | + nodes = list(edge_program.graph.nodes) |
| 58 | + |
| 59 | + # Last 2 dimensions are not used or keepdim is False, cannot be converted to MeanDim, node is not delegated |
| 60 | + assert nodes[6].target.__name__ == 'aten.mean.dim' |
| 61 | + |
| 62 | + # Capture generated model |
| 63 | + tflite_flatbuffers_model, io_formats = converter_spy.spy_return |
| 64 | + |
| 65 | + # Capture converted program |
| 66 | + exported_program: ExportedProgram = converter_spy.call_args.args[1] |
| 67 | + |
| 68 | + input_data = (np.random.random(input_shape).astype(np.float32) * 50).astype(np.int8) |
| 69 | + |
| 70 | + convert_run_compare(exported_program, tfl_model=tflite_flatbuffers_model, input_data=input_data) |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.parametrize("input_shape, dim", [ |
| 74 | + pytest.param((1, 4, 8, 8), 0, id="Dim 0."), |
| 75 | + pytest.param((1, 4, 8, 8), 2, id="Dim 2."), |
| 76 | + pytest.param((1, 4, 8, 8), -1, id="Dim -1."), |
| 77 | + pytest.param((1, 4, 8, 8), -2, id="Dim -2."), |
| 78 | + pytest.param((1, 4, 8, 8), (0, 1), id="Dim 0, 1."), |
| 79 | + pytest.param((1, 4, 8, 8), (1, 3), id="Dim 1, 3."), |
| 80 | + pytest.param((1, 4, 8, 8), (-1, -3), id="Dim -1, -3."), |
| 81 | +]) |
| 82 | +@pytest.mark.parametrize("keeepdim", [ |
| 83 | + pytest.param(False, id="Don't keep dim."), |
| 84 | + pytest.param(True, id="Keep dim."), |
| 85 | +]) |
| 86 | +def test_mean_dim_conv_unsupported_quant_conversion(mocker, input_shape, dim, keeepdim): |
| 87 | + model = MeanDimConvModule(dim, keeepdim) |
| 88 | + |
| 89 | + converter_spy = mocker.spy(EdgeProgramToIRConverter, "convert_program") |
| 90 | + |
| 91 | + # Run conversion |
| 92 | + edge_program = to_quantized_edge_program(model, input_shape).exported_program() |
| 93 | + nodes = list(edge_program.graph.nodes) |
| 94 | + |
| 95 | + # Last 2 dimensions are not used or keepdim is False, cannot be converted to MeanDim, node is not delegated |
| 96 | + assert nodes[6].target.__name__ == 'aten.mean.dim' |
| 97 | + |
| 98 | + # Capture generated model |
| 99 | + tflite_flatbuffers_model, io_formats = converter_spy.spy_return |
| 100 | + |
| 101 | + # Capture converted program |
| 102 | + exported_program: ExportedProgram = converter_spy.call_args.args[1] |
| 103 | + |
| 104 | + input_data = (np.random.random(input_shape).astype(np.float32) * 50).astype(np.int8) |
| 105 | + |
| 106 | + convert_run_compare(exported_program, tflite_input_preprocess=ToChannelLastPreprocess(), input_data=input_data, |
| 107 | + tflite_output_preprocess=ToChannelFirstPreprocess(), tfl_model=tflite_flatbuffers_model) |
0 commit comments