• 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_APPLY_MOMENTUM_KERNEL_H_
18 #define MINDSPORE_CCSRC_FL_SERVER_KERNEL_APPLY_MOMENTUM_KERNEL_H_
19 
20 #include <vector>
21 #include <memory>
22 #include <utility>
23 #include "backend/kernel_compiler/cpu/apply_momentum_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::ApplyMomentumCPUKernel;
32 template <typename T>
33 class ApplyMomentumKernel : public ApplyMomentumCPUKernel, public OptimizerKernel {
34  public:
35   ApplyMomentumKernel() = default;
36   ~ApplyMomentumKernel() override = default;
37 
InitKernel(const CNodePtr & cnode)38   void InitKernel(const CNodePtr &cnode) override {
39     MS_EXCEPTION_IF_NULL(cnode);
40     ApplyMomentumCPUKernel::InitKernel(cnode);
41     InitServerKernelInputOutputSize(cnode);
42     GenerateReuseKernelNodeInfo();
43   }
44 
Launch(const std::vector<AddressPtr> & inputs,const std::vector<AddressPtr> & workspace,const std::vector<AddressPtr> & outputs)45   bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
46               const std::vector<AddressPtr> &outputs) override {
47     return ApplyMomentumCPUKernel::Launch(inputs, workspace, outputs);
48   }
49 
GenerateReuseKernelNodeInfo()50   void GenerateReuseKernelNodeInfo() override {
51     MS_LOG(INFO) << "FedAvg reuse 'weight', 'accumulation', 'learning rate' and 'momentum' of the kernel node.";
52     reuse_kernel_node_inputs_info_.insert(std::make_pair(kWeight, 0));
53     reuse_kernel_node_inputs_info_.insert(std::make_pair(kAccumulation, 1));
54     reuse_kernel_node_inputs_info_.insert(std::make_pair(kLearningRate, 2));
55     reuse_kernel_node_inputs_info_.insert(std::make_pair(kMomentum, 4));
56     return;
57   }
58 };
59 }  // namespace kernel
60 }  // namespace server
61 }  // namespace fl
62 }  // namespace mindspore
63 #endif  // MINDSPORE_CCSRC_FL_SERVER_KERNEL_APPLY_MOMENTUM_KERNEL_H_
64