1#ifndef TMVA_SOFIE_SOFIE_COMMON
2#define TMVA_SOFIE_SOFIE_COMMON
19namespace Experimental{
25 UNDEFINED = 0,
FLOAT = 1,
UNINT8 = 2,
INT8 = 3,
UINT16 = 4,
INT16 = 5,
INT32 = 6,
INT64 = 7,
STRING = 8,
BOOL = 9,
105 throw std::runtime_error(
"TMVA::SOFIE doesn't yet supports serialising data-type " +
132bool AreSameShape(
const std::vector<size_t>&,
const std::vector<size_t>&);
140std::string
Clean_name(std::string input_tensor_name);
144 size_t size = targetShape.size();
145 if (targetShape[1] != channel) {
146 std::stringstream ss;
147 ss <<
"TMVA::SOFIE - Error broadcasting Conv Bias of shape {";
148 ss << std::to_string(channel);
152 std::runtime_error(ss.str());
156 T* newData =
new T[targetLength];
158 if (targetLength == channel) {
159 std::copy(
data,
data + channel, newData);
165 for (
size_t i = 2; i <
size; i++)
166 cStride *= targetShape[i];
169 for (
size_t i = 0; i < channel; i++) {
170 std::fill(newData + i * cStride, newData + (i + 1) * cStride,
data[i]);
173 size_t batch = targetShape[0];
174 size_t bStride = channel * cStride;
175 for (
size_t i = 1; i < batch; i++) {
176 std::copy(newData, newData + bStride, newData + i * bStride);
185T*
BroadcastTensor(
const T*
data,
const std::vector<size_t>& shape,
const std::vector<size_t>& targetShape) {
187 size_t size = shape.size();
192 T* broadcastedData =
new T[targetLength];
193 std::copy(
data,
data + curLength, broadcastedData);
197 std::vector<T> newData(targetLength);
199 for (
size_t idx = 0; idx <
size; idx++) {
200 size_t dim = shape[idx];
201 size_t targetDim = targetShape[idx];
202 if (dim == 1 && targetDim > 1) {
204 size_t newLength = curLength * targetDim;
206 size_t arrayLength = curLength / arrayNum;
208 if (arrayLength > 1) {
210 for (
size_t arrayIdx = 0; arrayIdx < arrayNum; arrayIdx++) {
211 for (
size_t targetIdx = 0; targetIdx < targetDim; targetIdx++) {
212 size_t offset = arrayIdx * arrayLength * targetDim + targetIdx * arrayLength;
213 std::copy(broadcastedData + arrayIdx * arrayLength,
214 broadcastedData + (arrayIdx + 1) * arrayLength,
215 newData.begin() +
offset);
220 for (
size_t arrayIdx = 0; arrayIdx < arrayNum; arrayIdx++) {
221 std::fill(newData.begin() + arrayIdx * targetDim,
222 newData.begin() + (arrayIdx + 1) * targetDim, broadcastedData[arrayIdx]);
226 curLength = newLength;
228 std::copy(newData.begin(), newData.begin() + newLength, broadcastedData);
231 arrayNum *= targetDim;
233 return broadcastedData;
240 if (shape.size() < targetShape.size()) {
241 size_t targetSize = targetShape.size();
242 std::vector<size_t> newShape(targetSize, 1);
243 size_t offset = targetSize - shape.size();
244 std::copy(shape.begin(), shape.end(), newShape.begin() +
offset);
245 return BroadcastTensor<T>(
data, newShape, targetShape);
247 return BroadcastTensor<T>(
data, shape, targetShape);
256 return static_cast<unsigned>(
a) <
static_cast<unsigned>(
b);
280void Im2col(
const T *data_im,
const int channels,
const int height,
const int width,
const int kernel_h,
281 const int kernel_w,
const int pad_h,
const int pad_w,
const int stride_h,
const int stride_w,
282 const int dilation_h,
const int dilation_w, T *data_col)
284 const int output_h = (
height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
285 const int output_w = (
width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
287 for (
int channel = channels; channel--; data_im += channel_size) {
288 for (
int kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
289 for (
int kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
290 int input_row = -pad_h + kernel_row * dilation_h;
291 for (
int output_rows = output_h; output_rows; output_rows--) {
293 for (
int output_cols = output_w; output_cols; output_cols--) {
297 int input_col = -pad_w + kernel_col * dilation_w;
298 for (
int output_col = output_w; output_col; output_col--) {
300 *(data_col++) = data_im[input_row *
width + input_col];
304 input_col += stride_w;
307 input_row += stride_h;
317 const int depth,
const int height,
const int width,
318 const int kernel_d,
const int kernel_h,
const int kernel_w,
319 const int pad_d,
const int pad_h,
const int pad_w,
320 const int stride_d,
const int stride_h,
const int stride_w,
321 const int dilation_d,
const int dilation_h,
const int dilation_w, T *data_col)
323 const int output_h = (
height + 2 * pad_h - (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
324 const int output_w = (
width + 2 * pad_w - (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
325 const int output_d = (depth + 2 * pad_d - (dilation_d * (kernel_d - 1) + 1)) / stride_d + 1;
328 for (
int channel = channels; channel--; data_im += channel_size) {
329 for (
int kernel_depth = 0; kernel_depth < kernel_d; kernel_depth++) {
330 for (
int kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
331 for (
int kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
332 int input_dep = -pad_d + kernel_depth * dilation_d;
333 for (
int output_dep = output_d; output_dep; output_dep--) {
335 for (
int output_rows = output_h; output_rows; output_rows--) {
336 for (
int output_cols = output_w; output_cols; output_cols--) {
341 int input_row = -pad_h + kernel_row * dilation_h;
342 for (
int output_rows = output_h; output_rows; output_rows--) {
344 for (
int output_cols = output_w; output_cols; output_cols--) {
348 int input_col = -pad_w + kernel_col * dilation_w;
349 for (
int output_col = output_w; output_col; output_col--) {
351 *(data_col++) = data_im[input_dep *
width *
height + input_row *
width + input_col];
355 input_col += stride_w;
358 input_row += stride_h;
361 input_dep += stride_d;
369template <
typename Dtype>
370void col2im(
const Dtype* data_col,
const int channels,
371 const int height,
const int width,
const int kernel_h,
const int kernel_w,
372 const int pad_h,
const int pad_w,
373 const int stride_h,
const int stride_w,
374 const int dilation_h,
const int dilation_w,
377 std::fill(data_im, data_im +
height *
width * channels, 0.);
381 const int output_h = (
height + 2 * pad_h -
382 (dilation_h * (kernel_h - 1) + 1)) / stride_h + 1;
383 const int output_w = (
width + 2 * pad_w -
384 (dilation_w * (kernel_w - 1) + 1)) / stride_w + 1;
386 for (
int channel = channels; channel--; data_im += channel_size) {
387 for (
int kernel_row = 0; kernel_row < kernel_h; kernel_row++) {
388 for (
int kernel_col = 0; kernel_col < kernel_w; kernel_col++) {
389 int input_row = -pad_h + kernel_row * dilation_h;
390 for (
int output_rows = output_h; output_rows; output_rows--) {
392 data_col += output_w;
394 int input_col = -pad_w + kernel_col * dilation_w;
395 for (
int output_col = output_w; output_col; output_col--) {
402 data_im[input_row *
width + input_col] += *data_col;
405 input_col += stride_w;
408 input_row += stride_h;
421extern "C" void sgemm_(
const char * transa,
const char * transb,
const int *
m,
const int *
n,
const int * k,
422 const float * alpha,
const float * A,
const int * lda,
const float * B,
const int * ldb,
423 const float * beta,
float * C,
const int * ldc);
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
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)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations
std::shared_ptr< void > fData
std::vector< std::size_t > fShape
void CastSharedToPersistent()
void CastPersistentToShared()
std::vector< size_t > shape