• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 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 #ifndef MINDSPORE_CCSRC_FL_SERVER_KERNEL_SGD_KERNEL_H_
18 #define MINDSPORE_CCSRC_FL_SERVER_KERNEL_SGD_KERNEL_H_
19 
20 #include <vector>
21 #include <memory>
22 #include <utility>
23 #include "backend/kernel_compiler/cpu/sgd_cpu_kernel.h"
24 #include "fl/server/kernel/optimizer_kernel.h"
25 #include "fl/server/kernel/optimizer_kernel_factory.h"
26 
27 namespace mindspore {
28 namespace fl {
29 namespace server {
30 namespace kernel {
31 using mindspore::kernel::SGDCPUKernel;
32 template <typename T>
33 class SGDKernel : public SGDCPUKernel<T>, public OptimizerKernel {
34  public:
35   SGDKernel() = default;
36   ~SGDKernel() override = default;
37 
InitKernel(const CNodePtr & cnode)38   void InitKernel(const CNodePtr &cnode) override {
39     SGDCPUKernel<T>::InitKernel(cnode);
40     InitServerKernelInputOutputSize(cnode);
41     GenerateReuseKernelNodeInfo();
42   }
43 
Launch(const std::vector<AddressPtr> & inputs,const std::vector<AddressPtr> & workspace,const std::vector<AddressPtr> & outputs)44   bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
45               const std::vector<AddressPtr> &outputs) override {
46     return SGDCPUKernel<T>::Launch(inputs, workspace, outputs);
47   }
48 
GenerateReuseKernelNodeInfo()49   void GenerateReuseKernelNodeInfo() override {
50     MS_LOG(INFO) << "SGD reuse 'weight', 'learning rate', 'accumulation', 'momentum' and 'stat' of the kernel node.";
51     reuse_kernel_node_inputs_info_.insert(std::make_pair(kWeight, 0));
52     reuse_kernel_node_inputs_info_.insert(std::make_pair(kLearningRate, 2));
53     reuse_kernel_node_inputs_info_.insert(std::make_pair(kAccumulation, 3));
54     reuse_kernel_node_inputs_info_.insert(std::make_pair(kMomentum, 4));
55     reuse_kernel_node_inputs_info_.insert(std::make_pair(kStat, 5));
56     return;
57   }
58 };
59 }  // namespace kernel
60 }  // namespace server
61 }  // namespace fl
62 }  // namespace mindspore
63 #endif  // MINDSPORE_CCSRC_FL_SERVER_KERNEL_SGD_KERNEL_H_
64