|
| 1 | +@testset "accumulate_1d" begin |
| 2 | + |
| 3 | + Random.seed!(0) |
| 4 | + |
| 5 | + # Single block exlusive scan (each block processes two elements) |
| 6 | + for num_elems in 1:256 |
| 7 | + x = array_from_host(ones(Int32, num_elems)) |
| 8 | + y = copy(x) |
| 9 | + AK.accumulate!(+, y; init=0, inclusive=false, block_size=128) |
| 10 | + yh = Array(y) |
| 11 | + @test all(yh .== 0:length(yh) - 1) |
| 12 | + end |
| 13 | + |
| 14 | + # Single block inclusive scan |
| 15 | + for num_elems in 1:256 |
| 16 | + x = array_from_host(rand(1:1000, num_elems), Int32) |
| 17 | + y = copy(x) |
| 18 | + AK.accumulate!(+, y; init=0, block_size=128) |
| 19 | + @test all(Array(y) .== accumulate(+, Array(x))) |
| 20 | + end |
| 21 | + |
| 22 | + # Large exclusive scan |
| 23 | + for _ in 1:1000 |
| 24 | + num_elems = rand(1:100_000) |
| 25 | + x = array_from_host(ones(Int32, num_elems)) |
| 26 | + y = copy(x) |
| 27 | + AK.accumulate!(+, y; init=0, inclusive=false) |
| 28 | + yh = Array(y) |
| 29 | + @test all(yh .== 0:length(yh) - 1) |
| 30 | + end |
| 31 | + |
| 32 | + # Large inclusive scan |
| 33 | + for _ in 1:1000 |
| 34 | + num_elems = rand(1:100_000) |
| 35 | + x = array_from_host(rand(1:1000, num_elems), Int32) |
| 36 | + y = copy(x) |
| 37 | + AK.accumulate!(+, y; init=0) |
| 38 | + @test all(Array(y) .== accumulate(+, Array(x))) |
| 39 | + end |
| 40 | + |
| 41 | + # Stress-testing small block sizes -> many blocks |
| 42 | + for _ in 1:100 |
| 43 | + num_elems = rand(1:100_000) |
| 44 | + x = array_from_host(rand(1:1000, num_elems), Int32) |
| 45 | + y = copy(x) |
| 46 | + AK.accumulate!(+, y; init=0, block_size=16) |
| 47 | + @test all(Array(y) .== accumulate(+, Array(x))) |
| 48 | + end |
| 49 | + |
| 50 | + # Allowing N-dimensional arrays, still reduced as 1D |
| 51 | + for _ in 1:100 |
| 52 | + n1 = rand(1:100) |
| 53 | + n2 = rand(1:100) |
| 54 | + n3 = rand(1:100) |
| 55 | + vh = rand(Float32, n1, n2, n3) |
| 56 | + v = array_from_host(vh) |
| 57 | + AK.accumulate!(+, v; init=0) |
| 58 | + @test all(Array(v) .≈ accumulate(+, vh)) |
| 59 | + end |
| 60 | + |
| 61 | + # Ensuring the init value is respected |
| 62 | + for _ in 1:100 |
| 63 | + num_elems = rand(1:100_000) |
| 64 | + x = array_from_host(rand(1:1000, num_elems), Int32) |
| 65 | + y = similar(x) |
| 66 | + init = rand(-1000:1000) |
| 67 | + AK.accumulate!(+, y, x; init=Int32(init)) |
| 68 | + @test all(Array(y) .== accumulate(+, Array(x), init=init)) |
| 69 | + end |
| 70 | + |
| 71 | + # Exclusive scan |
| 72 | + x = array_from_host(ones(Int32, 10)) |
| 73 | + y = copy(x) |
| 74 | + AK.accumulate!(+, y; init=0, inclusive=false) |
| 75 | + @test all(Array(y) .== 0:9) |
| 76 | + |
| 77 | + # Test init value is respected with exclusive scan too |
| 78 | + x = array_from_host(ones(Int32, 10)) |
| 79 | + y = copy(x) |
| 80 | + init = 10 |
| 81 | + AK.accumulate!(+, y; init=Int32(init), inclusive=false) |
| 82 | + @test all(Array(y) .== 10:19) |
| 83 | + |
| 84 | + # Testing different settings |
| 85 | + AK.accumulate!(+, array_from_host(ones(Int32, 1000)), init=0, inclusive=false, |
| 86 | + block_size=128, |
| 87 | + temp=array_from_host(zeros(Int32, 1000)), |
| 88 | + temp_flags=array_from_host(zeros(Int8, 1000))) |
| 89 | + AK.accumulate(+, array_from_host(ones(Int32, 1000)), init=0, inclusive=false, |
| 90 | + block_size=128, |
| 91 | + temp=array_from_host(zeros(Int64, 1000)), |
| 92 | + temp_flags=array_from_host(zeros(Int8, 1000))) |
| 93 | +end |
| 94 | + |
| 95 | + |
| 96 | +@testset "accumulate_nd" begin |
| 97 | + Random.seed!(0) |
| 98 | + |
| 99 | + # Test all possible corner cases against Base.accumulate |
| 100 | + for dims in 1:4 |
| 101 | + for isize in 0:3 |
| 102 | + for jsize in 0:3 |
| 103 | + for ksize in 0:3 |
| 104 | + sh = rand(Int32(1):Int32(100), isize, jsize, ksize) |
| 105 | + s = array_from_host(sh) |
| 106 | + d = AK.accumulate(+, s; init=Int32(0), dims=dims) |
| 107 | + |
| 108 | + dh = Array(d) |
| 109 | + dhres = accumulate(+, sh, init=Int32(0), dims=dims) |
| 110 | + @test dh == dhres |
| 111 | + @test eltype(dh) == eltype(dhres) |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + # Fuzzy correctness testing |
| 118 | + for _ in 1:100 |
| 119 | + for dims in 1:3 |
| 120 | + n1 = rand(1:100) |
| 121 | + n2 = rand(1:100) |
| 122 | + n3 = rand(1:100) |
| 123 | + vh = rand(Int32(1):Int32(100), n1, n2, n3) |
| 124 | + v = array_from_host(vh) |
| 125 | + |
| 126 | + s = AK.accumulate(+, v; init=Int32(0), dims=dims) |
| 127 | + sh = Array(s) |
| 128 | + @test sh == accumulate(+, vh, init=Int32(0), dims=dims) |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + for _ in 1:100 |
| 133 | + for dims in 1:3 |
| 134 | + n1 = rand(1:100) |
| 135 | + n2 = rand(1:100) |
| 136 | + n3 = rand(1:100) |
| 137 | + vh = rand(UInt32(1):UInt32(100), n1, n2, n3) |
| 138 | + v = array_from_host(vh) |
| 139 | + |
| 140 | + s = AK.accumulate(+, v; init=UInt32(0), dims=dims) |
| 141 | + sh = Array(s) |
| 142 | + @test sh == accumulate(+, vh, init=UInt32(0), dims=dims) |
| 143 | + end |
| 144 | + end |
| 145 | + |
| 146 | + for _ in 1:100 |
| 147 | + for dims in 1:3 |
| 148 | + n1 = rand(1:100) |
| 149 | + n2 = rand(1:100) |
| 150 | + n3 = rand(1:100) |
| 151 | + vh = rand(Float32, n1, n2, n3) |
| 152 | + v = array_from_host(vh) |
| 153 | + |
| 154 | + s = AK.accumulate(+, v; init=Float32(0), dims=dims) |
| 155 | + sh = Array(s) |
| 156 | + @test all(sh .≈ accumulate(+, vh, init=Float32(0), dims=dims)) |
| 157 | + end |
| 158 | + end |
| 159 | + |
| 160 | + # Ensure the init value is respected |
| 161 | + for _ in 1:100 |
| 162 | + for dims in 1:3 |
| 163 | + n1 = rand(1:100) |
| 164 | + n2 = rand(1:100) |
| 165 | + n3 = rand(1:100) |
| 166 | + vh = rand(Float32, n1, n2, n3) |
| 167 | + v = array_from_host(vh) |
| 168 | + init = rand(-1000:1000) |
| 169 | + s = AK.accumulate(+, v; init=Float32(init), dims=dims) |
| 170 | + sh = Array(s) |
| 171 | + @test all(sh .≈ accumulate(+, vh, init=Float32(init), dims=dims)) |
| 172 | + end |
| 173 | + end |
| 174 | + |
| 175 | + # Exclusive scan |
| 176 | + vh = ones(Int32, 10, 10) |
| 177 | + v = array_from_host(vh) |
| 178 | + s = AK.accumulate(+, v; init=0, dims=2, inclusive=false) |
| 179 | + sh = Array(s) |
| 180 | + @test all([sh[i, :] == 0:9 for i in 1:10]) |
| 181 | + |
| 182 | + # Test init value is respected with exclusive scan too |
| 183 | + vh = ones(Int32, 10, 10) |
| 184 | + v = array_from_host(vh) |
| 185 | + s = AK.accumulate(+, v; init=10, dims=2, inclusive=false) |
| 186 | + sh = Array(s) |
| 187 | + @test all([sh[i, :] == 10:19 for i in 1:10]) |
| 188 | + |
| 189 | + # Testing different settings |
| 190 | + AK.accumulate( |
| 191 | + (x, y) -> x + 1, |
| 192 | + array_from_host(rand(Int32, 3, 4, 5)), |
| 193 | + init=Int32(0), |
| 194 | + neutral=Int32(0), |
| 195 | + dims=2, |
| 196 | + block_size=64, |
| 197 | + temp=array_from_host(zeros(Int32, 3, 1, 5)), |
| 198 | + ) |
| 199 | + AK.accumulate( |
| 200 | + (x, y) -> x + 1, |
| 201 | + array_from_host(rand(Int32, 3, 4, 5)), |
| 202 | + init=Int32(0), |
| 203 | + neutral=Int32(0), |
| 204 | + dims=3, |
| 205 | + block_size=64, |
| 206 | + temp=array_from_host(zeros(Int32, 3, 4, 1)), |
| 207 | + ) |
| 208 | +end |
| 209 | +@testset "cumsum" begin |
| 210 | + |
| 211 | + Random.seed!(0) |
| 212 | + |
| 213 | + # Simple correctness tests |
| 214 | + v = array_from_host(1:100) |
| 215 | + vh = Array(v) |
| 216 | + @test Array(AK.cumsum(v)) == cumsum(vh) |
| 217 | + |
| 218 | + # Fuzzy testing |
| 219 | + for _ in 1:100 |
| 220 | + num_elems = rand(1:100_000) |
| 221 | + vh = rand(Float32, num_elems) |
| 222 | + v = array_from_host(vh) |
| 223 | + @test all(Array(AK.cumsum(v)) .≈ cumsum(vh)) |
| 224 | + end |
| 225 | + |
| 226 | + for _ in 1:100 |
| 227 | + for dims in 1:3 |
| 228 | + n1 = rand(1:10) |
| 229 | + n2 = rand(1:10) |
| 230 | + n3 = rand(1:10) |
| 231 | + vh = rand(Int32(-5):Int32(5), n1, n2, n3) |
| 232 | + v = array_from_host(vh) |
| 233 | + |
| 234 | + # Indexing into array as if linear; not supported in Base |
| 235 | + # @test all(Array(AK.cumsum(v)) .== cumsum(vh)) |
| 236 | + |
| 237 | + # Along dimensions |
| 238 | + r = Array(AK.cumsum(v, dims=dims)) |
| 239 | + rh = cumsum(vh, dims=dims) |
| 240 | + |
| 241 | + @test r == rh |
| 242 | + end |
| 243 | + end |
| 244 | + |
| 245 | + # Test promotion to op-dictated type |
| 246 | + xh = rand(Bool, 16) |
| 247 | + x = array_from_host(xh) |
| 248 | + @test Array(AK.cumsum(x)) == cumsum(xh) |
| 249 | + |
| 250 | + # Testing different settings |
| 251 | + v = array_from_host(rand(-5:5, 100_000)) |
| 252 | + AK.cumsum(v, block_size=64) |
| 253 | + |
| 254 | + # The other settings are stress-tested in reduce |
| 255 | +end |
| 256 | + |
| 257 | + |
| 258 | +@testset "cumprod" begin |
| 259 | + |
| 260 | + Random.seed!(0) |
| 261 | + |
| 262 | + # Simple correctness tests |
| 263 | + v = array_from_host(1:100) |
| 264 | + vh = Array(v) |
| 265 | + @test Array(AK.cumprod(v)) == cumprod(vh) |
| 266 | + |
| 267 | + vh = ones(Float32, 100_000) |
| 268 | + v = array_from_host(vh) |
| 269 | + @test Array(AK.cumprod(v)) == vh |
| 270 | + |
| 271 | + # Fuzzy testing |
| 272 | + for _ in 1:100 |
| 273 | + for dims in 1:3 |
| 274 | + n1 = rand(1:10) |
| 275 | + n2 = rand(1:10) |
| 276 | + n3 = rand(1:10) |
| 277 | + vh = rand(Int32(-5):Int32(5), n1, n2, n3) |
| 278 | + v = array_from_host(vh) |
| 279 | + |
| 280 | + # Indexing into array as if linear; not supported in Base |
| 281 | + # @test all(Array(AK.cumprod(v)) .== cumprod(vh)) |
| 282 | + |
| 283 | + # Along dimensions |
| 284 | + r = Array(AK.cumprod(v, dims=dims)) |
| 285 | + rh = cumprod(vh, dims=dims) |
| 286 | + |
| 287 | + @test r == rh |
| 288 | + end |
| 289 | + end |
| 290 | + |
| 291 | + # Testing different settings |
| 292 | + v = array_from_host(rand(-5:5, 100_000)) |
| 293 | + AK.cumprod(v, block_size=64) |
| 294 | + |
| 295 | + # The other settings are stress-tested in reduce |
| 296 | +end |
0 commit comments