Caffe2 - C++ API
A deep learning, cross platform ML framework
Public Member Functions | Data Fields
at::SparseTensorImpl Struct Reference
Inheritance diagram for at::SparseTensorImpl:
c10::TensorImpl c10::intrusive_ptr_target

Public Member Functions

 SparseTensorImpl (at::TensorTypeId, const caffe2::TypeMeta &)
 
int64_t nnz () const
 
int64_t sparse_dim () const
 
int64_t dense_dim () const
 
bool coalesced () const
 
Tensor indices () const
 
Tensor values () const
 
IntArrayRef sizes () const override
 Return a reference to the sizes of this tensor. More...
 
IntArrayRef strides () const override
 Return a reference to the strides of this tensor. More...
 
bool is_contiguous () const override
 Whether or not a tensor is laid out in contiguous memory. More...
 
int64_t stride (int64_t d) const override
 Return the stride of a tensor at some dimension.
 
void resize_dim (int64_t ndim) override
 Change the dimensionality of a tensor. More...
 
void set_size (int64_t dim, int64_t new_size) override
 Change the size at some dimension. More...
 
void set_stride (int64_t dim, int64_t new_stride) override
 Change the stride at some dimension. More...
 
void set_storage_offset (int64_t storage_offset) override
 Set the offset into the storage of this tensor. More...
 
int64_t dim () const override
 Return the number of dimensions of this tensor. More...
 
TensorImplmaybe_zero_dim (bool condition_when_zero_dim) override
 If condition_when_zero_dim is true, and the tensor is a 1-dim, 1-size tensor, reshape the tensor into a 0-dim tensor (scalar). More...
 
bool has_storage () const override
 True if this tensor has storage. More...
 
const Storagestorage () const override
 Return the underlying storage of a Tensor. More...
 
int64_t storage_offset () const override
 Return the offset in number of elements into the storage that this tensor points to. More...
 
void raw_resize_ (int64_t sparse_dim, int64_t dense_dim, IntArrayRef size)
 
void resize_ (int64_t sparse_dim, int64_t dense_dim, IntArrayRef size)
 
void resize_and_clear_ (int64_t sparse_dim, int64_t dense_dim, IntArrayRef size)
 
void set_coalesced (bool coalesced)
 
void set_nnz_and_narrow (int64_t new_nnz)
 
void set_indices_and_values_unsafe (const Tensor &indices, const Tensor &values)
 
c10::intrusive_ptr< TensorImplshallow_copy_and_detach () const override
 
- Public Member Functions inherited from c10::TensorImpl
 TensorImpl (TensorTypeId type_id, const caffe2::TypeMeta &data_type, Allocator *allocator, bool is_variable)
 Construct a 1-dim 0-size tensor with the given settings. More...
 
 TensorImpl (Storage &&storage, TensorTypeId type_id, bool is_variable)
 Construct a 1-dim 0-size tensor backed by the given storage.
 
 TensorImpl (const TensorImpl &)=delete
 
TensorImploperator= (const TensorImpl &)=delete
 
 TensorImpl (TensorImpl &&)=default
 
TensorImploperator= (TensorImpl &&)=default
 
virtual void release_resources () override
 Release (decref) storage, and any other external allocations. More...
 
TensorTypeId type_id () const
 Return the TensorTypeId corresponding to this Tensor. More...
 
virtual int64_t numel () const
 The number of elements in a tensor. More...
 
bool is_sparse () const
 
bool is_cuda () const
 
bool is_hip () const
 
int64_t get_device () const
 
Device device () const
 
Layout layout () const
 
bool is_wrapped_number () const
 True if a tensor was auto-wrapped from a C++ or Python number. More...
 
void set_wrapped_number (bool value)
 Set whether or not a tensor was auto-wrapped from a C++ or Python number. More...
 
void set_requires_grad (bool requires_grad)
 Set whether or not a tensor requires gradient. More...
 
bool requires_grad () const
 True if a tensor requires gradient. More...
 
at::Tensorgrad ()
 Return a mutable reference to the gradient. More...
 
const at::Tensorgrad () const
 Return the accumulated gradient of a tensor. More...
 
template<typename T >
Tdata () const
 Return a typed data pointer to the actual data which this tensor refers to. More...
 
void * data () const
 Return a void* data pointer to the actual data which this tensor refers to. More...
 
virtual void * slow_data () const
 This is just like data(), except it works with Variables. More...
 
template<typename T >
Tunsafe_data () const
 Like data<T>(), but performs no checks. More...
 
const caffe2::TypeMetadtype () const
 Returns the TypeMeta of a tensor, which describes what data type it is (e.g., int, float, ...)
 
size_t itemsize () const
 Return the size of a single element of this tensor in bytes.
 
bool is_empty () const
 True if a tensor has no elements (e.g., numel() == 0).
 
void set_sizes_contiguous (IntArrayRef new_size)
 Like set_sizes_and_strides but assumes contiguous strides. More...
 
void set_sizes_and_strides (IntArrayRef new_size, IntArrayRef new_stride)
 Set the sizes and strides of a tensor. More...
 
virtual int64_t size (int64_t d) const
 Return the size of a tensor at some dimension.
 
bool is_variable () const
 True if a tensor is a variable. More...
 
virtual void set_allow_tensor_metadata_change (bool value)
 Set whether a tensor allows changes to its metadata (e.g. More...
 
virtual bool allow_tensor_metadata_change () const
 True if a tensor allows changes to its metadata (e.g. More...
 
void set_autograd_meta (std::unique_ptr< c10::AutogradMetaInterface > autograd_meta)
 Set the pointer to autograd metadata.
 
c10::AutogradMetaInterfaceautograd_meta () const
 Return the pointer to autograd metadata.
 
std::unique_ptr< c10::AutogradMetaInterfacedetach_autograd_meta ()
 Detach the autograd metadata unique_ptr from this tensor, and return it.
 
DeviceType device_type () const
 The device type of a Tensor, e.g., DeviceType::CPU or DeviceType::CUDA.
 
Device GetDevice () const
 The device of a Tensor; e.g., Device(kCUDA, 1) (the 1-index CUDA device).
 
void Extend (int64_t num, float growthPct)
 Extends the outer-most dimension of this tensor by num elements, preserving the existing data. More...
 
template<class T >
void ReserveSpace (const T &outer_dim)
 Reserve space for the underlying tensor. More...
 
template<typename... Ts>
void Resize (Ts...dim_source)
 Resizes a tensor. More...
 
void Reshape (const std::vector< int64_t > &dims)
 Resizes the tensor without touching underlying storage. More...
 
void FreeMemory ()
 Release whatever memory the tensor was holding but keep size and type information. More...
 
void ShareData (const TensorImpl &src)
 Shares the data with another tensor. More...
 
void ShareExternalPointer (DataPtr &&data_ptr, const caffe2::TypeMeta &data_type, size_t capacity)
 
void * raw_mutable_data (const caffe2::TypeMeta &meta)
 Returns a mutable raw pointer of the underlying storage. More...
 
template<typename T >
Tmutable_data ()
 Returns a typed pointer of the underlying storage. More...
 
bool storage_initialized () const noexcept
 True if a tensor is storage initialized. More...
 
bool dtype_initialized () const noexcept
 True if a tensor is dtype initialized. More...
 
void set_storage (at::Storage storage)
 

Data Fields

int64_t sparse_dim_ = 0
 
int64_t dense_dim_ = 0
 
Tensor indices_
 
Tensor values_
 
bool coalesced_ = false
 

Additional Inherited Members

- Protected Member Functions inherited from c10::TensorImpl
void refresh_numel ()
 Recompute the cached numel of a tensor. More...
 
void refresh_contiguous ()
 Recompute the cached contiguity of a tensor. More...
 
- Protected Member Functions inherited from c10::intrusive_ptr_target
 intrusive_ptr_target (intrusive_ptr_target &&other) noexcept
 
intrusive_ptr_targetoperator= (intrusive_ptr_target &&other) noexcept
 
 intrusive_ptr_target (const intrusive_ptr_target &other) noexcept
 
intrusive_ptr_targetoperator= (const intrusive_ptr_target &other) noexcept
 
- Protected Attributes inherited from c10::TensorImpl
Storage storage_
 
std::unique_ptr< c10::AutogradMetaInterfaceautograd_meta_ = nullptr
 
SmallVector< int64_t, 5 > sizes_
 
SmallVector< int64_t, 5 > strides_
 
int64_t storage_offset_ = 0
 
int64_t numel_ = 1
 
caffe2::TypeMeta data_type_
 
TensorTypeId type_id_
 
bool is_contiguous_ = true
 
bool is_variable_ = false
 
bool is_wrapped_number_ = false
 
bool allow_tensor_metadata_change_ = true
 
bool reserved_ = false
 

Detailed Description

Definition at line 8 of file SparseTensorImpl.h.

Member Function Documentation

int64_t at::SparseTensorImpl::dim ( ) const
overridevirtual

Return the number of dimensions of this tensor.

Note that 0-dimension represents a Tensor that is a Scalar, e.g., one that has a single element.

Reimplemented from c10::TensorImpl.

Definition at line 65 of file SparseTensorImpl.cpp.

bool at::SparseTensorImpl::has_storage ( ) const
overridevirtual

True if this tensor has storage.

See storage() for details.

Reimplemented from c10::TensorImpl.

Definition at line 75 of file SparseTensorImpl.cpp.

bool at::SparseTensorImpl::is_contiguous ( ) const
overridevirtual

Whether or not a tensor is laid out in contiguous memory.

Tensors with non-trivial strides are not contiguous. See compute_contiguous() for the exact definition of whether or not a tensor is contiguous or not.

Reimplemented from c10::TensorImpl.

Definition at line 46 of file SparseTensorImpl.cpp.

TensorImpl * at::SparseTensorImpl::maybe_zero_dim ( bool  condition_when_zero_dim)
overridevirtual

If condition_when_zero_dim is true, and the tensor is a 1-dim, 1-size tensor, reshape the tensor into a 0-dim tensor (scalar).

This helper function is called from generated wrapper code, to help "fix up" tensors that legacy code didn't generate in the correct shape. For example, suppose that we have a legacy function 'add' which produces a tensor which is the same shape as its inputs; however, if the inputs were zero-dimensional, it produced a 1-dim 1-size tensor (don't ask). result->maybe_zero_dim(lhs->dim() == 0 && rhs->dim() == 0) will be called, correctly resetting the dimension to 0 when when the inputs had 0-dim.

As we teach more and more of TH to handle 0-dim correctly, this function will become less necessary. At the moment, it is often called from functions that correctly handle the 0-dim case, and is just dead code in this case. In the glorious future, this function will be eliminated entirely.

Reimplemented from c10::TensorImpl.

Definition at line 68 of file SparseTensorImpl.cpp.

void at::SparseTensorImpl::resize_dim ( int64_t  ndim)
overridevirtual

Change the dimensionality of a tensor.

This is truly a resize: old sizes, if they are still valid, are preserved (this invariant is utilized by some call-sites, e.g., the implementation of squeeze, which mostly wants the sizes to stay the same). New dimensions are given zero size and zero stride; this is probably not what you want–you should set_size/set_stride afterwards.

TODO: This should be jettisoned in favor of set_sizes_and_strides, which is harder to misuse.

Reimplemented from c10::TensorImpl.

Definition at line 52 of file SparseTensorImpl.cpp.

void at::SparseTensorImpl::set_size ( int64_t  dim,
int64_t  new_size 
)
overridevirtual

Change the size at some dimension.

This DOES NOT update strides; thus, most changes to size will not preserve contiguity. You probably also want to call set_stride() when you call this.

TODO: This should be jettisoned in favor of set_sizes_and_strides, which is harder to misuse.

Reimplemented from c10::TensorImpl.

Definition at line 55 of file SparseTensorImpl.cpp.

void at::SparseTensorImpl::set_storage_offset ( int64_t  storage_offset)
overridevirtual

Set the offset into the storage of this tensor.

WARNING: This does NOT check if the tensor is in bounds for the new location at the storage; the caller is responsible for checking this (and resizing if necessary.)

Reimplemented from c10::TensorImpl.

Definition at line 61 of file SparseTensorImpl.cpp.

void at::SparseTensorImpl::set_stride ( int64_t  dim,
int64_t  new_stride 
)
overridevirtual

Change the stride at some dimension.

TODO: This should be jettisoned in favor of set_sizes_and_strides, which is harder to misuse.

Reimplemented from c10::TensorImpl.

Definition at line 58 of file SparseTensorImpl.cpp.

IntArrayRef at::SparseTensorImpl::sizes ( ) const
overridevirtual

Return a reference to the sizes of this tensor.

This reference remains valid as long as the tensor is live and not resized.

Reimplemented from c10::TensorImpl.

Definition at line 40 of file SparseTensorImpl.cpp.

const Storage & at::SparseTensorImpl::storage ( ) const
overridevirtual

Return the underlying storage of a Tensor.

Multiple tensors may share a single storage. A Storage is an impoverished, Tensor-like class which supports far less operations than Tensor.

Avoid using this method if possible; try to use only Tensor APIs to perform operations.

Reimplemented from c10::TensorImpl.

Definition at line 78 of file SparseTensorImpl.cpp.

int64_t at::SparseTensorImpl::storage_offset ( ) const
overridevirtual

Return the offset in number of elements into the storage that this tensor points to.

Most tensors have storage_offset() == 0, but, for example, an index into a tensor will have a non-zero storage_offset().

WARNING: This is NOT computed in bytes.

XXX: The only thing stopping this function from being virtual is Variable.

Reimplemented from c10::TensorImpl.

Definition at line 81 of file SparseTensorImpl.cpp.

IntArrayRef at::SparseTensorImpl::strides ( ) const
overridevirtual

Return a reference to the strides of this tensor.

This reference remains valid as long as the tensor is live and not restrided.

Reimplemented from c10::TensorImpl.

Definition at line 43 of file SparseTensorImpl.cpp.


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