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.

backend.h 2.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Copyright 2019 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_VM_BACKEND_H_
  17. #define MINDSPORE_CCSRC_VM_BACKEND_H_
  18. #include <list>
  19. #include <memory>
  20. #include <string>
  21. #include <unordered_map>
  22. #include <utility>
  23. #include "utils/contract.h"
  24. #include "ir/anf.h"
  25. #include "vm/segment_runner.h"
  26. #include "vm/vm.h"
  27. #include "backend/session/session_basic.h"
  28. namespace mindspore {
  29. namespace compile {
  30. enum SwitchCondStatus {
  31. kCondOk = 0,
  32. kCondAlreadyRun,
  33. };
  34. class Backend {
  35. public:
  36. explicit Backend(const std::string &name);
  37. virtual ~Backend() = default;
  38. LinkFuncType convert_fn() { return convert_fn_; }
  39. std::string name() { return name_; }
  40. virtual bool GetCond(const BaseRef &c, bool *value);
  41. virtual bool GetIndex(const BaseRef &c, int *value);
  42. virtual GraphId CompileGraph(NotNull<FuncGraphPtr> fg) { return kInvalidGraphId; }
  43. virtual void Link(GraphId) {}
  44. virtual void SetDebugger() {}
  45. bool is_multi_graph_sink() const { return is_multi_graph_sink_; }
  46. void set_is_multi_graph_sink(bool flag) { is_multi_graph_sink_ = flag; }
  47. protected:
  48. std::string name_;
  49. LinkFuncType convert_fn_;
  50. bool is_multi_graph_sink_;
  51. };
  52. class MsBackend : public Backend {
  53. public:
  54. MsBackend(const std::string &name, const std::string &target, uint32_t device_id);
  55. ~MsBackend() override = default;
  56. LinConvertResult MsConvert(const AnfNodePtrList &lst, const std::string &target = "");
  57. VectorRef MsRunGraph(const GraphId &g, const VectorRef &args, const std::string &target = "");
  58. VectorRef MsSimuRunGraph(const GraphId &g, const VectorRef &args);
  59. void Link(GraphId) override;
  60. GraphId CompileGraph(NotNull<FuncGraphPtr> fg) override;
  61. VectorRef RunGraph(GraphId graph_id, const VectorRef &args);
  62. void CreateOtherSession(const std::string &target);
  63. #ifdef ENABLE_DEBUGGER
  64. void SetDebugger() override;
  65. #endif
  66. private:
  67. session::SessionPtr target_sess_;
  68. session::SessionPtr other_sess_;
  69. std::string target_device_;
  70. std::string other_device_;
  71. std::unordered_map<GraphId, LinConvertResult> graph_id_map_;
  72. };
  73. } // namespace compile
  74. } // namespace mindspore
  75. #endif