Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom stacking for StaticArrays #564

Merged
merged 17 commits into from
Oct 10, 2024
Prev Previous commit
Next Next commit
Clearer modulo
  • Loading branch information
gdalle committed Oct 10, 2024
commit 85d319589fe1fc36d200cb9053acdce0279e1d87
Original file line number Diff line number Diff line change
@@ -62,7 +62,7 @@ function _prepare_sparse_hessian_aux(
seeds = [multibasis(backend, x, eachindex(x)[group]) for group in groups]
compressed_matrix = stack(_ -> vec(similar(x)), groups; dims=2)
batched_seeds = [
ntuple(b -> seeds[1 + ((a - 1) * B + (b - 1)) % Ng], Val(B)) for
ntuple(b -> seeds[mod1((a - 1) * B + (b - 1), Ng)], Val(B)) for
a in 1:div(Ng, B, RoundUp)
]
batched_results = [ntuple(b -> similar(x), Val(B)) for _ in batched_seeds]
Original file line number Diff line number Diff line change
@@ -113,7 +113,7 @@ function _prepare_sparse_jacobian_aux(
seeds = [multibasis(backend, x, eachindex(x)[group]) for group in groups]
compressed_matrix = stack(_ -> vec(similar(y)), groups; dims=2)
batched_seeds = [
ntuple(b -> seeds[1 + ((a - 1) * B + (b - 1)) % Ng], Val(B)) for
ntuple(b -> seeds[mod1((a - 1) * B + (b - 1), Ng)], Val(B)) for
a in 1:div(Ng, B, RoundUp)
]
batched_results = [ntuple(b -> similar(y), Val(B)) for _ in batched_seeds]
@@ -150,7 +150,7 @@ function _prepare_sparse_jacobian_aux(
seeds = [multibasis(backend, y, eachindex(y)[group]) for group in groups]
compressed_matrix = stack(_ -> vec(similar(x)), groups; dims=1)
batched_seeds = [
ntuple(b -> seeds[1 + ((a - 1) * B + (b - 1)) % Ng], Val(B)) for
ntuple(b -> seeds[mod1((a - 1) * B + (b - 1), Ng)], Val(B)) for
a in 1:div(Ng, B, RoundUp)
]
batched_results = [ntuple(b -> similar(x), Val(B)) for _ in batched_seeds]
4 changes: 2 additions & 2 deletions DifferentiationInterface/src/first_order/jacobian.jl
Original file line number Diff line number Diff line change
@@ -111,7 +111,7 @@ function _prepare_jacobian_aux(
N = length(x)
seeds = [basis(backend, x, ind) for ind in eachindex(x)]
batched_seeds = [
ntuple(b -> seeds[1 + ((a - 1) * B + (b - 1)) % N], Val(B)) for
ntuple(b -> seeds[mod1((a - 1) * B + (b - 1), N)], Val(B)) for
a in 1:div(N, B, RoundUp)
]
batched_results = [ntuple(b -> similar(y), Val(B)) for _ in batched_seeds]
@@ -138,7 +138,7 @@ function _prepare_jacobian_aux(
M = length(y)
seeds = [basis(backend, y, ind) for ind in eachindex(y)]
batched_seeds = [
ntuple(b -> seeds[1 + ((a - 1) * B + (b - 1)) % M], Val(B)) for
ntuple(b -> seeds[mod1((a - 1) * B + (b - 1), M)], Val(B)) for
a in 1:div(M, B, RoundUp)
]
batched_results = [ntuple(b -> similar(x), Val(B)) for _ in batched_seeds]
6 changes: 2 additions & 4 deletions DifferentiationInterface/src/second_order/hessian.jl
Original file line number Diff line number Diff line change
@@ -82,7 +82,7 @@ function _prepare_hessian_aux(
N = length(x)
seeds = [basis(backend, x, ind) for ind in eachindex(x)]
batched_seeds = [
ntuple(b -> seeds[1 + ((a - 1) * B + (b - 1)) % N], Val(B)) for
ntuple(b -> seeds[mod1((a - 1) * B + (b - 1), N)], Val(B)) for
a in 1:div(N, B, RoundUp)
]
batched_results = [ntuple(b -> similar(x), Val(B)) for _ in batched_seeds]
@@ -111,16 +111,14 @@ function hessian(
f, hvp_prep, backend, x, batched_seeds[1], contexts...
)

hess_blocks = map(eachindex(batched_seeds)) do a
hess = mapreduce(hcat, eachindex(batched_seeds)) do a
dg_batch = hvp(f, hvp_prep_same, backend, x, batched_seeds[a], contexts...)
block = stack_vec_col(dg_batch)
if N % B != 0 && a == lastindex(batched_seeds)
block = block[:, 1:(N - (a - 1) * B)]
end
block
end

hess = reduce(hcat, hess_blocks)
return hess
end

Loading