Tensor

class Tensor

The base tensor class, manage memory or not.

Tensor is usually constructed with the shape info:

auto* a = new dragon::Tensor(std::vector<int64_t>({2, 3}));
auto* b = dragon::Tensor().Reshape({2, 3}); // Equivalent

To allocate the data, type meta and device context are also required:

auto meta = dragon::TypeMeta::Make<float>();
auto* raw_data = a->raw_mutable_data<dragon::CPUContext>(meta);
auto* data = b->mutable_data<float, dragon::CPUContext>();

Memory will be reset if required bytes is larger than capacity:

std::cout << a->nbytes() << " " << a->capacity() << std::endl; // 24, 24
std::cout << a->Reshape({2, 4})->size() << std::endl; // 8
std::cout << a->nbytes() << " " << a->capacity() << std::endl; // 32, 0
a->mutable_data<float, dragon::CPUContext>();
a->Reshape({2, 3});
std::cout << a->nbytes() << " " << a->capacity() << std::endl; // 24, 32

Constructors

inline dragon::Tensor::Tensor()

Default constructor.

inline explicit dragon::Tensor::Tensor(const string &name)

Constructor with the name.

inline explicit dragon::Tensor::Tensor(const vec64_t &dims)

Constructor with the int64 dimensions.

inline explicit dragon::Tensor::Tensor(const vec32_t &dims)

Constructor with the int32 dimensions.

inline explicit dragon::Tensor::Tensor(const TypeMeta &meta)

Constructor with the type meta.

Public Properties

axis

inline int64_t dragon::Tensor::axis(const int64_t i) const

Return a canonical axis

capacity

inline size_t dragon::Tensor::capacity() const

Return the byte length of memory.

conut

inline int64_t dragon::Tensor::count() const

Return the number of elements counting along all axes.

conut

inline int64_t dragon::Tensor::count(int64_t start) const

Return the number of elements counting from given axis.

conut

inline int64_t dragon::Tensor::count(
  int64_t start,
  int64_t end) const

Return the number of elements counting along given axes.

data

template<typename T, class Context>
inline const T *dragon::Tensor::data()

Return the typed data pointer.

dim

inline int64_t dragon::Tensor::dim(int64_t i) const

Return the dimension of given axis.

dims

inline const vec64_t &dragon::Tensor::dims() const

Return the dimensions.

empty

inline bool dragon::Tensor::empty() const

Return whether the number of elements is zero.

memory

inline UnifiedMemory *dragon::Tensor::memory(
  bool required = false,
  bool owned = false)

Return the memory.

memory_state

inline UnifiedMemory::State dragon::Tensor::memory_state()

Return the memory state.

meta

inline const TypeMeta &dragon::Tensor::meta() const

Return the type meta.

mutable_data

template<typename T, class Context>
inline T *dragon::Tensor::mutable_data()

Return the typed mutable data pointer.

name

inline const string &dragon::Tensor::name() const

Return the tensor name.

nbytes

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

Return the byte length of all elements.

ndim

inline int dragon::Tensor::ndim() const

Return the number of dimensions.

raw_data

template<class Context>
const void *dragon::Tensor::raw_data()

Return the raw data pointer.

raw_mutable_data

template<class Context>
inline void *dragon::Tensor::raw_mutable_data()

Return the raw mutable data pointer.

size

inline size_t dragon::Tensor::size() const

Return the number of elements.

stride

inline int64_t dragon::Tensor::stride(int64_t i) const

Return the stride of given axis.

strides

inline const vec64_t &dragon::Tensor::strides() const

Return the strides.

version

inline int64_t dragon::Tensor::version() const

Return the tensor version.

has_memory

inline bool dragon::Tensor::has_memory() const

Return whether the memory is set.

has_name

inline bool dragon::Tensor::has_name() const

Return whether the tensor name is set.

Public Functions

CopyFrom

template<class Context>
inline Tensor *dragon::Tensor::CopyFrom(
  Tensor &other,
  Context *ctx)

Copy memory from a tensor.

CopyFrom

template<typename TensorType, typename VectorType>
inline Tensor *dragon::Tensor::CopyFrom(const vector<VectorType> &other)

Copy memory from a vector.

CopyTo

template<typename TensorType, typename VectorType>
inline void dragon::Tensor::CopyTo(vector<VectorType> &dest)

Copy memory to a vector.

DimString

inline string dragon::Tensor::DimString() const

Return a string formatting the tensor dimensions.

DimString

static inline string dragon::Tensor::DimString(const vector<int64_t> &dims)

Return a string formatting the given dimensions.

IsType

template<typename T>
inline bool dragon::Tensor::IsType()

Return whether the data type is matched.

MapFrom

inline Tensor *dragon::Tensor::MapFrom(
  Tensor *other,
  size_t offset = 0)

Map memory from a tensor.

Reset

inline void dragon::Tensor::Reset()

Reset tensor to release all resources.

Reshape

inline Tensor *dragon::Tensor::Reshape(const vec64_t &dims)

Change the dimensions.

ReshapeLike

inline Tensor *dragon::Tensor::ReshapeLike(const Tensor &other)

Change the dimensions as the other.