// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #include "test_util.h" template class MlasQuantizeLinearTest : public MlasTestBase { private: MatrixGuardBuffer BufferInput; MatrixGuardBuffer BufferOutput; MatrixGuardBuffer BufferOutputReference; void GenerateReference(const float* Input, QuantInt* OutputReference, size_t N, float Scale, QuantInt ZeroPoint) { for (size_t n = 0; n < N; n++) { float FloatValue = std::nearbyintf(Input[n] / Scale) + float(ZeroPoint); FloatValue = std::max(FloatValue, static_cast(std::numeric_limits::min())); FloatValue = std::min(FloatValue, static_cast(std::numeric_limits::max())); OutputReference[n] = static_cast(FloatValue); } } void Test(size_t N) { float* Input = BufferInput.GetBuffer(N); QuantInt* Output = BufferOutput.GetBuffer(N); QuantInt* OutputReference = BufferOutputReference.GetBuffer(N); std::default_random_engine generator(static_cast(N)); std::uniform_real_distribution min_gen(-10.f, -10e-3f); float MinimumValue = min_gen(generator); std::uniform_real_distribution max_gen(10e-3f, 10.f); float MaximumValue = max_gen(generator); float Scale = (MaximumValue - MinimumValue) / 512.f; std::uniform_int_distribution zp_distribution(std::numeric_limits::min(), std::numeric_limits::max()); QuantInt ZeroPoint = static_cast(zp_distribution(generator)); std::uniform_real_distribution distribution(MinimumValue, MaximumValue); for (size_t n = 0; n < N; n++) { Input[n] = distribution(generator); } GenerateReference(Input, OutputReference, N, Scale, ZeroPoint); MlasQuantizeLinear(Input, Output, N, Scale, ZeroPoint); for (size_t n = 0; n < N; n++) { ASSERT_EQ(Output[n], OutputReference[n]) << ", size=" << N << ", index=" << n; } } public: static const char* GetTestSuiteName() { if constexpr (std::is_same_v) { return "QuantizeLinearS8"; } else if (std::is_same_v) { return "QuantizeLinearU8"; } else if (std::is_same_v) { return "QuantizeLinearS16"; } else { return "QuantizeLinearU16"; } } void ExecuteShort(void) override { for (size_t n = 1; n <= 512; n++) { Test(n); } } }; template class MlasQuantizeLinear4BitTest : public MlasTestBase { private: MatrixGuardBuffer BufferInput; MatrixGuardBuffer BufferOutput; MatrixGuardBuffer BufferOutputReference; int32_t MinVal() const { if constexpr (Signed) { return -8; } else { return 0; } } int32_t MaxVal() const { if constexpr (Signed) { return 7; } else { return 15; } } void GenerateReference(const float* Input, uint8_t* OutputReference, size_t N, float Scale, int8_t ZeroPoint) { for (size_t n = 0; n < N; n++) { float FloatValue = std::nearbyintf(Input[n] / Scale) + static_cast(ZeroPoint); FloatValue = std::max(FloatValue, static_cast(MinVal())); FloatValue = std::min(FloatValue, static_cast(MaxVal())); int8_t IntValue = static_cast(FloatValue); size_t i = n >> 1; size_t j = n & 0x1; uint8_t Shift = 4 * static_cast(j); uint8_t Mask = 0xF << Shift; OutputReference[i] &= ~Mask; // Clear 4-bit lane OutputReference[i] |= static_cast((IntValue & 0xF) << Shift); // Set 4-bit lane } } void Test(size_t N) { size_t OutBufLen = (N + 1) / 2; float* Input = BufferInput.GetBuffer(N); uint8_t* Output = BufferOutput.GetBuffer(OutBufLen); uint8_t* OutputReference = BufferOutputReference.GetBuffer(OutBufLen); std::default_random_engine generator(static_cast(N)); std::uniform_real_distribution min_gen(-10.f, -10e-3f); float MinimumValue = min_gen(generator); std::uniform_real_distribution max_gen(10e-3f, 10.f); float MaximumValue = max_gen(generator); float Scale = (MaximumValue - MinimumValue) / 32.f; std::uniform_int_distribution zp_distribution(MinVal(), MaxVal()); int8_t ZeroPoint = static_cast(zp_distribution(generator)); std::uniform_real_distribution distribution(MinimumValue, MaximumValue); for (size_t n = 0; n < N; n++) { Input[n] = distribution(generator); } GenerateReference(Input, OutputReference, N, Scale, ZeroPoint); if constexpr (Signed) { MlasQuantizeLinearS4(Input, Output, N, Scale, ZeroPoint); } else { MlasQuantizeLinearU4(Input, Output, N, Scale, ZeroPoint); } for (size_t n = 0; n < N; n++) { size_t i = n >> 1; size_t j = n & 0x1; const uint8_t Shift = 4 * static_cast(j); int32_t actual_val = (Output[i] >> Shift) & 0xF; int32_t expected_val = (OutputReference[i] >> Shift) & 0xF; if constexpr (Signed) { constexpr uint8_t SignExtShift = (sizeof(int32_t) * 8) - 4; actual_val = (actual_val << SignExtShift) >> SignExtShift; expected_val = (expected_val << SignExtShift) >> SignExtShift; } ASSERT_EQ(actual_val, expected_val) << ", size=" << N << ", index=" << n << ", nibble=" << j; } } public: static const char* GetTestSuiteName() { if constexpr (Signed) { return "QuantizeLinearS4"; } else { return "QuantizeLinearU4"; } } void ExecuteShort(void) override { for (size_t n = 1; n <= 512; n++) { Test(n); } } }; static UNUSED_VARIABLE bool added_to_main = AddTestRegister([](bool is_short_execute) { size_t count = 0; if (is_short_execute) { count += MlasDirectShortExecuteTests>::RegisterShortExecute(); count += MlasDirectShortExecuteTests>::RegisterShortExecute(); count += MlasDirectShortExecuteTests>::RegisterShortExecute(); count += MlasDirectShortExecuteTests>::RegisterShortExecute(); count += MlasDirectShortExecuteTests>::RegisterShortExecute(); count += MlasDirectShortExecuteTests>::RegisterShortExecute(); } return count; });