Skip to content

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

Merged
merged 4 commits into from
Jun 7, 2025
Merged

Fix type stability #634

merged 4 commits into from
Jun 7, 2025

Conversation

wsmoses
Copy link
Contributor

@wsmoses wsmoses commented May 26, 2025

No description provided.

Copy link

codecov bot commented May 27, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.13%. Comparing base (08f4b0f) to head (399f4a9).
Report is 1 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@wheeheee
Copy link
Member

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.

@wsmoses
Copy link
Contributor Author

wsmoses commented May 28, 2025

The previous code failed to be differentiated by Enzyme due to the union, whereas the type stable one works from empirical tests

@wheeheee
Copy link
Member

LGTM then. Do you mind posting examples of how this failure occurs, just for future reference?

make `@code_warntype` prettier
@wsmoses
Copy link
Contributor Author

wsmoses commented May 30, 2025

@SarahWilliamson7 do you have the error log offhand?

@wheeheee
Copy link
Member

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?

@swilliamson7
Copy link

I don't think we printed this error to a text file, so no error log offhand that I can locate :/

@swilliamson7
Copy link

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.

@swilliamson7
Copy link

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)
)

@wheeheee
Copy link
Member

wheeheee commented Jun 7, 2025

I was looking for an example. Can you verify again that this PR fixes the error on your side?

@swilliamson7
Copy link

I can confirm it does fix it.

@wheeheee wheeheee merged commit 12de251 into JuliaDSP:master Jun 7, 2025
10 checks passed
@wheeheee
Copy link
Member

@martinholters should we make a patch release now?

@martinholters
Copy link
Member

Sure, why not. Do you take care of it?

@wheeheee
Copy link
Member

Sure.

@wheeheee
Copy link
Member

ah, forgot to update the version...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants