Skip to content

Commit abc6b82

Browse files
Roman Dzhabarovfacebook-github-bot
authored andcommitted
Support passing onnxifi backend through the GFLAG/LLVM param.
Summary: Support manual override of onnxifi backend. This would allow flexibility when there is a need to use onnxifi with private backend. Reviewed By: yinghai Test Plan: CI Differential Revision: D16215878 Pulled By: rdzhabarov fbshipit-source-id: 3635ab3dc3ae5be1dac38768cd5b3bf41e9376aa
1 parent 119b3fb commit abc6b82

File tree

1 file changed

+34
-38
lines changed

1 file changed

+34
-38
lines changed

lib/Onnxifi/onnxifiGlow.cpp

Lines changed: 34 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "Base.h"
1818
#include "GlowOnnxifiManager.h"
19+
#include "llvm/Support/CommandLine.h"
1920

2021
#include "glow/Importer/ONNXIFIModelLoader.h"
2122

@@ -28,6 +29,18 @@
2829

2930
#define EXTERNC extern "C"
3031

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+
3144
/**
3245
* This file contains implementation of the onnxifi interface.
3346
* Documentation on the functions implementing onnxifi interface in
@@ -57,67 +70,50 @@ GLOW_ONNXIFI_LIBRARY_FUNCTION_WRAPPER(onnxGetBackendIDs)(
5770
const size_t numBackendsCapacity = *numBackends;
5871

5972
using namespace glow::runtime;
73+
using namespace glow::onnxifi;
6074
const bool withCPU = DeviceManager::numDevices("CPU") > 0;
6175
const bool withHabana = DeviceManager::numDevices("Habana") > 0;
6276

6377
// Only return quantization backend if GLOW_DUMP_PROFILE.
6478
if (getenv("GLOW_DUMP_PROFILE")) {
65-
*numBackends = 2;
79+
*numBackends = 1;
6680

6781
// In case backendIDs is nullptr or does not have enough capacity just
6882
// return the total number of supported backends.
6983
if (numBackendsCapacity < *numBackends || !backendIDs) {
7084
return ONNXIFI_STATUS_FALLBACK;
7185
}
7286

73-
auto *quantizationBackendOnnx = manager.createBackend(
74-
"Interpreter", /*useOnnx*/ true, /*forQuantization*/ true);
7587
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);
8290

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;
10492
} 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+
}
107106

108107
// In case backendIDs is nullptr or does not have enough capacity just
109108
// return the total number of supported backends.
110109
if (numBackendsCapacity < *numBackends || !backendIDs) {
111110
return ONNXIFI_STATUS_FALLBACK;
112111
}
113112

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);
118115

119-
backendIDs[0] = interpreterBackendOnnx;
120-
backendIDs[1] = interpreterBackendC2;
116+
backendIDs[0] = executionBackend;
121117
}
122118

123119
return ONNXIFI_STATUS_SUCCESS;

0 commit comments

Comments
 (0)