Added HashtableLookup Op

Added HashtableLookup Op and unit test

Type: New Feature
Signed-off-by: Feiyue Chen <Feiyue.Chen@verisilicon.com>
This commit is contained in:
Feiyue Chen 2022-11-30 14:42:29 +08:00 committed by Sven
parent c231c54a66
commit dd7cd2504c
5 changed files with 179 additions and 1 deletions

View File

@ -98,5 +98,6 @@
#include "tim/vx/ops/custom_base.h" #include "tim/vx/ops/custom_base.h"
#include "tim/vx/ops/topk.h" #include "tim/vx/ops/topk.h"
#include "tim/vx/ops/bidirectional_sequence_lstm.h" #include "tim/vx/ops/bidirectional_sequence_lstm.h"
#include "tim/vx/ops/hashtable_lookup.h"
#endif /* TIM_VX_OPS_H_ */ #endif /* TIM_VX_OPS_H_ */

View File

@ -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<Operation> Clone(std::shared_ptr<Graph>& graph) const override;
};
} // namespace ops
} // namespace vx
} // namespace tim
#endif /* TIM_VX_OPS_HASHTABLE_LOOKUP_H_ */

View File

@ -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) |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) |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) ||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) ||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) ||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) ||HEATMAP_MAX_KEYPOINT|TBD|[ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0a5ffccf92d127766a741225ff7ad6f743)

View File

@ -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<Operation> HashtableLookup::Clone(
std::shared_ptr<Graph>& graph) const {
return graph->CreateOperation<HashtableLookup>();
}
}
}
}

View File

@ -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<int32_t> in_data = {
10, 66
};
std::vector<int32_t> key_data = {
10,20,30,40
};
std::vector<int32_t> value_data = {
7,8,9,10
};
std::vector<float> out_golden = {7,0};
std::vector<int32_t> 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<tim::vx::ops::HashtableLookup>();
(*op).BindInputs({in_tensor, key_tensor, value_tensor}).BindOutputs({out_tensor, hit_tensor});
EXPECT_TRUE(graph->Compile());
EXPECT_TRUE(graph->Run());
std::vector<float> output(out_golden.size());
std::vector<int32_t> 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);
}