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_context.h 3.2 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * Copyright 2019-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 GE_GRAPH_MANAGER_GRAPH_CONTEXT_H_
  17. #define GE_GRAPH_MANAGER_GRAPH_CONTEXT_H_
  18. #include <functional>
  19. #include <map>
  20. #include <memory>
  21. #include <string>
  22. #include <tuple>
  23. #include <unordered_map>
  24. #include <utility>
  25. #include <vector>
  26. #include "graph/compute_graph.h"
  27. #include "graph/manager/graph_manager_utils.h"
  28. namespace ge {
  29. class GraphContext;
  30. using SessionId = uint64_t;
  31. using GradOpList = std::vector<std::pair<GraphId, std::string>>;
  32. using VariableRecord = std::tuple<std::string, GradOpList, uint8_t>;
  33. using OutputOpNameIndex = std::pair<std::string, uint8_t>;
  34. struct key_hash : public std::unary_function<const ge::OutputOpNameIndex, std::size_t> {
  35. std::size_t operator()(const ge::OutputOpNameIndex &outputOpNameIndex) const {
  36. return (static_cast<uint8_t>(outputOpNameIndex.first[0])) ^ outputOpNameIndex.second;
  37. }
  38. };
  39. struct key_equal : public std::binary_function<const ge::OutputOpNameIndex, const ge::OutputOpNameIndex, bool> {
  40. bool operator()(const ge::OutputOpNameIndex &varR1, const ge::OutputOpNameIndex &varR2) const {
  41. return (varR1.first == varR2.first && varR1.second == varR2.second);
  42. }
  43. };
  44. using VarNodeTensorTable = std::vector<std::pair<VariableRecord, GeTensor>>;
  45. using SessionVarTableMap = std::map<ge::SessionId, VarNodeTensorTable>;
  46. using GraphContextPtr = std::shared_ptr<GraphContext>;
  47. struct OutputDescInfo {
  48. std::string op_name;
  49. uint8_t index;
  50. struct InputOutputDescInfo info;
  51. };
  52. ///
  53. /// @ingroup graph
  54. /// @brief Global graph context sharing, provide variable sharing facility for
  55. /// multiple graphs in the same session.
  56. /// @author
  57. ///
  58. class GraphContext {
  59. public:
  60. GraphContext() = default;
  61. ~GraphContext() = default;
  62. Status Initialize(const std::map<std::string, std::string> &options = {}) const;
  63. // Disable copy constructor and assignment operator
  64. GraphContext(const GraphContext &) = delete;
  65. GraphContext &operator=(const GraphContext &) = delete;
  66. Status Finalize() const;
  67. Status GetVariableTensor(const std::string &var_data_name, GeTensor &returned_tensor);
  68. const ComputeGraphPtr &GetComputeGraph() const { return compute_graph_; }
  69. Status SetComputeGraph(const GraphNodePtr &graph_node);
  70. private:
  71. explicit GraphContext(const GraphNodePtr &graph_node);
  72. ComputeGraphPtr compute_graph_ = nullptr;
  73. GraphId current_graph_id_ = 0;
  74. // Get the unique VarNode-Tensor table
  75. static VarNodeTensorTable &GetVarNodeTensorTable() {
  76. static VarNodeTensorTable _this;
  77. return _this;
  78. }
  79. };
  80. } // namespace ge
  81. #endif // GE_GRAPH_MANAGER_GRAPH_CONTEXT_H_

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示