1#ifndef TMVA_SOFIE_SOFIE_COMMON
2#define TMVA_SOFIE_SOFIE_COMMON
18namespace Experimental{
24 UNDEFINED = 0,
FLOAT = 1,
UNINT8 = 2,
INT8 = 3,
UINT16 = 4,
INT16 = 5,
INT32 = 6,
INT64 = 7,
STRING = 8,
BOOL = 9,
104 throw std::runtime_error(
"TMVA::SOFIE doesn't yet supports serialising data-type " +
131bool AreSameShape(
const std::vector<size_t>&,
const std::vector<size_t>&);
139std::string
Clean_name(std::string input_tensor_name);
143 size_t size = targetShape.size();
144 if (targetShape[1] != channel) {
145 std::stringstream ss;
146 ss <<
"TMVA::SOFIE - Error broadcasting Conv Bias of shape {";
147 ss << std::to_string(channel);
151 std::runtime_error(ss.str());
155 T* newData =
new T[targetLength];
157 if (targetLength == channel) {
158 std::copy(
data,
data + channel, newData);
164 for (
size_t i = 2; i <
size; i++)
165 cStride *= targetShape[i];
168 for (
size_t i = 0; i < channel; i++) {
169 std::fill(newData + i * cStride, newData + (i + 1) * cStride,
data[i]);
172 size_t batch = targetShape[0];
173 size_t bStride = channel * cStride;
174 for (
size_t i = 1; i < batch; i++) {
175 std::copy(newData, newData + bStride, newData + i * bStride);
184T*
BroadcastTensor(
const T*
data,
const std::vector<size_t>& shape,
const std::vector<size_t>& targetShape) {
186 size_t size = shape.size();
191 T* broadcastedData =
new T[targetLength];
192 std::copy(
data,
data + curLength, broadcastedData);
196 std::vector<T> newData(targetLength);
198 for (
size_t idx = 0; idx <
size; idx++) {
199 size_t dim = shape[idx];
200 size_t targetDim = targetShape[idx];
201 if (dim == 1 && targetDim > 1) {
203 size_t newLength = curLength * targetDim;
205 size_t arrayLength = curLength / arrayNum;
207 if (arrayLength > 1) {
209 for (
size_t arrayIdx = 0; arrayIdx < arrayNum; arrayIdx++) {
210 for (
size_t targetIdx = 0; targetIdx < targetDim; targetIdx++) {
211 size_t offset = arrayIdx * arrayLength * targetDim + targetIdx * arrayLength;
212 std::copy(broadcastedData + arrayIdx * arrayLength,
213 broadcastedData + (arrayIdx + 1) * arrayLength,
214 newData.begin() +
offset);
219 for (
size_t arrayIdx = 0; arrayIdx < arrayNum; arrayIdx++) {
220 std::fill(newData.begin() + arrayIdx * targetDim,
221 newData.begin() + (arrayIdx + 1) * targetDim, broadcastedData[arrayIdx]);
225 curLength = newLength;
227 std::copy(newData.begin(), newData.begin() + newLength, broadcastedData);
230 arrayNum *= targetDim;
232 return broadcastedData;
239 if (shape.size() < targetShape.size()) {
240 size_t targetSize = targetShape.size();
241 std::vector<size_t> newShape(targetSize, 1);
242 size_t offset = targetSize - shape.size();
243 std::copy(shape.begin(), shape.end(), newShape.begin() +
offset);
244 return BroadcastTensor<T>(
data, newShape, targetShape);
246 return BroadcastTensor<T>(
data, shape, targetShape);
255 return static_cast<unsigned>(
a) <
static_cast<unsigned>(
b);
279void Im2col(
const T *data_im,
const int channels,
const int height,
const int width,
const int kernel_h,
280 const int kernel_w,
const int pad_h,
const int pad_w,
const int stride_h,
const int stride_w,
281 const int dilation_h,
const int dilation_w, T *data_col)
283 const int output_h = (
height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
284 const int output_w = (
width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
286 for (
int channel = channels; channel--; data_im += channel_size) {
287 for (
int kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
288 for (
int kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
289 int input_row = -pad_h + kernel_row * dilation_h;
290 for (
int output_rows = output_h; output_rows; output_rows--) {
292 for (
int output_cols = output_w; output_cols; output_cols--) {
296 int input_col = -pad_w + kernel_col * dilation_w;
297 for (
int output_col = output_w; output_col; output_col--) {
299 *(data_col++) = data_im[input_row *
width + input_col];
303 input_col += stride_w;
306 input_row += stride_h;
316 const int depth,
const int height,
const int width,
317 const int kernel_d,
const int kernel_h,
const int kernel_w,
318 const int pad_d,
const int pad_h,
const int pad_w,
319 const int stride_d,
const int stride_h,
const int stride_w,
320 const int dilation_d,
const int dilation_h,
const int dilation_w, T *data_col)
322 const int output_h = (
height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
323 const int output_w = (
width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
324 const int output_d = (depth + 2 * pad_d - (dilation_d * (kernel_d - 1) + 1)) / stride_d + 1;
327 for (
int channel = channels; channel--; data_im += channel_size) {
328 for (
int kernel_depth = 0; kernel_depth < kernel_d; kernel_depth++) {
329 for (
int kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
330 for (
int kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
331 int input_dep = -pad_d + kernel_depth * dilation_d;
332 for (
int output_dep = output_d; output_dep; output_dep--) {
334 for (
int output_rows = output_h; output_rows; output_rows--) {
335 for (
int output_cols = output_w; output_cols; output_cols--) {
340 int input_row = -pad_h + kernel_row * dilation_h;
341 for (
int output_rows = output_h; output_rows; output_rows--) {
343 for (
int output_cols = output_w; output_cols; output_cols--) {
347 int input_col = -pad_w + kernel_col * dilation_w;
348 for (
int output_col = output_w; output_col; output_col--) {
350 *(data_col++) = data_im[input_dep *
width *
height + input_row *
width + input_col];
354 input_col += stride_w;
357 input_row += stride_h;
360 input_dep += stride_d;
368template <
typename Dtype>
369void col2im(
const Dtype* data_col,
const int channels,
370 const int height,
const int width,
const int kernel_h,
const int kernel_w,
371 const int pad_h,
const int pad_w,
372 const int stride_h,
const int stride_w,
373 const int dilation_h,
const int dilation_w,
376 std::fill(data_im, data_im +
height *
width * channels, 0.);
380 const int output_h = (
height + 2 * pad_h -
381 (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
382 const int output_w = (
width + 2 * pad_w -
383 (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
385 for (
int channel = channels; channel--; data_im += channel_size) {
386 for (
int kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
387 for (
int kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
388 int input_row = -pad_h + kernel_row * dilation_h;
389 for (
int output_rows = output_h; output_rows; output_rows--) {
391 data_col += output_w;
393 int input_col = -pad_w + kernel_col * dilation_w;
394 for (
int output_col = output_w; output_col; output_col--) {
401 data_im[input_row *
width + input_col] += *data_col;
404 input_col += stride_w;
407 input_row += stride_h;
420extern "C" void sgemm_(
const char * transa,
const char * transb,
const int *
m,
const int *
n,
const int * k,
421 const float * alpha,
const float * A,
const int * lda,
const float * B,
const int * ldb,
422 const float * beta,
float * C,
const int * ldc);
444 throw std::runtime_error(
"TMVA RTensor Concatenate - tensors have different memory layout");
445 auto & shape1 =
t1.GetShape();
447 if (
t1.GetSize()/shape1[axis] != t2.
GetSize()/shape2[axis])
448 throw std::runtime_error(
"TMVA RTensor Concatenate - tensors have incompatible shapes");
449 std::vector<size_t> outShape = shape1;
450 outShape[axis] = shape1[axis] + shape2[axis];
452 if (
t1.GetMemoryLayout() == TMVA::Experimental::MemoryLayout::ColumnMajor) {
453 throw std::runtime_error(
"TMVA RTensor Concatenate is not yet supported for column major tensors");
456 auto & stride1 =
t1.GetStrides();
460 size_t s1 = (axis > 0) ? stride1[axis-1] :
t1.GetSize();
461 size_t s2 = (axis > 0) ? stride2[axis-1] : t2.
GetSize();
462 size_t sout = (axis > 0) ? outStride[axis-1] : tout.
GetSize();
463 size_t nb =
t1.GetSize()/
s1;
464 for (
size_t i = 0; i < nb; i++) {
465 std::copy(
t1.GetData() + i*
s1,
t1.GetData() + (i+1)*
s1, tout.
GetData() + i * sout );
480 throw std::runtime_error(
"GNN_Data Concatenate: data1 and data2 have different net structures");
491 std::copy(
data.node_data.GetData(),
data.node_data.GetData()+
data.node_data.GetSize(), out.node_data.GetData());
492 std::copy(
data.edge_data.GetData(),
data.edge_data.GetData()+
data.edge_data.GetSize(), out.edge_data.GetData());
493 std::copy(
data.global_data.GetData(),
data.global_data.GetData()+
data.global_data.GetSize(), out.global_data.GetData());
494 out.receivers =
data.receivers;
495 out.senders =
data.senders;
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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
const Shape_t & GetStrides() const
std::size_t GetSize() const
const Shape_t & GetShape() const
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)
ETensorType GetTemplatedType(T)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::string ConvertTypeToString(ETensorType type)
ETensorType ConvertStringToType(std::string type)
TMVA::Experimental::RTensor< T > Concatenate(TMVA::Experimental::RTensor< T > &t1, TMVA::Experimental::RTensor< T > &t2, int axis=0)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
GNN_Data Copy(const GNN_Data &data)
create variable transformations
RTensor< float > global_data
RTensor< float > edge_data
std::vector< int > receivers
std::vector< int > senders
RTensor< float > node_data
std::shared_ptr< void > fData
std::vector< std::size_t > fShape
void CastSharedToPersistent()
void CastPersistentToShared()
std::vector< size_t > shape