Wrapping a graph in OpFromGraph breaks its gradient when it has an integer output

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

@atishay-kasliwal đang làm issue này rồi.

Từ ngày 24/11/2026.

  • #2718 của @atishay-kasliwal — đ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
22/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ệ
python
Lĩnh vực
machine-learning

Hướng nghiên cứu

Start in pytensor/compile/builders.py at _build_and_cache_lop_op, where the cotangent placeholder is created from the output variable's own type — here an integer scalar — and the inner pytensor.grad call then rejects it. Reproduce with the snippet in the issue, then compare against the unwrapped graph to see how a float known_grad reaches the same inner grad. Read the linked discussion #2065 for the Scan case that sends zeros for an integer carry. Note an open pull request (#2718) already targets this, so check its state before starting; done means the wrapped graph differentiates and the float cotangent is handled for the integer output.

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

Mô tả

bug gradients OpFromGraph

pytensor.grad raises on an OpFromGraph with an integer output when that output receives a float cotangent, though the same graph without the wrapper differentiates. _build_and_cache_lop_op builds the placeholder for that cotangent with the output's integer type, and the inner grad call rejects it. Scan hits this whenever its step is an OpFromGraph with an integer carry, because it sends zeros for that carry, as discussed in #2065.

import pytensor
import pytensor.tensor as pt
from pytensor.compile.builders import OpFromGraph

x = pt.dvector("x")
k0 = pt.lscalar("k0")
known_grads = lambda c, k: {c: pt.ones_like(c), k: pt.zeros((), dtype="float64")}

c, k = x * 2, k0 + 1
pytensor.grad(None, x, known_grads=known_grads(c, k))  # works

ix, ik = x.type(), pt.lscalar()
c, k = OpFromGraph([ix, ik], [ix * 2, ik + 1])(x, k0)
pytensor.grad(None, x, known_grads=known_grads(c, k))
# TypeError: Gradients must always be NullType, DisconnectedType, or continuous,
# but grad was given a known_grad of type Scalar(int64, shape=())
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.