Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
175 lines (161 loc) · 6.42 KB
/
Copy pathsetup.py
File metadata and controls
175 lines (161 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import os
import subprocess
import sys
from setuptools import setup
CUSTOM_KERNEL_FLAG = "--with-custom-kernel"
TRUTHY = {"1", "true", "yes", "on"}
DEFAULT_CUSTOM_KERNEL_DEPLOYMENT_TARGET = "15.0"
def _with_custom_kernel() -> bool:
if CUSTOM_KERNEL_FLAG in sys.argv:
sys.argv.remove(CUSTOM_KERNEL_FLAG)
return True
return os.environ.get("OMLX_WITH_CUSTOM_KERNEL", "").strip().lower() in TRUTHY
def _custom_kernel_build_kwargs() -> dict:
if not _with_custom_kernel():
return {}
target = (
os.environ.get("OMLX_CUSTOM_KERNEL_DEPLOYMENT_TARGET")
or os.environ.get("MACOSX_DEPLOYMENT_TARGET")
or DEFAULT_CUSTOM_KERNEL_DEPLOYMENT_TARGET
)
os.environ.setdefault("MACOSX_DEPLOYMENT_TARGET", target)
cmake_args = os.environ.get("CMAKE_ARGS", "").strip()
if "CMAKE_OSX_DEPLOYMENT_TARGET" not in cmake_args:
target_arg = f"-DCMAKE_OSX_DEPLOYMENT_TARGET={target}"
os.environ["CMAKE_ARGS"] = (
f"{cmake_args} {target_arg}".strip() if cmake_args else target_arg
)
from mlx import extension
extension._HOST_SOURCE_SUFFIXES.add(".mm")
root = "omlx/custom_kernels"
shared = f"{root}/include"
attention = f"{root}/attention/csrc"
lowbit = f"{root}/lowbit/csrc"
quant = f"{root}/quant_matmul/csrc"
sparse = f"{root}/sparse_index/csrc"
quant_libraries = [
extension.MetalLibrary(
"omlx_quant_matmul_kernels",
sources=[
f"{quant}/affine_qmm.metal",
f"{quant}/gather_qmm.metal",
f"{quant}/head_flat_qmm.metal",
f"{quant}/moe_weighted_sum.metal",
f"{quant}/mxfp8_qmv.metal",
f"{quant}/oq_a8.metal",
f"{quant}/ane/ane_hybrid.metal",
],
),
]
sdk_version = subprocess.check_output(
["xcrun", "-sdk", "macosx", "--show-sdk-version"], text=True
).strip()
if tuple(int(part) for part in sdk_version.split(".")) >= (26, 2):
quant_libraries.append(
extension.MetalLibrary(
"omlx_quant_matmul_kernels_nax",
sources=[
f"{quant}/affine_qmm_nax.metal",
f"{quant}/oq_a8_nax.metal",
f"{quant}/oq_q8_a8_nax.metal",
],
deployment_target="26.2",
)
)
return {
"ext_modules": [
extension.MetalExtension(
"omlx.custom_kernels.attention._ext",
sources=[
f"{attention}/bindings.cpp",
f"{attention}/block_mask_attention.cpp",
f"{attention}/compressed_sparse_attention.cpp",
f"{attention}/fa256_attention.cpp",
f"{attention}/kv_roll.cpp",
f"{attention}/packed_sparse_attention.cpp",
f"{attention}/sdpa_decode.cpp",
f"{attention}/sparse_gqa_attention.cpp",
f"{attention}/sparse_mla.cpp",
f"{attention}/verify_gemm.cpp",
],
include_dirs=[attention, shared],
metal_libraries=[
extension.MetalLibrary(
"omlx_attention_kernels",
sources=[
f"{attention}/block_mask_attention.metal",
f"{attention}/compressed_sparse_attention.metal",
f"{attention}/fa256_attention.metal",
f"{attention}/kv_roll.metal",
f"{attention}/packed_sparse_attention.metal",
f"{attention}/sdpa_decode.metal",
f"{attention}/sparse_gqa_attention.metal",
f"{attention}/sparse_mla.metal",
f"{attention}/verify_gemm.metal",
],
),
],
),
extension.MetalExtension(
"omlx.custom_kernels.lowbit._ext",
sources=[f"{lowbit}/bindings.cpp", f"{lowbit}/lowbit_qmv.cpp"],
include_dirs=[lowbit, shared],
metal_libraries=[
extension.MetalLibrary(
"omlx_lowbit_kernels",
sources=[
f"{lowbit}/lowbit_qmv.metal",
],
),
],
),
extension.MetalExtension(
"omlx.custom_kernels.quant_matmul._ext",
sources=[
f"{quant}/bindings.cpp",
f"{quant}/affine_qmm.cpp",
f"{quant}/command_buffer_caps.cpp",
f"{quant}/command_queue.cpp",
f"{quant}/gather_qmm.cpp",
f"{quant}/grouped_expert.cpp",
f"{quant}/head_flat_qmm.cpp",
f"{quant}/moe_staging.cpp",
f"{quant}/moe_weighted_sum.cpp",
f"{quant}/mxfp8_qmv.cpp",
f"{quant}/oq_a8.cpp",
f"{quant}/ane/ane_hybrid.mm",
],
include_dirs=[quant, shared],
metal_libraries=quant_libraries,
extra_link_args=[
"SHELL:-framework Accelerate",
"SHELL:-framework Foundation",
"SHELL:-framework IOSurface",
"SHELL:-framework Metal",
],
),
extension.MetalExtension(
"omlx.custom_kernels.sparse_index._ext",
sources=[
f"{sparse}/bindings.cpp",
f"{sparse}/dsa_indexer.cpp",
f"{sparse}/msa_block_topk.cpp",
],
include_dirs=[sparse, shared],
metal_libraries=[
extension.MetalLibrary(
"omlx_sparse_index_kernels",
sources=[
f"{sparse}/dsa_indexer.metal",
f"{sparse}/msa_block_topk.metal",
],
),
],
),
],
"cmdclass": {
"build_ext": extension.BuildExtension.with_options(generate_stubs=False),
},
}
if __name__ == "__main__":
setup(**_custom_kernel_build_kwargs())