rstrt-sys 0.1.0

rust tensorrt
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include "wrapper.h"

#include <NvInfer.h>
#include <cuda_runtime_api.h>

#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <vector>

namespace {

class Logger : public nvinfer1::ILogger
{
public:
    void log(Severity severity, const char* msg) noexcept override
    {
        // Surface errors + warnings to stderr for debugging.
        if (severity <= Severity::kWARNING)
            std::fprintf(stderr, "[TRT] %s\n", msg);
    }
};

// Thread-local error message, reset by each fallible call.
thread_local std::string g_err;

const char* err(const std::string& msg)
{
    g_err = msg;
    return g_err.c_str();
}

struct TensorBuf
{
    std::string name;
    int32_t mode = 0;     // TrtIoMode
    int32_t dtype = 0;    // nvinfer1::DataType as int
    int32_t ndims = 0;
    std::vector<int64_t> dims;

    void* pinned = nullptr;
    void* device = nullptr;
    int64_t bytes = 0;
};

}  // namespace

// Global definition of the opaque handle forward-declared in wrapper.h.
struct TrtInfer
{
    nvinfer1::IRuntime* runtime = nullptr;
    nvinfer1::ICudaEngine* engine = nullptr;
    nvinfer1::IExecutionContext* context = nullptr;
    cudaStream_t stream = nullptr;

    std::vector<std::string> io_names;                 // engine order
    std::map<std::string, TensorBuf> tensors;          // by name
};

namespace {

void* read_file(const char* path, size_t& out_size)
{
    std::vector<char> data;
    {
        FILE* f = std::fopen(path, "rb");
        if (!f)
        {
            err(std::string("failed to open engine file: ") + path);
            return nullptr;
        }
        std::fseek(f, 0, SEEK_END);
        size_t size = static_cast<size_t>(std::ftell(f));
        std::fseek(f, 0, SEEK_SET);
        data.resize(size);
        if (size && std::fread(data.data(), 1, size, f) != size)
        {
            std::fclose(f);
            err("failed to read engine file");
            return nullptr;
        }
        std::fclose(f);
        out_size = size;
    }
    void* buf = std::malloc(out_size);
    std::memcpy(buf, data.data(), out_size);
    return buf;
}

nvinfer1::IRuntime* make_runtime()
{
    static Logger logger;
    auto* runtime = nvinfer1::createInferRuntime(logger);
    if (!runtime)
        err("failed to create TensorRT runtime");
    return runtime;
}

// Element size in bytes for a TensorRT data type.
size_t elem_size(int32_t dtype)
{
    using DT = nvinfer1::DataType;
    switch (static_cast<DT>(dtype))
    {
        case DT::kFLOAT:
        case DT::kINT32:
            return 4;
        case DT::kINT64:
            return 8;
        case DT::kHALF:
        case DT::kBF16:
            return 2;
        case DT::kINT8:
        case DT::kUINT8:
        case DT::kBOOL:
        case DT::kFP8:
            return 1;
        case DT::kINT4:
        case DT::kFP4:
            return 1;  // packed; byte size still bounded by element count
        default:
            return 4;
    }
}

}  // namespace

extern "C" {

const char* trt_infer_last_error(void)
{
    return g_err.empty() ? "" : g_err.c_str();
}

TrtInfer* trt_infer_create(const char* engine_path)
{
    g_err.clear();

    size_t size = 0;
    void* blob = read_file(engine_path, size);
    if (!blob)
        return nullptr;

    auto* h = new (std::nothrow) TrtInfer();
    if (!h)
    {
        err("out of memory");
        std::free(blob);
        return nullptr;
    }

    h->runtime = make_runtime();
    if (!h->runtime)
    {
        delete h;
        std::free(blob);
        return nullptr;
    }

    h->engine = h->runtime->deserializeCudaEngine(blob, size);
    std::free(blob);
    if (!h->engine)
    {
        err("failed to deserialize engine");
        delete h;
        return nullptr;
    }

    h->context = h->engine->createExecutionContext();
    if (!h->context)
    {
        err("failed to create execution context");
        delete h;
        return nullptr;
    }

    if (cudaStreamCreate(&h->stream) != cudaSuccess)
    {
        err(std::string("cudaStreamCreate failed: ") + cudaGetErrorString(cudaGetLastError()));
        delete h;
        return nullptr;
    }

    if (!h->context->setOptimizationProfileAsync(0, h->stream))
    {
        err("failed to set optimization profile");
        delete h;
        return nullptr;
    }

    int nb = h->engine->getNbIOTensors();
    h->io_names.reserve(nb);
    for (int i = 0; i < nb; ++i)
    {
        const char* name = h->engine->getIOTensorName(i);
        TensorBuf tb;
        tb.name = name;
        tb.mode = static_cast<int32_t>(h->engine->getTensorIOMode(name));
        tb.dtype = static_cast<int32_t>(h->engine->getTensorDataType(name));

        nvinfer1::Dims d = h->context->getTensorShape(name);
        tb.ndims = d.nbDims;
        for (int j = 0; j < d.nbDims; ++j)
            tb.dims.push_back(d.d[j]);

        h->io_names.push_back(tb.name);
        h->tensors[tb.name] = std::move(tb);
    }

    return h;
}

void trt_infer_free(TrtInfer* h)
{
    if (!h)
        return;
    for (auto& [name, tb] : h->tensors)
    {
        if (tb.pinned)
            cudaFreeHost(tb.pinned);
        if (tb.device)
            cudaFree(tb.device);
    }
    if (h->stream)
        cudaStreamDestroy(h->stream);
    delete h->context;
    delete h->engine;
    delete h->runtime;
    delete h;
}

int32_t trt_infer_nb_io(TrtInfer* h)
{
    if (!h)
        return 0;
    return static_cast<int32_t>(h->io_names.size());
}

const char* trt_infer_get_io_name(TrtInfer* h, int32_t i)
{
    if (!h || i < 0 || i >= static_cast<int32_t>(h->io_names.size()))
        return "";
    return h->io_names[i].c_str();
}

int32_t trt_infer_get_io_mode(TrtInfer* h, int32_t i)
{
    if (!h || i < 0 || i >= static_cast<int32_t>(h->io_names.size()))
        return -1;
    return h->tensors.at(h->io_names[i]).mode;
}

int32_t trt_infer_get_io_dtype(TrtInfer* h, int32_t i)
{
    if (!h || i < 0 || i >= static_cast<int32_t>(h->io_names.size()))
        return -1;
    return h->tensors.at(h->io_names[i]).dtype;
}

int32_t trt_infer_get_io_ndims(TrtInfer* h, int32_t i)
{
    if (!h || i < 0 || i >= static_cast<int32_t>(h->io_names.size()))
        return -1;
    return h->tensors.at(h->io_names[i]).ndims;
}

int32_t trt_infer_get_io_dims(TrtInfer* h, int32_t i, int64_t* out, int32_t max_len)
{
    if (!h || i < 0 || i >= static_cast<int32_t>(h->io_names.size()) || !out || max_len <= 0)
        return -1;
    const auto& tb = h->tensors.at(h->io_names[i]);
    int32_t n = std::min(tb.ndims, max_len);
    for (int32_t j = 0; j < n; ++j)
        out[j] = tb.dims[j];
    return tb.ndims;
}

int32_t trt_infer_alloc(TrtInfer* h, const char* name, const int64_t* dims, int32_t ndims)
{
    if (!h || !name || !dims || ndims <= 0)
        return TRT_ERR_GENERIC;

    auto it = h->tensors.find(name);
    if (it == h->tensors.end())
    {
        err(std::string("unknown tensor: ") + name);
        return TRT_ERR_NOT_FOUND;
    }
    TensorBuf& tb = it->second;
    if (tb.device)
        return TRT_OK;  // already allocated

    size_t numel = 1;
    for (int32_t j = 0; j < ndims; ++j)
        numel *= static_cast<size_t>(dims[j] < 0 ? 1 : dims[j]);
    size_t nbytes = numel * elem_size(tb.dtype);

    if (cudaHostAlloc(&tb.pinned, nbytes, cudaHostAllocDefault) != cudaSuccess)
    {
        err(std::string("cudaHostAlloc failed: ") + cudaGetErrorString(cudaGetLastError()));
        return TRT_ERR_CUDA;
    }
    if (cudaMalloc(&tb.device, nbytes) != cudaSuccess)
    {
        err(std::string("cudaMalloc failed: ") + cudaGetErrorString(cudaGetLastError()));
        cudaFreeHost(tb.pinned);
        tb.pinned = nullptr;
        return TRT_ERR_CUDA;
    }

    tb.bytes = static_cast<int64_t>(nbytes);
    tb.ndims = ndims;
    tb.dims.assign(dims, dims + ndims);

    if (!h->context->setTensorAddress(name, tb.device))
    {
        err(std::string("setTensorAddress failed for ") + name);
        cudaFree(tb.device);
        cudaFreeHost(tb.pinned);
        tb.device = tb.pinned = nullptr;
        return TRT_ERR_GENERIC;
    }

    if (tb.mode == kTrtIoInput)
    {
        nvinfer1::Dims d;
        d.nbDims = ndims;
        for (int32_t j = 0; j < ndims; ++j)
            d.d[j] = dims[j];
        if (!h->context->setInputShape(name, d))
        {
            err(std::string("setInputShape failed for ") + name);
            cudaFree(tb.device);
            cudaFreeHost(tb.pinned);
            tb.device = tb.pinned = nullptr;
            return TRT_ERR_GENERIC;
        }
    }

    g_err.clear();
    return TRT_OK;
}

uintptr_t trt_infer_pinned_ptr(TrtInfer* h, const char* name)
{
    if (!h || !name)
        return 0;
    auto it = h->tensors.find(name);
    if (it == h->tensors.end())
        return 0;
    return reinterpret_cast<uintptr_t>(it->second.pinned);
}

int64_t trt_infer_byte_size(TrtInfer* h, const char* name)
{
    if (!h || !name)
        return 0;
    auto it = h->tensors.find(name);
    if (it == h->tensors.end())
        return 0;
    return it->second.bytes;
}

int32_t trt_infer_infer(TrtInfer* h)
{
    if (!h)
        return TRT_ERR_GENERIC;

    g_err.clear();
    cudaStream_t s = h->stream;

    for (const auto& name : h->io_names)
    {
        const TensorBuf& tb = h->tensors.at(name);
        if (!tb.device)
            continue;  // not allocated; skip
        if (tb.mode == kTrtIoInput)
        {
            if (cudaMemcpyAsync(tb.device, tb.pinned, tb.bytes, cudaMemcpyHostToDevice, s) != cudaSuccess)
            {
                err(std::string("H2D failed for ") + name + ": " + cudaGetErrorString(cudaGetLastError()));
                return TRT_ERR_CUDA;
            }
        }
    }

    if (!h->context->enqueueV3(s))
    {
        err("enqueueV3 failed");
        return TRT_ERR_GENERIC;
    }

    for (const auto& name : h->io_names)
    {
        const TensorBuf& tb = h->tensors.at(name);
        if (!tb.device)
            continue;
        if (tb.mode == kTrtIoOutput)
        {
            if (cudaMemcpyAsync(tb.pinned, tb.device, tb.bytes, cudaMemcpyDeviceToHost, s) != cudaSuccess)
            {
                err(std::string("D2H failed for ") + name + ": " + cudaGetErrorString(cudaGetLastError()));
                return TRT_ERR_CUDA;
            }
        }
    }

    if (cudaStreamSynchronize(s) != cudaSuccess)
    {
        err(std::string("cudaStreamSynchronize failed: ") + cudaGetErrorString(cudaGetLastError()));
        return TRT_ERR_CUDA;
    }

    g_err.clear();
    return TRT_OK;
}

}  // extern "C"