Skip to content

Commit 5ea0385

Browse files
Added HashtableLookup Op
Added HashtableLookup Op and unit test Type: New Feature Signed-off-by: Feiyue Chen <Feiyue.Chen@verisilicon.com>
1 parent c231c54 commit 5ea0385

File tree

5 files changed

+179
-1
lines changed

5 files changed

+179
-1
lines changed

include/tim/vx/ops.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,6 @@
9898
#include "tim/vx/ops/custom_base.h"
9999
#include "tim/vx/ops/topk.h"
100100
#include "tim/vx/ops/bidirectional_sequence_lstm.h"
101+
#include "tim/vx/ops/hashtable_lookup.h"
101102

102103
#endif /* TIM_VX_OPS_H_ */
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (c) 2022 Vivante Corporation
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a
6+
* copy of this software and associated documentation files (the "Software"),
7+
* to deal in the Software without restriction, including without limitation
8+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
* and/or sell copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21+
* DEALINGS IN THE SOFTWARE.
22+
*
23+
*****************************************************************************/
24+
#ifndef TIM_VX_OPS_HASHTABLE_LOOKUP_H_
25+
#define TIM_VX_OPS_HASHTABLE_LOOKUP_H_
26+
27+
#include "tim/vx/builtin_op.h"
28+
29+
namespace tim{
30+
namespace vx{
31+
namespace ops{
32+
33+
/**
34+
* ## HashtableLookup
35+
*
36+
* Looks up sub-tensors in the input tensor using a key-value map.
37+
*/
38+
class HashtableLookup : public BuiltinOp {
39+
public:
40+
HashtableLookup(Graph* graph);
41+
42+
std::shared_ptr<Operation> Clone(std::shared_ptr<Graph>& graph) const override;
43+
};
44+
45+
} // namespace ops
46+
} // namespace vx
47+
} // namespace tim
48+
49+
#endif /* TIM_VX_OPS_HASHTABLE_LOOKUP_H_ */

src/tim/vx/ops/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ MaxPool3d|MAX_POOL3D|Mapped|[Onnx.MaxPool](https://github.com/onnx/onnx/blob/mai
127127
|UnidirectionalSequenceLSTM|LSTM_OVXLIB|Mapped|[ANEURALNETWORKS_UNIDIRECTIONAL_SEQUENCE_LSTM](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0aaf30e491ad0b1fc7602cbde695b2c859)
128128
|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)
129129
||PRE_PROCESS|TBD |Image Preprocessing (YUV2RGB, Input Normalization, Resizing, etc)
130-
||HASHTABLE_LOOKUP|Planned 22Q4|[ANEURALNETWORKS_HASHTABLE_LOOKUP](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0aca92716c8c73c1f0fa7f0757916fee26)
130+
|HashtableLookup|HASHTABLE_LOOKUP|Mapped|[ANEURALNETWORKS_HASHTABLE_LOOKUP](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0aca92716c8c73c1f0fa7f0757916fee26)
131131
||EMBEDDING_LOOKUP|Planned 22Q4|[ANEURALNETWORKS_EMBEDDING_LOOKUP](developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0a8d2ada77adb74357fc0770405bca0e3)
132132
||LSH_PROJECTION|Planned 22Q4|[ANEURALNETWORKS_LSH_PROJECTION](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0a800cdcec5d7ba776789cb2d1ef669965)
133133
||HEATMAP_MAX_KEYPOINT|TBD|[ANEURALNETWORKS_HEATMAP_MAX_KEYPOINT](https://developer.android.com/ndk/reference/group/neural-networks#group___neural_networks_1ggaabbe492c60331b13038e39d4207940e0a5ffccf92d127766a741225ff7ad6f743)

src/tim/vx/ops/hashtable_lookup.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (c) 2022 Vivante Corporation
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a
6+
* copy of this software and associated documentation files (the "Software"),
7+
* to deal in the Software without restriction, including without limitation
8+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
* and/or sell copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21+
* DEALINGS IN THE SOFTWARE.
22+
*
23+
*****************************************************************************/
24+
#include "tim/vx/ops/hashtable_lookup.h"
25+
26+
#include "builtin_op_impl.h"
27+
#include "vsi_nn_pub.h"
28+
29+
namespace tim{
30+
namespace vx{
31+
namespace ops{
32+
33+
HashtableLookup::HashtableLookup(Graph* graph)
34+
: BuiltinOp (graph, VSI_NN_OP_HASHTABLE_LOOKUP, 3, 2){}
35+
36+
std::shared_ptr<Operation> HashtableLookup::Clone(
37+
std::shared_ptr<Graph>& graph) const {
38+
return graph->CreateOperation<HashtableLookup>();
39+
}
40+
41+
}
42+
}
43+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (c) 2022 Vivante Corporation
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a
6+
* copy of this software and associated documentation files (the "Software"),
7+
* to deal in the Software without restriction, including without limitation
8+
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
9+
* and/or sell copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21+
* DEALINGS IN THE SOFTWARE.
22+
*
23+
*****************************************************************************/
24+
#include "tim/vx/context.h"
25+
#include "tim/vx/graph.h"
26+
#include "tim/vx/ops/hashtable_lookup.h"
27+
28+
#include "gtest/gtest.h"
29+
TEST(HashtableLookup, int32) {
30+
auto ctx = tim::vx::Context::Create();
31+
auto graph = ctx->CreateGraph();
32+
33+
tim::vx::ShapeType in_shape({2});
34+
tim::vx::ShapeType key_shape({4});
35+
tim::vx::ShapeType value_shape({4});
36+
tim::vx::ShapeType out_shape({2});;
37+
tim::vx::ShapeType hit_shape({2});
38+
39+
tim::vx::TensorSpec in_spec(tim::vx::DataType::INT32,
40+
in_shape, tim::vx::TensorAttribute::INPUT);
41+
tim::vx::TensorSpec key_spec(tim::vx::DataType::INT32,
42+
key_shape, tim::vx::TensorAttribute::INPUT);
43+
tim::vx::TensorSpec value_spec(tim::vx::DataType::INT32,
44+
value_shape, tim::vx::TensorAttribute::INPUT);
45+
tim::vx::TensorSpec out_spec(tim::vx::DataType::FLOAT16,
46+
out_shape, tim::vx::TensorAttribute::OUTPUT);
47+
tim::vx::TensorSpec hit_spec(tim::vx::DataType::INT32,
48+
hit_shape, tim::vx::TensorAttribute::OUTPUT);
49+
auto in_tensor = graph->CreateTensor(in_spec);
50+
auto key_tensor = graph->CreateTensor(key_spec);
51+
auto value_tensor = graph->CreateTensor(value_spec);
52+
auto out_tensor = graph->CreateTensor(out_spec);
53+
auto hit_tensor = graph->CreateTensor(hit_spec);
54+
55+
std::vector<int32_t> in_data = {
56+
10, 66
57+
};
58+
std::vector<int32_t> key_data = {
59+
10,20,30,40
60+
};
61+
std::vector<int32_t> value_data = {
62+
7,8,9,10
63+
};
64+
std::vector<float> out_golden = {7,0};
65+
std::vector<int32_t> hit_golden = {1,0};//0 means not hit
66+
67+
EXPECT_TRUE(in_tensor->CopyDataToTensor(in_data.data(), in_data.size() * sizeof(int32_t)));
68+
EXPECT_TRUE(key_tensor->CopyDataToTensor(key_data.data(), key_data.size() * sizeof(int32_t)));
69+
EXPECT_TRUE(value_tensor->CopyDataToTensor(value_data.data(), value_data.size() * sizeof(int32_t)));
70+
EXPECT_TRUE(out_tensor->CopyDataToTensor(out_golden.data(), out_golden.size() * sizeof(float)));
71+
EXPECT_TRUE(hit_tensor->CopyDataToTensor(hit_golden.data(), hit_golden.size() * sizeof(int32_t)));
72+
73+
auto op = graph->CreateOperation<tim::vx::ops::HashtableLookup>();
74+
(*op).BindInputs({in_tensor, key_tensor, value_tensor}).BindOutputs({out_tensor, hit_tensor});
75+
76+
EXPECT_TRUE(graph->Compile());
77+
EXPECT_TRUE(graph->Run());
78+
79+
std::vector<float> output(out_golden.size());
80+
std::vector<int32_t> hit(hit_golden.size());
81+
EXPECT_TRUE(out_tensor->CopyDataFromTensor(output.data()));
82+
EXPECT_TRUE(hit_tensor->CopyDataFromTensor(hit.data()));
83+
EXPECT_EQ(out_golden, output);
84+
EXPECT_EQ(hit_golden, hit);
85+
}

0 commit comments

Comments
 (0)