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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/graph_partition.h"
  27. #include "vm/vm.h"
  28. #include "backend/session/session_basic.h"
  29. namespace mindspore {
  30. namespace compile {
  31. enum SwitchCondStatus {
  32. kCondOk = 0,
  33. kCondAlreadyRun,
  34. };
  35. class Backend {
  36. public:
  37. explicit Backend(const std::string &name);
  38. virtual ~Backend() = default;
  39. LinkFuncType convert_fn() { return convert_fn_; }
  40. std::string name() { return name_; }
  41. virtual bool GetCond(const BaseRef &c, bool *value);
  42. virtual bool GetIndex(const BaseRef &c, int64_t *value);
  43. virtual GraphId CompileGraph(NotNull<FuncGraphPtr> fg) { return kInvalidGraphId; }
  44. virtual void Link(GraphId) {}
  45. virtual void SetDebugger() {}
  46. bool is_multi_graph_sink() const { return is_multi_graph_sink_; }
  47. void set_is_multi_graph_sink(bool flag) { is_multi_graph_sink_ = flag; }
  48. protected:
  49. std::string name_;
  50. LinkFuncType convert_fn_;
  51. bool is_multi_graph_sink_;
  52. };
  53. class MsBackend : public Backend {
  54. public:
  55. MsBackend(const std::string &name, const std::string &target, uint32_t device_id);
  56. ~MsBackend() override = default;
  57. LinConvertResult MsConvert(const GraphSegmentPtr &segment, const std::string &target = "");
  58. VectorRef MsRunGraph(const GraphId &g, const VectorRef &args, const std::string &target = "");
  59. VectorRef MsSimuRunGraph(const GraphId &g, const VectorRef &args);
  60. void Link(GraphId) override;
  61. GraphId CompileGraph(NotNull<FuncGraphPtr> fg) override;
  62. VectorRef RunGraph(GraphId graph_id, const VectorRef &args);
  63. void ClearSessionGraphs();
  64. void CreateOtherSession(const std::string &target);
  65. #ifdef ENABLE_DEBUGGER
  66. void SetDebugger() override;
  67. #endif
  68. private:
  69. session::SessionPtr target_sess_;
  70. session::SessionPtr other_sess_;
  71. std::string target_device_;
  72. std::string other_device_;
  73. std::unordered_map<GraphId, LinConvertResult> graph_id_map_;
  74. };
  75. } // namespace compile
  76. } // namespace mindspore
  77. #endif