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.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Copyright 2021-2022 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 "plugin/device/cpu/kernel/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::SGDCpuKernelMod;
  29. template <typename T>
  30. class SGDKernelMod : public SGDCpuKernelMod<T>, public OptimizerKernelMod {
  31. public:
  32. SGDKernelMod() = default;
  33. ~SGDKernelMod() override = default;
  34. void InitKernel(const CNodePtr &cnode) override {
  35. SGDCpuKernelMod<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 SGDCpuKernelMod<T>::Launch(inputs, workspace, outputs);
  42. }
  43. void GenerateReuseKernelNodeInfo() override {
  44. constexpr int kWeightIndex = 0;
  45. constexpr int kLearningRateIndex = 2;
  46. constexpr int kAccumulationIndex = 3;
  47. constexpr int kMomentumIndex = 4;
  48. constexpr int kStatIndex = 5;
  49. MS_LOG(INFO) << "SGD reuse 'weight', 'learning rate', 'accumulation', 'momentum' and 'stat' of the kernel node.";
  50. reuse_kernel_node_inputs_info_.insert(std::make_pair(kWeight, kWeightIndex));
  51. reuse_kernel_node_inputs_info_.insert(std::make_pair(kLearningRate, kLearningRateIndex));
  52. reuse_kernel_node_inputs_info_.insert(std::make_pair(kAccumulation, kAccumulationIndex));
  53. reuse_kernel_node_inputs_info_.insert(std::make_pair(kMomentum, kMomentumIndex));
  54. reuse_kernel_node_inputs_info_.insert(std::make_pair(kStat, kStatIndex));
  55. return;
  56. }
  57. };
  58. } // namespace kernel
  59. } // namespace server
  60. } // namespace fl
  61. } // namespace mindspore
  62. #endif // MINDSPORE_CCSRC_FL_SERVER_KERNEL_SGD_KERNEL_H_