1#ifndef TMVA_SOFIE_SOFIE_COMMON
2#define TMVA_SOFIE_SOFIE_COMMON
27 UNDEFINED = 0,
FLOAT = 1,
UINT8 = 2,
INT8 = 3,
UINT16 = 4,
INT16 = 5,
INT32 = 6,
INT64 = 7,
STRING = 8, BOOL = 9,
97 return !(*
this == rhs);
127 static bool IsDim() {
return true; }
131 static bool IsDim() {
return false; }
139 static const std::string
Name() {
return "float"; }
143 static const std::string
Name() {
return "double"; }
147 static const std::string
Name() {
return "int64_t"; }
151 static const std::string
Name() {
return "int32_t"; }
155 static const std::string
Name() {
return "uint32_t"; }
159 static const std::string
Name() {
return "uint64_t"; }
163 static const std::string
Name() {
return "bool"; }
167 static const std::string
Name() {
return "int8_t"; }
171 static const std::string
Name() {
return "uint8_t"; }
180 throw std::invalid_argument(
"New size exceeds available tensor size.");
215 std::stringstream ret;
216 ret << std::to_string(
value);
222 std::stringstream ret;
224 if (std::isinf(
value))
225 ret << (
value > 0 ?
"std::numeric_limits<float>::infinity()" :
226 "-std::numeric_limits<float>::infinity()");
227 else if (std::isnan(
value))
228 ret <<
"std::numeric_limits<float>::quiet_NaN()";
230 ret << std::setprecision(std::numeric_limits<float>::max_digits10);
238 std::stringstream ret;
240 if (std::isinf(
value))
241 ret << (
value > 0 ?
"std::numeric_limits<double>::infinity()" :
242 "-std::numeric_limits<double>::infinity()");
243 else if (std::isnan(
value))
244 ret <<
"std::numeric_limits<double>::quiet_NaN()";
246 ret << std::setprecision(std::numeric_limits<double>::max_digits10);
254 std::stringstream ret;
255 if (
value == INT64_MIN)
258 ret << std::to_string(
value);
266 std::stringstream ret;
268 for (
size_t i = 0; i < std::min(
n,maxprint); i++) {
270 if (i <
n-1) ret <<
", ";
271 if (i <
n-1 && i == maxprint-1) ret <<
"..... ";
307 template <
class T =
void>
310 return static_cast<T
const *
>(
fData.get());
344std::string
Clean_name(std::string input_tensor_name);
347bool AreSameShape(
const std::vector<size_t>&,
const std::vector<size_t>&);
348bool AreSameShape(
const std::vector<size_t>&,
const std::vector<Dim>&);
349bool AreSameShape(
const std::vector<Dim>&,
const std::vector<Dim>&);
366 size_t size = targetShape.size();
367 if (targetShape[1] != channel) {
368 std::stringstream ss;
369 ss <<
"TMVA::SOFIE - Error broadcasting Conv Bias of shape {";
370 ss << std::to_string(channel);
374 std::runtime_error(ss.str());
378 T* newData =
new T[targetLength];
380 if (targetLength == channel) {
381 std::copy(
data,
data + channel, newData);
387 for (
size_t i = 2; i <
size; i++)
388 cStride *= targetShape[i];
391 for (
size_t i = 0; i < channel; i++) {
392 std::fill(newData + i * cStride, newData + (i + 1) * cStride,
data[i]);
395 size_t batch = targetShape[0];
396 size_t bStride = channel * cStride;
397 for (
size_t i = 1; i < batch; i++) {
398 std::copy(newData, newData + bStride, newData + i * bStride);
406template<
typename T,
class ConstContT = std::span<const T>>
407void BroadcastTensor(ConstContT
data,
const std::vector<size_t>& shape,
const std::vector<size_t>& targetShape, T *broadcastedData) {
409 size_t size = shape.size();
411 size_t curLength =
data.size();
413 if (
size > 1 && shape.front() == targetShape.front() && shape.back() == 1) {
414 size_t bsize = targetShape.back();
416 for (
int k =
int(
size)-2; k >=0; k--) {
417 if (shape[k] != 1)
break;
418 bsize *= targetShape[k];
420 for (
size_t i = 0; i < curLength; i++) {
421 std::fill(broadcastedData + i*bsize, broadcastedData + (i+1)*bsize ,
data[i]);
426 std::copy(
data.begin(),
data.end(), broadcastedData);
432 for (
size_t idx = 0; idx <
size; idx++) {
433 size_t dim = shape[idx];
434 size_t targetDim = targetShape[idx];
435 if (dim == 1 && targetDim > 1) {
437 size_t newLength = curLength * targetDim;
439 size_t arrayLength = curLength / arrayNum;
441 if (arrayLength > 1) {
443 for (
size_t arrayIdx = 0; arrayIdx < arrayNum; arrayIdx++) {
444 for (
size_t targetIdx = 0; targetIdx < targetDim; targetIdx++) {
445 size_t offset = arrayIdx * arrayLength * targetDim + targetIdx * arrayLength;
446 std::copy(broadcastedData + arrayIdx * arrayLength,
447 broadcastedData + (arrayIdx + 1) * arrayLength,
448 newData.begin() +
offset);
453 for (
size_t arrayIdx = 0; arrayIdx < arrayNum; arrayIdx++) {
454 std::fill(newData.begin() + arrayIdx * targetDim,
455 newData.begin() + (arrayIdx + 1) * targetDim, broadcastedData[arrayIdx]);
459 curLength = newLength;
461 std::copy(newData.begin(), newData.begin() + newLength, broadcastedData);
464 arrayNum *= targetDim;
472 T* broadcastedData =
new T[targetLength];
474 BroadcastTensor<T>({
data, curLength}, shape, targetShape, broadcastedData);
475 return broadcastedData;
482 if (shape.size() < targetShape.size()) {
483 size_t targetSize = targetShape.size();
484 std::vector<size_t> newShape(targetSize, 1);
485 size_t offset = targetSize - shape.size();
486 std::copy(shape.begin(), shape.end(), newShape.begin() +
offset);
496 std::span<T> inData(
const_cast<T*
>(
data), curLength);
498 if (shape.size() < targetShape.size()) {
499 size_t targetSize = targetShape.size();
500 std::vector<size_t> newShape(targetSize, 1);
501 size_t offset = targetSize - shape.size();
502 std::copy(shape.begin(), shape.end(), newShape.begin() +
offset);
517extern "C" void sgemm_(
const char * transa,
const char * transb,
const int *
m,
const int *
n,
const int * k,
518 const float * alpha,
const float * A,
const int * lda,
const float * B,
const int * ldb,
519 const float * beta,
float * C,
const int * ldc);
524void EmitNestedLoops(std::stringstream &out,
size_t loopRank,
const std::vector<Dim> shape);
553 const std::string & modelNamespace,
554 bool sgemmAlreadyDeclared =
false);
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
bool IsWeightTensor() const
std::shared_ptr< void > const & sharedptr() const
std::shared_ptr< void > fData
! Transient shared data
InitializedTensor()=default
ETensorType fType
Encodes the type of the data.
std::vector< std::size_t > const & shape() const
std::vector< std::size_t > fShape
The shape of the data in terms of elements in each dimension.
bool fIsNotWritable
Flag to indicate that tensor values do not need to be written as weight or generated code.
bool IsConstantTensor() const
bool fConstant
Flag specifying if tensor is a Constant one (coming from a Constant operator)
ETensorType const & type() const
bool IsNotWritable() const
InitializedTensor(ETensorType type, std::span< std::size_t > shape, std::shared_ptr< void > data, bool typeConstant=false)
void sgemm_(const char *transa, const char *transb, const int *m, const int *n, const int *k, const float *alpha, const float *A, const int *lda, const float *B, const int *ldb, const float *beta, float *C, const int *ldc)
bool AreSameShape(const std::vector< size_t > &, const std::vector< size_t > &)
T * BroadcastConvBias(const T *data, const size_t channel, const std::vector< size_t > &targetShape)
std::vector< size_t > UnidirectionalBroadcastShape(std::vector< size_t > &, std::vector< size_t > &)
void BroadcastTensor(ConstContT data, const std::vector< size_t > &shape, const std::vector< size_t > &targetShape, T *broadcastedData)
std::string Clean_name(std::string input_tensor_name)
std::vector< size_t > MultidirectionalBroadcastShape(std::vector< std::vector< size_t > >)
T * UnidirectionalBroadcast(const T *data, const std::vector< size_t > &shape, const std::vector< size_t > &targetShape)
T * CreateBroadcastTensor(const T *data, const std::vector< size_t > &shape, const std::vector< size_t > &targetShape, size_t targetLength)
std::vector< size_t > ComputeStrideFromShape(const std::vector< size_t > &shape)
compute stride of a tensor given its shape (assume layout is row-major)
std::string ConvertDimShapeToString(const std::vector< Dim > &shape)
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::string ConvertValToString< double >(double value)
std::string ConvertValuesToString(size_t n, const T *data, size_t maxprint=-1)
std::vector< Dim > ConvertShapeToDim(const std::vector< size_t > &shape)
Convert shape from integer format to dynamic one (based on Dim)
constexpr size_t GetTypeSize(ETensorType type)
ETensorType GetTemplatedType(T)
std::string ConvertValToString< float >(float value)
std::vector< size_t > ConvertShapeToInt(const std::vector< Dim > &shape)
Convert shape based on Dim to integer format.
std::string ConvertTypeToString(ETensorType type)
ETensorType ConvertStringToType(std::string type)
HelperFunctionsCode GenerateHelperFunctionsCode(const std::set< std::string > &neededHelpers, const std::string &modelNamespace, bool sgemmAlreadyDeclared=false)
Return the standalone C++ source of the inference helper functions requested in neededHelpers (see RM...
std::ostream & operator<<(std::ostream &os, const Dim &d)
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
void EmitNestedLoops(std::stringstream &out, size_t loopRank, const std::vector< Dim > shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
void CloseNestedLoops(std::stringstream &out, size_t loopRank)
std::string ConvertValToString(T value)
std::string ConvertValToString< int64_t >(int64_t value)
bool IsInteger(const std::string &s)
bool operator!=(const Dim &rhs) const
bool operator==(const Dim &rhs) const
Dim(const std::string &p, size_t d=0)
std::ostream & operator<<(std::ostream &os) const
std::string GetVal() const
Source code of the inference helper functions to embed in generated code so that it is standalone and...
std::string definitions
function/type definitions to place inside the generated model namespace
std::string cladDefinitions
Clad custom-derivative definitions to place at file scope (outside the model namespace) so that Clad ...
std::string includes
#include directives to place in the header preamble
std::map< size_t, TensorMemoryInfo > total_stack
std::map< size_t, size_t > available_stack
std::vector< size_t > shape
void merge(const TensorMemoryInfo &other)
std::string_view tensor_name
TensorMemoryInfo split(const std::string_view new_name, size_t new_size)
static const std::string Name()
static const std::string Name()
static const std::string Name()
static const std::string Name()
static const std::string Name()
static const std::string Name()
static const std::string Name()
static const std::string Name()
static const std::string Name()