Skip to content

Commit ee86fbb

Browse files
author
Avik Pal
committed
Fix numerical errors in the tests
1 parent e4232d7 commit ee86fbb

File tree

3 files changed

+37
-36
lines changed

3 files changed

+37
-36
lines changed

src/nnpack/NNPACK.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ Allows NNPACK to intelligently choose which threadpool to use for getting the be
3535
performance.
3636
"""
3737
function allocate_threadpool()
38-
for i in 1:Int(floor(log2(NNPACK_CPU_THREADS)))
38+
global NNPACK_CPU_THREADS = NNPACK_CPU_THREADS > 8 ? UInt64(8) : floor(log2(NNPACK_CPU_THREADS))
39+
for i in 1:Int(NNPACK_CPU_THREADS)
3940
threads = UInt64(2^i)
4041
push!(shared_threadpool_dict, threads => Ref(pthreadpool_create(threads)))
4142
end

src/nnpack/performance.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
function select_threadpool(cdims::DenseConvDims, batch_size::Int)
22
inp_size = input_size(cdims)[1]
33
if batch_size >= 32
4-
return shared_threadpool_dict[4][]
4+
return shared_threadpool_dict[Int(NNPACK_CPU_THREADS)][]
55
elseif batch_size >= 16 && inp_size >= 64
6-
return shared_threadpool_dict[4][]
6+
return shared_threadpool_dict[Int(NNPACK_CPU_THREADS)][]
77
elseif inp_size <= 32
88
return C_NULL
99
elseif inp_size >= 128
10-
return shared_threadpool_dict[4][]
10+
return shared_threadpool_dict[Int(NNPACK_CPU_THREADS)][]
1111
elseif inp_size * batch_size >= 256
12-
return shared_threadpool_dict[4][]
12+
return shared_threadpool_dict[Int(NNPACK_CPU_THREADS)][]
1313
end
1414
return C_NULL
1515
end
1616

1717
function select_threadpool(pdims::PoolDims, batch_size::Int)
1818
inp_size = input_size(pdims)[1]
1919
if batch_size >= 32
20-
return shared_threadpool_dict[4][]
20+
return shared_threadpool_dict[Int(NNPACK_CPU_THREADS)][]
2121
elseif batch_size >= 16 && inp_size >= 64
22-
return shared_threadpool_dict[4][]
22+
return shared_threadpool_dict[Int(NNPACK_CPU_THREADS)][]
2323
elseif inp_size <= 32
2424
return C_NULL
2525
elseif inp_size >= 128
26-
return shared_threadpool_dict[4][]
26+
return shared_threadpool_dict[Int(NNPACK_CPU_THREADS)][]
2727
elseif inp_size * batch_size >= 256
28-
return shared_threadpool_dict[4][]
28+
return shared_threadpool_dict[Int(NNPACK_CPU_THREADS)][]
2929
end
3030
return C_NULL
3131
end

test/conv.jl

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -278,27 +278,27 @@ conv_answer_dict = Dict(
278278
@testset "$(conv)" begin
279279
# First, your basic convolution with no parameters
280280
cdims = DenseConvDims(x, w)
281-
@test ddims(conv(x, w, cdims)) == y_plain
281+
@test isapprox(ddims(conv(x, w, cdims)), y_plain, rtol = 1.0e-7)
282282

283283
# Next, test convolution on views and alternate datatypes:
284-
@test ddims(conv(view(x, repeat([:], ndims(x))...), w, cdims)) == y_plain
285-
@test ddims(conv(Float32.(x), Float32.(w), cdims)) == Float32.(y_plain)
284+
@test isapprox(ddims(conv(view(x, repeat([:], ndims(x))...), w, cdims)), y_plain, rtol = 1.0e-7)
285+
@test isapprox(ddims(conv(Float32.(x), Float32.(w), cdims)), Float32.(y_plain), rtol = 1.0e-7)
286286

287287
# Next, introduce stride:
288288
cdims = DenseConvDims(x, w; stride=2)
289-
@test ddims(conv(x, w, cdims)) == y_stride
289+
@test isapprox(ddims(conv(x, w, cdims)), y_stride, rtol = 1.0e-7)
290290

291291
# Next, introduce dilation:
292292
cdims = DenseConvDims(x, w; dilation=2)
293-
@test ddims(conv(x, w, cdims)) == y_dil
293+
@test isapprox(ddims(conv(x, w, cdims)), y_dil, rtol = 1.0e-7)
294294

295295
# Next, introduce padding:
296296
cdims = DenseConvDims(x, w; padding=1)
297-
@test ddims(conv(x, w, cdims)) == y_pad
297+
@test isapprox(ddims(conv(x, w, cdims)), y_pad, rtol = 1.0e-7)
298298

299299
# Next, test crosscor/conv with a flipped kernel
300300
cdims = DenseConvDims(x, w; flipkernel=true)
301-
@test ddims(conv(x, w, cdims)) == y_flip
301+
@test isapprox(ddims(conv(x, w, cdims)), y_flip, rtol = 1.0e-7)
302302
end
303303
end
304304

@@ -312,39 +312,39 @@ conv_answer_dict = Dict(
312312
# First, your basic convolution with no parameters
313313
cdims = DenseConvDims(x, w)
314314
dy = NNlib.conv(x, w, cdims)
315-
@test ddims(∇conv_filter(x, dy, cdims)) == dw
316-
@test ddims(∇conv_data(dy, w, cdims)) == dx
315+
@test isapprox(ddims(∇conv_filter(x, dy, cdims)), dw, rtol = 1.0e-7)
316+
@test isapprox(ddims(∇conv_data(dy, w, cdims)), dx, rtol = 1.0e-7)
317317

318318
# Next, test convolution on views and alternate datatypes:
319-
@test ddims(∇conv_filter(x, view(dy, repeat([:], ndims(dy))...), cdims)) == dw
320-
@test ddims(∇conv_data(view(dy, repeat([:], ndims(dy))...), w, cdims)) == dx
319+
@test isapprox(ddims(∇conv_filter(x, view(dy, repeat([:], ndims(dy))...), cdims)), dw, rtol = 1.0e-7)
320+
@test isapprox(ddims(∇conv_data(view(dy, repeat([:], ndims(dy))...), w, cdims)), dx, rtol = 1.0e-7)
321321

322-
@test ddims(∇conv_filter(Float32.(x), Float32.(dy), cdims)) == dw
323-
@test ddims(∇conv_data(Float32.(dy), Float32.(w), cdims)) == dx
322+
@test isapprox(ddims(∇conv_filter(Float32.(x), Float32.(dy), cdims)), dw, rtol = 1.0e-7)
323+
@test isapprox(ddims(∇conv_data(Float32.(dy), Float32.(w), cdims)), dx, rtol = 1.0e-7)
324324

325325
# Next, introduce stride:
326326
cdims = DenseConvDims(x, w; stride=2)
327327
dy = NNlib.conv(x, w, cdims)
328-
@test ddims(∇conv_filter(x, dy, cdims)) == dw_stride
329-
@test ddims(∇conv_data(dy, w, cdims)) == dx_stride
328+
@test isapprox(ddims(∇conv_filter(x, dy, cdims)), dw_stride, rtol = 1.0e-7)
329+
@test isapprox(ddims(∇conv_data(dy, w, cdims)), dx_stride, rtol = 1.0e-7)
330330

331331
# Next, introduce dilation:
332332
cdims = DenseConvDims(x, w; dilation=2)
333333
dy = NNlib.conv(x, w, cdims)
334-
@test ddims(∇conv_filter(x, dy, cdims)) == dw_dil
335-
@test ddims(∇conv_data(dy, w, cdims)) == dx_dil
334+
@test isapprox(ddims(∇conv_filter(x, dy, cdims)), dw_dil, rtol = 1.0e-7)
335+
@test isapprox(ddims(∇conv_data(dy, w, cdims)), dx_dil, rtol = 1.0e-7)
336336

337337
# Next, introduce padding:
338338
cdims = DenseConvDims(x, w; padding=1)
339339
dy = NNlib.conv(x, w, cdims)
340-
@test ddims(∇conv_filter(x, dy, cdims)) == dw_pad
341-
@test ddims(∇conv_data(dy, w, cdims)) == dx_pad
340+
@test isapprox(ddims(∇conv_filter(x, dy, cdims)), dw_pad, rtol = 1.0e-7)
341+
@test isapprox(ddims(∇conv_data(dy, w, cdims)), dx_pad, rtol = 1.0e-7)
342342

343343
# Next, test crosscor/conv with a flipped kernel
344344
cdims = DenseConvDims(x, w; flipkernel=true)
345345
dy = NNlib.conv(x, w, cdims)
346-
@test ddims(∇conv_filter(x, dy, cdims)) == dw_flip
347-
@test ddims(∇conv_data(dy, w, cdims)) == dx_flip
346+
@test isapprox(ddims(∇conv_filter(x, dy, cdims)), dw_flip, rtol = 1.0e-7)
347+
@test isapprox(ddims(∇conv_data(dy, w, cdims)), dx_flip, rtol = 1.0e-7)
348348
end
349349
end
350350
end
@@ -481,24 +481,24 @@ end
481481
@test ddims(conv(x, w, cdims)) == y_plain
482482

483483
# Next, test convolution on views and alternate datatypes:
484-
@test ddims(conv(view(x, repeat([:], ndims(x))...), w, cdims)) == y_plain
485-
@test ddims(conv(Float32.(x), Float32.(w), cdims)) == Float32.(y_plain)
484+
@test isapprox(ddims(conv(view(x, repeat([:], ndims(x))...), w, cdims)), y_plain, rtol = 1.0e-7)
485+
@test isapprox(ddims(conv(Float32.(x), Float32.(w), cdims)), Float32.(y_plain), rtol = 1.0e-7)
486486

487487
# Next, introduce stride:
488488
cdims = DepthwiseConvDims(x, w; stride=2)
489-
@test ddims(conv(x, w, cdims)) == y_stride
489+
@test isapprox(ddims(conv(x, w, cdims)), y_stride, rtol = 1.0e-7)
490490

491491
# Next, introduce dilation:
492492
cdims = DepthwiseConvDims(x, w; dilation=2)
493-
@test ddims(conv(x, w, cdims)) == y_dil
493+
@test isapprox(ddims(conv(x, w, cdims)), y_dil, rtol = 1.0e-7)
494494

495495
# Next, introduce padding:
496496
cdims = DepthwiseConvDims(x, w; padding=1)
497-
@test ddims(conv(x, w, cdims)) == y_pad
497+
@test isapprox(ddims(conv(x, w, cdims)), y_pad, rtol = 1.0e-7)
498498

499499
# Next, test crosscor/conv with a flipped kernel
500500
cdims = DepthwiseConvDims(x, w; flipkernel=true)
501-
@test ddims(conv(x, w, cdims)) == y_flip
501+
@test isapprox(ddims(conv(x, w, cdims)), y_flip, rtol = 1.0e-7)
502502
end
503503
end
504504

0 commit comments

Comments
 (0)