afe.ir.operation_functionsο
Classesο
Supported run modes. |
Functionsο
|
|
|
|
|
Execute a floating-point convolution using an algorithm from ml_kernels. |
|
Execute a quantized convolution using an algorithm from ml_kernels. |
|
Helper function used to adapt weight tensor for use in ml_kernels |
|
Compute a floating-point convolution by calling Tensorflow's convolution operator. |
|
|
|
Execute ml_kernels ideal_add_sub function for AddActivation and Subtract operators. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Floating-point multiplication. |
|
Quantized multiplication. |
|
|
|
When in quantized set to True, using avg_pool2d to do mean along axis = |
|
|
|
|
|
|
|
|
|
Execute concatenate operation. Used for both non-quantized and quantized inputs. |
|
|
|
|
|
|
|
|
|
Flattens all the dimensions except for the batch dimension |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Helper function for expanding begin, end and strides to match the shape length. |
|
|
Get StridedSliceOp output shape. |
|
Execute strided slice operator. |
|
|
|
|
|
AFE and MLA does not have a way to support nearest_neighbor with asymmetric. However, |
|
Upsample the input tensor along H and/or W dimension |
|
Image interpolation through GridSample |
|
|
|
|
|
|
|
|
|
|
|
Input tensor is 4D NHWC, int8 only |
|
Input tensor is 2D |
|
Helper function to get a tensor shape where channel is aligned based on the element size. |
|
|
|
Reshape tensor to MLA 2D buffer shape (batch_size, data_size (prod(spatial dimensions) * C)) where data size |
|
Reshape tensor from MLA 2D shape to 4D/5D shape. |
|
Multiple tensors are packed sequentially as a 2D array. |
|
A 2D array is unpacked to produce multiple 2D arrays |
|
Normalization performs the following three steps: |
|
Quantization transform. |
|
Dequantization transform. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
For the rounding type used for this operator (away from 0), refer to: |
|
|
|
|
|
|
|
|
|
|
|
Initialize the custom op. Compile the custom op and put it into the |
|
Execute the custom op |
|
Execute batch matmul operation using ml_kernels implementation. |
Module Contentsο
- class afe.ir.operation_functions.RunMode(*args, **kwds)ο
Supported run modes.
MLA_MODE : use an implementation that exactly matches execution on the MLA. FAST_MODE : use a fast execution implementation that may not match MLA. JAX : use JAX implementation that matches MLA.
- MLA_MODE = 1ο
- FAST_MODE = 2ο
- JAX = 3ο
- is_fast_mode()ο
- is_jax() boolο
- afe.ir.operation_functions.placeholder(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.constant(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.float_convolution(attrs: afe.ir.attributes.ConvAddActivationAttrs, data: numpy.ndarray, mode: RunMode) numpy.ndarrayο
Execute a floating-point convolution using an algorithm from ml_kernels.
- Parameters:
attrs β Attributes of the convolution operator
data β Input activation data in NHWC layout
mode β Mode of execution
- Returns:
Convolved result
- afe.ir.operation_functions.quantized_convolution(attrs: afe.ir.attributes.ConvQuantAttrs, data: numpy.ndarray, mode: RunMode) numpy.ndarrayο
Execute a quantized convolution using an algorithm from ml_kernels.
- Parameters:
attrs β Attributes of the convolution operator
data β Input activation data in NHWC layout
mode β Mode of execution
- Returns:
Convolved result
- afe.ir.operation_functions.prepare_weight_tensor(weight: numpy.ndarray, do_transpose: bool) numpy.ndarrayο
Helper function used to adapt weight tensor for use in ml_kernels convolution.
- Parameters:
weight β Weight tensor.
do_transpose β Flag indicating whether weight needs to be transposed. Currently, Numpy and JAX ml_kernels convolution implementation observe weights using different layout.
- Returns:
Modified weights tensor in order to adapt its shape to ml_kernels convolution implementations.
- afe.ir.operation_functions.conv_tensorflow(attrs: afe.ir.attributes.ConvAttrs, data: numpy.ndarray, weight: numpy.ndarray) numpy.ndarrayο
Compute a floating-point convolution by calling Tensorflowβs convolution operator.
This function may not exactly match MLA behavior.
- Parameters:
attrs β Attributes of the convolution
data β Input tensor
weight β Weight tensor
Returns: Convolved tensor
- afe.ir.operation_functions.float_add(lhs: numpy.ndarray, rhs: numpy.ndarray, axis: int | None = None) numpy.ndarrayο
- afe.ir.operation_functions.ml_kernels_add_sub(attrs: afe.ir.attributes.AddActivationAttrs | afe.ir.attributes.AddQuantAttrs | afe.ir.attributes.SubtractAttrs | afe.ir.attributes.SubtractQuantAttrs, lhs: numpy.ndarray, rhs: numpy.ndarray, use_jax: bool = False) numpy.ndarrayο
Execute ml_kernels ideal_add_sub function for AddActivation and Subtract operators.
Supports int8, int16 and bfloat16 data types.
- Parameters:
attrs β SiMaIR attributes for AddActivationOp or SubtractOp.
lhs β The first operand of AddActivationOp or SubtractOp.
rhs β The second operand of AddActivationOp or SubtractOp.
use_jax β Flag indicating whether to use JAX implementation.
- Returns:
The result of executing the AddActivationOp or SubtractOp.
- afe.ir.operation_functions.relu(data: numpy.ndarray, zp: int = 0) numpy.ndarrayο
- afe.ir.operation_functions.clip(attrs: afe.ir.attributes.ClipAttrs | afe.ir.attributes.ClipQuantAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.prelu(data: numpy.ndarray, alpha: numpy.ndarray | float | int, axis: int | None = None, zp: int = 0) numpy.ndarrayο
- afe.ir.operation_functions.elu(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.leaky_relu(data: numpy.ndarray, alpha: float | int) numpy.ndarrayο
- afe.ir.operation_functions.maxpool(attrs: afe.ir.attributes.PoolAttrs, data: numpy.ndarray, pad_value: float | int, requantization: ml_kernels.requantization.BaseRequantization | None = None, mode: RunMode = RunMode.MLA_MODE) numpy.ndarrayο
- afe.ir.operation_functions.avgpool(attrs: afe.ir.attributes.PoolAttrs, data: numpy.ndarray, pad_value: float | int, output_type: numpy.dtype, requant: ml_kernels.requantization.BaseRequantization | None = None, mode: RunMode = RunMode.MLA_MODE) numpy.ndarrayο
- afe.ir.operation_functions.variance(data: numpy.ndarray, mean: numpy.ndarray, requant: ml_kernels.requantization.BaseRequantization | None = None, requant_var: ml_kernels.requantization.BaseRequantization | None = None)ο
- afe.ir.operation_functions.broadcast_to(attrs: afe.ir.attributes.BroadcastToAttrs | afe.ir.attributes.BroadcastToQuantAttrs, data: numpy.ndarray)ο
- afe.ir.operation_functions.multiply_tensorflow(lhs: numpy.ndarray, rhs: numpy.ndarray) numpy.ndarrayο
Floating-point multiplication.
- afe.ir.operation_functions.ml_kernels_multiply(attrs: afe.ir.attributes.MultiplyAttrs | afe.ir.attributes.MultiplyQuantAttrs, lhs: numpy.ndarray, rhs: numpy.ndarray, mode: RunMode) numpy.ndarrayο
Quantized multiplication.
- afe.ir.operation_functions.pad(attrs: afe.ir.attributes.PadAttrs, data: numpy.ndarray, pad_value: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.mean(attrs: afe.ir.attributes.ReduceAttrs, data: numpy.ndarray, quantized=False) numpy.ndarrayο
- When in quantized set to True, using avg_pool2d to do mean along axis =
(1, 2)
Parametersο
- param attrs:
ReduceAttrs. Attributes needed to execute the mean operation.
- param data:
np.ndarray. Input data to the mean operation.
- param quantized:
bool. Default is False. Set to True if the mean operation is executed in a quantization domain.
Returnο
- return:
np.ndarray. Result of the mean operation.
- afe.ir.operation_functions.squeeze(attrs: afe.ir.attributes.SqueezeAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.argmax(attrs: afe.ir.attributes.ArgMaxAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.softmax(attrs: afe.ir.attributes.SoftmaxAttrs | afe.ir.attributes.SoftmaxQuantAttrs, data: numpy.ndarray, mode: RunMode = RunMode.MLA_MODE) numpy.ndarrayο
- afe.ir.operation_functions.lrn(attrs: afe.ir.attributes.LRNAttrs | afe.ir.attributes.LRNQuantAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.concatenate(attrs: afe.ir.attributes.ConcatenateAttrs | afe.ir.attributes.ConcatQuantAttrs, data_list: list[numpy.ndarray], run_mode: RunMode = RunMode.MLA_MODE) numpy.ndarrayο
Execute concatenate operation. Used for both non-quantized and quantized inputs.
- Parameters:
attrs β Concatenate operation attributes.
data_list β List of input tensors to be concatenated.
run_mode β Execution mode.
- Returns:
The result of concatenate operation.
- afe.ir.operation_functions.transpose(attrs: afe.ir.attributes.TransposeAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.depth_to_space(attrs: afe.ir.attributes.DepthToSpaceAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.reshape(attrs: afe.ir.attributes.ReshapeAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.expand_dims(attrs: afe.ir.attributes.ExpandDimsAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.batch_flatten(data: numpy.ndarray) numpy.ndarrayο
Flattens all the dimensions except for the batch dimension
- afe.ir.operation_functions.min_op(attrs: afe.ir.attributes.ExtmAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.max_op(attrs: afe.ir.attributes.ExtmAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.sum_op(attrs: afe.ir.attributes.ReduceAttrs | afe.ir.attributes.ReduceQuantAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.prod(attrs: afe.ir.attributes.ProdAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.tuple_get_item(attrs: afe.ir.attributes.TupleGetItemAttrs, t: tuple) numpy.ndarrayο
- afe.ir.operation_functions.exp(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.sqrt(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.sigmoid(data: numpy.ndarray, mode: RunMode = RunMode.MLA_MODE) numpy.ndarrayο
- afe.ir.operation_functions.swish(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.quick_gelu(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.hard_sigmoid(data: numpy.ndarray, alpha: float, beta: float) numpy.ndarrayο
- afe.ir.operation_functions.hard_swish(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.log(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.softplus(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.erf(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.gelu(x: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.log2(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.log10(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.subtract_tensorflow(lhs: numpy.ndarray, rhs: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.power(lhs: numpy.ndarray, rhs: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.divide(lhs: numpy.ndarray, rhs: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.reciprocal(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.maximum(lhs: numpy.ndarray, rhs: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.minimum(lhs: numpy.ndarray, rhs: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.full(attrs: afe.ir.attributes.FullAttrs, fill_value: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.tile(attrs: afe.ir.attributes.TileAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.split(attrs: afe.ir.attributes.SplitAttrs, data: numpy.ndarray) Tuple[numpy.ndarray, Ellipsis]ο
- afe.ir.operation_functions.take(attrs: afe.ir.attributes.TakeAttrs, data: numpy.ndarray, indices: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.expand_strided_slice_indices_to_shape_length(begin: list[int], end: list[int], strides: list[int], axes: list[int] | None, input_shape: list[int]) tuple[list[int], list[int], list[int]]ο
Helper function for expanding begin, end and strides to match the shape length.
- afe.ir.operation_functions.get_strided_slice_out_shape(attrs: afe.ir.attributes.StridedSliceAttrs) tuple[int, Ellipsis]ο
Get StridedSliceOp output shape.
- Parameters:
attrs β Strided slice operator attributes.
- Returns:
Output shape of strided slice operator.
- afe.ir.operation_functions.strided_slice(attrs: afe.ir.attributes.StridedSliceAttrs, data: numpy.ndarray, run_mode: RunMode = RunMode.MLA_MODE) numpy.ndarrayο
Execute strided slice operator.
- Parameters:
attrs β Strided slice operator attributes.
data β Input tensor.
run_mode β Execution mode.
- Returns:
The result of a strided slice operator.
- afe.ir.operation_functions.rsqrt(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.tanh(data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.image_resize2d(attrs: afe.ir.attributes.ImageResize2DAttrs, data: numpy.ndarray, rounding: str | None = None) numpy.ndarrayο
AFE and MLA does not have a way to support nearest_neighbor with asymmetric. However, the error should be ignorable during inference.
- afe.ir.operation_functions.upsample(attrs: afe.ir.attributes.UpsamplingAttrs, data: numpy.ndarray, rounding: str | None = None) numpy.ndarrayο
Upsample the input tensor along H and/or W dimension
- afe.ir.operation_functions.gridsample(attrs: afe.ir.attributes.GridSampleAttrs, data: numpy.ndarray, grid: numpy.ndarray) numpy.ndarrayο
Image interpolation through GridSample
- afe.ir.operation_functions.layer_norm(attrs: afe.ir.attributes.LayerNormAttrs | afe.ir.attributes.LayerNormQuantAttrs, data: numpy.ndarray, run_mode: RunMode = RunMode.MLA_MODE) numpy.ndarrayο
- afe.ir.operation_functions.rms_norm(data: numpy.ndarray, attrs: afe.ir.attributes.RMSNormAttrs | afe.ir.attributes.RMSNormQuantAttrs, mode: RunMode) numpy.ndarrayο
- afe.ir.operation_functions.instance_norm(data: numpy.ndarray, mean: numpy.ndarray, variance: numpy.ndarray, attrs: afe.ir.attributes.InstanceNormAttrs | afe.ir.attributes.InstanceNormQuantAttrs)ο
- afe.ir.operation_functions.layout_transform(attrs: afe.ir.attributes.LayoutTransformAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.calculate_tessellated_tensor_shape(tensor_type: afe.ir.tensor_type.TensorType, slice_shape: Sequence[int], align_c16: bool) tuple[int, int]ο
- afe.ir.operation_functions.tessellation(attrs: afe.ir.attributes.TessellationTransformAttrs, data: numpy.ndarray) numpy.ndarrayο
Input tensor is 4D NHWC, int8 only Output tensor is 2D array
- afe.ir.operation_functions.detessellation(attrs: afe.ir.attributes.DetessellationTransformAttrs, data: numpy.ndarray) numpy.ndarrayο
Input tensor is 2D Output tensor is 4D: NHWC
- afe.ir.operation_functions.get_channel_aligned_shape(tensor_shape: Sequence[int], elem_size: int) tuple[int, Ellipsis]ο
Helper function to get a tensor shape where channel is aligned based on the element size.
- afe.ir.operation_functions.get_mla_padded_2d_shape(tensor_shape: Sequence[int], elem_size: int) tuple[int, int]ο
- afe.ir.operation_functions.reshape_to_mla_padded_2d_shape(tensor: numpy.ndarray) numpy.ndarrayο
Reshape tensor to MLA 2D buffer shape (batch_size, data_size (prod(spatial dimensions) * C)) where data size must be multiple of 16.
- afe.ir.operation_functions.reshape_from_mla_padded_2d_shape(tensor: numpy.ndarray, data_shape: Sequence[int], tensor_type: type) numpy.ndarrayο
Reshape tensor from MLA 2D shape to 4D/5D shape.
- Parameters:
tensor β 2D tensor.
data_shape β 4D/5D tensor shape.
- Returns:
Reshaped 4D/5D tensor.
- afe.ir.operation_functions.pack(data: List[numpy.ndarray]) numpy.ndarrayο
Multiple tensors are packed sequentially as a 2D array. Input data can be either a 2D tessellated tensor or
a 5D or higher tensor that will be tessellated on the MLA.
If there is the 5D or higher tensor, reshape it to MLA 2D shape.
- afe.ir.operation_functions.unpack(attrs: afe.ir.attributes.UnpackTransformAttrs, data: numpy.ndarray) List[numpy.ndarray]ο
A 2D array is unpacked to produce multiple 2D arrays
- afe.ir.operation_functions.normalization(attrs: afe.ir.attributes.NormalizationTransformAttrs, data: numpy.ndarray) numpy.ndarrayο
Normalization performs the following three steps: 1) Divide by a per-channel divisor 2) Subtract by per-channel mean values 3) Divide by per-channel standard deviation values
- afe.ir.operation_functions.ev_quantize(attrs: afe.ir.attributes.QuantizationTransformAttrs, data: numpy.ndarray) numpy.ndarrayο
Quantization transform.
- afe.ir.operation_functions.ev_dequantize(attrs: afe.ir.attributes.DequantizationTransformAttrs, data: numpy.ndarray) numpy.ndarrayο
Dequantization transform.
- afe.ir.operation_functions.ev_resize(attrs: afe.ir.attributes.ResizeTransformAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.chroma_upsample(attrs: afe.ir.attributes.ChromaUpsampleTransformAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.yuv_rgb_conversion(attrs: afe.ir.attributes.YuvRgbConversionTransformAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.bgr_rgb_conversion(attrs: afe.ir.attributes.BgrRgbConversionTransformAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.ev_sigmoid(attrs: afe.ir.attributes.SigmoidTransformAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.nms_maxpool(attrs: afe.ir.attributes.NmsMaxpoolTransformAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.cast(attrs: afe.ir.attributes.CastAttrs, data: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.qnn_quantize(attrs: afe.ir.attributes.QNNQuantizeAttrs, data: numpy.ndarray, output_scale: numpy.ndarray, output_zero_point: numpy.ndarray) numpy.ndarrayο
For the rounding type used for this operator (away from 0), refer to: https://github.com/apache/tvm/pull/3512/commits/c089ebcdf4b13f98b776bb0213779f6783fa6743#diff-a47be721cf0f30d86d0f548a8cc5a1fe184d0827efd450c8446bfc05d962abf5R47
- afe.ir.operation_functions.qnn_dequantize(attrs: afe.ir.attributes.QNNDequantizeAttrs, data: numpy.ndarray, input_scale: numpy.ndarray, input_zero_point: numpy.ndarray) numpy.ndarrayο
- afe.ir.operation_functions.do_requantize(in_scale, out_scale, in_zp, out_zp) boolο
- afe.ir.operation_functions.qnn_add(attrs: afe.ir.attributes.AwesomeAttributes, lhs: numpy.ndarray, rhs: numpy.ndarray, lhs_scale: float, lhs_zero_point: int, rhs_scale: float, rhs_zero_point: int, output_scale: float, output_zero_point: int, op: str = 'add') numpy.ndarrayο
- afe.ir.operation_functions.qnn_mul(attrs: afe.ir.attributes.AwesomeAttributes, lhs: numpy.ndarray, rhs: numpy.ndarray, lhs_scale: float, lhs_zero_point: int, rhs_scale: float, rhs_zero_point: int, output_scale: float, output_zero_point: int) numpy.ndarrayο
- afe.ir.operation_functions.external(attrs: afe.ir.attributes.ExternalAttrs, input_dict: Dict[str, numpy.ndarray]) numpy.ndarray | Tuple[numpy.ndarray, Ellipsis]ο
- afe.ir.operation_functions.init_custom_op(attrs: afe.ir.attributes.CustomOpAttrs, input_dict: Dict[afe.ir.defines.InputName, numpy.ndarray], output_shape: Tuple[int, Ellipsis], force_compile: bool = True) Noneο
Initialize the custom op. Compile the custom op and put it into the CustomOpLibraryManager. Update the CustomOpAttrs with generated arguments list and function so it can be used at the execution time. :param attrs: CustomOpAttrs :param input_dict: Dict[InputName, np.ndarray]. Input name to its tensor :param output_shape: Tuple[int, β¦]. Output shape :param force_compile: bool. Default is True. If True, the init_custom_op will
compile the custom op no matter the library is ready or not
- afe.ir.operation_functions.execute_custom_op(attrs: afe.ir.attributes.CustomOpAttrs, input_dict: Dict[afe.ir.defines.InputName, numpy.ndarray]) numpy.ndarrayο
Execute the custom op :param attrs: CustomOpAttrs :param input_dict: Dict[InputName, np.ndarray]. Input name to its tensor :return: np.ndarray
- afe.ir.operation_functions.batch_matmul(lhs: numpy.ndarray, rhs: numpy.ndarray, attrs: afe.ir.attributes.BatchMatmulAttrs | afe.ir.attributes.BatchMatmulQuantAttrs, run_mode: RunMode) numpy.ndarrayο
Execute batch matmul operation using ml_kernels implementation.
- Parameters:
lhs β Tensor representing lhs value of batch matmul operation.
rhs β Tensor representing rhs value of batch matmul operation.
attrs β BatchMatmul operatorβs attributes.
run_mode β Execution mode.
- Returns:
The result of batch matmul operation.