CMS 3D CMS Logo

TritonData.h
Go to the documentation of this file.
1 #ifndef HeterogeneousCore_SonicTriton_TritonData
2 #define HeterogeneousCore_SonicTriton_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 //forward declaration
19 class TritonClient;
20 
21 //aliases for local input and output types
22 template <typename DT>
23 using TritonInput = std::vector<std::vector<DT>>;
24 template <typename DT>
25 using TritonOutput = std::vector<edm::Span<const DT*>>;
26 
27 //store all the info needed for triton input and output
28 template <typename IO>
29 class TritonData {
30 public:
31  using Result = nvidia::inferenceserver::client::InferResult;
32  using TensorMetadata = inference::ModelMetadataResponse_TensorMetadata;
33  using ShapeType = std::vector<int64_t>;
35 
36  //constructor
37  TritonData(const std::string& name, const TensorMetadata& model_info, bool noBatch);
38 
39  //some members can be modified
40  bool setShape(const ShapeType& newShape) { return setShape(newShape, true); }
41  bool setShape(unsigned loc, int64_t val) { return setShape(loc, val, true); }
42 
43  //io accessors
44  template <typename DT>
45  void toServer(std::shared_ptr<TritonInput<DT>> ptr);
46  template <typename DT>
48 
49  //const accessors
50  const ShapeView& shape() const { return shape_; }
51  int64_t byteSize() const { return byteSize_; }
52  const std::string& dname() const { return dname_; }
53  unsigned batchSize() const { return batchSize_; }
54 
55  //utilities
56  bool variableDims() const { return variableDims_; }
57  int64_t sizeDims() const { return productDims_; }
58  //default to dims if shape isn't filled
59  int64_t sizeShape() const { return variableDims_ ? dimProduct(shape_) : sizeDims(); }
60 
61 private:
62  friend class TritonClient;
63 
64  //private accessors only used by client
65  bool setShape(const ShapeType& newShape, bool canThrow);
66  bool setShape(unsigned loc, int64_t val, bool canThrow);
67  void setBatchSize(unsigned bsize);
68  void reset();
69  void setResult(std::shared_ptr<Result> result) { result_ = result; }
70  IO* data() { return data_.get(); }
71 
72  //helpers
73  bool anyNeg(const ShapeView& vec) const {
74  return std::any_of(vec.begin(), vec.end(), [](int64_t i) { return i < 0; });
75  }
76  int64_t dimProduct(const ShapeView& vec) const {
77  return std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int64_t>());
78  }
79  void createObject(IO** ioptr) const;
80 
81  //members
83  std::shared_ptr<IO> data_;
85  bool noBatch_;
86  unsigned batchSize_;
90  int64_t productDims_;
93  int64_t byteSize_;
95  std::shared_ptr<Result> result_;
96 };
97 
99 using TritonInputMap = std::unordered_map<std::string, TritonInputData>;
101 using TritonOutputMap = std::unordered_map<std::string, TritonOutputData>;
102 
103 //avoid "explicit specialization after instantiation" error
104 template <>
105 template <typename DT>
106 void TritonInputData::toServer(std::shared_ptr<TritonInput<DT>> ptr);
107 template <>
108 template <typename DT>
110 template <>
112 template <>
114 template <>
115 void TritonInputData::createObject(nvidia::inferenceserver::client::InferInput** ioptr) const;
116 template <>
117 void TritonOutputData::createObject(nvidia::inferenceserver::client::InferRequestedOutput** ioptr) const;
118 
119 //explicit template instantiation declarations
122 
123 #endif
TritonData::Result
nvidia::inferenceserver::client::InferResult Result
Definition: TritonData.h:31
TritonData::data
IO * data()
Definition: TritonData.h:70
mps_fire.i
i
Definition: mps_fire.py:428
TritonData
Definition: TritonData.h:29
TritonData::result_
std::shared_ptr< Result > result_
Definition: TritonData.h:95
edm::Span::end
T end() const
Definition: Span.h:21
TritonData::holder_
std::any holder_
Definition: TritonData.h:94
TritonData::reset
void reset()
Definition: TritonData.cc:168
TritonData::TensorMetadata
inference::ModelMetadataResponse_TensorMetadata TensorMetadata
Definition: TritonData.h:32
TritonData::sizeShape
int64_t sizeShape() const
Definition: TritonData.h:59
TritonData::noBatch_
bool noBatch_
Definition: TritonData.h:85
TritonData::toServer
void toServer(std::shared_ptr< TritonInput< DT >> ptr)
Definition: TritonData.cc:107
TritonInputMap
std::unordered_map< std::string, TritonInputData > TritonInputMap
Definition: TritonData.h:99
TritonData::batchSize
unsigned batchSize() const
Definition: TritonData.h:53
TritonData::batchSize_
unsigned batchSize_
Definition: TritonData.h:86
TritonInput
std::vector< std::vector< DT > > TritonInput
Definition: TritonData.h:23
TritonData::ShapeType
std::vector< int64_t > ShapeType
Definition: TritonData.h:33
TritonData::fullShape_
ShapeType fullShape_
Definition: TritonData.h:87
TritonData::variableDims
bool variableDims() const
Definition: TritonData.h:56
TritonData::fromServer
TritonOutput< DT > fromServer() const
Definition: TritonData.cc:136
any
bool any(const std::vector< T > &v, const T &what)
Definition: ECalSD.cc:38
TritonData::createObject
void createObject(IO **ioptr) const
TritonData::sizeDims
int64_t sizeDims() const
Definition: TritonData.h:57
TritonData::dtype_
inference::DataType dtype_
Definition: TritonData.h:92
TritonData::dimProduct
int64_t dimProduct(const ShapeView &vec) const
Definition: TritonData.h:76
TritonData::name_
std::string name_
Definition: TritonData.h:82
TritonData::setShape
bool setShape(const ShapeType &newShape)
Definition: TritonData.h:40
edm::Span::begin
T begin() const
Definition: Span.h:20
TritonData::setShape
bool setShape(unsigned loc, int64_t val)
Definition: TritonData.h:41
TritonOutputMap
std::unordered_map< std::string, TritonOutputData > TritonOutputMap
Definition: TritonData.h:101
TritonData::variableDims_
bool variableDims_
Definition: TritonData.h:89
TritonData::dname_
std::string dname_
Definition: TritonData.h:91
TritonData::data_
std::shared_ptr< IO > data_
Definition: TritonData.h:83
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
Span.h
TritonOutput
std::vector< edm::Span< const DT * > > TritonOutput
Definition: TritonData.h:25
TritonData::setBatchSize
void setBatchSize(unsigned bsize)
Definition: TritonData.cc:98
TritonData::shape
const ShapeView & shape() const
Definition: TritonData.h:50
TritonData::setResult
void setResult(std::shared_ptr< Result > result)
Definition: TritonData.h:69
TritonData::shape_
ShapeView shape_
Definition: TritonData.h:88
TritonData::dims_
const ShapeType dims_
Definition: TritonData.h:84
heppy_batch.val
val
Definition: heppy_batch.py:351
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
Exception.h
TritonData::dname
const std::string & dname() const
Definition: TritonData.h:52
TritonData::byteSize
int64_t byteSize() const
Definition: TritonData.h:51
TritonData::byteSize_
int64_t byteSize_
Definition: TritonData.h:93
mps_fire.result
result
Definition: mps_fire.py:311
edm::Span< ShapeType::const_iterator >
TritonData::productDims_
int64_t productDims_
Definition: TritonData.h:90
TritonData::TritonData
TritonData(const std::string &name, const TensorMetadata &model_info, bool noBatch)
Definition: TritonData.cc:25
TritonClient
Definition: TritonClient.h:18
HLT_FULL_cff.DataType
DataType
Definition: HLT_FULL_cff.py:32831
TritonData::anyNeg
bool anyNeg(const ShapeView &vec) const
Definition: TritonData.h:73