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.

kernel_adjust.h 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * Copyright 2020 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_MINDSPORE_CCSRC_DEVICE_KERNEL_ADJUST_H_
  17. #define MINDSPORE_MINDSPORE_CCSRC_DEVICE_KERNEL_ADJUST_H_
  18. #include <memory>
  19. #include <map>
  20. #include <string>
  21. #include <vector>
  22. #include <unordered_set>
  23. #include "ir/anf.h"
  24. #include "session/kernel_graph.h"
  25. #include "kernel/kernel_build_info.h"
  26. #include "session/session_context.h"
  27. #include "ir/meta_tensor.h"
  28. #include "device/ascend/profiling/profiling_utils.h"
  29. #include "device/kernel_info.h"
  30. using mindspore::device::ascend::ProfilingTraceInfo;
  31. using mindspore::device::ascend::ProfilingUtils;
  32. namespace mindspore {
  33. constexpr auto kLoopCountParamName = "loop_count";
  34. constexpr auto kIterLoopParamName = "iter_loop";
  35. constexpr auto kZeroParamName = "zero";
  36. constexpr auto kOneParamName = "one";
  37. constexpr auto kStreamNeedActivedFirst = "stream_need_active_first";
  38. const uint32_t kFirstStreamSwitchLabel = kInvalidDistincLabel - 1;
  39. const uint32_t kGetNextLabel = kInvalidDistincLabel - 2;
  40. const uint32_t kSecondStreamSwitchLabel = kInvalidDistincLabel - 3;
  41. const uint32_t kInvalidEventId = UINT32_MAX;
  42. const uint32_t kFirstEventId = kInvalidEventId / 2;
  43. namespace device {
  44. class KernelAdjust {
  45. public:
  46. static KernelAdjust &GetInstance() {
  47. static KernelAdjust instance;
  48. return instance;
  49. }
  50. void Reorder(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr);
  51. void InsertSwitchLoop(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr);
  52. bool StepLoadCtrlInputs(const std::shared_ptr<session::Context> &context,
  53. const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr);
  54. void Profiling(NotNull<session::KernelGraph *> kernel_graph_ptr);
  55. static bool NeedInsertSwitch();
  56. CNodePtr CreateStreamActiveOp(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr);
  57. private:
  58. KernelAdjust() = default;
  59. ~KernelAdjust() = default;
  60. void ReorderGetNext(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr);
  61. CNodePtr CreateRecvApplyKernel(const std::shared_ptr<session::KernelGraph> &graph_ptr, uint32_t event_id);
  62. CNodePtr CreateSendApplyKernel(const std::shared_ptr<session::KernelGraph> &graph_ptr, uint32_t event_id);
  63. uint32_t FindFirstStreamSwitchLabel(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr);
  64. void CreateSwitchOpParameters(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  65. std::map<std::string, mindspore::ParameterPtr> *switch_loop_input);
  66. CNodePtr CreateStreamSwitchOp(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  67. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input);
  68. CNodePtr CreateStreamAssignAddnOP(const std::shared_ptr<session::KernelGraph> &kernel_graph_ptr,
  69. const std::map<std::string, mindspore::ParameterPtr> &switch_loop_input);
  70. kernel::KernelBuildInfo::KernelBuildInfoBuilder CreateMngKernelBuilder(const std::vector<std::string> &formats,
  71. const std::vector<TypeId> &type_ids);
  72. void LoadSwitchInputs(std::vector<tensor::TensorPtr> *inputs);
  73. void InsertProfilingKernel(const ProfilingTraceInfo &profiling_trace_info,
  74. NotNull<session::KernelGraph *> kernel_graph_ptr);
  75. };
  76. } // namespace device
  77. } // namespace mindspore
  78. #endif // MINDSPORE_MINDSPORE_CCSRC_DEVICE_KERNEL_ADJUST_H_