20using Shape = std::vector<int32_t>;
36 template <
typename It>
41 TypeToDtype<
typename std::iterator_traits<It>::value_type>());
54 std::initializer_list<T>
data,
75 if (this->
id() != other.id()) {
76 this->array_desc_ = other.array_desc_;
88 return array_desc_->size;
98 return array_desc_->shape.size();
103 return array_desc_->shape;
112 return shape().at(dim < 0 ? dim +
ndim() : dim);
117 return array_desc_->strides;
131 return array_desc_->dtype;
138 template <
typename T>
141 template <
typename T>
165 return a.arr.
id() == b.arr.
id() && a.idx == b.idx;
193 std::vector<array>
inputs);
196 std::vector<Shape> shapes,
197 const std::vector<Dtype>& dtypes,
198 const std::shared_ptr<Primitive>&
primitive,
199 const std::vector<array>&
inputs);
202 std::uintptr_t
id()
const {
203 return reinterpret_cast<std::uintptr_t
>(array_desc_.get());
208 return reinterpret_cast<std::uintptr_t
>(array_desc_->primitive.get());
247 return *(array_desc_->primitive);
252 return array_desc_->primitive;
257 return array_desc_->primitive !=
nullptr;
261 const std::vector<array>&
inputs()
const {
262 return array_desc_->inputs;
266 return array_desc_->inputs;
271 return array_desc_.use_count() == 1 && (array_desc_->data.use_count() == 1);
276 return array_desc_->siblings;
281 return array_desc_->siblings;
285 array_desc_->siblings = std::move(
siblings);
286 array_desc_->position = position;
292 auto idx = array_desc_->position;
306 return array_desc_->flags;
320 return array_desc_->data_size;
324 return array_desc_->data->buffer;
327 return array_desc_->data->buffer;
337 return array_desc_->data;
340 template <
typename T>
342 return static_cast<T*
>(array_desc_->data_ptr);
345 template <
typename T>
347 return static_cast<T*
>(array_desc_->data_ptr);
379 return array_desc_->status;
383 array_desc_->status = s;
388 return array_desc_->event;
393 array_desc_->event = std::move(e);
431 array_desc_ = other.array_desc_;
438 template <
typename It>
439 void init(
const It src);
446 std::shared_ptr<Primitive> primitive;
455 bool is_tracer{
false};
459 std::shared_ptr<Data> data;
462 void* data_ptr{
nullptr};
470 std::vector<array> inputs;
473 std::vector<array> siblings;
475 uint32_t position{0};
477 explicit ArrayDesc(
Shape shape, Dtype dtype);
482 std::shared_ptr<Primitive> primitive,
483 std::vector<array> inputs);
496 std::shared_ptr<ArrayDesc> array_desc_;
501 : array_desc_(
std::make_shared<ArrayDesc>(
std::vector<int>{}, dtype)) {
505template <
typename It>
510 array_desc_(
std::make_shared<ArrayDesc>(
std::move(shape), dtype)) {
516 std::initializer_list<T> data,
518 : array_desc_(
std::make_shared<ArrayDesc>(
519 std::vector<int>{
static_cast<int>(
data.size())},
526 std::initializer_list<T> data,
529 : array_desc_(
std::make_shared<ArrayDesc>(
std::move(shape), dtype)) {
531 throw std::invalid_argument(
532 "Data size and provided shape mismatch in array construction.");
540 throw std::invalid_argument(
"item can only be called on arrays of size 1.");
549 throw std::invalid_argument(
"item can only be called on arrays of size 1.");
552 throw std::invalid_argument(
553 "item() const can only be called on evaled arrays");
559template <
typename It>
560void array::init(It src) {
608 std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>,
array>;
610template <
typename... T>
613template <
typename... T>
Definition primitives.h:48
virtual size_t size(Buffer buffer) const =0
Definition allocator.h:12
void attach_event(Event e) const
Definition array.h:392
const Flags & flags() const
Get the Flags bit-field.
Definition array.h:305
Event & event() const
Definition array.h:387
Status
Definition array.h:350
@ available
Definition array.h:368
@ evaluated
Definition array.h:363
@ unscheduled
Definition array.h:353
@ scheduled
Definition array.h:358
const Shape & shape() const
The shape of the array as a vector of integers.
Definition array.h:102
void eval()
Evaluate the array.
const Strides & strides() const
The strides of the array.
Definition array.h:116
const std::vector< array > & inputs() const
The array's inputs.
Definition array.h:261
array(const array &other)=default
std::vector< array > outputs() const
The outputs of the array's primitive (i.e.
Definition array.h:291
size_t nbytes() const
The number of bytes in the array.
Definition array.h:92
void move_shared_buffer(array other)
static std::vector< array > make_arrays(std::vector< Shape > shapes, const std::vector< Dtype > &dtypes, const std::shared_ptr< Primitive > &primitive, const std::vector< array > &inputs)
array(std::initializer_list< float > data)
bool is_donatable() const
True indicates the arrays buffer is safe to reuse.
Definition array.h:270
array(allocator::Buffer data, Shape shape, Dtype dtype, Deleter deleter=allocator::free)
std::shared_ptr< Primitive > & primitive_ptr() const
A shared pointer to the array's primitive.
Definition array.h:251
size_t ndim() const
The number of dimensions of the array.
Definition array.h:97
size_t size() const
The number of elements in the array.
Definition array.h:87
array & operator=(array &&other) &&=delete
array & operator=(const array &other) &
Definition array.h:74
ArrayIterator end() const
Definition array.h:179
array(std::initializer_list< int > data, Dtype dtype)
void set_data(allocator::Buffer buffer, size_t data_size, Strides strides, Flags flags, Deleter d=allocator::free)
const allocator::Buffer & buffer() const
Definition array.h:326
void set_status(Status s) const
Definition array.h:382
array(const std::complex< float > &val, Dtype dtype=complex64)
Status status() const
Definition array.h:378
std::vector< array > & siblings()
The array's siblings.
Definition array.h:280
T * data()
Definition array.h:341
array(T val, Dtype dtype=TypeToDtype< T >())
Construct a scalar array with zero dimensions.
Definition array.h:500
ArrayIterator begin() const
Definition array.h:176
Primitive & primitive() const
The array's primitive.
Definition array.h:246
void detach()
Detach the array from the graph.
array & operator=(const array &other) &&=delete
Assignment to rvalue does not compile.
void set_siblings(std::vector< array > siblings, uint16_t position)
Definition array.h:284
T item()
Get the value from a scalar array.
Definition array.h:538
size_t buffer_size() const
Definition array.h:330
void copy_shared_buffer(const array &other)
void overwrite_descriptor(const array &other)
Definition array.h:430
const T * data() const
Definition array.h:346
bool has_primitive() const
Check if the array has an attached primitive or is a leaf node.
Definition array.h:256
allocator::Buffer & buffer()
Definition array.h:323
array(array &&other)=default
std::shared_ptr< Data > data_shared_ptr() const
Definition array.h:336
array(Shape shape, Dtype dtype, std::shared_ptr< Primitive > primitive, std::vector< array > inputs)
The following methods should be used with caution.
auto shape(int dim) const
Get the size of the corresponding dimension.
Definition array.h:111
auto strides(int dim) const
Get the stride of the corresponding dimension.
Definition array.h:125
const std::vector< array > & siblings() const
The array's siblings.
Definition array.h:275
std::vector< array > & inputs()
Definition array.h:265
void copy_shared_buffer(const array &other, const Strides &strides, Flags flags, size_t data_size, size_t offset=0)
array & operator=(array &&other) &=default
Default copy and move constructors otherwise.
void move_shared_buffer(array other, const Strides &strides, Flags flags, size_t data_size, size_t offset=0)
std::uintptr_t id() const
A unique identifier for an array.
Definition array.h:202
Dtype dtype() const
Get the arrays data type.
Definition array.h:130
bool is_available() const
void set_tracer(bool is_tracer)
Definition array.h:397
size_t itemsize() const
The size of the array's datatype in bytes.
Definition array.h:82
std::uintptr_t primitive_id() const
A unique identifier for an arrays primitive.
Definition array.h:207
void set_data(allocator::Buffer buffer, Deleter d=allocator::free)
size_t data_size() const
The size (in elements) of the underlying buffer the array points to.
Definition array.h:319
array std(const array &a, bool keepdims, int ddof=0, StreamOrDevice s={})
Computes the standard deviation of the elements of an array.
Buffer malloc(size_t size)
constexpr bool is_array_v
Definition array.h:607
constexpr Dtype bool_
Definition dtype.h:67
constexpr Dtype uint64
Definition dtype.h:72
constexpr Dtype uint16
Definition dtype.h:70
constexpr Dtype bfloat16
Definition dtype.h:81
constexpr Dtype int32
Definition dtype.h:76
constexpr Dtype float32
Definition dtype.h:80
constexpr Dtype int16
Definition dtype.h:75
constexpr Dtype int8
Definition dtype.h:74
constexpr Dtype int64
Definition dtype.h:77
constexpr bool is_arrays_v
Definition array.h:611
constexpr Dtype uint8
Definition dtype.h:69
std::vector< int32_t > Shape
Definition array.h:20
constexpr Dtype float16
Definition dtype.h:79
constexpr Dtype uint32
Definition dtype.h:71
std::vector< size_t > Strides
Definition array.h:21
uint8_t size_of(const Dtype &t)
Definition dtype.h:102
std::function< void(allocator::Buffer)> Deleter
Definition array.h:19
typename std::enable_if_t< is_arrays_v< T... > > enable_for_arrays_t
Definition array.h:614
constexpr Dtype complex64
Definition dtype.h:82
reference operator*() const
friend bool operator==(const ArrayIterator &a, const ArrayIterator &b)
Definition array.h:164
std::random_access_iterator_tag iterator_category
Definition array.h:145
ArrayIterator & operator++()
Definition array.h:159
friend bool operator!=(const ArrayIterator &a, const ArrayIterator &b)
Definition array.h:167
ArrayIterator(const array &arr, int idx=0)
size_t difference_type
Definition array.h:146
const array value_type
Definition array.h:147
ArrayIterator & operator+(difference_type diff)
Definition array.h:154
Deleter d
Definition array.h:213
Data(allocator::Buffer buffer, Deleter d=allocator::free)
Definition array.h:214
~Data()
Definition array.h:219
Data(const Data &d)=delete
Data & operator=(const Data &d)=delete
allocator::Buffer buffer
Definition array.h:212
bool row_contiguous
Definition array.h:236
bool col_contiguous
Definition array.h:242
bool contiguous
Definition array.h:230