From f5cb955e33101b31f6423ae513567defd69d2a3f Mon Sep 17 00:00:00 2001 From: Dhairya Gandhi Date: Mon, 23 Dec 2019 18:25:21 +0530 Subject: [PATCH 01/12] refactor tests --- test/conv.jl | 20 +++++++++++--------- test/inference.jl | 8 +++++--- test/pooling.jl | 14 ++++++++------ 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/test/conv.jl b/test/conv.jl index 55bbbaec6..bbe93e350 100644 --- a/test/conv.jl +++ b/test/conv.jl @@ -352,12 +352,11 @@ conv_answer_dict = Dict( end end end +end +if get(ENV,"NNLIB_TEST_FUZZING","false") == "true" + # @info("Skipping Convolutional fuzzing tests, set NNLIB_TEST_FUZZING=true to run them") @testset "fuzzing" begin - if get(ENV,"NNLIB_TEST_FUZZING","false") != "true" - @info("Skipping Convolutional fuzzing tests, set NNLIB_TEST_FUZZING=true to run them") - return - end @info("Starting Convolutional fuzzing tests; this can take a few minutes...") # Now that we're fairly certain things are working, let's fuzz things a little bit: for x_size in ( @@ -441,9 +440,10 @@ conv_answer_dict = Dict( end println() end +else + @info "Skipping Convolutional fuzzing tests, set NNLIB_TEST_FUZZING=true to run them" end - @testset "Depthwise Convolution" begin # Start with some easy-to-debug cases that we have worked through and _know_ work for rank in (1,) #2,3) @@ -552,12 +552,11 @@ end end end end +end + +if get(ENV,"NNLIB_TEST_FUZZING","false") == "true" @testset "fuzzing" begin - if get(ENV,"NNLIB_TEST_FUZZING","false") != "true" - @info("Skipping Depthwise Convolutional fuzzing tests, set NNLIB_TEST_FUZZING=true to run them") - return - end @info("Starting Depthwise Convolutional fuzzing tests; this can take a few minutes...") # Now that we're fairly certain things are working, let's fuzz things a little bit: for x_size in ( @@ -641,8 +640,11 @@ end end println() end +else + @info "Skipping Depthwise Convolutional fuzzing tests, set NNLIB_TEST_FUZZING=true to run them" end + @testset "conv_wrapper" begin x = rand(10, 10, 3, 10) w = rand(2, 2, 3, 16) diff --git a/test/inference.jl b/test/inference.jl index 896ff22a1..8add22312 100644 --- a/test/inference.jl +++ b/test/inference.jl @@ -1,5 +1,4 @@ -using NNlib, Test -using NNlib: conv_direct, conv_im2col +import NNlib: conv_direct, conv_im2col @testset "Conv Inference" begin x = rand(10, 10, 3, 2) @@ -9,6 +8,9 @@ using NNlib: conv_direct, conv_im2col NNlib.is_nnpack_available() && push!(impl, NNlib.conv_nnpack) for T in impl - @inferred T(x, w, DenseConvDims(x, w)) + @test T(x, w, DenseConvDims(x, w)) isa AbstractArray{K,4} where K end + + h() = error("check tests") + @test h() end diff --git a/test/pooling.jl b/test/pooling.jl index f7ada801c..7135a3770 100644 --- a/test/pooling.jl +++ b/test/pooling.jl @@ -1,4 +1,4 @@ -using NNlib, Test +#using NNlib, Test maxpool_answer_dict = Dict( 1 => Dict( @@ -298,11 +298,13 @@ for rank in (1, 2, 3) end end -x = rand(10, 10, 3, 10) -@test size(maxpool(x, (2, 2))) == (5, 5, 3, 10) -@test size(maxpool(x, (2, 2); pad = (1, 1), stride = (2, 2))) == (6, 6, 3, 10) -@test size(meanpool(x, (2, 2))) == (5, 5, 3, 10) -@test size(meanpool(x, (2, 2); pad = (1, 1), stride = (2, 2))) == (6, 6, 3, 10) +@testset "Pooling - Check Sizes" begin + x = rand(10, 10, 3, 10) + @test size(maxpool(x, (2, 2))) == (5, 5, 3, 10) + @test size(maxpool(x, (2, 2); pad = (1, 1), stride = (2, 2))) == (6, 6, 3, 10) + @test size(meanpool(x, (2, 2))) == (5, 5, 3, 10) + @test size(meanpool(x, (2, 2); pad = (1, 1), stride = (2, 2))) == (6, 6, 3, 10) +end # Add another test for 2d maxpool that uses an odd-length size: @testset "Issue #133" begin From b8fb4927ea64528632c2e5ef2ac1bc8481e8c40e Mon Sep 17 00:00:00 2001 From: Dhairya Gandhi Date: Mon, 23 Dec 2019 18:34:38 +0530 Subject: [PATCH 02/12] rm throwing fn --- test/inference.jl | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/inference.jl b/test/inference.jl index 8add22312..39b5108ce 100644 --- a/test/inference.jl +++ b/test/inference.jl @@ -10,7 +10,4 @@ import NNlib: conv_direct, conv_im2col for T in impl @test T(x, w, DenseConvDims(x, w)) isa AbstractArray{K,4} where K end - - h() = error("check tests") - @test h() end From daa862a58a5196a35ffc7c543bdf8809a1060608 Mon Sep 17 00:00:00 2001 From: Dhairya Gandhi Date: Mon, 23 Dec 2019 19:02:44 +0530 Subject: [PATCH 03/12] obscuring conv_nnpack for CI --- test/conv.jl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/test/conv.jl b/test/conv.jl index bbe93e350..cf91a5361 100644 --- a/test/conv.jl +++ b/test/conv.jl @@ -274,9 +274,13 @@ conv_answer_dict = Dict( # A "drop channels and batch dimension" helper ddims(x) = dropdims(x, dims=(rank+1, rank+2)) - for conv in (NNlib.conv, NNlib.conv_im2col, NNlib.conv_direct, NNlib.conv_nnpack) - if conv == NNlib.conv_nnpack && !NNlib.nnpack_supported_operation(DenseConvDims(x, w)) - continue + convs = [NNlib.conv, NNlib.conv_im2col, NNlib.conv_direct,] + NNlib.is_nnpack_available() && push!(convs, NNlib.conv_nnpack) + for conv in convs + if NNlib.is_nnpack_available() + if conv == NNlib.conv_nnpack && !NNlib.nnpack_supported_operation(DenseConvDims(x, w)) + continue + end end @testset "$(conv)" begin cdims = DenseConvDims(x, w) From 36010e4b58276392b5f503d46a270dff86b43b46 Mon Sep 17 00:00:00 2001 From: Dhairya Gandhi Date: Tue, 24 Dec 2019 12:07:14 +0530 Subject: [PATCH 04/12] add return types annotations --- src/nnpack/impl.jl | 6 +++--- src/nnpack/interface.jl | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/nnpack/impl.jl b/src/nnpack/impl.jl index 5d3086583..7e32d5400 100644 --- a/src/nnpack/impl.jl +++ b/src/nnpack/impl.jl @@ -7,7 +7,7 @@ end function conv_nnpack!(y::A1, x::A1, w::A1, cdims::ConvDims; b::A2 = zeros(Float32, size(x, 3)), - algo = UInt32(0)) where {A1<:Array{Float32, 4}, + algo = UInt32(0))::A1 where {A1<:Array{Float32, 4}, A2<:Array{Float32, 1}} check_dims(size(x), size(w), size(y), cdims) threadpool = select_threadpool(cdims, size(y, 4)) @@ -21,7 +21,7 @@ function conv_nnpack!(y::A1, x::A1, w::A1, cdims::ConvDims; end function ∇conv_data_nnpack!(dx::A, dy::A, w::A, cdims::ConvDims; - algo = UInt32(0)) where{A<:Array{Float32, 4}} + algo = UInt32(0))::A where{A<:Array{Float32, 4}} check_dims(size(dx), size(w), size(dy), cdims) threadpool = select_threadpool(cdims, size(y, 4)) @@ -34,7 +34,7 @@ function ∇conv_data_nnpack!(dx::A, dy::A, w::A, cdims::ConvDims; end function ∇conv_filter_nnpack!(dw::A, x::A, dy::A, cdims::ConvDims; - algo = UInt32(0)) where{A<:Array{Float32, 4}} + algo = UInt32(0))::A where{A<:Array{Float32, 4}} check_dims(size(x), size(dw), size(dy), cdims) threadpool = select_threadpool(cdims, size(y, 4)) diff --git a/src/nnpack/interface.jl b/src/nnpack/interface.jl index 25ab93632..316d44376 100644 --- a/src/nnpack/interface.jl +++ b/src/nnpack/interface.jl @@ -19,7 +19,7 @@ for (front_name, backend) in ( end -function conv_nnpack(x::Array{T1, 4}, w::Array{T2, 4}, cdims::ConvDims; kwargs...) where {T1, T2} +function conv_nnpack(x::Array{T1, 4}, w::Array{T2, 4}, cdims::ConvDims; kwargs...)::Array{Float32,4} where {T1, T2} y = similar(x, output_size(cdims)..., channels_out(cdims), size(x, 4)) return conv_nnpack!(y, x, w, cdims; kwargs...) end From 1b4ce0859609ff1527782b8b48afd2b854336851 Mon Sep 17 00:00:00 2001 From: Dhairya Gandhi Date: Tue, 24 Dec 2019 12:18:55 +0530 Subject: [PATCH 05/12] reverting the type annotations --- src/nnpack/impl.jl | 6 +++--- src/nnpack/interface.jl | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/nnpack/impl.jl b/src/nnpack/impl.jl index 7e32d5400..5d3086583 100644 --- a/src/nnpack/impl.jl +++ b/src/nnpack/impl.jl @@ -7,7 +7,7 @@ end function conv_nnpack!(y::A1, x::A1, w::A1, cdims::ConvDims; b::A2 = zeros(Float32, size(x, 3)), - algo = UInt32(0))::A1 where {A1<:Array{Float32, 4}, + algo = UInt32(0)) where {A1<:Array{Float32, 4}, A2<:Array{Float32, 1}} check_dims(size(x), size(w), size(y), cdims) threadpool = select_threadpool(cdims, size(y, 4)) @@ -21,7 +21,7 @@ function conv_nnpack!(y::A1, x::A1, w::A1, cdims::ConvDims; end function ∇conv_data_nnpack!(dx::A, dy::A, w::A, cdims::ConvDims; - algo = UInt32(0))::A where{A<:Array{Float32, 4}} + algo = UInt32(0)) where{A<:Array{Float32, 4}} check_dims(size(dx), size(w), size(dy), cdims) threadpool = select_threadpool(cdims, size(y, 4)) @@ -34,7 +34,7 @@ function ∇conv_data_nnpack!(dx::A, dy::A, w::A, cdims::ConvDims; end function ∇conv_filter_nnpack!(dw::A, x::A, dy::A, cdims::ConvDims; - algo = UInt32(0))::A where{A<:Array{Float32, 4}} + algo = UInt32(0)) where{A<:Array{Float32, 4}} check_dims(size(x), size(dw), size(dy), cdims) threadpool = select_threadpool(cdims, size(y, 4)) diff --git a/src/nnpack/interface.jl b/src/nnpack/interface.jl index 316d44376..25ab93632 100644 --- a/src/nnpack/interface.jl +++ b/src/nnpack/interface.jl @@ -19,7 +19,7 @@ for (front_name, backend) in ( end -function conv_nnpack(x::Array{T1, 4}, w::Array{T2, 4}, cdims::ConvDims; kwargs...)::Array{Float32,4} where {T1, T2} +function conv_nnpack(x::Array{T1, 4}, w::Array{T2, 4}, cdims::ConvDims; kwargs...) where {T1, T2} y = similar(x, output_size(cdims)..., channels_out(cdims), size(x, 4)) return conv_nnpack!(y, x, w, cdims; kwargs...) end From dc8d7348a4ace146a2b9a7306cd47ae1c59fba2e Mon Sep 17 00:00:00 2001 From: Dhairya Gandhi Date: Tue, 24 Dec 2019 13:40:16 +0530 Subject: [PATCH 06/12] switch NNPACK off --- src/NNlib.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NNlib.jl b/src/NNlib.jl index b2a8e8f2e..c4ad6ad3c 100644 --- a/src/NNlib.jl +++ b/src/NNlib.jl @@ -6,7 +6,8 @@ include("dim_helpers.jl") # NNPACK support if Sys.islinux() || Sys.isapple() - include("nnpack/NNPACK.jl") + # include("nnpack/NNPACK.jl") + is_nnpack_available() = false else is_nnpack_available() = false end From ba8e771d04a89cc71adcc2706b3fab6993bb74a1 Mon Sep 17 00:00:00 2001 From: Dhairya Gandhi Date: Tue, 24 Dec 2019 13:43:05 +0530 Subject: [PATCH 07/12] let the user decide --- src/NNlib.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/NNlib.jl b/src/NNlib.jl index c4ad6ad3c..2de0cd53b 100644 --- a/src/NNlib.jl +++ b/src/NNlib.jl @@ -5,11 +5,14 @@ using Requires include("dim_helpers.jl") # NNPACK support -if Sys.islinux() || Sys.isapple() - # include("nnpack/NNPACK.jl") - is_nnpack_available() = false +if get(ENV, "NNLIB_USE_NNPACK", "false") + if Sys.islinux() || Sys.isapple() + include("nnpack/NNPACK.jl") + else + is_nnpack_available() = false + end else - is_nnpack_available() = false + is_nnpack_available() = false end include("activation.jl") From cc06137b4641bea593c700cf430aef1c8771de8f Mon Sep 17 00:00:00 2001 From: Dhairya Gandhi Date: Tue, 24 Dec 2019 13:50:39 +0530 Subject: [PATCH 08/12] fixes --- src/NNlib.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NNlib.jl b/src/NNlib.jl index 2de0cd53b..967500b31 100644 --- a/src/NNlib.jl +++ b/src/NNlib.jl @@ -5,7 +5,7 @@ using Requires include("dim_helpers.jl") # NNPACK support -if get(ENV, "NNLIB_USE_NNPACK", "false") +if get(ENV, "NNLIB_USE_NNPACK", "false") == "true" if Sys.islinux() || Sys.isapple() include("nnpack/NNPACK.jl") else From 836889d678a98aafb5d3e960657bbb27f649df37 Mon Sep 17 00:00:00 2001 From: Dhairya Gandhi Date: Tue, 24 Dec 2019 20:28:06 +0530 Subject: [PATCH 09/12] make NNPACK conditional --- deps/build.jl | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/deps/build.jl b/deps/build.jl index 6021479f3..13cc54295 100644 --- a/deps/build.jl +++ b/deps/build.jl @@ -33,10 +33,18 @@ end # If we have a download, and we are unsatisfied (or the version we're # trying to install is not itself installed) then load it up! -if unsatisfied || !isinstalled(dl_info...; prefix=prefix) - # Download and install binaries +# Download and install binaries +use_nnpack = get(ENV, "NNLIB_USE_NNPACK", "false") == "true" +os_support = Sys.islinux() || Sys.isapple() +if use_nnpack && os_support + if unsatisfied || !isinstalled(dl_info...; prefix=prefix) install(dl_info...; prefix=prefix, force=true, verbose=verbose) + # Write out a deps.jl file that will contain mappings for our products + write_deps_file(joinpath(@__DIR__, "deps.jl"), products, verbose=verbose) + end +else + open(joinpath(@__DIR__, "deps.jl"), "w") do io + write(io, "check_deps() = false") + end end -# Write out a deps.jl file that will contain mappings for our products -write_deps_file(joinpath(@__DIR__, "deps.jl"), products, verbose=verbose) From cd789e8b3f79e93e0532898068738417e087f10c Mon Sep 17 00:00:00 2001 From: Dhairya Gandhi Date: Tue, 24 Dec 2019 20:29:21 +0530 Subject: [PATCH 10/12] check_deps from deps.jl --- src/NNlib.jl | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/NNlib.jl b/src/NNlib.jl index 967500b31..aa08bc299 100644 --- a/src/NNlib.jl +++ b/src/NNlib.jl @@ -5,12 +5,9 @@ using Requires include("dim_helpers.jl") # NNPACK support -if get(ENV, "NNLIB_USE_NNPACK", "false") == "true" - if Sys.islinux() || Sys.isapple() - include("nnpack/NNPACK.jl") - else - is_nnpack_available() = false - end +include(joinpath(@__DIR__, "..", "deps", "deps.jl")) +if check_deps() == nothing + include("nnpack/NNPACK.jl") else is_nnpack_available() = false end From db59d8ef3b7b6360a77a64051be4d3d56e3ada38 Mon Sep 17 00:00:00 2001 From: Dhairya Gandhi Date: Tue, 24 Dec 2019 20:30:46 +0530 Subject: [PATCH 11/12] fallback return false --- src/nnpack/NNPACK.jl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/nnpack/NNPACK.jl b/src/nnpack/NNPACK.jl index 52a518ed7..1e420a9cc 100644 --- a/src/nnpack/NNPACK.jl +++ b/src/nnpack/NNPACK.jl @@ -8,7 +8,6 @@ const depsjl_path = joinpath(dirname(@__FILE__), "..", "..", "deps", "deps.jl") if !isfile(depsjl_path) error("NNPACK not installed properly, run Pkg.build(\"NNlib\"), restart Julia and try again") end -include(depsjl_path) const shared_threadpool_dict = Dict{UInt64, Base.RefValue}() @@ -18,7 +17,7 @@ const shared_threadpool_dict = Dict{UInt64, Base.RefValue}() Checks if the current hardware is supported by NNPACK. """ function is_nnpack_available() - check_deps() + check_deps() isa Nothing || return false status = nnp_initialize() if status == nnp_status_unsupported_hardware return false From 20e8e47cadefee8d670e837b379096b2f7e62c66 Mon Sep 17 00:00:00 2001 From: Dhairya Gandhi Date: Tue, 24 Dec 2019 20:59:30 +0530 Subject: [PATCH 12/12] write deps on build everytime --- deps/build.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/deps/build.jl b/deps/build.jl index 13cc54295..3da8d53c2 100644 --- a/deps/build.jl +++ b/deps/build.jl @@ -39,9 +39,9 @@ os_support = Sys.islinux() || Sys.isapple() if use_nnpack && os_support if unsatisfied || !isinstalled(dl_info...; prefix=prefix) install(dl_info...; prefix=prefix, force=true, verbose=verbose) - # Write out a deps.jl file that will contain mappings for our products - write_deps_file(joinpath(@__DIR__, "deps.jl"), products, verbose=verbose) end + # Write out a deps.jl file that will contain mappings for our products + write_deps_file(joinpath(@__DIR__, "deps.jl"), products, verbose=verbose) else open(joinpath(@__DIR__, "deps.jl"), "w") do io write(io, "check_deps() = false")