Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CMSIS-NN] Aligned scale computation with TFLM to fix numerical mismatch #10817

Merged
merged 8 commits into from
Apr 6, 2022
10 changes: 5 additions & 5 deletions src/relay/backend/contrib/cmsisnn/generate_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ class GenerateConstantsMutator : public MixedModeMutator {
// Obtain input and output scales from Relay's Requantization
int64_t out_channels = conv2d_attrs->channels.as<IntImmNode>()->value;
float output_scale = GetScalarFromConstant<float>(requantize_call->args[3]);
auto input_scales = tvm::relay::qnn::GetFloatVectorFromConstant(requantize_call->args[1]);
ICHECK(input_scales.size() == static_cast<size_t>(out_channels));
auto input_scale = GetScalarFromConstant<float>(conv2d_call->args[4]);
auto filter_scales = tvm::relay::qnn::GetFloatVectorFromConstant(conv2d_call->args[5]);

// Calculate requantization multiplier and shift
Device dev{DLDeviceType::kDLCPU, 0};
Expand All @@ -134,10 +134,10 @@ class GenerateConstantsMutator : public MixedModeMutator {
int32_t* multiplier = static_cast<int32_t*>(multiplier_nda->data);
int32_t* shift = static_cast<int32_t*>(shift_nda->data);
for (int i = 0; i < out_channels; ++i) {
double quantized_multiplier =
static_cast<double>(input_scales[i]) / static_cast<double>(output_scale);
double effective_output_scale =
static_cast<double>(input_scale) * filter_scales[i] / static_cast<double>(output_scale);
std::tie(*(multiplier + i), *(shift + i)) =
tvm::relay::qnn::GetFixedPointMultiplierShift(quantized_multiplier);
tvm::relay::qnn::GetFixedPointMultiplierShift(effective_output_scale);
}

// Create constants from requantization multiplier and shift
Expand Down
88 changes: 88 additions & 0 deletions tests/python/contrib/test_cmsisnn/test_conv2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,94 @@ def test_conv2d_asymmetric_padding_int8(
)


# This test expects assertion as the output should mismatch between TVM and CMSIS-NN
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this assert that it matches the expected output? Similar to how the NPU tests check that they are consistent with TFLite?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we could compare against TFLite output. So far all the CMSIS-NN tests compare against TVM output.

@tvm.testing.requires_cmsisnn
@pytest.mark.parametrize("padding", ["SAME", "VALID"])
def test_conv2d_numerical_mismatch_int8(padding):
interface_api = "c"
use_unpacked_api = True
test_runner = AOT_USMP_CORSTONE300_RUNNER

out_channels = 3
ifm_shape = (1, 64, 100, 4)
input_zero_point = -64
input_scale = 1
strides = (1, 1)
dilation = (1, 1)
dtype = "int8"
groups = 1
relu_type = "RELU"
weight_format = "HWIO"
kernel_size = (3, 3)
kernel_h = kernel_size[0]
kernel_w = kernel_size[1]
kernel_shape = (kernel_h, kernel_w, ifm_shape[3] // groups, out_channels)
kernel_zero_point = 0
kernel_scale = [1, 0.0256, 1.37]
enable_bias = False
in_min, in_max = get_range_for_dtype_str(dtype)

output_scale, output_zero_point = get_conv2d_qnn_params(
kernel_shape,
input_scale,
input_zero_point,
kernel_scale,
kernel_zero_point,
dtype,
dtype,
dtype,
)

model, params = make_model(
ifm_shape,
kernel_shape,
input_zero_point,
input_scale,
kernel_zero_point,
kernel_scale,
output_zero_point,
output_scale,
padding,
strides,
dilation,
groups,
dtype,
dtype,
out_channels,
weight_format,
enable_bias,
relu_type,
)
orig_mod = make_module(model)
cmsisnn_mod = cmsisnn.partition_for_cmsisnn(orig_mod, params)

# validate pattern matching
assert_partitioned_function(orig_mod, cmsisnn_mod)

# DO_NOT_RANDOMIZE: validate the output with fixed input
seed = 0
random_state = np.random.RandomState(seed)
input_data = random_state.randint(in_min, in_max, ifm_shape, dtype)
inputs = {"input": input_data}
output_list = generate_ref_data(orig_mod["main"], inputs, params)
try:
compile_and_run(
AOTTestModel(
module=cmsisnn_mod,
inputs=inputs,
outputs=output_list,
params=params,
output_tolerance=0,
),
test_runner,
interface_api,
use_unpacked_api,
)
exit(-1)
except AssertionError:
return


@pytest.mark.skip(reason="See https://github.com/apache/tvm/issues/10314")
@tvm.testing.requires_cmsisnn
@pytest.mark.parametrize("ifm_shape", [(1, 28, 28, 12), (1, 64, 100, 4)])
Expand Down