-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathop_maximum.py
75 lines (61 loc) · 2.27 KB
/
op_maximum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Copyright 2024-2025 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
# pyre-unsafe
from typing import List
import executorch.backends.arm.tosa_quant_utils as tqutils
import serializer.tosa_serializer as ts # type: ignore
from executorch.backends.arm._passes.fold_qdq_with_annotated_qparams_pass import (
get_input_qparams,
)
from executorch.backends.arm.operators.node_visitor import (
NodeVisitor,
register_node_visitor,
)
from executorch.backends.arm.tosa_mapping import TosaArg
from executorch.backends.arm.tosa_utils import tosa_shape
from serializer.tosa_serializer import TosaOp
from torch.fx import Node
@register_node_visitor
class MaxVisitor(NodeVisitor):
target = "aten.maximum.default"
def __init__(self, *args):
super().__init__(*args)
def define_node(
self,
node: Node,
tosa_graph: ts.TosaSerializer,
inputs: List[TosaArg],
output: TosaArg,
) -> None:
assert inputs[0].dtype == inputs[1].dtype
scale_back = 1.0
max_output = output
if inputs[0].dtype == ts.DType.INT8:
input_qparams = get_input_qparams(node)
assert (
len(input_qparams) == 2
), f"Both inputs needs to have quantization information for {node}"
# insert RESCALEs to int32
assert (
input_qparams[0] == input_qparams[1]
), "Both inputs must have same quantization for MAX"
operand_inputs, scale_back = tqutils.insert_rescale_ops_to_int32(
tosa_graph, inputs, node
)
output.shape = tosa_shape(output.shape, output.dim_order)
max_output = tosa_graph.addIntermediate(output.shape, ts.DType.INT32)
else:
operand_inputs = inputs
tosa_graph.addOperator(
TosaOp.Op().MAXIMUM,
[
operand_inputs[0].name,
operand_inputs[1].name,
],
[max_output.name],
)
if output.dtype == ts.DType.INT8:
# insert RESCALE from int32 back to int8
tqutils.insert_rescale_op_to_int8(tosa_graph, max_output, scale_back, node)