Tensor is the basic class in Caffe2 that stores a contiguous memory with its shape information. More...
#include <tensor.h>
Public Member Functions | |
Tensor () | |
Initializes an empty tensor. | |
Tensor (const vector< TIndex > &dims) | |
Creates a tensor of the given dimension. More... | |
Tensor (const vector< int > &dims) | |
template<class SrcContext , class ContextForCopy > | |
Tensor (const Tensor< SrcContext > &src, ContextForCopy *context) | |
Creates a tensor from a source tensor, copying over the content. More... | |
template<class SrcContext > | |
Tensor (const Tensor< SrcContext > &src) | |
Creates a tensor from a source tensor, copying over the content. More... | |
template<typename T > | |
Tensor (const vector< TIndex > &dims, const vector< T > &values, Context *context) | |
Creates a tensor, and fills its contents with the given values. | |
template<typename T , typename = typename std::enable_if<std::is_scalar<T>::value>::type> | |
Tensor (const T &value, Context *context) | |
Creates a scalar tensor, and fills its content with the given value. | |
template<class SrcContext , class ContextForCopy > | |
void | CopyFrom (const Tensor< SrcContext > &src, ContextForCopy *context) |
Copies the data from a source tensor, with a contex provided to carry out the underlying memcpy operation. | |
template<class SrcContext > | |
void | CopyFrom (const Tensor< SrcContext > &src) |
Copies the data from a source tensor. More... | |
template<class ContextForCopy > | |
void | Extend (TIndex num, float growthPct, ContextForCopy *context) |
Extends the outer-most dimension of this tensor by num elements, preserving the existing data. More... | |
template<class T , class ContextForCopy > | |
void | Reserve (const std::vector< T > &newCapacity, ContextForCopy *context) |
void | Shrink (TIndex outer_dim) |
Shrinks the outer-most dimension to given size, keeping the data. More... | |
template<typename... Ts> | |
void | Resize (Ts... dim_source) |
Resizes a tensor. More... | |
template<class OtherContext > | |
void | ResizeLike (const Tensor< OtherContext > &src_tensor) |
Resize the tensor like the source tensor. More... | |
void | Reshape (const vector< TIndex > &dims) |
Resizes the tensor without touching underlying storage. More... | |
void | Reshape (const vector< int > &dims) |
string | DebugString () const |
A utility function to print the debug string for the tensor. More... | |
void | ShareData (const Tensor &src) |
Shares the data with another tensor. More... | |
template<typename T > | |
void | ShareExternalPointer (T *src, size_t capacity=0) |
Shares the data with an externally managed pointer. More... | |
template<typename T , typename Deleter > | |
void | ShareExternalPointer (T *src, size_t capacity, Deleter &&d) |
Shares the data with an externally managed pointer. More... | |
void | ShareExternalPointer (void *src, const TypeMeta &meta, size_t capacity=0) |
template<class Deleter > | |
void | ShareExternalPointer (void *src, const TypeMeta &meta, size_t capacity, Deleter &&d) |
bool | shares_data () |
const void * | raw_data () const |
Returns a const raw void* pointer of the underlying storage. More... | |
template<typename T > | |
const T * | data () const |
Returns a typed pointer of the underlying storage. More... | |
void * | raw_mutable_data (const TypeMeta &meta) |
Returns a mutable raw pointer of the underlying storage. More... | |
void * | raw_mutable_data () |
Returns a mutable raw pointer of the underlying storage. More... | |
template<typename T > | |
T * | mutable_data () |
Returns a typed pointer of the underlying storage. More... | |
int | ndim () const |
Returns the number of dimensions of the data. | |
TIndex | size () const |
Returns the size (i.e. More... | |
size_t | itemsize () const |
Return the number of bytes each item takes in the tensor. | |
size_t | nbytes () const |
Returns the total number of bytes of the storage. More... | |
size_t | capacity_nbytes () const |
const vector< TIndex > & | dims () const |
Returns the dimensions of the tensor as a vector. | |
TIndex | size_from_dim (int k) const |
TIndex | size_to_dim (int k) const |
int | canonical_axis_index (int axis_index) const |
Returns the 'canonical' version of a (usually) user-specified axis, allowing for negative indexing (e.g., -1 for the last axis). More... | |
template<typename T > | |
bool | IsType () const |
Checks if the tensor content is of the given data type. | |
const TypeMeta & | meta () const |
Returns the TypeMeta object associated with the current data type. | |
int | dim32 (const int i) const |
Returns the i-th dimension of the tensor in int. More... | |
TIndex | dim (const int i) const |
Returns the i-th dimension of the tensor. More... | |
Protected Attributes | |
vector< TIndex > | dims_ |
TIndex | size_ = -1 |
TypeMeta | meta_ |
std::shared_ptr< void > | data_ |
bool | shares_data_ = false |
size_t | capacity_ = 0 |
Tensor is the basic class in Caffe2 that stores a contiguous memory with its shape information.
The Tensor class is essentially a wrapper around a device-specific memory (the device is specified by the Context template argument), and deals with the allocation and de-allocation of such memory. We make a simplified assumption that the memory is always contiguous.
|
inlineexplicit |
Creates a tensor of the given dimension.
Note that the actual data allocation is not going to be carried out until the first time mutable_data() is called.
|
inline |
Creates a tensor from a source tensor, copying over the content.
Note that the source tensor can be from a different device context. The second argument provides a device context object (either Context or SrcContext) that will be responsible for copying the underlying data. If you do not wish to pass in a Context object, an equivalent constructor function exists that will create an implicit context object for copy, but be noted that this will cause a potential performance hit.
|
inline |
|
inline |
Returns the 'canonical' version of a (usually) user-specified axis, allowing for negative indexing (e.g., -1 for the last axis).
|
inline |
|
inline |
Returns a typed pointer of the underlying storage.
mutable_data() or raw_mutable_data() must have been called prior to this function call, and the data type must be of the correct type. If you want to get a void* pointer instead, use raw_data().
|
inline |
|
inline |
|
inline |
|
inline |
Extends the outer-most dimension of this tensor by num elements, preserving the existing data.
The underlying data may be reallocated in order to accommodate the new elements, in which case this tensors' capacity is grown at a factor of growthPct. This ensures that Extend runs on an amortized O(1) time complexity.
|
inline |
Returns the total number of bytes of the storage.
This is equivalent to calling size() * itemsize().
|
inline |
Returns a const raw void* pointer of the underlying storage.
mutable_data() or raw_mutable_data() must have been called prior to this function call.
|
inline |
Returns a mutable raw pointer of the underlying storage.
Since we will need to know the type of the data for allocation, a TypeMeta object is passed in to specify the necessary information. This is conceptually equivalent of calling mutable_data<T>() where the TypeMeta parameter meta is derived from the type T. This function differs from mutable_data<T>() in the sense that the type T can be specified during runtime via the TypeMeta object.
If the existing data does not match the desired type, it will be deleted and a new storage will be created.
|
inline |
Returns a mutable raw pointer of the underlying storage.
This can only be used when you know for sure that the underlying storage of the tensor is already created via an earlier raw_mutable_data(meta) call or a mutable_data<T>() call.
If the existing data does not match the desired type, it will be deleted and a new storage will be created.
|
inline |
|
inline |
Resizes a tensor.
Resize takes in a vector of ints specifying the dimensions of the tensor. You can pass in an empty vector to specify that it is a scalar (i.e. containing one single item).
The underlying storage may be deleted after calling Resize: if the new shape leads to a different number of items in the tensor, the old memory is deleted and new memory will be allocated next time you call mutable_data(). However, if the shape is different but the total number of items is the same, the underlying storage is kept.
|
inline |
|
inline |
Shares the data with another tensor.
To share data between two tensors, the sizes of the two tensors must be equal already. The reason we do not implicitly do a Resize to make the two tensors have the same shape is that we want to allow tensors of different shapes but the same number of items to still be able to share data. This allows one to e.g. have a n-dimensional Tensor and a flattened version sharing the same underlying storage.
The source tensor should already have its data allocated.
|
inline |
Shares the data with an externally managed pointer.
This is similar to ShareData() but the tensor does not take over ownership of the pointer, so the caller can explicitly manage the memory storage. One needs to make sure that the external memory is deallocated only after the tensor finishes using it.
|
inline |
|
inline |
|
inline |