Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/PolyesterForwardDiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading