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

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