diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 948ea39..5a27798 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -20,6 +20,15 @@ jobs: arch: - x86 - x64 + include: + - version: '1' + os: macos-latest + threads: '5' + arch: aarch64 + - version: '~1.10.0-0' + os: macos-latest + threads: '5' + arch: aarch64 steps: - uses: actions/checkout@v6 - uses: julia-actions/setup-julia@v2 diff --git a/src/PolyesterForwardDiff.jl b/src/PolyesterForwardDiff.jl index df12d4d..71bac63 100644 --- a/src/PolyesterForwardDiff.jl +++ b/src/PolyesterForwardDiff.jl @@ -62,8 +62,8 @@ function threaded_gradient!(f::F, Δx::AbstractArray, x::AbstractArray, ::Forwar N = length(x) d = cld_fast(N, C) r = Ref{eltype(Δx)}() - batch((d,min(d,Threads.nthreads())), r, Δx, x) do rΔxx,start,stop - evaluate_chunks!(f, rΔxx, start, stop, ForwardDiff.Chunk{C}(), check) + batch((d,min(d,Threads.nthreads())), f, check, r, Δx, x) do (f, check, r, Δx, x), start, stop + evaluate_chunks!(f, (r, Δx, x), start, stop, ForwardDiff.Chunk{C}(), check) end r[] end @@ -120,8 +120,8 @@ end function threaded_jacobian!(f::F, Δx::AbstractArray, x::AbstractArray, ::ForwardDiff.Chunk{C}, check = Val{false}()) where {F,C} N = length(x) d = cld_fast(N, C) - batch((d,min(d,Threads.nthreads())), Δx, x) do Δxx,start,stop - evaluate_jacobian_chunks!(f, Δxx, start, stop, ForwardDiff.Chunk{C}(), check) + batch((d,min(d,Threads.nthreads())), f, check, Δx, x) do (f, check, Δx, x), start, stop + evaluate_jacobian_chunks!(f, (Δx, x), start, stop, ForwardDiff.Chunk{C}(), check) end return Δx end @@ -179,8 +179,8 @@ end function threaded_jacobian!(f!::F, y::AbstractArray, Δx::AbstractArray, x::AbstractArray, ::ForwardDiff.Chunk{C},check = Val{false}()) where {F,C} N = length(x) d = cld_fast(N, C) - batch((d,min(d,Threads.nthreads())), y, Δx, x) do yΔxx,start,stop - evaluate_f_and_jacobian_chunks!(f!, yΔxx, start, stop, ForwardDiff.Chunk{C}(), check) + batch((d,min(d,Threads.nthreads())), f!, check, y, Δx, x) do (f!, check, y, Δx, x), start, stop + evaluate_f_and_jacobian_chunks!(f!, (y, Δx, x), start, stop, ForwardDiff.Chunk{C}(), check) end Δx end diff --git a/test/runtests.jl b/test/runtests.jl index 1ab6e05..ac2b6ef 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -46,3 +46,35 @@ ForwardDiff.gradient!(dXref, f, X, ForwardDiff.GradientConfig(f, X, ForwardDiff. PolyesterForwardDiff.threaded_gradient!(f, dX, X, ForwardDiff.Chunk(8)); @test dX ≈ dXref + +@testset "closures capturing data" begin + make_loss(obs) = p -> sum(abs2, p .- obs) + sum(sum ∘ sincos, p) + xc = randn(803) + fc = make_loss(randn(803)) + @test !Base.issingletontype(typeof(fc)) + dxc = similar(xc) + ref = ForwardDiff.gradient(fc, xc) + for check in (Val{false}(), Val{true}()) + for _ in 1:50 + dxc .= NaN + val = PolyesterForwardDiff.threaded_gradient!(fc, dxc, xc, ForwardDiff.Chunk(8), check) + @test dxc ≈ ref + @test val ≈ fc(xc) + end + end + + B = randn(100, 803) + hc = let B = B; x -> B * x; end + hc! = let B = B; (y, x) -> (y .= B * x); end + Jref = ForwardDiff.jacobian(hc, xc) + J = similar(Jref); yc = similar(xc, 100) + for check in (Val{false}(), Val{true}()) + J .= NaN + PolyesterForwardDiff.threaded_jacobian!(hc, J, xc, ForwardDiff.Chunk(8), check) + @test J ≈ Jref + J .= NaN; yc .= NaN + PolyesterForwardDiff.threaded_jacobian!(hc!, yc, J, xc, ForwardDiff.Chunk(8), check) + @test J ≈ Jref + @test yc ≈ B * xc + end +end