triton_cpp v1.2.1
Header-only C++ wrapper for NVIDIA Triton Inference Server clients
Loading...
Searching...
No Matches
triton_cpp Namespace Reference

Namespaces

namespace  detail
 

Classes

class  CudaSharedMemoryRegion
 Own a CUDA device allocation exportable through a CUDA IPC handle. More...
 
struct  InputData
 Couple a Triton input descriptor with its backing storage. More...
 
struct  InputOutputMetaData
 Shape, datatype, and byte-size metadata for one model tensor. More...
 
class  SharedMemoryRegion
 Own a POSIX shared-memory mapping used by the Triton client. More...
 
class  TritonInterface
 Synchronous, typed interface to one model served by Triton. More...
 

Typedefs

template<typename T >
using VectorType
 Helper type template for Eigen::Vector compatible with Triton.
 
template<typename T >
using MatrixType
 Helper type template for Eigen::Matrix compatible with Triton.
 
template<typename T , int rank>
using TensorType
 Helper type template for Eigen::Tensor compatible with Triton.
 
using TritonDataType
 Type alias for all possible C++ scalar types that the triton server supports.
 
using ModelOutput = std::map<std::string, std::shared_ptr<triton::client::InferRequestedOutput>>
 Map model output names to Triton requested-output objects.
 

Functions

void throw_on_cuda_error (cudaError_t status, const char *operation)
 Throw a descriptive exception when a CUDA Runtime API call fails.
 
bool LocalCudaSharedMemorySupported (std::string *reason=nullptr)
 Check whether this process can allocate CUDA IPC shared memory.
 
TritonDataType getZero (inference::DataType type)
 Get a zero value in the correct C++ type, given a Triton datatype.
 
std::size_t getAlignment (inference::DataType type)
 Return the natural C++ alignment for a Triton datatype.
 
std::size_t getSharedMemoryAlignment (inference::DataType type)
 Return the alignment used when packing tensors into shared memory.
 
std::size_t alignUp (std::size_t offset, std::size_t alignment)
 Round an offset up to an alignment boundary.
 
template<typename T >
std::ostream & operator<< (std::ostream &os, const std::vector< T > &data)
 Write a vector in bracketed, comma-separated form.
 
template<typename It >
std::int64_t accumulate_shape (It begin, It end)
 Compute the element count represented by a tensor shape.
 
void fail_on_error (const triton::client::Error &err, std::string message="")
 Convert a failed Triton client status into a C++ exception.
 
std::string randstring (std::size_t len)
 Generate an alphanumeric random string.
 

Typedef Documentation

◆ MatrixType

template<typename T >
using triton_cpp::MatrixType
Initial value:
typename std::conditional_t<
std::is_const_v<T>,
Eigen::Map<const Eigen::Matrix<typename std::remove_const_t<T>, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>,
Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>>

Helper type template for Eigen::Matrix compatible with Triton.

Template Parameters
TScalar data type. If T is const, the underlying Matrix will be const

Definition at line 41 of file types.hpp.

◆ ModelOutput

using triton_cpp::ModelOutput = std::map<std::string, std::shared_ptr<triton::client::InferRequestedOutput>>

Map model output names to Triton requested-output objects.

Definition at line 148 of file types.hpp.

◆ TensorType

template<typename T , int rank>
using triton_cpp::TensorType
Initial value:
typename std::conditional_t<
std::is_const_v<T>,
Eigen::TensorMap<const Eigen::Tensor<typename std::remove_const_t<T>, rank, Eigen::RowMajor, Eigen::Index>>,
Eigen::TensorMap<Eigen::Tensor<T, rank, Eigen::RowMajor, Eigen::Index>>>

Helper type template for Eigen::Tensor compatible with Triton.

Note that Eigen::Tensor is officially unsupported in Eigen3, but works for the mappings used here.

Template Parameters
TScalar data type. If T is const, the underlying Tensor will be const

Definition at line 55 of file types.hpp.

◆ TritonDataType

Initial value:
std::variant<bool, uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t, float, double, Eigen::half>

Type alias for all possible C++ scalar types that the triton server supports.

String values are not represented because they require dedicated storage and conversion handling.

Eigen::half is appended to preserve the variant indexes of the previously supported alternatives.

Definition at line 69 of file types.hpp.

◆ VectorType

template<typename T >
using triton_cpp::VectorType
Initial value:
typename std::conditional_t<std::is_const_v<T>,
Eigen::Map<const Eigen::VectorX<typename std::remove_const_t<T>>>,
Eigen::Map<Eigen::VectorX<T>>>

Helper type template for Eigen::Vector compatible with Triton.

Template Parameters
TScalar data type. If T is const, the underlying Vector will be const

Definition at line 31 of file types.hpp.

Function Documentation

◆ accumulate_shape()

template<typename It >
std::int64_t triton_cpp::accumulate_shape ( It begin,
It end )

Compute the element count represented by a tensor shape.

Dimensions smaller than one contribute one to the product. This treats Triton's dynamic dimension marker -1 as an unspecified unit dimension.

Template Parameters
ItIterator over values convertible to std::int64_t.
Parameters
beginBeginning of the shape range.
endEnd of the shape range.
Returns
Product of all positive dimensions.

Definition at line 55 of file utils.hpp.

55 {
56 return std::accumulate(begin, end, 1l, [](std::int64_t a, std::int64_t b) { return a * std::max(b, 1l); });
57}

◆ alignUp()

std::size_t triton_cpp::alignUp ( std::size_t offset,
std::size_t alignment )
inline

Round an offset up to an alignment boundary.

Parameters
offsetOriginal byte offset.
alignmentRequired alignment in bytes. Values zero and one leave the offset unchanged.
Returns
Smallest aligned offset greater than or equal to offset.

Definition at line 137 of file types.hpp.

137 {
138 if (alignment <= 1) {
139 return offset;
140 }
141 const std::size_t remainder = offset % alignment;
142 return remainder == 0 ? offset : offset + (alignment - remainder);
143}

◆ fail_on_error()

void triton_cpp::fail_on_error ( const triton::client::Error & err,
std::string message = "" )
inline

Convert a failed Triton client status into a C++ exception.

Parameters
errTriton client status to inspect.
messageContext prepended to the Triton error message.
Exceptions
std::runtime_errorif err does not represent success.

Definition at line 65 of file utils.hpp.

65 {
66 if (!err.IsOk()) {
67 throw std::runtime_error(message + ": " + err.Message());
68 }
69}

◆ getAlignment()

std::size_t triton_cpp::getAlignment ( inference::DataType type)
inline

Return the natural C++ alignment for a Triton datatype.

Parameters
typeTriton datatype to inspect.
Returns
Alignment requirement in bytes.
Exceptions
std::invalid_argumentif type is unsupported.

Definition at line 116 of file types.hpp.

116 {
117 return std::visit([](auto&& arg) { return alignof(std::decay_t<decltype(arg)>); }, getZero(type));
118}

References getZero().

Referenced by getSharedMemoryAlignment().

◆ getSharedMemoryAlignment()

std::size_t triton_cpp::getSharedMemoryAlignment ( inference::DataType type)
inline

Return the alignment used when packing tensors into shared memory.

Parameters
typeTriton datatype to inspect.
Returns
Alignment in bytes, with a minimum of eight bytes.
Exceptions
std::invalid_argumentif type is unsupported.

Definition at line 126 of file types.hpp.

126 {
127 constexpr std::size_t kMinSharedMemoryAlignment = 8;
128 return std::max(getAlignment(type), kMinSharedMemoryAlignment);
129}
std::size_t getAlignment(inference::DataType type)
Return the natural C++ alignment for a Triton datatype.
Definition types.hpp:116

References getAlignment().

◆ getZero()

TritonDataType triton_cpp::getZero ( inference::DataType type)
inline

Get a zero value in the correct C++ type, given a Triton datatype.

Parameters
typeThe triton type
Returns
TritonDataType Zero as C++ value in the correct datatype
Exceptions
std::invalid_argument,ifthe datatype is not supported yet

Definition at line 79 of file types.hpp.

79 {
80 switch (type) {
81 case inference::DataType::TYPE_BOOL:
82 return false;
83 case inference::DataType::TYPE_UINT8:
84 return static_cast<uint8_t>(0);
85 case inference::DataType::TYPE_UINT16:
86 return static_cast<uint16_t>(0);
87 case inference::DataType::TYPE_UINT32:
88 return static_cast<uint32_t>(0);
89 case inference::DataType::TYPE_UINT64:
90 return static_cast<uint64_t>(0);
91 case inference::DataType::TYPE_INT8:
92 return static_cast<int8_t>(0);
93 case inference::DataType::TYPE_INT16:
94 return static_cast<int16_t>(0);
95 case inference::DataType::TYPE_INT32:
96 return static_cast<int32_t>(0);
97 case inference::DataType::TYPE_INT64:
98 return static_cast<int64_t>(0);
99 case inference::DataType::TYPE_FP16:
100 return Eigen::half{0.0f};
101 case inference::DataType::TYPE_FP32:
102 return static_cast<float>(0);
103 case inference::DataType::TYPE_FP64:
104 return static_cast<double>(0);
105 default:
106 throw std::invalid_argument("Unsupported data type");
107 }
108}

Referenced by getAlignment().

◆ LocalCudaSharedMemorySupported()

bool triton_cpp::LocalCudaSharedMemorySupported ( std::string * reason = nullptr)
inline

Check whether this process can allocate CUDA IPC shared memory.

Parameters
reasonOptional destination for a diagnostic when support is unavailable.
Returns
true when at least one usable CUDA device is visible, otherwise false.

Definition at line 36 of file cuda_shm.hpp.

36 {
37 int device_count = 0;
38 const auto status = cudaGetDeviceCount(&device_count);
39 if (status != cudaSuccess) {
40 if (reason != nullptr) {
41 *reason = cudaGetErrorString(status);
42 }
43 cudaGetLastError();
44 return false;
45 }
46 if (device_count <= 0) {
47 if (reason != nullptr) {
48 *reason = "no CUDA-capable device is visible to the client";
49 }
50 return false;
51 }
52 return true;
53}

Referenced by triton_cpp::TritonInterface::TritonInterface().

◆ operator<<()

template<typename T >
std::ostream & triton_cpp::operator<< ( std::ostream & os,
const std::vector< T > & data )

Write a vector in bracketed, comma-separated form.

Template Parameters
TStreamable vector element type.
Parameters
osStream receiving the formatted vector.
dataVector to format.
Returns
Reference to os.

Definition at line 31 of file utils.hpp.

31 {
32 os << "[";
33 if (data.size() > 0) {
34 for (auto d = data.begin(); d != data.end() - 1; ++d) {
35 os << *d << ", ";
36 }
37 os << data.back();
38 }
39 os << "]";
40 return os;
41};

◆ randstring()

std::string triton_cpp::randstring ( std::size_t len)
inline

Generate an alphanumeric random string.

Parameters
lenNumber of characters to generate.
Returns
Random string of exactly len characters.

Definition at line 76 of file utils.hpp.

76 {
77 static constexpr auto chars =
78 "0123456789"
79 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
80 "abcdefghijklmnopqrstuvwxyz";
81 thread_local static std::mt19937 rng{std::random_device{}()};
82 thread_local static std::uniform_int_distribution<std::string::size_type> dist(0, std::strlen(chars) - 1);
83
84 std::string result(len, '\0');
85 std::generate_n(begin(result), len, [&]() { return chars[dist(rng)]; });
86 return result;
87}

◆ throw_on_cuda_error()

void triton_cpp::throw_on_cuda_error ( cudaError_t status,
const char * operation )
inline

Throw a descriptive exception when a CUDA Runtime API call fails.

Parameters
statusStatus returned by the CUDA Runtime API.
operationHuman-readable name of the failed operation.
Exceptions
std::runtime_errorif status is not cudaSuccess.

Definition at line 24 of file cuda_shm.hpp.

24 {
25 if (status == cudaSuccess) {
26 return;
27 }
28 throw std::runtime_error(std::string(operation) + " failed: " + cudaGetErrorString(status));
29}

Referenced by triton_cpp::TritonInterface::copyInputTensorToDevice(), and triton_cpp::CudaSharedMemoryRegion::CudaSharedMemoryRegion().