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.

aggregation_kernel.h 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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_AGGREGATION_KERNEL_H_
  17. #define MINDSPORE_CCSRC_FL_SERVER_KERNEL_AGGREGATION_KERNEL_H_
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include "plugin/device/cpu/kernel/cpu_kernel.h"
  22. #include "fl/server/common.h"
  23. #include "fl/server/memory_register.h"
  24. #include "fl/server/kernel/params_info.h"
  25. namespace mindspore {
  26. namespace fl {
  27. namespace server {
  28. namespace kernel {
  29. // AggregationKernelMod is the kernel for weight, grad or other kinds of parameters' aggregation.
  30. // For example, dense gradients accumulation, federated average, etc.
  31. // Normally the aggregation process in AggregationKernelMod is like a finite-state machine:
  32. // Initial->Aggregating->Aggregation done->Initial.
  33. class AggregationKernelMod : public NativeCpuKernelMod {
  34. public:
  35. AggregationKernelMod() : name_(""), done_(false), done_count_(0), accum_count_(0) {}
  36. virtual ~AggregationKernelMod() = default;
  37. // InitKernel and Launch methods are inherited from pure virtual function of NativeCpuKernelMod so it must have
  38. // implementation.
  39. virtual void InitKernel(const CNodePtr &kernel_node) {}
  40. virtual bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
  41. const std::vector<AddressPtr> &outputs) {
  42. return true;
  43. }
  44. virtual bool AllReduce() = 0;
  45. // Server kernel's memory allocation method, which is different from the workflow in
  46. // Session(GPUSession/CPUSession/AscendSession).
  47. // virtual void AssignMemory(const CNodePtr &kernel_node, std::shared_ptr<MemoryRegister> memory_register) = 0;
  48. // Set the cumulative count this aggregation kernel needs before aggregation is done.
  49. void set_done_count(size_t count) { done_count_ = count; }
  50. // So we use Reset to set the finite-state machine state to Initial after considering this round of aggregation is
  51. // done.
  52. virtual void Reset() = 0;
  53. virtual bool IsAggregationDone() = 0;
  54. // Some kernels should know the inputs/workspace/outputs addresses at initializing phase. For example, FedAvgKernel.
  55. virtual void SetParameterAddress(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
  56. const std::vector<AddressPtr> &outputs) {
  57. return;
  58. }
  59. // Reinitialize aggregation kernel after scaling operations are done.
  60. virtual bool ReInitForScaling() { return true; }
  61. virtual bool ReInitForUpdatingHyperParams(size_t) { return true; }
  62. // Setter and getter of kernels parameters information.
  63. void set_params_info(const ParamsInfo &params_info) { params_info_ = params_info; }
  64. const std::vector<std::string> &input_names() { return params_info_.inputs_names(); }
  65. const std::vector<std::string> &workspace_names() { return params_info_.workspace_names(); }
  66. const std::vector<std::string> &output_names() { return params_info_.outputs_names(); }
  67. // Returns information about whether some inputs should reuse kernel node inputs memory.
  68. const ReuseKernelNodeInfo &reuse_kernel_node_inputs_info() { return reuse_kernel_node_inputs_info_; }
  69. protected:
  70. virtual void GenerateReuseKernelNodeInfo() = 0;
  71. // Aggregation kernel's name which is set by kernel register function.
  72. std::string name_;
  73. // The aggregation is considered done after done_count_ times of accumulation.
  74. bool done_;
  75. // Cumulative count this aggregation kernel needs before aggregation is done.
  76. size_t done_count_;
  77. // Current cumulative count.
  78. size_t accum_count_;
  79. // Parameters information used for kernel register, memory assignment, etc.
  80. ParamsInfo params_info_;
  81. // Information about server kernel reusing kernel node inputs memory from the front end.
  82. // Key refers to the server kernel's input index. Value refers to the kernel node's input index.
  83. ReuseKernelNodeInfo reuse_kernel_node_inputs_info_;
  84. };
  85. } // namespace kernel
  86. } // namespace server
  87. } // namespace fl
  88. } // namespace mindspore
  89. #endif // MINDSPORE_CCSRC_FL_SERVER_KERNEL_AGGREGATION_KERNEL_H_