`TypeError` in `deref_phi` for a phi node without an edge from every predecessor
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
- 48/100
Hướng nghiên cứu
Start with src/transformation.jl at deref_phi (around line 955) and trace how derive_copyable_task_ir handles phi nodes and their predecessor edges. Use the Julia MWE in the issue to reproduce the failure, then compare it with the all-Bool case and the ordinary function result. Done means the taped task produces the expected sequence without a TypeError, with relevant regression coverage.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Dear Turing,
Thank you for your incredible work!
Below is a Claude-reduced MWE of a problem I ran into when using Turing as a backend for my package. LibTask is far outside my technical understanding of Turing's internals, so I hope this is useful - forgive me if not. Let me know if I can do anything.
--- Claude ---
A TapedTask throws a TypeError when the code of its function has a phi node that has no edge from one of the predecessors of its block, and whose type is narrower than the type of its value. Julia makes such nodes when it splits a Union. The same function runs as an ordinary function.
using Libtask
struct NotRecorded end
# A value with one of two concrete types, depending on an element of a vector
named(value::Bool, state) = (; choice=value, state)
named(value::NotRecorded, state) = (; choice=value, state)
# A method for each of the two types
read_state(current::@NamedTuple{choice::Bool, state::Float64}) = current.state + current.choice
read_state(current::@NamedTuple{choice::NotRecorded, state::Float64}) = current.state
function f(values)
current = named(values[1], 0.0)
for t in 2:length(values)
state = read_state(current)
produce(state)
current = named(values[t], state + 1)
end
return nothing
end
values = Union{NotRecorded,Bool}[true, true, NotRecorded(), true, true]
task = TapedTask(nothing, f, values)
consume(task) # 1.0
consume(task) # 3.0
consume(task) # should be 4.0
ERROR: TypeError: non-boolean (NotRecorded) used in boolean context
Stacktrace:
[1] deref_phi
@ ~/.julia/packages/Libtask/ooOpb/src/transformation.jl:955 [inlined]
[2] f
@ ./issue_code.jl:14 [inlined]
[3] (::Tuple{Base.RefValue{Bool}, Base.RefValue{Union{NotRecorded, Bool}}, …})(...)
@ Base.Experimental ./<missing>:0
[4] consume(t::TapedTask{…})
@ Libtask ~/.julia/packages/Libtask/ooOpb/src/copyable_task.jl:516
With produce replaced by a function that collects the values, f gives [1.0, 3.0, 4.0, 6.0]. With Union{NotRecorded,Bool}[true, true, false, true, true], where every element has the one type, the task gives the right values too.
The phi nodes
This is the end of the loop in Libtask.generate_ir(:input_ir, f, values), where %76 is values[t]:
29 ┄ %74 = builtin Base.getfield(_2, :ref)::MemoryRef{Union{NotRecorded, Bool}}
│ %75 = builtin Base.memoryrefnew(%74, %46, false)::MemoryRef{Union{NotRecorded, Bool}}
│ %76 = builtin Base.memoryrefget(%75, :not_atomic, false)::Union{NotRecorded, Bool}
└─── goto #30
30 ─ %78 = intrinsic Base.add_float(%58, 1.0)::Float64
│ %79 = builtin (isa)(%76, NotRecorded)::Bool
└─── goto #32 if not %79
31 ─ goto #34
32 ─ nothing::Nothing
33 ─ goto #34
34 ┄ %84 = φ (#31 => false, #33 => true)::Bool
│ %85 = φ (#31 => %78)::Float64
│ %86 = φ (#33 => %76)::Bool
│ %87 = φ (#33 => %78)::Float64
%86 has an edge from block 33 only, where %76 is known to be a Bool. When control comes from block 31, %76 is a NotRecorded and %86 is undefined, which is fine in the original code, since nothing reads it on that path.
derive_copyable_task_ir turns every phi node of a block into deref_phi(refs, phi, T) followed by set_ref_at!, at the top of the block, where T is the type of the phi node (ref_index_to_type_map). The only value of %86 is the TupleRef of %76, so when control comes from block 31, deref_phi reads the Ref of %76, which holds a NotRecorded, and ref[]::Bool throws.
As a check of this reading, and not as a fix: with the two methods below, the task gives [1.0, 3.0, 4.0, 6.0].
struct UndefinedPhi end
@inline function Libtask.deref_phi(refs::R, n::Libtask.TupleRef, ::Type{T}) where {R<:Tuple,T}
value = refs[n.n][]
return value isa T ? value : UndefinedPhi()
end
@inline function Libtask.set_ref_at!(refs::R, n::Int, value) where {R<:Tuple}
value isa UndefinedPhi && return nothing
refs[n][] = value
return nothing
end
Where I met it
In Turing.jl, with PG on a model with a loop over timesteps, whose loop variable is a NamedTuple of one of two types, since an action of a timestep is either a Bool or a marker for a value that wasn't recorded. There the phi nodes have NamedTuple types, and the error reads
TypeError: in typeassert, expected @NamedTuple{…, action::@NamedTuple{choice::UnimputedMissingValue}, …}, got a value of type @NamedTuple{…, action::@NamedTuple{choice::Bool}, …}
Stacktrace:
[1] deref_phi
@ ~/.julia/packages/Libtask/ooOpb/src/transformation.jl:955 [inlined]
The model samples with PG when every action is recorded, and with MH in both cases.
Possibly related: #207 (a Ref of a Union) and #204 (variables that are sometimes defined), both closed.
Libtask 0.9.19, Julia 1.13.4, macOS (aarch64).
- Ngôn ngữ chính
- Julia
- Star
- 22
- Fork
- 11
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của TuringLang/Libtask.jl
-
Issues found by claudeĐang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
TuringLang/Libtask.jl#233 ·
Tất cả issue của TuringLang/Libtask.jl
Issue tương tự
-
Release CorleoneInverse as 0.1.1Có thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 78/100
SciML/Corleone.jl#200 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
JuliaAI/MLJTransforms.jl#120 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
exercism/julia#1205 · 3 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
SciML/OrdinaryDiffEq.jl#5175 ·
Maintainer thường phản hồi trong vòng 1 ngày