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.4 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "pybind11/pybind11.h"
  30. #include "ir/anf.h"
  31. #include "base/base_ref.h"
  32. namespace py = pybind11;
  33. namespace mindspore {
  34. namespace compile {
  35. class Backend;
  36. using BackendPtr = std::shared_ptr<Backend>;
  37. enum Instruction {
  38. kCall = 0,
  39. kTailCall,
  40. kReturn,
  41. kPartial,
  42. kSwitch,
  43. kSwitchReturn,
  44. kTuple,
  45. kInput,
  46. kExternal,
  47. kPush,
  48. kPrim,
  49. kGraph,
  50. kPadStack,
  51. kSwitchLayer
  52. };
  53. using InstType = std::pair<Instruction, VectorRef>;
  54. using InstSet = std::vector<InstType>;
  55. using InstFunctionMap = std::map<Instruction, std::function<void(const VectorRef &)>>;
  56. const std::vector<std::string> inst_str{"call", "tail_call", "Return", "partial", "switch",
  57. "switch_return", "tuple", "input", "external", "push",
  58. "primitive", "graph", "pad_stack", "switch_layer"};
  59. class StructPartial : public Base {
  60. public:
  61. // Initialize StructPartial.
  62. StructPartial(int64_t fn, const VectorRef &args, const FuncGraphPtr &fg = nullptr);
  63. virtual ~StructPartial() = default;
  64. MS_DECLARE_PARENT(StructPartial, Base)
  65. int64_t fn_;
  66. VectorRef args_;
  67. FuncGraphPtr fg_;
  68. };
  69. std::ostream &operator<<(std::ostream &os, const StructPartial &other);
  70. bool operator==(const StructPartial &lhs, const StructPartial &rhs);
  71. class StructSimuSwitch : public Base {
  72. public:
  73. StructSimuSwitch(const BaseRef &fn, const BaseRef &value);
  74. virtual ~StructSimuSwitch() = default;
  75. MS_DECLARE_PARENT(StructSimuSwitch, Base)
  76. BaseRef fn_;
  77. BaseRef value_;
  78. };
  79. std::ostream &operator<<(std::ostream &os, const StructSimuSwitch &other);
  80. bool operator==(const StructSimuSwitch &lhs, const StructSimuSwitch &rhs);
  81. class FinalVM {
  82. public:
  83. // Create a VM with the specified instructions and backend.
  84. explicit FinalVM(const InstSet &insts, const BackendPtr &backend);
  85. virtual ~FinalVM() = default;
  86. BaseRef Eval(const VectorRef &args);
  87. void InstCall(const VectorRef &args);
  88. void InstTailCall(const VectorRef &args);
  89. void InstReturn(const VectorRef &args);
  90. void InstPartial(const VectorRef &args);
  91. void InstRealPartial(const VectorRef &args);
  92. void InstSwitch(const VectorRef &args);
  93. void InstRealSwitch(const VectorRef &args);
  94. void InstTuple(const VectorRef &args);
  95. void InstPush(const VectorRef &args);
  96. void InstInput(const VectorRef &args);
  97. void InstPadStack(const VectorRef &args);
  98. void InstExternal(const VectorRef &args);
  99. void InstPushPrim(const VectorRef &args);
  100. void InstSwitchReturn(const VectorRef &args);
  101. void InstSwitchLayer(const VectorRef &args);
  102. void set_insts(const InstSet &value) { insts_ = value; }
  103. BaseRef RunHook(const PrimitivePtr &prim, const VectorRef &arg);
  104. protected:
  105. BaseRef Ref(int64_t i);
  106. void Push(const BaseRef &v);
  107. void Pop(int64_t n = 1);
  108. void MoveStack(int64_t nitems, int64_t height);
  109. void Pushp();
  110. void Popp();
  111. void Pushsp();
  112. void Popsp();
  113. void DoJmp(const BaseRef &jmp);
  114. void SyncData(const py::object &args);
  115. private:
  116. InstSet insts_;
  117. std::deque<BaseRef> insts_stack_;
  118. std::stack<int64_t> retp_;
  119. std::stack<int64_t> retsp_;
  120. int64_t pc_;
  121. int64_t sp_;
  122. BackendPtr backend_;
  123. const InstFunctionMap inst_function_map = {
  124. {Instruction::kCall, [this](const VectorRef &args) { InstCall(args); }},
  125. {Instruction::kTailCall, [this](const VectorRef &args) { InstTailCall(args); }},
  126. {Instruction::kReturn, [this](const VectorRef &args) { InstReturn(args); }},
  127. {Instruction::kPartial, [this](const VectorRef &args) { InstPartial(args); }},
  128. {Instruction::kSwitch, [this](const VectorRef &args) { InstSwitch(args); }},
  129. {Instruction::kTuple, [this](const VectorRef &args) { InstTuple(args); }},
  130. {Instruction::kPush, [this](const VectorRef &args) { InstPush(args); }},
  131. {Instruction::kInput, [this](const VectorRef &args) { InstInput(args); }},
  132. {Instruction::kPadStack, [this](const VectorRef &args) { InstPadStack(args); }},
  133. {Instruction::kExternal, [this](const VectorRef &args) { InstExternal(args); }},
  134. {Instruction::kPrim, [this](const VectorRef &args) { InstPushPrim(args); }},
  135. {Instruction::kSwitchReturn, [this](const VectorRef &args) { InstSwitchReturn(args); }},
  136. {Instruction::kSwitchLayer, [this](const VectorRef &args) { InstSwitchLayer(args); }}};
  137. };
  138. using FinalVMPtr = std::shared_ptr<FinalVM>;
  139. } // namespace compile
  140. } // namespace mindspore
  141. #endif // MINDSPORE_CCSRC_VM_VM_H_