Caffe2 - C++ API
A deep learning, cross platform ML framework
onnxifi_graph_info.h
1 #pragma once
2 
3 #include <functional>
4 #include <memory>
5 #include <mutex>
6 #include <unordered_map>
7 
8 #include "caffe2/core/logging.h"
9 #include "foxi/onnxifi_loader.h"
10 
11 namespace caffe2 {
12 namespace onnx {
13 
15  onnxBackendID backend_id;
16  onnxBackend backend;
17  onnxGraph graph;
18  onnxifi_library* lib{nullptr};
19 
21  onnxBackendID backend_id,
22  onnxBackend backend,
23  onnxGraph graph,
24  onnxifi_library* lib)
25  : backend_id(backend_id), backend(backend), graph(graph), lib(lib) {}
26 
27  BackendGraphInfo(const BackendGraphInfo& other) = delete;
28 
29  BackendGraphInfo& operator=(const BackendGraphInfo& other) = delete;
30 
31  BackendGraphInfo(BackendGraphInfo&& other) noexcept {
32  backend_id = other.backend_id;
33  backend = other.backend;
34  graph = other.graph;
35  lib = other.lib;
36  other.backend_id = other.backend = other.graph = other.lib = nullptr;
37  }
38 
39  BackendGraphInfo& operator=(BackendGraphInfo&& other) {
40  backend_id = other.backend_id;
41  backend = other.backend;
42  graph = other.graph;
43  lib = other.lib;
44  other.backend_id = other.backend = other.graph = other.lib = nullptr;
45  return *this;
46  }
47 
48  ~BackendGraphInfo() {
49  if (lib) {
50  onnxStatus err;
51  if (graph) {
52  err = lib->onnxReleaseGraph(graph);
53  if (err != ONNXIFI_STATUS_SUCCESS) {
54  LOG(ERROR) << "Error when calling onnxReleaseGraph";
55  }
56  }
57  if (backend) {
58  err = lib->onnxReleaseBackend(backend);
59  if (err != ONNXIFI_STATUS_SUCCESS) {
60  LOG(ERROR) << "Error when calling onnxReleaseBackend";
61  }
62  }
63  if (backend_id) {
64  err = lib->onnxReleaseBackendID(backend_id);
65  if (err != ONNXIFI_STATUS_SUCCESS) {
66  LOG(ERROR) << "Error when calling onnxReleaseBackendID";
67  }
68  }
69  }
70  }
71 };
72 using SharedPtrBackendGraphInfo = std::shared_ptr<BackendGraphInfo>;
73 
74 // This class maintains a map of already created graph for nets+ops
76  public:
78  // Make class noncopyable and nomovable.
81  OnnxBackendGraphMap operator=(const OnnxBackendGraphMap&) = delete;
82  OnnxBackendGraphMap operator=(OnnxBackendGraphMap&&) = delete;
83 
84  SharedPtrBackendGraphInfo lookup(const std::string& key);
85 
86  // If corresponding BackendGraphInfo already exists, return it directly.
87  // Otherwise we use creator to create the BackendGraphInfo shared_ptr and
88  // insert it into the map and return it. The whole process should be guarded
89  // by a lock. Note that since it will create the backend while holding the
90  // lock, expect latency during initialization phase when there are lots of
91  // models to compile.
92  SharedPtrBackendGraphInfo insert(
93  const std::string& key,
94  std::function<SharedPtrBackendGraphInfo()> creator);
95 
96  void remove(const std::string& key);
97 
98  private:
99  std::mutex backend_graph_map_lock_;
100  std::unordered_map<std::string, SharedPtrBackendGraphInfo> backend_graph_map_;
101 };
102 
103 OnnxBackendGraphMap* getOnnxBackendGraphMap();
104 } // namespace onnx
105 } // namespace caffe2
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13