惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
Netflix TechBlog - Medium
罗磊的独立博客
云风的 BLOG
云风的 BLOG
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
月光博客
月光博客
博客园 - Franky
F
Fortinet All Blogs
D
Docker
博客园 - 司徒正美
腾讯CDC
Recent Announcements
Recent Announcements
The Cloudflare Blog
B
Blog RSS Feed
GbyAI
GbyAI
T
Tailwind CSS Blog
雷峰网
雷峰网
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 三生石上(FineUI控件)
阮一峰的网络日志
阮一峰的网络日志

Mox的笔记库

2026PPoPP MLIR Tutorial学习 | Mox的笔记库 MacOS配置《明日方舟:终末地》 | Mox的笔记库 2025:向内生长 | Mox的笔记库 由mlir::ExecutionEngine引发的跨系统问题 | Mox的笔记库 WSL2配置Cuda-Tile环境记录(未完待续) | Mox的笔记库 Vibe Coding手搓项目记录 | Mox的笔记库 给Debian上包——以DuckDB为例 | Mox的笔记库 UCPD.sys事件存档 | Mox的笔记库 换新电脑之Mac mini M4从购买到配置 | Mox的笔记库 RISC-V meets RDBMS——RISC-V架构上可运行数据库一览 | Mox的笔记库 DuckDB Sort实现调查 | Mox的笔记库 修复Redis在树莓派5上无法运行的问题 | Mox的笔记库 如何在MLIR中自定义类型并且输出运行 | Mox的笔记库 网站网络结构变更记录 | Mox的笔记库 EDBT25论文阅读:PhoebeDB——A Disk-Based RDBMS Kernel for High-Performance and Cost-Effective OLTP SIGMOD25论文阅读:BPF-DB:——A Kernel-Embedded Transactional Database Management System For eBPF Applications Apache Arrow Gandiva项目解析 | Mox的笔记库 VLDB24论文阅读:Cloud-Native Database Systems and Unikernels——Reimagining OS Abstractions for Modern Hardware NoisePage源码分析(未完待续) | Mox的笔记库 VLDB20论文阅读:Mainlining Databases——Supporting Fast Transactional Workloads on Universal Columnar Data File Formats VLDB17论文阅读:Relaxed Operator Fusion for In-Memory Databases:Making Compilation, Vectorization, and Prefetching Work Together At Last 论文阅读:How not to structure your database-backed web applications——a study of performance bugs in the wild SIGMOD24阅读:ROME——Robust Query Optimization via Parallel Multi-Plan Execution 文章阅读:First Past the Post-Evaluating Query Optimization in MongoDB SIGMOD文章阅读:Apache Calcite——A Foundational Framework for Optimized Query Processing Over Heterogeneous Data Sources VLDB23论文阅读:Analyzing the Impact of Cardinality Estimation on Execution Plans in Microsoft SQL Server SIGMOD22论文阅读:Efficient Massively Parallel Join Optimization for Large Queries VLDB论文阅读:Weaving Relations for Cache Performance VLDB22论文阅读:ConnectorX——Accelerating Data Loading From Databases to Dataframes 论文阅读:UniKraft-Fast, Specialized Unikernels the Easy Way
Mac配置MLX-C开发环境 | Mox的笔记库
MocusEZ · 2025-11-30 · via Mox的笔记库

Apple的MLX是一个用于机器学习的推理框架,其最大的特点是能是用Apple的统一内存进行LLM推理。想到明年可能做AI编译器的相关工作,而手边有一台学校发的M3 Macbook Air,就想着尝鲜试下

环境配置

MLX手册给的是使用Python安装,这很明显不是我想要的,看了下HomeBrew有收入,那就直接HomeBrew安装

但这个环境只能运行Python和C++版本的MLX,想要运行C版本的MLX,需要拉取Git,手动配置CMake手动安装(也许后面可以试试以后把MLX-C打包上传HomeBrew?)

实测安装mlx-c后,可以不安装mlx

git clone https://github.com/ml-explore/mlx-c

配置CMake和Ninja(这里使用XCode自带的Clang)

mkdir build && cd build

cmake .. -G Ninja -DCMAKE_BUILD_TYPE=Release

sudo ninja install

但我在编译的时候出现报错,告诉我找不到Metal API,网上找资料被告知需要去AppStore安装完整版XCode,完成第一次运行后才能使用

sudo xcode-select -s /Applications/Xcode.app/Contents/Developer

sudo xcodebuild -license accept

sudo xcodebuild -runFirstLaunch

在安装完XCode以后,还需要再安装MetalToolchain才行

xcodebuild -downloadComponent MetalToolchain

CMake项目环境配置

CMAKE_PREFIX_PATH里把编译好的MLX-C给加上(如果使用XCode的Clang似乎可以忽略这条)

cmake .. -G Ninja -DCMAKE_PREFIX_PATH="$(brew --prefix mlx);/usr/local"

相对应的CMakeLists.txt加上对应的find_package

find_package(MLX CONFIG REQUIRED)

find_package(MLXC CONFIG REQUIRED)

测试程序

完整的CMakeLists.txt

cmake_minimum_required(VERSION 3.10.0)

project(test-mlx VERSION 0.1.0 LANGUAGES C CXX)

find_package(MLX CONFIG REQUIRED)

find_package(MLXC CONFIG REQUIRED)

add_executable(test-mlx test.c)

target_link_libraries(test-mlx PRIVATE mlxc)

测试代码

#include <stdio.h>

#include <mlx/c/mlx.h>

void print_array(const char* msg, mlx_array arr) {

mlx_string str = mlx_string_new();

mlx_array_tostring(&str, arr);

printf("%s\n%s\n", msg, mlx_string_data(str));

mlx_string_free(str);

}

int main() {

// 打印 MLX 版本

mlx_string version = mlx_string_new();

mlx_version(&version);

printf("MLX C API 版本: %s\n\n", mlx_string_data(version));

// 获取默认 GPU stream

mlx_stream stream = mlx_default_gpu_stream_new();

// 创建浮点数数组

float data_a[] = {1.0f, 2.0f, 3.0f, 4.0f};

float data_b[] = {5.0f, 6.0f, 7.0f, 8.0f};

int shape[] = {4};

// 创建 MLX 数组

mlx_array a = mlx_array_new_data(data_a, shape, 1, MLX_FLOAT32);

mlx_array b = mlx_array_new_data(data_b, shape, 1, MLX_FLOAT32);

mlx_array c = mlx_array_new();

// 执行加法运算

mlx_add(&c, a, b, stream);

print_array("a + b =", c);

// 矩阵运算示例

int shape_2d[] = {2, 2};

mlx_array x = mlx_array_new();

mlx_array y = mlx_array_new();

mlx_array z = mlx_array_new();

mlx_reshape(&x, a, shape_2d, 2, stream);

mlx_reshape(&y, b, shape_2d, 2, stream);

mlx_matmul(&z, x, y, stream);

print_array("矩阵乘法结果:", z);

// 释放资源

mlx_array_free(a);

mlx_array_free(b);

mlx_array_free(c);

mlx_array_free(x);

mlx_array_free(y);

mlx_array_free(z);

mlx_stream_free(stream);

mlx_string_free(version);

return 0;

}


avatar

探索未曾设想的道路