-
Notifications
You must be signed in to change notification settings - Fork 115
Fix type stability #634
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
Fix type stability #634
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #634 +/- ##
=======================================
Coverage 98.13% 98.13%
=======================================
Files 19 19
Lines 3277 3277
=======================================
Hits 3216 3216
Misses 61 61 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Iirc I tried something like that in the past and it was slightly slower. The small union is not too bad anyway and I think the whole function ends up type stable. |
The previous code failed to be differentiated by Enzyme due to the union, whereas the type stable one works from empirical tests |
LGTM then. Do you mind posting examples of how this failure occurs, just for future reference? |
@SarahWilliamson7 do you have the error log offhand? |
GitHub's docs says they might not be notified by the mention, since they're not in the thread or in this organization, so it might be better to contact them somewhere else... Also, perhaps you meant @swilliamson7? |
I don't think we printed this error to a text file, so no error log offhand that I can locate :/ |
I believe I recreated it, this is the error output: ERROR: Enzyme compilation failed due to illegal type analysis.
This usually indicates the use of a Union type, which is not fully supported with Enzyme.API.strictAliasing set to true [the default].
Ideally, remove the union (which will also make your code faster), or try setting Enzyme.API.strictAliasing!(false) before any autodiff call.
To toggle more information for debugging (needed for bug reports), set Enzyme.Compiler.VERBOSE_ERRORS[] = true (default false)
Failure within method: MethodInstance for DSP.Periodograms.fft2pow2radial!(::Vector{Float32}, ::Matrix{ComplexF32}, ::Int64, ::Int64, ::Int64, ::Int64)
Hint: catch this exception as `err` and call `code_typed(err; interactive = true)` to introspect the erroneous code with Cthulhu.jl
Caused by:
Stacktrace:
[1] fft2pow2radial!
@ ~/.julia/packages/DSP/oCNW8/src/periodograms.jl:207
Stacktrace:
[1] julia_error(msg::String, val::Ptr{…}, errtype::Enzyme.API.ErrorType, data::Ptr{…}, data2::Ptr{…}, B::Ptr{…})
@ Enzyme.Compiler ~/.julia/packages/Enzyme/4oLyn/src/errors.jl:419
[2] julia_error(cstr::Cstring, val::Ptr{…}, errtype::Enzyme.API.ErrorType, data::Ptr{…}, data2::Ptr{…}, B::Ptr{…})
@ Enzyme.Compiler ~/.julia/packages/Enzyme/4oLyn/src/errors.jl:300
[3] EnzymeCreatePrimalAndGradient(logic::Enzyme.Logic, todiff::LLVM.Function, retType::Enzyme.API.CDIFFE_TYPE, constant_args::Vector{…}, TA::Enzyme.TypeAnalysis, returnValue::Bool, dretUsed::Bool, mode::Enzyme.API.CDerivativeMode, runtimeActivity::Bool, strongZero::Bool, width::Int64, additionalArg::Ptr{…}, forceAnonymousTape::Bool, typeInfo::Enzyme.FnTypeInfo, uncacheable_args::Vector{…}, augmented::Ptr{…}, atomicAdd::Bool)
@ Enzyme.API ~/.julia/packages/Enzyme/4oLyn/src/api.jl:270
[4] enzyme!(job::GPUCompiler.CompilerJob{…}, mod::LLVM.Module, primalf::LLVM.Function, TT::Type, mode::Enzyme.API.CDerivativeMode, width::Int64, parallel::Bool, actualRetType::Type, wrap::Bool, modifiedBetween::Tuple{…} where N, returnPrimal::Bool, expectedTapeType::Type, loweredArgs::Set{…}, boxedArgs::Set{…})
@ Enzyme.Compiler ~/.julia/packages/Enzyme/4oLyn/src/compiler.jl:1760
[5] codegen(output::Symbol, job::GPUCompiler.CompilerJob{…}; libraries::Bool, deferred_codegen::Bool, optimize::Bool, toplevel::Bool, strip::Bool, validate::Bool, only_entry::Bool, parent_job::Nothing)
@ Enzyme.Compiler ~/.julia/packages/Enzyme/4oLyn/src/compiler.jl:4700
[6] codegen
@ ~/.julia/packages/Enzyme/4oLyn/src/compiler.jl:3463 [inlined]
[7] _thunk(job::GPUCompiler.CompilerJob{Enzyme.Compiler.EnzymeTarget, Enzyme.Compiler.EnzymeCompilerParams}, postopt::Bool)
@ Enzyme.Compiler ~/.julia/packages/Enzyme/4oLyn/src/compiler.jl:5561
[8] _thunk
@ ~/.julia/packages/Enzyme/4oLyn/src/compiler.jl:5561 [inlined]
[9] cached_compilation
@ ~/.julia/packages/Enzyme/4oLyn/src/compiler.jl:5613 [inlined]
[10] thunkbase(mi::Core.MethodInstance, World::UInt64, FA::Type{…}, A::Type{…}, TT::Type, Mode::Enzyme.API.CDerivativeMode, width::Int64, ModifiedBetween::Tuple{…} where N, ReturnPrimal::Bool, ShadowInit::Bool, ABI::Type, ErrIfFuncWritten::Bool, RuntimeActivity::Bool, StrongZero::Bool, edges::Vector{…})
@ Enzyme.Compiler ~/.julia/packages/Enzyme/4oLyn/src/compiler.jl:5727
[11] thunk_generator(world::UInt64, source::LineNumberNode, FA::Type, A::Type, TT::Type, Mode::Enzyme.API.CDerivativeMode, Width::Int64, ModifiedBetween::Tuple{…} where N, ReturnPrimal::Bool, ShadowInit::Bool, ABI::Type, ErrIfFuncWritten::Bool, RuntimeActivity::Bool, StrongZero::Bool, self::Any, fakeworld::Any, fa::Type, a::Type, tt::Type, mode::Type, width::Type, modifiedbetween::Type, returnprimal::Type, shadowinit::Type, abi::Type, erriffuncwritten::Type, runtimeactivity::Type, strongzero::Type)
@ Enzyme.Compiler ~/.julia/packages/Enzyme/4oLyn/src/compiler.jl:5915
[12] autodiff
@ ~/.julia/packages/Enzyme/4oLyn/src/Enzyme.jl:496 [inlined]
[13] autodiff
@ ~/.julia/packages/Enzyme/4oLyn/src/Enzyme.jl:536 [inlined]
[14] macro expansion
@ ./timing.jl:279 [inlined]
[15] compare_gradients()
@ Main ./REPL[33]:75
[16] top-level scope
@ REPL[34]:1
Some type information was truncated. Use `show(err)` to see complete types. |
and this will produce the above error (I didn't quite understand if the error output or an example was needed) using Enzyme
using DSP
function f(x)
dsp_x = power(periodogram(x; radialavg=true))
J = sum(dsp_x)
return J
end
x = 0.01 .* randn(10, 10)
dx = zeros(10,10)
J = autodiff(
set_runtime_activity(Enzyme.ReverseWithPrimal),
f,
Duplicated(x, dx)
) |
I was looking for an example. Can you verify again that this PR fixes the error on your side? |
I can confirm it does fix it. |
@martinholters should we make a patch release now? |
Sure, why not. Do you take care of it? |
Sure. |
ah, forgot to update the version... |
No description provided.