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