mlx-cpu 0.30.1__py3-none-manylinux_2_35_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mlx/__main__.py +27 -0
- mlx/_reprlib_fix.py +16 -0
- mlx/extension.py +88 -0
- mlx/include/mlx/3rdparty/pocketfft.h +3581 -0
- mlx/include/mlx/allocator.h +73 -0
- mlx/include/mlx/array.h +645 -0
- mlx/include/mlx/backend/common/binary.h +97 -0
- mlx/include/mlx/backend/common/broadcasting.h +11 -0
- mlx/include/mlx/backend/common/buffer_cache.h +157 -0
- mlx/include/mlx/backend/common/compiled.h +77 -0
- mlx/include/mlx/backend/common/copy.h +50 -0
- mlx/include/mlx/backend/common/hadamard.h +109 -0
- mlx/include/mlx/backend/common/matmul.h +67 -0
- mlx/include/mlx/backend/common/reduce.h +59 -0
- mlx/include/mlx/backend/common/slicing.h +20 -0
- mlx/include/mlx/backend/common/ternary.h +85 -0
- mlx/include/mlx/backend/common/unary.h +29 -0
- mlx/include/mlx/backend/common/utils.h +205 -0
- mlx/include/mlx/backend/cpu/arange.h +28 -0
- mlx/include/mlx/backend/cpu/available.h +9 -0
- mlx/include/mlx/backend/cpu/binary.h +517 -0
- mlx/include/mlx/backend/cpu/binary_ops.h +98 -0
- mlx/include/mlx/backend/cpu/binary_two.h +166 -0
- mlx/include/mlx/backend/cpu/compiled_preamble.h +12 -0
- mlx/include/mlx/backend/cpu/copy.h +36 -0
- mlx/include/mlx/backend/cpu/encoder.h +67 -0
- mlx/include/mlx/backend/cpu/eval.h +12 -0
- mlx/include/mlx/backend/cpu/gemm.h +26 -0
- mlx/include/mlx/backend/cpu/gemms/simd_gemm.h +139 -0
- mlx/include/mlx/backend/cpu/jit_compiler.h +20 -0
- mlx/include/mlx/backend/cpu/lapack.h +80 -0
- mlx/include/mlx/backend/cpu/simd/accelerate_fp16_simd.h +56 -0
- mlx/include/mlx/backend/cpu/simd/accelerate_simd.h +329 -0
- mlx/include/mlx/backend/cpu/simd/base_simd.h +295 -0
- mlx/include/mlx/backend/cpu/simd/math.h +193 -0
- mlx/include/mlx/backend/cpu/simd/neon_fp16_simd.h +212 -0
- mlx/include/mlx/backend/cpu/simd/simd.h +4 -0
- mlx/include/mlx/backend/cpu/simd/type.h +11 -0
- mlx/include/mlx/backend/cpu/slicing.h +21 -0
- mlx/include/mlx/backend/cpu/ternary.h +154 -0
- mlx/include/mlx/backend/cpu/threefry.h +21 -0
- mlx/include/mlx/backend/cpu/unary.h +281 -0
- mlx/include/mlx/backend/cpu/unary_ops.h +180 -0
- mlx/include/mlx/backend/cuda/allocator.h +89 -0
- mlx/include/mlx/backend/cuda/conv/conv.h +126 -0
- mlx/include/mlx/backend/cuda/cublas_utils.h +96 -0
- mlx/include/mlx/backend/cuda/cuda.h +10 -0
- mlx/include/mlx/backend/cuda/cuda_utils.h +89 -0
- mlx/include/mlx/backend/cuda/cudnn_utils.h +171 -0
- mlx/include/mlx/backend/cuda/device/config.h +12 -0
- mlx/include/mlx/backend/cuda/device.h +189 -0
- mlx/include/mlx/backend/cuda/event.h +78 -0
- mlx/include/mlx/backend/cuda/gemms/cublas_gemm.h +114 -0
- mlx/include/mlx/backend/cuda/gemms/gemv.h +24 -0
- mlx/include/mlx/backend/cuda/jit_module.h +119 -0
- mlx/include/mlx/backend/cuda/lru_cache.h +189 -0
- mlx/include/mlx/backend/cuda/quantized/cublas_qqmm.h +88 -0
- mlx/include/mlx/backend/cuda/quantized/cuda_fp4.h +83 -0
- mlx/include/mlx/backend/cuda/quantized/qqmm_utils.h +30 -0
- mlx/include/mlx/backend/cuda/quantized/quantized.h +45 -0
- mlx/include/mlx/backend/cuda/utils.h +46 -0
- mlx/include/mlx/backend/cuda/worker.h +55 -0
- mlx/include/mlx/backend/gpu/available.h +9 -0
- mlx/include/mlx/backend/gpu/copy.h +57 -0
- mlx/include/mlx/backend/gpu/eval.h +18 -0
- mlx/include/mlx/backend/gpu/slicing.h +36 -0
- mlx/include/mlx/backend/metal/allocator.h +79 -0
- mlx/include/mlx/backend/metal/binary.h +33 -0
- mlx/include/mlx/backend/metal/device.h +283 -0
- mlx/include/mlx/backend/metal/jit/includes.h +57 -0
- mlx/include/mlx/backend/metal/jit/indexing.h +76 -0
- mlx/include/mlx/backend/metal/kernels/arange.h +9 -0
- mlx/include/mlx/backend/metal/kernels/atomic.h +345 -0
- mlx/include/mlx/backend/metal/kernels/bf16.h +16 -0
- mlx/include/mlx/backend/metal/kernels/bf16_math.h +380 -0
- mlx/include/mlx/backend/metal/kernels/binary.h +199 -0
- mlx/include/mlx/backend/metal/kernels/binary_ops.h +326 -0
- mlx/include/mlx/backend/metal/kernels/binary_two.h +244 -0
- mlx/include/mlx/backend/metal/kernels/cexpf.h +134 -0
- mlx/include/mlx/backend/metal/kernels/complex.h +173 -0
- mlx/include/mlx/backend/metal/kernels/copy.h +276 -0
- mlx/include/mlx/backend/metal/kernels/defines.h +24 -0
- mlx/include/mlx/backend/metal/kernels/erf.h +69 -0
- mlx/include/mlx/backend/metal/kernels/expm1f.h +90 -0
- mlx/include/mlx/backend/metal/kernels/fft/radix.h +328 -0
- mlx/include/mlx/backend/metal/kernels/fft/readwrite.h +624 -0
- mlx/include/mlx/backend/metal/kernels/fft.h +486 -0
- mlx/include/mlx/backend/metal/kernels/fp4.h +59 -0
- mlx/include/mlx/backend/metal/kernels/fp8.h +82 -0
- mlx/include/mlx/backend/metal/kernels/fp_quantized.h +1804 -0
- mlx/include/mlx/backend/metal/kernels/fp_quantized_nax.h +1059 -0
- mlx/include/mlx/backend/metal/kernels/gemv_masked.h +827 -0
- mlx/include/mlx/backend/metal/kernels/hadamard.h +182 -0
- mlx/include/mlx/backend/metal/kernels/indexing/gather.h +51 -0
- mlx/include/mlx/backend/metal/kernels/indexing/gather_axis.h +44 -0
- mlx/include/mlx/backend/metal/kernels/indexing/gather_front.h +24 -0
- mlx/include/mlx/backend/metal/kernels/indexing/indexing.h +23 -0
- mlx/include/mlx/backend/metal/kernels/indexing/masked_scatter.h +38 -0
- mlx/include/mlx/backend/metal/kernels/indexing/scatter.h +59 -0
- mlx/include/mlx/backend/metal/kernels/indexing/scatter_axis.h +52 -0
- mlx/include/mlx/backend/metal/kernels/logsumexp.h +140 -0
- mlx/include/mlx/backend/metal/kernels/quantized.h +2502 -0
- mlx/include/mlx/backend/metal/kernels/quantized_nax.h +1705 -0
- mlx/include/mlx/backend/metal/kernels/quantized_utils.h +90 -0
- mlx/include/mlx/backend/metal/kernels/reduce.h +5 -0
- mlx/include/mlx/backend/metal/kernels/reduce_utils.h +6 -0
- mlx/include/mlx/backend/metal/kernels/reduction/ops.h +275 -0
- mlx/include/mlx/backend/metal/kernels/reduction/reduce_all.h +66 -0
- mlx/include/mlx/backend/metal/kernels/reduction/reduce_col.h +398 -0
- mlx/include/mlx/backend/metal/kernels/reduction/reduce_init.h +8 -0
- mlx/include/mlx/backend/metal/kernels/reduction/reduce_row.h +369 -0
- mlx/include/mlx/backend/metal/kernels/scan.h +514 -0
- mlx/include/mlx/backend/metal/kernels/sdpa_vector.h +415 -0
- mlx/include/mlx/backend/metal/kernels/softmax.h +190 -0
- mlx/include/mlx/backend/metal/kernels/sort.h +715 -0
- mlx/include/mlx/backend/metal/kernels/steel/attn/attn.h +296 -0
- mlx/include/mlx/backend/metal/kernels/steel/attn/kernels/steel_attention.h +476 -0
- mlx/include/mlx/backend/metal/kernels/steel/attn/kernels/steel_attention_nax.h +481 -0
- mlx/include/mlx/backend/metal/kernels/steel/attn/loader.h +264 -0
- mlx/include/mlx/backend/metal/kernels/steel/attn/mma.h +750 -0
- mlx/include/mlx/backend/metal/kernels/steel/attn/nax.h +1076 -0
- mlx/include/mlx/backend/metal/kernels/steel/attn/params.h +44 -0
- mlx/include/mlx/backend/metal/kernels/steel/attn/transforms.h +71 -0
- mlx/include/mlx/backend/metal/kernels/steel/conv/conv.h +13 -0
- mlx/include/mlx/backend/metal/kernels/steel/conv/kernels/steel_conv.h +176 -0
- mlx/include/mlx/backend/metal/kernels/steel/conv/kernels/steel_conv_general.h +225 -0
- mlx/include/mlx/backend/metal/kernels/steel/conv/loader.h +6 -0
- mlx/include/mlx/backend/metal/kernels/steel/conv/loaders/loader_channel_l.h +451 -0
- mlx/include/mlx/backend/metal/kernels/steel/conv/loaders/loader_channel_n.h +319 -0
- mlx/include/mlx/backend/metal/kernels/steel/conv/loaders/loader_general.h +381 -0
- mlx/include/mlx/backend/metal/kernels/steel/conv/params.h +62 -0
- mlx/include/mlx/backend/metal/kernels/steel/defines.h +7 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/gemm.h +295 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/gemm_nax.h +156 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_fused.h +346 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_fused_nax.h +207 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_gather.h +459 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_gather_nax.h +132 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_masked.h +719 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_segmented.h +266 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/kernels/steel_gemm_splitk.h +227 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/loader.h +137 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/mma.h +1146 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/nax.h +1084 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/params.h +64 -0
- mlx/include/mlx/backend/metal/kernels/steel/gemm/transforms.h +72 -0
- mlx/include/mlx/backend/metal/kernels/steel/utils/integral_constant.h +134 -0
- mlx/include/mlx/backend/metal/kernels/steel/utils/type_traits.h +55 -0
- mlx/include/mlx/backend/metal/kernels/steel/utils.h +42 -0
- mlx/include/mlx/backend/metal/kernels/ternary.h +145 -0
- mlx/include/mlx/backend/metal/kernels/ternary_ops.h +10 -0
- mlx/include/mlx/backend/metal/kernels/unary.h +63 -0
- mlx/include/mlx/backend/metal/kernels/unary_ops.h +454 -0
- mlx/include/mlx/backend/metal/kernels/utils.h +444 -0
- mlx/include/mlx/backend/metal/matmul.h +144 -0
- mlx/include/mlx/backend/metal/metal.h +22 -0
- mlx/include/mlx/backend/metal/reduce.h +41 -0
- mlx/include/mlx/backend/metal/resident.h +32 -0
- mlx/include/mlx/backend/metal/scan.h +17 -0
- mlx/include/mlx/backend/metal/ternary.h +21 -0
- mlx/include/mlx/backend/metal/unary.h +21 -0
- mlx/include/mlx/backend/metal/utils.h +84 -0
- mlx/include/mlx/backend/no_gpu/apple_memory.h +16 -0
- mlx/include/mlx/backend/no_gpu/linux_memory.h +22 -0
- mlx/include/mlx/compile.h +44 -0
- mlx/include/mlx/compile_impl.h +69 -0
- mlx/include/mlx/device.h +31 -0
- mlx/include/mlx/distributed/distributed.h +60 -0
- mlx/include/mlx/distributed/distributed_impl.h +59 -0
- mlx/include/mlx/distributed/jaccl/jaccl.h +12 -0
- mlx/include/mlx/distributed/mpi/mpi.h +12 -0
- mlx/include/mlx/distributed/mpi/mpi_declarations.h +28 -0
- mlx/include/mlx/distributed/nccl/nccl.h +12 -0
- mlx/include/mlx/distributed/ops.h +56 -0
- mlx/include/mlx/distributed/primitives.h +156 -0
- mlx/include/mlx/distributed/reduction_ops.h +38 -0
- mlx/include/mlx/distributed/ring/ring.h +12 -0
- mlx/include/mlx/distributed/utils.h +67 -0
- mlx/include/mlx/dtype.h +115 -0
- mlx/include/mlx/dtype_utils.h +119 -0
- mlx/include/mlx/einsum.h +22 -0
- mlx/include/mlx/event.h +58 -0
- mlx/include/mlx/export.h +136 -0
- mlx/include/mlx/export_impl.h +98 -0
- mlx/include/mlx/fast.h +102 -0
- mlx/include/mlx/fast_primitives.h +427 -0
- mlx/include/mlx/fence.h +39 -0
- mlx/include/mlx/fft.h +167 -0
- mlx/include/mlx/graph_utils.h +66 -0
- mlx/include/mlx/io/gguf.h +20 -0
- mlx/include/mlx/io/load.h +175 -0
- mlx/include/mlx/io.h +61 -0
- mlx/include/mlx/linalg.h +111 -0
- mlx/include/mlx/memory.h +78 -0
- mlx/include/mlx/mlx.h +25 -0
- mlx/include/mlx/ops.h +1627 -0
- mlx/include/mlx/primitives.h +2524 -0
- mlx/include/mlx/random.h +282 -0
- mlx/include/mlx/scheduler.h +188 -0
- mlx/include/mlx/small_vector.h +540 -0
- mlx/include/mlx/stream.h +41 -0
- mlx/include/mlx/threadpool.h +133 -0
- mlx/include/mlx/transforms.h +229 -0
- mlx/include/mlx/transforms_impl.h +86 -0
- mlx/include/mlx/types/bf16.h +187 -0
- mlx/include/mlx/types/complex.h +113 -0
- mlx/include/mlx/types/fp16.h +234 -0
- mlx/include/mlx/types/half_types.h +58 -0
- mlx/include/mlx/types/limits.h +70 -0
- mlx/include/mlx/utils.h +175 -0
- mlx/include/mlx/version.h +20 -0
- mlx/lib/libmlx.so +0 -0
- mlx/py.typed +1 -0
- mlx/share/cmake/MLX/FindNCCL.cmake +54 -0
- mlx/share/cmake/MLX/Findnvpl.cmake +3 -0
- mlx/share/cmake/MLX/MLXConfig.cmake +66 -0
- mlx/share/cmake/MLX/MLXConfigVersion.cmake +65 -0
- mlx/share/cmake/MLX/MLXTargets-release.cmake +19 -0
- mlx/share/cmake/MLX/MLXTargets.cmake +106 -0
- mlx/share/cmake/MLX/extension.cmake +50 -0
- mlx/utils.py +325 -0
- mlx_cpu-0.30.1.dist-info/METADATA +142 -0
- mlx_cpu-0.30.1.dist-info/RECORD +231 -0
- mlx_cpu-0.30.1.dist-info/WHEEL +5 -0
- mlx_cpu-0.30.1.dist-info/licenses/LICENSE +21 -0
- mlx_cpu-0.30.1.dist-info/sboms/auditwheel.cdx.json +1 -0
- mlx_cpu-0.30.1.dist-info/top_level.txt +1 -0
- mlx_cpu.libs/libblas-bd8a282c.so.3.10.0 +0 -0
- mlx_cpu.libs/libgfortran-3ec47101.so.5.0.0 +0 -0
- mlx_cpu.libs/liblapack-86b2c207.so.3.10.0 +0 -0
- mlx_cpu.libs/libquadmath-67d31475.so.0.0.0 +0 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Copyright © 2023 Apple Inc.
|
|
2
|
+
|
|
3
|
+
#pragma once
|
|
4
|
+
|
|
5
|
+
#include <cstdlib>
|
|
6
|
+
|
|
7
|
+
namespace mlx::core::allocator {
|
|
8
|
+
|
|
9
|
+
// Simple wrapper around buffer pointers
|
|
10
|
+
// WARNING: Only Buffer objects constructed from and those that wrap
|
|
11
|
+
// raw pointers from mlx::allocator are supported.
|
|
12
|
+
class Buffer {
|
|
13
|
+
private:
|
|
14
|
+
void* ptr_;
|
|
15
|
+
|
|
16
|
+
public:
|
|
17
|
+
explicit Buffer(void* ptr) : ptr_(ptr) {};
|
|
18
|
+
|
|
19
|
+
// Get the raw data pointer from the buffer
|
|
20
|
+
void* raw_ptr();
|
|
21
|
+
|
|
22
|
+
// Get the buffer pointer from the buffer
|
|
23
|
+
const void* ptr() const {
|
|
24
|
+
return ptr_;
|
|
25
|
+
};
|
|
26
|
+
void* ptr() {
|
|
27
|
+
return ptr_;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
class Allocator {
|
|
32
|
+
/** Abstract base class for a memory allocator. */
|
|
33
|
+
public:
|
|
34
|
+
virtual Buffer malloc(size_t size) = 0;
|
|
35
|
+
virtual void free(Buffer buffer) = 0;
|
|
36
|
+
virtual size_t size(Buffer buffer) const = 0;
|
|
37
|
+
virtual Buffer make_buffer(void* ptr, size_t size) {
|
|
38
|
+
return Buffer{nullptr};
|
|
39
|
+
};
|
|
40
|
+
virtual void release(Buffer buffer) {}
|
|
41
|
+
|
|
42
|
+
Allocator() = default;
|
|
43
|
+
Allocator(const Allocator& other) = delete;
|
|
44
|
+
Allocator(Allocator&& other) = delete;
|
|
45
|
+
Allocator& operator=(const Allocator& other) = delete;
|
|
46
|
+
Allocator& operator=(Allocator&& other) = delete;
|
|
47
|
+
virtual ~Allocator() = default;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
Allocator& allocator();
|
|
51
|
+
|
|
52
|
+
inline Buffer malloc(size_t size) {
|
|
53
|
+
return allocator().malloc(size);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
inline void free(Buffer buffer) {
|
|
57
|
+
allocator().free(buffer);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// Make a Buffer from a raw pointer of the given size without a copy. If a
|
|
61
|
+
// no-copy conversion is not possible then the returned buffer.ptr() will be
|
|
62
|
+
// nullptr. Any buffer created with this function must be released with
|
|
63
|
+
// release(buffer)
|
|
64
|
+
inline Buffer make_buffer(void* ptr, size_t size) {
|
|
65
|
+
return allocator().make_buffer(ptr, size);
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// Release a buffer from the allocator made with make_buffer
|
|
69
|
+
inline void release(Buffer buffer) {
|
|
70
|
+
allocator().release(buffer);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
} // namespace mlx::core::allocator
|
mlx/include/mlx/array.h
ADDED
|
@@ -0,0 +1,645 @@
|
|
|
1
|
+
// Copyright © 2023 Apple Inc.
|
|
2
|
+
#pragma once
|
|
3
|
+
|
|
4
|
+
#include <algorithm>
|
|
5
|
+
#include <cstdint>
|
|
6
|
+
#include <functional>
|
|
7
|
+
#include <memory>
|
|
8
|
+
#include <vector>
|
|
9
|
+
|
|
10
|
+
#include "mlx/allocator.h"
|
|
11
|
+
#include "mlx/dtype.h"
|
|
12
|
+
#include "mlx/event.h"
|
|
13
|
+
#include "mlx/small_vector.h"
|
|
14
|
+
|
|
15
|
+
namespace mlx::core {
|
|
16
|
+
|
|
17
|
+
// Forward declaration
|
|
18
|
+
class Primitive;
|
|
19
|
+
|
|
20
|
+
using Deleter = std::function<void(allocator::Buffer)>;
|
|
21
|
+
using ShapeElem = int32_t;
|
|
22
|
+
using Shape = SmallVector<ShapeElem>;
|
|
23
|
+
using Strides = SmallVector<int64_t>;
|
|
24
|
+
|
|
25
|
+
class array {
|
|
26
|
+
/* An array is really a node in a graph. It contains a shared ArrayDesc
|
|
27
|
+
* object */
|
|
28
|
+
|
|
29
|
+
public:
|
|
30
|
+
/** Construct a scalar array with zero dimensions. */
|
|
31
|
+
template <typename T>
|
|
32
|
+
explicit array(T val, Dtype dtype = TypeToDtype<T>());
|
|
33
|
+
|
|
34
|
+
/* Special case since std::complex can't be implicitly converted to other
|
|
35
|
+
* types. */
|
|
36
|
+
explicit array(const std::complex<float>& val, Dtype dtype = complex64);
|
|
37
|
+
|
|
38
|
+
template <typename It>
|
|
39
|
+
explicit array(
|
|
40
|
+
It data,
|
|
41
|
+
Shape shape,
|
|
42
|
+
Dtype dtype =
|
|
43
|
+
TypeToDtype<typename std::iterator_traits<It>::value_type>());
|
|
44
|
+
|
|
45
|
+
template <typename T>
|
|
46
|
+
explicit array(std::initializer_list<T> data, Dtype dtype = TypeToDtype<T>());
|
|
47
|
+
|
|
48
|
+
/* Special case so empty lists default to float32. */
|
|
49
|
+
explicit array(std::initializer_list<float> data);
|
|
50
|
+
|
|
51
|
+
/* Special case so array({}, type) is an empty array. */
|
|
52
|
+
explicit array(std::initializer_list<int> data, Dtype dtype);
|
|
53
|
+
|
|
54
|
+
template <typename T>
|
|
55
|
+
explicit array(
|
|
56
|
+
std::initializer_list<T> data,
|
|
57
|
+
Shape shape,
|
|
58
|
+
Dtype dtype = TypeToDtype<T>());
|
|
59
|
+
|
|
60
|
+
/* Build an array from a raw pointer. The constructor will attempt to use the
|
|
61
|
+
* input data without a copy. The deleter will be called when the array no
|
|
62
|
+
* longer needs the underlying memory - after the array is destroyed in the
|
|
63
|
+
* no-copy case and after the copy otherwise. */
|
|
64
|
+
explicit array(
|
|
65
|
+
void* data,
|
|
66
|
+
Shape shape,
|
|
67
|
+
Dtype dtype,
|
|
68
|
+
const std::function<void(void*)>& deleter);
|
|
69
|
+
|
|
70
|
+
/* Build an array from a buffer */
|
|
71
|
+
explicit array(
|
|
72
|
+
allocator::Buffer data,
|
|
73
|
+
Shape shape,
|
|
74
|
+
Dtype dtype,
|
|
75
|
+
Deleter deleter = allocator::free);
|
|
76
|
+
|
|
77
|
+
/** Assignment to rvalue does not compile. */
|
|
78
|
+
array& operator=(const array& other) && = delete;
|
|
79
|
+
array& operator=(array&& other) && = delete;
|
|
80
|
+
|
|
81
|
+
/** Default copy and move constructors otherwise. */
|
|
82
|
+
array& operator=(array&& other) & = default;
|
|
83
|
+
array(const array& other) = default;
|
|
84
|
+
array(array&& other) = default;
|
|
85
|
+
|
|
86
|
+
array& operator=(const array& other) & {
|
|
87
|
+
if (this->id() != other.id()) {
|
|
88
|
+
this->array_desc_ = other.array_desc_;
|
|
89
|
+
}
|
|
90
|
+
return *this;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** The size of the array's datatype in bytes. */
|
|
94
|
+
size_t itemsize() const {
|
|
95
|
+
return size_of(dtype());
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The number of elements in the array. */
|
|
99
|
+
size_t size() const {
|
|
100
|
+
return array_desc_->size;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** The number of bytes in the array. */
|
|
104
|
+
size_t nbytes() const {
|
|
105
|
+
return size() * itemsize();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** The number of dimensions of the array. */
|
|
109
|
+
size_t ndim() const {
|
|
110
|
+
return array_desc_->shape.size();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** The shape of the array as a vector of integers. */
|
|
114
|
+
const Shape& shape() const {
|
|
115
|
+
return array_desc_->shape;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Get the size of the corresponding dimension.
|
|
120
|
+
*
|
|
121
|
+
* This function supports negative indexing and provides
|
|
122
|
+
* bounds checking. */
|
|
123
|
+
auto shape(int dim) const {
|
|
124
|
+
return shape().at(dim < 0 ? dim + ndim() : dim);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The strides of the array. */
|
|
128
|
+
const Strides& strides() const {
|
|
129
|
+
return array_desc_->strides;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Get the stride of the corresponding dimension.
|
|
134
|
+
*
|
|
135
|
+
* This function supports negative indexing and provides
|
|
136
|
+
* bounds checking. */
|
|
137
|
+
auto strides(int dim) const {
|
|
138
|
+
return strides().at(dim < 0 ? dim + ndim() : dim);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** Get the arrays data type. */
|
|
142
|
+
Dtype dtype() const {
|
|
143
|
+
return array_desc_->dtype;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** Evaluate the array. */
|
|
147
|
+
void eval();
|
|
148
|
+
|
|
149
|
+
/** Get the value from a scalar array. */
|
|
150
|
+
template <typename T>
|
|
151
|
+
T item();
|
|
152
|
+
|
|
153
|
+
template <typename T>
|
|
154
|
+
T item() const;
|
|
155
|
+
|
|
156
|
+
struct ArrayIterator {
|
|
157
|
+
using iterator_category = std::random_access_iterator_tag;
|
|
158
|
+
using difference_type = size_t;
|
|
159
|
+
using value_type = const array;
|
|
160
|
+
using reference = value_type;
|
|
161
|
+
|
|
162
|
+
explicit ArrayIterator(const array& arr, int idx = 0);
|
|
163
|
+
|
|
164
|
+
reference operator*() const;
|
|
165
|
+
|
|
166
|
+
ArrayIterator& operator+(difference_type diff) {
|
|
167
|
+
idx += diff;
|
|
168
|
+
return *this;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
ArrayIterator& operator++() {
|
|
172
|
+
idx++;
|
|
173
|
+
return *this;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
friend bool operator==(const ArrayIterator& a, const ArrayIterator& b) {
|
|
177
|
+
return a.arr.id() == b.arr.id() && a.idx == b.idx;
|
|
178
|
+
}
|
|
179
|
+
friend bool operator!=(const ArrayIterator& a, const ArrayIterator& b) {
|
|
180
|
+
return !(a == b);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
private:
|
|
184
|
+
const array& arr;
|
|
185
|
+
int idx;
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
ArrayIterator begin() const {
|
|
189
|
+
return ArrayIterator(*this);
|
|
190
|
+
}
|
|
191
|
+
ArrayIterator end() const {
|
|
192
|
+
return ArrayIterator(*this, shape(0));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The following methods should be used with caution.
|
|
197
|
+
* They are intended for use by the backend implementation and the
|
|
198
|
+
* API may change.
|
|
199
|
+
*/
|
|
200
|
+
|
|
201
|
+
array(
|
|
202
|
+
Shape shape,
|
|
203
|
+
Dtype dtype,
|
|
204
|
+
std::shared_ptr<Primitive> primitive,
|
|
205
|
+
std::vector<array> inputs);
|
|
206
|
+
|
|
207
|
+
static std::vector<array> make_arrays(
|
|
208
|
+
std::vector<Shape> shapes,
|
|
209
|
+
const std::vector<Dtype>& dtypes,
|
|
210
|
+
const std::shared_ptr<Primitive>& primitive,
|
|
211
|
+
const std::vector<array>& inputs);
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Get a new array that refers to the same data as the input but with a
|
|
215
|
+
* non-owning pointer to it. Note the array is detached from the graph and has
|
|
216
|
+
* no inputs, siblings or primitive.
|
|
217
|
+
*/
|
|
218
|
+
static array unsafe_weak_copy(const array& other);
|
|
219
|
+
|
|
220
|
+
/** A unique identifier for an array. */
|
|
221
|
+
std::uintptr_t id() const {
|
|
222
|
+
return reinterpret_cast<std::uintptr_t>(array_desc_.get());
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/** A unique identifier for an arrays primitive. */
|
|
226
|
+
std::uintptr_t primitive_id() const {
|
|
227
|
+
return reinterpret_cast<std::uintptr_t>(array_desc_->primitive.get());
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
struct Data {
|
|
231
|
+
allocator::Buffer buffer;
|
|
232
|
+
Deleter d;
|
|
233
|
+
Data(allocator::Buffer buffer, Deleter d = allocator::free)
|
|
234
|
+
: buffer(buffer), d(d) {}
|
|
235
|
+
// Not copyable
|
|
236
|
+
Data(const Data& d) = delete;
|
|
237
|
+
Data& operator=(const Data& d) = delete;
|
|
238
|
+
Data(Data&& o) : buffer(o.buffer), d(o.d) {
|
|
239
|
+
o.buffer = allocator::Buffer(nullptr);
|
|
240
|
+
o.d = [](allocator::Buffer) {};
|
|
241
|
+
}
|
|
242
|
+
~Data() {
|
|
243
|
+
d(buffer);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
struct Flags {
|
|
248
|
+
// True iff there are no gaps in the underlying data. Each item
|
|
249
|
+
// in the underlying data buffer belongs to at least one index.
|
|
250
|
+
//
|
|
251
|
+
// True iff:
|
|
252
|
+
// prod(shape[i] for i in range(ndim) if strides[i] > 0) == data_size()
|
|
253
|
+
bool contiguous : 1;
|
|
254
|
+
|
|
255
|
+
// True iff:
|
|
256
|
+
// strides[-1] == 1 and
|
|
257
|
+
// all(strides[i] == (shape[i+1]*strides[i+1]) or shape[i] == 1 for i in
|
|
258
|
+
// range(ndim - 1))
|
|
259
|
+
bool row_contiguous : 1;
|
|
260
|
+
|
|
261
|
+
// True iff:
|
|
262
|
+
// strides[0] == 1 and
|
|
263
|
+
// all(strides[i] == (shape[i-1]*strides[i-1]) or shape[i] == 1 for i in
|
|
264
|
+
// range(1, ndim))
|
|
265
|
+
bool col_contiguous : 1;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
/** The array's primitive. */
|
|
269
|
+
Primitive& primitive() const {
|
|
270
|
+
return *(array_desc_->primitive);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** A shared pointer to the array's primitive. */
|
|
274
|
+
std::shared_ptr<Primitive>& primitive_ptr() const {
|
|
275
|
+
return array_desc_->primitive;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** Check if the array has an attached primitive or is a leaf node. */
|
|
279
|
+
bool has_primitive() const {
|
|
280
|
+
return array_desc_->primitive != nullptr;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** The array's inputs. */
|
|
284
|
+
const std::vector<array>& inputs() const {
|
|
285
|
+
return array_desc_->inputs;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
std::vector<array>& inputs() {
|
|
289
|
+
return array_desc_->inputs;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/** True indicates the arrays buffer is safe to reuse */
|
|
293
|
+
bool is_donatable() const {
|
|
294
|
+
return array_desc_.use_count() == 1 && (array_desc_->data.use_count() == 1);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/** The array's siblings. */
|
|
298
|
+
const std::vector<array>& siblings() const {
|
|
299
|
+
return array_desc_->siblings;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/** The array's siblings. */
|
|
303
|
+
std::vector<array>& siblings() {
|
|
304
|
+
return array_desc_->siblings;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/** The array's position in the sibling list. */
|
|
308
|
+
int sibling_position() const {
|
|
309
|
+
return array_desc_->position;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
void set_siblings(std::vector<array> siblings, uint16_t position) {
|
|
313
|
+
array_desc_->siblings = std::move(siblings);
|
|
314
|
+
array_desc_->position = position;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/** The outputs of the array's primitive (i.e. this array and
|
|
318
|
+
* its siblings) in the order the primitive expects. */
|
|
319
|
+
std::vector<array> outputs() const {
|
|
320
|
+
auto idx = array_desc_->position;
|
|
321
|
+
std::vector<array> outputs;
|
|
322
|
+
outputs.reserve(siblings().size() + 1);
|
|
323
|
+
outputs.insert(outputs.end(), siblings().begin(), siblings().begin() + idx);
|
|
324
|
+
outputs.push_back(*this);
|
|
325
|
+
outputs.insert(outputs.end(), siblings().begin() + idx, siblings().end());
|
|
326
|
+
return outputs;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** Detach the array from the graph. */
|
|
330
|
+
void detach();
|
|
331
|
+
|
|
332
|
+
/** Get the Flags bit-field. */
|
|
333
|
+
const Flags& flags() const {
|
|
334
|
+
return array_desc_->flags;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/** The size (in elements) of the underlying buffer the array points to.
|
|
338
|
+
*
|
|
339
|
+
* This can be different than the actual size of the array if the array has
|
|
340
|
+
* been broadcast or irregularly strided. If ``first`` is the offset into
|
|
341
|
+
* the data buffer of the first element of the array (i.e. the offset
|
|
342
|
+
* corresponding to ``arr[0, 0, ...]``) and last is the offset into the
|
|
343
|
+
* data buffer of the last element of the array (i.e. the offset
|
|
344
|
+
* corresponding to ``arr[-1, -1, ...]``) then ``data_size = last - first``.
|
|
345
|
+
* Note, ``data_size`` is in units of ``item_size`` (not bytes).
|
|
346
|
+
**/
|
|
347
|
+
size_t data_size() const {
|
|
348
|
+
return array_desc_->data_size;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
allocator::Buffer& buffer() {
|
|
352
|
+
return array_desc_->data->buffer;
|
|
353
|
+
}
|
|
354
|
+
const allocator::Buffer& buffer() const {
|
|
355
|
+
return array_desc_->data->buffer;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
size_t buffer_size() const {
|
|
359
|
+
return allocator::allocator().size(buffer());
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// Return the shared pointer to the array::Data struct
|
|
363
|
+
const std::shared_ptr<Data>& data_shared_ptr() const {
|
|
364
|
+
return array_desc_->data;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Return a raw pointer to the arrays data. This function may do a copy if
|
|
368
|
+
// the underlying buffer is not accessible on the CPU. When accessing the
|
|
369
|
+
// data for GPU kernels, be sure to use the correct method / function for the
|
|
370
|
+
// given backend to access the GPU pointer.
|
|
371
|
+
template <typename T>
|
|
372
|
+
T* data() {
|
|
373
|
+
return reinterpret_cast<T*>(
|
|
374
|
+
(static_cast<char*>(buffer().raw_ptr()) + array_desc_->offset));
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
template <typename T>
|
|
378
|
+
const T* data() const {
|
|
379
|
+
return const_cast<array&>(*this).data<T>();
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
int64_t offset() const {
|
|
383
|
+
return array_desc_->offset;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
enum Status {
|
|
387
|
+
// The output of a computation which has not been scheduled.
|
|
388
|
+
// For example, the status of `x` in `auto x = a + b`.
|
|
389
|
+
unscheduled,
|
|
390
|
+
|
|
391
|
+
// The array's `eval_*` function has been run, but the computation is not
|
|
392
|
+
// necessarily complete. The array will have memory allocated and if it is
|
|
393
|
+
// not a tracer then it will be detached from the graph.
|
|
394
|
+
evaluated,
|
|
395
|
+
|
|
396
|
+
// If the array is the output of a computation then the computation
|
|
397
|
+
// is complete. Constant arrays are always available (e.g. `array({1, 2,
|
|
398
|
+
// 3})`)
|
|
399
|
+
available
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
// Check if the array is safe to read.
|
|
403
|
+
bool is_available() const;
|
|
404
|
+
|
|
405
|
+
// Wait on the array to be available. After this `is_available` returns
|
|
406
|
+
// `true`.
|
|
407
|
+
void wait();
|
|
408
|
+
|
|
409
|
+
Status status() const {
|
|
410
|
+
return array_desc_->status;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
void set_status(Status s) const {
|
|
414
|
+
array_desc_->status = s;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// Get the array's shared event
|
|
418
|
+
Event& event() const {
|
|
419
|
+
return array_desc_->event;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// Attach an event to a not yet evaluated array
|
|
423
|
+
void attach_event(Event e) const {
|
|
424
|
+
array_desc_->event = std::move(e);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
void detach_event() const {
|
|
428
|
+
array_desc_->event = Event{};
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
// Mark the array as a tracer array (true) or not.
|
|
432
|
+
void set_tracer(bool is_tracer) {
|
|
433
|
+
array_desc_->is_tracer = is_tracer;
|
|
434
|
+
}
|
|
435
|
+
// Check if the array is a tracer array
|
|
436
|
+
bool is_tracer() const;
|
|
437
|
+
|
|
438
|
+
void set_data(allocator::Buffer buffer, Deleter d = allocator::free);
|
|
439
|
+
|
|
440
|
+
void set_data(
|
|
441
|
+
allocator::Buffer buffer,
|
|
442
|
+
size_t data_size,
|
|
443
|
+
Strides strides,
|
|
444
|
+
Flags flags,
|
|
445
|
+
Deleter d = allocator::free);
|
|
446
|
+
|
|
447
|
+
void copy_shared_buffer(
|
|
448
|
+
const array& other,
|
|
449
|
+
const Strides& strides,
|
|
450
|
+
Flags flags,
|
|
451
|
+
size_t data_size,
|
|
452
|
+
int64_t offset = 0);
|
|
453
|
+
|
|
454
|
+
void copy_shared_buffer(const array& other);
|
|
455
|
+
|
|
456
|
+
void overwrite_descriptor(const array& other) {
|
|
457
|
+
array_desc_ = other.array_desc_;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
~array();
|
|
461
|
+
|
|
462
|
+
private:
|
|
463
|
+
// Initialize the arrays data
|
|
464
|
+
template <typename It>
|
|
465
|
+
void init(const It src);
|
|
466
|
+
|
|
467
|
+
struct ArrayDesc {
|
|
468
|
+
Shape shape;
|
|
469
|
+
Strides strides;
|
|
470
|
+
size_t size;
|
|
471
|
+
Dtype dtype;
|
|
472
|
+
std::shared_ptr<Primitive> primitive;
|
|
473
|
+
|
|
474
|
+
Status status;
|
|
475
|
+
|
|
476
|
+
// An event on the array used for synchronization
|
|
477
|
+
Event event;
|
|
478
|
+
|
|
479
|
+
// Indicates an array is being used in a graph transform
|
|
480
|
+
// and should not be detached from the graph
|
|
481
|
+
bool is_tracer{false};
|
|
482
|
+
|
|
483
|
+
// This is a shared pointer so that *different* arrays
|
|
484
|
+
// can share the underlying data buffer.
|
|
485
|
+
std::shared_ptr<Data> data;
|
|
486
|
+
|
|
487
|
+
// Offset from beginning of data pointer
|
|
488
|
+
int64_t offset{0};
|
|
489
|
+
|
|
490
|
+
// The size in elements of the data buffer the array accesses
|
|
491
|
+
size_t data_size;
|
|
492
|
+
|
|
493
|
+
// Contains useful meta data about the array
|
|
494
|
+
Flags flags;
|
|
495
|
+
|
|
496
|
+
std::vector<array> inputs;
|
|
497
|
+
// An array to keep track of the siblings from a multi-output
|
|
498
|
+
// primitive.
|
|
499
|
+
std::vector<array> siblings;
|
|
500
|
+
// The arrays position in the output list
|
|
501
|
+
uint32_t position{0};
|
|
502
|
+
|
|
503
|
+
explicit ArrayDesc(Shape shape, Dtype dtype);
|
|
504
|
+
|
|
505
|
+
explicit ArrayDesc(
|
|
506
|
+
Shape shape,
|
|
507
|
+
Dtype dtype,
|
|
508
|
+
std::shared_ptr<Primitive> primitive,
|
|
509
|
+
std::vector<array> inputs);
|
|
510
|
+
|
|
511
|
+
~ArrayDesc();
|
|
512
|
+
|
|
513
|
+
private:
|
|
514
|
+
// Initialize size, strides, and other metadata
|
|
515
|
+
void init();
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
// The ArrayDesc contains the details of the materialized array including the
|
|
519
|
+
// shape, strides, the data type. It also includes
|
|
520
|
+
// the primitive which knows how to compute the array's data from its inputs
|
|
521
|
+
// and the list of array's inputs for the primitive.
|
|
522
|
+
std::shared_ptr<ArrayDesc> array_desc_;
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
template <typename T>
|
|
526
|
+
array::array(T val, Dtype dtype /* = TypeToDtype<T>() */)
|
|
527
|
+
: array_desc_(std::make_shared<ArrayDesc>(Shape{}, dtype)) {
|
|
528
|
+
init(&val);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
template <typename It>
|
|
532
|
+
array::array(
|
|
533
|
+
It data,
|
|
534
|
+
Shape shape,
|
|
535
|
+
Dtype dtype /* = TypeToDtype<typename std::iterator_traits<It>::value_type>() */) :
|
|
536
|
+
array_desc_(std::make_shared<ArrayDesc>(std::move(shape), dtype)) {
|
|
537
|
+
init(data);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
template <typename T>
|
|
541
|
+
array::array(
|
|
542
|
+
std::initializer_list<T> data,
|
|
543
|
+
Dtype dtype /* = TypeToDtype<T>() */)
|
|
544
|
+
: array_desc_(std::make_shared<ArrayDesc>(
|
|
545
|
+
Shape{static_cast<ShapeElem>(data.size())},
|
|
546
|
+
dtype)) {
|
|
547
|
+
init(data.begin());
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
template <typename T>
|
|
551
|
+
array::array(
|
|
552
|
+
std::initializer_list<T> data,
|
|
553
|
+
Shape shape,
|
|
554
|
+
Dtype dtype /* = TypeToDtype<T>() */)
|
|
555
|
+
: array_desc_(std::make_shared<ArrayDesc>(std::move(shape), dtype)) {
|
|
556
|
+
if (data.size() != size()) {
|
|
557
|
+
throw std::invalid_argument(
|
|
558
|
+
"Data size and provided shape mismatch in array construction.");
|
|
559
|
+
}
|
|
560
|
+
init(data.begin());
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
template <typename T>
|
|
564
|
+
T array::item() {
|
|
565
|
+
if (size() != 1) {
|
|
566
|
+
throw std::invalid_argument("item can only be called on arrays of size 1.");
|
|
567
|
+
}
|
|
568
|
+
eval();
|
|
569
|
+
return *data<T>();
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
template <typename T>
|
|
573
|
+
T array::item() const {
|
|
574
|
+
if (size() != 1) {
|
|
575
|
+
throw std::invalid_argument("item can only be called on arrays of size 1.");
|
|
576
|
+
}
|
|
577
|
+
if (status() == Status::unscheduled) {
|
|
578
|
+
throw std::invalid_argument(
|
|
579
|
+
"item() const can only be called on evaled arrays");
|
|
580
|
+
}
|
|
581
|
+
const_cast<array*>(this)->eval();
|
|
582
|
+
return *data<T>();
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
template <typename It>
|
|
586
|
+
void array::init(It src) {
|
|
587
|
+
set_data(allocator::malloc(size() * size_of(dtype())));
|
|
588
|
+
switch (dtype()) {
|
|
589
|
+
case bool_:
|
|
590
|
+
std::copy(src, src + size(), data<bool>());
|
|
591
|
+
break;
|
|
592
|
+
case uint8:
|
|
593
|
+
std::copy(src, src + size(), data<uint8_t>());
|
|
594
|
+
break;
|
|
595
|
+
case uint16:
|
|
596
|
+
std::copy(src, src + size(), data<uint16_t>());
|
|
597
|
+
break;
|
|
598
|
+
case uint32:
|
|
599
|
+
std::copy(src, src + size(), data<uint32_t>());
|
|
600
|
+
break;
|
|
601
|
+
case uint64:
|
|
602
|
+
std::copy(src, src + size(), data<uint64_t>());
|
|
603
|
+
break;
|
|
604
|
+
case int8:
|
|
605
|
+
std::copy(src, src + size(), data<int8_t>());
|
|
606
|
+
break;
|
|
607
|
+
case int16:
|
|
608
|
+
std::copy(src, src + size(), data<int16_t>());
|
|
609
|
+
break;
|
|
610
|
+
case int32:
|
|
611
|
+
std::copy(src, src + size(), data<int32_t>());
|
|
612
|
+
break;
|
|
613
|
+
case int64:
|
|
614
|
+
std::copy(src, src + size(), data<int64_t>());
|
|
615
|
+
break;
|
|
616
|
+
case float16:
|
|
617
|
+
std::copy(src, src + size(), data<float16_t>());
|
|
618
|
+
break;
|
|
619
|
+
case float32:
|
|
620
|
+
std::copy(src, src + size(), data<float>());
|
|
621
|
+
break;
|
|
622
|
+
case float64:
|
|
623
|
+
std::copy(src, src + size(), data<double>());
|
|
624
|
+
break;
|
|
625
|
+
case bfloat16:
|
|
626
|
+
std::copy(src, src + size(), data<bfloat16_t>());
|
|
627
|
+
break;
|
|
628
|
+
case complex64:
|
|
629
|
+
std::copy(src, src + size(), data<complex64_t>());
|
|
630
|
+
break;
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/* Utilities for determining whether a template parameter is array. */
|
|
635
|
+
template <typename T>
|
|
636
|
+
inline constexpr bool is_array_v =
|
|
637
|
+
std::is_same_v<std::remove_cv_t<std::remove_reference_t<T>>, array>;
|
|
638
|
+
|
|
639
|
+
template <typename... T>
|
|
640
|
+
inline constexpr bool is_arrays_v = (is_array_v<T> && ...);
|
|
641
|
+
|
|
642
|
+
template <typename... T>
|
|
643
|
+
using enable_for_arrays_t = typename std::enable_if_t<is_arrays_v<T...>>;
|
|
644
|
+
|
|
645
|
+
} // namespace mlx::core
|