def test_op_int8(op, input_0_scale, input_0_zero_point, input_1_scale, input_1_zero_point): interface_api = "c" use_unpacked_api = True test_runner = AOT_CORSTONE300_RUNNER dtype = "int8" shape = [1, 16, 16, 3] model = make_model( op, shape, dtype, dtype, input_0_scale, input_0_zero_point, input_1_scale, input_1_zero_point, ) orig_mod = make_module(model) cmsisnn_mod = cmsisnn.partition_for_cmsisnn(orig_mod) # validate pattern matching attrs = [ cmsisnn_mod[var.name_hint].attrs for var in cmsisnn_mod.get_global_vars() if cmsisnn_mod[var.name_hint].attrs ] assert any(attrs), "At least one function with external attributes was expected." compilers = [ key == "Compiler" and value == "cmsis-nn" for attr in attrs for key, value in attr.items() ] assert any(compilers), "Module does not contain function for cmsisnn target." assert count_num_calls(orig_mod) == count_num_calls( cmsisnn_mod ), "Number of calls changed during partitioning" # validate the output in_min, in_max = get_range_for_dtype_str(dtype) inputs = { "input_0": np.random.randint(in_min, high=in_max, size=shape, dtype=dtype), "input_1": np.random.randint(in_min, high=in_max, size=shape, dtype=dtype), } output_list = generate_ref_data(orig_mod["main"], inputs) compile_and_run( AOTTestModel( module=cmsisnn_mod, inputs=inputs, outputs=output_list, output_tolerance=1, ), test_runner, interface_api, use_unpacked_api, )
def test_depthwise_int8( ifm_shape, kernel_size, padding, strides, dilation, enable_bias, relu_type, input_zero_point, input_scale, kernel_scale, out_channels, depth_multiplier, ): interface_api = "c" use_unpacked_api = True test_runner = AOT_CORSTONE300_RUNNER dtype = "int8" groups = 1 weight_format = "HWIO" 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 in_min, in_max = get_range_for_dtype_str(dtype) groups = ifm_shape[3] weight_format = "HWOI" kernel_shape = (kernel_h, kernel_w, ifm_shape[3], depth_multiplier) out_channels = ifm_shape[3] * depth_multiplier ks_len = len(kernel_scale) kernel_scale = [kernel_scale[i % ks_len] for i in range(out_channels)] output_scale, output_zero_point = get_conv2d_qnn_params( kernel_shape, input_scale, input_zero_point, kernel_scale, kernel_zero_point, dtype, dtype, dtype, True, ) 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 attrs = [ cmsisnn_mod[var.name_hint].attrs for var in cmsisnn_mod.get_global_vars() if cmsisnn_mod[var.name_hint].attrs ] assert any(attrs), "At least one function with external attributes was expected." compilers = [ key == "Compiler" and value == "cmsis-nn" for attr in attrs for key, value in attr.items() ] assert any(compilers), "Module does not contain function for cmsis-nn target." assert count_num_calls(orig_mod) == count_num_calls( cmsisnn_mod ), "Number of calls changed during partitioning" # validate the output rng = np.random.default_rng(12345) inputs = {"input": rng.integers(in_min, high=in_max, size=ifm_shape, dtype=dtype)} output_list = generate_ref_data(orig_mod["main"], inputs, params) compile_and_run( AOTTestModel( module=cmsisnn_mod, inputs=inputs, outputs=output_list, params=params, output_tolerance=1, ), test_runner, interface_api, use_unpacked_api, )
def test_op_int8( in_shape, enable_bias, input_zero_point, input_scale, kernel_scale, out_channels, relu_type, ): interface_api = "c" use_unpacked_api = True test_runner = AOT_CORSTONE300_RUNNER dtype = "int8" kernel_zero_point = 0 kernel_shape = [out_channels, in_shape[1]] conv2d_kernel_shape = (1, 1, kernel_shape[0], kernel_shape[1]) in_min, in_max = get_range_for_dtype_str(dtype) output_scale, output_zero_point = get_conv2d_qnn_params( conv2d_kernel_shape, input_scale, input_zero_point, kernel_scale, kernel_zero_point, dtype, ) model, params = make_model( in_shape, kernel_shape, input_zero_point, kernel_zero_point, input_scale, kernel_scale, output_zero_point, output_scale, dtype, dtype, out_channels, enable_bias, ) orig_mod = make_module(model) cmsisnn_mod = cmsisnn.partition_for_cmsisnn(orig_mod, params) # validate pattern matching attrs = [ cmsisnn_mod[var.name_hint].attrs for var in cmsisnn_mod.get_global_vars() if cmsisnn_mod[var.name_hint].attrs ] assert any( attrs), "At least one function with external attributes was expected." compilers = [ key == "Compiler" and value == "cmsis-nn" for attr in attrs for key, value in attr.items() ] assert any( compilers), "Module does not contain function for cmsisnn target." assert count_num_calls(orig_mod) == count_num_calls( cmsisnn_mod), "Number of calls changed during partitioning" # validate the output rng = np.random.default_rng(12345) inputs = { "input": rng.integers(in_min, high=in_max, size=in_shape, dtype=dtype) } output_list = generate_ref_data(orig_mod["main"], inputs, params) compile_and_run( AOTTestModel( module=cmsisnn_mod, inputs=inputs, outputs=output_list, params=params, output_tolerance=1, ), test_runner, interface_api, use_unpacked_api, )