diff --git a/include/tim/vx/ops.h b/include/tim/vx/ops.h index d8da63b..f09d00f 100644 --- a/include/tim/vx/ops.h +++ b/include/tim/vx/ops.h @@ -98,5 +98,6 @@ #include "tim/vx/ops/custom_base.h" #include "tim/vx/ops/topk.h" #include "tim/vx/ops/bidirectional_sequence_lstm.h" +#include "tim/vx/ops/hashtable_lookup.h" #endif /* TIM_VX_OPS_H_ */ diff --git a/include/tim/vx/ops/hashtable_lookup.h b/include/tim/vx/ops/hashtable_lookup.h new file mode 100644 index 0000000..f8ba35f --- /dev/null +++ b/include/tim/vx/ops/hashtable_lookup.h @@ -0,0 +1,49 @@ +/**************************************************************************** +* +* Copyright (c) 2022 Vivante Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +* DEALINGS IN THE SOFTWARE. +* +*****************************************************************************/ +#ifndef TIM_VX_OPS_HASHTABLE_LOOKUP_H_ +#define TIM_VX_OPS_HASHTABLE_LOOKUP_H_ + +#include "tim/vx/builtin_op.h" + +namespace tim{ +namespace vx{ +namespace ops{ + +/** + * ## HashtableLookup + * + * Looks up sub-tensors in the input tensor using a key-value map. + */ +class HashtableLookup : public BuiltinOp { + public: + HashtableLookup(Graph* graph); + + std::shared_ptr Clone(std::shared_ptr& graph) const override; +}; + +} // namespace ops +} // namespace vx +} // namespace tim + +#endif /* TIM_VX_OPS_HASHTABLE_LOOKUP_H_ */ diff --git a/src/tim/vx/ops/README.md b/src/tim/vx/ops/README.md index bc6249c..05af286 100644 --- a/src/tim/vx/ops/README.md +++ b/src/tim/vx/ops/README.md @@ -127,7 +127,7 @@ MaxPool3d|MAX_POOL3D|Mapped|[Onnx.MaxPool](https://github.com/onnx/onnx/blob/mai |UnidirectionalSequenceLSTM|LSTM_OVXLIB|Mapped|[ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0aaf30e491ad0b1fc7602cbde695b2c859) |LSTMCell|LSTMUNIT_OVXLIB|replace with UnidirectionalSequenceLSTM by set n_step = 1 |[ANEURALNETWORKS_LSTM](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0ad0377e8c305e596fb7f64ff896671fc5) ||PRE_PROCESS|TBD |Image Preprocessing (YUV2RGB, Input Normalization, Resizing, etc) -||HASHTABLE_LOOKUP|Planned 22Q4|[ANEURALNETWORKS_HASHTABLE_LOOKUP](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0aca92716c8c73c1f0fa7f0757916fee26) +|HashtableLookup|HASHTABLE_LOOKUP|Mapped|[ANEURALNETWORKS_HASHTABLE_LOOKUP](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0aca92716c8c73c1f0fa7f0757916fee26) ||EMBEDDING_LOOKUP|Planned 22Q4|[ANEURALNETWORKS_EMBEDDING_LOOKUP](developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0a8d2ada77adb74357fc0770405bca0e3) ||LSH_PROJECTION|Planned 22Q4|[ANEURALNETWORKS_LSH_PROJECTION](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0a800cdcec5d7ba776789cb2d1ef669965) ||HEATMAP_MAX_KEYPOINT|TBD|[ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0a5ffccf92d127766a741225ff7ad6f743) diff --git a/src/tim/vx/ops/hashtable_lookup.cc b/src/tim/vx/ops/hashtable_lookup.cc new file mode 100644 index 0000000..4f0fe93 --- /dev/null +++ b/src/tim/vx/ops/hashtable_lookup.cc @@ -0,0 +1,43 @@ +/**************************************************************************** +* +* Copyright (c) 2022 Vivante Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +* DEALINGS IN THE SOFTWARE. +* +*****************************************************************************/ +#include "tim/vx/ops/hashtable_lookup.h" + +#include "builtin_op_impl.h" +#include "vsi_nn_pub.h" + +namespace tim{ +namespace vx{ +namespace ops{ + + HashtableLookup::HashtableLookup(Graph* graph) + : BuiltinOp (graph, VSI_NN_OP_HASHTABLE_LOOKUP, 3, 2){} + + std::shared_ptr HashtableLookup::Clone( + std::shared_ptr& graph) const { + return graph->CreateOperation(); +} + +} +} +} \ No newline at end of file diff --git a/src/tim/vx/ops/hashtable_lookup_test.cc b/src/tim/vx/ops/hashtable_lookup_test.cc new file mode 100644 index 0000000..e743da9 --- /dev/null +++ b/src/tim/vx/ops/hashtable_lookup_test.cc @@ -0,0 +1,85 @@ +/**************************************************************************** +* +* Copyright (c) 2022 Vivante Corporation +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +* DEALINGS IN THE SOFTWARE. +* +*****************************************************************************/ +#include "tim/vx/context.h" +#include "tim/vx/graph.h" +#include "tim/vx/ops/hashtable_lookup.h" + +#include "gtest/gtest.h" +TEST(HashtableLookup, int32) { + auto ctx = tim::vx::Context::Create(); + auto graph = ctx->CreateGraph(); + + tim::vx::ShapeType in_shape({2}); + tim::vx::ShapeType key_shape({4}); + tim::vx::ShapeType value_shape({4}); + tim::vx::ShapeType out_shape({2});; + tim::vx::ShapeType hit_shape({2}); + + tim::vx::TensorSpec in_spec(tim::vx::DataType::INT32, + in_shape, tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec key_spec(tim::vx::DataType::INT32, + key_shape, tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec value_spec(tim::vx::DataType::INT32, + value_shape, tim::vx::TensorAttribute::INPUT); + tim::vx::TensorSpec out_spec(tim::vx::DataType::FLOAT16, + out_shape, tim::vx::TensorAttribute::OUTPUT); + tim::vx::TensorSpec hit_spec(tim::vx::DataType::INT32, + hit_shape, tim::vx::TensorAttribute::OUTPUT); + auto in_tensor = graph->CreateTensor(in_spec); + auto key_tensor = graph->CreateTensor(key_spec); + auto value_tensor = graph->CreateTensor(value_spec); + auto out_tensor = graph->CreateTensor(out_spec); + auto hit_tensor = graph->CreateTensor(hit_spec); + + std::vector in_data = { + 10, 66 + }; + std::vector key_data = { + 10,20,30,40 + }; + std::vector value_data = { + 7,8,9,10 + }; + std::vector out_golden = {7,0}; + std::vector hit_golden = {1,0};//0 means not hit + + EXPECT_TRUE(in_tensor->CopyDataToTensor(in_data.data(), in_data.size() * sizeof(int32_t))); + EXPECT_TRUE(key_tensor->CopyDataToTensor(key_data.data(), key_data.size() * sizeof(int32_t))); + EXPECT_TRUE(value_tensor->CopyDataToTensor(value_data.data(), value_data.size() * sizeof(int32_t))); + EXPECT_TRUE(out_tensor->CopyDataToTensor(out_golden.data(), out_golden.size() * sizeof(float))); + EXPECT_TRUE(hit_tensor->CopyDataToTensor(hit_golden.data(), hit_golden.size() * sizeof(int32_t))); + + auto op = graph->CreateOperation(); + (*op).BindInputs({in_tensor, key_tensor, value_tensor}).BindOutputs({out_tensor, hit_tensor}); + + EXPECT_TRUE(graph->Compile()); + EXPECT_TRUE(graph->Run()); + + std::vector output(out_golden.size()); + std::vector hit(hit_golden.size()); + EXPECT_TRUE(out_tensor->CopyDataFromTensor(output.data())); + EXPECT_TRUE(hit_tensor->CopyDataFromTensor(hit.data())); + EXPECT_EQ(out_golden, output); + EXPECT_EQ(hit_golden, hit); +} \ No newline at end of file