1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <vector>
18 #include "common/common_test.h"
19 #define private public
20 #define protected public
21 #include "backend/kernel_compiler/cpu/unique_cpu_kernel.h"
22 #undef private
23 #undef protected
24
25 namespace mindspore {
26 namespace kernel {
27 class UniqueCpuKernelTest : public UT::Common {
28 public:
UniqueCpuKernelTest()29 UniqueCpuKernelTest() : unique_(std::make_shared<UniqueCPUKernel>()) {}
30
SetUp()31 void SetUp() override {
32 unique_->input_size_ = 9;
33 unique_->dtype_ = kNumberTypeFloat32;
34 inputs_.clear();
35 workspace_.clear();
36 outputs_.clear();
37 }
38
CreateKernelAddress(void * addr)39 AddressPtr CreateKernelAddress(void *addr) {
40 auto kernel_addr = std::make_shared<Address>();
41 kernel_addr->addr = addr;
42 return kernel_addr;
43 }
44
CreateAddress()45 void CreateAddress() {
46 inputs_.push_back(CreateKernelAddress(x_.data()));
47 outputs_.push_back(CreateKernelAddress(y_.data()));
48 outputs_.push_back(CreateKernelAddress(idx_.data()));
49 workspace_.push_back(CreateKernelAddress(workspace_idx_.data()));
50 workspace_.push_back(CreateKernelAddress(workspace_idx_.data()));
51 workspace_.push_back(CreateKernelAddress(workspace_idx_.data()));
52 }
53
54 std::vector<float> x_;
55 std::vector<float> y_;
56 std::vector<int> idx_;
57 std::vector<int64_t> workspace_idx_;
58 std::vector<AddressPtr> inputs_;
59 std::vector<AddressPtr> workspace_;
60 std::vector<AddressPtr> outputs_;
61 std::shared_ptr<UniqueCPUKernel> unique_;
62 };
63
TEST_F(UniqueCpuKernelTest,compute_test)64 TEST_F(UniqueCpuKernelTest, compute_test) {
65 x_ = {1, 1, 2, 4, 4, 4, 7, 8, 8};
66 y_ = {1, 1, 1, 1, 1};
67 idx_ = {1, 1, 1, 1, 1, 1, 1, 1, 1};
68 workspace_idx_ = {1, 1, 1, 1, 1, 1, 1, 1, 1};
69 CreateAddress();
70 unique_->Launch(inputs_, workspace_, outputs_);
71
72 // check compute result
73 std::vector<float> expect_y{1, 2, 4, 7, 8};
74 std::vector<int> expect_idx{0, 0, 1, 2, 2, 2, 3, 4, 4};
75 EXPECT_TRUE(y_ == expect_y);
76 EXPECT_TRUE(idx_ == expect_idx);
77 }
78 } // namespace kernel
79 } // namespace mindspore
80