trtx-sys 0.5.0

Raw FFI bindings to NVIDIA TensorRT-RTX (EXPERIMENTAL - NOT FOR PRODUCTION)
Documentation
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/**
 * Logger Bridge for TensorRT-RTX Rust Bindings
 *
 * This file provides C wrapper functions for TensorRT-RTX C++ API.
 * While we use autocxx for most C++ bindings, some wrappers are still
 * necessary.
 *
 * ## Architecture
 *
 * ```
 * Rust (trtx) → Raw FFI (trtx-sys) → logger_bridge.cpp → TensorRT C++ + autocxx
 * ```
 *
 * ## Why These Wrappers Exist
 *
 * ### NECESSARY WRAPPERS (Cannot be removed):
 *
 * 1. **Logger Bridge (lines 29-52)**:
 *    - Rust cannot implement C++ virtual classes
 *    - RustLoggerImpl forwards virtual method calls to Rust callbacks
 *    - REQUIRED: No alternative
 *
 * 2. **Factory Functions (lines 55-91)**:
 *    - createInferBuilder/Runtime take `ILogger&` references
 *    - autocxx struggles with C++ reference parameters
 *    - REQUIRED: Simplest solution for reference params
 *
 * 3. **CUDA Wrappers (lines 658-677)**:
 *    - Bridge between std::ffi::c_void and autocxx::c_void
 *    - Type compatibility issue between Rust and autocxx types
 *    - KEEP FOR NOW: Could be removed with codebase-wide type migration
 *
 * ### POTENTIALLY REDUNDANT WRAPPERS:
 *
 * 4. **TensorRT Method Wrappers (lines 94-657)**:
 *    - Builder, Network, Tensor, Engine, Context methods
 *    - autocxx CAN generate these with
 * `generate!("nvinfer1::INetworkDefinition")`
 *    - POTENTIALLY REMOVABLE: ~75% code reduction if refactored
 *    - STATUS: Kept for now due to stability, could be migrated to direct
 * autocxx calls
 *
 * ## Why Not Full autocxx?
 *
 * We TRIED to use autocxx for everything but encountered:
 * - Type mismatches (autocxx::c_void vs std::ffi::c_void)
 * - Reference parameter handling issues
 * - Virtual method/callback complications
 *
 * ## See Also
 * - docs/LOGGER_BRIDGE_ANALYSIS.md - Detailed analysis of each function
 * - docs/REFACTORING_SUMMARY.md - Test results and recommendations
 * - docs/FFI_GUIDE.md - How to modify bindings
 */

#include "logger_bridge.hpp"
#include <NvInferRuntime.h>
#include <NvOnnxParser.h>
#include <cstdint>
#include <cstring>

//==============================================================================
// SECTION 1: LOGGER BRIDGE (NECESSARY - Virtual Methods)
//==============================================================================
// Rust cannot implement C++ virtual classes, so we need this C++ class
// to forward ILogger::log() calls back to Rust via function pointer callbacks.

// C++ implementation of ILogger that bridges to Rust
class RustLoggerImpl : public nvinfer1::ILogger {
public:
  RustLoggerImpl(RustLogCallback callback, void *user_data)
      : callback_(callback), user_data_(user_data) {}

  void log(int32_t severity, const char *msg) noexcept override {
    if (callback_) {
      callback_(user_data_, static_cast<int32_t>(severity), msg);
    }
  }

private:
  RustLogCallback callback_;
  void *user_data_;
};

// Opaque struct that holds the logger implementation
struct RustLoggerBridge {
  RustLoggerImpl *impl;
};

extern "C" {

RustLoggerBridge *create_rust_logger_bridge(RustLogCallback callback,
                                            void *user_data) {
  if (!callback) {
    return nullptr;
  }

  try {
    auto *bridge = new RustLoggerBridge();
    bridge->impl = new RustLoggerImpl(callback, user_data);
    return bridge;
  } catch (...) {
    return nullptr;
  }
}

void destroy_rust_logger_bridge(RustLoggerBridge *logger) {
  if (logger) {
    delete logger->impl;
    delete logger;
  }
}

nvinfer1::ILogger *get_logger_interface(RustLoggerBridge *logger) {
  return logger ? logger->impl : nullptr;
}

//==============================================================================
// SECTION 2: FACTORY FUNCTIONS (NECESSARY - Reference Parameters)
//==============================================================================
// These functions take `ILogger&` references which autocxx struggles with.
// Simpler to keep these thin wrappers than to work around autocxx limitations.

// Factory functions for TensorRT
#ifdef TRTX_LINK_TENSORRT_RTX
void *create_infer_builder(void *logger) {
  if (!logger) {
    return nullptr;
  }
  try {
    auto *ilogger = static_cast<nvinfer1::ILogger *>(logger);
    return nvinfer1::createInferBuilder(*ilogger);
  } catch (...) {
    return nullptr;
  }
}

void *create_infer_runtime(void *logger) {
  if (!logger) {
    return nullptr;
  }
  try {
    auto *ilogger = static_cast<nvinfer1::ILogger *>(logger);
    return nvinfer1::createInferRuntime(*ilogger);
  } catch (...) {
    return nullptr;
  }
}
void *create_infer_refitter(void *engine, void *logger) {
  if (!engine || !logger) {
    return nullptr;
  }
  try {
    auto *iengine = static_cast<nvinfer1::ICudaEngine *>(engine);
    auto *ilogger = static_cast<nvinfer1::ILogger *>(logger);
    return nvinfer1::createInferRefitter(*iengine, *ilogger);
  } catch (...) {
    return nullptr;
  }
}
#endif

#ifdef TRTX_LINK_TENSORRT_ONNXPARSER
// ONNX Parser factory function
void *create_onnx_parser(void *network, void *logger) {
  if (!network || !logger) {
    return nullptr;
  }
  try {
    auto *inetwork = static_cast<nvinfer1::INetworkDefinition *>(network);
    auto *ilogger = static_cast<nvinfer1::ILogger *>(logger);
    return nvonnxparser::createParser(*inetwork, *ilogger);
  } catch (...) {
    return nullptr;
  }
}
#endif

// Refitter methods that use char const** (pointer-to-pointer); autocxx cannot
// bind these.
int32_t trtx_refitter_get_missing(void *refitter, int32_t size,
                                  char const **layer_names, int32_t *roles) {
  if (!refitter || !layer_names || !roles)
    return 0;
  try {
    auto *ir = static_cast<nvinfer1::IRefitter *>(refitter);
    return ir->getMissing(size, layer_names,
                          reinterpret_cast<nvinfer1::WeightsRole *>(roles));
  } catch (...) {
    return 0;
  }
}

int32_t trtx_refitter_get_all(void *refitter, int32_t size,
                              char const **layer_names, int32_t *roles) {
  if (!refitter || !layer_names || !roles)
    return 0;
  try {
    auto *ir = static_cast<nvinfer1::IRefitter *>(refitter);
    return ir->getAll(size, layer_names,
                      reinterpret_cast<nvinfer1::WeightsRole *>(roles));
  } catch (...) {
    return 0;
  }
}

int32_t trtx_refitter_get_missing_weights(void *refitter, int32_t size,
                                          char const **weights_names) {
  if (!refitter || !weights_names)
    return 0;
  try {
    auto *ir = static_cast<nvinfer1::IRefitter *>(refitter);
    return ir->getMissingWeights(size, weights_names);
  } catch (...) {
    return 0;
  }
}

int32_t trtx_refitter_get_all_weights(void *refitter, int32_t size,
                                      char const **weights_names) {
  if (!refitter || !weights_names)
    return 0;
  try {
    auto *ir = static_cast<nvinfer1::IRefitter *>(refitter);
    return ir->getAllWeights(size, weights_names);
  } catch (...) {
    return 0;
  }
}

bool parser_parse(void *parser, const void *data, size_t size) {
  if (!parser || !data)
    return false;
  try {
    auto *iparser = static_cast<nvonnxparser::IParser *>(parser);
    return iparser->parse(data, size);
  } catch (...) {
    return false;
  }
}

int32_t parser_get_nb_errors(void *parser) {
  if (!parser)
    return 0;
  try {
    auto *iparser = static_cast<nvonnxparser::IParser *>(parser);
    return iparser->getNbErrors();
  } catch (...) {
    return 0;
  }
}

void *parser_get_error(void *parser, int32_t index) {
  if (!parser)
    return nullptr;
  try {
    auto *iparser = static_cast<nvonnxparser::IParser *>(parser);
    return const_cast<nvonnxparser::IParserError *>(iparser->getError(index));
  } catch (...) {
    return nullptr;
  }
}

const char *parser_error_desc(void *error) {
  if (!error)
    return nullptr;
  try {
    auto *ierror = static_cast<nvonnxparser::IParserError *>(error);
    return ierror->desc();
  } catch (...) {
    return nullptr;
  }
}

void *network_add_concatenation(void *network, void **inputs,
                                int32_t nb_inputs) {
  if (!network || !inputs || nb_inputs <= 0)
    return nullptr;
  try {
    auto *inetwork = static_cast<nvinfer1::INetworkDefinition *>(network);
    std::vector<nvinfer1::ITensor *> tensors;
    tensors.reserve(nb_inputs);
    for (int32_t i = 0; i < nb_inputs; ++i) {
      tensors.push_back(static_cast<nvinfer1::ITensor *>(inputs[i]));
    }
    auto *layer = inetwork->addConcatenation(tensors.data(), nb_inputs);
    return layer; // Return layer, not output tensor
  } catch (...) {
    return nullptr;
  }
}

uint32_t get_tensorrt_version() { return NV_TENSORRT_VERSION; }
uint32_t get_tensorrt_major_version() { return NV_TENSORRT_MAJOR; }
uint32_t get_tensorrt_minor_version() { return NV_TENSORRT_MINOR; }
uint32_t get_tensorrt_patch_version() { return NV_TENSORRT_PATCH; }
uint32_t get_nvonnxparser_version() { return NV_ONNX_PARSER_VERSION; }
uint32_t get_nvonnxparser_major_version() { return NV_ONNX_PARSER_MAJOR; }
uint32_t get_nvonnxparser_minor_version() { return NV_ONNX_PARSER_MINOR; }
uint32_t get_nvonnxparser_patch_version() { return NV_ONNX_PARSER_PATCH; }

namespace nvinfer1 {
class ProgressMonitor : public IProgressMonitor {
public:
  ProgressMonitor(void *self, void *phaseStart, void *stepComplete,
                  void *phaseFinish)
      : self(self), m_phaseStart((decltype(m_phaseStart))phaseStart),
        m_stepComplete((decltype(m_stepComplete))stepComplete),
        m_phaseFinish((decltype(m_phaseFinish))phaseFinish) {}
  ~ProgressMonitor() = default;
  void *self;
  void (*m_phaseStart)(void *self, char const *phaseName,
                       char const *parentPhase, int32_t nbSteps);
  bool (*m_stepComplete)(void *self, char const *phaseName, int32_t step);
  void (*m_phaseFinish)(void *self, char const *phaseName);

  void phaseStart(char const *phaseName, char const *parentPhase,
                  int32_t nbSteps) noexcept override {
    m_phaseStart(self, phaseName, parentPhase, nbSteps);
  };
  bool stepComplete(char const *phaseName, int32_t step) noexcept override {
    return m_stepComplete(self, phaseName, step);
  };
  void phaseFinish(char const *phaseName) noexcept override {
    m_phaseFinish(self, phaseName);
  };
};
} // namespace nvinfer1

void *trtx_create_progress_monitor(void *self, void *phaseStart,
                                   void *stepComplete, void *phaseFinish) {
  try {
    return new nvinfer1::ProgressMonitor(self, phaseStart, stepComplete,
                                         phaseFinish);
  } catch (...) {
    return nullptr;
  }
}
void trtx_destroy_progress_monitor(void *self) {
  delete (nvinfer1::ProgressMonitor *)(self);
}

//==============================================================================
// ErrorRecorder subclass (bridge to Rust RecordError)
//==============================================================================
namespace nvinfer1 {
class ErrorRecorderSubclass : public IErrorRecorder {
public:
  using ErrorCode = nvinfer1::ErrorCode;
  ErrorRecorderSubclass(void *self, int32_t (*getNbErrors)(void *),
                        int32_t (*getErrorCode)(void *, int32_t),
                        void (*getErrorDesc)(void *, int32_t, char *, size_t),
                        bool (*hasOverflowed)(void *), void (*clear)(void *),
                        bool (*reportError)(void *, int32_t, char const *),
                        int32_t (*incRefCount)(void *),
                        int32_t (*decRefCount)(void *))
      : self(self), m_getNbErrors(getNbErrors), m_getErrorCode(getErrorCode),
        m_getErrorDesc(getErrorDesc), m_hasOverflowed(hasOverflowed),
        m_clear(clear), m_reportError(reportError), m_incRefCount(incRefCount),
        m_decRefCount(decRefCount) {}
  ~ErrorRecorderSubclass() = default;

  void *self;
  int32_t (*m_getNbErrors)(void *);
  int32_t (*m_getErrorCode)(void *, int32_t);
  void (*m_getErrorDesc)(void *, int32_t, char *, size_t);
  bool (*m_hasOverflowed)(void *);
  void (*m_clear)(void *);
  bool (*m_reportError)(void *, int32_t, char const *);
  int32_t (*m_incRefCount)(void *);
  int32_t (*m_decRefCount)(void *);

  mutable std::string m_lastDesc;

  int32_t getNbErrors() const noexcept override { return m_getNbErrors(self); }
  int32_t getErrorCode(int32_t errorIdx) const noexcept override {
    return m_getErrorCode(self, errorIdx);
  }
  ErrorDesc getErrorDesc(int32_t errorIdx) const noexcept override {
    char buf[128];
    m_getErrorDesc(self, errorIdx, buf, sizeof(buf));
    m_lastDesc = buf;
    return m_lastDesc.c_str();
  }
  bool hasOverflowed() const noexcept override { return m_hasOverflowed(self); }
  void clear() noexcept override { m_clear(self); }
  bool reportError(int32_t val, ErrorDesc desc) noexcept override {
    return m_reportError(self, val, desc);
  }
  RefCount incRefCount() noexcept override { return m_incRefCount(self); }
  RefCount decRefCount() noexcept override { return m_decRefCount(self); }
};
} // namespace nvinfer1

void *trtx_create_error_recorder(void *self, void *getNbErrors,
                                 void *getErrorCode, void *getErrorDesc,
                                 void *hasOverflowed, void *clear,
                                 void *reportError, void *incRefCount,
                                 void *decRefCount) {
  try {
    return new nvinfer1::ErrorRecorderSubclass(
        self, (int32_t (*)(void *))getNbErrors,
        (int32_t (*)(void *, int32_t))getErrorCode,
        (void (*)(void *, int32_t, char *, size_t))getErrorDesc,
        (bool (*)(void *))hasOverflowed, (void (*)(void *))clear,
        (bool (*)(void *, int32_t, char const *))reportError,
        (int32_t (*)(void *))incRefCount, (int32_t (*)(void *))decRefCount);
  } catch (...) {
    return nullptr;
  }
}
void trtx_destroy_error_recorder(void *obj) {
  delete static_cast<nvinfer1::ErrorRecorderSubclass *>(obj);
}

//==============================================================================
// GpuAllocator subclass (bridge to Rust AllocateGpu)
//==============================================================================
namespace nvinfer1 {
class GpuAllocatorSubclass : public IGpuAllocator {
public:
  GpuAllocatorSubclass(void *self,
                       void *(*allocateAsync)(void *, uint64_t, uint64_t,
                                              uint32_t, void *),
                       void *(*reallocate)(void *, void *, uint64_t, uint64_t),
                       bool (*deallocateAsync)(void *, void *, void *))
      : self(self), m_allocateAsync((decltype(m_allocateAsync))allocateAsync),
        m_reallocate((decltype(m_reallocate))reallocate),
        m_deallocateAsync((decltype(m_deallocateAsync))deallocateAsync) {}
  ~GpuAllocatorSubclass() = default;

  void *self;
  void *(*m_allocateAsync)(void *, uint64_t, uint64_t, uint32_t, void *);
  void *(*m_reallocate)(void *, void *, uint64_t, uint64_t);
  bool (*m_deallocateAsync)(void *, void *, void *);

  void *allocate(uint64_t size, uint64_t alignment,
                 AllocatorFlags flags) noexcept override {
    return m_allocateAsync(self, size, alignment, static_cast<uint32_t>(flags),
                           nullptr);
  }
  void *reallocate(void *baseAddr, uint64_t alignment,
                   uint64_t newSize) noexcept override {
    return m_reallocate(self, baseAddr, alignment, newSize);
  }
  bool deallocate(void *memory) noexcept override {
    return m_deallocateAsync(self, memory, nullptr);
  }
  void *allocateAsync(uint64_t size, uint64_t alignment, AllocatorFlags flags,
                      cudaStream_t stream) noexcept override {
    return m_allocateAsync(self, size, alignment, static_cast<uint32_t>(flags),
                           stream);
  }
  bool deallocateAsync(void *memory, cudaStream_t stream) noexcept override {
    return m_deallocateAsync(self, memory, stream);
  }
};
} // namespace nvinfer1

void *trtx_create_gpu_allocator(void *self, void *allocateAsync,
                                void *reallocate, void *deallocateAsync) {

  try {
    return new nvinfer1::GpuAllocatorSubclass(
        self,
        (void *(*)(void *, uint64_t, uint64_t, uint32_t, void *))allocateAsync,
        (void *(*)(void *, void *, uint64_t, uint64_t))reallocate,
        (bool (*)(void *, void *, void *))deallocateAsync);
  } catch (...) {
    return nullptr;
  }
}
void trtx_destroy_gpu_allocator(void *obj) {
  delete static_cast<nvinfer1::GpuAllocatorSubclass *>(obj);
}

//==============================================================================
// IProfiler subclass (bridge to Rust ReportLayerTime)
//==============================================================================
namespace nvinfer1 {
class ProfilerSubclass : public IProfiler {
public:
  ProfilerSubclass(void *self, void (*reportLayerTime)(void *, char const *, float))
      : self(self),
        m_reportLayerTime((decltype(m_reportLayerTime))reportLayerTime) {}
  ~ProfilerSubclass() = default;

  void *self;
  void (*m_reportLayerTime)(void *, char const *, float);

  void reportLayerTime(char const *layerName, float ms) noexcept override {
    m_reportLayerTime(self, layerName, ms);
  }
};
} // namespace nvinfer1

void *trtx_create_profiler(
    void *self, void (*reportLayerTime)(void *, char const *, float)) {
  try {
    return new nvinfer1::ProfilerSubclass(
        self, (void (*)(void *, char const *, float))reportLayerTime);
  } catch (...) {
    return nullptr;
  }
}

void trtx_destroy_profiler(void *obj) {
  delete static_cast<nvinfer1::ProfilerSubclass *>(obj);
}
}

namespace nvinfer1 {
class DebugListener : public IDebugListener {
public:
  DebugListener(void *self,
                bool (*processDebugTensor)(void *self, void const *addr,
                                           TensorLocation location,
                                           DataType type, Dims const *shape,
                                           char const *name,
                                           cudaStream_t stream))
      : self(self), m_processDebugTensor(
                        (decltype(m_processDebugTensor))processDebugTensor) {}
  ~DebugListener() = default;

  void *self;
  bool (*m_processDebugTensor)(void *self, void const *addr,
                               TensorLocation location, DataType type,
                               Dims const *shape, char const *name,
                               cudaStream_t stream);

  bool processDebugTensor(void const *addr, TensorLocation location,
                          DataType type, Dims const &shape, char const *name,
                          cudaStream_t stream) noexcept override {
    return m_processDebugTensor(self, addr, location, type, &shape, name,
                                stream);
  };
};

extern "C" {
void *trtx_create_debug_listener(
    nvinfer1::IDebugListener *self,
    bool (*processDebugTensor)(void *self, void const *addr,
                               TensorLocation location, DataType type,
                               Dims const *shape, char const *name,
                               cudaStream_t stream)) {
  return new DebugListener(self, processDebugTensor);
}
void trtx_destroy_debug_listener(nvinfer1::IDebugListener *self) {
  delete self;
}
}
} // namespace nvinfer1