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.

optimizer_kernel.h 4.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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_OPTIMIZER_KERNEL_H_
  17. #define MINDSPORE_CCSRC_FL_SERVER_KERNEL_OPTIMIZER_KERNEL_H_
  18. #include <memory>
  19. #include <string>
  20. #include <vector>
  21. #include <functional>
  22. #include "kernel/common_utils.h"
  23. #include "plugin/device/cpu/kernel/cpu_kernel.h"
  24. #include "fl/server/common.h"
  25. #include "fl/server/memory_register.h"
  26. #include "fl/server/kernel/params_info.h"
  27. namespace mindspore {
  28. namespace fl {
  29. namespace server {
  30. namespace kernel {
  31. using mindspore::kernel::IsSameShape;
  32. using mindspore::kernel::USE_NESTEROV;
  33. // OptimizerKernelMod is the kernel in server for weights' optimizing.
  34. // Normally server's optimizer kernels should be inherited from CPU's optimzier kernels to reuse the implementation.
  35. class OptimizerKernelMod : public NativeCpuKernelMod {
  36. public:
  37. OptimizerKernelMod() = default;
  38. virtual ~OptimizerKernelMod() = default;
  39. // InitKernel and Launch methods are inherited from pure virtual function of NativeCpuKernelMod so it must have
  40. // implementation.
  41. virtual void InitKernel(const CNodePtr &kernel_node) {}
  42. virtual bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
  43. const std::vector<AddressPtr> &outputs) {
  44. return true;
  45. }
  46. // Server kernel's memory allocation method, which is different from the workflow in
  47. // Session(GPUSession/CPUSession/AscendSession).
  48. // virtual void AssignMemory(const CNodePtr &kernel_node, std::shared_ptr<MemoryRegister> memory_register) = 0;
  49. // Setter and getter of kernels parameters information.
  50. void set_params_info(const ParamsInfo &params_info) { params_info_ = params_info; }
  51. const std::vector<std::string> &input_names() { return params_info_.inputs_names(); }
  52. const std::vector<std::string> &workspace_names() { return params_info_.workspace_names(); }
  53. const std::vector<std::string> &output_names() { return params_info_.outputs_names(); }
  54. // Returns information about whether some inputs should reuse kernel node inputs memory.
  55. const ReuseKernelNodeInfo &reuse_kernel_node_inputs_info() { return reuse_kernel_node_inputs_info_; }
  56. protected:
  57. virtual void GenerateReuseKernelNodeInfo() = 0;
  58. void InitServerKernelInputOutputSize(const CNodePtr &kernel_node) {
  59. MS_EXCEPTION_IF_NULL(kernel_node);
  60. size_t input_num = common::AnfAlgo::GetInputTensorNum(kernel_node);
  61. size_t type_size = sizeof(float);
  62. for (size_t input_index = 0; input_index < input_num; ++input_index) {
  63. std::vector<size_t> shape = common::AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, input_index);
  64. size_t tensor_size =
  65. shape.empty() ? type_size : std::accumulate(shape.begin(), shape.end(), type_size, std::multiplies<size_t>());
  66. input_size_list_.emplace_back(tensor_size);
  67. }
  68. size_t output_num = common::AnfAlgo::GetOutputTensorNum(kernel_node);
  69. for (size_t output_index = 0; output_index < output_num; ++output_index) {
  70. std::vector<size_t> shape = common::AnfAlgo::GetOutputInferShape(kernel_node, output_index);
  71. size_t tensor_size =
  72. shape.empty() ? type_size : std::accumulate(shape.begin(), shape.end(), type_size, std::multiplies<size_t>());
  73. output_size_list_.emplace_back(tensor_size);
  74. }
  75. }
  76. // Parameters information used for kernel register, memory assignment, etc.
  77. ParamsInfo params_info_;
  78. // Information about server kernel reusing kernel node inputs memory from the front end.
  79. // Key refers to the server kernel's input index. Value refers to the kernel node's input index.
  80. ReuseKernelNodeInfo reuse_kernel_node_inputs_info_;
  81. };
  82. } // namespace kernel
  83. } // namespace server
  84. } // namespace fl
  85. } // namespace mindspore
  86. #endif // MINDSPORE_CCSRC_FL_SERVER_KERNEL_OPTIMIZER_KERNEL_H_