Blockwise(AdvancedSubtensor) with broadcast-batch indices is not rewritten to AdvancedSubtensor

Đang mở
#2,704 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

Chưa có ai nhận issue này.

Đá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
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
compilers

Hướng nghiên cứu

Start by running the issue's snippet with pytensor.dprint to confirm the Blockwise(AdvancedSubtensor) node. Then compare it with the plain AdvancedSubtensor that vectorize_graph produces for unbatched indices. The task is a graph rewrite that drops broadcastable batch dims from the index inputs and adds leading full slices. Done when the dprint output shows a plain AdvancedSubtensor and the numerical results match the original graph.

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

Mô tả

graph rewriting indexing vectorization

vectorize_graph of advanced indexing with unbatched indices already gives a plain AdvancedSubtensor with a leading slice. But if the indices were batched when the graph was vectorized and only lose their batch dims later (broadcastable batch dims after rewrites), the Blockwise(AdvancedSubtensor) stays:

import pytensor
import pytensor.tensor as pt
from pytensor.compile.mode import Mode
from pytensor.tensor.blockwise import Blockwise

xc = pt.matrix("xc")
r = pt.vector("r", dtype="int64")
c = pt.vector("c", dtype="int64")
core_op = xc[r[:, None], c[None, :]].owner.op

x = pt.tensor3("x")
rb = pt.tensor("rb", shape=(1, None, 1), dtype="int64")  # batch dim only broadcast
cb = pt.tensor("cb", shape=(1, 1, None), dtype="int64")
out = Blockwise(core_op, signature="(i00,i01),(i10,i11),(i20,i21)->(o00,o01)")(x, rb, cb)

fn = pytensor.function([x, rb, cb], out, mode=Mode(linker="py", optimizer="fast_run"))
pytensor.dprint(fn)
# Blockwise{AdvancedSubtensor{idx_list=(0, 1)}, (i00,i01),(i10,i11),(i20,i21)->(o00,o01)} [id A] 0
#  ├─ x [id B]
#  ├─ rb [id C]
#  └─ cb [id D]

This is equivalent to x[:, rb[0], cb[0]].

It is what's left in the batched block_diag gradient from #2702 once #2703 removes the batched ARange. There the indices are ix_-style aranges with a broadcast batch dim:

Blockwise{AdvancedSubtensor{idx_list=(0, 1)}, (i00,i01),(i10,i11),(i20,i21)->(o00,o01)} [id A] <Tensor3(float64, shape=(?, ?, ?))>
 ├─ Mul [id B] <Tensor3(float64, shape=(?, ?, ?))>
 │  ├─ [[[2.]]] [id C] <Tensor3(float64, shape=(1, 1, 1))>
 │  └─ Blockwise{BlockDiagonal{n_inputs=2}, (m0,n0),(m1,n1)->(m,n)} [id D] <Tensor3(float64, shape=(?, ?, ?))>
 ├─ ExpandDims{axes=(0, 2)} [id H] <Tensor3(int64, shape=(1, ?, 1))>
 │  └─ ARange{dtype='int64'} [id I] <Vector(int64, shape=(?,))>
 └─ ExpandDims{axes=(0, 1)} [id BA] <Tensor3(int64, shape=(1, 1, ?))>
    └─ ARange{dtype='int64'} [id BB] <Vector(int64, shape=(?,))>

A rewrite that drops the broadcast batch dims of the index inputs and indexes with leading full slices would turn it back into a plain AdvancedSubtensor, as vectorize_graph produces directly.

Ngôn ngữ chính
Python
Star
655
Fork
266
Merge trung bình
3 ngày 23 giờ
Pull request đã merge (30 ngày)
43

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.