Skip to content

Fix segfault when passing friendly tangents to "non-friendly" forward cache in 1.10 and formatting#983

Merged
Technici4n merged 12 commits intochalk-lab:mainfrom
gdalle:gd/forwardcache
Jan 30, 2026
Merged

Fix segfault when passing friendly tangents to "non-friendly" forward cache in 1.10 and formatting#983
Technici4n merged 12 commits intochalk-lab:mainfrom
gdalle:gd/forwardcache

Conversation

@gdalle
Copy link
Copy Markdown
Collaborator

@gdalle gdalle commented Jan 30, 2026

#980 was merged before formatting passed

@gdalle gdalle marked this pull request as ready for review January 30, 2026 07:42
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 30, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@gdalle
Copy link
Copy Markdown
Collaborator Author

gdalle commented Jan 30, 2026

@Technici4n the segfault is happening again, no clue why

@gdalle gdalle changed the title Fix formatting Fix segfault and formatting Jan 30, 2026
@gdalle
Copy link
Copy Markdown
Collaborator Author

gdalle commented Jan 30, 2026

Here's the MWE for the segfault. It happens when we try to use value_and_derivative!! with friendly tangent types even though the cache was not prepared for that:

using Mooncake: Config, zero_tangent, prepare_derivative_cache, value_and_derivative!!

struct SimplePair
    x1::Float64
    x2::Float64
end

f = (x, y) -> x * y + cos(x)
g = (sp::SimplePair) -> SimplePair(f(sp.x1, sp.x2), 2.0)

x, y = 5.0, 4.0
dx, dy = 3.0, 2.0

fx_sp = (g, SimplePair(x, y))
dfx_sp = (zero_tangent(g), SimplePair(dx, dy))

cache_sp_friendly = prepare_derivative_cache(
    fx_sp...; config=Config(; friendly_tangents=true)
)
value_and_derivative!!(cache_sp_friendly, zip(fx_sp, dfx_sp)...)  # works

cache_sp_unfriendly = prepare_derivative_cache(
    fx_sp...; config=Config(; friendly_tangents=false)
)
value_and_derivative!!(cache_sp_unfriendly, zip(fx_sp, dfx_sp)...)  # segfaults on 1.10 only

Comment thread src/interface.jl
@Technici4n
Copy link
Copy Markdown
Collaborator

Is the segfault maybe coming from calling an opaque closure with argument types that don't match the signature? Then it should hopefully go away with debug_mode=true.

@gdalle
Copy link
Copy Markdown
Collaborator Author

gdalle commented Jan 30, 2026

Is the segfault maybe coming from calling an opaque closure with argument types that don't match the signature? Then it should hopefully go away with debug_mode=true.

Maybe that was the root cause, I got it to go away by manually checking that the tangent types match the primal types inside provided Dual numbers. Not sure why this isn't done inside the Dual constructor, probably because of partially initialized structs?

Comment thread src/interface.jl Outdated
Signed-off-by: Guillaume Dalle <[email protected]>
Comment thread src/interface.jl
Comment thread src/interface.jl Outdated
@gdalle
Copy link
Copy Markdown
Collaborator Author

gdalle commented Jan 30, 2026

Are we good vis-a-vis failing tests?

@Technici4n
Copy link
Copy Markdown
Collaborator

Yes, good. Same failures as the Mooncake 0.5 PR + a spurious rules/blas_level_3-1.12-x64 failure.

@Technici4n Technici4n changed the title Fix segfault and formatting Fix segfault when passing friendly tangents to "non-friendly" forward cache and formatting Jan 30, 2026
@Technici4n Technici4n changed the title Fix segfault when passing friendly tangents to "non-friendly" forward cache and formatting Fix segfault when passing friendly tangents to "non-friendly" forward cache in 1.10 and formatting Jan 30, 2026
@Technici4n Technici4n merged commit 504b778 into chalk-lab:main Jan 30, 2026
118 of 141 checks passed
@Technici4n
Copy link
Copy Markdown
Collaborator

Thank you for the quick fixup, let's get this released!

yebai pushed a commit that referenced this pull request Mar 25, 2026
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.

2 participants