提交 9cc66963 编写于 作者: M Megvii Engine Team

refactor(sdk/extern-opr): add cpu threads config for MACE

GitOrigin-RevId: 350ca7d15a855085a537b1826692ebc34aa628f1
上级 f4694669
......@@ -48,7 +48,7 @@ First of all, send all files to the executed device:
- opencl library(something like libOpenCL.so, libmali.so or libEGL.so ...) if you want to run it on GPU
```
RUNTIME=GPU OPENCPATH=/path/to/opencl DATAFORMAT=NCHW /path/to/load_and_run /path/to/resnet_50.mdl --c-opr-lib /path/to/libmace_loader.so
MGB_MACE_RUNTIME=GPU MGB_MACE_OPENCL_PATH=/path/to/opencl MGB_MACE_LOADER_FORMAT=NCHW /path/to/load_and_run /path/to/resnet_50.mdl --c-opr-lib /path/to/libmace_loader.so
```
RUNTIME candidates:
......@@ -56,8 +56,10 @@ RUNTIME candidates:
- CPU
- GPU
Running with GPU runtime on android needs opencl library, one can set `OPENCLPATH` by using environment variable
Running with GPU runtime on android needs opencl library, one can set `MGB_MACE_OPENCL_PATH` by using environment variable
We mainly use NCHW data format, if you have NHWC model, use environment `DATAFORMAT=NHWC`
We mainly use NCHW data format, if you have NHWC model, use environment `MGB_MACE_LOADER_FORMAT=NHWC`
For CPU runtime, default running thread is 1, could be specified with `MGB_MACE_NR_THREADS=n`
if you want to run with HEXAGON runtime, more efforts should be made, please check [here](https://mace.readthedocs.io/en/latest/faq.html#why-is-mace-not-working-on-dsp).
......@@ -11,10 +11,57 @@
#include <numeric>
#include <iostream>
#include <sys/stat.h>
#include "mace/public/mace.h"
#include "extern_c_opr.h"
#if defined(__APPLE__) || defined(__MACOSX)
static const char* default_so_paths[] = {
"/System/Library/Frameworks/OpenCL.framework/OpenCL", "libOpenCL.so"};
#elif defined(__ANDROID__)
static const char* default_so_paths[] = {
#if defined(__aarch64__)
"/system/lib64/libOpenCL.so",
"/system/lib64/libOpenCL_system.so",
"/system/lib64/egl/libGLES_mali.so",
"/system/vendor/lib64/libOpenCL.so",
"/system/vendor/lib64/egl/libGLES_mali.so",
"/system/vendor/lib64/libPVROCL.so",
"/vendor/lib64/libOpenCL.so",
"/data/data/org.pocl.libs/files/lib64/libpocl.so",
#else
"/system/lib/libOpenCL.so",
"/system/lib/libOpenCL_system.so",
"/system/lib/egl/libGLES_mali.so",
"/system/vendor/lib/libOpenCL.so",
"/system/vendor/lib/egl/libGLES_mali.so",
"/system/vendor/lib/libPVROCL.so",
"/vendor/lib/libOpenCL.so",
"/data/data/org.pocl.libs/files/lib/libpocl.so",
#endif
"libOpenCL.so"};
#elif defined(_WIN32)
static const char* default_so_paths[] = {"OpenCL.dll"};
#elif defined(__linux__)
static const char* default_so_paths[] = {
#if defined(__x86_64__) || defined(__amd64__)
"/usr/lib64/libOpenCL.so", "/usr/local/lib64/libOpenCL.so",
"/usr/local/cuda/lib64/libOpenCL.so",
"/opt/intel/opencl/libOpenCL.so",
//! As in some system like apex, the driver exists here
"/usr/lib/libOpenCL.so",
#else
"/usr/lib/libOpenCL.so",
"/usr/lib32/libOpenCL.so",
"/usr/local/lib/libOpenCL.so",
"/usr/local/lib/libpocl.so",
"/usr/local/cuda/lib/libOpenCL.so",
#endif
"libOpenCL.so"};
#endif
#define ASSERT(x, msg) \
do { \
if (!(x)) { \
......@@ -24,6 +71,10 @@
} \
} while (0)
inline bool file_exists (const char* name) {
struct stat buffer;
return (stat (name, &buffer) == 0);
}
class MGBOprDescImpl {
struct UserData {
......@@ -78,8 +129,8 @@ class MGBOprDescImpl {
std::map<std::string, mace::MaceTensor> mace_outputs;
auto mace_data_format = mace::DataFormat::NCHW;
char *data_format = getenv("DATAFORMAT");
if (!strcmp(data_format, "NHWC")) {
char *data_format = getenv("MGB_MACE_LOADER_FROAMT");
if (data_format != nullptr && !strcmp(data_format, "NHWC")) {
mace_data_format = mace::DataFormat::NHWC;
}
......@@ -132,21 +183,41 @@ public:
auto ud = std::make_unique<UserData>();
std::shared_ptr<mace::MaceEngine> engine;
mace::DeviceType device_type;
char *runtime_mode = getenv("RUNTIME");
if (!strcmp(runtime_mode, "GPU")) {
mace::DeviceType device_type = mace::DeviceType::CPU;
char *runtime_mode = getenv("MGB_MACE_RUNTIME");
if (runtime_mode != nullptr && !strcmp(runtime_mode, "GPU")) {
device_type = mace::DeviceType::GPU;
} else {
device_type = mace::DeviceType::CPU;
}
mace::MaceEngineConfig config(device_type);
// set number of threads for cpu, default 1
if (device_type == mace::DeviceType::CPU) {
int nthread = 1;
char *str_nthread = getenv("MGB_MACE_NR_THREADS");
if (str_nthread != nullptr) {
nthread = atoi(str_nthread);
}
config.SetCPUThreadPolicy(nthread, mace::CPUAffinityPolicy::AFFINITY_NONE);
}
// set gpu context, mainly opencl path
if (device_type == mace::DeviceType::GPU) {
std::shared_ptr<mace::GPUContext> gpu_context;
char *opencl_path = getenv("OPENCLPATH");
char *opencl_path = getenv("MGB_MACE_OPENCL_PATH");
// check default opencl paths
if (opencl_path == nullptr) {
for (size_t i = 0; i < (sizeof(default_so_paths) / sizeof(char*)); i++) {
if (file_exists(default_so_paths[i])) {
opencl_path = const_cast<char *>(default_so_paths[i]);
break;
}
}
}
ASSERT(opencl_path, "Please set opencl library path");
std::string storage_path(opencl_path);
gpu_context = mace::GPUContextBuilder()
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册