Conversation
Contributor
|
Your PR requires formatting changes to meet the project's style guidelines. Click here to view the suggested changes.diff --git a/src/internal_rules/bigfloat.jl b/src/internal_rules/bigfloat.jl
index 1c2c93e0..c8454596 100644
--- a/src/internal_rules/bigfloat.jl
+++ b/src/internal_rules/bigfloat.jl
@@ -71,12 +71,12 @@ end
# for which there is no derivative, so any active use of an integer-constructed
# BigFloat otherwise fails with `EnzymeNoDerivativeError`.
function EnzymeRules.forward(
- config::EnzymeRules.FwdConfig,
- Ty::Const{Type{BigFloat}},
- RT::Type{<:Union{DuplicatedNoNeed,Duplicated,BatchDuplicated,BatchDuplicatedNoNeed}},
- x::Const{Ti},
- rs::Const{Base.MPFR.MPFRRoundingMode}...;
- kwargs...,
+ config::EnzymeRules.FwdConfig,
+ Ty::Const{Type{BigFloat}},
+ RT::Type{<:Union{DuplicatedNoNeed, Duplicated, BatchDuplicated, BatchDuplicatedNoNeed}},
+ x::Const{Ti},
+ rs::Const{Base.MPFR.MPFRRoundingMode}...;
+ kwargs...,
) where {Ti <: Union{Clong, Culong}}
rvals = map(r -> r.val, rs)
if EnzymeRules.needs_primal(config) && EnzymeRules.needs_shadow(config)
@@ -109,12 +109,12 @@ function EnzymeRules.forward(
end
function EnzymeRules.augmented_primal(
- config::EnzymeRules.RevConfig,
- Ty::Const{Type{BigFloat}},
- RT::Type{<:Union{DuplicatedNoNeed,Duplicated,BatchDuplicated,BatchDuplicatedNoNeed}},
- x::Const{Ti},
- rs::Const{Base.MPFR.MPFRRoundingMode}...;
- kwargs...,
+ config::EnzymeRules.RevConfig,
+ Ty::Const{Type{BigFloat}},
+ RT::Type{<:Union{DuplicatedNoNeed, Duplicated, BatchDuplicated, BatchDuplicatedNoNeed}},
+ x::Const{Ti},
+ rs::Const{Base.MPFR.MPFRRoundingMode}...;
+ kwargs...,
) where {Ti <: Union{Clong, Culong}}
rvals = map(r -> r.val, rs)
primal = if EnzymeRules.needs_primal(config)
@@ -136,14 +136,14 @@ function EnzymeRules.augmented_primal(
end
function EnzymeRules.reverse(
- config::EnzymeRules.RevConfig,
- Ty::Const{Type{BigFloat}},
- RT::Type{<:Union{DuplicatedNoNeed,Duplicated,BatchDuplicated,BatchDuplicatedNoNeed}},
- tape,
- x::Const{<:Union{Culong, Clong}},
- rs::Const{Base.MPFR.MPFRRoundingMode}...;
- kwargs...,
-)
+ config::EnzymeRules.RevConfig,
+ Ty::Const{Type{BigFloat}},
+ RT::Type{<:Union{DuplicatedNoNeed, Duplicated, BatchDuplicated, BatchDuplicatedNoNeed}},
+ tape,
+ x::Const{<:Union{Culong, Clong}},
+ rs::Const{Base.MPFR.MPFRRoundingMode}...;
+ kwargs...,
+ )
# the integer argument carries no derivative
return ntuple(Returns(nothing), Val(1 + length(rs)))
end |
kshyatt
force-pushed
the
ksh/bigfloat-int-constructors
branch
from
August 15, 2026 09:03
02d21ea to
921c454
Compare
Contributor
Benchmark Results
Benchmark PlotsA plot of the benchmark results has been uploaded as an artifact at https://github.com/EnzymeAD/Enzyme.jl/actions/runs/36103894614/artifacts/10850443528. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3461 +/- ##
==========================================
+ Coverage 77.43% 78.44% +1.01%
==========================================
Files 68 68
Lines 23278 22966 -312
==========================================
- Hits 18025 18016 -9
+ Misses 5253 4950 -303 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
kshyatt
force-pushed
the
ksh/bigfloat-int-constructors
branch
from
August 16, 2026 18:31
921c454 to
c2ed260
Compare
wsmoses
reviewed
Aug 25, 2026
wsmoses
reviewed
Aug 25, 2026
| rs::Const{Base.MPFR.MPFRRoundingMode}...; | ||
| kwargs..., | ||
| ) where {Ti <: Union{Clong, Culong}} | ||
| rvals = map(r -> r.val, rs) |
Member
There was a problem hiding this comment.
closures are bad
Citation needed?
kshyatt
force-pushed
the
ksh/bigfloat-int-constructors
branch
from
September 17, 2026 10:19
c2ed260 to
b894ba5
Compare
`BigFloat(x::Clong)` and `BigFloat(x::Culong)` lower to `mpfr_set_si` /
`mpfr_set_ui`, for which there is no derivative, so any active use of an
integer-constructed BigFloat failed with `EnzymeNoDerivativeError`.
The argument is an integer, so the result never carries a derivative.
This cannot be expressed as `inactive`, though: the result is a fresh
mutable BigFloat that subsequent MPFR calls may write into, so it still
needs a shadow to exist -- the same reason the existing no-argument
`BigFloat()` rule allocates one. The shadow is built through the same
constructor with a zero value, so it lands at the requested precision as
a properly initialised MPFR value rather than raw zeroed memory.
Having a rule here also keeps Enzyme from compiling the body of these
constructors at all, which matters beyond the missing derivative:
`eps(::Type{BigFloat})` is `nextfloat(BigFloat(1)) - BigFloat(1)`, and
compiling that construction inside an Enzyme-generated function
miscompiles it into a BigFloat with an invalid `d::Memory`, segfaulting
on first use. This repro segfaults before the change and passes after:
f(out) = (out[1] = eps(BigFloat) > 0 ? 1.0 : 0.0; nothing)
Enzyme.autodiff(Forward, Const(f), Const(zeros(3)))
Note the reverse rules are untested: reverse-mode BigFloat scalar
arithmetic currently returns zero rather than the correct derivative,
independently of these constructors, so there is nothing meaningful to
assert yet.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
kshyatt
force-pushed
the
ksh/bigfloat-int-constructors
branch
from
September 25, 2026 06:40
b894ba5 to
2223e29
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BigFloat(x::Clong)andBigFloat(x::Culong)lower tompfr_set_si/mpfr_set_ui, for which there is no derivative, so any active use of an integer-constructed BigFloat failed withEnzymeNoDerivativeError.The argument is an integer, so the result never carries a derivative. This cannot be expressed as
inactive, though: the result is a fresh mutable BigFloat that subsequent MPFR calls may write into, so it still needs a shadow to exist -- the same reason the existing no-argumentBigFloat()rule allocates one. The shadow is built through the same constructor with a zero value, so it lands at the requested precision as a properly initialised MPFR value rather than raw zeroed memory.Having a rule here also keeps Enzyme from compiling the body of these constructors at all, which matters beyond the missing derivative:
eps(::Type{BigFloat})isnextfloat(BigFloat(1)) - BigFloat(1), and compiling that construction inside an Enzyme-generated function miscompiles it into a BigFloat with an invalidd::Memory, segfaulting on first use. This repro segfaults before the change and passes after:Developed with 🤖