MLX mode never reaches the mx.addmm dispatches because BlasOpt is excluded

オープン
#2,525 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
1〜2日
初心者へのやさしさ
57/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
python

調査の方向性

Start by reading MLXLinker and the BlasOpt rewrite rules, then inspect the Gemv and Ger dispatches in link/mlx/dispatch/blas.py. Reproduce the two examples in the issue and inspect the MLX graph to confirm which rewrites run. Done when both expressions reach the intended mx.addmm dispatches and the outer product is handled in MLX mode.

索引モデルが issue の本文から書いたものです。

説明

mlx performance

MLXLinker lists BlasOpt among its incompatible rewrites, so Gemv and Ger never appear in an MLX graph, and their mx.addmm dispatches in link/mlx/dispatch/blas.py are unreachable. A @ v + b runs as a Matmul plus a separate Add, and an outer product col @ row runs as a Matmul. local_dot_to_mul leaves core outer products alone so GER can handle them, but MLX mode never produces a GER. #2514 adds local_outer_dot_to_mul for Numba, which has no GER either, and tagging it mlx would cover the outer product. #2406 turns Gemv and Ger into plain Ops, but MLX still excludes BlasOpt there.

import re

import mlx.core as mx
import numpy as np
import pytensor
import pytensor.tensor as pt

A = pt.matrix("A", dtype="float32")
v = pt.vector("v", dtype="float32")
b = pt.vector("b", dtype="float32")
col = pt.matrix("col", shape=(None, 1), dtype="float32")
row = pt.matrix("row", shape=(1, None), dtype="float32")

rng = np.random.default_rng(0)
cases = {
    "A @ v + b": ([A, v, b], A @ v + b, [rng.normal(size=(5, 3)), rng.normal(size=3), rng.normal(size=5)]),
    "col @ row": ([col, row], col @ row, [rng.normal(size=(5, 1)), rng.normal(size=(1, 3))]),
}
for name, (inputs, output, values) in cases.items():
    fn = pytensor.function(inputs, output, mode="MLX")
    mx.export_to_dot("graph.dot", fn.vm.jit_fn(*[mx.array(value.astype("float32")) for value in values]))
    print(name, [type(node.op).__name__ for node in fn.maker.fgraph.toposort()], re.findall(r'label ="([^"]+)"', open("graph.dot").read()))
# A @ v + b ['DimShuffle', 'Dot', 'DimShuffle', 'Elemwise'] ['Transpose', 'Reshape', 'Matmul', 'Transpose', 'Reshape', 'Add']
# col @ row ['Dot'] ['Matmul']
主要言語
Python
スター
654
フォーク
258
平均マージ
2日 7時間
マージ済み PR(30日)
39

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

pymc-devs/pytensor のほかの issue

pymc-devs/pytensor の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。