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,380 @@
|
|
|
1
|
+
// Copyright © 2023 Apple Inc.
|
|
2
|
+
|
|
3
|
+
#pragma once
|
|
4
|
+
|
|
5
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
6
|
+
// Metal math for bfloat16
|
|
7
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
8
|
+
|
|
9
|
+
/*
|
|
10
|
+
|
|
11
|
+
Following the Metal Shading Language Specification (Metal 3.1)
|
|
12
|
+
|
|
13
|
+
"bfloat is an extended itypeing point type that only allows implicit conversion
|
|
14
|
+
to a type of greater itypeing point rank. While bfloat can be implicitly
|
|
15
|
+
converted to itype, it cannot be implicitly converted to half, and neither
|
|
16
|
+
itype nor half can be implicitly converted to bfloat."
|
|
17
|
+
|
|
18
|
+
Further, as far as I can tell, the stdlib math/simd functions are not defined
|
|
19
|
+
for bfloat and calling with an argument of type bfloat will result in that
|
|
20
|
+
argument getting implicitly converted to itype which then returns an output
|
|
21
|
+
that is (likely) a itype which cannot be implicitly converted into a bfloat
|
|
22
|
+
|
|
23
|
+
This leads to situations where
|
|
24
|
+
bfloat a = 5.0bf;
|
|
25
|
+
bfloat b = metal::abs(a); // this will throw an error since abs return itype
|
|
26
|
+
bfloat c = static_cast<bfloat>(metal::abs(a)); // this is fine
|
|
27
|
+
|
|
28
|
+
For the moment, I will be adding overloaded instantiations of the math
|
|
29
|
+
functions to accordingly automatically handle the casting
|
|
30
|
+
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
#define instantiate_metal_math_funcs(itype, otype, ctype, mfast) \
|
|
34
|
+
\
|
|
35
|
+
METAL_FUNC otype abs(itype x) { \
|
|
36
|
+
return static_cast<otype>(__metal_fabs(static_cast<ctype>(x), mfast)); \
|
|
37
|
+
} \
|
|
38
|
+
METAL_FUNC otype acos(itype x) { \
|
|
39
|
+
return static_cast<otype>(__metal_acos(static_cast<ctype>(x), mfast)); \
|
|
40
|
+
} \
|
|
41
|
+
METAL_FUNC otype acosh(itype x) { \
|
|
42
|
+
return static_cast<otype>(__metal_acosh(static_cast<ctype>(x), mfast)); \
|
|
43
|
+
} \
|
|
44
|
+
METAL_FUNC otype asin(itype x) { \
|
|
45
|
+
return static_cast<otype>(__metal_asin(static_cast<ctype>(x), mfast)); \
|
|
46
|
+
} \
|
|
47
|
+
METAL_FUNC otype asinh(itype x) { \
|
|
48
|
+
return static_cast<otype>(__metal_asinh(static_cast<ctype>(x), mfast)); \
|
|
49
|
+
} \
|
|
50
|
+
METAL_FUNC otype atan(itype y_over_x) { \
|
|
51
|
+
return static_cast<otype>( \
|
|
52
|
+
__metal_atan(static_cast<ctype>(y_over_x), mfast)); \
|
|
53
|
+
} \
|
|
54
|
+
METAL_FUNC otype atan2(itype y, itype x) { \
|
|
55
|
+
return static_cast<otype>( \
|
|
56
|
+
__metal_atan2(static_cast<ctype>(y), static_cast<ctype>(x), mfast)); \
|
|
57
|
+
} \
|
|
58
|
+
METAL_FUNC otype atanh(itype x) { \
|
|
59
|
+
return static_cast<otype>(__metal_atanh(static_cast<ctype>(x), mfast)); \
|
|
60
|
+
} \
|
|
61
|
+
METAL_FUNC otype ceil(itype x) { \
|
|
62
|
+
return static_cast<otype>(__metal_ceil(static_cast<ctype>(x), mfast)); \
|
|
63
|
+
} \
|
|
64
|
+
METAL_FUNC otype cos(itype x) { \
|
|
65
|
+
return static_cast<otype>(__metal_cos(static_cast<ctype>(x), mfast)); \
|
|
66
|
+
} \
|
|
67
|
+
METAL_FUNC otype cosh(itype x) { \
|
|
68
|
+
return static_cast<otype>(__metal_cosh(static_cast<ctype>(x), mfast)); \
|
|
69
|
+
} \
|
|
70
|
+
METAL_FUNC otype cospi(itype x) { \
|
|
71
|
+
return static_cast<otype>(__metal_cospi(static_cast<ctype>(x), mfast)); \
|
|
72
|
+
} \
|
|
73
|
+
METAL_FUNC otype divide(itype x, itype y) { \
|
|
74
|
+
return static_cast<otype>( \
|
|
75
|
+
__metal_divide(static_cast<ctype>(x), static_cast<ctype>(y), mfast)); \
|
|
76
|
+
} \
|
|
77
|
+
METAL_FUNC otype exp(itype x) { \
|
|
78
|
+
return static_cast<otype>(__metal_exp(static_cast<ctype>(x), mfast)); \
|
|
79
|
+
} \
|
|
80
|
+
METAL_FUNC otype exp10(itype x) { \
|
|
81
|
+
return static_cast<otype>(__metal_exp10(static_cast<ctype>(x), mfast)); \
|
|
82
|
+
} \
|
|
83
|
+
METAL_FUNC otype exp2(itype x) { \
|
|
84
|
+
return static_cast<otype>(__metal_exp2(static_cast<ctype>(x), mfast)); \
|
|
85
|
+
} \
|
|
86
|
+
METAL_FUNC otype fabs(itype x) { \
|
|
87
|
+
return static_cast<otype>(__metal_fabs(static_cast<ctype>(x), mfast)); \
|
|
88
|
+
} \
|
|
89
|
+
METAL_FUNC otype fdim(itype x, itype y) { \
|
|
90
|
+
ctype t = static_cast<ctype>(x - y); \
|
|
91
|
+
return static_cast<otype>(select(t, ctype(0), t < ctype(0) || x == y)); \
|
|
92
|
+
} \
|
|
93
|
+
METAL_FUNC otype floor(itype x) { \
|
|
94
|
+
return static_cast<otype>(__metal_floor(static_cast<ctype>(x), mfast)); \
|
|
95
|
+
} \
|
|
96
|
+
METAL_FUNC otype fma(itype x, itype y, itype z) { \
|
|
97
|
+
return static_cast<otype>(__metal_fma( \
|
|
98
|
+
static_cast<ctype>(x), static_cast<ctype>(y), static_cast<ctype>(z))); \
|
|
99
|
+
} \
|
|
100
|
+
METAL_FUNC otype fmax(itype x, itype y) { \
|
|
101
|
+
return static_cast<otype>( \
|
|
102
|
+
__metal_fmax(static_cast<ctype>(x), static_cast<ctype>(y), mfast)); \
|
|
103
|
+
} \
|
|
104
|
+
METAL_FUNC otype fmax3(itype x, itype y, itype z) { \
|
|
105
|
+
return static_cast<otype>(__metal_fmax3( \
|
|
106
|
+
static_cast<ctype>(x), \
|
|
107
|
+
static_cast<ctype>(y), \
|
|
108
|
+
static_cast<ctype>(z), \
|
|
109
|
+
mfast)); \
|
|
110
|
+
} \
|
|
111
|
+
METAL_FUNC otype fmedian3(itype x, itype y, itype z) { \
|
|
112
|
+
return static_cast<otype>(__metal_fmedian3( \
|
|
113
|
+
static_cast<ctype>(x), \
|
|
114
|
+
static_cast<ctype>(y), \
|
|
115
|
+
static_cast<ctype>(z), \
|
|
116
|
+
mfast)); \
|
|
117
|
+
} \
|
|
118
|
+
METAL_FUNC otype fmin(itype x, itype y) { \
|
|
119
|
+
return static_cast<otype>( \
|
|
120
|
+
__metal_fmin(static_cast<ctype>(x), static_cast<ctype>(y), mfast)); \
|
|
121
|
+
} \
|
|
122
|
+
METAL_FUNC otype fmin3(itype x, itype y, itype z) { \
|
|
123
|
+
return static_cast<otype>(__metal_fmin3( \
|
|
124
|
+
static_cast<ctype>(x), \
|
|
125
|
+
static_cast<ctype>(y), \
|
|
126
|
+
static_cast<ctype>(z), \
|
|
127
|
+
mfast)); \
|
|
128
|
+
} \
|
|
129
|
+
METAL_FUNC otype fmod(itype x, itype y) { \
|
|
130
|
+
return static_cast<otype>( \
|
|
131
|
+
__metal_fmod(static_cast<ctype>(x), static_cast<ctype>(y), mfast)); \
|
|
132
|
+
} \
|
|
133
|
+
METAL_FUNC otype fract(itype x) { \
|
|
134
|
+
return static_cast<otype>(__metal_fract(static_cast<ctype>(x), mfast)); \
|
|
135
|
+
} \
|
|
136
|
+
METAL_FUNC otype frexp(itype x, thread int& exp) { \
|
|
137
|
+
return static_cast<otype>(__metal_frexp(static_cast<ctype>(x), &exp)); \
|
|
138
|
+
} \
|
|
139
|
+
METAL_FUNC otype ldexp(itype x, int k) { \
|
|
140
|
+
return static_cast<otype>(__metal_ldexp(static_cast<ctype>(x), k, mfast)); \
|
|
141
|
+
} \
|
|
142
|
+
METAL_FUNC otype log(itype x) { \
|
|
143
|
+
return static_cast<otype>(__metal_log(static_cast<ctype>(x), mfast)); \
|
|
144
|
+
} \
|
|
145
|
+
METAL_FUNC otype log10(itype x) { \
|
|
146
|
+
return static_cast<otype>(__metal_log10(static_cast<ctype>(x), mfast)); \
|
|
147
|
+
} \
|
|
148
|
+
METAL_FUNC otype log2(itype x) { \
|
|
149
|
+
return static_cast<otype>(__metal_log2(static_cast<ctype>(x), mfast)); \
|
|
150
|
+
} \
|
|
151
|
+
METAL_FUNC otype max(itype x, itype y) { \
|
|
152
|
+
return static_cast<otype>( \
|
|
153
|
+
__metal_fmax(static_cast<ctype>(x), static_cast<ctype>(y), mfast)); \
|
|
154
|
+
} \
|
|
155
|
+
METAL_FUNC otype max3(itype x, itype y, itype z) { \
|
|
156
|
+
return static_cast<otype>(__metal_fmax3( \
|
|
157
|
+
static_cast<ctype>(x), \
|
|
158
|
+
static_cast<ctype>(y), \
|
|
159
|
+
static_cast<ctype>(z), \
|
|
160
|
+
mfast)); \
|
|
161
|
+
} \
|
|
162
|
+
METAL_FUNC otype median3(itype x, itype y, itype z) { \
|
|
163
|
+
return static_cast<otype>(__metal_fmedian3( \
|
|
164
|
+
static_cast<ctype>(x), \
|
|
165
|
+
static_cast<ctype>(y), \
|
|
166
|
+
static_cast<ctype>(z), \
|
|
167
|
+
mfast)); \
|
|
168
|
+
} \
|
|
169
|
+
METAL_FUNC otype min(itype x, itype y) { \
|
|
170
|
+
return static_cast<otype>( \
|
|
171
|
+
__metal_fmin(static_cast<ctype>(x), static_cast<ctype>(y), mfast)); \
|
|
172
|
+
} \
|
|
173
|
+
METAL_FUNC otype min3(itype x, itype y, itype z) { \
|
|
174
|
+
return static_cast<otype>(__metal_fmin3( \
|
|
175
|
+
static_cast<ctype>(x), \
|
|
176
|
+
static_cast<ctype>(y), \
|
|
177
|
+
static_cast<ctype>(z), \
|
|
178
|
+
mfast)); \
|
|
179
|
+
} \
|
|
180
|
+
METAL_FUNC otype nextafter(itype x, itype y) { \
|
|
181
|
+
return static_cast<otype>( \
|
|
182
|
+
__metal_nextafter(static_cast<ctype>(x), static_cast<ctype>(y))); \
|
|
183
|
+
} \
|
|
184
|
+
METAL_FUNC otype pow(itype x, itype y) { \
|
|
185
|
+
return static_cast<otype>( \
|
|
186
|
+
__metal_pow(static_cast<ctype>(x), static_cast<ctype>(y), mfast)); \
|
|
187
|
+
} \
|
|
188
|
+
METAL_FUNC otype powr(itype x, itype y) { \
|
|
189
|
+
return static_cast<otype>( \
|
|
190
|
+
__metal_powr(static_cast<ctype>(x), static_cast<ctype>(y), mfast)); \
|
|
191
|
+
} \
|
|
192
|
+
METAL_FUNC otype rint(itype x) { \
|
|
193
|
+
return static_cast<otype>(__metal_rint(static_cast<ctype>(x), mfast)); \
|
|
194
|
+
} \
|
|
195
|
+
METAL_FUNC otype round(itype x) { \
|
|
196
|
+
return static_cast<otype>(__metal_round(static_cast<ctype>(x), mfast)); \
|
|
197
|
+
} \
|
|
198
|
+
METAL_FUNC otype rsqrt(itype x) { \
|
|
199
|
+
return static_cast<otype>(__metal_rsqrt(static_cast<ctype>(x), mfast)); \
|
|
200
|
+
} \
|
|
201
|
+
METAL_FUNC otype sin(itype x) { \
|
|
202
|
+
return static_cast<otype>(__metal_sin(static_cast<ctype>(x), mfast)); \
|
|
203
|
+
} \
|
|
204
|
+
METAL_FUNC otype sinh(itype x) { \
|
|
205
|
+
return static_cast<otype>(__metal_sinh(static_cast<ctype>(x), mfast)); \
|
|
206
|
+
} \
|
|
207
|
+
METAL_FUNC otype sinpi(itype x) { \
|
|
208
|
+
return static_cast<otype>(__metal_sinpi(static_cast<ctype>(x), mfast)); \
|
|
209
|
+
} \
|
|
210
|
+
METAL_FUNC otype sqrt(itype x) { \
|
|
211
|
+
return static_cast<otype>(__metal_sqrt(static_cast<ctype>(x), mfast)); \
|
|
212
|
+
} \
|
|
213
|
+
METAL_FUNC otype tan(itype x) { \
|
|
214
|
+
return static_cast<otype>(__metal_tan(static_cast<ctype>(x), mfast)); \
|
|
215
|
+
} \
|
|
216
|
+
METAL_FUNC otype tanh(itype x) { \
|
|
217
|
+
return static_cast<otype>(__metal_tanh(static_cast<ctype>(x), mfast)); \
|
|
218
|
+
} \
|
|
219
|
+
METAL_FUNC otype tanpi(itype x) { \
|
|
220
|
+
return static_cast<otype>(__metal_tanpi(static_cast<ctype>(x), mfast)); \
|
|
221
|
+
} \
|
|
222
|
+
METAL_FUNC otype trunc(itype x) { \
|
|
223
|
+
return static_cast<otype>(__metal_trunc(static_cast<ctype>(x), mfast)); \
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
namespace metal {
|
|
227
|
+
|
|
228
|
+
instantiate_metal_math_funcs(
|
|
229
|
+
bfloat16_t,
|
|
230
|
+
bfloat16_t,
|
|
231
|
+
float,
|
|
232
|
+
__METAL_MAYBE_FAST_MATH__);
|
|
233
|
+
|
|
234
|
+
namespace fast {
|
|
235
|
+
|
|
236
|
+
instantiate_metal_math_funcs(
|
|
237
|
+
bfloat16_t,
|
|
238
|
+
bfloat16_t,
|
|
239
|
+
float,
|
|
240
|
+
__METAL_FAST_MATH__);
|
|
241
|
+
|
|
242
|
+
} // namespace fast
|
|
243
|
+
|
|
244
|
+
namespace precise {
|
|
245
|
+
|
|
246
|
+
instantiate_metal_math_funcs(
|
|
247
|
+
bfloat16_t,
|
|
248
|
+
bfloat16_t,
|
|
249
|
+
float,
|
|
250
|
+
__METAL_PRECISE_MATH__);
|
|
251
|
+
|
|
252
|
+
} // namespace precise
|
|
253
|
+
|
|
254
|
+
} // namespace metal
|
|
255
|
+
|
|
256
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
257
|
+
// Metal simd for bfloat16
|
|
258
|
+
///////////////////////////////////////////////////////////////////////////////
|
|
259
|
+
|
|
260
|
+
#define instantiate_metal_simd_comm_funcs( \
|
|
261
|
+
itype, otype, ctype, itype_to_ctype, ctype_to_otype) \
|
|
262
|
+
\
|
|
263
|
+
METAL_FUNC otype simd_broadcast(itype data, ushort broadcast_lane_id) { \
|
|
264
|
+
return ctype_to_otype( \
|
|
265
|
+
__metal_simd_broadcast(itype_to_ctype(data), broadcast_lane_id)); \
|
|
266
|
+
} \
|
|
267
|
+
\
|
|
268
|
+
METAL_FUNC otype simd_shuffle(itype data, ushort simd_lane_id) { \
|
|
269
|
+
return ctype_to_otype( \
|
|
270
|
+
__metal_simd_shuffle(itype_to_ctype(data), simd_lane_id)); \
|
|
271
|
+
} \
|
|
272
|
+
\
|
|
273
|
+
METAL_FUNC otype simd_shuffle_and_fill_down( \
|
|
274
|
+
itype data, itype filling_data, ushort delta, ushort modulo) { \
|
|
275
|
+
return ctype_to_otype(__metal_simd_shuffle_and_fill_down( \
|
|
276
|
+
itype_to_ctype(data), itype_to_ctype(filling_data), delta, modulo)); \
|
|
277
|
+
} \
|
|
278
|
+
\
|
|
279
|
+
METAL_FUNC otype simd_shuffle_and_fill_down( \
|
|
280
|
+
itype data, itype filling_data, ushort delta) { \
|
|
281
|
+
return ctype_to_otype(__metal_simd_shuffle_and_fill_down( \
|
|
282
|
+
itype_to_ctype(data), \
|
|
283
|
+
itype_to_ctype(filling_data), \
|
|
284
|
+
delta, \
|
|
285
|
+
__metal_get_simdgroup_size(ushort()))); \
|
|
286
|
+
} \
|
|
287
|
+
\
|
|
288
|
+
METAL_FUNC otype simd_shuffle_and_fill_up( \
|
|
289
|
+
itype data, itype filling_data, ushort delta, ushort modulo) { \
|
|
290
|
+
return ctype_to_otype(__metal_simd_shuffle_and_fill_up( \
|
|
291
|
+
itype_to_ctype(data), itype_to_ctype(filling_data), delta, modulo)); \
|
|
292
|
+
} \
|
|
293
|
+
\
|
|
294
|
+
METAL_FUNC otype simd_shuffle_and_fill_up( \
|
|
295
|
+
itype data, itype filling_data, ushort delta) { \
|
|
296
|
+
return ctype_to_otype(__metal_simd_shuffle_and_fill_up( \
|
|
297
|
+
itype_to_ctype(data), \
|
|
298
|
+
itype_to_ctype(filling_data), \
|
|
299
|
+
delta, \
|
|
300
|
+
__metal_get_simdgroup_size(ushort()))); \
|
|
301
|
+
} \
|
|
302
|
+
\
|
|
303
|
+
METAL_FUNC otype simd_shuffle_down(itype data, ushort delta) { \
|
|
304
|
+
return ctype_to_otype( \
|
|
305
|
+
__metal_simd_shuffle_down(itype_to_ctype(data), delta)); \
|
|
306
|
+
} \
|
|
307
|
+
\
|
|
308
|
+
METAL_FUNC otype simd_shuffle_rotate_down(itype data, ushort delta) { \
|
|
309
|
+
return ctype_to_otype( \
|
|
310
|
+
__metal_simd_shuffle_rotate_down(itype_to_ctype(data), delta)); \
|
|
311
|
+
} \
|
|
312
|
+
\
|
|
313
|
+
METAL_FUNC otype simd_shuffle_rotate_up(itype data, ushort delta) { \
|
|
314
|
+
return ctype_to_otype( \
|
|
315
|
+
__metal_simd_shuffle_rotate_up(itype_to_ctype(data), delta)); \
|
|
316
|
+
} \
|
|
317
|
+
\
|
|
318
|
+
METAL_FUNC otype simd_shuffle_up(itype data, ushort delta) { \
|
|
319
|
+
return ctype_to_otype( \
|
|
320
|
+
__metal_simd_shuffle_up(itype_to_ctype(data), delta)); \
|
|
321
|
+
} \
|
|
322
|
+
\
|
|
323
|
+
METAL_FUNC otype simd_shuffle_xor(itype data, ushort mask) { \
|
|
324
|
+
return ctype_to_otype( \
|
|
325
|
+
__metal_simd_shuffle_xor(itype_to_ctype(data), mask)); \
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
#define instantiate_metal_simd_reduction_funcs(itype, otype, ctype) \
|
|
329
|
+
\
|
|
330
|
+
METAL_FUNC otype simd_max(itype data) { \
|
|
331
|
+
return static_cast<otype>(__metal_simd_max(static_cast<ctype>(data))); \
|
|
332
|
+
} \
|
|
333
|
+
\
|
|
334
|
+
METAL_FUNC otype simd_min(itype data) { \
|
|
335
|
+
return static_cast<otype>(__metal_simd_min(static_cast<ctype>(data))); \
|
|
336
|
+
} \
|
|
337
|
+
\
|
|
338
|
+
METAL_FUNC otype simd_prefix_exclusive_product(itype data) { \
|
|
339
|
+
return static_cast<otype>( \
|
|
340
|
+
__metal_simd_prefix_exclusive_product(static_cast<ctype>(data))); \
|
|
341
|
+
} \
|
|
342
|
+
\
|
|
343
|
+
METAL_FUNC otype simd_prefix_exclusive_sum(itype data) { \
|
|
344
|
+
return static_cast<otype>( \
|
|
345
|
+
__metal_simd_prefix_exclusive_sum(static_cast<ctype>(data))); \
|
|
346
|
+
} \
|
|
347
|
+
\
|
|
348
|
+
METAL_FUNC otype simd_prefix_inclusive_product(itype data) { \
|
|
349
|
+
return static_cast<otype>( \
|
|
350
|
+
__metal_simd_prefix_inclusive_product(static_cast<ctype>(data))); \
|
|
351
|
+
} \
|
|
352
|
+
\
|
|
353
|
+
METAL_FUNC otype simd_prefix_inclusive_sum(itype data) { \
|
|
354
|
+
return static_cast<otype>( \
|
|
355
|
+
__metal_simd_prefix_inclusive_sum(static_cast<ctype>(data))); \
|
|
356
|
+
} \
|
|
357
|
+
\
|
|
358
|
+
METAL_FUNC otype simd_product(itype data) { \
|
|
359
|
+
return static_cast<otype>(__metal_simd_product(static_cast<ctype>(data))); \
|
|
360
|
+
} \
|
|
361
|
+
\
|
|
362
|
+
METAL_FUNC otype simd_sum(itype data) { \
|
|
363
|
+
return static_cast<otype>(__metal_simd_sum(static_cast<ctype>(data))); \
|
|
364
|
+
} \
|
|
365
|
+
\
|
|
366
|
+
METAL_FUNC otype simd_xor(itype data) { \
|
|
367
|
+
return static_cast<otype>(__metal_simd_xor(static_cast<ctype>(data))); \
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
namespace metal {
|
|
371
|
+
|
|
372
|
+
instantiate_metal_simd_comm_funcs(
|
|
373
|
+
bfloat16_t,
|
|
374
|
+
bfloat16_t,
|
|
375
|
+
uint16_t,
|
|
376
|
+
bfloat16_to_uint16,
|
|
377
|
+
uint16_to_bfloat16);
|
|
378
|
+
instantiate_metal_simd_reduction_funcs(bfloat16_t, bfloat16_t, float);
|
|
379
|
+
|
|
380
|
+
} // namespace metal
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// Copyright © 2024 Apple Inc.
|
|
2
|
+
|
|
3
|
+
template <typename T, typename U, typename Op>
|
|
4
|
+
[[kernel]] void binary_ss(
|
|
5
|
+
device const T* a,
|
|
6
|
+
device const T* b,
|
|
7
|
+
device U* c,
|
|
8
|
+
uint index [[thread_position_in_grid]]) {
|
|
9
|
+
c[index] = Op()(a[0], b[0]);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
template <typename T, typename U, typename Op, int N = WorkPerThread<T>::n>
|
|
13
|
+
[[kernel]] void binary_sv(
|
|
14
|
+
device const T* a,
|
|
15
|
+
device const T* b,
|
|
16
|
+
device U* c,
|
|
17
|
+
constant uint& size,
|
|
18
|
+
uint index [[thread_position_in_grid]]) {
|
|
19
|
+
index *= N;
|
|
20
|
+
if (N > 1 && index + N > size) {
|
|
21
|
+
for (int i = 0; index + i < size; ++i) {
|
|
22
|
+
c[index + i] = Op()(a[0], b[index + i]);
|
|
23
|
+
}
|
|
24
|
+
} else {
|
|
25
|
+
for (int i = 0; i < N; ++i) {
|
|
26
|
+
c[index + i] = Op()(a[0], b[index + i]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
template <typename T, typename U, typename Op, int N = WorkPerThread<T>::n>
|
|
32
|
+
[[kernel]] void binary_vs(
|
|
33
|
+
device const T* a,
|
|
34
|
+
device const T* b,
|
|
35
|
+
device U* c,
|
|
36
|
+
constant uint& size,
|
|
37
|
+
uint index [[thread_position_in_grid]]) {
|
|
38
|
+
index *= N;
|
|
39
|
+
if (N > 1 && index + N > size) {
|
|
40
|
+
for (int i = 0; index + i < size; ++i) {
|
|
41
|
+
c[index + i] = Op()(a[index + i], b[0]);
|
|
42
|
+
}
|
|
43
|
+
} else {
|
|
44
|
+
for (int i = 0; i < N; ++i) {
|
|
45
|
+
c[index + i] = Op()(a[index + i], b[0]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
template <typename T, typename U, typename Op, int N = WorkPerThread<T>::n>
|
|
51
|
+
[[kernel]] void binary_vv(
|
|
52
|
+
device const T* a,
|
|
53
|
+
device const T* b,
|
|
54
|
+
device U* c,
|
|
55
|
+
constant uint& size,
|
|
56
|
+
uint index [[thread_position_in_grid]]) {
|
|
57
|
+
index *= N;
|
|
58
|
+
if (N > 1 && index + N > size) {
|
|
59
|
+
for (int i = 0; index + i < size; ++i) {
|
|
60
|
+
c[index + i] = Op()(a[index + i], b[index + i]);
|
|
61
|
+
}
|
|
62
|
+
} else {
|
|
63
|
+
for (int i = 0; i < N; ++i) {
|
|
64
|
+
c[index + i] = Op()(a[index + i], b[index + i]);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
template <typename T, typename U, typename Op, int N = WorkPerThread<T>::n>
|
|
70
|
+
[[kernel]] void binary_sv2(
|
|
71
|
+
device const T* a,
|
|
72
|
+
device const T* b,
|
|
73
|
+
device U* c,
|
|
74
|
+
constant int64_t& size,
|
|
75
|
+
uint2 index [[thread_position_in_grid]],
|
|
76
|
+
uint2 grid_dim [[threads_per_grid]]) {
|
|
77
|
+
int64_t offset = N * (index.x + grid_dim.x * int64_t(index.y));
|
|
78
|
+
if (N > 1 && offset + N > size) {
|
|
79
|
+
for (int i = 0; offset + i < size; ++i) {
|
|
80
|
+
c[offset + i] = Op()(a[0], b[offset + i]);
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
for (int i = 0; i < N; ++i) {
|
|
84
|
+
c[offset + i] = Op()(a[0], b[offset + i]);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
template <typename T, typename U, typename Op, int N = WorkPerThread<T>::n>
|
|
90
|
+
[[kernel]] void binary_vs2(
|
|
91
|
+
device const T* a,
|
|
92
|
+
device const T* b,
|
|
93
|
+
device U* c,
|
|
94
|
+
constant int64_t& size,
|
|
95
|
+
uint2 index [[thread_position_in_grid]],
|
|
96
|
+
uint2 grid_dim [[threads_per_grid]]) {
|
|
97
|
+
int64_t offset = N * (index.x + grid_dim.x * int64_t(index.y));
|
|
98
|
+
if (N > 1 && offset + N > size) {
|
|
99
|
+
for (int i = 0; offset + i < size; ++i) {
|
|
100
|
+
c[offset + i] = Op()(a[offset + i], b[0]);
|
|
101
|
+
}
|
|
102
|
+
} else {
|
|
103
|
+
for (int i = 0; i < N; ++i) {
|
|
104
|
+
c[offset + i] = Op()(a[offset + i], b[0]);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
template <typename T, typename U, typename Op, int N = WorkPerThread<T>::n>
|
|
110
|
+
[[kernel]] void binary_vv2(
|
|
111
|
+
device const T* a,
|
|
112
|
+
device const T* b,
|
|
113
|
+
device U* c,
|
|
114
|
+
constant int64_t& size,
|
|
115
|
+
uint2 index [[thread_position_in_grid]],
|
|
116
|
+
uint2 grid_dim [[threads_per_grid]]) {
|
|
117
|
+
int64_t offset = N * (index.x + grid_dim.x * int64_t(index.y));
|
|
118
|
+
if (N > 1 && offset + N > size) {
|
|
119
|
+
for (int i = 0; offset + i < size; ++i) {
|
|
120
|
+
c[offset + i] = Op()(a[offset + i], b[offset + i]);
|
|
121
|
+
}
|
|
122
|
+
} else {
|
|
123
|
+
for (int i = 0; i < N; ++i) {
|
|
124
|
+
c[offset + i] = Op()(a[offset + i], b[offset + i]);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
template <typename T, typename U, typename Op, typename IdxT = int64_t>
|
|
130
|
+
[[kernel]] void binary_g_nd1(
|
|
131
|
+
device const T* a,
|
|
132
|
+
device const T* b,
|
|
133
|
+
device U* c,
|
|
134
|
+
constant const int64_t& a_stride,
|
|
135
|
+
constant const int64_t& b_stride,
|
|
136
|
+
uint index [[thread_position_in_grid]]) {
|
|
137
|
+
auto a_idx = elem_to_loc_1<IdxT>(index, a_stride);
|
|
138
|
+
auto b_idx = elem_to_loc_1<IdxT>(index, b_stride);
|
|
139
|
+
c[index] = Op()(a[a_idx], b[b_idx]);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
template <typename T, typename U, typename Op, typename IdxT = int64_t>
|
|
143
|
+
[[kernel]] void binary_g_nd2(
|
|
144
|
+
device const T* a,
|
|
145
|
+
device const T* b,
|
|
146
|
+
device U* c,
|
|
147
|
+
constant const int64_t a_strides[2],
|
|
148
|
+
constant const int64_t b_strides[2],
|
|
149
|
+
uint2 index [[thread_position_in_grid]],
|
|
150
|
+
uint2 grid_dim [[threads_per_grid]]) {
|
|
151
|
+
auto a_idx = elem_to_loc_2<IdxT>(index, a_strides);
|
|
152
|
+
auto b_idx = elem_to_loc_2<IdxT>(index, b_strides);
|
|
153
|
+
IdxT out_idx = index.x + IdxT(grid_dim.x) * index.y;
|
|
154
|
+
c[out_idx] = Op()(a[a_idx], b[b_idx]);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
template <typename T, typename U, typename Op, typename IdxT = int64_t>
|
|
158
|
+
[[kernel]] void binary_g_nd3(
|
|
159
|
+
device const T* a,
|
|
160
|
+
device const T* b,
|
|
161
|
+
device U* c,
|
|
162
|
+
constant const int64_t a_strides[3],
|
|
163
|
+
constant const int64_t b_strides[3],
|
|
164
|
+
uint3 index [[thread_position_in_grid]],
|
|
165
|
+
uint3 grid_dim [[threads_per_grid]]) {
|
|
166
|
+
auto a_idx = elem_to_loc_3<IdxT>(index, a_strides);
|
|
167
|
+
auto b_idx = elem_to_loc_3<IdxT>(index, b_strides);
|
|
168
|
+
IdxT out_idx = index.x + grid_dim.x * (index.y + IdxT(grid_dim.y) * index.z);
|
|
169
|
+
c[out_idx] = Op()(a[a_idx], b[b_idx]);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
template <
|
|
173
|
+
typename T,
|
|
174
|
+
typename U,
|
|
175
|
+
typename Op,
|
|
176
|
+
int N = 1,
|
|
177
|
+
typename IdxT = int64_t>
|
|
178
|
+
[[kernel]] void binary_g(
|
|
179
|
+
device const T* a,
|
|
180
|
+
device const T* b,
|
|
181
|
+
device U* c,
|
|
182
|
+
constant const int* shape,
|
|
183
|
+
constant const int64_t* a_strides,
|
|
184
|
+
constant const int64_t* b_strides,
|
|
185
|
+
constant const int& ndim,
|
|
186
|
+
uint3 index [[thread_position_in_grid]],
|
|
187
|
+
uint3 grid_dim [[threads_per_grid]]) {
|
|
188
|
+
auto idx = elem_to_loc_2_nd<IdxT>(
|
|
189
|
+
{N * index.x, index.y, index.z}, shape, a_strides, b_strides, ndim);
|
|
190
|
+
auto xshape = shape[ndim - 1];
|
|
191
|
+
IdxT out_idx = N * index.x + xshape * (index.y + IdxT(grid_dim.y) * index.z);
|
|
192
|
+
IdxT a_xstride = a_strides[ndim - 1];
|
|
193
|
+
IdxT b_xstride = b_strides[ndim - 1];
|
|
194
|
+
for (int i = 0; i < N && (int(N * index.x) + i) < xshape; ++i) {
|
|
195
|
+
c[out_idx++] = Op()(a[idx.x], b[idx.y]);
|
|
196
|
+
idx.x += a_xstride;
|
|
197
|
+
idx.y += b_xstride;
|
|
198
|
+
}
|
|
199
|
+
}
|