Skip to content

Commit f6d752f

Browse files
committed
merge and update
2 parents 4ca7695 + 342928e commit f6d752f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+4506
-1801
lines changed

.codecov.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
comment: false

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
*.dll
88
*~
99
\#*
10+
deps/usr
11+
deps.jl
12+
*.log

.travis.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,22 @@ os:
44
- linux
55
- osx
66
julia:
7-
- 0.7
7+
- 1.0
8+
- 1.1
9+
- nightly
10+
matrix:
11+
allow_failures:
12+
- julia: nightly
813
notifications:
914
email: false
1015
git:
1116
depth: 99999999
17+
env:
18+
# Disable test fuzzing for the moment, as we're a little too slow for Travis
19+
- NNLIB_TEST_FUZZING=false
1220

13-
# Submit to Codecov
21+
# Submit to Codecov
1422
after_success:
15-
- julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())'
23+
- if [[ $TRAVIS_JULIA_VERSION = 1.1 ]] && [[ $TRAVIS_OS_NAME = linux ]]; then
24+
julia -e 'using Pkg; Pkg.add("Coverage"); using Coverage; Codecov.submit(process_folder())';
25+
fi

LICENSE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The NNlib.jl package is licensed under the MIT "Expat" License:
22

3-
> Copyright (c) 2017: Mike J Innes.
3+
> Copyright (c) 2017-19: Julia Computing, Inc., Mike J Innes, and Contributors
44
>
55
> Permission is hereby granted, free of charge, to any person obtaining a copy
66
> of this software and associated documentation files (the "Software"), to deal

Manifest.toml

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1+
# This file is machine-generated - editing it directly is not advised
2+
13
[[Base64]]
24
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
35

4-
[[Compat]]
5-
deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"]
6-
git-tree-sha1 = "ff2595695fc4f14427358ce2593f867085c45dcb"
7-
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
8-
version = "1.2.0"
6+
[[BinaryProvider]]
7+
deps = ["Libdl", "Pkg", "SHA", "Test"]
8+
git-tree-sha1 = "055eb2690182ebc31087859c3dd8598371d3ef9e"
9+
uuid = "b99e7846-7c00-51b0-8f62-c81ae34c0232"
10+
version = "0.5.3"
911

1012
[[Dates]]
1113
deps = ["Printf"]
1214
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
1315

14-
[[DelimitedFiles]]
15-
deps = ["Mmap"]
16-
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"
17-
1816
[[Distributed]]
19-
deps = ["LinearAlgebra", "Random", "Serialization", "Sockets"]
17+
deps = ["Random", "Serialization", "Sockets"]
2018
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"
2119

2220
[[InteractiveUtils]]
23-
deps = ["LinearAlgebra", "Markdown"]
21+
deps = ["Markdown"]
2422
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
2523

2624
[[LibGit2]]
@@ -36,19 +34,10 @@ uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
3634
[[Logging]]
3735
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"
3836

39-
[[MacroTools]]
40-
deps = ["Compat"]
41-
git-tree-sha1 = "c443e1c8d58a4e9f61b708ad0a88286c7042145b"
42-
uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
43-
version = "0.4.4"
44-
4537
[[Markdown]]
4638
deps = ["Base64"]
4739
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
4840

49-
[[Mmap]]
50-
uuid = "a63ad114-7e13-5084-954f-fe012c677804"
51-
5241
[[Pkg]]
5342
deps = ["Dates", "LibGit2", "Markdown", "Printf", "REPL", "Random", "SHA", "UUIDs"]
5443
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
@@ -77,10 +66,6 @@ uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"
7766
[[Serialization]]
7867
uuid = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
7968

80-
[[SharedArrays]]
81-
deps = ["Distributed", "Mmap", "Random", "Serialization"]
82-
uuid = "1a1011a3-84de-559e-8e89-a11a2f7dc383"
83-
8469
[[Sockets]]
8570
uuid = "6462fe0b-24de-5631-8697-dd941f90decc"
8671

@@ -97,7 +82,7 @@ deps = ["Distributed", "InteractiveUtils", "Logging", "Random"]
9782
uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
9883

9984
[[UUIDs]]
100-
deps = ["Random"]
85+
deps = ["Random", "SHA"]
10186
uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"
10287

10388
[[Unicode]]

Project.toml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
name = "NNlib"
22
uuid = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"
3+
version = "0.6.0"
34

45
[deps]
6+
BinaryProvider = "b99e7846-7c00-51b0-8f62-c81ae34c0232"
57
Libdl = "8f399da3-3557-5675-b5ff-fb832c97cbdb"
68
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
7-
MacroTools = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09"
89
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
10+
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
11+
12+
[extras]
913
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
14+
15+
[targets]
16+
test = ["Test"]

REQUIRE

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
julia 0.7-
1+
julia 1.0
22
Requires
33
MacroTools
4+
BinaryProvider
5+
TimerOutputs

deps/build.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using BinaryProvider
2+
3+
# Parse some basic command-line arguments
4+
const verbose = "--verbose" in ARGS
5+
const prefix = Prefix(get([a for a in ARGS if a != "--verbose"], 1, joinpath(@__DIR__, "usr")))
6+
products = [
7+
LibraryProduct(prefix, ["libnnpack"], :libnnpack),
8+
]
9+
10+
# Download binaries from hosted location
11+
bin_prefix = "https://github.com/JuliaPackaging/Yggdrasil/releases/download/NNPACK-v2018.06.22-0"
12+
13+
# Listing of files generated by BinaryBuilder:
14+
download_info = Dict(
15+
Linux(:aarch64, libc=:glibc) => ("$bin_prefix/NNPACK.v2018.6.22.aarch64-linux-gnu.tar.gz", "e0c6e21ba4c47acfd5a3d3e3510e8786474080f654338f4583b88860296c1437"),
16+
Linux(:i686, libc=:glibc) => ("$bin_prefix/NNPACK.v2018.6.22.i686-linux-gnu.tar.gz", "e9b6685001bc5a5d17acef15f3f6ffeb7beb6081926300f23ed4a442beac71ca"),
17+
Linux(:i686, libc=:musl) => ("$bin_prefix/NNPACK.v2018.6.22.i686-linux-musl.tar.gz", "36c1d3c30b3bc3e0b34f215945bb46319f88e28f011fc758f21ba888b1fd9e25"),
18+
MacOS(:x86_64) => ("$bin_prefix/NNPACK.v2018.6.22.x86_64-apple-darwin14.tar.gz", "b30046223a11470b15a2ceb0d0df6f7d8a43260fe52f4a2f8ebe5f0b2df822ca"),
19+
Linux(:x86_64, libc=:glibc) => ("$bin_prefix/NNPACK.v2018.6.22.x86_64-linux-gnu.tar.gz", "150d5b6ca81fa72bfdc8bbda2428f0d3483fd11a5813724646c6d6c6a7ef969f"),
20+
Linux(:x86_64, libc=:musl) => ("$bin_prefix/NNPACK.v2018.6.22.x86_64-linux-musl.tar.gz", "d961a104f814ec5b356519a82746a70a1df193ae37fc8130f38ffb61336def16"),
21+
)
22+
23+
# Install unsatisfied or updated dependencies:
24+
unsatisfied = any(!satisfied(p; verbose=verbose) for p in products)
25+
dl_info = choose_download(download_info, platform_key_abi())
26+
if dl_info === nothing && unsatisfied
27+
# If we don't have a compatible .tar.gz to download, complain.
28+
# Alternatively, you could attempt to install from a separate provider,
29+
# build from source or something even more ambitious here.
30+
@warn "Your platform (\"$(Sys.MACHINE)\", parsed as \"$(triplet(platform_key_abi()))\") is not supported by NNPACK!
31+
You will only be able to use only the default NNlib backend."
32+
end
33+
34+
# If we have a download, and we are unsatisfied (or the version we're
35+
# trying to install is not itself installed) then load it up!
36+
if unsatisfied || !isinstalled(dl_info...; prefix=prefix)
37+
# Download and install binaries
38+
install(dl_info...; prefix=prefix, force=true, verbose=verbose)
39+
end
40+
41+
# Write out a deps.jl file that will contain mappings for our products
42+
write_deps_file(joinpath(@__DIR__, "deps.jl"), products, verbose=verbose)

src/NNlib.jl

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
module NNlib
2+
using Requires
23

3-
using Requires, Libdl
4+
# Include APIs
5+
include("dim_helpers.jl")
46

5-
export σ, sigmoid, relu, leakyrelu, elu, gelu, swish, selu, softplus, softsign, logσ, logsigmoid,
6-
softmax, logsoftmax, maxpool, meanpool, batched_mul, batched_transpose, batched_adjoint
7+
# NNPACK support
8+
if Sys.islinux() || Sys.isapple()
9+
include("nnpack/NNPACK.jl")
10+
else
11+
is_nnpack_available() = false
12+
end
713

8-
include("numeric.jl")
914
include("activation.jl")
1015
include("softmax.jl")
11-
include("logsoftmax.jl")
12-
include("linalg.jl")
1316
include("batchedmul.jl")
17+
include("gemm.jl")
1418
include("conv.jl")
15-
include("cubroadcast.jl")
19+
include("pooling.jl")
1620

17-
end # module
21+
## Include implementations
22+
include("impl/padding_edges.jl")
23+
24+
# Direct implementations of convolutional and depthwise-convolutional algorithms
25+
include("impl/conv_direct.jl")
26+
include("impl/depthwiseconv_direct.jl")
27+
# im2col implementations of convolutional and depthwise-convolutional algorithms
28+
include("impl/conv_im2col.jl")
29+
include("impl/depthwiseconv_im2col.jl")
30+
31+
# Direct implementations of pooling
32+
include("impl/pooling_direct.jl")
33+
34+
end # module NNlib

src/activation.jl

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
1+
export σ, sigmoid, relu, leakyrelu, elu, gelu, swish, selu, softplus, softsign, logσ,
2+
logsigmoid, logcosh
3+
14
"""
25
σ(x) = 1 / (1 + exp(-x))
36
47
Classic [sigmoid](https://en.wikipedia.org/wiki/Sigmoid_function) activation
58
function.
69
"""
7-
σ(x) = one(x) / (one(x) + exp(-x))
8-
10+
σ(x::Real) = one(x) / (one(x) + exp(-x))
911
const sigmoid = σ
1012

1113
# ForwardDiff numerical stability hack
12-
σ_stable(x) = ifelse(x < -80, zero(x), one(x) / (one(x) + exp(-x)))
13-
14+
σ_stable(x::Real) = ifelse(x < -80, zero(x), one(x) / (one(x) + exp(-x)))
1415
σ(x::Float32) = σ_stable(x)
15-
1616
@init @require ForwardDiff="f6369f11-7733-5829-9624-2563aa707210" begin
1717
σ(x::ForwardDiff.Dual{T,Float32}) where T = σ_stable(x)
1818
end
1919

20+
2021
"""
2122
logσ(x)
2223
2324
Return `log(σ(x))` which is computed in a numerically stable way.
2425
25-
julia> logσ(0.)
26+
julia> logσ(0)
2627
-0.6931471805599453
27-
julia> logσ.([-100, -10, 100.])
28+
julia> logσ.([-100, -10, 100])
2829
3-element Array{Float64,1}:
2930
-100.0
30-
-10.0
31-
-0.0
31+
-10.000045398899218
32+
-3.720075976020836e-44
3233
"""
33-
function logσ(x)
34-
max_v = max(zero(x), -x)
35-
z = exp(-max_v) + exp(-x-max_v)
36-
-(max_v + log(z))
37-
end
38-
34+
logσ(x::Real) = -softplus(-x)
3935
const logsigmoid = logσ
4036

37+
4138
"""
4239
relu(x) = max(0, x)
4340
4441
[Rectified Linear Unit](https://en.wikipedia.org/wiki/Rectifier_(neural_networks))
4542
activation function.
4643
"""
47-
relu(x) = max(zero(x), x)
44+
relu(x::Real) = max(zero(x), x)
4845

4946

5047
"""
@@ -54,7 +51,8 @@ Leaky [Rectified Linear Unit](https://en.wikipedia.org/wiki/Rectifier_(neural_ne
5451
activation function.
5552
You can also specify the coefficient explicitly, e.g. `leakyrelu(x, 0.01)`.
5653
"""
57-
leakyrelu(x, a = oftype(x/1, 0.01)) = max(a*x, x/1)
54+
leakyrelu(x::Real, a = oftype(x/1, 0.01)) = max(a*x, x/1)
55+
5856

5957
"""
6058
elu(x, α = 1) =
@@ -66,13 +64,14 @@ You can also specify the coefficient explicitly, e.g. `elu(x, 1)`.
6664
"""
6765
elu(x, α = one(x)) = ifelse(x 0, x/1, α * (exp(x) - one(x)))
6866

67+
6968
"""
7069
gelu(x) = 0.5x*(1 + tanh(√(2/π)*(x + 0.044715x^3)))
7170
7271
[Gaussian Error Linear Unit](https://arxiv.org/pdf/1606.08415.pdf)
7372
activation function.
7473
"""
75-
function gelu(x)
74+
function gelu(x::Real)
7675
λ = oftype(x/1, (2/π))
7776
α = oftype(x/1, 0.044715)
7877
h = oftype(x/1, 0.5)
@@ -86,7 +85,7 @@ end
8685
Self-gated actvation function.
8786
See [Swish: a Self-Gated Activation Function](https://arxiv.org/pdf/1710.05941.pdf).
8887
"""
89-
swish(x) = x * σ(x)
88+
swish(x::Real) = x * σ(x)
9089

9190
"""
9291
selu(x) = λ * (x ≥ 0 ? x : α * (exp(x) - 1))
@@ -97,23 +96,38 @@ swish(x) = x * σ(x)
9796
Scaled exponential linear units.
9897
See [Self-Normalizing Neural Networks](https://arxiv.org/pdf/1706.02515.pdf).
9998
"""
100-
function selu(x)
99+
function selu(x::Real)
101100
λ = oftype(x/1, 1.0507009873554804934193349852946)
102101
α = oftype(x/1, 1.6732632423543772848170429916717)
103102
λ * ifelse(x > 0, x/1, α * (exp(x) - 1))
104103
end
105104

105+
106106
"""
107107
softsign(x) = x / (1 + |x|)
108108
109109
See [Quadratic Polynomials Learn Better Image Features](http://www.iro.umontreal.ca/~lisa/publications2/index.php/attachments/single/205).
110110
"""
111-
softsign(x) = x / (one(x) + abs(x))
111+
softsign(x::Real) = x / (one(x) + abs(x))
112112

113113

114114
"""
115115
softplus(x) = log(exp(x) + 1)
116116
117117
See [Deep Sparse Rectifier Neural Networks](http://proceedings.mlr.press/v15/glorot11a/glorot11a.pdf).
118118
"""
119-
softplus(x) = ifelse(x > 0, x + log1p(exp(-x)), log1p(exp(x)))
119+
softplus(x::Real) = ifelse(x > 0, x + log1p(exp(-x)), log1p(exp(x)))
120+
121+
122+
"""
123+
logcosh(x)
124+
125+
Return `log(cosh(x))` which is computed in a numerically stable way.
126+
"""
127+
logcosh(x::T) where T = x + softplus(-2x) - log(convert(T, 2))
128+
129+
# Provide an informative error message if activation functions are called with an array
130+
for f in (, :σ_stable, :logσ, :relu, :leakyrelu, :elu, :gelu, :swish, :selu, :softsign, :softplus, :logcosh)
131+
@eval $(f)(x::AbstractArray, args...) =
132+
error("Use broadcasting (`", $(string(f)), ".(x)`) to apply activation functions to arrays.")
133+
end

0 commit comments

Comments
 (0)