triton_cpp v1.2.1
Header-only C++ wrapper for NVIDIA Triton Inference Server clients
Loading...
Searching...
No Matches
utils.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 <array>
8#include <cstdint>
9#include <cstring>
10#include <functional>
11#include <iostream>
12#include <numeric>
13#include <random>
14#include <stdexcept>
15#include <string>
16#include <vector>
17
18#include <common.h>
19
20namespace triton_cpp {
21
30template <typename T>
31std::ostream& operator<<(std::ostream& os, const std::vector<T>& data) {
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};
42
54template <typename It>
55std::int64_t accumulate_shape(It begin, It end) {
56 return std::accumulate(begin, end, 1l, [](std::int64_t a, std::int64_t b) { return a * std::max(b, 1l); });
57}
58
65inline void fail_on_error(const triton::client::Error& err, std::string message = "") {
66 if (!err.IsOk()) {
67 throw std::runtime_error(message + ": " + err.Message());
68 }
69}
70
76inline std::string randstring(std::size_t len) {
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}
88
89} // namespace triton_cpp
std::ostream & operator<<(std::ostream &os, const std::vector< T > &data)
Write a vector in bracketed, comma-separated form.
Definition utils.hpp:31
std::string randstring(std::size_t len)
Generate an alphanumeric random string.
Definition utils.hpp:76
void fail_on_error(const triton::client::Error &err, std::string message="")
Convert a failed Triton client status into a C++ exception.
Definition utils.hpp:65
std::int64_t accumulate_shape(It begin, It end)
Compute the element count represented by a tensor shape.
Definition utils.hpp:55