scan_checkpoints: argument-handling bugs and tests that cannot fail

Đang mở
#2,677 2 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
55/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
backend, testing

Hướng nghiên cứu

Start with pytensor.scan.scan_checkpoints and tests/scan/test_checkpoints.py, especially the referenced setup_method lines. Read the scan handling of non_sequences, until, recurrent outputs, and sequences, then compare its behavior with scan_checkpoints using the reproducers. Fix each listed failure and make the tests assert meaningful non-underflowing values and exercise both return_updates settings.

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

Mô tả

bug scan tests
  • scan_checkpoints raises TypeError when non_sequences is empty
  • scan_checkpoints ignores until and returns wrong values
  • scan_checkpoints fails when None precedes a recurrent state in outputs_info
  • scan_checkpoints replaces entries of the caller's sequences list
  • test_checkpoints.py compares values that underflow to about 1e-64
  • TestScanCheckpoint never builds its return_updates=False graph
reproducers

non_sequences empty:

import pytensor.tensor as pt
from pytensor.scan import scan_checkpoints

x0 = pt.vector("x0")
A = pt.matrix("A")

scan_checkpoints(
    lambda x: pt.tanh(A @ x),
    outputs_info=[x0],
    n_steps=10,
    save_every_N=3,
    return_updates=False,
)
# TypeError: <lambda>() takes 1 positional argument but 2 were given

until ignored:

import pytensor.tensor as pt
from pytensor.scan import scan, scan_checkpoints, until

x0 = pt.scalar("x0")
A = pt.scalar("A")


def step(x, A):
    return x * A, until(x * A > 50)


kwargs = dict(outputs_info=[x0], non_sequences=[A], n_steps=20, return_updates=False)
reference = scan(step, **kwargs)
checkpointed = scan_checkpoints(step, save_every_N=4, **kwargs)

print(reference.eval({x0: 1.0, A: 2.0}))  # [ 2.  4.  8. 16. 32. 64.]
print(checkpointed.eval({x0: 1.0, A: 2.0}))  # [ 16.  64. 128. 256. 512.]

None before a recurrent state:

import pytensor.tensor as pt
from pytensor.scan import scan_checkpoints

x0 = pt.vector("x0")
A = pt.matrix("A")


def step(x, A):
    x_new = pt.tanh(A @ x)
    return x_new.sum(), x_new


scan_checkpoints(
    step,
    outputs_info=[None, x0],
    non_sequences=[A],
    n_steps=10,
    save_every_N=3,
    return_updates=False,
)
# ValueError: The initial state ... has 2 dimension(s), while the corresponding
# variable in the result of the inner function of scan (`fn`) has 0 dimension(s)

Caller's sequences list modified:

import pytensor.tensor as pt
from pytensor.scan import scan_checkpoints

s = pt.vector("s")
x0 = pt.scalar("x0")
A = pt.scalar("A")
sequences = [s]

scan_checkpoints(
    lambda s_t, x, A: x * A + s_t,
    sequences=sequences,
    outputs_info=[x0],
    non_sequences=[A],
    save_every_N=3,
    return_updates=False,
)
print(sequences[0] is s)  # False
print(sequences[0])  # Join{axis=0}.0

Underflowing test values (test_forward_pass evaluates A**k / k! with A = range(10) and k = 101):

import math

import numpy as np

values = np.array([a**101 / math.factorial(101) for a in range(10)], dtype=float)
print(values.max())  # 2.536e-64
print(np.allclose(values, np.zeros_like(values)))  # True

return_updates parametrization: pytest passes the test method as the second argument of setup_method, so return_updates there is always a truthy bound method.

https://github.com/pymc-devs/pytensor/blob/0c8dbe6f346ad472e6f9cb0f3e594416030d6346/tests/scan/test_checkpoints.py#L12-L14

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.