@@ -150,71 +150,67 @@ def _create_const_ops_for_rescale(
150150 return [multipliers .name , shifts .name , input_zp .name , output_zp .name ]
151151
152152
153- def _build_rescale (
154- tosa_fb : Any ,
155- scale : list [float ],
156- input_node : Any ,
157- output_name : str ,
158- output_type : Any ,
159- input_zp : list [int ],
160- output_zp : list [int ],
161- rounding_mode : ts .RoundingMode ,
162- per_channel : bool = False ,
163- is_scale32 : bool = True ,
164- input_unsigned : bool = False ,
165- output_unsigned : bool = False ,
166- ):
167- """Insert a TOSA RESCALE operator configured for the quantized path.
153+ @register_node_visitor
154+ class RescaleVisitor (NodeVisitor ):
155+ target = "tosa.RESCALE.default"
168156
169- Args:
170- tosa_fb (Any): Graph builder receiving the RESCALE operator.
171- scale (list[float]): Scale factors applied during rescaling.
172- input_node (Any): Input tensor node feeding the operator.
173- output_name (str): Name assigned to the RESCALE output tensor.
174- output_type (ts.DType): Data type of the output tensor.
175- input_zp (list[int]): Quantization zero points for the input tensor.
176- output_zp (list[int]): Quantization zero points for the output tensor.
177- rounding_mode (ts.RoundingMode): Rounding policy for the RESCALE op.
178- per_channel (bool): Whether scales are applied per output channel.
179- is_scale32 (bool): Declared scale width; ignored when the input type is
180- ``ts.DType.INT48``.
157+ def _build_rescale (
158+ self ,
159+ node : Node ,
160+ tosa_graph : Any ,
161+ scale : list [float ],
162+ input_node : TosaArg ,
163+ output : TosaArg ,
164+ input_zp : list [int ],
165+ output_zp : list [int ],
166+ rounding_mode : ts .RoundingMode ,
167+ per_channel : bool = False ,
168+ input_unsigned : bool = False ,
169+ output_unsigned : bool = False ,
170+ ) -> None :
171+ """Insert a TOSA RESCALE operator configured for the quantized path.
181172
182- """
183- scaleWidth = 16 if input_node .dtype == ts .DType .INT48 else 32
184- is_scale32 = False if input_node .dtype == ts .DType .INT48 else True
185- multipliers , shifts = _compute_multiplier_and_shift (scale , scaleWidth )
186- rescale_inputs = _create_const_ops_for_rescale (
187- tosa_fb ,
188- is_scale32 ,
189- input_node .dtype ,
190- output_name ,
191- multipliers ,
192- shifts ,
193- input_zp ,
194- output_zp ,
195- output_type ,
196- ts ,
197- )
198- attr_rescale = ts .TosaSerializerAttribute ()
199- attr_rescale .RescaleAttribute (
200- scale32 = is_scale32 ,
201- rounding_mode = rounding_mode ,
202- per_channel = per_channel ,
203- input_unsigned = input_unsigned ,
204- output_unsigned = output_unsigned ,
205- )
173+ RESCALE is serialized through NodeVisitor._serialize_operator so that
174+ debug-location metadata is attached consistently with other TOSA ops.
175+ The scale width is derived from the input dtype: INT48 uses 16-bit
176+ multipliers, otherwise 32-bit multipliers are used.
206177
207- tosa_fb .addOperator (
208- ts .Op .RESCALE ,
209- [input_node .name , * rescale_inputs ],
210- [output_name ],
211- attr_rescale ,
212- )
178+ """
179+ scale_width = 16 if input_node .dtype == ts .DType .INT48 else 32
180+ is_scale32 = input_node .dtype != ts .DType .INT48
213181
182+ multipliers , shifts = _compute_multiplier_and_shift (scale , scale_width )
214183
215- @register_node_visitor
216- class RescaleVisitor (NodeVisitor ):
217- target = "tosa.RESCALE.default"
184+ rescale_inputs = _create_const_ops_for_rescale (
185+ tosa_graph ,
186+ is_scale32 ,
187+ input_node .dtype ,
188+ output .name ,
189+ multipliers ,
190+ shifts ,
191+ input_zp ,
192+ output_zp ,
193+ output .dtype ,
194+ ts ,
195+ )
196+
197+ attr_rescale = ts .TosaSerializerAttribute ()
198+ attr_rescale .RescaleAttribute (
199+ scale32 = is_scale32 ,
200+ rounding_mode = rounding_mode ,
201+ per_channel = per_channel ,
202+ input_unsigned = input_unsigned ,
203+ output_unsigned = output_unsigned ,
204+ )
205+
206+ self ._serialize_operator (
207+ node ,
208+ tosa_graph ,
209+ ts .Op .RESCALE ,
210+ [input_node .name , * rescale_inputs ],
211+ [output .name ],
212+ attr_rescale ,
213+ )
218214
219215 def define_node (
220216 self ,
@@ -254,12 +250,12 @@ def define_node(
254250 raise ValueError (
255251 f"If output dtype is not int8 or int16, output_zp must be 0. Got { ts .DTypeNames [output_dtype ]} , { output_zp = } "
256252 )
257- _build_rescale (
258- tosa_graph ,
253+ self ._build_rescale (
254+ node = node ,
255+ tosa_graph = tosa_graph ,
259256 scale = scales ,
260257 input_node = inputs [0 ],
261- output_name = output .name ,
262- output_type = output .dtype ,
258+ output = output ,
263259 input_zp = [input_zp ],
264260 output_zp = [output_zp ],
265261 rounding_mode = ts .RoundingMode .SINGLE_ROUND ,
0 commit comments