CMS 3D CMS Logo

List of all members | Public Types | Public Member Functions | Private Member Functions | Private Attributes | Friends
TritonData< IO > Class Template Reference

#include <TritonData.h>

Public Types

using Result = nvidia::inferenceserver::client::InferResult
 
using ShapeType = std::vector< int64_t >
 
using ShapeView = edm::Span< ShapeType::const_iterator >
 
using TensorMetadata = inference::ModelMetadataResponse_TensorMetadata
 

Public Member Functions

unsigned batchSize () const
 
int64_t byteSize () const
 
const std::string & dname () const
 
template<typename DT >
TritonOutput< DT > fromServer () const
 
bool setShape (const ShapeType &newShape)
 
bool setShape (unsigned loc, int64_t val)
 
const ShapeViewshape () const
 
int64_t sizeDims () const
 
int64_t sizeShape () const
 
template<typename DT >
void toServer (std::shared_ptr< TritonInput< DT >> ptr)
 
 TritonData (const std::string &name, const TensorMetadata &model_info, bool noBatch)
 
bool variableDims () const
 

Private Member Functions

bool anyNeg (const ShapeView &vec) const
 
void createObject (IO **ioptr) const
 
IO * data ()
 
int64_t dimProduct (const ShapeView &vec) const
 
void reset ()
 
void setBatchSize (unsigned bsize)
 
void setResult (std::shared_ptr< Result > result)
 
bool setShape (const ShapeType &newShape, bool canThrow)
 
bool setShape (unsigned loc, int64_t val, bool canThrow)
 

Private Attributes

unsigned batchSize_
 
int64_t byteSize_
 
std::shared_ptr< IO > data_
 
const ShapeType dims_
 
std::string dname_
 
inference::DataType dtype_
 
ShapeType fullShape_
 
std::any holder_
 
std::string name_
 
bool noBatch_
 
int64_t productDims_
 
std::shared_ptr< Resultresult_
 
ShapeView shape_
 
bool variableDims_
 

Friends

class TritonClient
 

Detailed Description

template<typename IO>
class TritonData< IO >

Definition at line 29 of file TritonData.h.

Member Typedef Documentation

◆ Result

template<typename IO>
using TritonData< IO >::Result = nvidia::inferenceserver::client::InferResult

Definition at line 31 of file TritonData.h.

◆ ShapeType

template<typename IO>
using TritonData< IO >::ShapeType = std::vector<int64_t>

Definition at line 33 of file TritonData.h.

◆ ShapeView

template<typename IO>
using TritonData< IO >::ShapeView = edm::Span<ShapeType::const_iterator>

Definition at line 34 of file TritonData.h.

◆ TensorMetadata

template<typename IO>
using TritonData< IO >::TensorMetadata = inference::ModelMetadataResponse_TensorMetadata

Definition at line 32 of file TritonData.h.

Constructor & Destructor Documentation

◆ TritonData()

template<typename IO>
TritonData< IO >::TritonData ( const std::string &  name,
const TensorMetadata model_info,
bool  noBatch 
)

Definition at line 25 of file TritonData.cc.

26  : name_(name),
27  dims_(model_info.shape().begin(), model_info.shape().end()),
28  noBatch_(noBatch),
29  batchSize_(0),
31  shape_(fullShape_.begin() + (noBatch_ ? 0 : 1), fullShape_.end()),
34  dname_(model_info.datatype()),
37  //create input or output object
38  IO* iotmp;
39  createObject(&iotmp);
40  data_.reset(iotmp);
41 }

References TritonData< IO >::createObject(), and TritonData< IO >::data_.

Member Function Documentation

◆ anyNeg()

template<typename IO>
bool TritonData< IO >::anyNeg ( const ShapeView vec) const
inlineprivate

Definition at line 73 of file TritonData.h.

73  {
74  return std::any_of(vec.begin(), vec.end(), [](int64_t i) { return i < 0; });
75  }

References edm::Span< T >::begin(), edm::Span< T >::end(), and mps_fire::i.

◆ batchSize()

template<typename IO>
unsigned TritonData< IO >::batchSize ( ) const
inline

Definition at line 53 of file TritonData.h.

53 { return batchSize_; }

References TritonData< IO >::batchSize_.

◆ byteSize()

template<typename IO>
int64_t TritonData< IO >::byteSize ( ) const
inline

Definition at line 51 of file TritonData.h.

51 { return byteSize_; }

References TritonData< IO >::byteSize_.

◆ createObject()

template<typename IO>
void TritonData< IO >::createObject ( IO **  ioptr) const
private

◆ data()

template<typename IO>
IO* TritonData< IO >::data ( )
inlineprivate

Definition at line 70 of file TritonData.h.

70 { return data_.get(); }

References TritonData< IO >::data_.

◆ dimProduct()

template<typename IO>
int64_t TritonData< IO >::dimProduct ( const ShapeView vec) const
inlineprivate

Definition at line 76 of file TritonData.h.

76  {
77  return std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int64_t>());
78  }

References edm::Span< T >::begin(), and edm::Span< T >::end().

Referenced by TritonData< IO >::sizeShape().

◆ dname()

template<typename IO>
const std::string& TritonData< IO >::dname ( ) const
inline

Definition at line 52 of file TritonData.h.

52 { return dname_; }

References TritonData< IO >::dname_.

◆ fromServer()

template<typename DT >
TritonOutput< DT > TritonOutputData::fromServer ( ) const

Definition at line 136 of file TritonData.cc.

136  {
137  if (!result_) {
138  throw cms::Exception("TritonDataError") << name_ << " output(): missing result";
139  }
140 
141  if (byteSize_ != sizeof(DT)) {
142  throw cms::Exception("TritonDataError") << name_ << " output(): inconsistent byte size " << sizeof(DT)
143  << " (should be " << byteSize_ << " for " << dname_ << ")";
144  }
145 
146  uint64_t nOutput = sizeShape();
147  TritonOutput<DT> dataOut;
148  const uint8_t* r0;
149  size_t contentByteSize;
150  size_t expectedContentByteSize = nOutput * byteSize_ * batchSize_;
151  triton_utils::throwIfError(result_->RawData(name_, &r0, &contentByteSize), "output(): unable to get raw");
152  if (contentByteSize != expectedContentByteSize) {
153  throw cms::Exception("TritonDataError") << name_ << " output(): unexpected content byte size " << contentByteSize
154  << " (expected " << expectedContentByteSize << ")";
155  }
156 
157  const DT* r1 = reinterpret_cast<const DT*>(r0);
158  dataOut.reserve(batchSize_);
159  for (unsigned i0 = 0; i0 < batchSize_; ++i0) {
160  auto offset = i0 * nOutput;
161  dataOut.emplace_back(r1 + offset, r1 + offset + nOutput);
162  }
163 
164  return dataOut;
165 }

References TritonData< IO >::batchSize_, TritonData< IO >::byteSize_, TritonData< IO >::dname_, GeomDetEnumerators::DT, Exception, TritonData< IO >::name_, hltrates_dqm_sourceclient-live_cfg::offset, diffTwoXMLs::r1, TritonData< IO >::result_, TritonData< IO >::sizeShape(), and triton_utils::throwIfError().

◆ reset()

void TritonOutputData::reset ( void  )
private

Definition at line 168 of file TritonData.cc.

168  {
169  data_->Reset();
170  holder_.reset();
171 }

References TritonData< IO >::data_, and TritonData< IO >::holder_.

◆ setBatchSize()

template<typename IO >
void TritonData< IO >::setBatchSize ( unsigned  bsize)
private

Definition at line 98 of file TritonData.cc.

98  {
99  batchSize_ = bsize;
100  if (!noBatch_)
101  fullShape_[0] = batchSize_;
102 }

◆ setResult()

template<typename IO>
void TritonData< IO >::setResult ( std::shared_ptr< Result result)
inlineprivate

Definition at line 69 of file TritonData.h.

69 { result_ = result; }

References mps_fire::result, and TritonData< IO >::result_.

◆ setShape() [1/4]

template<typename IO>
bool TritonData< IO >::setShape ( const ShapeType newShape)
inline

Definition at line 40 of file TritonData.h.

40 { return setShape(newShape, true); }

References TritonData< IO >::setShape().

Referenced by TritonData< IO >::setShape().

◆ setShape() [2/4]

template<typename IO>
bool TritonData< IO >::setShape ( const ShapeType newShape,
bool  canThrow 
)
private

◆ setShape() [3/4]

template<typename IO>
bool TritonData< IO >::setShape ( unsigned  loc,
int64_t  val 
)
inline

Definition at line 41 of file TritonData.h.

41 { return setShape(loc, val, true); }

References TritonData< IO >::setShape(), and heppy_batch::val.

Referenced by TritonData< IO >::setShape().

◆ setShape() [4/4]

template<typename IO >
bool TritonData< IO >::setShape ( unsigned  loc,
int64_t  val,
bool  canThrow 
)
private

Definition at line 64 of file TritonData.cc.

64  {
65  std::stringstream msg;
66  unsigned full_loc = loc + (noBatch_ ? 0 : 1);
67 
68  //check boundary
69  if (full_loc >= fullShape_.size()) {
70  msg << name_ << " setShape(): dimension " << full_loc << " out of bounds (" << fullShape_.size() << ")";
71  if (canThrow)
72  throw cms::Exception("TritonDataError") << msg.str();
73  else {
74  edm::LogWarning("TritonDataWarning") << msg.str();
75  return false;
76  }
77  }
78 
79  if (val != fullShape_[full_loc]) {
80  if (dims_[full_loc] == -1) {
81  fullShape_[full_loc] = val;
82  return true;
83  } else {
84  msg << name_ << " setShape(): attempt to change value of non-variable shape dimension " << loc;
85  if (canThrow)
86  throw cms::Exception("TritonDataError") << msg.str();
87  else {
88  edm::LogWarning("TritonDataError") << msg.str();
89  return false;
90  }
91  }
92  }
93 
94  return true;
95 }

References Exception, mps_check::msg, and heppy_batch::val.

◆ shape()

template<typename IO>
const ShapeView& TritonData< IO >::shape ( void  ) const
inline

Definition at line 50 of file TritonData.h.

50 { return shape_; }

References TritonData< IO >::shape_.

◆ sizeDims()

template<typename IO>
int64_t TritonData< IO >::sizeDims ( ) const
inline

Definition at line 57 of file TritonData.h.

57 { return productDims_; }

References TritonData< IO >::productDims_.

Referenced by TritonData< IO >::sizeShape().

◆ sizeShape()

template<typename IO>
int64_t TritonData< IO >::sizeShape ( ) const
inline

◆ toServer()

template<typename DT >
void TritonInputData::toServer ( std::shared_ptr< TritonInput< DT >>  ptr)

Definition at line 107 of file TritonData.cc.

107  {
108  const auto& data_in = *ptr;
109 
110  //check batch size
111  if (data_in.size() != batchSize_) {
112  throw cms::Exception("TritonDataError") << name_ << " input(): input vector has size " << data_in.size()
113  << " but specified batch size is " << batchSize_;
114  }
115 
116  //shape must be specified for variable dims or if batch size changes
117  data_->SetShape(fullShape_);
118 
119  if (byteSize_ != sizeof(DT))
120  throw cms::Exception("TritonDataError") << name_ << " input(): inconsistent byte size " << sizeof(DT)
121  << " (should be " << byteSize_ << " for " << dname_ << ")";
122 
123  int64_t nInput = sizeShape();
124  for (unsigned i0 = 0; i0 < batchSize_; ++i0) {
125  const DT* arr = data_in[i0].data();
126  triton_utils::throwIfError(data_->AppendRaw(reinterpret_cast<const uint8_t*>(arr), nInput * byteSize_),
127  name_ + " input(): unable to set data for batch entry " + std::to_string(i0));
128  }
129 
130  //keep input data in scope
131  holder_ = std::move(ptr);
132 }

References TritonData< IO >::batchSize_, TritonData< IO >::byteSize_, TritonData< IO >::data_, TritonData< IO >::dname_, GeomDetEnumerators::DT, Exception, TritonData< IO >::fullShape_, TritonData< IO >::holder_, eostools::move(), TritonData< IO >::name_, TritonData< IO >::sizeShape(), and triton_utils::throwIfError().

◆ variableDims()

template<typename IO>
bool TritonData< IO >::variableDims ( ) const
inline

Definition at line 56 of file TritonData.h.

56 { return variableDims_; }

References TritonData< IO >::variableDims_.

Friends And Related Function Documentation

◆ TritonClient

template<typename IO>
friend class TritonClient
friend

Definition at line 62 of file TritonData.h.

Member Data Documentation

◆ batchSize_

template<typename IO>
unsigned TritonData< IO >::batchSize_
private

◆ byteSize_

template<typename IO>
int64_t TritonData< IO >::byteSize_
private

◆ data_

template<typename IO>
std::shared_ptr<IO> TritonData< IO >::data_
private

◆ dims_

template<typename IO>
const ShapeType TritonData< IO >::dims_
private

Definition at line 84 of file TritonData.h.

◆ dname_

template<typename IO>
std::string TritonData< IO >::dname_
private

◆ dtype_

template<typename IO>
inference::DataType TritonData< IO >::dtype_
private

Definition at line 92 of file TritonData.h.

◆ fullShape_

template<typename IO>
ShapeType TritonData< IO >::fullShape_
private

Definition at line 87 of file TritonData.h.

Referenced by TritonData< IO >::toServer().

◆ holder_

template<typename IO>
std::any TritonData< IO >::holder_
private

Definition at line 94 of file TritonData.h.

Referenced by TritonData< IO >::reset(), and TritonData< IO >::toServer().

◆ name_

template<typename IO>
std::string TritonData< IO >::name_
private

Definition at line 82 of file TritonData.h.

Referenced by TritonData< IO >::fromServer(), and TritonData< IO >::toServer().

◆ noBatch_

template<typename IO>
bool TritonData< IO >::noBatch_
private

Definition at line 85 of file TritonData.h.

◆ productDims_

template<typename IO>
int64_t TritonData< IO >::productDims_
private

Definition at line 90 of file TritonData.h.

Referenced by TritonData< IO >::sizeDims().

◆ result_

template<typename IO>
std::shared_ptr<Result> TritonData< IO >::result_
private

Definition at line 95 of file TritonData.h.

Referenced by TritonData< IO >::fromServer(), and TritonData< IO >::setResult().

◆ shape_

template<typename IO>
ShapeView TritonData< IO >::shape_
private

Definition at line 88 of file TritonData.h.

Referenced by TritonData< IO >::shape(), and TritonData< IO >::sizeShape().

◆ variableDims_

template<typename IO>
bool TritonData< IO >::variableDims_
private

Definition at line 89 of file TritonData.h.

Referenced by TritonData< IO >::sizeShape(), and TritonData< IO >::variableDims().

mps_fire.i
i
Definition: mps_fire.py:428
TritonData::result_
std::shared_ptr< Result > result_
Definition: TritonData.h:95
TritonData::holder_
std::any holder_
Definition: TritonData.h:94
TritonData::sizeShape
int64_t sizeShape() const
Definition: TritonData.h:59
TritonData::noBatch_
bool noBatch_
Definition: TritonData.h:85
TritonData::batchSize_
unsigned batchSize_
Definition: TritonData.h:86
mps_check.msg
tuple msg
Definition: mps_check.py:285
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
TritonData::fullShape_
ShapeType fullShape_
Definition: TritonData.h:87
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
triton_utils::throwIfError
void throwIfError(const Error &err, std::string_view msg)
Definition: triton_utils.cc:20
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
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
GeomDetEnumerators::DT
Definition: GeomDetEnumerators.h:18
TritonOutput
std::vector< edm::Span< const DT * > > TritonOutput
Definition: TritonData.h:25
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
eostools.move
def move(src, dest)
Definition: eostools.py:511
diffTwoXMLs.r1
r1
Definition: diffTwoXMLs.py:53
Exception
Definition: hltDiff.cc:246
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
nvidia::inferenceserver::ProtocolStringToDataType
inference::DataType ProtocolStringToDataType(const std::string &dtype)
cond::uint64_t
unsigned long long uint64_t
Definition: Time.h:13
TritonData::byteSize_
int64_t byteSize_
Definition: TritonData.h:93
mps_fire.result
result
Definition: mps_fire.py:311
nvidia::inferenceserver::GetDataTypeByteSize
size_t GetDataTypeByteSize(const inference::DataType dtype)
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:82
TritonData::productDims_
int64_t productDims_
Definition: TritonData.h:90
TritonData::anyNeg
bool anyNeg(const ShapeView &vec) const
Definition: TritonData.h:73