|
| 1 | +from typing import Sequence |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +import torch |
| 5 | +from torch import nn |
| 6 | + |
| 7 | +from executorch.backends.nxp.backend.ir.converter.builder.model_builder import ModelBuilder |
| 8 | +from executorch.backends.nxp.backend.ir.tflite_generator.builtin_options.conv_2d_options import Conv2D |
| 9 | +from executorch.backends.nxp.backend.ir.tflite_generator.builtin_options.reshape_options import Reshape |
| 10 | +from executorch.backends.nxp.backend.ir.tflite_generator.builtin_options.transpose_options import Transpose |
| 11 | +from executorch.backends.nxp.tests.executorch_pipeline import to_edge_program |
| 12 | +from executorch.backends.nxp.tests.executors import convert_run_compare, ToNHWCPreprocess, ToNCHWPreprocess |
| 13 | + |
| 14 | + |
| 15 | +class FormatlessToChannelsFirstModule(nn.Module): |
| 16 | + def __init__(self, channels: int, new_shape: Sequence[int]): |
| 17 | + super().__init__() |
| 18 | + self.conv = nn.Conv2d(channels, channels, 2, bias=True) |
| 19 | + self.new_shape = new_shape |
| 20 | + |
| 21 | + def forward(self, x): |
| 22 | + x = torch.reshape(x, self.new_shape) |
| 23 | + x = self.conv(x) |
| 24 | + return x |
| 25 | + |
| 26 | + |
| 27 | +class FormatlessToFormatlessModule(nn.Module): |
| 28 | + def __init__(self, new_shape: Sequence[int]): |
| 29 | + super().__init__() |
| 30 | + self.new_shape = new_shape |
| 31 | + |
| 32 | + def forward(self, x): |
| 33 | + x = torch.reshape(x, self.new_shape) |
| 34 | + return x |
| 35 | + |
| 36 | + |
| 37 | +class ConvReshapeModule(nn.Module): |
| 38 | + def __init__(self, channels: int, new_shape: Sequence[int]): |
| 39 | + super().__init__() |
| 40 | + self.conv = nn.Conv2d(channels, channels, 2, bias=True) |
| 41 | + self.new_shape = new_shape |
| 42 | + |
| 43 | + def forward(self, x): |
| 44 | + x = self.conv(x) |
| 45 | + x = torch.reshape(x, self.new_shape) |
| 46 | + return x |
| 47 | + |
| 48 | + |
| 49 | +def test__channels_first_to_2d(mocker): |
| 50 | + input_shape = [2, 4, 7, 9] |
| 51 | + new_shape = [12, 32] # Mix up the dimensions for a thorough test. |
| 52 | + |
| 53 | + torch_model = ConvReshapeModule(channels=input_shape[1], new_shape=new_shape) |
| 54 | + edge_program = to_edge_program(torch_model, input_shape).exported_program() |
| 55 | + |
| 56 | + torch.manual_seed(23) |
| 57 | + input_data = np.random.random(input_shape).astype('float32') |
| 58 | + |
| 59 | + converter_spy = mocker.spy(ModelBuilder, "finish") |
| 60 | + |
| 61 | + convert_run_compare(edge_program, input_data, tflite_input_preprocess=ToNHWCPreprocess()) |
| 62 | + |
| 63 | + tflite_model = converter_spy.spy_return |
| 64 | + ops = tflite_model.sub_graphs[0].operators.vector |
| 65 | + assert len(ops) == 3 |
| 66 | + assert isinstance(ops[0].builtin_options, Conv2D) |
| 67 | + assert isinstance(ops[1].builtin_options, Transpose) |
| 68 | + assert isinstance(ops[2].builtin_options, Reshape) |
| 69 | + |
| 70 | + |
| 71 | +def test__channels_first_to_4d(mocker): |
| 72 | + input_shape = [2, 4, 6, 8] |
| 73 | + new_shape = [7, 4, 2, 5] |
| 74 | + |
| 75 | + torch_model = ConvReshapeModule(channels=input_shape[1], new_shape=new_shape) |
| 76 | + edge_program = to_edge_program(torch_model, input_shape).exported_program() |
| 77 | + |
| 78 | + torch.manual_seed(23) |
| 79 | + input_data = np.random.random(input_shape).astype('float32') |
| 80 | + |
| 81 | + converter_spy = mocker.spy(ModelBuilder, "finish") |
| 82 | + |
| 83 | + convert_run_compare(edge_program, input_data, tflite_input_preprocess=ToNHWCPreprocess()) |
| 84 | + |
| 85 | + tflite_model = converter_spy.spy_return |
| 86 | + ops = tflite_model.sub_graphs[0].operators.vector |
| 87 | + assert len(ops) == 3 |
| 88 | + assert isinstance(ops[0].builtin_options, Conv2D) |
| 89 | + assert isinstance(ops[1].builtin_options, Transpose) |
| 90 | + assert isinstance(ops[2].builtin_options, Reshape) |
| 91 | + |
| 92 | + |
| 93 | +def test__formatless_to_channels_first(mocker): |
| 94 | + input_shape = [12, 32] |
| 95 | + new_shape = [2, 4, 6, 8] # Mix up the dimensions for a thorough test. |
| 96 | + |
| 97 | + torch_model = FormatlessToChannelsFirstModule(channels=new_shape[1], new_shape=new_shape) |
| 98 | + edge_program = to_edge_program(torch_model, input_shape).exported_program() |
| 99 | + |
| 100 | + torch.manual_seed(23) |
| 101 | + input_data = np.random.random(input_shape).astype('float32') |
| 102 | + |
| 103 | + converter_spy = mocker.spy(ModelBuilder, "finish") |
| 104 | + |
| 105 | + convert_run_compare(edge_program, input_data, tflite_output_preprocess=ToNCHWPreprocess()) |
| 106 | + |
| 107 | + tflite_model = converter_spy.spy_return |
| 108 | + ops = tflite_model.sub_graphs[0].operators.vector |
| 109 | + assert len(ops) == 3 |
| 110 | + assert isinstance(ops[0].builtin_options, Reshape) |
| 111 | + assert isinstance(ops[1].builtin_options, Transpose) |
| 112 | + assert isinstance(ops[2].builtin_options, Conv2D) |
| 113 | + |
| 114 | + |
| 115 | +def test__formatless_to_formatless(mocker): |
| 116 | + input_shape = [12, 32] |
| 117 | + new_shape = [2, 4, 6, 8] |
| 118 | + |
| 119 | + torch_model = FormatlessToFormatlessModule(new_shape=new_shape) |
| 120 | + edge_program = to_edge_program(torch_model, input_shape).exported_program() |
| 121 | + |
| 122 | + torch.manual_seed(23) |
| 123 | + input_data = np.random.random(input_shape).astype('float32') |
| 124 | + |
| 125 | + converter_spy = mocker.spy(ModelBuilder, "finish") |
| 126 | + |
| 127 | + convert_run_compare(edge_program, input_data) |
| 128 | + |
| 129 | + tflite_model = converter_spy.spy_return |
| 130 | + ops = tflite_model.sub_graphs[0].operators.vector |
| 131 | + assert len(ops) == 1 # No extra Transpose ops. |
| 132 | + assert isinstance(ops[0].builtin_options, Reshape) |
0 commit comments