TritonData.h
Go to the documentation of this file.
1 #ifndef NuSonic_Triton_TritonData
2 #define NuSonic_Triton_TritonData
3 
6 
7 #include <vector>
8 #include <string>
9 #include <unordered_map>
10 #include <numeric>
11 #include <algorithm>
12 #include <memory>
13 #include <any>
14 
15 #include "grpc_client.h"
16 #include "grpc_service.pb.h"
17 
18 namespace lartriton {
19 
20 //forward declaration
21 class TritonClient;
22 
23 //aliases for local input and output types
24 template <typename DT>
25 using TritonInput = std::vector<std::vector<DT>>;
26 template <typename DT>
27 using TritonOutput = std::vector<triton_span::Span<const DT*>>;
28 
29 //store all the info needed for triton input and output
30 template <typename IO>
31 class TritonData {
32 public:
33  using Result = nvidia::inferenceserver::client::InferResult;
34  using TensorMetadata = inference::ModelMetadataResponse_TensorMetadata;
35  using ShapeType = std::vector<int64_t>;
37 
38  //constructor
39  TritonData(const std::string& name, const TensorMetadata& model_info, bool noBatch);
40 
41  //some members can be modified
42  bool setShape(const ShapeType& newShape) { return setShape(newShape, true); }
43  bool setShape(unsigned loc, int64_t val) { return setShape(loc, val, true); }
44 
45  //io accessors
46  template <typename DT>
47  void toServer(std::shared_ptr<TritonInput<DT>> ptr);
48  template <typename DT>
50 
51  //const accessors
52  const ShapeView& shape() const { return shape_; }
53  int64_t byteSize() const { return byteSize_; }
54  const std::string& dname() const { return dname_; }
55  unsigned batchSize() const { return batchSize_; }
56 
57  //utilities
58  bool variableDims() const { return variableDims_; }
59  int64_t sizeDims() const { return productDims_; }
60  //default to dims if shape isn't filled
61  int64_t sizeShape() const { return variableDims_ ? dimProduct(shape_) : sizeDims(); }
62 
63 private:
64  friend class TritonClient;
65 
66  //private accessors only used by client
67  bool setShape(const ShapeType& newShape, bool canThrow);
68  bool setShape(unsigned loc, int64_t val, bool canThrow);
69  void setBatchSize(unsigned bsize);
70  void reset();
71  void setResult(std::shared_ptr<Result> result) { result_ = result; }
72  IO* data() { return data_.get(); }
73 
74  //helpers
75  bool anyNeg(const ShapeView& vec) const {
76  return std::any_of(vec.begin(), vec.end(), [](int64_t i) { return i < 0; });
77  }
78  int64_t dimProduct(const ShapeView& vec) const {
79  return std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int64_t>());
80  }
81  void createObject(IO** ioptr) const;
82 
83  //members
85  std::shared_ptr<IO> data_;
87  bool noBatch_;
88  unsigned batchSize_;
92  int64_t productDims_;
94  inference::DataType dtype_;
95  int64_t byteSize_;
96  std::any holder_;
97  std::shared_ptr<Result> result_;
98 };
99 
101 using TritonInputMap = std::unordered_map<std::string, TritonInputData>;
103 using TritonOutputMap = std::unordered_map<std::string, TritonOutputData>;
104 
105 template <>
107 template <>
109 template <>
110 void TritonInputData::createObject(nvidia::inferenceserver::client::InferInput** ioptr) const;
111 template <>
112 void TritonOutputData::createObject(nvidia::inferenceserver::client::InferRequestedOutput** ioptr) const;
113 
114 //explicit template instantiation declarations
117 
118 }
119 #endif
static QCString name
Definition: declinfo.cpp:673
std::string name_
Definition: TritonData.h:84
std::unordered_map< std::string, TritonOutputData > TritonOutputMap
Definition: TritonData.h:103
static QCString result
void setBatchSize(unsigned bsize)
Definition: TritonData.cc:100
bool setShape(const ShapeType &newShape)
Definition: TritonData.h:42
TritonData(const std::string &name, const TensorMetadata &model_info, bool noBatch)
Definition: TritonData.cc:27
const ShapeType dims_
Definition: TritonData.h:86
nvidia::inferenceserver::client::InferResult Result
Definition: TritonData.h:33
std::string string
Definition: nybbler.cc:12
bool anyNeg(const ShapeView &vec) const
Definition: TritonData.h:75
int64_t sizeDims() const
Definition: TritonData.h:59
bool variableDims() const
Definition: TritonData.h:58
const ShapeView & shape() const
Definition: TritonData.h:52
ShapeType fullShape_
Definition: TritonData.h:89
std::vector< int64_t > ShapeType
Definition: TritonData.h:35
T begin() const
Definition: Span.h:20
inference::DataType dtype_
Definition: TritonData.h:94
void toServer(std::shared_ptr< TritonInput< DT >> ptr)
Definition: TritonData.cc:109
void setResult(std::shared_ptr< Result > result)
Definition: TritonData.h:71
T end() const
Definition: Span.h:21
std::vector< std::vector< DT >> TritonInput
Definition: TritonData.h:25
unsigned batchSize() const
Definition: TritonData.h:55
int64_t sizeShape() const
Definition: TritonData.h:61
void createObject(IO **ioptr) const
bool setShape(unsigned loc, int64_t val)
Definition: TritonData.h:43
std::shared_ptr< Result > result_
Definition: TritonData.h:97
std::vector< triton_span::Span< const DT * >> TritonOutput
Definition: TritonData.h:27
std::shared_ptr< IO > data_
Definition: TritonData.h:85
int64_t dimProduct(const ShapeView &vec) const
Definition: TritonData.h:78
const std::string & dname() const
Definition: TritonData.h:54
std::unordered_map< std::string, TritonInputData > TritonInputMap
Definition: TritonData.h:101
int64_t byteSize() const
Definition: TritonData.h:53
inference::ModelMetadataResponse_TensorMetadata TensorMetadata
Definition: TritonData.h:34
std::string dname_
Definition: TritonData.h:93
TritonOutput< DT > fromServer() const
Definition: TritonData.cc:138