1#ifndef TMVA_SOFIE_SOFIE_COMMON
2#define TMVA_SOFIE_SOFIE_COMMON
20namespace Experimental{
26 UNDEFINED = 0,
FLOAT = 1,
UNINT8 = 2,
INT8 = 3,
UINT16 = 4,
INT16 = 5,
INT32 = 6,
INT64 = 7,
STRING = 8,
BOOL = 9,
87 std::stringstream ret;
89 for (
size_t i = 0; i <
n; i++) {
91 if (i <
n-1) ret <<
", ";
119 template <
class T =
void>
122 return static_cast<T
const *
>(
fData.get());
130 for (std::size_t item :
fShape) {
131 fSize *=
static_cast<int>(item);
140 throw std::runtime_error(
"TMVA::SOFIE doesn't yet supports serialising data-type " +
198bool AreSameShape(
const std::vector<size_t>&,
const std::vector<size_t>&);
199bool AreSameShape(
const std::vector<size_t>&,
const std::vector<Dim>&);
200bool AreSameShape(
const std::vector<Dim>&,
const std::vector<Dim>&);
209std::string
Clean_name(std::string input_tensor_name);
213 size_t size = targetShape.size();
214 if (targetShape[1] != channel) {
215 std::stringstream ss;
216 ss <<
"TMVA::SOFIE - Error broadcasting Conv Bias of shape {";
217 ss << std::to_string(channel);
221 std::runtime_error(ss.str());
225 T* newData =
new T[targetLength];
227 if (targetLength == channel) {
228 std::copy(
data,
data + channel, newData);
234 for (
size_t i = 2; i <
size; i++)
235 cStride *= targetShape[i];
238 for (
size_t i = 0; i < channel; i++) {
239 std::fill(newData + i * cStride, newData + (i + 1) * cStride,
data[i]);
242 size_t batch = targetShape[0];
243 size_t bStride = channel * cStride;
244 for (
size_t i = 1; i < batch; i++) {
245 std::copy(newData, newData + bStride, newData + i * bStride);
254T*
BroadcastTensor(
const T*
data,
const std::vector<size_t>& shape,
const std::vector<size_t>& targetShape) {
256 size_t size = shape.size();
261 T* broadcastedData =
new T[targetLength];
262 std::copy(
data,
data + curLength, broadcastedData);
266 std::vector<T> newData(targetLength);
268 for (
size_t idx = 0; idx <
size; idx++) {
269 size_t dim = shape[idx];
270 size_t targetDim = targetShape[idx];
271 if (dim == 1 && targetDim > 1) {
273 size_t newLength = curLength * targetDim;
275 size_t arrayLength = curLength / arrayNum;
277 if (arrayLength > 1) {
279 for (
size_t arrayIdx = 0; arrayIdx < arrayNum; arrayIdx++) {
280 for (
size_t targetIdx = 0; targetIdx < targetDim; targetIdx++) {
281 size_t offset = arrayIdx * arrayLength * targetDim + targetIdx * arrayLength;
282 std::copy(broadcastedData + arrayIdx * arrayLength,
283 broadcastedData + (arrayIdx + 1) * arrayLength,
284 newData.begin() +
offset);
289 for (
size_t arrayIdx = 0; arrayIdx < arrayNum; arrayIdx++) {
290 std::fill(newData.begin() + arrayIdx * targetDim,
291 newData.begin() + (arrayIdx + 1) * targetDim, broadcastedData[arrayIdx]);
295 curLength = newLength;
297 std::copy(newData.begin(), newData.begin() + newLength, broadcastedData);
300 arrayNum *= targetDim;
302 return broadcastedData;
309 if (shape.size() < targetShape.size()) {
310 size_t targetSize = targetShape.size();
311 std::vector<size_t> newShape(targetSize, 1);
312 size_t offset = targetSize - shape.size();
313 std::copy(shape.begin(), shape.end(), newShape.begin() +
offset);
314 return BroadcastTensor<T>(
data, newShape, targetShape);
316 return BroadcastTensor<T>(
data, shape, targetShape);
326 return static_cast<unsigned>(
a) <
static_cast<unsigned>(
b);
350void Im2col(
const T *data_im,
const int channels,
const int height,
const int width,
const int kernel_h,
351 const int kernel_w,
const int pad_h,
const int pad_w,
const int stride_h,
const int stride_w,
352 const int dilation_h,
const int dilation_w, T *data_col)
354 const int output_h = (
height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
355 const int output_w = (
width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
357 for (
int channel = channels; channel--; data_im += channel_size) {
358 for (
int kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
359 for (
int kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
360 int input_row = -pad_h + kernel_row * dilation_h;
361 for (
int output_rows = output_h; output_rows; output_rows--) {
363 for (
int output_cols = output_w; output_cols; output_cols--) {
367 int input_col = -pad_w + kernel_col * dilation_w;
368 for (
int output_col = output_w; output_col; output_col--) {
370 *(data_col++) = data_im[input_row *
width + input_col];
374 input_col += stride_w;
377 input_row += stride_h;
387 const int depth,
const int height,
const int width,
388 const int kernel_d,
const int kernel_h,
const int kernel_w,
389 const int pad_d,
const int pad_h,
const int pad_w,
390 const int stride_d,
const int stride_h,
const int stride_w,
391 const int dilation_d,
const int dilation_h,
const int dilation_w, T *data_col)
393 const int output_h = (
height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
394 const int output_w = (
width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
395 const int output_d = (depth + 2 * pad_d - (dilation_d * (kernel_d - 1) + 1)) / stride_d + 1;
398 for (
int channel = channels; channel--; data_im += channel_size) {
399 for (
int kernel_depth = 0; kernel_depth < kernel_d; kernel_depth++) {
400 for (
int kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
401 for (
int kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
402 int input_dep = -pad_d + kernel_depth * dilation_d;
403 for (
int output_dep = output_d; output_dep; output_dep--) {
405 for (
int output_rows = output_h; output_rows; output_rows--) {
406 for (
int output_cols = output_w; output_cols; output_cols--) {
411 int input_row = -pad_h + kernel_row * dilation_h;
412 for (
int output_rows = output_h; output_rows; output_rows--) {
414 for (
int output_cols = output_w; output_cols; output_cols--) {
418 int input_col = -pad_w + kernel_col * dilation_w;
419 for (
int output_col = output_w; output_col; output_col--) {
421 *(data_col++) = data_im[input_dep *
width *
height + input_row *
width + input_col];
425 input_col += stride_w;
428 input_row += stride_h;
431 input_dep += stride_d;
439template <
typename Dtype>
440void col2im(
const Dtype* data_col,
const int channels,
441 const int height,
const int width,
const int kernel_h,
const int kernel_w,
442 const int pad_h,
const int pad_w,
443 const int stride_h,
const int stride_w,
444 const int dilation_h,
const int dilation_w,
447 std::fill(data_im, data_im +
height *
width * channels, 0.);
451 const int output_h = (
height + 2 * pad_h -
452 (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
453 const int output_w = (
width + 2 * pad_w -
454 (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
456 for (
int channel = channels; channel--; data_im += channel_size) {
457 for (
int kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
458 for (
int kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
459 int input_row = -pad_h + kernel_row * dilation_h;
460 for (
int output_rows = output_h; output_rows; output_rows--) {
462 data_col += output_w;
464 int input_col = -pad_w + kernel_col * dilation_w;
465 for (
int output_col = output_w; output_col; output_col--) {
472 data_im[input_row *
width + input_col] += *data_col;
475 input_col += stride_w;
478 input_row += stride_h;
491extern "C" void sgemm_(
const char * transa,
const char * transb,
const int *
m,
const int *
n,
const int * k,
492 const float * alpha,
const float * A,
const int * lda,
const float * B,
const int * ldb,
493 const float * beta,
float * C,
const int * ldc);
515 throw std::runtime_error(
"TMVA RTensor Concatenate - tensors have different memory layout");
516 auto & shape1 =
t1.GetShape();
518 if (
t1.GetSize()/shape1[axis] != t2.
GetSize()/shape2[axis]) {
519 std::cout <<
"axis " << axis <<
" sizes " <<
t1.GetSize() <<
" " << t2.
GetSize() <<
" ";
522 throw std::runtime_error(
"TMVA RTensor Concatenate - tensors have incompatible shapes");
524 std::vector<size_t> outShape = shape1;
525 outShape[axis] = shape1[axis] + shape2[axis];
527 if (
t1.GetMemoryLayout() == TMVA::Experimental::MemoryLayout::ColumnMajor) {
528 throw std::runtime_error(
"TMVA RTensor Concatenate is not yet supported for column major tensors");
531 auto & stride1 =
t1.GetStrides();
535 size_t s1 = (axis > 0) ? stride1[axis-1] :
t1.GetSize();
536 size_t s2 = (axis > 0) ? stride2[axis-1] : t2.
GetSize();
537 size_t sout = (axis > 0) ? outStride[axis-1] : tout.
GetSize();
538 size_t nb =
t1.GetSize()/
s1;
539 for (
size_t i = 0; i < nb; i++) {
540 std::copy(
t1.GetData() + i*
s1,
t1.GetData() + (i+1)*
s1, tout.
GetData() + i * sout );
564 std::copy(
data.node_data.GetData(),
data.node_data.GetData()+
data.node_data.GetSize(), out.node_data.GetData());
565 std::copy(
data.edge_data.GetData(),
data.edge_data.GetData()+
data.edge_data.GetSize(), out.edge_data.GetData());
566 std::copy(
data.global_data.GetData(),
data.global_data.GetData()+
data.global_data.GetSize(), out.global_data.GetData());
567 std::copy(
data.edge_index.GetData(),
data.edge_index.GetData()+
data.edge_index.GetSize(), out.edge_index.GetData());
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 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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t height
RTensor is a container with contiguous memory and shape information.
MemoryLayout GetMemoryLayout() const
RTensor< Value_t, Container_t > Copy(MemoryLayout layout=MemoryLayout::RowMajor) const
Copy RTensor to new object.
const Shape_t & GetStrides() const
std::size_t GetSize() const
const Shape_t & GetShape() const
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
char * fPersistentData
[fSize] Persistent version of the data
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
void CastSharedToPersistent()
bool fConstant
Flag specifying if tensor is a Constant one (coming from a Constant operator)
ETensorType const & type() const
void CastPersistentToShared()
InitializedTensor(ETensorType type, std::span< std::size_t > shape, std::shared_ptr< void > data, bool typeConstant=false)
int fSize
The size of the persistent data in bytes (not number of elements!)
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 > &)
void Im2col_3d(const T *data_im, const int channels, const int depth, const int height, const int width, const int kernel_d, const int kernel_h, const int kernel_w, const int pad_d, const int pad_h, const int pad_w, const int stride_d, const int stride_h, const int stride_w, const int dilation_d, const int dilation_h, const int dilation_w, T *data_col)
3d implementation
T * BroadcastConvBias(const T *data, const size_t channel, const std::vector< size_t > &targetShape)
void col2im(const Dtype *data_col, const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, Dtype *data_im)
T * BroadcastTensor(const T *data, const std::vector< size_t > &shape, const std::vector< size_t > &targetShape)
std::vector< size_t > UnidirectionalBroadcastShape(std::vector< size_t >, std::vector< size_t >)
std::string Clean_name(std::string input_tensor_name)
bool is_a_ge_zero_and_a_lt_b(int a, int b)
function to check if a >> 0 and a < MAX using a single comparison / use trick casting to unsigned val...
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)
void Im2col(const T *data_im, const int channels, const int height, const int width, const int kernel_h, const int kernel_w, const int pad_h, const int pad_w, const int stride_h, const int stride_w, const int dilation_h, const int dilation_w, T *data_col)
im2col : efficient function to re-arrange input data of convolution to a matrix that can be used by B...
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::vector< Dim > ConvertShapeToDim(std::vector< size_t > shape)
Convert shape from integer format to dynamic one (based on Dim)
std::string ConvertDynamicShapeToLength(std::vector< Dim > shape)
ETensorType GetTemplatedType(T)
std::string ConvertValuesToString(size_t n, const T *data)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::string ConvertTypeToString(ETensorType type)
std::string ConvertDynamicShapeToString(std::vector< Dim > shape)
ETensorType ConvertStringToType(std::string type)
TMVA::Experimental::RTensor< T > Concatenate(TMVA::Experimental::RTensor< T > &t1, TMVA::Experimental::RTensor< T > &t2, int axis=0)
std::vector< size_t > ConvertShapeToInt(std::vector< Dim > shape)
Convert shape based on Dim to integer format.
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
GNN_Data Copy(const GNN_Data &data)
create variable transformations
Dim(const std::string &p, size_t d=0)
std::string GetVal() const
RTensor< float > global_data
RTensor< float > edge_data
RTensor< int > edge_index
RTensor< float > node_data
std::vector< size_t > shape