17#ifndef TMVA_DNN_ARCHITECTURES_CPU_CPUTENSOR
18#define TMVA_DNN_ARCHITECTURES_CPU_CPUTENSOR
49template <
typename AFloat>
72 if (shape.size() == 0)
86 const auto size = shape.size();
89 for (std::size_t i = 0; i <
size; i++) {
91 strides[
size - 1 - i] = 1;
93 strides[
size - 1 - i] = strides[
size - 1 - i + 1] * shape[
size - 1 - i + 1];
97 for (std::size_t i = 0; i <
size; i++) {
101 strides[i] = strides[i - 1] * shape[i - 1];
130 :
TCpuTensor(std::make_shared<
TCpuBuffer<AFloat>>(bsize * depth * hw), {depth, hw, bsize}, memlayout)
157 for (
size_t i = 0; i < this->
GetSize(); ++i) container[i] =
data[i];
181 shape.insert(shape.end(),dim-2, 1);
183 shape.insert(shape.begin(), dim - 2, 1);
219 std::stringstream ss;
220 ss <<
"Cannot reshape tensor with size " <<
fSize <<
" into shape { ";
221 for (std::size_t i = 0; i < shape.size(); i++) {
222 if (i != shape.size() - 1) {
223 ss << shape[i] <<
", ";
225 ss << shape[i] <<
" }.";
228 throw std::runtime_error(ss.str());
261 if (shape.size() == 2)
return 1;
268 if (shape.size() == 2)
return shape[0];
270 if (shape.size() >= 4)
return shape[2] ;
277 if (shape.size() == 2)
return shape[1];
279 if (shape.size() >= 4)
return shape[3] ;
296 [[maybe_unused]]
size_t ndims = 0;
299 for (
auto& shape_i : shape){
304 assert(ndims <= 2 && shape.size() > 1);
312 x.ReshapeInplace(shape);
323 :
Shape_t(shape.begin(), shape.end() - 1);
326 size_t offset = i * buffsize;
336 return At(i).GetMatrix();
343 for (
size_t i = 0; i < this->
GetSize(); ++i)
351 assert(shape.size() == 2);
361 assert(shape.size() == 3);
364 ? (*(this->
GetContainer()))[i * shape[1] * shape[2] + j * shape[2] + k]
365 : (*(this->
GetContainer()))[i * shape[0] * shape[1] + k * shape[0] + j];
372 assert(shape.size() == 2);
374 : (this->
GetData())[j * shape[0] + i];
380 assert(shape.size() == 3);
383 ? (this->
GetData())[i * shape[1] * shape[2] + j * shape[2] + k]
384 : (this->
GetData())[i * shape[0] * shape[1] + k * shape[0] + j];
389 template <
typename Function_t>
390 void Map(Function_t &
f);
394 template <
typename Function_t>
403 for (
size_t i = 0; i < this->
GetSize(); i++)
404 std::cout << (this->
GetData())[i] <<
" ";
405 std::cout << std::endl;
410 std::cout <<
name <<
" shape : { ";
412 for (
size_t i = 0; i < shape.size() - 1; ++i)
413 std::cout << shape[i] <<
" , ";
414 std::cout << shape.back() <<
" } "
415 <<
" Layout : " << memlayout << std::endl;
420template <
typename AFloat>
421template <
typename Function_t>
424 AFloat *
data = GetRawDataPointer();
425 size_t nelements = GetNoElements();
428 auto ff = [
data, &nsteps, &nelements, &
f](
UInt_t workerID) {
429 size_t jMax = std::min(workerID + nsteps, nelements);
430 for (
size_t j = workerID; j < jMax; ++j) {
436 if (nsteps < nelements) {
449template <
typename AFloat>
450template <
typename Function_t>
453 AFloat *dataB = GetRawDataPointer();
456 size_t nelements = GetNoElements();
460 auto ff = [&dataB, &dataA, &nsteps, &nelements, &
f](
UInt_t workerID) {
461 size_t jMax = std::min(workerID + nsteps, nelements);
462 for (
size_t j = workerID; j < jMax; ++j) {
463 dataB[j] =
f(dataA[j]);
467 if (nsteps < nelements) {
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
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 height
A pseudo container class which is a generator of indices.
Executor & GetThreadExecutor()
Get executor class for multi-thread usage In case when MT is not enabled will return a serial executo...
static Config & Instance()
static function: returns TMVA instance
static size_t GetNWorkItems(size_t nelements)
size_t GetBufferUseCount() const
const Shape_t & GetStrides() const
AFloat operator()(size_t i, size_t j, size_t k) const
TCpuTensor(size_t n, size_t m, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from n m
TCpuTensor(size_t bsize, size_t depth, size_t height, size_t width, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from batch size, depth, height, width
AFloat * GetRawDataPointer()
Return raw pointer to the elements stored contiguously in column-major order.
size_t GetNoElements() const
void Map(Function_t &f)
Map the given function over the matrix elements.
std::size_t GetSize() const
const TCpuBuffer< AFloat > & GetDeviceBuffer() const
TCpuTensor(std::shared_ptr< TCpuBuffer< AFloat > > container, Shape_t shape, MemoryLayout layout)
Construct a tensor sharing the given buffer.
MemoryLayout fLayout
Memory layout of the tensor.
TCpuTensor(size_t bsize, size_t depth, size_t hw, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from batch size, depth, height*width
TCpuMatrix< AFloat > operator[](size_t i) const
const AFloat * GetRawDataPointer() const
std::shared_ptr< TCpuBuffer< AFloat > > GetContainer()
TCpuBuffer< AFloat > & GetDeviceBuffer()
AFloat & operator()(size_t i, size_t j, size_t k)
const AFloat * GetData() const
std::shared_ptr< TCpuBuffer< AFloat > > fContainer
Buffer owning the data.
const Shape_t & GetShape() const
std::size_t fSize
Total number of elements.
TCpuTensor(const TCpuBuffer< AFloat > &buffer, Shape_t shape, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from a TCpuBuffer and a shape
AFloat * fData
Pointer to the first element.
void MapFrom(Function_t &f, const TCpuTensor< AFloat > &A)
Same as maps but takes the input values from the tensor A and writes the results in this tensor.
AFloat operator()(size_t i, size_t j) const
Shape_t fStrides
Strides of the tensor.
TCpuTensor(const TCpuMatrix< AFloat > &matrix, size_t dim=3, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from a TCpuMatrix.
TCpuMatrix< AFloat > GetMatrix() const
TCpuTensor< AFloat > At(size_t i) const
TCpuTensor(Shape_t shape, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from a shape.
size_t GetFirstSize() const
void PrintShape(const char *name="Tensor") const
std::vector< std::size_t > Shape_t
static Shape_t ComputeStridesFromShape(const Shape_t &shape, MemoryLayout layout)
Compute strides from a shape vector.
AFloat & operator()(size_t i, size_t j)
void ReshapeInplace(const Shape_t &shape)
Reshape tensor in place.
Shape_t fShape
Shape of the tensor.
const std::shared_ptr< TCpuBuffer< AFloat > > GetContainer() const
TCpuTensor< AFloat > At(size_t i)
friend class TCpuMatrix< AFloat >
MemoryLayout GetLayout() const
void Print(const char *name="Tensor") const
static std::size_t GetSizeFromShape(const Shape_t &shape)
Compute the total number of elements from a shape vector.
TCpuTensor< AFloat > Reshape(Shape_t shape) const
MemoryLayout GetMemoryLayout() const
TCpuTensor(AFloat *data, const Shape_t &shape, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
void Foreach(Function func, unsigned int nTimes, unsigned nChunks=0)
wrap TExecutor::Foreach
MemoryLayout
Memory layout type (row- or column-major storage of the tensor elements)
create variable transformations