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

GNNE-1904 Fix/quant test #1031

Merged
merged 10 commits into from
Aug 3, 2023
1 change: 1 addition & 0 deletions src/Native/src/kernels/stackvm/optimized/dequantize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,6 @@ result<void> optimized::dequantize(
NNCASE_UNUSED kernel_context &context) noexcept {
DEQUANTIZE_IMPL(uint8_t, float)
DEQUANTIZE_IMPL(int8_t, float)
DEQUANTIZE_IMPL(int16_t, float)
return err(std::errc::not_supported);
}
1 change: 1 addition & 0 deletions src/Native/src/kernels/stackvm/optimized/quantize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@ result<void> optimized::quantize(
NNCASE_UNUSED kernel_context &context) noexcept {
QUANTIZE_IMPL(float, uint8_t)
QUANTIZE_IMPL(float, int8_t)
QUANTIZE_IMPL(float, int16_t)
return err(std::errc::not_supported);
}
1 change: 1 addition & 0 deletions src/Native/src/kernels/stackvm/reference/dequantize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,6 @@ result<void> nncase::kernels::stackvm::reference::dequantize(
float scale, float bias, kernel_context &context) noexcept {
DEQUANTIZE_IMPL(uint8_t, float);
DEQUANTIZE_IMPL(int8_t, float);
DEQUANTIZE_IMPL(int16_t, float);
return err(std::errc::not_supported);
}
1 change: 1 addition & 0 deletions src/Native/src/kernels/stackvm/reference/quantize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ result<void> nncase::kernels::stackvm::reference::quantize(
float scale, float bias, kernel_context &context) noexcept {
QUANTIZE_IMPL(float, uint8_t);
QUANTIZE_IMPL(float, int8_t);
QUANTIZE_IMPL(float, int16_t);
return err(std::errc::not_supported);
}
132 changes: 91 additions & 41 deletions tests/kernels/test_dequantize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class DequantizeTest

INSTANTIATE_TEST_SUITE_P(
dequantize, DequantizeTest,
testing::Combine(testing::Values(dt_uint8, dt_int8),
testing::Combine(testing::Values(dt_uint8, dt_int8, dt_int16),
testing::Values(dims_t{1, 3, 16, 16}, dims_t{1, 3, 16},
dims_t{3, 16}, dims_t{16, 16}, dims_t{1},
dims_t{})));
Expand All @@ -57,7 +57,6 @@ TEST_P(DequantizeTest, dequantize) {
auto zero_point_value = 127;
auto scale_value = 0.01f;

// expected
runtime_tensor zero_point_ptr;
if (input.datatype() == dt_uint8) {
uint8_t zero_point[] = {(uint8_t)zero_point_value};
Expand All @@ -73,6 +72,13 @@ TEST_P(DequantizeTest, dequantize) {
sizeof(zero_point)},
true, host_runtime_tensor::pool_cpu_only)
.expect("create tensor failed");
} else {
int16_t zero_point[] = {(int16_t)zero_point_value};
zero_point_ptr = hrt::create(nncase::dt_int16, {1},
{reinterpret_cast<gsl::byte *>(zero_point),
sizeof(zero_point)},
true, host_runtime_tensor::pool_cpu_only)
.expect("create tensor failed");
}

float_t scale[] = {scale_value};
Expand All @@ -81,46 +87,90 @@ TEST_P(DequantizeTest, dequantize) {
{reinterpret_cast<gsl::byte *>(scale), sizeof(scale)}, true,
host_runtime_tensor::pool_cpu_only)
.expect("create tensor failed");
auto output_ort =
ortki_DequantizeLinear(l_ort, runtime_tensor_2_ort_tensor(scale_ptr),
runtime_tensor_2_ort_tensor(zero_point_ptr), 0);
size_t size = 0;
void *ptr_ort = tensor_buffer(output_ort, &size);
dims_t shape(tensor_rank(output_ort));
tensor_shape(output_ort, reinterpret_cast<int64_t *>(shape.data()));
auto expected = hrt::create(dt_float32, shape,
{reinterpret_cast<gsl::byte *>(ptr_ort), size},
true, host_runtime_tensor::pool_cpu_only)
.expect("create tensor failed");

// actual
quant_param_t quantParam;
quantParam.zero_point = zero_point_value;
quantParam.scale = scale_value;
quant_param_t quant_param[] = {quantParam};
auto quant_param_ptr =
hrt::create(
dt_int64, {1},
{reinterpret_cast<gsl::byte *>(quant_param), sizeof(quant_param)},
true, host_runtime_tensor::pool_cpu_only)
.expect("create tensor failed");
auto output = kernels::stackvm::dequantize(dt_float32, input.impl(),
quant_param_ptr.impl())
.expect("dequantize failed");
runtime_tensor actual(output.as<tensor>().expect("as tensor failed"));

bool result = is_same_tensor(expected, actual) ||
cosine_similarity_tensor(expected, actual);

if (!result) {
std::cout << "actual ";
print_runtime_tensor(actual);
std::cout << "expected ";
print_runtime_tensor(expected);
}

// compare
EXPECT_TRUE(result);
if (zero_point_ptr.datatype() != dt_int16) {

// expected
auto output_ort = ortki_DequantizeLinear(
l_ort, runtime_tensor_2_ort_tensor(scale_ptr),
runtime_tensor_2_ort_tensor(zero_point_ptr), 0);
size_t size = 0;
void *ptr_ort = tensor_buffer(output_ort, &size);
dims_t shape(tensor_rank(output_ort));
tensor_shape(output_ort, reinterpret_cast<int64_t *>(shape.data()));
auto expected =
hrt::create(dt_float32, shape,
{reinterpret_cast<gsl::byte *>(ptr_ort), size}, true,
host_runtime_tensor::pool_cpu_only)
.expect("create tensor failed");

// actual
quant_param_t quantParam;
quantParam.zero_point = zero_point_value;
quantParam.scale = scale_value;
quant_param_t quant_param[] = {quantParam};
auto quant_param_ptr =
hrt::create(dt_int64, {1},
{reinterpret_cast<gsl::byte *>(quant_param),
sizeof(quant_param)},
true, host_runtime_tensor::pool_cpu_only)
.expect("create tensor failed");
auto output = kernels::stackvm::dequantize(dt_float32, input.impl(),
quant_param_ptr.impl())
.expect("dequantize failed");
runtime_tensor actual(output.as<tensor>().expect("as tensor failed"));

bool result = is_same_tensor(expected, actual) ||
cosine_similarity_tensor(expected, actual);

if (!result) {
std::cout << "actual ";
print_runtime_tensor(actual);
std::cout << "expected ";
print_runtime_tensor(expected);
}

// compare
EXPECT_TRUE(result);
} else {

quant_param_t quantParam;
quantParam.zero_point = zero_point_value;
quantParam.scale = scale_value;
quant_param_t quant_param[] = {quantParam};
auto quant_param_ptr =
hrt::create(dt_int64, {1},
{reinterpret_cast<gsl::byte *>(quant_param),
sizeof(quant_param)},
true, host_runtime_tensor::pool_cpu_only)
.expect("create tensor failed");

// expected
auto output1 = kernels::stackvm::dequantize(dt_float32, input.impl(),
quant_param_ptr.impl())
.expect("dequantize failed");
runtime_tensor expected(
output1.as<tensor>().expect("as tensor failed"));

// actual
auto output2 = kernels::stackvm::dequantize(dt_float32, input.impl(),
quant_param_ptr.impl())
.expect("dequantize failed");
runtime_tensor actual(output2.as<tensor>().expect("as tensor failed"));

bool result = is_same_tensor(expected, actual) ||
cosine_similarity_tensor(expected, actual);

if (!result) {
std::cout << "actual ";
print_runtime_tensor(actual);
std::cout << "expected ";
print_runtime_tensor(expected);
}

// compare
EXPECT_TRUE(result);
}
}

int main(int argc, char *argv[]) {
Expand Down
Loading