1/*
2 * Copyright (c) Glow Contributors. See CONTRIBUTORS file.
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#include "GlowOnnxifiManager.h"
17
18#include <mutex>
19
20namespace glow {
21namespace onnxifi {
22GlowOnnxifiManager &GlowOnnxifiManager::get() {
23 static GlowOnnxifiManager manager;
24 return manager;
25}
26
27BackendPtr GlowOnnxifiManager::createBackend(llvm::StringRef backendName,
28 bool useOnnx,
29 bool forQuantization) {
30 std::lock_guard<std::mutex> lock(m_);
31
32 BackendPtr backend;
33 if (forQuantization) {
34 backend = new onnxifi::Backend(backendName, useOnnx);
35 } else {
36 auto hostManager = getOrCreateHostManager(backendName);
37 backend =
38 new onnxifi::HostManagerBackend(hostManager, backendName, useOnnx);
39 }
40
41 auto res = backends_.insert(backend);
42
43 (void)res;
44 assert((res.second && *res.first) && "Failed to add new Backend");
45
46 return backend;
47}
48
49EventPtr GlowOnnxifiManager::createEvent() {
50 EventPtr event = new onnxifi::Event();
51
52 std::lock_guard<std::mutex> lock(m_);
53
54 auto res = events_.insert(event);
55
56 (void)res;
57 assert((res.second && *res.first) && "Failed to create new Event");
58 return event;
59}
60
61GraphPtr GlowOnnxifiManager::createGraph(BackendPtr backend,
62 QuantizationMode quantizationMode) {
63 assert(isValid(backend));
64
65 GraphPtr graph;
66
67 if (quantizationMode == QuantizationMode::None) {
68 graph = new onnxifi::HostManagerGraph(backend);
69 } else {
70 graph = new onnxifi::InlineGraph(backend, quantizationMode);
71 }
72
73 std::lock_guard<std::mutex> lock(m_);
74
75 auto res = graphs_.insert(graph);
76
77 (void)res;
78 assert((res.second && *res.first) && "Failed to create new Graph");
79 return graph;
80}
81
82std::shared_ptr<runtime::HostManager>
83GlowOnnxifiManager::getOrCreateHostManager(llvm::StringRef backendName) {
84 std::shared_ptr<runtime::HostManager> hostManager;
85
86 auto it = hostManagers_.find(backendName.str());
87
88 if (it != hostManagers_.end()) {
89 hostManager = it->second.lock();
90 }
91
92 if (!hostManager) {
93 hostManager = onnxifi::HostManagerBackend::createHostManager(backendName);
94 assert(hostManager);
95 hostManagers_[backendName.str()] = hostManager;
96 }
97
98 return hostManager;
99}
100
101bool GlowOnnxifiManager::isValid(BackendPtr backend) const {
102 std::lock_guard<std::mutex> lock(m_);
103 return backend && backends_.count(backend) == 1;
104}
105
106bool GlowOnnxifiManager::isValid(EventPtr event) const {
107 std::lock_guard<std::mutex> lock(m_);
108 return event && events_.count(event) == 1;
109}
110
111bool GlowOnnxifiManager::isValid(GraphPtr graph) const {
112 std::lock_guard<std::mutex> lock(m_);
113 return graph && graphs_.count(graph) == 1;
114}
115
116void GlowOnnxifiManager::release(BackendPtr backend) {
117 // TODO: fix this so that a HostManager is deleted when all backends
118 // holding pointers to that HostManager are deleted.
119 std::lock_guard<std::mutex> lock(m_);
120 size_t erased = backends_.erase(backend);
121
122 if (erased) {
123 delete backend;
124 }
125
126 if (backends_.empty()) {
127 hostManagers_.clear();
128 }
129}
130
131void GlowOnnxifiManager::release(EventPtr event) {
132 size_t erased;
133 {
134 std::lock_guard<std::mutex> lock(m_);
135 erased = events_.erase(event);
136 }
137 if (erased) {
138 delete event;
139 }
140}
141
142void GlowOnnxifiManager::release(GraphPtr graph) {
143 size_t erased;
144 {
145 std::lock_guard<std::mutex> lock(m_);
146 erased = graphs_.erase(graph);
147 }
148 if (erased) {
149 delete graph;
150 }
151}
152} // namespace onnxifi
153} // namespace glow
154