-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathop_add.py
142 lines (119 loc) · 4.59 KB
/
op_add.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Copyright 2023-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 executorch.backends.arm.tosa_utils as tutils
import serializer.tosa_serializer as ts # type: ignore
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_specification import TosaSpecification
from serializer.tosa_serializer import TosaOp
from torch.fx import Node
@register_node_visitor
class AddVisitor_080_BI(NodeVisitor):
target = "aten.add.Tensor"
tosa_specs = [
TosaSpecification.create_from_string("TOSA-0.80+BI"),
]
def __init__(self, *args):
super().__init__(*args)
def define_node(
self,
node: Node,
tosa_graph: ts.TosaSerializer,
inputs: List[TosaArg],
output: TosaArg,
) -> None:
# Specification (0.80) states that input and output types
# should all be the same
if inputs[0].dtype != inputs[1].dtype or inputs[0].dtype != output.dtype:
raise TypeError(
f"All IO needs to have the same data type, got input 1: "
f"{inputs[0].dtype}, input 2: {inputs[1].dtype} and output: "
f"{output.dtype}"
)
# Handle int8 (quantized) and int32
supported_dtypes = [ts.DType.INT8, ts.DType.INT32]
if inputs[0].dtype not in supported_dtypes:
raise TypeError(
f'IO data type needs to be {supported_dtypes}, got "{inputs[0].dtype}"'
)
dim_order = (
inputs[0].dim_order
if len(inputs[0].shape) > len(inputs[1].shape)
else inputs[1].dim_order
)
if inputs[0].dtype == ts.DType.INT8:
rescaled_inputs, scale_back = tqutils.insert_rescale_ops_to_int32(
tosa_graph, inputs, node
)
else:
# input[0].dtype == ts.DType.INT32
# Non quantized input, natively support by TOSA.ADD
rescaled_inputs = inputs
if output.dtype == ts.DType.INT8:
broadcasted_shape = tutils.tosa_shape(output.shape, output.dim_order)
add_output = tosa_graph.addIntermediate(broadcasted_shape, ts.DType.INT32)
else:
# output.dtype == ts.DType.INT32
add_output = output
input1, input2 = tutils.reshape_for_broadcast(
tosa_graph, rescaled_inputs, dim_order
)
# Do the INT32 Add
tosa_graph.addOperator(
TosaOp.Op().ADD,
[input1.name, input2.name],
[add_output.name],
None,
)
if output.dtype == ts.DType.INT8:
# Scale output back to 8 bit
# pyre-ignore
tqutils.insert_rescale_op_to_int8(tosa_graph, add_output, scale_back, node) # type: ignore[possibly-undefined]
@register_node_visitor
class AddVisitor_080_MI(AddVisitor_080_BI):
# inheriting 'target' from BI class
tosa_specs = [
TosaSpecification.create_from_string("TOSA-0.80+MI"),
]
def __init__(self, *args):
super().__init__(*args)
def define_node(
self,
node: Node,
tosa_graph: ts.TosaSerializer,
inputs: List[TosaArg],
output: TosaArg,
) -> None:
# Specification (0.80) states that input and output types
# should all be the same
if inputs[0].dtype != inputs[1].dtype or inputs[0].dtype != output.dtype:
raise TypeError(
f"All IO needs to have the same data type, got input 1: "
f"{inputs[0].dtype}, input 2: {inputs[1].dtype} and output: "
f"{output.dtype}"
)
if inputs[0].dtype in [ts.DType.INT8, ts.DType.INT32]:
# Call the inherited define_node for handling integers
super().define_node(node, tosa_graph, inputs, output)
else:
# FP32 Add lowering
if inputs[0].dtype != ts.DType.FP32:
raise TypeError(
f"Expected IO data type to be FP32, got {inputs[0].dtype}"
)
input1, input2 = tutils.reshape_for_broadcast(tosa_graph, inputs)
# MI lowering
tosa_graph.addOperator(
TosaOp.Op().ADD,
[input1.name, input2.name],
[output.name],
None,
)