-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathop_abs.py
133 lines (111 loc) · 4.16 KB
/
op_abs.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
# Copyright 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 AbsVisitor_080_BI(NodeVisitor):
target = "aten.abs.default"
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 not (inputs[0].dtype == output.dtype):
raise ValueError(
"All inputs and outputs need same dtype."
f"Got {inputs[0].dtype=}, {output.dtype=}"
)
# Handle int8 (quantized) and int32
if not (inputs[0].dtype in [ts.DType.INT8, ts.DType.INT32]):
raise ValueError(
"All inputs need to be INT8 or INT32." f"Got {inputs[0].dtype=}"
)
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.abs
rescaled_inputs = inputs
if output.dtype == ts.DType.INT8:
broadcasted_shape = tutils.tosa_shape(output.shape, output.dim_order)
abs_output = tosa_graph.addIntermediate(broadcasted_shape, ts.DType.INT32)
else:
# output.dtype == ts.DType.INT32
abs_output = output
# Do the INT32 Abs
tosa_graph.addOperator(
TosaOp.Op().ABS,
[
rescaled_inputs[0].name,
],
[abs_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, abs_output, scale_back, node) # type: ignore[possibly-undefined]
@register_node_visitor
class AbsVisitor_080_MI(AbsVisitor_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 not (inputs[0].dtype == output.dtype):
raise ValueError(
"All inputs and output need same dtype."
f"Got {inputs[0].dtype=}, {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 Abs lowering
if not (inputs[0].dtype == ts.DType.FP32):
raise ValueError(
"All inputs need to be FP32." f"Got {inputs[0].dtype=}"
)
if not (output.dtype == ts.DType.FP32):
raise ValueError("All outputs need to be FP32." f"Got {output.dtype=}")
# MI lowering
tosa_graph.addOperator(
TosaOp.Op().ABS,
[inputs[0].name],
[output.name],
None,
)