You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sgd_kernel.h 2.3 kB

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