Gradient of batched block_diag falls back to numba object mode (Blockwise ARange)
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- numpy, python
- 領域
- performance
調査の方向性
Reproduce with the snippet in mode="NUMBA" and confirm the Blockwise{ARange} object-mode warning and the timing gap. Then read BlockDiagonal.pullback and how Blockwise calls vectorize_shape, since that is where core shapes become batched; compare against the concatenate-based construction to see what a non-batched pullback would look like. Done means the gradient runs in nopython mode, timings approach the concatenate figure, and a regression test covers batched plus unbatched inputs.
索引モデルが issue の本文から書いたものです。
説明
When one input to block_diag is batched, its gradient compiles a Blockwise{ARange} that numba runs in object mode. In a small example the gradient is about 20x slower than building the same matrix with concatenate.
import pytensor
import pytensor.tensor as pt
A = pt.tensor3("A") # batch of 6x6 blocks
B = pt.matrix("B") # 2x2, not batched
out = (pt.linalg.block_diag(A, B) ** 2).sum()
f = pytensor.function([A, B], pytensor.grad(out, [A, B]), mode="NUMBA")
# UserWarning: Numba will use object mode to run Blockwise{ARange{dtype='int64'}, (),(),()->(o00)}'s perform method
Timings with A of shape (1000, 6, 6), pytensor 3.3.8, M-series Mac:
| gradient of | ms per call |
|---|---|
block_diag(A, B) as it is now |
3.2 |
same, with the pullback using slices instead of arange |
1.1 (still object mode, now Blockwise{Subtensor}) |
the same matrix built with concatenate and zeros |
0.14 |
What I think happens: BlockDiagonal.pullback indexes gout with arange(index_begin, index_end), computed from i.shape. When Blockwise vectorizes the pullback, vectorize_shape broadcasts each core shape over the batch (which is correct in general), so the indices and the arange become batched even though they are identical for every batch element. Swapping arange for slices removes the ARange, but the slice bounds are still batched, so it moves the problem to Blockwise{Subtensor}.
Where I hit it: pymc-extras structural models. TimeSeasonality(duration > 1) has a time-varying transition matrix, and adding any other component (e.g. Autoregressive) combines them with block_diag(T, o_T), so the Kalman filter gradient picks this up (pymc-devs/pymc-extras#833).
I'm not sure what the preferred fix is: a pullback that avoids batched indices, a rewrite that drops batch dims from Blockwise inputs that only come from broadcast shapes, or something else. Happy to work on it with some direction.
- 主要言語
- Python
- スター
- 654
- フォーク
- 258
- 平均マージ
- 2日 7時間
- マージ済み PR(30日)
- 39
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
pymc-devs/pytensor のほかの issue
-
Docs of mode still mentions C/CVM as the default with FAST_RUN対応中かも @ADITYASINGH1206 が 8 日前に担当しました。 オープンdocs
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
pymc-devs/pytensor#2641 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
docs
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
BUG: geomspace returns wrong sign for the endpoints when start and stop are negative対応中かも @mayuriphad が 27 日前に担当しました。 オープン
難易度 2/5 1時間未満 初心者へのやさしさ 85/100
メンテナーはふだん 1 日以内に返信
-
Reciprocal.impl computes np.float32(1.0) / x: float32 precision for float64 scalars under NEP 50 (Python linker)再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
pymc-devs/pytensor#2430 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
pymc-devs/pytensor#2428 · コメント 5 件 ·
メンテナーはふだん 1 日以内に返信
pymc-devs/pytensor の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
dgunning/edgartools#2019 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
topoteretes/cognee#6818 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
source_verification_agent: no outcome when the publisher confirms a work but its DOI resolves to a different record対応中かも @pinjun99 が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
Imbad0202/academic-research-skills#1159 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[Bug]: DoubleMLData.from_arrays checks the treatment d with force_all_x_finite instead of force_all_d_finite対応中かも @leninworld が今日担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
DoubleML/doubleml-for-py#471 ·
メンテナーはふだん 1 日以内に返信