• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/sparse_apply_ftrl_cpu_kernel.h"
22 #undef private
23 #undef protected
24 
25 namespace mindspore {
26 namespace kernel {
27 class SparseApplyFtrlCpuKernelTest : public UT::Common {
28  public:
SparseApplyFtrlCpuKernelTest()29   SparseApplyFtrlCpuKernelTest() : sparse_ftrl_(std::make_shared<SparseApplyFtrlCPUKernel>()) {}
30 
SetUp()31   void SetUp() override {
32     sparse_ftrl_->lr_ = 0.001;
33     sparse_ftrl_->l1_ = 0.0;
34     sparse_ftrl_->l2_ = 0.0;
35     sparse_ftrl_->lr_power_ = -0.5;
36     var_.clear();
37     accum_.clear();
38     linear_.clear();
39     grad_.clear();
40     inputs_.clear();
41     workspace_.clear();
42     outputs_.clear();
43   }
44 
CreateKernelAddress(void * addr)45   AddressPtr CreateKernelAddress(void *addr) {
46     auto kernel_addr = std::make_shared<Address>();
47     kernel_addr->addr = addr;
48     return kernel_addr;
49   }
50 
CreateInputAddress(std::vector<int64_t> & indices)51   void CreateInputAddress(std::vector<int64_t> &indices) {
52     inputs_.push_back(CreateKernelAddress(var_.data()));
53     inputs_.push_back(CreateKernelAddress(accum_.data()));
54     inputs_.push_back(CreateKernelAddress(linear_.data()));
55     inputs_.push_back(CreateKernelAddress(grad_.data()));
56     inputs_.push_back(CreateKernelAddress(indices.data()));
57   }
58 
CreateWorkspaceAddress(std::vector<float> & new_grad,std::vector<int64_t> & new_indices,std::vector<float> & tmp_grad,std::vector<int64_t> & tmp_indices)59   void CreateWorkspaceAddress(std::vector<float> &new_grad, std::vector<int64_t> &new_indices,
60                               std::vector<float> &tmp_grad, std::vector<int64_t> &tmp_indices) {
61     workspace_.push_back(CreateKernelAddress(new_grad.data()));
62     workspace_.push_back(CreateKernelAddress(new_indices.data()));
63     workspace_.push_back(CreateKernelAddress(tmp_grad.data()));
64     workspace_.push_back(CreateKernelAddress(tmp_indices.data()));
65   }
66 
67   std::vector<float> var_;
68   std::vector<float> accum_;
69   std::vector<float> linear_;
70   std::vector<float> grad_;
71   std::vector<AddressPtr> inputs_;
72   std::vector<AddressPtr> workspace_;
73   std::vector<AddressPtr> outputs_;
74   std::shared_ptr<SparseApplyFtrlCPUKernel> sparse_ftrl_;
75 };
76 
TEST_F(SparseApplyFtrlCpuKernelTest,dense_test)77 TEST_F(SparseApplyFtrlCpuKernelTest, dense_test) {
78   for (size_t i = 0; i < 3 * 3 * 3; ++i) {
79     var_.push_back(1.0);
80     accum_.push_back(1.0);
81     linear_.push_back(1.0);
82     grad_.push_back(1.0);
83   }
84   sparse_ftrl_->indices_size_ = 3;
85   sparse_ftrl_->var_first_dim_size_ = 3;
86   sparse_ftrl_->var_outer_dim_size_ = 9;
87   sparse_ftrl_->indices_data_type_ = kNumberTypeInt64;
88 
89   std::vector<int64_t> indices{0, 1, 2};
90   CreateInputAddress(indices);
91   std::vector<float> new_grad(3 * 3 * 3);
92   std::vector<int64_t> new_indices(3);
93   std::vector<float> tmp_grad(3 * 3 * 3);
94   std::vector<int64_t> tmp_indices(3);
95   CreateWorkspaceAddress(new_grad, new_indices, tmp_grad, tmp_indices);
96   sparse_ftrl_->Launch(inputs_, workspace_, outputs_);
97   for (size_t i = 0; i < 3 * 3 * 3; ++i) {
98     EXPECT_TRUE(std::fabs(var_[i] - 0.291479) < 1e-6);
99   }
100 }
101 
TEST_F(SparseApplyFtrlCpuKernelTest,sparse_test1)102 TEST_F(SparseApplyFtrlCpuKernelTest, sparse_test1) {
103   for (size_t i = 0; i < 3 * 3 * 3; ++i) {
104     var_.push_back(1.0);
105     accum_.push_back(1.0);
106     linear_.push_back(1.0);
107   }
108   for (size_t i = 0; i < 2 * 3 * 3; ++i) {
109     grad_.push_back(1.0);
110   }
111   sparse_ftrl_->indices_size_ = 2;
112   sparse_ftrl_->var_first_dim_size_ = 3;
113   sparse_ftrl_->var_outer_dim_size_ = 9;
114   sparse_ftrl_->indices_data_type_ = kNumberTypeInt64;
115 
116   std::vector<int64_t> indices{0, 2};
117   CreateInputAddress(indices);
118   std::vector<float> new_grad(3 * 3 * 3);
119   std::vector<int64_t> new_indices(3);
120   std::vector<float> tmp_grad(3 * 3 * 3);
121   std::vector<int64_t> tmp_indices(3);
122   CreateWorkspaceAddress(new_grad, new_indices, tmp_grad, tmp_indices);
123   sparse_ftrl_->Launch(inputs_, workspace_, outputs_);
124   for (size_t i = 0; i < 3 * 3; ++i) {
125     EXPECT_TRUE(std::fabs(var_[i] - 0.291479) < 1e-6);
126   }
127   for (size_t i = 3 * 3; i < 2 * 3 * 3; ++i) {
128     EXPECT_EQ(var_[i], 1.0);
129   }
130   for (size_t i = 2 * 3 * 3; i < 3 * 3 * 3; ++i) {
131     EXPECT_TRUE(std::fabs(var_[i] - 0.291479) < 1e-6);
132   }
133 }
134 
TEST_F(SparseApplyFtrlCpuKernelTest,sparse_test2)135 TEST_F(SparseApplyFtrlCpuKernelTest, sparse_test2) {
136   for (size_t i = 0; i < 3 * 3 * 3; ++i) {
137     var_.push_back(1.0);
138     accum_.push_back(1.0);
139     linear_.push_back(1.0);
140     grad_.push_back(1.0);
141   }
142   sparse_ftrl_->indices_size_ = 3;
143   sparse_ftrl_->var_first_dim_size_ = 3;
144   sparse_ftrl_->var_outer_dim_size_ = 9;
145   sparse_ftrl_->indices_data_type_ = kNumberTypeInt64;
146 
147   std::vector<int64_t> indices{2, 2, 1};
148   CreateInputAddress(indices);
149   std::vector<float> new_grad(3 * 3 * 3);
150   std::vector<int64_t> new_indices(3);
151   std::vector<float> tmp_grad(3 * 3 * 3);
152   std::vector<int64_t> tmp_indices(3);
153   CreateWorkspaceAddress(new_grad, new_indices, tmp_grad, tmp_indices);
154   sparse_ftrl_->Launch(inputs_, workspace_, outputs_);
155   for (size_t i = 0; i < 3 * 3; ++i) {
156     EXPECT_EQ(var_[i], 1.0);
157   }
158   for (size_t i = 3 * 3; i < 2 * 3 * 3; ++i) {
159     EXPECT_TRUE(std::fabs(var_[i] - 0.291479) < 1e-6);
160   }
161   for (size_t i = 2 * 3 * 3; i < 3 * 3 * 3; ++i) {
162     EXPECT_TRUE(std::fabs(var_[i] - 0.551445) < 1e-6);
163   }
164 }
165 }  // namespace kernel
166 }  // namespace mindspore
167