All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits > Class Template Reference

Our tensor type. More...

Inheritance diagram for faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >:

## Public Types

enum | { NumDim = Dim }
enum | { IsInnerContig = InnerContig }
typedef T | DataType
typedef IndexT | IndexType
typedef PtrTraits< T >::PtrType | DataPtrType
typedef Tensor< T, Dim, InnerContig, IndexT, PtrTraits > | TensorType

## Public Member Functions

__host__ __device__ | Tensor ()
| Default constructor.
__host__ __device__ | Tensor (Tensor< T, Dim, InnerContig, IndexT, PtrTraits > &t)=default
| Copy constructor.
__host__ __device__ | Tensor (Tensor< T, Dim, InnerContig, IndexT, PtrTraits > &&t)=default
| Move constructor.
__host__ __device__ Tensor< T, Dim, InnerContig, IndexT, PtrTraits > & | operator= (Tensor< T, Dim, InnerContig, IndexT, PtrTraits > &t)=default
| Assignment.
__host__ __device__ Tensor< T, Dim, InnerContig, IndexT, PtrTraits > & | operator= (Tensor< T, Dim, InnerContig, IndexT, PtrTraits > &&t)
| Move assignment.
__host__ __device__ | Tensor (DataPtrType data, const IndexT sizes[Dim])
| Constructor that calculates strides with no padding.
__host__ __device__ | Tensor (DataPtrType data, std::initializer_list< IndexT > sizes)
__host__ __device__ | Tensor (DataPtrType data, const IndexT sizes[Dim], const IndexT strides[Dim])
__host__ void | copyFrom (Tensor< T, Dim, InnerContig, IndexT, PtrTraits > &t, cudaStream_t stream)
| Copies a tensor into ourselves; sizes must match.
__host__ void | copyTo (Tensor< T, Dim, InnerContig, IndexT, PtrTraits > &t, cudaStream_t stream)
| Copies ourselves into a tensor; sizes must match.
template<typename OtherT , int OtherDim>
__host__ __device__ bool | isSame (const Tensor< OtherT, OtherDim, InnerContig, IndexT, PtrTraits > &rhs) const
template<typename OtherT , int OtherDim>
__host__ __device__ bool | isSameSize (const Tensor< OtherT, OtherDim, InnerContig, IndexT, PtrTraits > &rhs) const
| Returns true if the two tensors are of the same dimensionality and size.
template<typename U >
__host__ __device__ Tensor< U, Dim, InnerContig, IndexT, PtrTraits > | cast ()
template<typename U >
__host__ __device__ const Tensor< U, Dim, InnerContig, IndexT, PtrTraits > | cast () const
| Const version of `cast`
template<typename U >
__host__ __device__ Tensor< U, Dim, InnerContig, IndexT, PtrTraits > | castResize ()
template<typename U >
__host__ __device__ const Tensor< U, Dim, InnerContig, IndexT, PtrTraits > | castResize () const
| Const version of `castResize`
template<typename U >
__host__ __device__ bool | canCastResize () const
| Returns true if we can castResize() this tensor to the new type.
template<typename NewIndexT >
__host__ Tensor< T, Dim, InnerContig, NewIndexT, PtrTraits > | castIndexType () const
template<typename NewIndexT >
__host__ bool | canUseIndexType () const
__host__ __device__ DataPtrType | data ()
| Returns a raw pointer to the start of our data.
__host__ __device__ DataPtrType | end ()
__host__ __device__ const DataPtrType | data () const
| Returns a raw pointer to the start of our data (const).
__host__ __device__ DataPtrType | end () const
template<typename U >
__host__ __device__ PtrTraits < U >::PtrType | dataAs ()
| Cast to a different datatype.
template<typename U >
__host__ __device__ const PtrTraits< const U >::PtrType | dataAs () const
| Cast to a different datatype.
__host__ __device__ detail::SubTensor< TensorType, Dim-1, PtrTraits > | operator[] (IndexT)
| Returns a read/write view of a portion of our tensor.
__host__ __device__ const detail::SubTensor< TensorType, Dim-1, PtrTraits > | operator[] (IndexT) const
| Returns a read/write view of a portion of our tensor (const).
__host__ __device__ IndexT | getSize (int i) const
__host__ __device__ IndexT | getStride (int i) const
__host__ __device__ size_t | numElements () const
__host__ __device__ size_t | getSizeInBytes () const
__host__ __device__ const IndexT * | sizes () const
| Returns the size array.
__host__ __device__ const IndexT * | strides () const
| Returns the stride array.
__host__ __device__ bool | isContiguous () const
__host__ __device__ bool | isConsistentlySized (int i) const
__host__ __device__ bool | isConsistentlySized () const
__host__ __device__ bool | isContiguousDim (int i) const
| Returns true if the given dimension index has no padding.
__host__ __device__ Tensor< T, Dim, InnerContig, IndexT, PtrTraits > | transpose (int dim1, int dim2) const
template<int NewDim>
__host__ __device__ Tensor< T, NewDim, InnerContig, IndexT, PtrTraits > | upcastOuter ()
template<int NewDim>
__host__ __device__ Tensor< T, NewDim, InnerContig, IndexT, PtrTraits > | upcastInner ()
template<int NewDim>
__host__ __device__ Tensor< T, NewDim, InnerContig, IndexT, PtrTraits > | downcastOuter ()
template<int NewDim>
__host__ __device__ Tensor< T, NewDim, InnerContig, IndexT, PtrTraits > | downcastInner ()
template<int SubDim>
__host__ __device__ Tensor< T, SubDim, InnerContig, IndexT, PtrTraits > | view (DataPtrType at)
template<int SubDim>
__host__ __device__ Tensor< T, SubDim, InnerContig, IndexT, PtrTraits > | view ()
__host__ __device__ Tensor< T, Dim, InnerContig, IndexT, PtrTraits > | narrowOutermost (IndexT start, IndexT size)
__host__ __device__ Tensor< T, Dim, InnerContig, IndexT, PtrTraits > | narrow (int dim, IndexT start, IndexT size)
template<int NewDim>
__host__ __device__ Tensor< T, NewDim, InnerContig, IndexT, PtrTraits > | view (std::initializer_list< IndexT > sizes)

## Protected Attributes

DataPtrType | data_
| Raw pointer to where the tensor data begins.
IndexT | stride_ [Dim]
| Array of strides (in sizeof(T) terms) per each dimension.
IndexT | size_ [Dim]
| Size per each dimension.

## Detailed Description

### template<typename T, int Dim, bool InnerContig, typename IndexT, template< typename U > class PtrTraits>
class faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >

Our tensor type.

Templated multi-dimensional array that supports strided access of elements. Main access is through `operator[]`; e.g., `tensor[x][y][z]`.

- `T` is the contained type (e.g., `float`)
- `Dim` is the tensor rank
- If `InnerContig` is true, then the tensor is assumed to be innermost
- contiguous, and only operations that make sense on contiguous
- arrays are allowed (e.g., no transpose). Strides are still
- calculated, but innermost stride is assumed to be 1.
- `IndexT` is the integer type used for size/stride arrays, and for
- all indexing math. Default is `int`, but for large tensors, `long`
- can be used instead.
- `PtrTraits` are traits applied to our data pointer (T*). By default,
- this is just T*, but RestrictPtrTraits can be used to apply T*
- restrict for alias-free analysis.

Definition at line 30 of file Tensor.cuh.

## Constructor & Destructor Documentation

template<typename T, int Dim, bool InnerContig, typename IndexT, template< typename U > class PtrTraits>

__host__ __device__ faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::Tensor | ( | DataPtrType | data,
|  | const IndexT | sizes[Dim],
|  | const IndexT | strides[Dim]
| ) |  |

Constructor that takes arbitrary size/stride arrays. Errors if you attempt to pass non-contiguous strides to a contiguous tensor.

Definition at line 87 of file Tensor-inl.cuh.

## Member Function Documentation

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>
template<typename NewIndexT >

__host__ bool faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::canUseIndexType | ( |  | ) | const

Returns true if we can use this indexing type to access all elements index type

Definition at line 325 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>
template<typename U >

__host__ __device__ Tensor< U, Dim, InnerContig, IndexT, PtrTraits > faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::cast | ( |  | ) |

Cast to a tensor of a different type of the same size and stride. U and our type T must be of the same size

Definition at line 222 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>
template<typename NewIndexT >

__host__ Tensor< T, Dim, InnerContig, NewIndexT, PtrTraits > faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::castIndexType | ( |  | ) | const

Attempts to cast this tensor to a tensor of a different IndexT. Fails if size or stride entries are not representable in the new IndexT.

Definition at line 305 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>
template<typename U >

__host__ __device__ Tensor< U, Dim, InnerContig, IndexT, PtrTraits > faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::castResize | ( |  | ) |

Cast to a tensor of a different type which is potentially a different size than our type T. Tensor must be aligned and the innermost dimension must be a size that is a multiple of sizeof(U) / sizeof(T), and the stride of the innermost dimension must be contiguous. The stride of all outer dimensions must be a multiple of sizeof(U) / sizeof(T) as well.

Definition at line 244 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>
template<int NewDim>

__host__ __device__ Tensor< T, NewDim, InnerContig, IndexT, PtrTraits > faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::downcastInner | ( |  | ) |

Downcast a tensor of dimension `D` to some tensor of dimension D' < D by collapsing the leading dimensions. asserts if there is padding on the leading dimensions.

Definition at line 555 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>
template<int NewDim>

__host__ __device__ Tensor< T, NewDim, InnerContig, IndexT, PtrTraits > faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::downcastOuter | ( |  | ) |

Downcast a tensor of dimension `D` to some tensor of dimension D' < D by collapsing the leading dimensions. asserts if there is padding on the leading dimensions.

Definition at line 510 of file Tensor-inl.cuh.

template<typename T, int Dim, bool InnerContig, typename IndexT, template< typename U > class PtrTraits>

__host__ __device__ DataPtrType faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::end | ( |  | ) |

inline

Returns a raw pointer to the end of our data, assuming continuity

Definition at line 184 of file Tensor.cuh.

template<typename T, int Dim, bool InnerContig, typename IndexT, template< typename U > class PtrTraits>

__host__ __device__ DataPtrType faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::end | ( |  | ) | const

inline

Returns a raw pointer to the end of our data, assuming continuity (const)

Definition at line 196 of file Tensor.cuh.

template<typename T, int Dim, bool InnerContig, typename IndexT, template< typename U > class PtrTraits>

__host__ __device__ IndexT faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::getSize | ( | int | i | ) | const

inline

Returns the size of a given dimension, `[0, Dim - 1]`. No bounds checking.

Definition at line 226 of file Tensor.cuh.

template<typename T, int Dim, bool InnerContig, typename IndexT, template< typename U > class PtrTraits>

__host__ __device__ size_t faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::getSizeInBytes | ( |  | ) | const

inline

If we are contiguous, returns the total size in bytes of our data

Definition at line 242 of file Tensor.cuh.

template<typename T, int Dim, bool InnerContig, typename IndexT, template< typename U > class PtrTraits>

__host__ __device__ IndexT faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::getStride | ( | int | i | ) | const

inline

Returns the stride of a given dimension, `[0, Dim - 1]`. No bounds checking.

Definition at line 232 of file Tensor.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>

__host__ __device__ bool faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::isConsistentlySized | ( | int | i | ) | const

Returns whether a given dimension has only increasing stride from the previous dimension. A tensor that was permuted by exchanging size and stride only will fail this check. If `i == 0` just check `size > 0`. Returns `false` if `stride` is `<= 0`.

Definition at line 384 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>

__host__ __device__ bool faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::isContiguous | ( |  | ) | const

Returns true if there is no padding within the tensor and no re-ordering of the dimensions.

(stride(i) == size(i + 1) * stride(i + 1)) && stride(dim - 1) == 0

Definition at line 365 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT, template< typename U > class PtrTraits>
template<typename OtherT , int OtherDim>

__host__ __device__ bool faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::isSame | ( | const Tensor< OtherT, OtherDim, InnerContig, IndexT, PtrTraits > & | rhs | ) | const

Returns true if the two tensors are of the same dimensionality, size and stride.

Definition at line 180 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT, template< typename U > class PtrTraits>

__host__ __device__ Tensor< T, Dim, InnerContig, IndexT, PtrTraits > faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::narrow | ( | int | dim,
|  | IndexT | start,
|  | IndexT | size
| ) |  |

Returns a tensor of the same dimension that is a view of the original tensor with the specified dimension restricted to the elements in the range [start, start + size). Can occur in an arbitrary dimension

Definition at line 633 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT, template< typename U > class PtrTraits>

__host__ __device__ Tensor< T, Dim, InnerContig, IndexT, PtrTraits > faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::narrowOutermost | ( | IndexT | start,
|  | IndexT | size
| ) |  |

Returns a tensor of the same dimension that is a view of the original tensor with the specified dimension restricted to the elements in the range [start, start + size)

Definition at line 625 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>

__host__ __device__ size_t faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::numElements | ( |  | ) | const

Returns the total number of elements contained within our data (product of `getSize(i)`)

Definition at line 352 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>

__host__ __device__ Tensor< T, Dim, InnerContig, IndexT, PtrTraits > faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::transpose | ( | int | dim1,
|  | int | dim2
| ) |  | const

Returns a tensor of the same dimension after transposing the two dimensions given. Does not actually move elements; transposition is made by permuting the size/stride arrays. If the dimensions are not valid, asserts.

Definition at line 420 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>
template<int NewDim>

__host__ __device__ Tensor< T, NewDim, InnerContig, IndexT, PtrTraits > faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::upcastInner | ( |  | ) |

Upcast a tensor of dimension `D` to some tensor of dimension D' > D by padding the lowest/most varying dimensions by 1 e.g., upcasting a 2-d tensor `[2][3]` to a 4-d tensor `[2][3][1][1]`

Definition at line 483 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>
template<int NewDim>

__host__ __device__ Tensor< T, NewDim, InnerContig, IndexT, PtrTraits > faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::upcastOuter | ( |  | ) |

Upcast a tensor of dimension `D` to some tensor of dimension D' > D by padding the leading dimensions by 1 e.g., upcasting a 2-d tensor `[2][3]` to a 4-d tensor `[1][1][2][3]`

Definition at line 454 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>
template<int SubDim>

__host__ __device__ Tensor< T, SubDim, InnerContig, IndexT, PtrTraits > faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::view | ( | DataPtrType | at | ) |

Returns a tensor that is a view of the `SubDim`-dimensional slice of this tensor, starting at `at`.

Definition at line 598 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT , template< typename U > class PtrTraits>
template<int SubDim>

__host__ __device__ Tensor< T, SubDim, InnerContig, IndexT, PtrTraits > faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::view | ( |  | ) |

Returns a tensor that is a view of the `SubDim`-dimensional slice of this tensor, starting where our data begins

Definition at line 618 of file Tensor-inl.cuh.

template<typename T , int Dim, bool InnerContig, typename IndexT, template< typename U > class PtrTraits>
template<int NewDim>

__host__ __device__ Tensor< T, NewDim, InnerContig, IndexT, PtrTraits > faiss::gpu::Tensor< T, Dim, InnerContig, IndexT, PtrTraits >::view | ( | std::initializer_list< IndexT > | sizes | ) |

Returns a view of the given tensor expressed as a tensor of a different number of dimensions. Only works if we are contiguous.

Definition at line 664 of file Tensor-inl.cuh.

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

- /data/users/matthijs/github_faiss/faiss/gpu/utils/Tensor.cuh
- /data/users/matthijs/github_faiss/faiss/gpu/utils/Tensor-inl.cuh