17#ifndef TMVA_DNN_ARCHITECTURES_CPU_CPUTENSOR
18#define TMVA_DNN_ARCHITECTURES_CPU_CPUTENSOR
40template <
typename AFloat>
66 if (memlayout == MemoryLayout::RowMajor)
74 {depth, height,
width, bsize}, memlayout)
76 if (memlayout == MemoryLayout::RowMajor)
93 for (
size_t i = 0; i < this->
GetSize(); ++i) container[i] = data[i];
116 if (this->
GetLayout() == MemoryLayout::ColumnMajor) {
117 shape.insert(shape.end(),dim-2, 1);
119 shape.insert(shape.begin(), dim - 2, 1);
159 return (this->
GetMemoryLayout() == MemoryLayout::ColumnMajor) ? shape.back() : shape.front();
165 if (shape.size() == 2)
return 1;
166 return (this->
GetMemoryLayout() == MemoryLayout::ColumnMajor) ? shape.front() : shape[1];
172 if (shape.size() == 2)
return shape[0];
173 if (shape.size() == 3)
return (this->
GetMemoryLayout() == MemoryLayout::ColumnMajor) ? shape[0] : shape[1] ;
174 if (shape.size() >= 4)
return shape[2] ;
181 if (shape.size() == 2)
return shape[1];
182 if (shape.size() == 3)
return (this->
GetMemoryLayout() == MemoryLayout::ColumnMajor) ? shape[1] : shape[2] ;
183 if (shape.size() >= 4)
return shape[3] ;
203 for (
auto& shape_i : shape){
208 assert(ndims <= 2 && shape.size() > 1);
216 x.ReshapeInplace(shape);
226 Shape_t sliced_shape = (layout == MemoryLayout::RowMajor) ?
Shape_t(shape.begin() + 1, shape.end())
227 :
Shape_t(shape.begin(), shape.end() - 1);
229 size_t buffsize = (layout == MemoryLayout::RowMajor) ? this->
GetStrides().front() : this->
GetStrides().back();
230 size_t offset = i * buffsize;
241 for (
size_t i = 0; i < this->
GetSize(); ++i)
249 assert(shape.size() == 2);
259 assert(shape.size() == 3);
262 ? (*(this->
GetContainer()))[i * shape[1] * shape[2] + j * shape[2] + k]
263 : (*(this->
GetContainer()))[i * shape[0] * shape[1] + k * shape[0] + j];
270 assert(shape.size() == 2);
272 : (this->
GetData())[j * shape[0] + i];
278 assert(shape.size() == 3);
281 ? (this->
GetData())[i * shape[1] * shape[2] + j * shape[2] + k]
282 : (this->
GetData())[i * shape[0] * shape[1] + k * shape[0] + j];
287 template <
typename Function_t>
288 void Map(Function_t &
f);
292 template <
typename Function_t>
301 for (
size_t i = 0; i < this->
GetSize(); i++)
302 std::cout << (this->
GetData())[i] <<
" ";
303 std::cout << std::endl;
307 std::string memlayout = (
GetLayout() == MemoryLayout::RowMajor) ?
"RowMajor" :
"ColMajor";
308 std::cout <<
name <<
" shape : { ";
310 for (
size_t i = 0; i < shape.size() - 1; ++i)
311 std::cout << shape[i] <<
" , ";
312 std::cout << shape.back() <<
" } "
313 <<
" Layout : " << memlayout << std::endl;
318template <
typename AFloat>
319template <
typename Function_t>
322 AFloat *data = GetRawDataPointer();
323 size_t nelements = GetNoElements();
326 auto ff = [data, &nsteps, &nelements, &
f](
UInt_t workerID) {
327 size_t jMax = std::min(workerID + nsteps, nelements);
328 for (
size_t j = workerID; j < jMax; ++j) {
329 data[j] =
f(data[j]);
334 if (nsteps < nelements) {
347template <
typename AFloat>
348template <
typename Function_t>
351 AFloat *dataB = GetRawDataPointer();
352 const AFloat *dataA =
A.GetRawDataPointer();
354 size_t nelements = GetNoElements();
358 auto ff = [&dataB, &dataA, &nsteps, &nelements, &
f](
UInt_t workerID) {
359 size_t jMax = std::min(workerID + nsteps, nelements);
360 for (
size_t j = workerID; j < jMax; ++j) {
361 dataB[j] =
f(dataA[j]);
365 if (nsteps < nelements) {
include TDocParser_001 C image html pict1_TDocParser_001 png width
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
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.
const TCpuBuffer< AFloat > & GetDeviceBuffer() const
TCpuTensor(size_t bsize, size_t depth, size_t hw, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from batch size, depth, height*width
const AFloat * GetRawDataPointer() const
TCpuBuffer< AFloat > & GetDeviceBuffer()
AFloat & operator()(size_t i, size_t j, size_t k)
TCpuTensor(const TCpuBuffer< AFloat > &buffer, Shape_t shape, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
constructors from a TCpuBuffer and a shape
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
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
AFloat & operator()(size_t i, size_t j)
TCpuTensor< AFloat > At(size_t i)
friend class TCpuMatrix< AFloat >
MemoryLayout GetLayout() const
void Print(const char *name="Tensor") const
TCpuTensor< AFloat > Reshape(Shape_t shape) const
typename TMVA::Experimental::RTensor< AFloat >::Shape_t Shape_t
TCpuTensor(AFloat *data, const Shape_t &shape, MemoryLayout memlayout=MemoryLayout::ColumnMajor)
void Foreach(Function func, unsigned int nTimes, unsigned nChunks=0)
wrap TExecutor::Foreach
RTensor is a container with contiguous memory and shape information.
void ReshapeInplace(const Shape_t &shape)
Reshape tensor in place.
MemoryLayout GetMemoryLayout() const
std::shared_ptr< Container_t > GetContainer()
const Shape_t & GetStrides() const
std::size_t GetSize() const
RTensor(Value_t *data, Shape_t shape, MemoryLayout layout=MemoryLayout::RowMajor)
Construct a tensor as view on data.
const Shape_t & GetShape() const
std::vector< std::size_t > Shape_t
int GetBuffer(PyObject *pyobject, char tc, int size, void *&buf, Bool_t check=kTRUE)
Retrieve a linear buffer pointer from the given pyobject.
std::size_t GetSizeFromShape(const T &shape)
Get size of tensor from shape vector.
MemoryLayout
Memory layout type (copy from RTensor.hxx)
create variable transformations