|
| 1 | +# Copyright (c) Qualcomm Innovation Center, Inc. |
| 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 | +import warnings |
| 7 | +from typing import cast, List |
| 8 | + |
| 9 | +import torch |
| 10 | +import torch.nn as nn |
| 11 | +from executorch.exir import to_edge |
| 12 | +from executorch.exir.pass_base import ExportPass, PassResult |
| 13 | + |
| 14 | +from .utils import merge_decomposed_graph |
| 15 | + |
| 16 | + |
| 17 | +class ModelMaxPool3D(torch.nn.Module): |
| 18 | + def __init__( |
| 19 | + self, filter_size, stride, padding, dilation, return_indices, ceil_mode |
| 20 | + ): |
| 21 | + super().__init__() |
| 22 | + |
| 23 | + self.pool2d_hw = nn.MaxPool2d( |
| 24 | + kernel_size=[1, filter_size[2]], # (H, W) part |
| 25 | + stride=[1, stride[2]], |
| 26 | + padding=[0, padding[2]], |
| 27 | + return_indices=return_indices, |
| 28 | + ceil_mode=ceil_mode, |
| 29 | + ) |
| 30 | + self.pool2d_dh = nn.MaxPool2d( |
| 31 | + kernel_size=filter_size[:2], # (D, H) part |
| 32 | + stride=stride[:2], |
| 33 | + padding=padding[:2], |
| 34 | + return_indices=return_indices, |
| 35 | + ceil_mode=ceil_mode, |
| 36 | + ) |
| 37 | + |
| 38 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 39 | + N, C, D, H, W = x.shape |
| 40 | + x_ = x.permute(0, 1, 4, 2, 3) |
| 41 | + x1_1d = x_.reshape(N * C, W, D, H) |
| 42 | + # first pool over (D, H) |
| 43 | + out_pool1d_0 = self.pool2d_dh(x1_1d) |
| 44 | + D_out = out_pool1d_0.shape[2] |
| 45 | + # NC, W, D, H-> NC, D, H, W |
| 46 | + x1b = out_pool1d_0.permute(0, 2, 3, 1) |
| 47 | + # second pool over (H, W) |
| 48 | + out4d = self.pool2d_hw(x1b) |
| 49 | + H_out2 = out4d.shape[2] |
| 50 | + W_out = out4d.shape[3] |
| 51 | + out = out4d.reshape(N, C, D_out, H_out2, W_out) |
| 52 | + return out |
| 53 | + |
| 54 | + |
| 55 | +class DecomposeMaxPool3d(ExportPass): |
| 56 | + # The max_pool3d is not supported yet by QNN. |
| 57 | + # Decompose: input -> permute -> reshape -> max_pool2d -> permute -> max_pool2d -> reshape -> output |
| 58 | + |
| 59 | + def __init__(self, quantization_capture=False) -> None: |
| 60 | + super().__init__() |
| 61 | + self.quantization_capture = quantization_capture |
| 62 | + |
| 63 | + def call(self, graph_module: torch.fx.GraphModule) -> PassResult: |
| 64 | + graph = graph_module.graph |
| 65 | + for node in graph.nodes: |
| 66 | + if node.op == "call_function" and "max_pool3d" in str(node.target): |
| 67 | + # kernel info |
| 68 | + filter_size = cast(List[int], node.args[1]) |
| 69 | + if len(filter_size) == 1: |
| 70 | + filter_size *= 3 |
| 71 | + |
| 72 | + num_args = len(node.args) |
| 73 | + |
| 74 | + # stride info |
| 75 | + stride = filter_size |
| 76 | + if num_args > 2: |
| 77 | + stride = cast(List[int], node.args[2]) |
| 78 | + if len(stride) == 1: |
| 79 | + stride *= 3 |
| 80 | + |
| 81 | + # padding info |
| 82 | + padding = [0, 0, 0] |
| 83 | + if num_args > 3: |
| 84 | + padding = cast(List[int], node.args[3]) |
| 85 | + if len(padding) == 1: |
| 86 | + padding *= 3 |
| 87 | + |
| 88 | + # dilation info |
| 89 | + dilation = [1, 1, 1] |
| 90 | + if num_args > 4: |
| 91 | + dilation = cast(List[int], node.args[4]) |
| 92 | + if len(padding) == 1: |
| 93 | + dilation *= 3 |
| 94 | + |
| 95 | + ceil_mode = node.args[5] if num_args > 5 else False |
| 96 | + return_indices = node.args[6] if num_args > 6 else False |
| 97 | + if return_indices: |
| 98 | + warnings.warn( |
| 99 | + "[QNN Delegate Op Builder]: The case return_indices=True is not be support, fallback", |
| 100 | + stacklevel=1, |
| 101 | + ) |
| 102 | + return |
| 103 | + |
| 104 | + model = ModelMaxPool3D( |
| 105 | + filter_size, stride, padding, dilation, return_indices, ceil_mode |
| 106 | + ) |
| 107 | + if self.quantization_capture: |
| 108 | + decomposed_module = torch.export.export( |
| 109 | + model, (node.args[0].meta["val"],), strict=True |
| 110 | + ).module() |
| 111 | + else: |
| 112 | + edge_mgr = to_edge( |
| 113 | + torch.export.export( |
| 114 | + model, (node.args[0].meta["val"],), strict=True |
| 115 | + ) |
| 116 | + ) |
| 117 | + decomposed_module = edge_mgr.exported_program() |
| 118 | + |
| 119 | + with graph.inserting_before(node): |
| 120 | + # remap is used to map original node values to new node values, |
| 121 | + # which ensures that reference to nodes are correctly updated in the new graph |
| 122 | + remap = {"x": node.args[0]} |
| 123 | + merge_decomposed_graph( |
| 124 | + remap=remap, |
| 125 | + target_node=node, |
| 126 | + target_graph=graph, |
| 127 | + decomposed_graph_module=decomposed_module, |
| 128 | + ) |
| 129 | + graph.erase_node(node) |
| 130 | + |
| 131 | + graph.eliminate_dead_code() |
| 132 | + graph_module.recompile() |
| 133 | + return PassResult(graph_module, True) |
0 commit comments