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.

vm.h 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef MINDSPORE_CCSRC_VM_VM_H_
  19. #define MINDSPORE_CCSRC_VM_VM_H_
  20. #include <map>
  21. #include <memory>
  22. #include <stack>
  23. #include <string>
  24. #include <tuple>
  25. #include <utility>
  26. #include <vector>
  27. #include <deque>
  28. #include <unordered_map>
  29. #include "ir/anf.h"
  30. #include "utils/base_ref_extends.h"
  31. namespace mindspore {
  32. namespace compile {
  33. class Backend;
  34. using BackendPtr = std::shared_ptr<Backend>;
  35. enum Instruction {
  36. kCall = 0,
  37. kTailCall,
  38. kReturn,
  39. kPartial,
  40. kSwitch,
  41. kSwitchReturn,
  42. kTuple,
  43. kInput,
  44. kExternal,
  45. kPush,
  46. kPrim,
  47. kGraph,
  48. kPadStack
  49. };
  50. using InstType = std::pair<Instruction, VectorRef>;
  51. using InstSet = std::vector<InstType>;
  52. using InstFunctionMap = std::map<Instruction, std::function<void(const VectorRef &)>>;
  53. const std::vector<std::string> inst_str{"call", "tail_call", "return", "partial", "switch", "switch_return", "tuple",
  54. "input", "external", "push", "primitive", "graph", "pad_stack"};
  55. class StructPartial : public Base {
  56. public:
  57. // Initialize StructPartial.
  58. StructPartial(int fn, const VectorRef &args, const FuncGraphPtr &fg = nullptr);
  59. virtual ~StructPartial() = default;
  60. MS_DECLARE_PARENT(StructPartial, Base)
  61. int fn_;
  62. VectorRef args_;
  63. FuncGraphPtr fg_;
  64. };
  65. std::ostream &operator<<(std::ostream &os, const StructPartial &other);
  66. bool operator==(const StructPartial &lhs, const StructPartial &rhs);
  67. class StructSimuSwitch : public Base {
  68. public:
  69. StructSimuSwitch(const BaseRef &fn, const BaseRef &value);
  70. virtual ~StructSimuSwitch() = default;
  71. MS_DECLARE_PARENT(StructSimuSwitch, Base)
  72. BaseRef fn_;
  73. BaseRef value_;
  74. };
  75. std::ostream &operator<<(std::ostream &os, const StructSimuSwitch &other);
  76. bool operator==(const StructSimuSwitch &lhs, const StructSimuSwitch &rhs);
  77. class FinalVM {
  78. public:
  79. // Create a VM with the specified instructions and backend.
  80. explicit FinalVM(const InstSet &insts, const BackendPtr &backend);
  81. virtual ~FinalVM() = default;
  82. BaseRef Eval(const VectorRef &args);
  83. void InstCall(const VectorRef &args);
  84. void InstTailCall(const VectorRef &args);
  85. void InstReturn(const VectorRef &args);
  86. void InstPartial(const VectorRef &args);
  87. void InstSimuPartial(const VectorRef &args);
  88. void InstRealPartial(const VectorRef &args);
  89. void InstSwitch(const VectorRef &args);
  90. void InstSimuSwitch(const VectorRef &args);
  91. void InstRealSwitch(const VectorRef &args);
  92. void InstTuple(const VectorRef &args);
  93. void InstPush(const VectorRef &args);
  94. void InstInput(const VectorRef &args);
  95. void InstPadStack(const VectorRef &args);
  96. void InstExternal(const VectorRef &args);
  97. void InstPushPrim(const VectorRef &args);
  98. void InstSwitchReturn(const VectorRef &args);
  99. void set_insts(const InstSet &value) { insts_ = value; }
  100. BaseRef RunHook(const PrimitivePtr &prim, const VectorRef &arg);
  101. protected:
  102. BaseRef Ref(int i);
  103. void Push(const BaseRef &v);
  104. void Pop(int n = 1);
  105. void MoveStack(int nitems, int height);
  106. void Pushp();
  107. void Popp();
  108. void Pushsp();
  109. void Popsp();
  110. void PushStatus(bool is_switch_call);
  111. bool PopStatus();
  112. void DoJmp(const BaseRef &jmp);
  113. void SyncData(const py::object &args);
  114. void MergeJmpArgs(const BaseRef &jmp, const BaseRef &c);
  115. BaseRef MergeArgs(const BaseRef &first, const BaseRef &second);
  116. private:
  117. InstSet insts_;
  118. std::deque<BaseRef> insts_stack_;
  119. std::stack<int> retp_;
  120. std::stack<int> retsp_;
  121. std::stack<bool> ret_status_;
  122. int pc_;
  123. int sp_;
  124. std::unordered_map<BaseRef, BaseRef, BaseRefHash> cond_jmp_;
  125. std::unordered_map<BaseRef, BaseRef, BaseRefHash> cond_out_;
  126. BackendPtr backend_;
  127. const InstFunctionMap inst_function_map = {
  128. {Instruction::kCall, [this](const VectorRef &args) { InstCall(args); }},
  129. {Instruction::kTailCall, [this](const VectorRef &args) { InstTailCall(args); }},
  130. {Instruction::kReturn, [this](const VectorRef &args) { InstReturn(args); }},
  131. {Instruction::kPartial, [this](const VectorRef &args) { InstPartial(args); }},
  132. {Instruction::kSwitch, [this](const VectorRef &args) { InstSwitch(args); }},
  133. {Instruction::kTuple, [this](const VectorRef &args) { InstTuple(args); }},
  134. {Instruction::kPush, [this](const VectorRef &args) { InstPush(args); }},
  135. {Instruction::kInput, [this](const VectorRef &args) { InstInput(args); }},
  136. {Instruction::kPadStack, [this](const VectorRef &args) { InstPadStack(args); }},
  137. {Instruction::kExternal, [this](const VectorRef &args) { InstExternal(args); }},
  138. {Instruction::kPrim, [this](const VectorRef &args) { InstPushPrim(args); }},
  139. {Instruction::kSwitchReturn, [this](const VectorRef &args) { InstSwitchReturn(args); }},
  140. };
  141. std::map<std::string, py::object> _hook_grad;
  142. };
  143. using FinalVMPtr = std::shared_ptr<FinalVM>;
  144. } // namespace compile
  145. } // namespace mindspore
  146. #endif // MINDSPORE_CCSRC_VM_VM_H_