`unflatten` alone considered harmful

Đang mở
#995 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 2 ngày

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

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
25/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
julia
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu bằng cách đọc các phương thức VarInfo được nêu trong issue, đặc biệt là unflatten, evaluate!!, setlogp!!, getindex và getindex_internal, đồng thời xem lại cuộc thảo luận được liên kết trong PR #793. Xác định cách một cảnh báo và phương thức unflatten_and_evaluate nên duy trì trạng thái logp nhất quán mà không làm hỏng các tương tác với sampler; phần theo dõi tiếp theo được ghi nhận trong issue #854.

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

Mô tả

enhancement

It leaves VarInfo in an inconsistent state

unflatten updates values in the VarInfo, but doesn't update logp to match it.

I think there are vanishingly few use cases where one actually ONLY wants to perform unflatten without re-evaluating the model.

Conversely, there have been a number of bugs arising from the fact that logp isn't updated after unflatten-ing, and the number of such bugs will only increase as time goes on.

One situation where we DID use to call unflatten without evaluating was the general interaction between VarInfos and samplers:

  1. We have a varinfo as the AbstractMCMC state.
  2. We take vi[:] and pass that to the sampler.
  3. The sampler does its thing and returns us values and a log density.
  4. We call unflatten(vi, new_values) and setlogp!!(vi, new_logp). This avoids re-evaluating the model.

Unfortunately, in DynamicPPL 0.37 logp has been separated into prior and likelihood, and there is no (easy) way of apportioning the log density between prior and likelihood without re-evaluating the model.

(Note that I don't consider this a bad change. We have a richer representation of logp and we just need to do more work to make sure that it is correct.)

It's highly dependent on the internal structure of the VarInfo

On top of the danger of leaving the VarInfo in an inconsistent state, the actual effect of unflatten is highly dependent on for example:

  • which variables are present in the model;
  • the order in which they appear;
  • whether they are linked.

Of these points, the third is the most likely to arise in practice (the first and second can be triggered e.g. with dynamically sized models). For example, it means that initialisation can have wrong results if VarInfos are unexpectedly linked, or if the parameters used to initialise them are themselves linked: https://github.com/TuringLang/DynamicPPL.jl/pull/793#discussion_r1936246041

Proposed solution

IMO, all of this suggests that unflatten alone should be considered extremely dangerous. I would not remove it but I would:

  • stick a huge warning on using unflatten alone. I would personally even consider renaming it to something else like unsafe_setvals.
  • create a new method unflatten_and_evaluate which basically does unflatten and evaluate!!, and skips the warning.

For clarity: I am aware that a function like unflatten is necessary. However, that doesn't imply that we should gratuitously use it, nor does it preclude us from creating 'safer' wrappers around it like unflatten_and_evaluate. Pointer arithmetic also exists in Julia and is necessary, however, we certainly don't encourage people to use it whenever they can.

Follow-ups

There are similar issues whereby setindex!! actually sets internal values, getindex always returns user-parametrised values, getindex_internal always returns internal values, BUT getindex(vi, :) returns internal values. #854

Ngôn ngữ chính
Julia
Star
291
Fork
43
Merge trung bình
1 ngày 23 giờ
Pull request đã merge (30 ngày)
15

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 TuringLang/DynamicPPL.jl

Tất cả issue của TuringLang/DynamicPPL.jl

Issue tương tự

Thêm issue về Julia

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.