Skip to content

Commit ec74fbc

Browse files
Stop precomputing chunk sizes into a nonconcrete array (#708)
This prevents constant propagation of the length of a static array. Alternative to #707 Co-authored-by: David Widmann <[email protected]>
1 parent a286920 commit ec74fbc

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/prelude.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ const UNARY_PREDICATES = Symbol[:isinf, :isnan, :isfinite, :iseven, :isodd, :isr
77

88
struct Chunk{N} end
99

10-
const CHUNKS = [Chunk{i}() for i in 1:DEFAULT_CHUNK_THRESHOLD]
11-
1210
function Chunk(input_length::Integer, threshold::Integer = DEFAULT_CHUNK_THRESHOLD)
1311
N = pickchunksize(input_length, threshold)
14-
0 < N <= DEFAULT_CHUNK_THRESHOLD && return CHUNKS[N]
15-
return Chunk{N}()
12+
Base.@nif 12 d->(N == d) d->(Chunk{d}()) d->(Chunk{N}())
1613
end
1714

1815
function Chunk(x::AbstractArray, threshold::Integer = DEFAULT_CHUNK_THRESHOLD)

test/GradientTest.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ end
136136
@test DiffResults.gradient(sresult1) == DiffResults.gradient(result)
137137
@test DiffResults.gradient(sresult2) == DiffResults.gradient(result)
138138
@test DiffResults.gradient(sresult3) == DiffResults.gradient(result)
139+
140+
# make sure this is not a source of type instability
141+
@inferred ForwardDiff.GradientConfig(f, sx)
139142
end
140143

141144
@testset "exponential function at base zero" begin

test/JacobianTest.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ for T in (StaticArrays.SArray, StaticArrays.MArray)
222222
@test DiffResults.jacobian(sresult1) == DiffResults.jacobian(result)
223223
@test DiffResults.jacobian(sresult2) == DiffResults.jacobian(result)
224224
@test DiffResults.jacobian(sresult3) == DiffResults.jacobian(result)
225+
226+
# make sure this is not a source of type instability
227+
@inferred ForwardDiff.JacobianConfig(f, sx)
225228
end
226229

227230
#########
@@ -237,7 +240,7 @@ end
237240
@testset "eigen" begin
238241
@test ForwardDiff.jacobian(x -> eigvals(SymTridiagonal(x, x[1:end-1])), [1.,2.]) [(1 - 3/sqrt(5))/2 (1 - 1/sqrt(5))/2 ; (1 + 3/sqrt(5))/2 (1 + 1/sqrt(5))/2]
239242
@test ForwardDiff.jacobian(x -> eigvals(Symmetric(x*x')), [1.,2.]) [0 0; 2 4]
240-
243+
241244
x0 = [1.0, 2.0];
242245
ev1(x) = eigen(Symmetric(x*x')).vectors[:,1]
243246
@test ForwardDiff.jacobian(ev1, x0) Calculus.finite_difference_jacobian(ev1, x0)

0 commit comments

Comments
 (0)