|
5 | 5 |
|
6 | 6 | # pyre-unsafe |
7 | 7 |
|
8 | | -from typing import cast, List |
| 8 | +from typing import Any, cast, List |
9 | 9 |
|
10 | 10 | import executorch.backends.arm.tosa_quant_utils as tosa_quant_utils |
11 | 11 | import torch |
12 | | -import tosa_tools.v0_80.serializer.tosa_serializer as ts # type: ignore |
13 | | - |
14 | | -import tosa_tools.v0_80.tosa.Op as TosaOp # type: ignore |
15 | 12 | from executorch.backends.arm.operators.node_visitor import ( |
16 | 13 | NodeVisitor, |
17 | 14 | register_node_visitor, |
18 | 15 | ) |
19 | 16 | from executorch.backends.arm.tosa_mapping import map_dtype, TosaArg |
| 17 | +from executorch.backends.arm.tosa_quant_utils import create_const_ops_for_rescale |
| 18 | + |
| 19 | +from executorch.backends.arm.tosa_specification import TosaSpecification |
20 | 20 | from torch.fx import Node |
21 | 21 |
|
22 | 22 |
|
23 | 23 | @register_node_visitor |
24 | | -class RescaleVisitor(NodeVisitor): |
| 24 | +class RescaleVisitor_0_80(NodeVisitor): |
25 | 25 | target = "_rescale.default" |
26 | 26 |
|
| 27 | + tosa_specs = NodeVisitor.tosa_specs_0_80 |
| 28 | + |
27 | 29 | def define_node( |
28 | 30 | self, |
29 | 31 | node: Node, |
30 | | - tosa_graph: ts.TosaSerializer, |
| 32 | + tosa_graph: Any, |
31 | 33 | inputs: List[TosaArg], |
32 | 34 | output: TosaArg, |
33 | 35 | ) -> None: |
| 36 | + import tosa_tools.v0_80.serializer.tosa_serializer as ts # type: ignore |
34 | 37 |
|
35 | 38 | input_dtype = inputs[0].dtype |
36 | 39 | output_dtype = cast(torch.dtype, node.args[1]) |
@@ -68,5 +71,73 @@ def define_node( |
68 | 71 | ) |
69 | 72 |
|
70 | 73 | tosa_graph.addOperator( |
71 | | - TosaOp.Op().RESCALE, [inputs[0].name], [output.name], attr_rescale |
| 74 | + ts.TosaOp.Op().RESCALE, [inputs[0].name], [output.name], attr_rescale |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +@register_node_visitor |
| 79 | +class RescaleVisitor_INT(NodeVisitor): |
| 80 | + target = "_rescale.default" |
| 81 | + |
| 82 | + tosa_specs = [TosaSpecification.create_from_string("TOSA-1.0+INT")] |
| 83 | + |
| 84 | + def define_node( |
| 85 | + self, |
| 86 | + node: Node, |
| 87 | + tosa_graph: Any, |
| 88 | + inputs: List[TosaArg], |
| 89 | + output: TosaArg, |
| 90 | + ) -> None: |
| 91 | + import serializer.tosa_serializer as ts # type: ignore |
| 92 | + from tosa.RoundingMode import RoundingMode # type: ignore |
| 93 | + |
| 94 | + input_dtype = inputs[0].dtype |
| 95 | + output_dtype = cast(torch.dtype, node.args[1]) |
| 96 | + scale = cast(float, node.args[2]) |
| 97 | + input_zp = cast(int, node.args[3]) |
| 98 | + output_zp = cast(int, node.args[4]) |
| 99 | + |
| 100 | + if input_dtype != map_dtype(torch.int8) and input_zp != 0: |
| 101 | + raise ValueError( |
| 102 | + f"If input dtype is not int8, input_zp must be 0. Got input_dtype{ts.DTypeNames[input_dtype]}, {input_zp=}" |
| 103 | + ) |
| 104 | + if output_dtype != torch.int8 and output_zp != 0: |
| 105 | + raise ValueError( |
| 106 | + f"If output dtype is not int8, output_zp must be 0. Got {output_dtype=}, {output_zp=}" |
| 107 | + ) |
| 108 | + |
| 109 | + # scale32 gives higher accuracy but for a higher HW cost. |
| 110 | + # For now, always go for scale32. |
| 111 | + scale_32 = True |
| 112 | + scale_width = 32 if scale_32 else 16 |
| 113 | + multipliers, shifts = tosa_quant_utils.compute_multiplier_and_shift( |
| 114 | + [scale], scale_width |
| 115 | + ) |
| 116 | + |
| 117 | + rescale_inputs = create_const_ops_for_rescale( |
| 118 | + tosa_graph, |
| 119 | + input_dtype, |
| 120 | + inputs[0].name, |
| 121 | + multipliers, |
| 122 | + shifts, |
| 123 | + input_zp, |
| 124 | + output_zp, |
| 125 | + ts, |
| 126 | + ) |
| 127 | + |
| 128 | + attr_rescale = ts.TosaSerializerAttribute() |
| 129 | + |
| 130 | + attr_rescale.RescaleAttribute( |
| 131 | + scale32=scale_32, |
| 132 | + rounding_mode=RoundingMode.SINGLE_ROUND, |
| 133 | + per_channel=False, |
| 134 | + input_unsigned=False, |
| 135 | + output_unsigned=False, |
| 136 | + ) |
| 137 | + |
| 138 | + tosa_graph.addOperator( |
| 139 | + ts.TosaOp.Op().RESCALE, |
| 140 | + [inputs[0].name, *rescale_inputs], |
| 141 | + [output.name], |
| 142 | + attr_rescale, |
72 | 143 | ) |
0 commit comments