-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathutils.h
More file actions
95 lines (83 loc) · 2.58 KB
/
utils.h
File metadata and controls
95 lines (83 loc) · 2.58 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
#pragma once
#include <cuda_runtime_api.h>
#include <cassert>
#include <fstream>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
#include "macros.h"
using namespace nvinfer1;
constexpr const std::size_t WORKSPACE_SIZE = 16 << 20;
#define CHECK(status) \
do { \
auto ret = (status); \
if (ret != cudaSuccess) { \
std::cerr << "Cuda failure: " << ret << "\n"; \
std::abort(); \
} \
} while (0)
static void checkTrtEnv(int device = 0) {
#if TRT_VERSION < 8000
CHECK(cudaGetDevice(&device));
cudaDeviceProp prop{};
CHECK(cudaGetDeviceProperties(&prop, device));
const int sm = prop.major * 10 + prop.minor;
if (sm > 86) {
std::cerr << "TensorRT < 8 does not support SM > 86 on this GPU.";
std::abort();
}
#endif
}
/**
* @brief TensorRT weight files have a simple space delimited format:
* [type] [size] <data x size in hex>
*
* @param file input weight file path
* @return std::map<std::string, nvinfer1::Weights>
*/
static auto loadWeights(const std::string& file) {
std::cout << "Loading weights: " << file << "\n";
std::map<std::string, nvinfer1::Weights> weightMap;
// Open weights file
std::ifstream input(file);
assert(input.is_open() && "Unable to load weight file.");
// Read number of weight blobs
int32_t count;
input >> count;
assert(count > 0 && "Invalid weight map file.");
while (count--) {
nvinfer1::Weights wt{nvinfer1::DataType::kFLOAT, nullptr, 0};
// Read name and type of blob
std::string name;
input >> name >> std::dec >> wt.count;
// Load blob
auto* val = new uint32_t[wt.count];
input >> std::hex;
for (auto x = 0ll; x < wt.count; ++x) {
input >> val[x];
}
wt.values = val;
weightMap[name] = wt;
}
return weightMap;
}
static size_t getSize(DataType dt) {
switch (dt) {
#if TRT_VERSION >= 8510
case DataType::kUINT8:
#endif
case DataType::kINT8:
return sizeof(int8_t);
case DataType::kFLOAT:
return sizeof(float);
case DataType::kHALF:
return sizeof(int16_t);
case DataType::kINT32:
return sizeof(int32_t);
default: {
std::cerr << "Unsupported data type\n";
std::abort();
}
}
}