LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
lartriton::TritonData< IO > Class Template Reference

#include "TritonData.h"

Public Types

using Result = nic::InferResult
 
using TensorMetadata = inference::ModelMetadataResponse_TensorMetadata
 
using ShapeType = std::vector< int64_t >
 
using ShapeView = triton_span::Span< ShapeType::const_iterator >
 

Public Member Functions

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

Private Member Functions

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

Private Attributes

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

Friends

class TritonClient
 

Detailed Description

template<typename IO>
class lartriton::TritonData< IO >

Definition at line 32 of file TritonData.h.

Member Typedef Documentation

template<typename IO>
using lartriton::TritonData< IO >::Result = nic::InferResult

Definition at line 34 of file TritonData.h.

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

Definition at line 36 of file TritonData.h.

Definition at line 37 of file TritonData.h.

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

Definition at line 35 of file TritonData.h.

Constructor & Destructor Documentation

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

Definition at line 18 of file TritonData.cc.

References lartriton::TritonData< IO >::createObject(), lartriton::TritonData< IO >::data_, lartriton::TritonData< IO >::dname_, lartriton::TritonData< IO >::fullShape_, lartriton::TritonData< IO >::name_, and lartriton::TritonData< IO >::setShape().

21  : name_(name)
22  , dims_(model_info.shape().begin(), model_info.shape().end())
23  , noBatch_(noBatch)
24  , batchSize_(0)
25  , fullShape_(dims_)
26  , shape_(fullShape_.begin() + (noBatch_ ? 0 : 1), fullShape_.end())
29  , dname_(model_info.datatype())
30  , dtype_(ni::ProtocolStringToDataType(dname_))
31  , byteSize_(ni::GetDataTypeByteSize(dtype_))
32  {
33  //create input or output object
34  IO* iotmp;
35  createObject(&iotmp);
36  data_.reset(iotmp);
37  }
std::string name_
Definition: TritonData.h:87
const ShapeType dims_
Definition: TritonData.h:89
bool anyNeg(const ShapeView &vec) const
Definition: TritonData.h:76
ShapeType fullShape_
Definition: TritonData.h:92
inference::DataType dtype_
Definition: TritonData.h:97
void createObject(IO **ioptr) const
std::shared_ptr< IO > data_
Definition: TritonData.h:88
int64_t dimProduct(const ShapeView &vec) const
Definition: TritonData.h:80
std::string dname_
Definition: TritonData.h:96

Member Function Documentation

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

Definition at line 76 of file TritonData.h.

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

77  {
78  return std::any_of(vec.begin(), vec.end(), [](int64_t i) { return i < 0; });
79  }
template<typename IO>
unsigned lartriton::TritonData< IO >::batchSize ( ) const
inline

Definition at line 56 of file TritonData.h.

References lartriton::TritonData< IO >::batchSize_.

56 { return batchSize_; }
template<typename IO>
int64_t lartriton::TritonData< IO >::byteSize ( ) const
inline

Definition at line 54 of file TritonData.h.

References lartriton::TritonData< IO >::byteSize_.

54 { return byteSize_; }
template<typename IO>
void lartriton::TritonData< IO >::createObject ( IO **  ioptr) const
private
template<typename IO>
IO* lartriton::TritonData< IO >::data ( )
inlineprivate

Definition at line 73 of file TritonData.h.

References lartriton::TritonData< IO >::data_.

73 { return data_.get(); }
std::shared_ptr< IO > data_
Definition: TritonData.h:88
template<typename IO>
int64_t lartriton::TritonData< IO >::dimProduct ( const ShapeView vec) const
inlineprivate

Definition at line 80 of file TritonData.h.

References triton_span::Span< T >::begin(), lartriton::TritonData< IO >::createObject(), and triton_span::Span< T >::end().

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

81  {
82  return std::accumulate(vec.begin(), vec.end(), 1, std::multiplies<int64_t>());
83  }
template<typename IO>
const std::string& lartriton::TritonData< IO >::dname ( ) const
inline

Definition at line 55 of file TritonData.h.

References lartriton::TritonData< IO >::dname_.

55 { return dname_; }
std::string dname_
Definition: TritonData.h:96
template<typename DT >
TritonOutput< DT > lartriton::TritonOutputData::fromServer ( ) const

Definition at line 143 of file TritonData.cc.

References lartriton::TritonData< IO >::batchSize_, lartriton::TritonData< IO >::byteSize_, lartriton::TritonData< IO >::dname_, lartriton::TritonData< IO >::name_, lartriton::TritonData< IO >::result_, lartriton::TritonData< IO >::sizeShape(), and triton_utils::throwIfError().

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

144  {
145  if (!result_) {
146  throw cet::exception("TritonDataError") << name_ << " output(): missing result";
147  }
148 
149  if (byteSize_ != sizeof(DT)) {
150  throw cet::exception("TritonDataError")
151  << name_ << " output(): inconsistent byte size " << sizeof(DT) << " (should be "
152  << byteSize_ << " for " << dname_ << ")";
153  }
154 
155  uint64_t nOutput = sizeShape();
156  TritonOutput<DT> dataOut;
157  const uint8_t* r0;
158  size_t contentByteSize;
159  size_t expectedContentByteSize = nOutput * byteSize_ * batchSize_;
160  triton_utils::throwIfError(result_->RawData(name_, &r0, &contentByteSize),
161  "output(): unable to get raw");
162  if (contentByteSize != expectedContentByteSize) {
163  throw cet::exception("TritonDataError")
164  << name_ << " output(): unexpected content byte size " << contentByteSize << " (expected "
165  << expectedContentByteSize << ")";
166  }
167 
168  const DT* r1 = reinterpret_cast<const DT*>(r0);
169  dataOut.reserve(batchSize_);
170  for (unsigned i0 = 0; i0 < batchSize_; ++i0) {
171  auto offset = i0 * nOutput;
172  dataOut.emplace_back(r1 + offset, r1 + offset + nOutput);
173  }
174 
175  return dataOut;
176  }
std::string name_
Definition: TritonData.h:87
void throwIfError(const Error &err, std::string_view msg)
Definition: triton_utils.cc:26
int64_t sizeShape() const
Definition: TritonData.h:62
std::shared_ptr< Result > result_
Definition: TritonData.h:100
std::string dname_
Definition: TritonData.h:96
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void lartriton::TritonOutputData::reset ( )
private
template<typename IO >
void lartriton::TritonData< IO >::setBatchSize ( unsigned  bsize)
private
template<typename IO>
void lartriton::TritonData< IO >::setResult ( std::shared_ptr< Result result)
inlineprivate

Definition at line 72 of file TritonData.h.

References lartriton::TritonData< IO >::result_.

72 { result_ = result; }
std::shared_ptr< Result > result_
Definition: TritonData.h:100
template<typename IO>
bool lartriton::TritonData< IO >::setShape ( const ShapeType newShape)
inline

Definition at line 43 of file TritonData.h.

References lartriton::TritonData< IO >::setShape().

Referenced by lartriton::TritonData< IO >::setShape(), and lartriton::TritonData< IO >::TritonData().

43 { return setShape(newShape, true); }
bool setShape(const ShapeType &newShape)
Definition: TritonData.h:43
template<typename IO>
bool lartriton::TritonData< IO >::setShape ( unsigned  loc,
int64_t  val 
)
inline

Definition at line 44 of file TritonData.h.

References lartriton::TritonData< IO >::fromServer(), lartriton::TritonData< IO >::setShape(), and lartriton::TritonData< IO >::toServer().

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

44 { return setShape(loc, val, true); }
bool setShape(const ShapeType &newShape)
Definition: TritonData.h:43
template<typename IO>
bool lartriton::TritonData< IO >::setShape ( const ShapeType newShape,
bool  canThrow 
)
private
template<typename IO >
bool lartriton::TritonData< IO >::setShape ( unsigned  loc,
int64_t  val,
bool  canThrow 
)
private

Definition at line 63 of file TritonData.cc.

References lartriton::TritonData< IO >::dims_, lartriton::TritonData< IO >::fullShape_, MF_LOG_WARNING, lartriton::TritonData< IO >::name_, and lartriton::TritonData< IO >::noBatch_.

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 ("
71  << fullShape_.size() << ")";
72  if (canThrow)
73  throw cet::exception("TritonDataError") << msg.str();
74  else {
75  MF_LOG_WARNING("TritonDataWarning") << msg.str();
76  return false;
77  }
78  }
79 
80  if (val != fullShape_[full_loc]) {
81  if (dims_[full_loc] == -1) {
82  fullShape_[full_loc] = val;
83  return true;
84  }
85  else {
86  msg << name_ << " setShape(): attempt to change value of non-variable shape dimension "
87  << loc;
88  if (canThrow)
89  throw cet::exception("TritonDataError") << msg.str();
90  else {
91  MF_LOG_WARNING("TritonDataError") << msg.str();
92  return false;
93  }
94  }
95  }
96 
97  return true;
98  }
std::string name_
Definition: TritonData.h:87
const ShapeType dims_
Definition: TritonData.h:89
ShapeType fullShape_
Definition: TritonData.h:92
#define MF_LOG_WARNING(category)
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
template<typename IO>
const ShapeView& lartriton::TritonData< IO >::shape ( ) const
inline

Definition at line 53 of file TritonData.h.

References lartriton::TritonData< IO >::shape_.

53 { return shape_; }
template<typename IO>
int64_t lartriton::TritonData< IO >::sizeDims ( ) const
inline

Definition at line 60 of file TritonData.h.

References lartriton::TritonData< IO >::productDims_.

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

60 { return productDims_; }
template<typename DT >
void lartriton::TritonInputData::toServer ( std::shared_ptr< TritonInput< DT >>  ptr)

Definition at line 110 of file TritonData.cc.

References lartriton::TritonData< IO >::batchSize_, lartriton::TritonData< IO >::byteSize_, lartriton::TritonData< IO >::data_, lartriton::TritonData< IO >::dname_, lartriton::TritonData< IO >::fullShape_, lartriton::TritonData< IO >::holder_, lartriton::TritonData< IO >::name_, lartriton::TritonData< IO >::sizeShape(), triton_utils::throwIfError(), and util::to_string().

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

111  {
112  const auto& data_in = *ptr;
113 
114  //check batch size
115  if (data_in.size() != batchSize_) {
116  throw cet::exception("TritonDataError")
117  << name_ << " input(): input vector has size " << data_in.size()
118  << " but specified batch size is " << batchSize_;
119  }
120 
121  //shape must be specified for variable dims or if batch size changes
122  data_->SetShape(fullShape_);
123 
124  if (byteSize_ != sizeof(DT))
125  throw cet::exception("TritonDataError")
126  << name_ << " input(): inconsistent byte size " << sizeof(DT) << " (should be " << byteSize_
127  << " for " << dname_ << ")";
128 
129  int64_t nInput = sizeShape();
130  for (unsigned i0 = 0; i0 < batchSize_; ++i0) {
131  const DT* arr = data_in[i0].data();
133  data_->AppendRaw(reinterpret_cast<const uint8_t*>(arr), nInput * byteSize_),
134  name_ + " input(): unable to set data for batch entry " + std::to_string(i0));
135  }
136 
137  //keep input data in scope
138  holder_ = std::move(ptr);
139  }
std::string name_
Definition: TritonData.h:87
ShapeType fullShape_
Definition: TritonData.h:92
void throwIfError(const Error &err, std::string_view msg)
Definition: triton_utils.cc:26
decltype(auto) constexpr to_string(T &&obj)
ADL-aware version of std::to_string.
int64_t sizeShape() const
Definition: TritonData.h:62
std::shared_ptr< IO > data_
Definition: TritonData.h:88
std::string dname_
Definition: TritonData.h:96
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
template<typename IO>
bool lartriton::TritonData< IO >::variableDims ( ) const
inline

Definition at line 59 of file TritonData.h.

References lartriton::TritonData< IO >::variableDims_.

59 { return variableDims_; }

Friends And Related Function Documentation

template<typename IO>
friend class TritonClient
friend

Definition at line 65 of file TritonData.h.

Member Data Documentation

template<typename IO>
int64_t lartriton::TritonData< IO >::byteSize_
private
template<typename IO>
const ShapeType lartriton::TritonData< IO >::dims_
private

Definition at line 89 of file TritonData.h.

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

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

Definition at line 97 of file TritonData.h.

template<typename IO>
std::any lartriton::TritonData< IO >::holder_
private
template<typename IO>
bool lartriton::TritonData< IO >::noBatch_
private
template<typename IO>
int64_t lartriton::TritonData< IO >::productDims_
private

Definition at line 95 of file TritonData.h.

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

template<typename IO>
std::shared_ptr<Result> lartriton::TritonData< IO >::result_
private
template<typename IO>
ShapeView lartriton::TritonData< IO >::shape_
private
template<typename IO>
bool lartriton::TritonData< IO >::variableDims_
private

The documentation for this class was generated from the following files: