Apache Singa
A General Distributed Deep Learning Library
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
singa::Tensor Class Reference

A Tensor instance is a multi-dimensional array resident on a Device (default device is the host CPU). More...

#include <tensor.h>

Collaboration diagram for singa::Tensor:
Collaboration graph
[legend]

Public Member Functions

 Tensor (const Shape &shape, DataType dtype=kFloat32)
 Constructor using default device.
 
 Tensor (const Shape &shape, std::shared_ptr< Device > dev, DataType dtype=kFloat32)
 Constructor with shape, device and data type.
 
 Tensor (const Tensor &from)
 Copy constructor. No deep copy.
 
 Tensor (Tensor &&from)
 Move constructor. No deep copy.
 
Blockblock () const
 For functions in xx_math.cc to access the block. More...
 
std::shared_ptr< Devicedevice () const
 
template<typename SType >
const SType * data () const
 Return immutable Tensor values with given type.
 
const DataType data_type () const
 data type, including kFloat16, kFloat32, kInt
 
const Shape & shape () const
 
const size_t shape (const size_t idx) const
 
size_t nDim () const
 
bool empty () const
 
bool transpose () const
 The stride should decrease except dim with stride=0 due to broadcasting.
 
const vector< int > & stride () const
 
bool initailized () const
 Return true if the content of the tensor is initialized.
 
size_t Size () const
 Return number of total elements.
 
size_t MemSize () const
 Return memory size (i.e., Bytes)
 
template<typename SType >
void GetValue (SType *value, const size_t num)
 used for swig code to convert Tensor into numpy array. More...
 
void ToProto (singa::TensorProto *proto) const
 Serialize data, shape and transpose to protobuf object.
 
float L1 () const
 Return average L1 norm.
 
float L2 () const
 Return average L2 norm.
 
template<typename SType >
void SetValue (const SType x)
 Set each element of the tensor to be x.
 
template<typename SType >
void CopyDataFromHostPtr (const SType *src, const size_t num, const size_t offset=0)
 For init the tensor values, copy 'num' elements from 'src' to the internal memory with 'offset' (elements). More...
 
void CopyData (const Tensor &other)
 Copy data from another Tensor which may be on a diff device. More...
 
void FromProto (const singa::TensorProto &proto)
 Deserialize data, shape and transpose from protobuf object.
 
void RepeatData (const vector< size_t > &repeats, int axis, int total_repeats, const Tensor &other)
 TODO(wangwei) merge RepeatData into Repeat?
 
Tensor Repeat (const vector< size_t > &repeats, int axis, std::shared_ptr< Device > device=nullptr)
 
Tensor Clone (std::shared_ptr< Device > device=nullptr) const
 return an exactly the same Tensor with data been deep copied to the given device. More...
 
Tensoroperator= (const Tensor &in)
 Copy assignment.
 
Tensoroperator= (Tensor &&in)
 Move assignment.
 
Tensoroperator+= (const Tensor &in)
 
Tensoroperator-= (const Tensor &in)
 
Tensoroperator*= (const Tensor &in)
 
Tensoroperator/= (const Tensor &in)
 
template<typename SType >
Tensoroperator+= (const SType x)
 SType is a scalar type.
 
template<typename SType >
Tensoroperator-= (const SType x)
 SType is a scalar type.
 
template<typename SType >
Tensoroperator*= (const SType x)
 SType is a scalar type.
 
template<typename SType >
Tensoroperator/= (const SType x)
 SType is a scalar type.
 
TensorReshape (const Shape &shape)
 change the shape (and stride); the block may be reallocated.
 
TensorResize (const Shape &shape)
 Resize the memory and return itself.
 
TensorT ()
 Matrix transpose. Valid only if shape.size() == 2.
 
TensorTranspose ()
 Reverse the shape vector.
 
TensorTranspose (const vector< size_t > &axes)
 Change the axes.
 
TensorBroadcast (const Shape &shape)
 Return a view of the input tensor whose shape is broadcasted to be compitable with the given shape.
 
TensorResetLike (const Tensor &t)
 Reset the shape, device, and data type as given tensor. More...
 
TensorAsType (const DataType type)
 Reset the data type, it would reallocate block if type changes.
 
TensorToDevice (std::shared_ptr< Device > dev)
 Reset the device. More...
 
TensorToHost ()
 Equivalent to ToDevice(host_dev).
 

Protected Member Functions

void generate_stride ()
 
void set_strides (const vector< int > new_strides)
 

Protected Attributes

DataType data_type_ = kFloat32
 
std::shared_ptr< Devicedevice_ = nullptr
 
Blockblock_ = nullptr
 Note: block_ is allocated in lazy manner to avoid frequent malloc/free. More...
 
Shape shape_ = {}
 
vector< int > stride_ = {}
 

Detailed Description

A Tensor instance is a multi-dimensional array resident on a Device (default device is the host CPU).

The internal data is allocated in lazy manner. Linear algebra, neural net and random operations are provided against Tensor. For all operations, if the result tensor is passed as an argument, then it must be set up correctly (shape, device). Otherwise, runtime error like SegmentFault would happen. Simple type/device check would be conducted.

Member Function Documentation

◆ block()

Block* singa::Tensor::block ( ) const
inline

For functions in xx_math.cc to access the block.

Users should not operate against Block directly. block_ is allocated in constructors.

◆ Clone()

Tensor singa::Tensor::Clone ( std::shared_ptr< Device device = nullptr) const

return an exactly the same Tensor with data been deep copied to the given device.

If 'device' is nullptr, then clone it one the current device.

◆ CopyData()

void singa::Tensor::CopyData ( const Tensor other)

Copy data from another Tensor which may be on a diff device.

Meta data would not be copied!

◆ CopyDataFromHostPtr()

template<typename SType >
void singa::Tensor::CopyDataFromHostPtr ( const SType *  src,
const size_t  num,
const size_t  offset = 0 
)

For init the tensor values, copy 'num' elements from 'src' to the internal memory with 'offset' (elements).

◆ GetValue()

template<typename SType >
void singa::Tensor::GetValue ( SType *  value,
const size_t  num 
)
inline

used for swig code to convert Tensor into numpy array.

It gets data into 'value'

◆ ResetLike()

Tensor& singa::Tensor::ResetLike ( const Tensor t)

Reset the shape, device, and data type as given tensor.

If block size changes, then reallocate a new block. The previous block would be deleted.

◆ ToDevice()

Tensor& singa::Tensor::ToDevice ( std::shared_ptr< Device dev)

Reset the device.

If the target device is a diff device, then do deep data copy.

Member Data Documentation

◆ block_

Block* singa::Tensor::block_ = nullptr
protected

Note: block_ is allocated in lazy manner to avoid frequent malloc/free.

If you want to get an allocated Block, use block() instead of block_.


The documentation for this class was generated from the following file: