forked from Smorodov/Multitarget-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampleInference.h
More file actions
263 lines (216 loc) · 8.1 KB
/
sampleInference.h
File metadata and controls
263 lines (216 loc) · 8.1 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
* SPDX-FileCopyrightText: Copyright (c) 1993-2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TRT_SAMPLE_INFERENCE_H
#define TRT_SAMPLE_INFERENCE_H
#include "sampleDevice.h"
#include "sampleEngines.h"
#include "sampleReporting.h"
#include "sampleUtils.h"
#include <functional>
#include <iostream>
#include <list>
#include <memory>
#include <string>
#include <vector>
namespace sample
{
#if (NV_TENSORRT_MAJOR > 8)
// IDebugListener class for writing debug tensors to output file.
class DebugTensorWriter : public nvinfer1::IDebugListener
{
public:
DebugTensorWriter(std::unordered_map<std::string, std::string> fileNames)
: mDebugTensorFileNames(fileNames)
{
}
bool processDebugTensor(void const* addr, nvinfer1::TensorLocation location, nvinfer1::DataType type,
nvinfer1::Dims const& shape, char const* name, cudaStream_t stream) override;
private:
std::unordered_map<std::string, std::string> mDebugTensorFileNames;
};
#endif
struct InferenceEnvironment
{
InferenceEnvironment() = delete;
InferenceEnvironment(InferenceEnvironment const& other) = delete;
InferenceEnvironment(InferenceEnvironment&& other) = delete;
InferenceEnvironment(BuildEnvironment& bEnv) : engine(std::move(bEnv.engine)), safe(bEnv.engine.isSafe())
{
}
LazilyDeserializedEngine engine;
std::unique_ptr<Profiler> profiler;
std::vector<std::unique_ptr<nvinfer1::IExecutionContext>> contexts;
std::vector<TrtDeviceBuffer>
deviceMemory; //< Device memory used for inference when the allocation strategy is not static.
std::vector<std::unique_ptr<Bindings>> bindings;
#if (NV_TENSORRT_MAJOR > 8)
std::unique_ptr<DebugTensorWriter> listener;
#endif
bool error{false};
bool safe{false};
inline nvinfer1::IExecutionContext* getContext(int32_t streamIdx);
//! Storage for input shape tensors.
//!
//! It's important that the addresses of the data do not change between the calls to
//! setTensorAddress/setInputShape (which tells TensorRT where the input shape tensor is)
//! and enqueueV3 (when TensorRT might use the input shape tensor).
//!
//! The input shape tensors could alternatively be handled via member bindings,
//! but it simplifies control-flow to store the data here since it's shared across
//! the bindings.
std::list<std::vector<int32_t>> inputShapeTensorValues;
};
inline nvinfer1::IExecutionContext* InferenceEnvironment::getContext(int32_t streamIdx)
{
return contexts[streamIdx].get();
}
//!
//! \brief Set up contexts and bindings for inference
//!
bool setUpInference(InferenceEnvironment& iEnv, InferenceOptions const& inference, SystemOptions const& system);
//!
//! \brief Deserialize the engine and time how long it takes.
//!
bool timeDeserialize(InferenceEnvironment& iEnv, SystemOptions const& sys);
//!
//! \brief Run inference and collect timing, return false if any error hit during inference
//!
bool runInference(
InferenceOptions const& inference, InferenceEnvironment& iEnv, int32_t device, std::vector<InferenceTrace>& trace);
//!
//! \brief Get layer information of the engine.
//!
std::string getLayerInformation(
nvinfer1::ICudaEngine* engine, nvinfer1::IExecutionContext* context, nvinfer1::LayerInformationFormat format);
struct Binding
{
bool isInput{false};
std::unique_ptr<IMirroredBuffer> buffer;
std::unique_ptr<OutputAllocator> outputAllocator;
int64_t volume{0};
nvinfer1::DataType dataType{nvinfer1::DataType::kFLOAT};
void fill(std::string const& fileName);
void fill();
void dump(std::ostream& os, nvinfer1::Dims dims, nvinfer1::Dims strides, int32_t vectorDim, int32_t spv,
std::string const separator = " ") const;
};
struct TensorInfo
{
int32_t bindingIndex{-1};
char const* name{nullptr};
nvinfer1::Dims dims{};
bool isDynamic{};
int32_t comps{-1};
nvinfer1::Dims strides{};
int32_t vectorDimIndex{-1};
bool isInput{};
nvinfer1::DataType dataType{};
int64_t vol{-1};
void updateVolume(int32_t batch)
{
vol = volume(dims, strides, vectorDimIndex, comps, batch);
}
};
class Bindings
{
public:
Bindings() = delete;
explicit Bindings(bool useManaged)
: mUseManaged(useManaged)
{
}
void addBinding(TensorInfo const& tensorInfo, std::string const& fileName = "");
void** getDeviceBuffers();
void transferInputToDevice(TrtCudaStream& stream);
void transferOutputToHost(TrtCudaStream& stream);
void fill(int binding, std::string const& fileName)
{
mBindings[binding].fill(fileName);
}
void fill(int binding)
{
mBindings[binding].fill();
}
void dumpBindingDimensions(
std::string const& name, nvinfer1::IExecutionContext const& context, std::ostream& os) const;
void dumpBindingValues(nvinfer1::IExecutionContext const& context, int32_t binding, std::ostream& os,
std::string const& separator = " ", int32_t batch = 1) const;
void dumpRawBindingToFiles(nvinfer1::IExecutionContext const& context, std::ostream& os) const;
void dumpInputs(nvinfer1::IExecutionContext const& context, std::ostream& os) const
{
auto isInput = [](Binding const& b) { return b.isInput; };
dumpBindings(context, isInput, os);
}
void dumpOutputs(nvinfer1::IExecutionContext const& context, std::ostream& os) const;
void dumpBindings(nvinfer1::IExecutionContext const& context, std::ostream& os) const
{
auto all = [](Binding const& b) { return true; };
dumpBindings(context, all, os);
}
void dumpBindings(nvinfer1::IExecutionContext const& context, std::function<bool(Binding const&)> predicate,
std::ostream& os) const
{
for (auto const& n : mNames)
{
auto const name = n.first;
auto const binding = n.second;
if (predicate(mBindings[binding]))
{
os << n.first << ": (";
dumpBindingDimensions(name, context, os);
os << ")" << std::endl;
dumpBindingValues(context, binding, os);
os << std::endl;
}
}
}
std::unordered_map<std::string, int> getInputBindings() const
{
auto isInput = [](Binding const& b) { return b.isInput; };
return getBindings(isInput);
}
std::unordered_map<std::string, int> getOutputBindings() const
{
auto isOutput = [](Binding const& b) { return !b.isInput; };
return getBindings(isOutput);
}
std::unordered_map<std::string, int> getBindings() const
{
auto all = [](Binding const& b) { return true; };
return getBindings(all);
}
std::unordered_map<std::string, int> getBindings(std::function<bool(Binding const&)> predicate) const;
bool setTensorAddresses(nvinfer1::IExecutionContext& context) const;
private:
std::unordered_map<std::string, int32_t> mNames;
std::vector<Binding> mBindings;
std::vector<void*> mDevicePointers;
bool mUseManaged{false};
};
struct TaskInferenceEnvironment
{
TaskInferenceEnvironment(std::string engineFile, InferenceOptions inference, int32_t deviceId = 0,
int32_t DLACore = -1, int32_t bs = batchNotProvided);
InferenceOptions iOptions{};
int32_t device{defaultDevice};
int32_t batch{batchNotProvided};
std::unique_ptr<InferenceEnvironment> iEnv;
std::vector<InferenceTrace> trace;
};
bool runMultiTasksInference(std::vector<std::unique_ptr<TaskInferenceEnvironment>>& tEnvList);
} // namespace sample
#endif // TRT_SAMPLE_INFERENCE_H