From 789d4458ffd762e60335b4be7a2570babb29d6f7 Mon Sep 17 00:00:00 2001 From: Feiyue Chen Date: Wed, 14 Dec 2022 10:12:01 +0800 Subject: [PATCH] Added 4d quantized LUT unittest for embedding_lookup Type: New Feature | Bug Fix | Code Improvement | Documentation Issue: bugzilla id | jira id #No more newline after this Signed-off-by: Feiyue Chen --- src/tim/vx/ops/embedding_lookup_test.cc | 52 ++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/tim/vx/ops/embedding_lookup_test.cc b/src/tim/vx/ops/embedding_lookup_test.cc index 4683dc7..983df96 100644 --- a/src/tim/vx/ops/embedding_lookup_test.cc +++ b/src/tim/vx/ops/embedding_lookup_test.cc @@ -24,10 +24,11 @@ #include "tim/vx/context.h" #include "tim/vx/graph.h" #include "tim/vx/ops/embedding_lookup.h" - +#include "test_utils.h" #include "gtest/gtest.h" -TEST(EmbeddingLookup, shape_2_5_int32) { + +TEST(EmbeddingLookup, shape_2_5_int32LUT) { auto ctx = tim::vx::Context::Create(); auto graph = ctx->CreateGraph(); tim::vx::ShapeType idx_shape({3}); @@ -63,4 +64,51 @@ TEST(EmbeddingLookup, shape_2_5_int32) { std::vector output(golden.size()); EXPECT_TRUE(out_tensor->CopyDataFromTensor(output.data())); EXPECT_EQ(golden, output); +} + +TEST(EmbeddingLookup, shape_2_2_2_3_Uint8QuantizedLUT) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + tim::vx::ShapeType idx_shape({3}); + tim::vx::ShapeType lut_shape({2, 2, 2, 3}); + tim::vx::ShapeType out_shape({2, 2, 2, 3}); + tim::vx::Quantization quant_lut(tim::vx::QuantType::ASYMMETRIC, + 0.0167716537, 0); + + tim::vx::TensorSpec idx_spec(tim::vx::DataType::INT32, idx_shape, + tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec lut_spec(tim::vx::DataType::UINT8, lut_shape, + tim::vx::TensorAttribute::INPUT, quant_lut); + tim::vx::TensorSpec out_spec(tim::vx::DataType::FLOAT32, out_shape, + tim::vx::TensorAttribute::OUTPUT); + auto idx_tensor = graph->CreateTensor(idx_spec); + auto lut_tensor = graph->CreateTensor(lut_spec); + auto out_tensor = graph->CreateTensor(out_spec); + + std::vector idx_data = {1,0,2}; + std::vector lut_data = + { + 0, 1, 1, 2, 6, 7, 7, 8, // Row 0 + 60, 60, 61, 61,66, 66, 67, 67, // Row 1 + 119, 120,120, 121, 125, 126, 126, 127, // Row 2 + }; + + std::vector golden = + {1.00, 1.01, 1.02, 1.03, 1.10, 1.11, 1.12, 1.13, // Row 1 + 0.00, 0.01, 0.02, 0.03, 0.10, 0.11, 0.12, 0.13, // Row 0 + 2.00, 2.01, 2.02, 2.03, 2.10, 2.11, 2.12, 2.13, // Row 2 + }; + EXPECT_TRUE(idx_tensor->CopyDataToTensor(idx_data.data(), + idx_data.size() * sizeof(int32_t))); + EXPECT_TRUE(lut_tensor->CopyDataToTensor(lut_data.data(), + lut_data.size() * sizeof(uint8_t))); + auto op = graph->CreateOperation(); + (*op).BindInputs({idx_tensor,lut_tensor}).BindOutput(out_tensor); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + + std::vector output(golden.size()); + EXPECT_TRUE(out_tensor->CopyDataFromTensor(output.data())); + EXPECT_TRUE(ArraysMatch(golden, output, (float)7.41e-03)); } \ No newline at end of file