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.

graph_compiler.h 3.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_RUNTIME_FRAMEWORK_GRAPH_COMPILER_H_
  17. #define MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_GRAPH_COMPILER_H_
  18. #include <vector>
  19. #include <memory>
  20. #include <string>
  21. #include <unordered_map>
  22. #include "runtime/hardware/device_context.h"
  23. #include "backend/session/session_basic.h"
  24. namespace mindspore {
  25. namespace runtime {
  26. using device::DeviceContext;
  27. class GraphCompiler {
  28. public:
  29. static GraphCompiler &GetInstance() {
  30. static GraphCompiler instance;
  31. return instance;
  32. }
  33. // Set device context which is initialized, the function must be called
  34. // before using GraphCompiler and after changing device type or device id.
  35. void set_device_context(DeviceContext *device_context);
  36. // Construct kernel graph from anf nodes list and compile kernel graph in Graph mode,
  37. // the detailed implementation of compiling graph is in 'CompileGraphImpl'.
  38. GraphId CompileGraph(const AnfNodePtrList &nodes, const AnfNodePtrList &outputs);
  39. // Construct single op kernel graph and compile the kernel graph in PyNative mode.
  40. GraphId CompileGraph(session::OpRunInfo *op_run_info, const GraphInfo &graph_info,
  41. std::vector<tensor::TensorPtr> *input_tensors, const std::vector<int64_t> &tensors_mask);
  42. // Get graph by graph id, if not exist return nullptr, used in Graph mode.
  43. KernelGraphPtr Fetch(GraphId graph_id) const;
  44. // Get graph by graph info, if not exist return nullptr, used in PyNative mode.
  45. KernelGraphPtr Fetch(const GraphInfo &graph_info) const;
  46. private:
  47. GraphCompiler() = default;
  48. ~GraphCompiler() = default;
  49. DISABLE_COPY_AND_ASSIGN(GraphCompiler);
  50. // The implementation of compiling graph in Graph Mode, including optimizing graph,
  51. // setting operator info, creating kernel and transforming kernel graph to ActorSet.
  52. GraphId CompileGraphImpl(const KernelGraphPtr &graph) const;
  53. // Create device address for all anf nodes of graph.
  54. void CreateDeviceAddress(const KernelGraphPtr &graph) const;
  55. DeviceContext *device_context_{nullptr};
  56. // Single op kernel graph cache for PyNative mode.
  57. std::unordered_map<GraphInfo, KernelGraphPtr> run_op_graphs_;
  58. // The member variable 'session_' will be removed after removing session module.
  59. session::SessionPtr session_{nullptr};
  60. };
  61. } // namespace runtime
  62. } // namespace mindspore
  63. #endif // MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_GRAPH_COMPILER_H_