Ejemplo n.º 1
0
def _create_npu_op_conv2d(
    serial_2d_convolution: spec.Serial2DConvolution,
) -> Tuple[vapi.NpuConv2DOperation, int]:
    """This is a helper function to capture a list
    of arguments to create Vela NpuConv2DOperation object.
    """
    has_two_weights = serial_2d_convolution.weight2.address != -1
    has_two_biases = serial_2d_convolution.scale_bias2.address != -1

    npu_conv2d_op = vapi.NpuConv2DOperation()
    npu_conv2d_op.ifm = _create_npu_feature_map(serial_2d_convolution.ifm)
    npu_conv2d_op.ofm = _create_npu_feature_map(serial_2d_convolution.ofm)
    npu_conv2d_op.kernel = _create_npu_kernel(serial_2d_convolution.kernel)
    npu_conv2d_op.weights = ([
        _create_npu_address_range(serial_2d_convolution.weight),
        _create_npu_address_range(serial_2d_convolution.weight2),
    ] if has_two_weights else [
        _create_npu_address_range(serial_2d_convolution.weight)
    ])
    weights_zero_point = np.int64(
        serial_2d_convolution.weight_zero_point.value)
    npu_conv2d_op.biases = ([
        _create_npu_address_range(serial_2d_convolution.scale_bias),
        _create_npu_address_range(serial_2d_convolution.scale_bias2),
    ] if has_two_biases else [
        _create_npu_address_range(serial_2d_convolution.scale_bias)
    ])
    npu_conv2d_op.padding = _create_npu_padding(serial_2d_convolution.padding)

    npu_conv2d_op.activation = _create_npu_activation(
        serial_2d_convolution.activation)
    if (npu_conv2d_op.activation and npu_conv2d_op.activation.op_type
            == vapi.NpuActivationOp.NONE_OR_RELU):
        _convert_clip_bounds(npu_conv2d_op)

    npu_conv2d_op.rounding_mode = _create_npu_rounding_mode(
        serial_2d_convolution.rounding_mode)
    npu_conv2d_op.ifm_upscale = _create_npu_resampling_mode(
        serial_2d_convolution.upscale)
    weights_shape_ohwi = [
        npu_conv2d_op.ofm.shape.depth,
        npu_conv2d_op.kernel.height,
        npu_conv2d_op.kernel.width,
        npu_conv2d_op.ifm.shape.depth,
    ]
    npu_conv2d_op.block_traversal = vela_api.calculate_block_traversal_mode(
        is_depthwise=False,
        weights_shape_ohwi=weights_shape_ohwi,
        ifm_bitdepth=npu_conv2d_op.ifm.data_type.size_in_bits(),
    )
    npu_conv2d_op.block_config = _create_npu_block_config(
        serial_2d_convolution.block_config)

    if not npu_conv2d_op.block_config:
        target_accel_config = vela_api.get_accelerator_config()
        block_config = vela_api.get_optimal_block_config(
            npu_conv2d_op, target_accel_config)
        npu_conv2d_op.block_config = block_config

    return npu_conv2d_op, weights_zero_point
Ejemplo n.º 2
0
def _create_npu_op_conv2d(serial_2d_convolution):
    """This is a helper function to capture a list
    of arguments to create Vela NpuConv2DOperation object
    """
    npu_conv2d_op = vapi.NpuConv2DOperation()
    npu_conv2d_op.ifm = _create_npu_feature_map(serial_2d_convolution.ifm)
    npu_conv2d_op.ofm = _create_npu_feature_map(serial_2d_convolution.ofm)
    npu_conv2d_op.kernel = _create_npu_kernel(serial_2d_convolution.kernel)
    npu_conv2d_op.weights = [
        _create_npu_address_range(serial_2d_convolution.weight)
    ]
    weights_zero_point = np.int64(
        serial_2d_convolution.weight_zero_point.value)
    npu_conv2d_op.biases = [
        _create_npu_address_range(serial_2d_convolution.scale_bias)
    ]
    npu_conv2d_op.padding = _create_npu_padding(serial_2d_convolution.padding)

    npu_conv2d_op.activation = _create_npu_activation(
        serial_2d_convolution.activation)
    if (npu_conv2d_op.activation and npu_conv2d_op.activation.op_type
            == vapi.NpuActivationOp.NONE_OR_RELU):
        _convert_clip_bounds(npu_conv2d_op)

    npu_conv2d_op.upscale = _create_npu_resampling_mode(
        serial_2d_convolution.upscale)
    target_accel_type = vela_api.get_target_accel_type()  # type: ignore
    block_config = vela_api.get_optimal_block_config(npu_conv2d_op,
                                                     target_accel_type)
    npu_conv2d_op.block_config = block_config
    weights_shape_ohwi = [
        npu_conv2d_op.ofm.shape.depth,
        npu_conv2d_op.kernel.height,
        npu_conv2d_op.kernel.width,
        npu_conv2d_op.ifm.shape.depth,
    ]
    npu_conv2d_op.block_traversal = vela_api.calculate_block_traversal_mode(
        is_depthwise=False,
        weights_shape_ohwi=weights_shape_ohwi,
        ifm_bitdepth=npu_conv2d_op.ifm.data_type.size_in_bits(),
    )
    return npu_conv2d_op, weights_zero_point