Gradient of batched block_diag falls back to numba object mode (Blockwise ARange)

Đang mở
#2,702 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@saksham10arora-dotcom đang làm issue này rồi.

Từ ngày 21/11/2026.

  • #2703 của @saksham10arora-dotcom — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
numpy, python
Lĩnh vực
performance

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
654
Fork
258
Merge trung bình
2 ngày 7 giờ
Pull request đã merge (30 ngày)
39

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của pymc-devs/pytensor

Tất cả issue của pymc-devs/pytensor

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.