Skip to content

Commit 58c1fbb

Browse files
authored
Merge pull request #53 from mcabbott/upci
Add tests for LoopVectorization
2 parents 90f32e3 + 6806340 commit 58c1fbb

File tree

8 files changed

+76
-49
lines changed

8 files changed

+76
-49
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
fail-fast: false
1717
matrix:
1818
version:
19-
- '1.3' # Replace this with the minimum Julia version that your package supports. E.g. if your package requires Julia 1.5 or higher, change this to '1.5'.
19+
- '1.4' # Replace this with the minimum Julia version that your package supports. E.g. if your package requires Julia 1.5 or higher, change this to '1.5'.
2020
- '1' # Leave this line unchanged. '1' will automatically expand to the latest stable 1.x release of Julia.
2121
- 'nightly'
2222
os:

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ language: julia
33
os:
44
- linux
55
julia:
6-
- 1.3
7-
- 1.5
6+
- 1.4
7+
- 1
88
- nightly
99
env:
1010
- JULIA_NUM_THREADS=1

Project.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
name = "Tullio"
22
uuid = "bc48ee85-29a4-5162-ae0b-a64e1601d4bc"
33
authors = ["Michael Abbott"]
4-
version = "0.2.10"
4+
version = "0.2.11"
55

66
[deps]
77
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b"
88
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
99
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
10-
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
1110

1211
[compat]
1312
DiffRules = "1"
1413
Requires = "1"
15-
SpecialFunctions = "0.10" # just for this bound
1614
julia = "1.3"
1715

1816
[extras]
@@ -21,6 +19,7 @@ FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
2119
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
2220
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
2321
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
22+
LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890"
2423
NamedDims = "356022a1-0364-5f58-8944-0da4b18d706f"
2524
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
2625
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
@@ -31,4 +30,4 @@ Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c"
3130
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"
3231

3332
[targets]
34-
test = ["Test", "CUDA", "FillArrays", "ForwardDiff", "KernelAbstractions", "LinearAlgebra", "NamedDims", "OffsetArrays", "Printf", "Random", "TensorOperations", "Tracker", "Zygote"]
33+
test = ["Test", "CUDA", "FillArrays", "ForwardDiff", "KernelAbstractions", "LinearAlgebra", "LoopVectorization", "NamedDims", "OffsetArrays", "Printf", "Random", "TensorOperations", "Tracker", "Zygote"]

src/eval.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ using Requires
5353

5454
@init @require LoopVectorization = "bdcacae8-1622-11e9-2a5c-532679323890" begin
5555
using .LoopVectorization
56-
SVec = if isdefined(LoopVectorization, :SVec) # version 0.8, for Julia ⩽1.5
56+
if isdefined(LoopVectorization, :SVec) # version 0.8, for Julia ⩽1.5
5757
using .LoopVectorization.VectorizationBase: SVec, Mask, prevpow2
58-
Svec
5958
else # version 0.9, supports Julia 1.6
6059
using .LoopVectorization.VectorizationBase: Vec, Mask, prevpow2
61-
Vec
60+
SVec{N,T} = Vec{N,T}
6261
end
6362

6463
# Functions needed for safe vectorised max gradient
@@ -71,7 +70,8 @@ using Requires
7170
@inline allzero(seen::Int) = iszero(seen)
7271
@inline allzero(seen::SVec{N,Int}) where {N} = iszero((!iszero(seen)).u)
7372

74-
@inline Tullio.anyone(cond::Mask) = cond != zero(cond)
73+
# @inline Tullio.anyone(cond::Mask) = cond != zero(cond)
74+
@inline Tullio.anyone(cond::Mask) = cond.u != zero(cond).u # for v0.9
7575

7676
@require ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" begin
7777
# Dual numbers + svec, should live in PaddedMatricesForwardDiff?

src/macro.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,9 @@ function make_many_actors(act!, args, ex1, outer::Vector, ex3, inner::Vector, ex
10701070
safe = if act! == ACT!
10711071
isempty(store.unsafeleft)
10721072
else # working on ∇act!
1073-
isempty(store.unsaferight)
1073+
isempty(store.unsaferight) &&
1074+
store.redfun == :+ && # Disable @avx for min/max grad, #53
1075+
store.grad != :Dual # and for use with ForwardDiff
10741076
end
10751077

10761078
if safe && store.avx != false && isdefined(store.mod, :LoopVectorization)

test/gradients.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,24 +259,24 @@ if Tullio._GRAD[] != :Dual
259259
dv = ForwardDiff.gradient(v -> sum(f6(m2,v)), v2)
260260
@test dv _gradient(sumf6, m2, v2)[2]
261261

262-
f7(x,y) = @tullio (max) z[i] := x[i,j]^2 / sqrt(y[i]) + exp(y[j])
262+
f7(x,y) = @tullio (max) z[i] := x[i,j]^2 / sqrt(y[i]) + exp(y[j]) avx=false
263263

264264
dm = ForwardDiff.gradient(m -> sum(f7(m,v2)), m2)
265-
@test dm _gradient(sumf7, m2, v2)[1]
265+
@test dm _gradient(sumf7, m2, v2)[1] # gives wrong answers with avx, 1.4 in tests
266266
dv = ForwardDiff.gradient(v -> sum(f7(m2,v)), v2)
267267
@test dv _gradient(sumf7, m2, v2)[2]
268268

269-
f8(x,y) = @tullio (max) z[i,l] := log(x[i,j,k,l]) / y[j]^1/3
270-
f9(x,y) = @tullio (min) z[i,j] := log(x[i,j,k,l]) / y[j]^1/3
269+
f8(x,y) = @tullio (max) z[i,l] := log(x[i,j,k,l]) / y[j]^1/3 avx=false
270+
f9(x,y) = @tullio (min) z[i,j] := log(x[i,j,k,l]) / y[j]^1/3 avx=false
271271

272272
dm = ForwardDiff.gradient(m -> sum(f8(m,v2)), m4)
273-
@test dm _gradient(sumf8, m4, v2)[1]
273+
@test dm _gradient(sumf8, m4, v2)[1] # gives wrong answers with avx, 1.5 in tests
274274
dv = ForwardDiff.gradient(v -> sum(f8(m4,v)), v2)
275275
@test dv _gradient(sumf8, m4, v2)[2]
276276
dm = ForwardDiff.gradient(m -> sum(f9(m,v2)), m4)
277-
@test dm _gradient(sumf9, m4, v2)[1]
277+
@test dm _gradient(sumf9, m4, v2)[1] # gives wrong answers with avx, repl
278278
dv = ForwardDiff.gradient(v -> sum(f9(m4,v)), v2)
279-
@test dv _gradient(sumf9, m4, v2)[2]
279+
@test dv _gradient(sumf9, m4, v2)[2] # gives wrong answers with avx
280280

281281
end
282282

test/parsing.jl

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using Tullio, Test, LinearAlgebra
1111
@test A == [i^2 for i in 1:10]
1212

1313
# diagonals
14-
@tullio D[i,i] := trunc(Int, sqrt(A[i]))
14+
@tullio D[i,i] := trunc(Int, sqrt(A[i])) avx=false # MethodError: no method matching trunc(::Type{Int64}, ::VectorizationBase.Vec{4,Float64})
1515
@test D == Diagonal(sqrt.(A))
1616

1717
# arrays of arrays
@@ -124,7 +124,7 @@ using Tullio, Test, LinearAlgebra
124124
@test H[1,:] == M[2,:] # but H[3,:] gets written into twice.
125125

126126
J′ = [1,2,10]
127-
@tullio H′[J′[i'],k] := A[k]
127+
@tullio H′[J′[i'],k] := A[k] avx=false # StackOverflowError
128128
@test size(H′) == (10, length(A))
129129
@test H′[2,:] == A
130130
@test H′[3,4] == 0 # zeroed before being written into
@@ -219,7 +219,7 @@ end
219219
# scatter operation
220220
D = similar(A, 10, 10) .= 999
221221
inds = [2,3,5,2]
222-
@tullio D[inds[i],j] = A[j]
222+
@tullio D[inds[i],j] = A[j] avx=false # StackOverflowError
223223
@test D[2,:] == A
224224
@test D[4,4] != 0 # not zeroed before writing.
225225

@@ -444,7 +444,7 @@ end
444444
# basics
445445
@test [prod(A)] == @tullio (*) P[_] := float(A[i])
446446
@test maximum(A) == @tullio (max) m := float(A[i])
447-
@test minimum(A) == @tullio (min) m := float(A[i]) # fails with @avx
447+
@test minimum(A) == @tullio (min) m := float(A[i])
448448

449449
@test true == @tullio (&) p := A[i] > 0
450450
@test true === @tullio (&) p := A[i] > 0
@@ -505,11 +505,11 @@ end
505505

506506
# promotion of init & += cases:
507507
B = rand(10)
508-
@test sum(B.^2)+2 @tullio s2 := B[i]^2 init=2 threads=false
508+
@test sum(B.^2)+2 @tullio s2 := B[i]^2 init=2 threads=false avx=false # InexactError: Int64 on LV 0.8
509509
s3 = 3
510510
@test sum(B.^2)+3 @tullio s3 += B[i]^2
511511
s4 = 4im
512-
@test sum(B.^2)+4im @tullio s4 += B[i]^2
512+
@test sum(B.^2)+4im @tullio s4 += B[i]^2 avx=false # TypeError: in AbstractSIMD, in T, expected T<:(Union{Bool, Float32
513513

514514
# no reduction means no redfun, and no init:
515515
@test_throws LoadError @eval @tullio (max) A2[i] := A[i]^2
@@ -566,18 +566,18 @@ end
566566
# reading
567567
N = NamedDimsArray(rand(Int8,3,10), (:r, :c))
568568

569-
@tullio A[i,j] := N[i, j] + 100 * (1:10)[j]
569+
@tullio A[i,j] := N[i, j] + 100 * (1:10)[j] avx=false # conversion to pointer not defined for NamedDimsArray
570570
@test A == N .+ 100 .* (1:10)'
571571

572-
@tullio B[i] := N[r=i, c=1]
572+
@tullio B[i] := N[r=i, c=1] avx=false
573573
@test B == N[:,1]
574574

575-
@tullio C[j,i] := N[c=j, r=i] + 100 * (1:10)[j]
575+
@tullio C[j,i] := N[c=j, r=i] + 100 * (1:10)[j] avx=false
576576
@test A == C'
577577
@test dimnames(C) == (:_, :_) # similar(parent(A)) avoids a bug
578578

579579
# writing
580-
@tullio M[row=i, col=j, i=1] := (1:3)[i] // (1:7)[j]
580+
@tullio M[row=i, col=j, i=1] := (1:3)[i] // (1:7)[j] avx=false
581581
@test dimnames(M) == (:row, :col, :i)
582582

583583
end
@@ -593,7 +593,7 @@ end
593593
@test_throws LoadError @macroexpand1 @tullio A[i] := (1:10)[i]^2 threads=:maybe
594594

595595
# keyword verbose accepts values [true, false, 2, 3]
596-
@tullio A[i] := (1:10)[i]^2 verbose=1
596+
@tullio A[i] := (1:10)[i]^2 verbose=1 avx=false # @error: rejected by LoopVectorization's check_args
597597
@tullio A[i] := (1:10)[i]^2 verbose=false
598598
@test_throws LoadError @macroexpand1 @tullio A[i] := (1:10)[i]^2 verbose=4
599599

@@ -632,7 +632,7 @@ end
632632
# https://github.com/mcabbott/Tullio.jl/issues/36
633633
# final type real, intermediate complex... not fixed yet!
634634
xs = randn(1000)
635-
@test_throws InexactError @tullio z[i] := exp(im * xs[i] - xs[j]) |> abs2
635+
@test_throws InexactError @tullio z[i] := exp(im * xs[i] - xs[j]) |> abs2 avx=false # TypeError with LV
636636

637637
# https://github.com/mcabbott/Tullio.jl/issues/43
638638
P = rand(2,2,3); Diff = rand(3,3); n=4
@@ -664,7 +664,7 @@ end
664664
@tullio x[i] := s[i]
665665
end
666666
@test_broken [3,7] == let s = [1 2;3 4], zero=one # left because of #50
667-
@tullio x[i] := s[i,j]
667+
@tullio x[i] := s[i,j] avx=false # Unexpected Pass with LV
668668
end
669669

670670
end

test/runtests.jl

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ end
9696

9797
#===== Zygote =====#
9898

99+
if VERSION < v"1.6-" # Zygote isn't working on 1.6
100+
99101
t5 = time()
100102
using Zygote
101103

@@ -109,7 +111,6 @@ _gradient(x...) = Zygote.gradient(x...)
109111
@testset "gradients: Zygote + ForwardDiff" begin include("gradients.jl") end
110112

111113
@tullio grad=Base
112-
if VERSION >= v"1.4" # mysterious failures on 1.3
113114
@testset "complex gradients with Zygote" begin
114115

115116
x0 = [1,2,3] .+ [5im, 0, -11im]
@@ -161,10 +162,11 @@ if VERSION >= v"1.4" # mysterious failures on 1.3
161162

162163
end
163164
end
164-
end # VERSION
165165

166166
@info @sprintf("Zygote tests took %.1f seconds", time()-t5)
167167

168+
end # VERSION
169+
168170
#===== ReverseDiff =====#
169171
#=
170172
t6 = time()
@@ -200,17 +202,43 @@ _gradient(x...) = Yota.grad(x...)[2]
200202
=#
201203

202204
#===== LoopVectorization =====#
203-
#=
205+
204206
t8 = time()
205207
using LoopVectorization
206208

207-
using LoopVectorization.VectorizationBase: SVec, Mask, prevpow2
208-
sv = SVec{4,Int}(1,2,3,4) # SVec{4,Int64}<1, 2, 3, 4>
209-
ms = Mask(0x03) # Mask{8,Bool}<1, 1, 0, 0, 0, 0, 0, 0>
210-
@test Tullio.onlyone(ms, 0) == Mask(0x02)
211-
@test Tullio.onlyone(ms, sv) == Mask(0x00)
212-
@test Tullio.onlyone(ms, zero(sv)) == Mask(0x02)
209+
if isdefined(LoopVectorization, :SVec) # version 0.8, for Julia ⩽1.5
210+
using LoopVectorization.VectorizationBase: SVec, Mask
211+
else # version 0.9, supports Julia 1.6
212+
using LoopVectorization.VectorizationBase: Vec, Mask
213+
SVec{N,T} = Vec{N,T}
214+
end
215+
216+
@testset "LoopVectorization onlyone" begin
217+
ms = Mask{UInt8}(0x03); # Mask{8,Bool}<1, 1, 0, 0, 0, 0, 0, 0>
218+
sv = SVec{4,Int}(1,2,3,4) # SVec{4,Int64}<1, 2, 3, 4>
219+
220+
# preliminaries:
221+
@test Tullio.allzero(sv) === false
222+
@test Tullio.allzero(zero(sv)) === true
223+
224+
@test Tullio.anyone(ms) === true
225+
226+
# the main function:
227+
@test Tullio.onlyone(false, 0) === false
228+
@test Tullio.onlyone(true, 0) === true
229+
@test Tullio.onlyone(true, 1) === false
213230

231+
# @test Tullio.onlyone(ms, 0) === Mask{UInt8}(0x02)
232+
@test Tullio.onlyone(ms, 0).u == 0x02
233+
# @test Tullio.onlyone(ms, sv) === Mask{UInt8}(0x00)
234+
@test Tullio.onlyone(ms, sv).u == 0x00
235+
# @test Tullio.onlyone(ms, zero(sv)) === Mask{UInt8}(0x02)
236+
@test Tullio.onlyone(ms, zero(sv)).u == 0x02
237+
end
238+
239+
@testset "parsing + LoopVectorization" begin include("parsing.jl") end
240+
241+
using Tracker
214242
GRAD = :Tracker
215243
_gradient(x...) = Tracker.gradient(x...)
216244

@@ -220,16 +248,10 @@ _gradient(x...) = Tracker.gradient(x...)
220248
@tullio grad=Dual
221249
@testset "gradients: Tracker + ForwardDiff + LoopVectorization" begin include("gradients.jl") end
222250

223-
GRAD = :Zygote
224-
_gradient(x...) = Zygote.gradient(x...)
251+
@info @sprintf("LoopVectorization tests took %.1f seconds", time()-t8)
225252

226-
@tullio grad=Base
227-
@testset "gradients: Zygote + LoopVectorization" begin include("gradients.jl") end
253+
@tullio avx=false
228254

229-
@testset "parsing + LoopVectorization" begin include("parsing.jl") end
230-
231-
@info @sprintf("LoopVectorization tests took %.1f seconds", time()-t8)
232-
=#
233255
#===== TensorOperations =====#
234256

235257
t9 = time()
@@ -242,6 +264,8 @@ _gradient(x...) = Tracker.gradient(x...)
242264
@tullio grad=Base
243265
@testset "gradients: Tracker + TensorOperations" begin include("gradients.jl") end
244266

267+
if VERSION < v"1.6-" # Zygote isn't working on 1.6
268+
245269
using Zygote
246270
GRAD = :Zygote
247271
_gradient(x...) = Zygote.gradient(x...)
@@ -271,6 +295,8 @@ _gradient(x...) = Zygote.gradient(x...)
271295
end
272296
end
273297

298+
end # VERSION
299+
274300
@testset "parsing + TensorOperations" begin include("parsing.jl") end # testing correct fallback
275301

276302
@info @sprintf("TensorOperations tests took %.1f seconds", time()-t9)

0 commit comments

Comments
 (0)