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 = triton::client::InferResult
 
using ShapeType = std::vector< int64_t >
 
using ShapeView = edm::Span< ShapeType::const_iterator >
 
using TensorMetadata = inference::ModelMetadataResponse_TensorMetadata
 

Public Member Functions

template<typename DT >
TritonInputContainer< DT > allocate (bool reserve=true)
 
unsigned batchSize () const
 
int64_t byteSize () const
 
const std::string & dname () const
 
template<typename DT >
TritonOutput< DT > fromServer () const
 
void prepare ()
 
void setShape (const ShapeType &newShape)
 
void setShape (unsigned loc, int64_t val)
 
const ShapeViewshape () const
 
int64_t sizeDims () const
 
int64_t sizeShape () const
 
template<typename DT >
void toServer (TritonInputContainer< DT > ptr)
 
 TritonData (const std::string &name, const TensorMetadata &model_info, TritonClient *client, const std::string &pid)
 
bool variableDims () const
 

Private Member Functions

bool anyNeg (const ShapeView &vec) const
 
triton::client::InferenceServerGrpcClient * client ()
 
void computeSizes ()
 
void createObject (IO **ioptr)
 
IO * data ()
 
int64_t dimProduct (const ShapeView &vec) const
 
unsigned fullLoc (unsigned loc) const
 
void reset ()
 
void resetSizes ()
 
void setBatchSize (unsigned bsize)
 
void setResult (std::shared_ptr< Result > result)
 
unsigned uid () const
 
void updateMem (size_t size)
 
std::string xput () const
 

Private Attributes

unsigned batchSize_
 
int64_t byteSize_
 
size_t byteSizePerBatch_
 
TritonClientclient_
 
std::shared_ptr< IO > data_
 
const ShapeType dims_
 
std::string dname_
 
bool done_ {}
 
inference::DataType dtype_
 
ShapeType fullShape_
 
std::shared_ptr< void > holder_
 
std::shared_ptr< TritonMemResource< IO > > memResource_
 
std::string name_
 
bool noBatch_
 
int64_t productDims_
 
std::shared_ptr< Resultresult_
 
ShapeView shape_
 
std::string shmName_
 
size_t sizeShape_
 
size_t totalByteSize_
 
bool useShm_
 
bool variableDims_
 

Friends

class TritonClient
 
class TritonCpuShmResource< IO >
 
class TritonHeapResource< IO >
 
class TritonMemResource< IO >
 

Detailed Description

template<typename IO>
class TritonData< IO >

Definition at line 45 of file TritonData.h.

Member Typedef Documentation

◆ Result

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

Definition at line 47 of file TritonData.h.

◆ ShapeType

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

Definition at line 49 of file TritonData.h.

◆ ShapeView

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

Definition at line 50 of file TritonData.h.

◆ TensorMetadata

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

Definition at line 48 of file TritonData.h.

Constructor & Destructor Documentation

◆ TritonData()

template<typename IO>
TritonData< IO >::TritonData ( const std::string &  name,
const TensorMetadata model_info,
TritonClient client,
const std::string &  pid 
)

Definition at line 19 of file TritonData.cc.

23  : name_(name),
24  client_(client),
26  //ensure unique name for shared memory region
27  shmName_(useShm_ ? pid + "_" + xput() + std::to_string(uid()) : ""),
28  dims_(model_info.shape().begin(), model_info.shape().end()),
29  noBatch_(client_->noBatch()),
30  batchSize_(0),
32  shape_(fullShape_.begin() + (noBatch_ ? 0 : 1), fullShape_.end()),
35  dname_(model_info.datatype()),
36  dtype_(ni::ProtocolStringToDataType(dname_)),
37  byteSize_(ni::GetDataTypeByteSize(dtype_)),
38  totalByteSize_(0) {
39  //create input or output object
40  IO* iotmp;
41  createObject(&iotmp);
42  data_.reset(iotmp);
43 }

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

Member Function Documentation

◆ allocate()

template<typename DT >
TritonInputContainer< DT > TritonInputData::allocate ( bool  reserve = true)

Definition at line 150 of file TritonData.cc.

150  {
151  //automatically creates a vector for each batch entry (if batch size known)
152  auto ptr = std::make_shared<TritonInput<DT>>(batchSize_);
153  if (reserve and !anyNeg(shape_)) {
154  computeSizes();
155  for (auto& vec : *ptr) {
156  vec.reserve(sizeShape_);
157  }
158  }
159  return ptr;
160 }

References TritonData< IO >::anyNeg(), TritonData< IO >::batchSize_, TritonData< IO >::computeSizes(), TritonData< IO >::shape_, and TritonData< IO >::sizeShape_.

◆ anyNeg()

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

Definition at line 101 of file TritonData.h.

101  {
102  return std::any_of(vec.begin(), vec.end(), [](int64_t i) { return i < 0; });
103  }

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

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

◆ batchSize()

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

Definition at line 72 of file TritonData.h.

72 { return batchSize_; }

References TritonData< IO >::batchSize_.

◆ byteSize()

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

Definition at line 70 of file TritonData.h.

70 { return byteSize_; }

References TritonData< IO >::byteSize_.

◆ client()

template<typename IO >
tc::InferenceServerGrpcClient * TritonData< IO >::client ( )
private

Definition at line 68 of file TritonData.cc.

68  {
69  return client_->client();
70 }

◆ computeSizes()

template<typename IO >
void TritonData< IO >::computeSizes ( )
private

◆ createObject()

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

◆ data()

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

Definition at line 94 of file TritonData.h.

94 { return data_.get(); }

References TritonData< IO >::data_.

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

◆ dimProduct()

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

Definition at line 104 of file TritonData.h.

104  {
105  return std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int64_t>());
106  }

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 71 of file TritonData.h.

71 { return dname_; }

References TritonData< IO >::dname_.

◆ fromServer()

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

Definition at line 206 of file TritonData.cc.

206  {
207  //shouldn't be called twice
208  if (done_)
209  throw cms::Exception("TritonDataError") << name_ << " fromServer() was already called for this event";
210 
211  if (!result_) {
212  throw cms::Exception("TritonDataError") << name_ << " fromServer(): missing result";
213  }
214 
215  if (byteSize_ != sizeof(DT)) {
216  throw cms::Exception("TritonDataError") << name_ << " fromServer(): inconsistent byte size " << sizeof(DT)
217  << " (should be " << byteSize_ << " for " << dname_ << ")";
218  }
219 
220  const uint8_t* r0 = memResource_->copyOutput();
221  const DT* r1 = reinterpret_cast<const DT*>(r0);
222 
223  TritonOutput<DT> dataOut;
224  dataOut.reserve(batchSize_);
225  for (unsigned i0 = 0; i0 < batchSize_; ++i0) {
226  auto offset = i0 * sizeShape_;
227  dataOut.emplace_back(r1 + offset, r1 + offset + sizeShape_);
228  }
229 
230  done_ = true;
231  return dataOut;
232 }

References TritonData< IO >::batchSize_, TritonData< IO >::byteSize_, TritonData< IO >::dname_, TritonData< IO >::done_, GeomDetEnumerators::DT, Exception, TritonData< IO >::memResource_, TritonData< IO >::name_, hltrates_dqm_sourceclient-live_cfg::offset, diffTwoXMLs::r1, TritonData< IO >::result_, and TritonData< IO >::sizeShape_.

◆ fullLoc()

template<typename IO>
unsigned TritonData< IO >::fullLoc ( unsigned  loc) const
inlineprivate

Definition at line 90 of file TritonData.h.

90 { return loc + (noBatch_ ? 0 : 1); }

References TritonData< IO >::noBatch_.

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

◆ prepare()

void TritonOutputData::prepare ( )

◆ reset()

void TritonOutputData::reset ( void  )
private

Definition at line 235 of file TritonData.cc.

235  {
236  done_ = false;
237  holder_.reset();
238  data_->Reset();
239  //reset shape
240  if (variableDims_) {
241  for (unsigned i = 0; i < shape_.size(); ++i) {
242  unsigned locFull = fullLoc(i);
243  fullShape_[locFull] = dims_[locFull];
244  }
245  }
246  resetSizes();
247 }

References TritonData< IO >::data_, TritonData< IO >::dims_, TritonData< IO >::done_, TritonData< IO >::fullLoc(), TritonData< IO >::fullShape_, TritonData< IO >::holder_, mps_fire::i, TritonData< IO >::resetSizes(), TritonData< IO >::shape_, edm::Span< T >::size(), and TritonData< IO >::variableDims_.

◆ resetSizes()

template<typename IO >
void TritonData< IO >::resetSizes ( )
private

Definition at line 112 of file TritonData.cc.

112  {
113  sizeShape_ = 0;
114  byteSizePerBatch_ = 0;
115  totalByteSize_ = 0;
116 }

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

◆ setBatchSize()

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

Definition at line 99 of file TritonData.cc.

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

◆ setResult()

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

Definition at line 93 of file TritonData.h.

93 { result_ = result; }

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

◆ setShape() [1/2]

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

◆ setShape() [2/2]

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

Definition at line 81 of file TritonData.cc.

81  {
82  unsigned locFull = fullLoc(loc);
83 
84  //check boundary
85  if (locFull >= fullShape_.size())
86  throw cms::Exception("TritonDataError")
87  << name_ << " setShape(): dimension " << locFull << " out of bounds (" << fullShape_.size() << ")";
88 
89  if (val != fullShape_[locFull]) {
90  if (dims_[locFull] == -1)
91  fullShape_[locFull] = val;
92  else
93  throw cms::Exception("TritonDataError")
94  << name_ << " setShape(): attempt to change value of non-variable shape dimension " << loc;
95  }
96 }

References Exception, and heppy_batch::val.

◆ shape()

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

Definition at line 69 of file TritonData.h.

69 { return shape_; }

References TritonData< IO >::shape_.

◆ sizeDims()

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

Definition at line 76 of file TritonData.h.

76 { 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 ( TritonInputContainer< DT >  ptr)

Definition at line 164 of file TritonData.cc.

164  {
165  //shouldn't be called twice
166  if (done_)
167  throw cms::Exception("TritonDataError") << name_ << " toServer() was already called for this event";
168 
169  const auto& data_in = *ptr;
170 
171  //check batch size
172  if (data_in.size() != batchSize_) {
173  throw cms::Exception("TritonDataError") << name_ << " toServer(): input vector has size " << data_in.size()
174  << " but specified batch size is " << batchSize_;
175  }
176 
177  //shape must be specified for variable dims or if batch size changes
178  data_->SetShape(fullShape_);
179 
180  if (byteSize_ != sizeof(DT))
181  throw cms::Exception("TritonDataError") << name_ << " toServer(): inconsistent byte size " << sizeof(DT)
182  << " (should be " << byteSize_ << " for " << dname_ << ")";
183 
184  computeSizes();
186  for (unsigned i0 = 0; i0 < batchSize_; ++i0) {
187  memResource_->copyInput(data_in[i0].data(), i0 * byteSizePerBatch_);
188  }
189  memResource_->set();
190 
191  //keep input data in scope
192  holder_ = ptr;
193  done_ = true;
194 }

References TritonData< IO >::batchSize_, TritonData< IO >::byteSize_, TritonData< IO >::byteSizePerBatch_, TritonData< IO >::computeSizes(), TritonData< IO >::data(), TritonData< IO >::data_, TritonData< IO >::dname_, TritonData< IO >::done_, GeomDetEnumerators::DT, Exception, TritonData< IO >::fullShape_, TritonData< IO >::holder_, TritonData< IO >::memResource_, TritonData< IO >::name_, TritonData< IO >::totalByteSize_, and TritonData< IO >::updateMem().

◆ uid()

template<typename IO>
unsigned TritonData< IO >::uid ( ) const
inlineprivate

Definition at line 109 of file TritonData.h.

109  {
110  static std::atomic<unsigned> uid{0};
111  return ++uid;
112  }

◆ updateMem()

template<typename IO >
void TritonData< IO >::updateMem ( size_t  size)
private

Definition at line 121 of file TritonData.cc.

121  {
122  if (!memResource_ or size > memResource_->size()) {
124  //avoid unnecessarily throwing in destructor
125  if (memResource_)
126  memResource_->close();
127  //need to destroy before constructing new instance because shared memory key will be reused
128  memResource_.reset();
129  memResource_ = std::make_shared<TritonCpuShmResource<IO>>(this, shmName_, size);
130  }
131 #ifdef TRITON_ENABLE_GPU
133  //avoid unnecessarily throwing in destructor
134  if (memResource_)
135  memResource_->close();
136  //need to destroy before constructing new instance because shared memory key will be reused
137  memResource_.reset();
138  memResource_ = std::make_shared<TritonGpuShmResource<IO>>(this, shmName_, size);
139  }
140 #endif
141  //for remote/heap, size increases don't matter
142  else if (!memResource_)
143  memResource_ = std::make_shared<TritonHeapResource<IO>>(this, shmName_, size);
144  }
145 }

References LocalCPU, LocalGPU, or, and findQualityFiles::size.

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

◆ variableDims()

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

Definition at line 75 of file TritonData.h.

75 { return variableDims_; }

References TritonData< IO >::variableDims_.

◆ xput()

std::string TritonOutputData::xput ( ) const
private

Definition at line 58 of file TritonData.cc.

58  {
59  return "input";
60 }

Friends And Related Function Documentation

◆ TritonClient

template<typename IO>
friend class TritonClient
friend

Definition at line 81 of file TritonData.h.

◆ TritonCpuShmResource< IO >

template<typename IO>
friend class TritonCpuShmResource< IO >
friend

Definition at line 84 of file TritonData.h.

◆ TritonHeapResource< IO >

template<typename IO>
friend class TritonHeapResource< IO >
friend

Definition at line 83 of file TritonData.h.

◆ TritonMemResource< IO >

template<typename IO>
friend class TritonMemResource< IO >
friend

Definition at line 82 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

◆ byteSizePerBatch_

template<typename IO>
size_t TritonData< IO >::byteSizePerBatch_
private

Definition at line 132 of file TritonData.h.

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

◆ client_

template<typename IO>
TritonClient* TritonData< IO >::client_
private

Definition at line 118 of file TritonData.h.

◆ 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 121 of file TritonData.h.

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

◆ dname_

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

◆ done_

template<typename IO>
bool TritonData< IO >::done_ {}
mutableprivate

◆ dtype_

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

Definition at line 129 of file TritonData.h.

◆ fullShape_

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

Definition at line 124 of file TritonData.h.

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

◆ holder_

template<typename IO>
std::shared_ptr<void> TritonData< IO >::holder_
private

Definition at line 137 of file TritonData.h.

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

◆ memResource_

template<typename IO>
std::shared_ptr<TritonMemResource<IO> > TritonData< IO >::memResource_
private

◆ name_

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

Definition at line 116 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 122 of file TritonData.h.

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

◆ productDims_

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

Definition at line 127 of file TritonData.h.

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

◆ result_

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

Definition at line 139 of file TritonData.h.

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

◆ shape_

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

◆ shmName_

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

Definition at line 120 of file TritonData.h.

◆ sizeShape_

template<typename IO>
size_t TritonData< IO >::sizeShape_
private

Definition at line 131 of file TritonData.h.

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

◆ totalByteSize_

template<typename IO>
size_t TritonData< IO >::totalByteSize_
private

Definition at line 133 of file TritonData.h.

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

◆ useShm_

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

Definition at line 119 of file TritonData.h.

◆ variableDims_

template<typename IO>
bool TritonData< IO >::variableDims_
private
TritonData::data
IO * data()
Definition: TritonData.h:94
mps_fire.i
i
Definition: mps_fire.py:428
TritonClient::useSharedMemory
bool useSharedMemory() const
Definition: TritonClient.h:41
TritonData::client_
TritonClient * client_
Definition: TritonData.h:118
TritonData::result_
std::shared_ptr< Result > result_
Definition: TritonData.h:139
TritonData::sizeShape
int64_t sizeShape() const
Definition: TritonData.h:78
TritonData::noBatch_
bool noBatch_
Definition: TritonData.h:122
TritonData::xput
std::string xput() const
Definition: TritonData.cc:58
TritonData::batchSize_
unsigned batchSize_
Definition: TritonData.h:123
TritonData::resetSizes
void resetSizes()
Definition: TritonData.cc:112
TritonData::memResource_
std::shared_ptr< TritonMemResource< IO > > memResource_
Definition: TritonData.h:138
TritonData::fullShape_
ShapeType fullShape_
Definition: TritonData.h:124
TritonData::sizeDims
int64_t sizeDims() const
Definition: TritonData.h:76
TritonData::dtype_
inference::DataType dtype_
Definition: TritonData.h:129
TritonData::createObject
void createObject(IO **ioptr)
TritonClient::serverType
TritonServerType serverType() const
Definition: TritonClient.h:46
TritonData::dimProduct
int64_t dimProduct(const ShapeView &vec) const
Definition: TritonData.h:104
TritonData::name_
std::string name_
Definition: TritonData.h:116
TritonData::shmName_
std::string shmName_
Definition: TritonData.h:120
mps_fire.end
end
Definition: mps_fire.py:242
TritonData::variableDims_
bool variableDims_
Definition: TritonData.h:126
TritonClient::client
auto client()
Definition: TritonClient.h:87
TritonData::dname_
std::string dname_
Definition: TritonData.h:128
TritonData::data_
std::shared_ptr< IO > data_
Definition: TritonData.h:117
edm::Span::size
auto size() const
Definition: Span.h:24
TritonData::done_
bool done_
Definition: TritonData.h:141
TritonData::uid
unsigned uid() const
Definition: TritonData.h:109
GeomDetEnumerators::DT
Definition: GeomDetEnumerators.h:18
TritonServerType::LocalCPU
TritonOutput
std::vector< edm::Span< const DT * > > TritonOutput
Definition: TritonData.h:35
TritonData::shape
const ShapeView & shape() const
Definition: TritonData.h:69
TritonData::fullLoc
unsigned fullLoc(unsigned loc) const
Definition: TritonData.h:90
TritonData::shape_
ShapeView shape_
Definition: TritonData.h:125
TritonData::byteSizePerBatch_
size_t byteSizePerBatch_
Definition: TritonData.h:132
TritonData::holder_
std::shared_ptr< void > holder_
Definition: TritonData.h:137
TritonData::updateMem
void updateMem(size_t size)
Definition: TritonData.cc:121
TritonData::computeSizes
void computeSizes()
Definition: TritonData.cc:106
TritonData::totalByteSize_
size_t totalByteSize_
Definition: TritonData.h:133
TritonData::dims_
const ShapeType dims_
Definition: TritonData.h:121
TritonServerType::LocalGPU
heppy_batch.val
val
Definition: heppy_batch.py:351
TritonData::sizeShape_
size_t sizeShape_
Definition: TritonData.h:131
diffTwoXMLs.r1
r1
Definition: diffTwoXMLs.py:53
Exception
Definition: hltDiff.cc:245
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
or
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
TritonData::byteSize_
int64_t byteSize_
Definition: TritonData.h:130
mps_fire.result
result
Definition: mps_fire.py:311
cms::Exception
Definition: Exception.h:70
TritonData::useShm_
bool useShm_
Definition: TritonData.h:119
hltrates_dqm_sourceclient-live_cfg.offset
offset
Definition: hltrates_dqm_sourceclient-live_cfg.py:82
TritonData::productDims_
int64_t productDims_
Definition: TritonData.h:127
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
TritonData::anyNeg
bool anyNeg(const ShapeView &vec) const
Definition: TritonData.h:101