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

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