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

Aperta
#2,525 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
1-2 giorni
Idoneità per principianti
57/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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']
Lingua principale
Python
Stelle
654
Fork
258
Merge medio
2g 7h
PR unite (30g)
39

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pymc-devs/pytensor

Tutte le issue di pymc-devs/pytensor

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.