|
16 | 16 |
|
17 | 17 | #include "Base.h" |
18 | 18 | #include "GlowOnnxifiManager.h" |
| 19 | +#include "llvm/Support/CommandLine.h" |
19 | 20 |
|
20 | 21 | #include "glow/Importer/ONNXIFIModelLoader.h" |
21 | 22 |
|
|
28 | 29 |
|
29 | 30 | #define EXTERNC extern "C" |
30 | 31 |
|
| 32 | +namespace glow { |
| 33 | +namespace onnxifi { |
| 34 | + |
| 35 | +std::string GlowOnnxifiBackend = ""; |
| 36 | +static llvm::cl::opt<std::string, /*external storage*/ true> |
| 37 | + GlowOnnxifiBackendOpt("glow-onnxifi-backend", |
| 38 | + llvm::cl::desc("Glow backend used for ONNXIFI"), |
| 39 | + llvm::cl::location(GlowOnnxifiBackend)); |
| 40 | + |
| 41 | +} // namespace onnxifi |
| 42 | +} // namespace glow |
| 43 | + |
31 | 44 | /** |
32 | 45 | * This file contains implementation of the onnxifi interface. |
33 | 46 | * Documentation on the functions implementing onnxifi interface in |
@@ -57,67 +70,50 @@ GLOW_ONNXIFI_LIBRARY_FUNCTION_WRAPPER(onnxGetBackendIDs)( |
57 | 70 | const size_t numBackendsCapacity = *numBackends; |
58 | 71 |
|
59 | 72 | using namespace glow::runtime; |
| 73 | + using namespace glow::onnxifi; |
60 | 74 | const bool withCPU = DeviceManager::numDevices("CPU") > 0; |
61 | 75 | const bool withHabana = DeviceManager::numDevices("Habana") > 0; |
62 | 76 |
|
63 | 77 | // Only return quantization backend if GLOW_DUMP_PROFILE. |
64 | 78 | if (getenv("GLOW_DUMP_PROFILE")) { |
65 | | - *numBackends = 2; |
| 79 | + *numBackends = 1; |
66 | 80 |
|
67 | 81 | // In case backendIDs is nullptr or does not have enough capacity just |
68 | 82 | // return the total number of supported backends. |
69 | 83 | if (numBackendsCapacity < *numBackends || !backendIDs) { |
70 | 84 | return ONNXIFI_STATUS_FALLBACK; |
71 | 85 | } |
72 | 86 |
|
73 | | - auto *quantizationBackendOnnx = manager.createBackend( |
74 | | - "Interpreter", /*useOnnx*/ true, /*forQuantization*/ true); |
75 | 87 | auto *quantizationBackendC2 = manager.createBackend( |
76 | | - "Interpreter", /*useOnnx*/ false, /*forQuantization*/ true); |
77 | | - |
78 | | - backendIDs[0] = quantizationBackendOnnx; |
79 | | - backendIDs[1] = quantizationBackendC2; |
80 | | - } else if (withCPU || withHabana) { |
81 | | - *numBackends = 4; |
| 88 | + GlowOnnxifiBackend.empty() ? "Interpreter" : GlowOnnxifiBackend, |
| 89 | + /*useOnnx*/ false, /*forQuantization*/ true); |
82 | 90 |
|
83 | | - auto backendName = withHabana ? "Habana" : "CPU"; |
84 | | - |
85 | | - // In case backendIDs is nullptr or does not have enough capacity just |
86 | | - // return the total number of supported backends. |
87 | | - if (numBackendsCapacity < *numBackends || !backendIDs) { |
88 | | - return ONNXIFI_STATUS_FALLBACK; |
89 | | - } |
90 | | - |
91 | | - auto *cpuBackendOnnx = manager.createBackend(backendName, |
92 | | - /*useOnnx*/ true); |
93 | | - auto *interpreterBackendOnnx = |
94 | | - manager.createBackend("Interpreter", /*useOnnx*/ true); |
95 | | - auto *cpuBackendC2 = manager.createBackend(backendName, |
96 | | - /*useOnnx*/ false); |
97 | | - auto *interpreterBackendC2 = |
98 | | - manager.createBackend("Interpreter", /*useOnnx*/ false); |
99 | | - |
100 | | - backendIDs[0] = cpuBackendOnnx; |
101 | | - backendIDs[1] = interpreterBackendOnnx; |
102 | | - backendIDs[2] = cpuBackendC2; |
103 | | - backendIDs[3] = interpreterBackendC2; |
| 91 | + backendIDs[0] = quantizationBackendC2; |
104 | 92 | } else { |
105 | | - |
106 | | - *numBackends = 2; |
| 93 | + *numBackends = 1; |
| 94 | + |
| 95 | + auto backendName = GlowOnnxifiBackend; |
| 96 | + |
| 97 | + if (backendName.empty()) { |
| 98 | + if (withHabana) { |
| 99 | + backendName = "Habana"; |
| 100 | + } else if (withCPU) { |
| 101 | + backendName = "CPU"; |
| 102 | + } else { |
| 103 | + backendName = "Interpreter"; |
| 104 | + } |
| 105 | + } |
107 | 106 |
|
108 | 107 | // In case backendIDs is nullptr or does not have enough capacity just |
109 | 108 | // return the total number of supported backends. |
110 | 109 | if (numBackendsCapacity < *numBackends || !backendIDs) { |
111 | 110 | return ONNXIFI_STATUS_FALLBACK; |
112 | 111 | } |
113 | 112 |
|
114 | | - auto *interpreterBackendOnnx = |
115 | | - manager.createBackend("Interpreter", /*useOnnx*/ true); |
116 | | - auto *interpreterBackendC2 = |
117 | | - manager.createBackend("Interpreter", /*useOnnx*/ false); |
| 113 | + auto *executionBackend = manager.createBackend(backendName, |
| 114 | + /*useOnnx*/ false); |
118 | 115 |
|
119 | | - backendIDs[0] = interpreterBackendOnnx; |
120 | | - backendIDs[1] = interpreterBackendC2; |
| 116 | + backendIDs[0] = executionBackend; |
121 | 117 | } |
122 | 118 |
|
123 | 119 | return ONNXIFI_STATUS_SUCCESS; |
|
0 commit comments