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.

gpu_session.h 4.4 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * Copyright 2019-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_BACKEND_SESSION_GPU_SESSION_H
  17. #define MINDSPORE_CCSRC_BACKEND_SESSION_GPU_SESSION_H
  18. #include <vector>
  19. #include <memory>
  20. #include <algorithm>
  21. #include <string>
  22. #include <map>
  23. #include "backend/session/session_basic.h"
  24. #include "backend/session/kernel_graph.h"
  25. #include "backend/session/session_factory.h"
  26. using KernelGraph = mindspore::session::KernelGraph;
  27. namespace mindspore {
  28. namespace session {
  29. namespace gpu {
  30. class GPUSession : public SessionBasic {
  31. public:
  32. GPUSession() = default;
  33. ~GPUSession() override = default;
  34. void Init(uint32_t device_id) override;
  35. void SyncStream() override;
  36. protected:
  37. void UnifyMindIR(const KernelGraphPtr &graph) override { SessionBasic::UnifyMindIR(graph); }
  38. GraphId CompileGraphImpl(const AnfNodePtrList &lst, const AnfNodePtrList &outputs) override;
  39. GraphId CompileGraphImpl(NotNull<FuncGraphPtr> func_graph) override;
  40. void PreExecuteGraph(const std::shared_ptr<KernelGraph> &kernel_graph, const std::vector<tensor::TensorPtr> &inputs,
  41. VectorRef *const outputs) override;
  42. void PostExecuteGraph(const std::shared_ptr<KernelGraph> &kernel_graph, const std::vector<tensor::TensorPtr> &inputs,
  43. VectorRef *const outputs) override;
  44. void ExecuteGraph(const std::shared_ptr<KernelGraph> &kernel_graph) override;
  45. KernelGraphPtr BuildOpImpl(const OpRunInfo &op_run_info, const GraphInfo &graph_info,
  46. const std::vector<tensor::TensorPtr> &input_tensors,
  47. const std::vector<int64_t> &tensors_mask) override;
  48. void RunOpImpl(const GraphInfo &graph_info, OpRunInfo *op_run_info, std::vector<tensor::TensorPtr> *input_tensors,
  49. VectorRef *outputs, const std::vector<int64_t> &tensors_mask) override;
  50. std::shared_ptr<device::Bucket> CreateBucket(uint32_t bucket_id, uint32_t bucket_size) override;
  51. std::string GetCommWorldGroup() override { return kNcclWorldGroup; }
  52. void LoadInputData(const std::shared_ptr<KernelGraph> &kernel_graph,
  53. const std::vector<tensor::TensorPtr> &inputs_const) const override;
  54. void UpdateOutputTensors(const VectorRef *outputs,
  55. const std::map<tensor::TensorPtr, session::KernelWithIndex> &tensor_to_node) override;
  56. private:
  57. void SelectKernel(const std::shared_ptr<KernelGraph> &kernel_graph) const;
  58. void StartKernelRT() const;
  59. void Optimize(const std::shared_ptr<KernelGraph> &kernel_graph);
  60. void HardwareOptimize(const std::shared_ptr<KernelGraph> &kernel_graph);
  61. void RunOpOptimize(const std::shared_ptr<KernelGraph> &kernel_graph);
  62. void RunOpHardwareOptimize(const std::shared_ptr<KernelGraph> &kernel_graph);
  63. void GraphKernelOptimize(const std::shared_ptr<KernelGraph> &kernel_graph);
  64. void AssignStream(const std::shared_ptr<KernelGraph> &kernel_graph);
  65. void BuildKernel(const std::shared_ptr<KernelGraph> &kernel_graph) const;
  66. void AllocateMemory(KernelGraph *kernel_graph) const;
  67. void RunOpAllocateMemory(const std::vector<tensor::TensorPtr> &input_tensors, KernelGraph *kernel_graph) const;
  68. void RunOpClearMemory(KernelGraph *kernel_graph) const;
  69. void RunOpGenKernelEvent(const KernelGraph *graph) const;
  70. void Execute(const std::shared_ptr<KernelGraph> &kernel_graph) const;
  71. void Dump(const std::shared_ptr<KernelGraph> &kernel_graph) const;
  72. void DumpSetup(const std::shared_ptr<KernelGraph> &kernel_graph) const;
  73. bool DumpDataEnabledIteration() const;
  74. GraphId CompileGraphImpl(KernelGraphPtr kernel_graph);
  75. };
  76. using GPUSessionPtr = std::shared_ptr<GPUSession>;
  77. MS_REG_SESSION(kGPUDevice, GPUSession);
  78. } // namespace gpu
  79. } // namespace session
  80. } // namespace mindspore
  81. #endif // MINDSPORE_CCSRC_BACKEND_SESSION_GPU_SESSION_H