Skip to content

Commit 7b5d4f0

Browse files
Merge pull request #162 from JuliaDiff/arrayinterface
Update to ArrayInterface v7
2 parents 0402436 + f2d8ed6 commit 7b5d4f0

7 files changed

Lines changed: 76 additions & 88 deletions

File tree

Project.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ uuid = "6a86dc24-6348-571c-b903-95158fe2bd41"
33
version = "2.17.0"
44

55
[deps]
6-
ArrayInterfaceCore = "30b0a656-2188-435a-8636-2ec0e6a096e2"
6+
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
88
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
99
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
1010
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1111
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
1212

1313
[compat]
14-
ArrayInterfaceCore = "0.1.1"
15-
Requires = "0.5, 1.0"
16-
Setfield = "0.8, 1"
17-
StaticArrays = "0.10, 0.11, 0.12, 1.0"
14+
ArrayInterface = "7"
15+
Requires = "1.0"
16+
Setfield = "1"
17+
StaticArrays = "1.0"
1818
julia = "1.6"
1919

2020
[extras]

README.md

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -535,15 +535,4 @@ HessianCache(x,fdtype::Type{T1}=Val{:hcentral},
535535
HessianCache(xpp,xpm,xmp,xmm,
536536
fdtype::Type{T1}=Val{:hcentral},
537537
inplace::Type{Val{T2}} = x isa StaticArray ? Val{true} : Val{false})
538-
```
539-
540-
# Note about sparse differentiation of GPUArrays, BandedMatrices, and BlockBandedMatrices
541-
542-
These two matrix types need the dependencies ArrayInterfaceBandedMatrices.jl and
543-
ArrayInterfaceBlockBandedMatrices.jl to basically work with any functionality
544-
(anywhere). For now, the right thing to do is to add these libraries and do
545-
`import` on them if you are using BandedMatrices.jl or BlockBandedMatrices.jl
546-
for sparsity patterns. In the future, those two packages should just depend on
547-
ArrayInterface.jl and remove this issue entirely from the user space.
548-
549-
Additionally, GPUs need ArrayInterfaceGPUArrays for proper determination of the indexing.
538+
```

src/FiniteDiff.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module FiniteDiff
22

3-
4-
using LinearAlgebra, SparseArrays, StaticArrays, ArrayInterfaceCore, Requires
3+
using LinearAlgebra, SparseArrays, StaticArrays, ArrayInterface, Requires
54

65
import Base: resize!
76

src/gradients.jl

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -181,20 +181,20 @@ function finite_difference_gradient!(
181181
# NOTE: in this case epsilon is a vector, we need two arrays for epsilon and x1
182182
# c1 denotes x1, c2 is epsilon
183183
fx, c1, c2, c3 = cache.fx, cache.c1, cache.c2, cache.c3
184-
if fdtype != Val(:complex) && ArrayInterfaceCore.fast_scalar_indexing(c2)
184+
if fdtype != Val(:complex) && ArrayInterface.fast_scalar_indexing(c2)
185185
@. c2 = compute_epsilon(fdtype, x, relstep, absstep, dir)
186186
copyto!(c1, x)
187187
end
188188
copyto!(c3, x)
189189
if fdtype == Val(:forward)
190190
@inbounds for i eachindex(x)
191-
if ArrayInterfaceCore.fast_scalar_indexing(c2)
192-
epsilon = ArrayInterfaceCore.allowed_getindex(c2, i) * dir
191+
if ArrayInterface.fast_scalar_indexing(c2)
192+
epsilon = ArrayInterface.allowed_getindex(c2, i) * dir
193193
else
194194
epsilon = compute_epsilon(fdtype, x, relstep, absstep, dir) * dir
195195
end
196-
c1_old = ArrayInterfaceCore.allowed_getindex(c1, i)
197-
ArrayInterfaceCore.allowed_setindex!(c1, c1_old + epsilon, i)
196+
c1_old = ArrayInterface.allowed_getindex(c1, i)
197+
ArrayInterface.allowed_setindex!(c1, c1_old + epsilon, i)
198198
if typeof(fx) != Nothing
199199
dfi = (f(c1) - fx) / epsilon
200200
else
@@ -203,51 +203,51 @@ function finite_difference_gradient!(
203203
end
204204
df_tmp = real(dfi)
205205
if eltype(df) <: Complex
206-
ArrayInterfaceCore.allowed_setindex!(c1, c1_old + im * epsilon, i)
206+
ArrayInterface.allowed_setindex!(c1, c1_old + im * epsilon, i)
207207
if typeof(fx) != Nothing
208208
dfi = (f(c1) - fx) / (im * epsilon)
209209
else
210210
dfi = (f(c1) - fx0) / (im * epsilon)
211211
end
212-
ArrayInterfaceCore.allowed_setindex!(c1, c1_old, i)
213-
ArrayInterfaceCore.allowed_setindex!(df, df_tmp - im * imag(dfi), i)
212+
ArrayInterface.allowed_setindex!(c1, c1_old, i)
213+
ArrayInterface.allowed_setindex!(df, df_tmp - im * imag(dfi), i)
214214
else
215-
ArrayInterfaceCore.allowed_setindex!(df, df_tmp, i)
216-
ArrayInterfaceCore.allowed_setindex!(c1, c1_old, i)
215+
ArrayInterface.allowed_setindex!(df, df_tmp, i)
216+
ArrayInterface.allowed_setindex!(c1, c1_old, i)
217217
end
218218
end
219219
elseif fdtype == Val(:central)
220220
@inbounds for i eachindex(x)
221-
if ArrayInterfaceCore.fast_scalar_indexing(c2)
222-
epsilon = ArrayInterfaceCore.allowed_getindex(c2, i) * dir
221+
if ArrayInterface.fast_scalar_indexing(c2)
222+
epsilon = ArrayInterface.allowed_getindex(c2, i) * dir
223223
else
224224
epsilon = compute_epsilon(fdtype, x, relstep, absstep, dir) * dir
225225
end
226-
c1_old = ArrayInterfaceCore.allowed_getindex(c1, i)
227-
ArrayInterfaceCore.allowed_setindex!(c1, c1_old + epsilon, i)
228-
x_old = ArrayInterfaceCore.allowed_getindex(x, i)
229-
ArrayInterfaceCore.allowed_setindex!(c3, x_old - epsilon, i)
226+
c1_old = ArrayInterface.allowed_getindex(c1, i)
227+
ArrayInterface.allowed_setindex!(c1, c1_old + epsilon, i)
228+
x_old = ArrayInterface.allowed_getindex(x, i)
229+
ArrayInterface.allowed_setindex!(c3, x_old - epsilon, i)
230230
df_tmp = real((f(c1) - f(c3)) / (2 * epsilon))
231231
if eltype(df) <: Complex
232-
ArrayInterfaceCore.allowed_setindex!(c1, c1_old + im * epsilon, i)
233-
ArrayInterfaceCore.allowed_setindex!(c3, x_old - im * epsilon, i)
232+
ArrayInterface.allowed_setindex!(c1, c1_old + im * epsilon, i)
233+
ArrayInterface.allowed_setindex!(c3, x_old - im * epsilon, i)
234234
df_tmp2 = im * imag((f(c1) - f(c3)) / (2 * im * epsilon))
235-
ArrayInterfaceCore.allowed_setindex!(df, df_tmp - df_tmp2, i)
235+
ArrayInterface.allowed_setindex!(df, df_tmp - df_tmp2, i)
236236
else
237-
ArrayInterfaceCore.allowed_setindex!(df, df_tmp, i)
237+
ArrayInterface.allowed_setindex!(df, df_tmp, i)
238238
end
239-
ArrayInterfaceCore.allowed_setindex!(c1, c1_old, i)
240-
ArrayInterfaceCore.allowed_setindex!(c3, x_old, i)
239+
ArrayInterface.allowed_setindex!(c1, c1_old, i)
240+
ArrayInterface.allowed_setindex!(c3, x_old, i)
241241
end
242242
elseif fdtype == Val(:complex) && returntype <: Real
243243
copyto!(c1, x)
244244
epsilon_complex = eps(real(eltype(x)))
245245
# we use c1 here to avoid typing issues with x
246246
@inbounds for i eachindex(x)
247-
c1_old = ArrayInterfaceCore.allowed_getindex(c1, i)
248-
ArrayInterfaceCore.allowed_setindex!(c1, c1_old + im * epsilon_complex, i)
249-
ArrayInterfaceCore.allowed_setindex!(df, imag(f(c1)) / epsilon_complex, i)
250-
ArrayInterfaceCore.allowed_setindex!(c1, c1_old, i)
247+
c1_old = ArrayInterface.allowed_getindex(c1, i)
248+
ArrayInterface.allowed_setindex!(c1, c1_old + im * epsilon_complex, i)
249+
ArrayInterface.allowed_setindex!(df, imag(f(c1)) / epsilon_complex, i)
250+
ArrayInterface.allowed_setindex!(c1, c1_old, i)
251251
end
252252
else
253253
fdtype_error(returntype)

src/hessians.jl

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,27 @@ function finite_difference_hessian!(H,f,x,
6868
end
6969

7070
for i = 1:n
71-
xi = ArrayInterfaceCore.allowed_getindex(x,i)
71+
xi = ArrayInterface.allowed_getindex(x,i)
7272
epsilon = compute_epsilon(Val(:hcentral), xi, relstep, absstep)
7373

7474
if inplace === Val(true)
75-
ArrayInterfaceCore.allowed_setindex!(xpp,xi + epsilon,i)
76-
ArrayInterfaceCore.allowed_setindex!(xmm,xi - epsilon,i)
75+
ArrayInterface.allowed_setindex!(xpp,xi + epsilon,i)
76+
ArrayInterface.allowed_setindex!(xmm,xi - epsilon,i)
7777
else
7878
_xpp = setindex(xpp,xi + epsilon, i)
7979
_xmm = setindex(xmm,xi - epsilon, i)
8080
end
8181

82-
ArrayInterfaceCore.allowed_setindex!(H,(f(_xpp) - 2*fx + f(_xmm)) / epsilon^2,i,i)
82+
ArrayInterface.allowed_setindex!(H,(f(_xpp) - 2*fx + f(_xmm)) / epsilon^2,i,i)
8383
epsiloni = compute_epsilon(Val(:central), xi, relstep, absstep)
8484
xp = xi + epsiloni
8585
xm = xi - epsiloni
8686

8787
if inplace === Val(true)
88-
ArrayInterfaceCore.allowed_setindex!(xpp,xp,i)
89-
ArrayInterfaceCore.allowed_setindex!(xpm,xp,i)
90-
ArrayInterfaceCore.allowed_setindex!(xmp,xm,i)
91-
ArrayInterfaceCore.allowed_setindex!(xmm,xm,i)
88+
ArrayInterface.allowed_setindex!(xpp,xp,i)
89+
ArrayInterface.allowed_setindex!(xpm,xp,i)
90+
ArrayInterface.allowed_setindex!(xmp,xm,i)
91+
ArrayInterface.allowed_setindex!(xmm,xm,i)
9292
else
9393
_xpp = setindex(xpp,xp,i)
9494
_xpm = setindex(xpm,xp,i)
@@ -97,30 +97,30 @@ function finite_difference_hessian!(H,f,x,
9797
end
9898

9999
for j = i+1:n
100-
xj = ArrayInterfaceCore.allowed_getindex(x,j)
100+
xj = ArrayInterface.allowed_getindex(x,j)
101101
epsilonj = compute_epsilon(Val(:central), xj, relstep, absstep)
102102
xp = xj + epsilonj
103103
xm = xj - epsilonj
104104

105105
if inplace === Val(true)
106-
ArrayInterfaceCore.allowed_setindex!(xpp,xp,j)
107-
ArrayInterfaceCore.allowed_setindex!(xpm,xm,j)
108-
ArrayInterfaceCore.allowed_setindex!(xmp,xp,j)
109-
ArrayInterfaceCore.allowed_setindex!(xmm,xm,j)
106+
ArrayInterface.allowed_setindex!(xpp,xp,j)
107+
ArrayInterface.allowed_setindex!(xpm,xm,j)
108+
ArrayInterface.allowed_setindex!(xmp,xp,j)
109+
ArrayInterface.allowed_setindex!(xmm,xm,j)
110110
else
111111
_xpp = setindex(_xpp,xp,j)
112112
_xpm = setindex(_xpm,xm,j)
113113
_xmp = setindex(_xmp,xp,j)
114114
_xmm = setindex(_xmm,xm,j)
115115
end
116116

117-
ArrayInterfaceCore.allowed_setindex!(H,(f(_xpp) - f(_xpm) - f(_xmp) + f(_xmm))/(4*epsiloni*epsilonj),i,j)
117+
ArrayInterface.allowed_setindex!(H,(f(_xpp) - f(_xpm) - f(_xmp) + f(_xmm))/(4*epsiloni*epsilonj),i,j)
118118

119119
if inplace === Val(true)
120-
ArrayInterfaceCore.allowed_setindex!(xpp,xj,j)
121-
ArrayInterfaceCore.allowed_setindex!(xpm,xj,j)
122-
ArrayInterfaceCore.allowed_setindex!(xmp,xj,j)
123-
ArrayInterfaceCore.allowed_setindex!(xmm,xj,j)
120+
ArrayInterface.allowed_setindex!(xpp,xj,j)
121+
ArrayInterface.allowed_setindex!(xpm,xj,j)
122+
ArrayInterface.allowed_setindex!(xmp,xj,j)
123+
ArrayInterface.allowed_setindex!(xmm,xj,j)
124124
else
125125
_xpp = setindex(_xpp,xj,j)
126126
_xpm = setindex(_xpm,xj,j)
@@ -130,10 +130,10 @@ function finite_difference_hessian!(H,f,x,
130130
end
131131

132132
if inplace === Val(true)
133-
ArrayInterfaceCore.allowed_setindex!(xpp,xi,i)
134-
ArrayInterfaceCore.allowed_setindex!(xpm,xi,i)
135-
ArrayInterfaceCore.allowed_setindex!(xmp,xi,i)
136-
ArrayInterfaceCore.allowed_setindex!(xmm,xi,i)
133+
ArrayInterface.allowed_setindex!(xpp,xi,i)
134+
ArrayInterface.allowed_setindex!(xpm,xi,i)
135+
ArrayInterface.allowed_setindex!(xmp,xi,i)
136+
ArrayInterface.allowed_setindex!(xmm,xi,i)
137137
end
138138
end
139139
LinearAlgebra.copytri!(H,'U')

src/iteration_utils.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ end
2929
end
3030

3131
#override default setting of using findstructralnz
32-
_use_findstructralnz(sparsity) = ArrayInterfaceCore.has_sparsestruct(sparsity)
32+
_use_findstructralnz(sparsity) = ArrayInterface.has_sparsestruct(sparsity)
3333
_use_findstructralnz(::SparseMatrixCSC) = false
3434

3535
# test if J, sparsity are both SparseMatrixCSC and have the same sparsity pattern of stored values

src/jacobians.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ function finite_difference_jacobian(
183183
nrows, ncols = size(J)
184184

185185
if !(sparsity isa Nothing)
186-
rows_index, cols_index = ArrayInterfaceCore.findstructralnz(sparsity)
186+
rows_index, cols_index = ArrayInterface.findstructralnz(sparsity)
187187
rows_index = [rows_index[i] for i in 1:length(rows_index)]
188188
cols_index = [cols_index[i] for i in 1:length(cols_index)]
189189
end
190190

191191
if fdtype == Val(:forward)
192192

193193
function calculate_Ji_forward(i)
194-
x_save = ArrayInterfaceCore.allowed_getindex(vecx, i)
194+
x_save = ArrayInterface.allowed_getindex(vecx, i)
195195
epsilon = compute_epsilon(Val(:forward), x_save, relstep, absstep, dir)
196196
_vecx1 = setindex(vecx, x_save+epsilon, i)
197197
_x1 = reshape(_vecx1, axes(x))
@@ -223,8 +223,8 @@ function finite_difference_jacobian(
223223
elseif fdtype == Val(:central)
224224

225225
function calculate_Ji_central(i)
226-
x1_save = ArrayInterfaceCore.allowed_getindex(vecx1,i)
227-
x_save = ArrayInterfaceCore.allowed_getindex(vecx,i)
226+
x1_save = ArrayInterface.allowed_getindex(vecx1,i)
227+
x_save = ArrayInterface.allowed_getindex(vecx,i)
228228
epsilon = compute_epsilon(Val(:forward), x1_save, relstep, absstep, dir)
229229
_vecx1 = setindex(vecx1,x1_save+epsilon,i)
230230
_vecx = setindex(vecx,x_save-epsilon,i)
@@ -263,7 +263,7 @@ function finite_difference_jacobian(
263263
epsilon = eps(eltype(x))
264264

265265
function calculate_Ji_complex(i)
266-
x_save = ArrayInterfaceCore.allowed_getindex(vecx,i)
266+
x_save = ArrayInterface.allowed_getindex(vecx,i)
267267
_vecx = setindex(complex.(vecx),x_save+im*epsilon,i)
268268
_x = reshape(_vecx, axes(x))
269269
vecfx = _vec(f(_x))
@@ -304,7 +304,7 @@ function finite_difference_jacobian!(J,
304304
relstep=default_relstep(fdtype, eltype(x)),
305305
absstep=relstep,
306306
colorvec = 1:length(x),
307-
sparsity = ArrayInterfaceCore.has_sparsestruct(J) ? J : nothing)
307+
sparsity = ArrayInterface.has_sparsestruct(J) ? J : nothing)
308308
if f_in isa Nothing && fdtype == Val(:forward)
309309
if size(J,1) == length(x)
310310
fx = zero(x)
@@ -360,7 +360,7 @@ function finite_difference_jacobian!(
360360
rows_index = nothing
361361
cols_index = nothing
362362
if _use_findstructralnz(sparsity)
363-
rows_index, cols_index = ArrayInterfaceCore.findstructralnz(sparsity)
363+
rows_index, cols_index = ArrayInterface.findstructralnz(sparsity)
364364
elseif sparsity isa DenseMatrix
365365
rows_index, cols_index = FiniteDiff._findstructralnz(sparsity)
366366
end
@@ -388,15 +388,15 @@ function finite_difference_jacobian!(
388388

389389
@inbounds for color_i 1:maximum(colorvec)
390390
if sparsity isa Nothing
391-
x1_save = ArrayInterfaceCore.allowed_getindex(x1,color_i)
391+
x1_save = ArrayInterface.allowed_getindex(x1,color_i)
392392
epsilon = compute_epsilon(Val(:forward), x1_save, relstep, absstep, dir)
393-
ArrayInterfaceCore.allowed_setindex!(x1, x1_save + epsilon, color_i)
393+
ArrayInterface.allowed_setindex!(x1, x1_save + epsilon, color_i)
394394
f(fx1, x1)
395395
# J is dense, so either it is truly dense or this is the
396396
# compressed form of the coloring, so write into it.
397397
@. J[:,color_i] = (vfx1 - vfx) / epsilon
398398
# Now return x1 back to its original value
399-
ArrayInterfaceCore.allowed_setindex!(x1, x1_save, color_i)
399+
ArrayInterface.allowed_setindex!(x1, x1_save, color_i)
400400
else # Perturb along the colorvec vector
401401
@. x2 = x1 * (_color == color_i)
402402
tmp = norm(x2)
@@ -405,7 +405,7 @@ function finite_difference_jacobian!(
405405
f(fx1, x1)
406406
# J is a sparse matrix, so decompress on the fly
407407
@. vfx1 = (vfx1 - vfx) / epsilon
408-
if ArrayInterfaceCore.fast_scalar_indexing(x1)
408+
if ArrayInterface.fast_scalar_indexing(x1)
409409
if sparseCSC_common_sparsity
410410
_colorediteration!(J,vfx1,colorvec,color_i,n)
411411
else
@@ -432,14 +432,14 @@ function finite_difference_jacobian!(
432432
vfx1 = _vec(fx1)
433433
@inbounds for color_i 1:maximum(colorvec)
434434
if sparsity isa Nothing
435-
x_save = ArrayInterfaceCore.allowed_getindex(x, color_i)
435+
x_save = ArrayInterface.allowed_getindex(x, color_i)
436436
epsilon = compute_epsilon(Val(:central), x_save, relstep, absstep, dir)
437-
ArrayInterfaceCore.allowed_setindex!(x1, x_save + epsilon, color_i)
437+
ArrayInterface.allowed_setindex!(x1, x_save + epsilon, color_i)
438438
f(fx1, x1)
439-
ArrayInterfaceCore.allowed_setindex!(x1, x_save - epsilon, color_i)
439+
ArrayInterface.allowed_setindex!(x1, x_save - epsilon, color_i)
440440
f(fx, x1)
441441
@. J[:,color_i] = (vfx1 - vfx) / 2epsilon
442-
ArrayInterfaceCore.allowed_setindex!(x1, x_save, color_i)
442+
ArrayInterface.allowed_setindex!(x1, x_save, color_i)
443443
else # Perturb along the colorvec vector
444444
@. x2 = x1 * (_color == color_i)
445445
tmp = norm(x2)
@@ -449,7 +449,7 @@ function finite_difference_jacobian!(
449449
f(fx1, x1)
450450
f(fx, x)
451451
@. vfx1 = (vfx1 - vfx) / 2epsilon
452-
if ArrayInterfaceCore.fast_scalar_indexing(x1)
452+
if ArrayInterface.fast_scalar_indexing(x1)
453453
if sparseCSC_common_sparsity
454454
_colorediteration!(J,vfx1,colorvec,color_i,n)
455455
else
@@ -470,16 +470,16 @@ function finite_difference_jacobian!(
470470
epsilon = eps(eltype(x))
471471
@inbounds for color_i 1:maximum(colorvec)
472472
if sparsity isa Nothing
473-
x1_save = ArrayInterfaceCore.allowed_getindex(x1, color_i)
474-
ArrayInterfaceCore.allowed_setindex!(x1, x1_save + im*epsilon, color_i)
473+
x1_save = ArrayInterface.allowed_getindex(x1, color_i)
474+
ArrayInterface.allowed_setindex!(x1, x1_save + im*epsilon, color_i)
475475
f(fx,x1)
476476
@. J[:,color_i] = imag(vfx) / epsilon
477-
ArrayInterfaceCore.allowed_setindex!(x1, x1_save,color_i)
477+
ArrayInterface.allowed_setindex!(x1, x1_save,color_i)
478478
else # Perturb along the colorvec vector
479479
@. x1 = x1 + im * epsilon * (_color == color_i)
480480
f(fx,x1)
481481
@. vfx = imag(vfx) / epsilon
482-
if ArrayInterfaceCore.fast_scalar_indexing(x1)
482+
if ArrayInterface.fast_scalar_indexing(x1)
483483
if sparseCSC_common_sparsity
484484
_colorediteration!(J,vfx,colorvec,color_i,n)
485485
else

0 commit comments

Comments
 (0)