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