Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
d921319
Add Highway-backed x86 CPU SIMD and threading
dhiltgen May 28, 2026
231f5bf
Adjust for runtime dynamic selection
dhiltgen May 29, 2026
f694e0b
Restore Simd<T,1>::operator[] for upstream's sincos
rcfa Sep 27, 2026
d01923d
Fix the Highway build on MLX 0.32.2: writable Simd lanes
rcfa Sep 27, 2026
ac7be65
Fix the Highway build on MLX 0.32.3: a vector divide
rcfa Oct 13, 2026
a17e5de
Keep the fork's CPU JIT
rcfa Sep 27, 2026
56faa32
Select the CPU quantized backend by file
rcfa Sep 27, 2026
d3fd60b
Put #3019's changes to shared CPU files behind MLX_USE_HIGHWAY_KERNELS
rcfa Sep 27, 2026
f9d4d55
Decode E4M3 0x7F and 0xFF as NaN in the quantized fp8 paths
rcfa Oct 13, 2026
7ef5e1a
Compile #3019's new CPU sources only with Highway, and drop the GCD pool
rcfa Sep 27, 2026
9c7e450
Name the CMake option MLX_USE_HIGHWAY, on for Linux x86-64
rcfa Sep 27, 2026
a51c0de
Keep 3, 5 and 6-bit weights off #3019's M >= 32 dequantize path
rcfa Sep 27, 2026
607608c
Round QQMatmul's simulated activations as upstream does
rcfa Sep 27, 2026
bf85789
Send attention the native CPU kernel cannot hold to the fallback
rcfa Sep 27, 2026
5ccb3bf
Compute float64 elementwise math in double in the Highway facade
rcfa Sep 27, 2026
10e601e
Return 0 for integer pow with a negative exponent in the Highway facade
rcfa Sep 27, 2026
5410adc
Keep bf16 and fp16 reduction partials in float until the end
rcfa Sep 27, 2026
cacd750
Round once in the Highway facade's fma, on every target
rcfa Sep 27, 2026
ec8e5e3
Keep float16 inf and NaN through Highway's conversions
rcfa Sep 27, 2026
f282160
Decode E4M3 0x7F and 0xFF as NaN under Highway, as MLX 0.32.3 does
rcfa Oct 14, 2026
97c68ce
Make #3019's int8 activation path opt-in
rcfa Sep 27, 2026
9cafbdb
Record which Highway target each CPU kernel family ran
rcfa Sep 27, 2026
b695d0f
Size the CPU pool to the CPUs the process may use, and harden it
rcfa Sep 27, 2026
00e805e
Split short fp32 GEMMs by columns across the CPU pool
rcfa Sep 27, 2026
75b0e55
Free CPU kernel scratch that grew past 64 MiB
rcfa Sep 27, 2026
50e092f
Keep the whole 4-bit dot product in the int8 path on every target
rcfa Sep 27, 2026
ca6c1b6
Pin OpenBLAS to one thread at every CPU pool size
rcfa Sep 28, 2026
d04694e
Make quantized_float32_accumulation() a test hook in cpu::detail
rcfa Sep 28, 2026
8567107
Export cpu::ThreadPool from shared builds
rcfa Sep 28, 2026
32ba210
Make MLX_USE_HIGHWAY_KERNELS a public compile definition
rcfa Sep 28, 2026
67b16c1
Fix the edges of the Highway CMake option
rcfa Sep 28, 2026
2f43d18
Refuse a nested scratch lease
rcfa Sep 28, 2026
5c63645
Tidy the Highway tests, and test bf16 sin and cos past 2^23
rcfa Sep 28, 2026
dea0743
Shorten our CPU backend comments, and drop claims no log shows
rcfa Sep 28, 2026
fcb6aee
Keep the sign of zero in the Highway facade's floor, ceil and rint
rcfa Sep 28, 2026
2109bbe
Fix a stale slot claim and a missed first call in the CPU pool
rcfa Sep 29, 2026
8469702
Read each lane as C++ does when a vector becomes Simd<bool>
rcfa Sep 30, 2026
35e3d6a
Tell cores apart by the CPUs they hold, not by core_id
rcfa Sep 30, 2026
814aef8
Test the mxfp8 decode at every Highway target
rcfa Oct 14, 2026
3b95ae3
Return early from #3019's CPU kernels for an empty array
rcfa Oct 14, 2026
204dd3c
Widen float16 sums and products under Highway
rcfa Oct 14, 2026
eeae425
Keep the norms' input copy alive until their tasks run
rcfa Oct 14, 2026
75f1ceb
Copy the norms' weight and bias when their stride is not 1
rcfa Oct 21, 2026
b64de7a
Copy rope's offsets and freqs when their stride is not 1
rcfa Oct 21, 2026
e8725f0
Copy sdpa's sinks when they are not row-contiguous
rcfa Oct 21, 2026
1adba0a
Reject an unquantized weight in QQMatmul under Highway
rcfa Oct 23, 2026
1d7f247
Take sdpa's composed path when keys and values differ in length
rcfa Oct 23, 2026
f3b3b94
Bound sdpa's per-lane buffers at compile time
rcfa Oct 23, 2026
303d8d4
Merge osaurus-ai main (#55, 61cc7a4d7) into hwy/adopt-x86
rcfa Nov 22, 2026
ca35ada
Add convert_in_range for Highway vectors
rcfa Nov 8, 2026
a4d45df
Saturate float to integer conversions in the Highway Simd type
rcfa Nov 8, 2026
74477bb
Take the sum order of the Highway Simd type in the block sum tests
rcfa Nov 8, 2026
5e52728
Keep the Highway Simd type out of ops_tests.cpp
rcfa Nov 8, 2026
2916b31
Declare the Highway test hooks outside the anonymous namespace
rcfa Nov 8, 2026
a430b14
Sum long rows in blocks in the Highway norm kernels
rcfa Nov 8, 2026
97c141c
Test an E8M0 scale of 0xFF in Highway builds
rcfa Nov 8, 2026
a073c53
Decode an E8M0 scale of 0xFF as NaN in the Highway quantized code
rcfa Nov 8, 2026
2536f9c
Return early from empty quantized matmuls in Highway builds
rcfa Nov 8, 2026
5a64403
Use the scalar integer divide and remainder in Highway's vector forms
rcfa Nov 8, 2026
a275efd
Use the scalar integer pow in Highway's vector form
rcfa Nov 8, 2026
acf0c66
Return early from an empty qqmm in Highway builds
rcfa Nov 8, 2026
6beda62
Accumulate in float32 in the Highway affine quantized matmul
rcfa Nov 15, 2026
35a2a85
Keep the CPU on promoting quantized matmuls in Highway builds
rcfa Nov 8, 2026
0a10b88
Pass lda and ldb to simd_gemm in the Highway low-precision GEMM
rcfa Nov 8, 2026
e61dd69
Preserve mixed-scale and one-bit CPU quantization in Highway builds
Nov 22, 2026
b6e5b8a
Guard optional Highway dispatch in SwiftPM builds
Nov 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ option(MLX_BUILD_GGUF "Include support for GGUF format" ON)
option(MLX_BUILD_SAFETENSORS "Include support for safetensors format" ON)
option(MLX_BUILD_PYTHON_STUBS "Build stub files for python bindings" ON)
option(MLX_METAL_JIT "Use JIT compilation for Metal kernels" OFF)
# Highway SIMD kernels for the CPU backend, on by default for Linux x86-64.
if(MLX_BUILD_CPU
AND CMAKE_SYSTEM_NAME STREQUAL "Linux"
AND CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64|amd64)$")
set(_MLX_USE_HIGHWAY_DEFAULT ON)
else()
set(_MLX_USE_HIGHWAY_DEFAULT OFF)
endif()
option(MLX_USE_HIGHWAY "Build the CPU backend's Highway SIMD kernels"
${_MLX_USE_HIGHWAY_DEFAULT})
# MLX_ENABLE_X86_SIMD (ml-explore/mlx#3019's name) sets it on any configure.
if(DEFINED MLX_ENABLE_X86_SIMD)
set(MLX_USE_HIGHWAY
${MLX_ENABLE_X86_SIMD}
CACHE BOOL "Build the CPU backend's Highway SIMD kernels" FORCE)
unset(MLX_ENABLE_X86_SIMD CACHE)
endif()
set(MLX_HIGHWAY_SOURCE_DIR
""
CACHE PATH "Path to a local Highway source checkout")
set(MLX_HIGHWAY_GIT_TAG
"1.4.0"
CACHE STRING "Highway git tag or commit used when fetching Highway")
option(MLX_USE_CCACHE "Use CCache for compilation cache when available" ON)
option(MLX_EXPORT_ALL_SYMBOLS "Export all symbols in the library" OFF)
option(BUILD_SHARED_LIBS "Build mlx as a shared library" OFF)
Expand Down
5 changes: 5 additions & 0 deletions mlx.pc.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

@PACKAGE_INIT@

if (@MLX_BUILD_CPU@ AND @MLX_USE_HIGHWAY@)
include(CMakeFindDependencyMacro)
find_dependency(hwy CONFIG PATHS "${PACKAGE_PREFIX_DIR}" NO_DEFAULT_PATH)
endif()

include(CMakeFindDependencyMacro)

if(WIN32 AND @MLX_BUILD_CPU@ AND NOT TARGET MLX::OpenBLAS)
Expand Down
148 changes: 148 additions & 0 deletions mlx/backend/cpu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,72 @@ else()
set(CLANG FALSE)
endif()

set(MLX_HIGHWAY_INCLUDE_DIR "")

if(MLX_USE_HIGHWAY)
if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64|amd64)$")
message(FATAL_ERROR "MLX_USE_HIGHWAY requires an x86-64 target")
endif()

set(HWY_ENABLE_CONTRIB
OFF
CACHE BOOL "" FORCE)
set(HWY_ENABLE_EXAMPLES
OFF
CACHE BOOL "" FORCE)
set(HWY_ENABLE_INSTALL
ON
CACHE BOOL "" FORCE)
set(HWY_ENABLE_TESTS
OFF
CACHE BOOL "" FORCE)
set(HWY_FORCE_STATIC_LIBS
ON
CACHE BOOL "" FORCE)
if(MLX_HIGHWAY_SOURCE_DIR)
set(MLX_HIGHWAY_INCLUDE_DIR ${MLX_HIGHWAY_SOURCE_DIR})
add_subdirectory(${MLX_HIGHWAY_SOURCE_DIR} ${CMAKE_BINARY_DIR}/highway)
else()
FetchContent_Declare(
highway
GIT_REPOSITORY https://github.com/google/highway.git
GIT_TAG ${MLX_HIGHWAY_GIT_TAG}
GIT_SHALLOW TRUE
EXCLUDE_FROM_ALL)
FetchContent_MakeAvailable(highway)
set(MLX_HIGHWAY_INCLUDE_DIR ${highway_SOURCE_DIR})
endif()
if(NOT TARGET hwy)
message(FATAL_ERROR "MLX_USE_HIGHWAY requires Highway target hwy")
endif()
if(NOT TARGET hwy::hwy)
add_library(hwy::hwy ALIAS hwy)
endif()

set(MLX_HIGHWAY_TARGET_DEFINITIONS HWY_DISABLE_PCLMUL_AES)
if(MSVC AND NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# Highway marks its AVX3-family targets broken for MSVC. Mirror that policy
# here by using Highway's target-filtering define.
set(MLX_HIGHWAY_DISABLED_AVX3_TARGETS
"HWY_DISABLED_TARGETS=(HWY_AVX3+HWY_AVX3_DL+HWY_AVX3_ZEN4+HWY_AVX3_SPR+HWY_AVX10_2)"
)
list(APPEND MLX_HIGHWAY_TARGET_DEFINITIONS
${MLX_HIGHWAY_DISABLED_AVX3_TARGETS})
endif()

# Keep ISA flags out of the main MLX target. Clang/GCC use Highway's
# foreach-target attributes; MSVC uses per-target shims below because it needs
# /arch:AVX2 on AVX2 codegen to avoid slow SSE/AVX transitions.
target_compile_definitions(hwy PRIVATE ${MLX_HIGHWAY_TARGET_DEFINITIONS})
# PUBLIC, since installed headers (fast_primitives.h, unary.h, binary.h)
# change with it. Highway's own definitions stay PRIVATE.
target_compile_definitions(mlx PUBLIC MLX_USE_HIGHWAY_KERNELS)
target_compile_definitions(mlx PRIVATE ${MLX_HIGHWAY_TARGET_DEFINITIONS})
target_include_directories(mlx PRIVATE ${MLX_HIGHWAY_INCLUDE_DIR})
target_link_libraries(mlx PRIVATE hwy::hwy)
message(STATUS "Highway: runtime-dispatched CPU kernels enabled")
endif()

set(COMPILE_DEPS
${PROJECT_SOURCE_DIR}/mlx/types/half_types.h
${PROJECT_SOURCE_DIR}/mlx/types/fp16.h
Expand Down Expand Up @@ -76,6 +142,7 @@ target_sources(
${CMAKE_CURRENT_SOURCE_DIR}/masked_mm.cpp
${CMAKE_CURRENT_SOURCE_DIR}/primitives.cpp
${CMAKE_CURRENT_SOURCE_DIR}/quantized.cpp
${CMAKE_CURRENT_SOURCE_DIR}/precision.cpp
${CMAKE_CURRENT_SOURCE_DIR}/reduce.cpp
${CMAKE_CURRENT_SOURCE_DIR}/scan.cpp
${CMAKE_CURRENT_SOURCE_DIR}/select.cpp
Expand All @@ -93,6 +160,87 @@ target_sources(
${CMAKE_CURRENT_SOURCE_DIR}/eval.cpp
${CMAKE_CURRENT_BINARY_DIR}/compiled_preamble.cpp)

if(MLX_USE_HIGHWAY)
# Highway quantization dispatch and kernel counters.
target_sources(
mlx PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/quantized_highway_backend.cpp
${CMAKE_CURRENT_SOURCE_DIR}/highway_info.cpp)

# #3019's native norms, RoPE and attention; without Highway, fast_primitives.h
# keeps their inline eval_cpu.
target_sources(
mlx
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/norms.cpp
${CMAKE_CURRENT_SOURCE_DIR}/rope.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sdpa.cpp)

# The thread pool the Highway kernels use.
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/threading)

if(MSVC AND NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(MLX_HIGHWAY_MSVC_DISPATCH_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/norms_highway_dispatch.cpp
${CMAKE_CURRENT_SOURCE_DIR}/quantized_highway_dispatch.cpp
${CMAKE_CURRENT_SOURCE_DIR}/rope_highway_dispatch.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sdpa_highway_dispatch.cpp)
set(MLX_HIGHWAY_MSVC_TARGET_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/norms_highway.cpp
${CMAKE_CURRENT_SOURCE_DIR}/quantized_highway.cpp
${CMAKE_CURRENT_SOURCE_DIR}/rope_highway.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sdpa_highway.cpp)

# Native MSVC needs per-target object builds because /arch:AVX2 is TU-wide.
function(mlx_add_highway_msvc_target target_name suffix)
add_library(${target_name} OBJECT ${MLX_HIGHWAY_MSVC_TARGET_SOURCES})
target_link_libraries(${target_name} PRIVATE hwy::hwy)
target_include_directories(
${target_name} PRIVATE ${PROJECT_SOURCE_DIR} ${MLX_HIGHWAY_INCLUDE_DIR})
target_compile_definitions(
${target_name}
PRIVATE MLX_USE_HIGHWAY_KERNELS
${MLX_HIGHWAY_TARGET_DEFINITIONS}
MLX_HIGHWAY_MANUAL_TARGET
MLX_HIGHWAY_TARGET_SUFFIX=${suffix}
HWY_COMPILE_ONLY_STATIC
NOMINMAX
WIN32_LEAN_AND_MEAN
FMT_UNICODE=0)
if(BUILD_SHARED_LIBS)
target_compile_definitions(${target_name} PRIVATE MLX_EXPORT)
else()
target_compile_definitions(${target_name} PRIVATE MLX_STATIC)
endif()
target_compile_options(${target_name} PRIVATE /bigobj /Zc:preprocessor)
endfunction()

mlx_add_highway_msvc_target(mlx_highway_msvc_avx2 _avx2)
# Highway's AVX2 target assumes AVX2/BMI2/FMA/F16C. Limit /arch:AVX2 to the
# AVX2 target bodies; public dispatch wrappers stay baseline-safe and only
# call them after runtime CPU detection.
target_compile_options(mlx_highway_msvc_avx2 PRIVATE /arch:AVX2)
mlx_add_highway_msvc_target(mlx_highway_msvc_sse4 _sse4)
target_compile_definitions(mlx_highway_msvc_sse4 PRIVATE HWY_WANT_SSSE3=1
HWY_WANT_SSE4=1)
mlx_add_highway_msvc_target(mlx_highway_msvc_ssse3 _ssse3)
target_compile_definitions(mlx_highway_msvc_ssse3 PRIVATE HWY_WANT_SSSE3=1)
mlx_add_highway_msvc_target(mlx_highway_msvc_sse2 _sse2)

target_sources(
mlx
PRIVATE ${MLX_HIGHWAY_MSVC_DISPATCH_SOURCES}
$<TARGET_OBJECTS:mlx_highway_msvc_avx2>
$<TARGET_OBJECTS:mlx_highway_msvc_sse4>
$<TARGET_OBJECTS:mlx_highway_msvc_ssse3>
$<TARGET_OBJECTS:mlx_highway_msvc_sse2>)
else()
target_sources(mlx PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/norms_highway.cpp)
target_sources(mlx
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/quantized_highway.cpp)
target_sources(mlx PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/rope_highway.cpp)
target_sources(mlx PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/sdpa_highway.cpp)
endif()
endif()

if(MLX_BUILD_ACCELERATE)
target_sources(mlx PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/gemms/bnns.cpp)
else()
Expand Down
Loading