Skip to content
Back to top
Tensor
-
class Tensor
Public Functions
-
Tensor()
-
Tensor(const Shape &shape, DType dtype = DType::Float32, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
Tensor(std::initializer_list<size_t> shape, DType dtype = DType::Float32, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
Tensor(std::shared_ptr<Storage> storage, const Shape &shape, const Strides &strides, DType dtype, size_t offset = 0, MemoryOrder order = MemoryOrder::RowMajor)
-
explicit Tensor(std::shared_ptr<graph::GraphNode> lazy_node)
-
Tensor(const Tensor &other)
-
Tensor &operator=(const Tensor &other)
-
Tensor(Tensor &&other) noexcept
-
Tensor &operator=(Tensor &&other) noexcept
-
const Shape &shape() const
-
inline size_t ndim() const
-
inline size_t size() const
-
inline size_t numel() const
-
const Strides &strides() const
-
inline size_t itemsize() const
-
inline size_t nbytes() const
-
inline MemoryOrder memory_order() const
-
DType dtype() const
-
inline std::string dtype_name() const
-
Device device() const
-
inline const TensorFlags &flags() const
-
inline bool is_contiguous() const
-
inline bool is_c_contiguous() const
-
inline bool is_f_contiguous() const
-
inline std::shared_ptr<Storage> storage() const
-
inline size_t offset() const
-
inline bool empty() const
-
inline bool is_lazy() const
-
inline std::shared_ptr<graph::GraphNode> lazy_node() const
-
inline void sync() const
-
inline bool is_view() const
-
inline bool owns_data() const
-
bool has_zero_stride() const
-
bool has_negative_stride() const
-
inline bool shares_storage(const Tensor &other) const
-
bool would_materialize_on_reshape(const Shape &new_shape) const
-
inline bool would_materialize_on_transpose() const
-
void *data()
-
const void *data() const
-
Tensor slice(const std::vector<Slice> &slice_args) const
-
Tensor operator[](std::initializer_list<Index> indices) const
-
template<typename T>
inline T *typed_data()
-
template<typename T>
inline const T *typed_data() const
-
template<typename T>
inline T item(const std::vector<size_t> &indices) const
-
template<typename T>
inline void set_item(const std::vector<size_t> &indices, const T &value)
-
template<typename T>
inline void fill(const T &value)
-
Tensor ascontiguousarray() const
-
inline Tensor contiguous() const
-
inline Tensor as_c_contiguous() const
-
Tensor asfortranarray() const
-
inline Tensor as_f_contiguous() const
-
Tensor reshape(const Shape &new_shape, MemoryOrder order = MemoryOrder::RowMajor) const
-
Tensor reshape(std::initializer_list<size_t> new_shape, MemoryOrder order = MemoryOrder::RowMajor) const
-
Tensor rearrange(const std::string &pattern, const std::map<std::string, size_t> &axis_sizes = {}) const
-
Tensor reduce(const std::string &pattern, const std::string &reduction, const std::map<std::string, size_t> &axis_sizes = {}) const
-
Tensor repeat(const std::string &pattern, const std::map<std::string, size_t> &axis_sizes = {}) const
-
Tensor transpose() const
-
Tensor transpose(const std::vector<int> &axes) const
-
Tensor squeeze(int axis = -1) const
-
Tensor unsqueeze(int axis) const
-
Tensor view(const Shape &new_shape) const
-
Tensor flatten(int start_dim = 0, int end_dim = -1) const
-
Tensor unflatten(int dim, const Shape &sizes) const
-
inline Tensor permute(const std::vector<int> &dims) const
-
inline Tensor T() const
-
inline Tensor ravel() const
-
Tensor negative() const
-
Tensor flipud() const
-
Tensor fliplr() const
-
Tensor swapaxes(int axis1, int axis2) const
-
Tensor moveaxis(int source, int destination) const
-
Tensor flip(int axis) const
-
Tensor flip(const std::vector<int> &axes) const
-
Tensor rot90(int k = 1, const std::vector<int> &axes = {0, 1}) const
-
Tensor roll(int64_t shift, int axis = -1) const
-
Tensor roll(const std::vector<int64_t> &shifts, const std::vector<int> &axes) const
-
Tensor diagonal(int offset = 0, int axis1 = 0, int axis2 = 1) const
-
Tensor trace(int offset = 0, int axis1 = 0, int axis2 = 1) const
-
template<typename T>
inline T item() const
-
Tensor expand(const Shape &new_shape) const
-
inline Tensor expand_as(const Tensor &other) const
-
inline Tensor broadcast_to(const Shape &shape) const
-
Tensor repeat(const std::vector<size_t> &repeats) const
-
inline Tensor repeat(std::initializer_list<size_t> repeats) const
-
inline Tensor tile(const std::vector<size_t> &reps) const
-
Tensor matmul(const Tensor &other, bool transpose_self = false, bool transpose_other = false) const
-
inline Tensor mm(const Tensor &other) const
-
inline Tensor dot(const Tensor &other) const
-
Tensor det() const
-
Tensor inv() const
-
Tensor where(const Tensor &condition, const Tensor &other) const
-
Tensor where(const Tensor &condition, float other) const
-
Tensor where(const Tensor &condition, double other) const
-
Tensor where(const Tensor &condition, int32_t other) const
-
Tensor masked_fill(const Tensor &mask, float value) const
-
Tensor masked_fill(const Tensor &mask, double value) const
-
Tensor masked_fill(const Tensor &mask, int32_t value) const
-
Tensor masked_fill(const Tensor &mask, const Tensor &value) const
-
Tensor &masked_fill_(const Tensor &mask, float value)
-
Tensor &masked_fill_(const Tensor &mask, double value)
-
Tensor &masked_fill_(const Tensor &mask, int32_t value)
-
Tensor masked_select(const Tensor &mask) const
-
Tensor gather(int dim, const Tensor &indices) const
-
Tensor scatter(int dim, const Tensor &indices, const Tensor &src) const
-
Tensor &scatter_(int dim, const Tensor &indices, const Tensor &src)
-
Tensor index_select(int dim, const Tensor &indices) const
-
Tensor sum(int axis = -1, bool keep_dims = false) const
-
Tensor sum(const std::vector<int> &axes, bool keep_dims = false) const
-
Tensor mean(int axis = -1, bool keep_dims = false) const
-
Tensor mean(const std::vector<int> &axes, bool keep_dims = false) const
-
Tensor max(int axis = -1, bool keep_dims = false) const
-
Tensor min(int axis = -1, bool keep_dims = false) const
-
Tensor argmax(int axis = -1, bool keep_dims = false) const
-
Tensor argmin(int axis = -1, bool keep_dims = false) const
-
Tensor prod(int axis = -1, bool keep_dims = false) const
-
Tensor prod(const std::vector<int> &axes, bool keep_dims = false) const
-
Tensor any(int axis = -1, bool keep_dims = false) const
-
Tensor any(const std::vector<int> &axes, bool keep_dims = false) const
-
Tensor all(int axis = -1, bool keep_dims = false) const
-
Tensor all(const std::vector<int> &axes, bool keep_dims = false) const
-
Tensor var(int axis = -1, int ddof = 0, bool keep_dims = false) const
-
Tensor var(const std::vector<int> &axes, int ddof = 0, bool keep_dims = false) const
-
Tensor std(int axis = -1, int ddof = 0, bool keep_dims = false) const
-
Tensor std(const std::vector<int> &axes, int ddof = 0, bool keep_dims = false) const
-
Tensor ptp(int axis = -1, bool keep_dims = false) const
-
Tensor abs() const
-
Tensor sqrt() const
-
Tensor exp() const
-
Tensor log() const
-
Tensor sin() const
-
Tensor cos() const
-
Tensor tan() const
-
Tensor sign() const
-
Tensor floor() const
-
Tensor ceil() const
-
Tensor trunc() const
-
Tensor round(int decimals = 0) const
-
Tensor reciprocal() const
-
Tensor square() const
-
Tensor cbrt() const
-
Tensor isnan() const
-
Tensor isinf() const
-
Tensor isfinite() const
-
Tensor clip(const Tensor &min_val, const Tensor &max_val) const
-
Tensor clip(double min_val, double max_val) const
-
inline Tensor clamp(const Tensor &min_val, const Tensor &max_val) const
-
inline Tensor clamp(double min_val, double max_val) const
-
Tensor relu() const
-
Tensor leaky_relu(float negative_slope = 0.01f) const
-
Tensor gelu() const
-
Tensor silu() const
-
Tensor sigmoid() const
-
Tensor tanh() const
-
Tensor softmax(int axis = -1) const
-
Tensor log_softmax(int axis = -1) const
-
Tensor glu(int dim = -1) const
-
Tensor copy(MemoryOrder order = MemoryOrder::RowMajor) const
-
inline Tensor clone() const
-
Tensor to(Device device, MemoryOrder order = MemoryOrder::RowMajor) const
-
Tensor cpu() const
-
Tensor gpu() const
-
Tensor astype(DType new_dtype) const
-
Tensor astype_safe(DType new_dtype) const
-
inline Tensor to_float() const
-
inline Tensor to_double() const
-
inline Tensor to_int() const
-
inline Tensor to_int64() const
-
inline Tensor to_bool() const
-
inline Tensor to_contiguous_cpu() const
-
inline Tensor to_complex() const
-
inline Tensor to_complex128() const
-
inline Tensor half() const
-
inline Tensor bfloat16() const
-
Tensor real() const
-
Tensor imag() const
-
Tensor conj() const
-
std::string repr() const
-
std::string str() const
-
std::string shape_str() const
-
bool same_shape(const Tensor &other) const
-
bool same_dtype(const Tensor &other) const
-
bool same_device(const Tensor &other) const
-
bool same_memory_order(const Tensor &other) const
-
Tensor isclose(const Tensor &other, double rtol = 1e-5, double atol = 1e-8) const
-
bool allclose(const Tensor &other, double rtol = 1e-5, double atol = 1e-8) const
-
bool array_equal(const Tensor &other) const
-
bool has_nan() const
-
bool has_inf() const
-
inline bool is_finite() const
-
Tensor &nan_guard()
-
Tensor &assert_finite()
-
Tensor &assert_shape(const std::string &pattern)
-
Tensor &assert_shape(const Shape &expected)
-
std::string debug_info() const
-
void save(const std::string &filename) const
-
inline Tensor new_zeros(const Shape &shape) const
-
inline Tensor new_ones(const Shape &shape) const
-
template<typename T>
inline Tensor new_full(const Shape &shape, T value) const
-
std::vector<Tensor> split(size_t sections, int axis = 0) const
-
std::vector<Tensor> split(const std::vector<size_t> &indices, int axis = 0) const
-
std::vector<Tensor> chunk(size_t n_chunks, int axis = 0) const
-
inline std::vector<Tensor> vsplit(size_t sections) const
-
inline std::vector<Tensor> hsplit(size_t sections) const
-
inline std::vector<Tensor> dsplit(size_t sections) const
-
inline Tensor cat(const Tensor &other, int axis = 0) const
Public Static Functions
-
static Tensor load(const std::string &filename, Device device = Device::CPU)
-
static void save_tensors(const std::map<std::string, Tensor> &tensors, const std::string &filename)
-
static std::map<std::string, Tensor> load_tensors(const std::string &filename, Device device = Device::CPU)
-
static std::vector<std::string> list_tensors_in_archive(const std::string &filename)
-
static Tensor load_tensor_from_archive(const std::string &filename, const std::string &tensor_name, Device device = Device::CPU)
-
static Tensor zeros(const Shape &shape, DType dtype = DType::Float32, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
static Tensor zeros(std::initializer_list<size_t> shape, DType dtype = DType::Float32, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
static Tensor ones(const Shape &shape, DType dtype = DType::Float32, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
static Tensor ones(std::initializer_list<size_t> shape, DType dtype = DType::Float32, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
static Tensor empty(const Shape &shape, DType dtype = DType::Float32, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
static Tensor empty(std::initializer_list<size_t> shape, DType dtype = DType::Float32, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
static Tensor eye(size_t n, DType dtype = DType::Float32, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
static Tensor identity(size_t n, DType dtype = DType::Float32, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
static Tensor randn(const Shape &shape, DType dtype = DType::Float32, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
static Tensor rand(const Shape &shape, DType dtype = DType::Float32, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
static Tensor uniform(double low, double high, const Shape &shape, DType dtype = DType::Float32, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
static Tensor randint(int64_t low, int64_t high, const Shape &shape, DType dtype = DType::Int64, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
static Tensor rand_like(const Tensor &prototype)
-
static Tensor randn_like(const Tensor &prototype)
-
static Tensor randint_like(const Tensor &prototype, int64_t low, int64_t high)
-
static void manual_seed(uint64_t seed)
-
static Tensor arange(int64_t start, int64_t end, int64_t step = 1, DType dtype = DType::Int32, Device device = Device::CPU)
-
static Tensor arange(int64_t end, DType dtype = DType::Int32, Device device = Device::CPU)
-
static Tensor linspace(double start, double stop, size_t num = 50, bool endpoint = true, DType dtype = DType::Float64, Device device = Device::CPU)
-
static Tensor logspace(double start, double stop, size_t num = 50, bool endpoint = true, double base = 10.0, DType dtype = DType::Float64, Device device = Device::CPU)
-
static Tensor geomspace(double start, double stop, size_t num = 50, bool endpoint = true, DType dtype = DType::Float64, Device device = Device::CPU)
-
static Tensor zeros_like(const Tensor &prototype)
-
static Tensor ones_like(const Tensor &prototype)
-
static Tensor empty_like(const Tensor &prototype)
-
template<typename T>
static inline Tensor full_like(const Tensor &prototype, const T &value)
-
static Tensor diag(const Tensor &v, int64_t k = 0)
-
static Tensor tri(size_t N, size_t M = 0, int64_t k = 0, DType dtype = DType::Float64, Device device = Device::CPU)
-
static Tensor tril(const Tensor &m, int64_t k = 0)
-
static Tensor triu(const Tensor &m, int64_t k = 0)
-
template<typename T>
static inline Tensor full(const Shape &shape, const T &value, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
template<typename T>
static inline Tensor from_data(const T *data, const Shape &shape, bool copy = true, MemoryOrder order = MemoryOrder::RowMajor)
-
template<typename T, size_t N>
static inline Tensor from_array(const T (&data)[N], const Shape &shape, DType target_dtype = dtype_of_v<T>, Device device = Device::CPU, MemoryOrder order = MemoryOrder::RowMajor)
-
template<typename T>
static inline Tensor asarray(const Tensor &tensor)
-
template<typename T>
static inline Tensor asarray(const Tensor &tensor, Device device)
-
static inline DType result_type(const Tensor &a, const Tensor &b)
-
static Tensor concatenate(const std::vector<Tensor> &tensors, int axis = 0)
-
static inline Tensor cat(const std::vector<Tensor> &tensors, int axis = 0)
-
static inline Tensor cat(std::initializer_list<Tensor> tensors, int axis = 0)
-
static Tensor stack(const std::vector<Tensor> &tensors, int axis = 0)
-
static inline Tensor stack(std::initializer_list<Tensor> tensors, int axis = 0)
-
static Tensor vstack(const std::vector<Tensor> &tensors)
-
static inline Tensor vstack(std::initializer_list<Tensor> tensors)
-
static Tensor hstack(const std::vector<Tensor> &tensors)
-
static inline Tensor hstack(std::initializer_list<Tensor> tensors)
-
static Tensor dstack(const std::vector<Tensor> &tensors)
-
static inline Tensor dstack(std::initializer_list<Tensor> tensors)
-
static Tensor column_stack(const std::vector<Tensor> &tensors)
-
static inline Tensor column_stack(std::initializer_list<Tensor> tensors)
-
static inline Tensor row_stack(const std::vector<Tensor> &tensors)
-
static inline Tensor row_stack(std::initializer_list<Tensor> tensors)
-
static inline Tensor ascontiguousarray(const Tensor &tensor)
-
static inline Tensor asfortranarray(const Tensor &tensor)