Apache Singa
A General Distributed Deep Learning Library
|
A Tensor instance is a multi-dimensional array resident on a Device (default device is the host CPU). More...
#include <tensor.h>
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. | |
Block * | block () const |
For functions in xx_math.cc to access the block. More... | |
std::shared_ptr< Device > | device () 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... | |
Tensor & | operator= (const Tensor &in) |
Copy assignment. | |
Tensor & | operator= (Tensor &&in) |
Move assignment. | |
Tensor & | operator+= (const Tensor &in) |
Tensor & | operator-= (const Tensor &in) |
Tensor & | operator*= (const Tensor &in) |
Tensor & | operator/= (const Tensor &in) |
template<typename SType > | |
Tensor & | operator+= (const SType x) |
SType is a scalar type. | |
template<typename SType > | |
Tensor & | operator-= (const SType x) |
SType is a scalar type. | |
template<typename SType > | |
Tensor & | operator*= (const SType x) |
SType is a scalar type. | |
template<typename SType > | |
Tensor & | operator/= (const SType x) |
SType is a scalar type. | |
Tensor & | Reshape (const Shape &shape) |
change the shape (and stride); the block may be reallocated. | |
Tensor & | Resize (const Shape &shape) |
Resize the memory and return itself. | |
Tensor & | T () |
Matrix transpose. Valid only if shape.size() == 2. | |
Tensor & | Transpose () |
Reverse the shape vector. | |
Tensor & | Transpose (const vector< size_t > &axes) |
Change the axes. | |
Tensor & | Broadcast (const Shape &shape) |
Return a view of the input tensor whose shape is broadcasted to be compitable with the given shape. | |
Tensor & | ResetLike (const Tensor &t) |
Reset the shape, device, and data type as given tensor. More... | |
Tensor & | AsType (const DataType type) |
Reset the data type, it would reallocate block if type changes. | |
Tensor & | ToDevice (std::shared_ptr< Device > dev) |
Reset the device. More... | |
Tensor & | ToHost () |
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< Device > | device_ = nullptr |
Block * | block_ = nullptr |
Note: block_ is allocated in lazy manner to avoid frequent malloc/free. More... | |
Shape | shape_ = {} |
vector< int > | stride_ = {} |
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.
|
inline |
For functions in xx_math.cc to access the block.
Users should not operate against Block directly. block_ is allocated in constructors.
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.
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!
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).
|
inline |
used for swig code to convert Tensor into numpy array.
It gets data into 'value'
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.
Reset the device.
If the target device is a diff device, then do deep data copy.
|
protected |