88 translator
99
1010Module contains functions for context-free conversion of various
11- things from ONNX to TFLite .
11+ things from ExecuTorch to NeutronIR .
1212"""
1313
1414from typing import Any , Collection , List , Optional , Sequence
2323
2424
2525def permute_static_tensor (tensor : tflite_model .Tensor , perm : list [int ]):
26- """Take a static TFLite tensor and permute its shape and data according to the permutation in 'perm'.
26+ """Take a static NeutronIR tensor and permute its shape and data according to the permutation in 'perm'.
2727
28- :param tensor: Static TFLite tensor to permute.
28+ :param tensor: Static NeutronIR tensor to permute.
2929 :param perm: Permutation to apply to the tensor.
3030 """
3131
@@ -48,7 +48,7 @@ def permute_static_tensor(tensor: tflite_model.Tensor, perm: list[int]):
4848def get_tflite_tensor_shape_with_explicit_padding (
4949 tflite_shape : List [int ], explicit_padding : List [List [int ]]
5050) -> List [int ]:
51- """Get the resulting shape of a tensor with shape 'tflite_shape' (in TFLite format), after 'explicit_padding' is
51+ """Get the resulting shape of a tensor with shape 'tflite_shape' (in NeutronIR format), after 'explicit_padding' is
5252 applied to it.
5353 """
5454
@@ -57,7 +57,7 @@ def get_tflite_tensor_shape_with_explicit_padding(
5757 ):
5858 logger .e (
5959 logger .Code .INTERNAL_ERROR ,
60- f"Cannot apply padding '{ explicit_padding } ' to TFLite shape '{ tflite_shape } '!" ,
60+ f"Cannot apply padding '{ explicit_padding } ' to NeutronIR shape '{ tflite_shape } '!" ,
6161 )
6262
6363 total_padding = [
@@ -86,8 +86,8 @@ def get_tflite_tensor_shape_with_explicit_padding(
8686
8787
8888def dims_to_channels_first (channels_last_dimensions : List [int ]) -> List [int ]:
89- """Convert a list of ints which represent dimensions in the channels last (TFLite ) format to the channels first
90- (ONNX ) format.
89+ """Convert a list of ints which represent dimensions in the channels last (NeutronIR ) format to the channels first
90+ (ExecuTorch ) format.
9191 """
9292 assert len (channels_last_dimensions ) > 0 , "Dimensions list is empty!"
9393
@@ -102,8 +102,8 @@ def dims_to_channels_first(channels_last_dimensions: List[int]) -> List[int]:
102102
103103
104104def dims_to_channels_last (channels_first_dimensions : List [int ]) -> List [int ]:
105- """Convert a list of ints which represent dimensions in the channels first (ONNX ) format to the channels last
106- (TFLite ) format.
105+ """Convert a list of ints which represent dimensions in the channels first (ExecuTorch ) format to the channels last
106+ (NeutronIR ) format.
107107 """
108108 assert len (channels_first_dimensions ) > 0 , "Dimensions list is empty!"
109109
@@ -151,7 +151,7 @@ def _same_upper_equals_same_lower(
151151 o_strides : Optional [List [int ]] = None ,
152152 o_dilations : Optional [List [int ]] = None ,
153153) -> bool :
154- """Determine if in a given particular setting, the values of the ONNX `auto_pads` attribute SAME_UPPER and
154+ """Determine if in a given particular setting, the values of the ExecuTorch `auto_pads` attribute SAME_UPPER and
155155 SAME_LOWER represent the exact same padding.
156156 """
157157
@@ -173,7 +173,7 @@ def _tflite_padding_compute_output_size(
173173 """
174174 Calculates the output shape of the tensor with particular setting as tflite would. Implementation corresponds to
175175 tensorflow/lite/kernels/padding.h:ComputeOutSize()
176- :param padding: TFLite Padding value - 'Same' or 'Valid'
176+ :param padding: NeutronIR Padding value - 'Same' or 'Valid'
177177 :param tflite_spatial_input_shape: input tensor shape
178178 :param tflite_kernel_shape: convolution kernel shape
179179 :param strides: strides (default is 1)
@@ -209,7 +209,7 @@ def tflite_compute_padding_with_offset(
209209 dilations : Optional [List [int ]] = None ,
210210) -> (List [int ], List [int ]):
211211 """
212- Calculate padding and offset for each dimension for particular convolution setting as TFLite .
212+ Calculate padding and offset for each dimension for particular convolution setting as NeutronIR .
213213 Implementation corresponds to tensorflow/lite/kernels/padding.h:ComputePaddingWithOffset()
214214 :param tflite_input_shape: tensorflow lite input shape
215215 :param tflite_kernel_shape: tensorflow lite kernel shape
@@ -252,14 +252,14 @@ def _is_same_padding(
252252 o_strides : Optional [List [int ]] = None ,
253253 o_dilations : Optional [List [int ]] = None ,
254254) -> bool :
255- """Determine if given ONNX 'pads' padding can be represented exactly with the TFLite 'SAME' padding type.
256-
257- :param o_pads: ONNX 'pads' attribute.
258- :param tflite_input_shape: The shape of the main input of the operator in TFLite format.
259- :param tflite_output_shape: The shape of the main output of the operator in TFLite format.
260- :param o_kernel_shape: ONNX 'kernel_shape' attribute.
261- :param o_strides: ONNX 'strides' attribute. Can be omitted.
262- :param o_dilations: ONNX 'dilations' attribute. Can be omitted.
255+ """Determine if given ExecuTorch 'pads' padding can be represented exactly with the NeutronIR 'SAME' padding type.
256+
257+ :param o_pads: ExecuTorch 'pads' attribute.
258+ :param tflite_input_shape: The shape of the main input of the operator in NeutronIR format.
259+ :param tflite_output_shape: The shape of the main output of the operator in NeutronIR format.
260+ :param o_kernel_shape: ExecuTorch 'kernel_shape' attribute.
261+ :param o_strides: ExecuTorch 'strides' attribute. Can be omitted.
262+ :param o_dilations: ExecuTorch 'dilations' attribute. Can be omitted.
263263 """
264264
265265 if len (tflite_input_shape ) == 0 or len (tflite_output_shape ) == 0 :
@@ -269,7 +269,7 @@ def _is_same_padding(
269269 f"'{ tflite_input_shape } ' and output shape '{ tflite_output_shape } '." ,
270270 )
271271
272- # Calculate if the output shape corresponds to Same padding setting in TFLite
272+ # Calculate if the output shape corresponds to Same padding setting in NeutronIR
273273 tflite_spatial_input_shape = tflite_input_shape [1 :- 1 ]
274274 tmp_spatial_output_shape = _tflite_padding_compute_output_size (
275275 tflPadding .Padding .SAME ,
@@ -282,10 +282,10 @@ def _is_same_padding(
282282 return False
283283
284284 # For every dimension, the padding is added to the start and end of the dimension.
285- # TFLite padding 'SAME' tries to split it evenly, but in case of odd padding, 'SAME' adds the excess 1 at the end.
286- # TFLite represents this in the offset. The offset is added to the end of particular dimension,
285+ # NeutronIR padding 'SAME' tries to split it evenly, but in case of odd padding, 'SAME' adds the excess 1 at the end.
286+ # NeutronIR represents this in the offset. The offset is added to the end of particular dimension,
287287 # i.e. bottom for H dim, right for W dim and so on.
288- # ONNX represents this in 'pads' as [x1_begin, x2_begin,... , x1_end, x2_end,...].
288+ # ExecuTorch represents this in 'pads' as [x1_begin, x2_begin,... , x1_end, x2_end,...].
289289 padding , offset = tflite_compute_padding_with_offset (
290290 tflite_input_shape , o_kernel_shape , tflite_output_shape , o_strides , o_dilations
291291 )
@@ -331,31 +331,35 @@ def shape_from_numpy(numpy_array):
331331 return tflite_model .Shape (dims )
332332
333333
334- def onnx_explicit_padding_to_tflite (onnx_pads : list [int ]) -> list [list [int ]]:
335- """Convert the attribute or input 'pads' of the ONNX 'Pad' operator to the 'paddings' input of the TFLite 'Pad'
334+ def executorch_explicit_padding_to_tflite (
335+ executorch_pads : list [int ],
336+ ) -> list [list [int ]]:
337+ """Convert the attribute or input 'pads' of the ExecuTorch 'Pad' operator to the 'paddings' input of the NeutronIR 'Pad'
336338 class of operators.
337339
338340 This function does NOT take tensor formats into consideration.
339341 """
340342
341- start_padding = onnx_pads [
342- : len (onnx_pads ) // 2
343+ start_padding = executorch_pads [
344+ : len (executorch_pads ) // 2
343345 ] # Padding at the start of each dimension
344- end_padding = onnx_pads [
345- len (onnx_pads ) // 2 :
346+ end_padding = executorch_pads [
347+ len (executorch_pads ) // 2 :
346348 ] # Padding at the end of each dimension
347349
348350 return list (zip (start_padding , end_padding ))
349351
350352
351- def onnx_pads_to_tflite_explicit_padding (onnx_pads : List [int ]) -> List [List [int ]]:
352- """Convert an ONNX attribute 'pads' of operators such as Conv, MaxPool or AveragePool, to a list of ints which is
353- compatible with the TFLite 'Pad' operator.
353+ def executorch_pads_to_tflite_explicit_padding (
354+ executorch_pads : List [int ],
355+ ) -> List [List [int ]]:
356+ """Convert an ExecuTorch attribute 'pads' of operators such as Conv, MaxPool or AveragePool, to a list of ints which is
357+ compatible with the NeutronIR 'Pad' operator.
354358 """
355359
356- tflite_padding = onnx_explicit_padding_to_tflite ( onnx_pads )
360+ tflite_padding = executorch_explicit_padding_to_tflite ( executorch_pads )
357361
358- # TFLite also allows padding to the 'batch' and 'channels'. ONNX does not
362+ # NeutronIR also allows padding to the 'batch' and 'channels'. ExecuTorch does not
359363 tflite_padding .insert (0 , [0 , 0 ])
360364 tflite_padding .append ([0 , 0 ])
361365
@@ -369,15 +373,15 @@ def _get_explicit_tflite_padding_for_same_lower(
369373 o_strides : Optional [List [int ]] = None ,
370374 o_dilations : Optional [List [int ]] = None ,
371375) -> List [List [int ]]:
372- """Get the TFLite explicit padding required to represent ONNX 'SAME_LOWER' auto_pad for a particular setting.
376+ """Get the NeutronIR explicit padding required to represent ExecuTorch 'SAME_LOWER' auto_pad for a particular setting.
373377
374- :param tflite_input_shape: TFLite (NHWC) shape of the input tensor of the operator.
375- :param tflite_output_shape: TFLite (NHWC) shape of the output tensor of the operator.
376- :param o_kernel_shape: ONNX 'kernel_shape' attribute.
377- :param o_strides: Optional ONNX 'o_strides' attribute.
378- :param o_dilations: Optional ONNX 'o_dilations' attribute.
378+ :param tflite_input_shape: NeutronIR (NHWC) shape of the input tensor of the operator.
379+ :param tflite_output_shape: NeutronIR (NHWC) shape of the output tensor of the operator.
380+ :param o_kernel_shape: ExecuTorch 'kernel_shape' attribute.
381+ :param o_strides: Optional ExecuTorch 'o_strides' attribute.
382+ :param o_dilations: Optional ExecuTorch 'o_dilations' attribute.
379383
380- :return: A TFLite style explicit padding, compatible with the TFLite 'Pad' operator.
384+ :return: A NeutronIR style explicit padding, compatible with the NeutronIR 'Pad' operator.
381385 """
382386
383387 padding , offset = tflite_compute_padding_with_offset (
@@ -396,8 +400,8 @@ def _get_explicit_tflite_padding_for_same_lower(
396400
397401
398402def convert_data_to_channels_first (array : np .ndarray ) -> np .ndarray :
399- """Convert a numpy array representing the data of a tensor from the channels last format (TFLite ), to channels
400- first format (ONNX ).
403+ """Convert a numpy array representing the data of a tensor from the channels last format (NeutronIR ), to channels
404+ first format (ExecuTorch ).
401405
402406 :param array: Numpy array holding the tensor's data.
403407 :return: The transformed data.
@@ -412,8 +416,8 @@ def convert_data_to_channels_first(array: np.ndarray) -> np.ndarray:
412416
413417
414418def convert_data_to_channels_last (array : np .ndarray ) -> np .ndarray :
415- """Convert a numpy array representing the data of a tensor from the channels first format (ONNX ), to channels last
416- format (TFLite ).
419+ """Convert a numpy array representing the data of a tensor from the channels first format (ExecuTorch ), to channels last
420+ format (NeutronIR ).
417421
418422 :param array: Numpy array holding the tensor's data.
419423 :return: The transformed data.
@@ -442,9 +446,9 @@ def create_channels_last_to_channels_first_permutation(
442446 rank : int , return_list : bool = False
443447) -> np .ndarray | list [int ]:
444448 """Return a numpy array with data that describes the permutation, which would change a tensor from the channels
445- last (TFLite ) format to the channels first (ONNX ) format.
449+ last (NeutronIR ) format to the channels first (ExecuTorch ) format.
446450
447- This permutation is compatible with the TFLite `Transpose` operator.
451+ This permutation is compatible with the NeutronIR `Transpose` operator.
448452
449453 :param rank: The rank of the required permutation.
450454 :param return_list: If True, the function returns a list of ints. If False, a numpy array is returned.
@@ -463,9 +467,9 @@ def create_channels_first_to_channels_last_permutation(
463467 rank : int , return_list : bool = False
464468) -> np .ndarray | list [int ]:
465469 """Return a numpy array with data that describes the permutation, which would change a tensor from the channels
466- first (ONNX ) format to the channels last (TFLite ) format.
470+ first (ExecuTorch ) format to the channels last (NeutronIR ) format.
467471
468- This permutation is compatible with the TFLite `Transpose` operator.
472+ This permutation is compatible with the NeutronIR `Transpose` operator.
469473
470474 :param rank: The rank of the required permutation.
471475 :param return_list: If True, the function returns a list of ints. If False, a numpy array is returned.
@@ -481,7 +485,7 @@ def create_channels_first_to_channels_last_permutation(
481485
482486
483487def apply_permutation_to (target : List [Any ], permutation : Collection [int ]) -> List :
484- """Permute a list according to a permutation. Uses the same permutation format as the TFLite Transpose operator.
488+ """Permute a list according to a permutation. Uses the same permutation format as the NeutronIR Transpose operator.
485489
486490 :param target: A list of any types, to permute. Must be same size as the permutation.
487491 :param permutation: The permutation to apply to the target.
@@ -499,7 +503,7 @@ def apply_permutation_to(target: List[Any], permutation: Collection[int]) -> Lis
499503
500504def create_inverse_permutation (permutation : List [int ]) -> List [int ]:
501505 """Create and return a permutation, that is the inverse of the given 'permutation' parameter.
502- Uses the same permutation format as the TFLite Transpose operator.
506+ Uses the same permutation format as the NeutronIR Transpose operator.
503507
504508 :param permutation: The permutation to create the inverse of.
505509 :return: Inverse permutation.
@@ -516,7 +520,7 @@ def create_inverse_permutation(permutation: List[int]) -> List[int]:
516520
517521
518522def convert_data_type (torch_type : torch .TensorType ) -> TensorType :
519- """Convert Torch DataType to TFLite TensorType"""
523+ """Convert Torch DataType to NeutronIR TensorType"""
520524
521525 if torch_type == torch .float32 :
522526 return TensorType .FLOAT32
@@ -544,7 +548,7 @@ def convert_data_type(torch_type: torch.TensorType) -> TensorType:
544548
545549
546550def torch_type_to_numpy_type (torch_type : torch .TensorType ) -> np .ScalarType :
547- """Convert Torch DataType to TFLite TensorType"""
551+ """Convert Torch DataType to NeutronIR TensorType"""
548552
549553 if torch_type == torch .float32 :
550554 return np .dtype (np .float32 )
@@ -569,10 +573,10 @@ def torch_type_to_numpy_type(torch_type: torch.TensorType) -> np.ScalarType:
569573
570574
571575def numpy_type_to_tf_lite (numpy_type : np .dtype ) -> TensorType : # noqa C901
572- """Convert the numpy data type to a corresponding TFLite 'TensorType'.
576+ """Convert the numpy data type to a corresponding NeutronIR 'TensorType'.
573577
574578 :param numpy_type: Numpy dtype to convert.
575- :return: Corresponding TFLite TensorType.
579+ :return: Corresponding NeutronIR TensorType.
576580 """
577581 numpy_type = numpy_type .type
578582
@@ -626,12 +630,12 @@ def numpy_type_to_tf_lite(numpy_type: np.dtype) -> TensorType: # noqa C901
626630 else :
627631 logger .e (
628632 logger .Code .CONVERSION_IMPOSSIBLE ,
629- f"Cannot convert numpy data type '{ numpy_type } ' to TFLite ." ,
633+ f"Cannot convert numpy data type '{ numpy_type } ' to NeutronIR ." ,
630634 )
631635
632636
633637def tf_lite_type_to_numpy (tfl_type : TensorType ) -> np .ScalarType : # noqa C901
634- """Convert TFLite TensorType to numpy dtype"""
638+ """Convert NeutronIR TensorType to numpy dtype"""
635639
636640 if tfl_type == TensorType .FLOAT32 :
637641 return np .dtype (np .float32 )
0 commit comments