Skip to content

Commit dd83a19

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 2ff6808 commit dd83a19

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
@@ -14,12 +14,9 @@ const BINARY_PREDICATES = Symbol[:isequal, :isless, :<, :>, :(==), :(!=), :(<=),
1414

1515
struct Chunk{N} end
1616

17-
const CHUNKS = [Chunk{i}() for i in 1:DEFAULT_CHUNK_THRESHOLD]
18-
1917
function Chunk(input_length::Integer, threshold::Integer = DEFAULT_CHUNK_THRESHOLD)
2018
N = pickchunksize(input_length, threshold)
21-
0 < N <= DEFAULT_CHUNK_THRESHOLD && return CHUNKS[N]
22-
return Chunk{N}()
19+
Base.@nif 12 d->(N == d) d->(Chunk{d}()) d->(Chunk{N}())
2320
end
2421

2522
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
@@ -139,6 +139,9 @@ for T in (StaticArrays.SArray, StaticArrays.MArray)
139139
@test DiffResults.gradient(sresult1) == DiffResults.gradient(result)
140140
@test DiffResults.gradient(sresult2) == DiffResults.gradient(result)
141141
@test DiffResults.gradient(sresult3) == DiffResults.gradient(result)
142+
143+
# make sure this is not a source of type instability
144+
@inferred ForwardDiff.GradientConfig(f, sx)
142145
end
143146

144147
@testset "exponential function at base zero" begin

test/JacobianTest.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,9 @@ for T in (StaticArrays.SArray, StaticArrays.MArray)
224224
@test DiffResults.jacobian(sresult1) == DiffResults.jacobian(result)
225225
@test DiffResults.jacobian(sresult2) == DiffResults.jacobian(result)
226226
@test DiffResults.jacobian(sresult3) == DiffResults.jacobian(result)
227+
228+
# make sure this is not a source of type instability
229+
@inferred ForwardDiff.JacobianConfig(f, sx)
227230
end
228231

229232
@testset "dimension errors for jacobian" begin
@@ -235,7 +238,7 @@ end
235238
@testset "eigen" begin
236239
@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]
237240
@test ForwardDiff.jacobian(x -> eigvals(Symmetric(x*x')), [1.,2.]) [0 0; 2 4]
238-
241+
239242
x0 = [1.0, 2.0];
240243
ev1(x) = eigen(Symmetric(x*x')).vectors[:,1]
241244
@test ForwardDiff.jacobian(ev1, x0) Calculus.finite_difference_jacobian(ev1, x0)

0 commit comments

Comments
 (0)