-
Notifications
You must be signed in to change notification settings - Fork 316
/
trt_dep.hpp
108 lines (77 loc) · 2.2 KB
/
trt_dep.hpp
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#ifndef _TRT_DEP_HPP_
#define _TRT_DEP_HPP_
#include "NvInfer.h"
#include "NvOnnxParser.h"
#include "NvInferPlugin.h"
#include <cuda_runtime_api.h>
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include "argmax_plugin.h"
using std::string;
using std::vector;
using std::cout;
using std::endl;
using nvinfer1::ICudaEngine;
using nvinfer1::ILogger;
using nvinfer1::IRuntime;
using Severity = nvinfer1::ILogger::Severity;
void CHECK(bool success, string msg);
class Logger: public ILogger {
public:
void log(Severity severity, const char* msg) noexcept override {
if (severity != Severity::kINFO) {
std::cout << msg << std::endl;
}
}
};
struct TrtDeleter {
template <typename T>
void operator()(T* obj) const {
delete obj;
}
};
struct CudaStreamDeleter {
void operator()(cudaStream_t* stream) const {
cudaStreamDestroy(*stream);
}
};
template <typename T>
using TrtUnqPtr = std::unique_ptr<T, TrtDeleter>;
using CudaStreamUnqPtr = std::unique_ptr<cudaStream_t, CudaStreamDeleter>;
using TrtSharedEnginePtr = std::shared_ptr<ICudaEngine>;
extern Logger gLogger;
struct SemanticSegmentTrt {
public:
TrtSharedEnginePtr engine;
CudaStreamUnqPtr stream;
TrtUnqPtr<IRuntime> runtime;
std::unique_ptr<ArgMaxPluginCreator> plugin_creator;
string input_name;
string output_name;
int opt_bsize{1};
SemanticSegmentTrt():
engine(nullptr), runtime(nullptr), stream(nullptr) {
stream.reset(new cudaStream_t);
auto fail = cudaStreamCreate(stream.get());
CHECK(!fail, "create stream failed");
register_plugins();
}
~SemanticSegmentTrt() {
engine.reset();
runtime.reset();
stream.reset();
}
void register_plugins();
void set_opt_batch_size(int bs);
void serialize(string save_path);
void deserialize(string serpth);
void parse_to_engine(string onnx_path, string quant,
string data_root, string data_file);
vector<int32_t> inference(vector<float>& data);
void test_speed_fps();
vector<int> get_input_shape();
vector<int> get_output_shape();
};
#endif