Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve catch block identification #1552

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/compiler/reverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ end

function adjoint(pr::Primal)
ir, sigs = adjointcfg(pr)
catch_blocks = falses(length(blocks(pr.ir)))
for b in reverse(blocks(pr.ir))
rb = block(ir, b.id)
grads = Dict()
Expand All @@ -309,12 +310,13 @@ function adjoint(pr::Primal)
grad(sigs[b.id][i], arguments(rb)[i])
end

has_leave = false

# Backprop through statements
for v in reverse(keys(b))
ex = b[v].expr
has_leave |= isexpr(ex, :leave)

if isexpr(ex, :catch)
catch_blocks[first(ex.args)] = true
end

if haskey(pr.pullbacks, v)
g = push!(rb, stmt(Expr(:call, alpha(pr.pullbacks[v]), grad(v)),
Expand All @@ -338,16 +340,6 @@ function adjoint(pr::Primal)
end
end

# This is corresponds to a catch blocks which technically
# has predecessors but they are not modelled in the IRTools CFG.
# We put an error message at the beginning of said block.
if has_leave && isempty(predecessors(b)) && b.id != 1
_, f_stmt = first(b)
li = pr.ir.lines[f_stmt.line]
pushfirst!(rb, stmt(xcall(Base, :error,
"Can't differentiate function execution in catch block at $(li.file):$(li.line).")))
end

if b.id > 1 # Backprop through (predecessor) branch arguments
gs = grad.(arguments(b))
for br in branches(rb)
Expand All @@ -368,6 +360,22 @@ function adjoint(pr::Primal)
branches(rb)[1].args[1] = Δ
end
end

for (id, is_catch) in enumerate(catch_blocks)
is_catch || continue

b = block(pr.ir, id)
rb = block(ir, id)
err_message = if isempty(b)
"Can't differentiate function execution in catch block"
else
_, f_stmt = first(b)
li = pr.ir.lines[f_stmt.line]
"Can't differentiate function execution in catch block at $(li.file):$(li.line)."
end
pushfirst!(rb, stmt(xcall(Base, :error, err_message)))
end

return ir
end

Expand Down
12 changes: 2 additions & 10 deletions test/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,7 @@ end
@test res == 12.
@test_throws ErrorException pull(1.)
err = try pull(1.) catch ex; ex end
if VERSION >= v"1.11"
@test_broken occursin("Can't differentiate function execution in catch block", string(err))
else
@test occursin("Can't differentiate function execution in catch block", string(err))
end
@test occursin("Can't differentiate function execution in catch block", string(err))
end

if VERSION >= v"1.8"
Expand Down Expand Up @@ -351,9 +347,5 @@ end
@test_throws ErrorException pull(1.)

err = try pull(1.) catch ex; ex end
if VERSION >= v"1.11"
@test_broken occursin("Can't differentiate function execution in catch block", string(err))
else
@test occursin("Can't differentiate function execution in catch block", string(err))
end
@test occursin("Can't differentiate function execution in catch block", string(err))
end
Loading