|
| 1 | +# Copyright (c) 2025 Samsung Electronics Co. LTD |
| 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 | +from typing import cast, Dict, List |
| 7 | + |
| 8 | +import torch |
| 9 | +from executorch.backends.samsung.builders.node_visitor import ( |
| 10 | + NodeVisitor, |
| 11 | + register_node_visitor, |
| 12 | +) |
| 13 | +from executorch.backends.samsung.serialization.enn_graph_schema import EnnGraph |
| 14 | + |
| 15 | + |
| 16 | +@register_node_visitor |
| 17 | +class AvgPool2dVisitor(NodeVisitor): |
| 18 | + target = "aten.avg_pool2d.default" |
| 19 | + |
| 20 | + def __init__(self, *args) -> None: |
| 21 | + super().__init__(*args) |
| 22 | + |
| 23 | + def define_node( |
| 24 | + self, |
| 25 | + node: torch.fx.Node, |
| 26 | + enn_graph: EnnGraph, |
| 27 | + vals_to_ids: Dict[torch.Tensor, int], |
| 28 | + ) -> None: |
| 29 | + input = node.args[0] |
| 30 | + input_id = self.define_tensor(input, enn_graph, vals_to_ids) |
| 31 | + |
| 32 | + kernel_size = cast(List[int], node.args[1]) |
| 33 | + if len(kernel_size) == 1: |
| 34 | + kernel_size = kernel_size * 2 |
| 35 | + |
| 36 | + stride = cast(List[int], node.args[2]) if len(node.args) > 2 else kernel_size |
| 37 | + if len(stride) == 1: |
| 38 | + stride = stride * 2 |
| 39 | + |
| 40 | + padding = cast(List[int], node.args[3]) if len(node.args) > 3 else [0, 0] |
| 41 | + if len(padding) == 1: |
| 42 | + padding = padding * 2 |
| 43 | + explicit_padding = [padding[0], padding[1], padding[0], padding[1]] |
| 44 | + |
| 45 | + params = {} |
| 46 | + params["kernel_h"] = kernel_size[0] |
| 47 | + params["kernel_w"] = kernel_size[1] |
| 48 | + params["stride_h"] = stride[0] |
| 49 | + params["stride_w"] = stride[1] |
| 50 | + params["padding"] = "EXPLICIT" |
| 51 | + params["explicit_padding"] = explicit_padding |
| 52 | + |
| 53 | + if len(node.args) > 4: |
| 54 | + ceil_mode = cast(bool, node.args[4]) |
| 55 | + assert not ceil_mode, "Not support ceil_mode = True." |
| 56 | + |
| 57 | + if len(node.args) > 5: |
| 58 | + params["count_include_pad"] = cast(bool, node.args[5]) |
| 59 | + else: |
| 60 | + params["count_include_pad"] = True |
| 61 | + |
| 62 | + if len(node.args) > 6: |
| 63 | + divisor_override = cast(int, node.args[6]) |
| 64 | + assert divisor_override == kernel_size[0] * kernel_size[1], "Not supported divisor_override which is not equal to pooling region." |
| 65 | + |
| 66 | + output_id = self.define_tensor(node, enn_graph, vals_to_ids) |
| 67 | + |
| 68 | + enn_graph.define_op(node.name, "AVGPOOL2D", [input_id], [output_id], params) |
0 commit comments