概要
MLX は、Apple の機械学習研究チームによって開発された配列フレームワークで、Apple シリコン向けに設計されており、NumPy や PyTorch に似た API を提供します。合成可能な関数変換、遅延計算、動的グラフ構築、および CPU と GPU の統一メモリをサポートします。機械学習モデルの効率的な学習とデプロイを目的としています。
README プレビュー
# MLX\n\n[**Quickstart**](#quickstart) | [**Installation**](#installation) |\n[**Documentation**](https://ml-explore.github.io/mlx/build/html/index.html) |\n[**Examples**](#examples)\n\n[](https://circleci.com/gh/ml-explore/mlx)\n\nMLX is an array framework for machine learning on Apple silicon,\nbrought to you by Apple machine learning research.\n\nSome key features of MLX include:\n\n- **Familiar APIs**: MLX has a Python API that closely follows NumPy. MLX\n also has fully featured C++, [C](https://github.com/ml-explore/mlx-c), and\n [Swift](https://github.com/ml-explore/mlx-swift/) APIs, which closely mirror\n the Python API. MLX has higher-level packages like `mlx.nn` and\n `mlx.optimizers` with APIs that closely follow PyTorch to simplify building\n more complex models.\n\n- **Composable function transformations**: MLX supports composable function\n transformations for automatic differentiation, automatic vectorization,\n and computation graph optimization.\n\n- **Lazy computation**: Computations in MLX are lazy. Arrays are only\n materialized when needed.\n\n- **Dynamic graph construction**: Computation graphs in MLX are constructed\n dynamically. Changing the shapes of function arguments does not trigger\n slow compilations, and debugging is simple and intuitive.\n\n- **Multi-device**: Operations can run on any of the supported devices\n (currently the CPU and the GPU).\n\n- **Unified memory**: A notable difference from MLX and other frameworks\n is the *unified memory model*. Arrays in MLX live in shared memory.\n Operations on MLX arrays can be performed on any of the supported\n device types without transferring data.\n\nMLX is designed by machine learning researchers for machine learning\nresearchers. The framework is intended to be user-friendly, but still efficient\nto train and deploy models. The design of the framework itself is also\nconceptually simple. We intend to make it easy for researchers to extend and\nimprove MLX with the
FAQ (5)
トラブル対応M5 Maxでsorted_indices=Trueを指定したmx.gather_qmmの結果が、行数が32,768を超えて64の倍数でない場合に誤ってしまう問題は、どうすれば修正できますか?
MLX を 0.32.3 以降にアップグレードしてください。このバグは PR #3922 で修正されています。
pip install --upgrade mlx
以下で確認できます。
import mlx.core as mx
print(mx.__version__)
アップグレードできない場合は、sorted_indices=False を使用するか、行数を 64 の倍数になるようにパディングして、パディングされた出力行を破棄してください。この問題は Apple M5 Max/macOS 27 上の MLX 0.31.2 に影響します。M4 Pro は影響を受けません。
トラブル対応MLXでbf16アクティベーションを使うとmx.fast.gated_delta_updateが精度を失うのはなぜですか?また、どうすれば修正できますか?
mx.fast.gated_delta_update は、カーネルを実行する前に減衰項の g と beta を q の dtype(通常は bf16)にキャストします。bf16 は 1 のすぐ下では 1/256 刻みの精度しかないため、0.9995 のような減衰値は正確に 1.0 に丸められ、Gated DeltaNet の状態がそれらのトークンでまったく減衰しなくなります。これは PR #4760 で修正されました。g と beta は現在 float32 でカーネルに渡されるようになり(flash-linear-attention や vLLM の動作と一致)、float64 の参照値と比較した出力誤差が ~4.8e-2 から ~2.4e-3 に削減されました。その修正を含む MLX バージョンにアップグレードしてください。古いバージョンでの唯一の回避策は、q、k、v も float32 で渡すことですが、これは約 30% 遅くなります。
トラブル対応set_wired_limit を設定しているにもかかわらず、なぜ MLX Metal の推論は数秒間アイドル状態になると 4〜5 倍遅くなるのですか?
これは、GPUがアイドル状態になってからまもなく、macOSがMLXのワイヤードメモリ常駐(wired-memory residency)を取り消すことが原因で発生します。MLX 0.32.xは常駐を一度だけ要求し、その後更新しません。常駐をハートビートで再要求するPR #4633を含むリリースにMLXをアップグレードすることで修正されます。一時的な回避策として、次を実行してください:
sudo sysctl iogpu.disable_wired_collector=1
次のコマンドで確認できます:
sysctl iogpu.disable_wired_collector
この設定は再起動後にクリアされます。別の方法として、約1秒ごとに小さなMLX演算を実行してGPUをアクティブに保つこともできます。テストでは3秒間隔では不十分でした。この問題は、OllamaのApple Silicon MLXランナーを含む、macOS 26以降上のMLX 0.32.xに影響します。
トラブル対応macOS 14をターゲットとする場合に、Apple Silicon上でMLX CPU Float16のsigmoidまたは比較の結果が正しくない問題を修正するにはどうすればよいですか?
これは MLX issue #4601 が原因です: Float16 NEON 比較フォールバックはレーン 0 のみを読み取り、そのマスクをすべてのレーンにブロードキャストします。mlx/backend/cpu/simd/neon_fp16_simd.h では、return Simd<uint16_t, N>(*(uint16_t*)(&out)); のようなコードを、NEON ベクター全体を保持するように変更する必要があり、通常は return Simd<uint16_t, N>(out); とします。この変更をすべての Float16 比較オーバーロードおよび論理否定オーバーロードに適用し、その後 MLX とターゲットをクリーンリビルドしてください。修正後、リプロデューサーは 0.268799 ではなく、期待値 0.731059 に近い sigmoid(1) = 0.731445 を報告します。すぐにパッチを適用できない場合は、Float32 で活性化を計算してからキャストし直してください。例えば sigmoid(x.astype(float32)).astype(float16) とするか、アップストリームの修正を含む MLX リリースを使用してください。
トラブル対応MLX で大きなテンソルの最後以外の軸を縮約した際に mx.sum がすべてゼロを返す問題を修正するにはどうすればよいですか?
これは、MLX の列/最終軸以外のリダクションカーネルにおける 32 ビット Metal グリッドのオーバーフローが原因です。内部リダクションストライドが 2^29 の場合、起動サイズは 256 * ceil(2^29 / 32) = 2^32 となり、ディスパッチ次元がオーバーフローして、出力がゼロまたは部分的にゼロになります。MLX 0.32.0.dev20260609+cc3f3e60 では、(1, 32, 4096, 131072) などの形状に対する mx.sum(x, axis=1) が影響を受けます。最終軸のリダクションは別のコードパスを使用するため、正しく動作します。
即時の回避策: リダクション対象の軸を最終軸に移動してからリダクションを実行します:
y = mx.sum(mx.moveaxis(x, 1, -1), axis=-1)
mx.eval(y)
任意の軸の場合:
y = mx.sum(mx.moveaxis(x, axis, -1), axis=-1)
別の方法として、スライスを明示的に加算します:
y = None
for i in range(x.shape[1]):
block = x[:, i:i+1]
y = block if y is None else y + block
mx.eval(y)
恒久対策: issue #3784 に対する "Fix Metal column reduction grid overflow" の修正を含む MLX リリースにアップグレードするか、その PR を含むコミットから MLX をビルドしてください。