LArSoft  v10_04_05
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 34 of file TritonData.h.

Member Typedef Documentation

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

Definition at line 36 of file TritonData.h.

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

Definition at line 38 of file TritonData.h.

Definition at line 39 of file TritonData.h.

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

Definition at line 37 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 17 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().

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

Member Function Documentation

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

Definition at line 107 of file TritonData.h.

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

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

Definition at line 87 of file TritonData.h.

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

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

Definition at line 85 of file TritonData.h.

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

85 { 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 104 of file TritonData.h.

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

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

Definition at line 111 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().

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

Definition at line 86 of file TritonData.h.

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

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

Definition at line 110 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 >::toServer().

111  {
112  if (!result_) {
113  throw cet::exception("TritonDataError") << name_ << " output(): missing result";
114  }
115 
116  if (byteSize_ != sizeof(DT)) {
117  throw cet::exception("TritonDataError")
118  << name_ << " output(): inconsistent byte size " << sizeof(DT) << " (should be "
119  << byteSize_ << " for " << dname_ << ")";
120  }
121 
122  uint64_t nOutput = sizeShape();
123  TritonOutput<DT> dataOut;
124  const uint8_t* r0;
125  size_t contentByteSize;
126  size_t expectedContentByteSize = nOutput * byteSize_ * batchSize_;
127  triton_utils::throwIfError(result_->RawData(name_, &r0, &contentByteSize),
128  "output(): unable to get raw");
129  if (contentByteSize != expectedContentByteSize) {
130  throw cet::exception("TritonDataError")
131  << name_ << " output(): unexpected content byte size " << contentByteSize << " (expected "
132  << expectedContentByteSize << ")";
133  }
134 
135  const DT* r1 = reinterpret_cast<const DT*>(r0);
136  dataOut.reserve(batchSize_);
137  for (unsigned i0 = 0; i0 < batchSize_; ++i0) {
138  auto offset = i0 * nOutput;
139  dataOut.emplace_back(r1 + offset, r1 + offset + nOutput);
140  }
141 
142  return dataOut;
143  }
void throwIfError(const Error &err, std::string_view msg)
Definition: triton_utils.cc:26
int64_t sizeShape() const
Definition: TritonData.h:93
std::shared_ptr< Result > result_
Definition: TritonData.h:131
std::string dname_
Definition: TritonData.h:127
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 103 of file TritonData.h.

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

103 { result_ = result; }
std::shared_ptr< Result > result_
Definition: TritonData.h:131
template<typename IO>
bool lartriton::TritonData< IO >::setShape ( const ShapeType newShape)
inline
template<typename IO>
bool lartriton::TritonData< IO >::setShape ( unsigned  loc,
int64_t  val 
)
inline

Definition at line 46 of file TritonData.h.

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

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

46 { return setShape(loc, val, true); }
bool setShape(const ShapeType &newShape)
Definition: TritonData.h:45
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 62 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_.

63  {
64  std::stringstream msg;
65  unsigned full_loc = loc + (noBatch_ ? 0 : 1);
66 
67  //check boundary
68  if (full_loc >= fullShape_.size()) {
69  msg << name_ << " setShape(): dimension " << full_loc << " out of bounds ("
70  << fullShape_.size() << ")";
71  if (canThrow)
72  throw cet::exception("TritonDataError") << msg.str();
73  else {
74  MF_LOG_WARNING("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  }
84  else {
85  msg << name_ << " setShape(): attempt to change value of non-variable shape dimension "
86  << loc;
87  if (canThrow)
88  throw cet::exception("TritonDataError") << msg.str();
89  else {
90  MF_LOG_WARNING("TritonDataError") << msg.str();
91  return false;
92  }
93  }
94  }
95 
96  return true;
97  }
const ShapeType dims_
Definition: TritonData.h:120
#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 84 of file TritonData.h.

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

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

Definition at line 91 of file TritonData.h.

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

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

91 { return productDims_; }
template<typename IO>
int64_t lartriton::TritonData< IO >::sizeShape ( ) const
inline
template<typename IO>
template<typename DT >
void lartriton::TritonData< IO >::toServer ( std::shared_ptr< TritonInput< DT >>  ptr)
inline

Definition at line 50 of file TritonData.h.

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

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

51  {
52  const auto& data_in = *ptr;
53 
54  //check batch size
55  if (data_in.size() != batchSize_) {
56  throw cet::exception("TritonDataError")
57  << name_ << " input(): input vector has size " << data_in.size()
58  << " but specified batch size is " << batchSize_;
59  }
60 
61  //shape must be specified for variable dims or if batch size changes
62  data_->SetShape(fullShape_);
63 
64  if (byteSize_ != sizeof(DT))
65  throw cet::exception("TritonDataError")
66  << name_ << " input(): inconsistent byte size " << sizeof(DT) << " (should be "
67  << byteSize_ << " for " << dname_ << ")";
68 
69  for (unsigned i0 = 0; i0 < batchSize_; ++i0) {
70  const DT* arr = data_in[i0].data();
72  data_->AppendRaw(reinterpret_cast<const uint8_t*>(arr), data_in[i0].size() * byteSize_),
73  name_ + " input(): unable to set data for batch entry " + std::to_string(i0));
74  }
75 
76  //keep input data in scope
77  holder_ = std::move(ptr);
78  }
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.
std::shared_ptr< IO > data_
Definition: TritonData.h:119
std::string dname_
Definition: TritonData.h:127
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
template<typename IO>
bool lartriton::TritonData< IO >::variableDims ( ) const
inline

Definition at line 90 of file TritonData.h.

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

90 { return variableDims_; }

Friends And Related Function Documentation

template<typename IO>
friend class TritonClient
friend

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

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

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

Definition at line 128 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 126 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: