Caffe2 - C++ API
A deep learning, cross platform ML framework
Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes
caffe2::Tensor Class Referencefinal

Tensor class holds a shared pointer to the implementation TensorImpl, redirects API calls to TensorImpl; Copying of Tensor results in sharing the same underlying implementation object. More...

#include <tensor.h>

Public Member Functions

 Tensor (const Tensor &)=delete
 
Tensoroperator= (const Tensor &)=delete
 
 Tensor (Tensor &&)=default
 
Tensoroperator= (Tensor &&)=default
 
 operator bool () const
 
TensorImplunsafeGetTensorImpl () const
 
Tensor UnsafeSharedInstance () const
 
 Tensor (at::Device device)
 Creates a tensor of the given device type. More...
 
 Tensor (at::IntArrayRef dims, DeviceType type)
 Creates a tensor of the given dimension. More...
 
 Tensor (at::IntArrayRef dims, at::Device device)
 
 Tensor (const vector< int > &dims, DeviceType type)
 
 Tensor (const Tensor &src, DeviceType type)
 : Create a Tensor of at::DeviceType type and initialize it with src Tensor
 
 Tensor (const at::Tensor &tensor)
 Mutual conversion with at::Tensor. More...
 
 operator at::Tensor () const &
 
 operator at::Tensor ()&&
 
 Tensor (C10Tensor tensor)
 Mutual conversion with C10Tensor. More...
 
 operator C10Tensor () const &
 
 operator C10Tensor ()&&
 
bool is_same (const Tensor &other) const noexcept
 
Tensor Clone () const
 
Tensor Alias () const
 Clone self as a Tensor that share the same Storage, that is, both Tensors are views on the same Storage. More...
 
DeviceType GetDeviceType () const
 
at::Device GetDevice () const
 
void CopyFrom (const Tensor &src, bool async=false)
 Copies the data from a source tensor, with a contex provided to carry out the underlying memcpy operation. More...
 
void ExtendTo (int64_t num, float growthPct) const
 Extend the outer-most dimension of this tensor to dimension of num.
 
void Extend (int64_t num, float growthPct) const
 
void ShrinkTo (int64_t outer_dim) const
 Shrinks the outer-most dimension to given size, keeping the data. More...
 
template<class T >
void ReserveSpace (const T &outer_dim) const
 
template<typename... Ts>
void Resize (Ts...dim_source) const
 
void ResizeLike (const Tensor &src_tensor) const
 Resize the tensor like the source tensor. More...
 
void Reshape (const vector< int64_t > &dims) const
 
void Reshape (const vector< int > &dims) const
 
void FreeMemory () const
 
string DebugString () const
 A utility function to print the debug string for the tensor. More...
 
void ShareData (const Tensor &src) const
 
template<typename T >
void ShareExternalPointer (T *src, size_t capacity=0, MemoryDeleter d=nullptr) const
 Shares the data with an externally managed pointer. More...
 
template<typename T >
void ShareExternalPointer (at::DataPtr &&data_ptr, size_t capacity=0) const
 
void ShareExternalPointer (void *src, const TypeMeta &data_type, size_t capacity=0, MemoryDeleter d=nullptr) const
 
void ShareExternalPointer (at::DataPtr &&data_ptr, const TypeMeta &data_type, size_t capacity)
 
const c10::intrusive_ptr< TensorImpl, UndefinedTensorImpl > & getIntrusivePtr () const
 
bool defined () const
 
void * raw_data () const
 Returns a raw void* pointer of the underlying storage. More...
 
template<typename T >
Tdata () const
 
void * raw_mutable_data (const TypeMeta &meta) const
 
void * raw_mutable_data () const
 Returns a mutable raw pointer of the underlying storage. More...
 
template<typename T >
Tmutable_data () const
 
int dim () const
 Returns the number of dimensions of the data.
 
int ndim () const
 (To be deprecated) Returns the number of dimensions of the data.
 
int64_t size () const
 (To be deprecated) Returns the size (i.e. More...
 
int64_t numel () const
 Returns the number of items of the tensor.
 
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...
 
at::IntArrayRef sizes () const
 
int64_t size_from_dim (int k) const
 
int64_t size_to_dim (int k) const
 
int64_t size_between_dim (int k, int l) 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...
 
int64_t stride (int64_t dim) const
 
at::IntArrayRef strides () const
 
bool is_contiguous () const
 
template<typename T >
bool IsType () const
 Checks if the tensor content is of the given data type.
 
const TypeMetadtype () const
 Returns the TypeMeta object associated with the current data type.
 
const TypeMetameta () const
 (To be deprecated) 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...
 
int64_t size (const int i) const
 
int64_t dim (const int i) const
 
const Storagestorage ()
 
const Storagestorage () const
 
bool storage_initialized () const
 
bool dtype_initialized () const
 

Protected Types

using TensorImplPtr = c10::intrusive_ptr< TensorImpl, UndefinedTensorImpl >
 

Protected Member Functions

void enforce_invariants ()
 

Protected Attributes

TensorImplPtr impl_
 

Detailed Description

Tensor class holds a shared pointer to the implementation TensorImpl, redirects API calls to TensorImpl; Copying of Tensor results in sharing the same underlying implementation object.

NB: See TensorImpl for documentation on these methods.

Definition at line 25 of file tensor.h.

Constructor & Destructor Documentation

caffe2::Tensor::Tensor ( at::Device  device)
inlineexplicit

Creates a tensor of the given device type.

Note that the actual data allocation is not going to be carried out until you resize the tensor and then call mutable_data().

Definition at line 72 of file tensor.h.

caffe2::Tensor::Tensor ( at::IntArrayRef  dims,
DeviceType  type 
)
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.

Definition at line 86 of file tensor.h.

caffe2::Tensor::Tensor ( const at::Tensor tensor)
inlineexplicit

Mutual conversion with at::Tensor.

The tensor will share the same instance (data, strides, sizes, etc) but a different subset of APIs would be available

Definition at line 120 of file tensor.h.

caffe2::Tensor::Tensor ( C10Tensor  tensor)
inlineexplicit

Mutual conversion with C10Tensor.

The tensor will share the same instance (data, strides, sizes, etc) but a different subset of APIs would be available

Definition at line 139 of file tensor.h.

Member Function Documentation

Tensor caffe2::Tensor::Alias ( ) const
inline

Clone self as a Tensor that share the same Storage, that is, both Tensors are views on the same Storage.

If we change the sizes or strides of one Tensor, it does not affect the other Tensor that it shares Storage with. A similar yet different usage is Tensor x = y;, this will make x and y pointing to the same Tensor and resizing one of them will resize the other as well.

TODO: Deduplicate this with THTensor_(newWithTensor) (exposed in ATen as at::alias but not otherwise available)

Definition at line 174 of file tensor.h.

int caffe2::Tensor::canonical_axis_index ( int  axis_index) const
inline

Returns the 'canonical' version of a (usually) user-specified axis, allowing for negative indexing (e.g., -1 for the last axis).

Parameters
axis_indexthe axis index. If 0 <= index < dim(), return index. If -ndim <= index <= -1, return (dim() - (-index)), e.g., the last axis index (dim() - 1) if index == -1, the second to last if index == -2, etc. Dies on out of range index.

Definition at line 530 of file tensor.h.

void caffe2::Tensor::CopyFrom ( const Tensor src,
bool  async = false 
)
inline

Copies the data from a source tensor, with a contex provided to carry out the underlying memcpy operation.

This method respects caffe2_keep_on_shrink.

After CopyFrom, this function guarantees that the destination tensor will have the same initialization state and dtype as src. This function preserves the DeviceType of the source tensor (so, e.g., if you allocate a tensor on CPU and then CopyFrom a CUDA tensor, that will to a CUDA-to-CPU transfer).

'async' parameter triggers async copy for CUDA tensors

Definition at line 211 of file tensor.h.

string caffe2::Tensor::DebugString ( ) const
inline

A utility function to print the debug string for the tensor.

Note that this is very slow since it involves quite some string operations, so do not use it in your performance-critical code.

Definition at line 348 of file tensor.h.

int caffe2::Tensor::dim32 ( const int  i) const
inline

Returns the i-th dimension of the tensor in int.

This function returns an int value instead of int64_t, which depending on the typedef could be int64. If you want int64 dim values, make sure you call dim() instead.

Definition at line 576 of file tensor.h.

size_t caffe2::Tensor::nbytes ( ) const
inline

Returns the total number of bytes of the storage.

This is equivalent to calling size() * itemsize().

Definition at line 499 of file tensor.h.

void* caffe2::Tensor::raw_data ( ) const
inline

Returns a raw void* pointer of the underlying storage.

mutable_data() or raw_mutable_data() must have been called prior to this function call.

Definition at line 422 of file tensor.h.

void* caffe2::Tensor::raw_mutable_data ( ) const
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.

Definition at line 444 of file tensor.h.

void caffe2::Tensor::ResizeLike ( const Tensor src_tensor) const
inline

Resize the tensor like the source tensor.

Note that this is just a sugar wrapper that essentially calls Resize(src_tensor.dims()). This method respects caffe2_keep_on_shrink.

Definition at line 322 of file tensor.h.

template<typename T >
void caffe2::Tensor::ShareExternalPointer ( T src,
size_t  capacity = 0,
MemoryDeleter  d = nullptr 
) const
inline

Shares the data with an externally managed pointer.

This is similar to ShareData() but the source is a pointer with an advanced deleter option. In default, no deletion takes place, and one needs to make sure that the external memory is deallocated only after the tensor finishes using it. If a Deleter object is passed in, when this tensor is reallocated or freed, the deleter function is going to be called.

Definition at line 374 of file tensor.h.

void caffe2::Tensor::ShrinkTo ( int64_t  outer_dim) const
inline

Shrinks the outer-most dimension to given size, keeping the data.

This method guarantees that no re-allocations are carried out, which means that the extra capacity after the end of the shrunk tensor is maintained. Notably, this function does NOT respect caffe2_keep_on_shrink.

Definition at line 293 of file tensor.h.

int64_t caffe2::Tensor::size ( ) const
inline

(To be deprecated) Returns the size (i.e.

the number of items) of the tensor.

Definition at line 476 of file tensor.h.


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