MCPcopy Create free account
hub / github.com/ml-explore/mlx-data / Array

Class Array

mlx/data/Array.h:20–112  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18inline constexpr bool dependent_false_v = false;
19
20class Array {
21 public:
22 Array();
23 Array(const std::shared_ptr<const Array>& src);
24 Array(ArrayType type);
25 Array(const std::string& str);
26 Array(std::string_view str);
27 Array(ArrayType type, const std::vector<int64_t>& shape);
28 Array(
29 ArrayType type,
30 const std::vector<int64_t>& shape,
31 std::shared_ptr<void> data);
32
33 template <class... Args>
34 Array(ArrayType type, Args... dim)
35 : Array(type, std::vector<int64_t>({dim...})){};
36
37 template <class T>
38 Array(const std::vector<T>& vec) {
39 init_(to_array_type<T>(), {static_cast<int64_t>(vec.size())});
40 memcpy(data(), vec.data(), vec.size() * itemsize_);
41 };
42
43 template <class T>
44 Array(T scalar) {
45 init_(to_array_type<T>(), {});
46 *reinterpret_cast<T*>(data()) = scalar;
47 };
48
49 template <class T>
50 static ArrayType to_array_type() {
51 if constexpr (std::is_same_v<T, char>) {
52 return ArrayType::Int8;
53 } else if constexpr (std::is_same_v<T, unsigned char>) {
54 return ArrayType::UInt8;
55 } else if constexpr (std::is_same_v<T, int32_t>) {
56 return ArrayType::Int32;
57 } else if constexpr (std::is_same_v<T, int64_t>) {
58 return ArrayType::Int64;
59 } else if constexpr (std::is_same_v<T, float>) {
60 return ArrayType::Float;
61 } else if constexpr (std::is_same_v<T, double>) {
62 return ArrayType::Double;
63 } else {
64 static_assert(dependent_false_v<T>, "unsupported type");
65 }
66 };
67
68 const std::vector<int64_t>& shape() const;
69 int64_t shape(int d) const;
70 int ndim() const;
71 int64_t itemsize() const;
72
73 template <class T>
74 T* data() const {
75 if (type_ != to_array_type<T>()) {
76 throw std::runtime_error("Array: incompatible array type");
77 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected