triton_cpp v1.2.1
Header-only C++ wrapper for NVIDIA Triton Inference Server clients
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1// Copyright Institute for Automotive Engineering (ika), RWTH Aachen University
2// SPDX-License-Identifier: Apache-2.0
3
4#pragma once
5
6#include <algorithm>
7#include <cstddef>
8#include <cstdint>
9#include <map>
10#include <memory>
11#include <stdexcept>
12#include <string>
13#include <type_traits>
14#include <variant>
15#include <vector>
16
17#include <Eigen/Dense>
18#include <eigen3/unsupported/Eigen/CXX11/Tensor>
19
20#include <model_config.pb.h>
21
22#include "triton_cpp/utils.hpp"
23
24namespace triton_cpp {
30template <typename T>
31using VectorType = typename std::conditional_t<std::is_const_v<T>,
32 Eigen::Map<const Eigen::VectorX<typename std::remove_const_t<T>>>,
33 Eigen::Map<Eigen::VectorX<T>>>;
34
40template <typename T>
41using MatrixType = typename std::conditional_t<
42 std::is_const_v<T>,
43 Eigen::Map<const Eigen::Matrix<typename std::remove_const_t<T>, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>,
44 Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>>;
45
54template <typename T, int rank>
55using TensorType = typename std::conditional_t<
56 std::is_const_v<T>,
57 Eigen::TensorMap<const Eigen::Tensor<typename std::remove_const_t<T>, rank, Eigen::RowMajor, Eigen::Index>>,
58 Eigen::TensorMap<Eigen::Tensor<T, rank, Eigen::RowMajor, Eigen::Index>>>;
59
70 std::variant<bool, uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t, float, double, Eigen::half>;
71
79inline TritonDataType getZero(inference::DataType type) {
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}
109
116inline std::size_t getAlignment(inference::DataType type) {
117 return std::visit([](auto&& arg) { return alignof(std::decay_t<decltype(arg)>); }, getZero(type));
118}
119
126inline std::size_t getSharedMemoryAlignment(inference::DataType type) {
127 constexpr std::size_t kMinSharedMemoryAlignment = 8;
128 return std::max(getAlignment(type), kMinSharedMemoryAlignment);
129}
130
137inline std::size_t alignUp(std::size_t offset, std::size_t alignment) {
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}
144
148using ModelOutput = std::map<std::string, std::shared_ptr<triton::client::InferRequestedOutput>>;
149
157struct InputData {
159 std::shared_ptr<triton::client::InferInput> input;
161 std::vector<uint8_t> data;
163 uint8_t* data_raw{nullptr};
165 uint8_t* device_data_raw{nullptr};
167 std::size_t data_raw_size{0};
168
170 InputData() = default;
171
176 InputData(const InputData& other)
177 : input{other.input},
178 data{other.data},
179 data_raw{other.ownsHostBuffer() ? data.data() : other.data_raw},
182
187 InputData(InputData&& other) noexcept {
188 const bool owns_host_buffer = other.ownsHostBuffer();
189 input = std::move(other.input);
190 data = std::move(other.data);
191 data_raw = owns_host_buffer ? data.data() : other.data_raw;
192 device_data_raw = other.device_data_raw;
193 data_raw_size = other.data_raw_size;
194 other.data_raw = nullptr;
195 other.device_data_raw = nullptr;
196 other.data_raw_size = 0;
197 }
198
201 if (this != &other) {
202 const bool owns_host_buffer = other.ownsHostBuffer();
203 input = other.input;
204 data = other.data;
205 data_raw = owns_host_buffer ? data.data() : other.data_raw;
208 }
209 return *this;
210 }
211
213 InputData& operator=(InputData&& other) noexcept {
214 if (this != &other) {
215 const bool owns_host_buffer = other.ownsHostBuffer();
216 input = std::move(other.input);
217 data = std::move(other.data);
218 data_raw = owns_host_buffer ? data.data() : other.data_raw;
219 device_data_raw = other.device_data_raw;
220 data_raw_size = other.data_raw_size;
221 other.data_raw = nullptr;
222 other.device_data_raw = nullptr;
223 other.data_raw_size = 0;
224 }
225 return *this;
226 }
227
233 InputData(std::shared_ptr<triton::client::InferInput> input, std::vector<uint8_t>&& data)
234 : input{std::move(input)},
235 data{std::move(data)},
236 data_raw{this->data.data()},
237 device_data_raw{nullptr},
238 data_raw_size{this->data.size()} {}
239
246 InputData(std::shared_ptr<triton::client::InferInput> input, uint8_t* data_raw, std::size_t data_raw_size)
248
256 InputData(std::shared_ptr<triton::client::InferInput> input,
257 uint8_t* data_raw,
258 uint8_t* device_data_raw,
259 std::size_t data_raw_size)
261
263 bool isHostMappable() const { return data_raw != nullptr; }
265 bool isDeviceBacked() const { return device_data_raw != nullptr; }
266
267 private:
268 bool ownsHostBuffer() const noexcept { return data_raw == data.data(); }
269};
270
274 const std::vector<int64_t> shape;
276 const inference::DataType datatype;
278 const int64_t bytesize;
279
280 public:
287 InputOutputMetaData(const std::vector<int64_t>& shape, const inference::DataType& datatype)
288 : shape{shape},
290 bytesize{accumulate_shape(shape.begin(), shape.end()) *
291 std::visit([](auto&& arg) { return static_cast<std::int64_t>(sizeof(arg)); }, getZero(datatype))} {}
292};
293
294} // namespace triton_cpp
std::size_t getAlignment(inference::DataType type)
Return the natural C++ alignment for a Triton datatype.
Definition types.hpp:116
std::variant< bool, uint8_t, uint16_t, uint32_t, uint64_t, int8_t, int16_t, int32_t, int64_t, float, double, Eigen::half > TritonDataType
Type alias for all possible C++ scalar types that the triton server supports.
Definition types.hpp:69
std::size_t getSharedMemoryAlignment(inference::DataType type)
Return the alignment used when packing tensors into shared memory.
Definition types.hpp:126
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 > > > MatrixType
Helper type template for Eigen::Matrix compatible with Triton.
Definition types.hpp:41
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 > > > VectorType
Helper type template for Eigen::Vector compatible with Triton.
Definition types.hpp:31
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 > > > TensorType
Helper type template for Eigen::Tensor compatible with Triton.
Definition types.hpp:55
TritonDataType getZero(inference::DataType type)
Get a zero value in the correct C++ type, given a Triton datatype.
Definition types.hpp:79
std::size_t alignUp(std::size_t offset, std::size_t alignment)
Round an offset up to an alignment boundary.
Definition types.hpp:137
std::int64_t accumulate_shape(It begin, It end)
Compute the element count represented by a tensor shape.
Definition utils.hpp:55
std::map< std::string, std::shared_ptr< triton::client::InferRequestedOutput > > ModelOutput
Map model output names to Triton requested-output objects.
Definition types.hpp:148
Couple a Triton input descriptor with its backing storage.
Definition types.hpp:157
InputData()=default
Construct an empty input-storage descriptor.
InputData(const InputData &other)
Copy an input descriptor and rebind pointers into copied owned storage.
Definition types.hpp:176
InputData & operator=(InputData &&other) noexcept
Move an input descriptor while preserving storage ownership.
Definition types.hpp:213
bool isDeviceBacked() const
Definition types.hpp:265
InputData & operator=(const InputData &other)
Copy an input descriptor while preserving storage ownership.
Definition types.hpp:200
std::size_t data_raw_size
Definition types.hpp:167
InputData(std::shared_ptr< triton::client::InferInput > input, std::vector< uint8_t > &&data)
Construct an input backed by an owned host vector.
Definition types.hpp:233
InputData(std::shared_ptr< triton::client::InferInput > input, uint8_t *data_raw, uint8_t *device_data_raw, std::size_t data_raw_size)
Construct an input backed by CUDA shared memory.
Definition types.hpp:256
std::shared_ptr< triton::client::InferInput > input
Definition types.hpp:159
InputData(std::shared_ptr< triton::client::InferInput > input, uint8_t *data_raw, std::size_t data_raw_size)
Construct an input backed by externally owned host memory.
Definition types.hpp:246
InputData(InputData &&other) noexcept
Move an input descriptor and preserve its storage association.
Definition types.hpp:187
uint8_t * device_data_raw
Definition types.hpp:165
bool isHostMappable() const
Definition types.hpp:263
std::vector< uint8_t > data
Definition types.hpp:161
Shape, datatype, and byte-size metadata for one model tensor.
Definition types.hpp:272
const inference::DataType datatype
Definition types.hpp:276
InputOutputMetaData(const std::vector< int64_t > &shape, const inference::DataType &datatype)
Build metadata and calculate the required tensor byte size.
Definition types.hpp:287
const std::vector< int64_t > shape
Definition types.hpp:274