|
| 1 | +const HINT_END = "\n\nThis hint appears because DifferentiationInterface and Enzyme are both loaded. It does not necessarily imply that Enzyme is being called through DifferentiationInterface.\n\n" |
| 2 | + |
| 3 | +function HINT_START(option) |
| 4 | + return "\nIf you are using Enzyme by selecting the `AutoEnzyme` object from ADTypes, you may want to try setting the `$option` option as follows:" |
| 5 | +end |
| 6 | + |
| 7 | +function __init__() |
| 8 | + # robust against internal changes |
| 9 | + condition = ( |
| 10 | + isdefined(Enzyme, :Compiler) && |
| 11 | + Enzyme.Compiler isa Module && |
| 12 | + isdefined(Enzyme.Compiler, :EnzymeError) && |
| 13 | + Enzyme.Compiler.EnzymeError isa DataType |
| 14 | + ) |
| 15 | + condition || return nothing |
| 16 | + # see https://github.com/JuliaLang/julia/issues/58367 for why this isn't easier |
| 17 | + for n in names(Enzyme.Compiler; all=true) |
| 18 | + T = getfield(Enzyme.Compiler, n) |
| 19 | + if T isa DataType && T <: Enzyme.Compiler.EnzymeError |
| 20 | + # robust against internal changes |
| 21 | + Base.Experimental.register_error_hint(T) do io, exc |
| 22 | + if occursin("EnzymeMutabilityException", string(nameof(T))) |
| 23 | + printstyled(io, HINT_START("function_annotation"); bold=true) |
| 24 | + printstyled( |
| 25 | + io, |
| 26 | + "\n\n\tAutoEnzyme(; function_annotation=Enzyme.Duplicated)"; |
| 27 | + color=:cyan, |
| 28 | + bold=true, |
| 29 | + ) |
| 30 | + printstyled(io, HINT_END; italic=true) |
| 31 | + elseif occursin("EnzymeRuntimeActivityError", string(nameof(T))) |
| 32 | + printstyled(io, HINT_START("mode"); bold=true) |
| 33 | + printstyled( |
| 34 | + io, |
| 35 | + "\n\n\tAutoEnzyme(; mode=Enzyme.set_runtime_activity(Enzyme.Forward))\n\tAutoEnzyme(; mode=Enzyme.set_runtime_activity(Enzyme.Reverse))"; |
| 36 | + color=:cyan, |
| 37 | + bold=true, |
| 38 | + ) |
| 39 | + printstyled(io, HINT_END; italic=true) |
| 40 | + end |
| 41 | + end |
| 42 | + end |
| 43 | + end |
| 44 | +end |
0 commit comments