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.

proxy_graph.h 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * \file imperative/src/impl/proxy_graph.h
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #pragma once
  12. #include "megbrain/comp_node.h"
  13. #include "megbrain/graph/cg.h"
  14. #include "megbrain/graph/grad_impl.h"
  15. #include "megbrain/imperative.h"
  16. #include "megbrain/imperative/ops/backward_graph.h"
  17. namespace mgb {
  18. namespace imperative {
  19. class ProxyGraph : public NonCopyableObj {
  20. public:
  21. static ProxyGraph* get_default_graph();
  22. static std::unique_ptr<MegBrainError> get_async_error() {
  23. return std::move(tm_async_error);
  24. }
  25. /********************** Physical Tensor API **********************/
  26. SmallVector<LogicalTensorDesc> infer_output_attrs(
  27. const OpDef& opdef, const SmallVector<Tensor*>& inputs);
  28. void invoke_op(
  29. const OpDef& opdef, const SmallVector<Tensor*>& inputs,
  30. const SmallVector<Tensor*>& outputs, const SmallVector<Tensor*>& workspace);
  31. EncodedSubgraph make_backward_graph(
  32. const OpDef& opdef, const SmallVector<LogicalTensorDesc>& input_descs,
  33. const SmallVector<bool>& input_requires_grad,
  34. const SmallVector<bool>& output_has_grad);
  35. std::tuple<SmallVector<MemoryDesc>, SmallVector<MemoryDesc>> infer_output_mem_desc(
  36. const OpDef& def, const SmallVector<Tensor*>& inputs_tensors,
  37. const SmallVector<MemoryDesc>& inputs_mems);
  38. /********************** Logical Tensor API **********************/
  39. size_t get_opr_output_size(
  40. const OpDef& opdef, const SmallVector<LogicalTensorDesc>& inputs);
  41. std::tuple<SmallVector<LogicalTensorDesc>, bool> infer_output_attrs_fallible(
  42. const OpDef& opdef, const SmallVector<LogicalTensorDesc>& inputs);
  43. private:
  44. ProxyGraph();
  45. class ProxyGraphImpl;
  46. class ExecEnv;
  47. class StaticInferManager;
  48. class SeqCompNodeOptimizer;
  49. class InputPlaceholder;
  50. struct ProxyGraphInst;
  51. struct GradGraph;
  52. class CurOprGuard;
  53. void reset();
  54. /********************** Physical Tensor Helper **********************/
  55. void cleanup();
  56. void init_output_tensor(
  57. const SmallVector<Tensor*>& outputs, const SmallVector<Tensor*>& workspace);
  58. cg::OperatorNodeBase* get_proxy_opr(
  59. const OpDef& opdef, const SmallVector<Tensor*>& inputs);
  60. /********************** Logical Tensor Helper **********************/
  61. cg::OperatorNodeBase* get_proxy_opr(
  62. const OpDef& opdef, const SmallVector<LogicalTensorDesc>& inputs);
  63. cg::VarNodeArray make_input_place_holders(
  64. const SmallVector<LogicalTensorDesc>& inputs);
  65. /********************** Common Helper **********************/
  66. bool do_shape_infer(bool sync_value);
  67. TensorPtr as_tensor(cg::OperatorNodeBase* opr, bool share = true);
  68. cg::OperatorNodeBase* m_cur_opr = nullptr;
  69. std::unique_ptr<ProxyGraphImpl> m_graph;
  70. size_t m_max_op_cnt = 100;
  71. std::unique_ptr<ExecEnv> m_env;
  72. std::unique_ptr<StaticInferManager> m_static_infer_manager;
  73. std::unique_ptr<SeqCompNodeOptimizer> m_seq_comp_node_optimizer;
  74. static thread_local std::unique_ptr<MegBrainError> tm_async_error;
  75. };
  76. } // namespace imperative
  77. } // namespace mgb
  78. // vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台