Repository navigation
Expand file tree
/
Copy pathsession.cpp
More file actions
75 lines (69 loc) · 2.32 KB
/
Copy pathsession.cpp
File metadata and controls
75 lines (69 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/************************************************
* Copyright MaybeShewill-CV. All Rights Reserved.
* Author: MaybeShewill-CV
* File: session.cpp
* Date: 26-8-20
************************************************/
#include "models/backend/session.h"
#include "glog/logging.h"
#include "models/backend/mnn_session.h"
#include "models/backend/ort_session.h"
#ifdef MORTRED_HAS_TRT
#include "models/backend/trt_session.h"
#endif
namespace mortred {
namespace models {
namespace backend {
using mortred::common::StatusCode;
std::unique_ptr<InferenceSession> InferenceSession::create(const BackendConfig& config,
std::string* err) {
if (err != nullptr) {
err->clear();
}
std::unique_ptr<InferenceSession> session;
if (config.is_mnn()) {
auto mnn_session = std::make_unique<MnnSession>();
const auto status = mnn_session->init(config, err);
if (status != StatusCode::OK) {
return nullptr;
}
session = std::move(mnn_session);
} else if (config.is_onnx()) {
auto ort_session = std::make_unique<OrtSession>();
const auto status = ort_session->init(config, err);
if (status != StatusCode::OK) {
return nullptr;
}
session = std::move(ort_session);
} else if (config.is_tensorrt()) {
if (config.device == "cpu") {
if (err != nullptr) {
*err = "tensorrt backend requires device=gpu; device=cpu is a configuration error";
}
return nullptr;
}
#ifdef MORTRED_HAS_TRT
auto trt_session = std::make_unique<TrtSession>();
const auto status = trt_session->init(config, err);
if (status != StatusCode::OK) {
return nullptr;
}
session = std::move(trt_session);
#else
if (err != nullptr) {
*err = "tensorrt backend is not compiled into this build (cpu profile); "
"use an mnn/onnx backend config or switch to the gpu profile";
}
return nullptr;
#endif
} else {
if (err != nullptr) {
*err = "unknown backend type '" + config.type + "', expected mnn | onnx | tensorrt";
}
return nullptr;
}
return session;
}
} // namespace backend
} // namespace models
} // namespace mortred