Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NNlibExt #67

Merged
merged 4 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

[weakdeps]
NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"

[extensions]
TaylorDiffNNlibExt = ["NNlib"]
TaylorDiffSFExt = ["SpecialFunctions"]

[compat]
ChainRules = "1"
ChainRulesCore = "1"
ChainRulesOverloadGeneration = "0.1"
SpecialFunctions = "2"
NNlib = "0.9"
SymbolicUtils = "1"
Symbolics = "5"
Zygote = "0.6.55"
Expand Down
18 changes: 18 additions & 0 deletions ext/TaylorDiffNNlibExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module TaylorDiffNNlibExt

using TaylorDiff
import NNlib: oftf
import NNlib: sigmoid_fast, tanh_fast, rrelu, leakyrelu

@inline sigmoid_fast(t::TaylorScalar) = one(t) / (one(t) + exp(-t))

Check warning on line 7 in ext/TaylorDiffNNlibExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/TaylorDiffNNlibExt.jl#L7

Added line #L7 was not covered by tests

@inline tanh_fast(t::TaylorScalar) = tanh(t)

Check warning on line 9 in ext/TaylorDiffNNlibExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/TaylorDiffNNlibExt.jl#L9

Added line #L9 was not covered by tests

@inline function rrelu(t::TaylorScalar{T, N},

Check warning on line 11 in ext/TaylorDiffNNlibExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/TaylorDiffNNlibExt.jl#L11

Added line #L11 was not covered by tests
l = oftf(t, 1 / 8),
u = oftf(t, 1 / 3)) where {T, N}
a = (u - l) * rand(float(T)) + l
return leakyrelu(t, a)

Check warning on line 15 in ext/TaylorDiffNNlibExt.jl

View check run for this annotation

Codecov / codecov/patch

ext/TaylorDiffNNlibExt.jl#L14-L15

Added lines #L14 - L15 were not covered by tests
end

end
3 changes: 2 additions & 1 deletion ext/TaylorDiffSFExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ using ChainRules, ChainRulesCore

dummy = (NoTangent(), 1)
@variables z
for func in (erf,)
# logerfc, logerfcx, erfinv, gamma, digamma, trigamma
for func in (erf, erfc, erfcinv, erfcx, erfi)
F = typeof(func)
# base case
@eval function (op::$F)(t::TaylorScalar{T, 2}) where {T}
Expand Down
3 changes: 2 additions & 1 deletion src/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ for func in (+, -, deg2rad, rad2deg,
asin, acos, atan, asec, acsc, acot,
log, log10, log1p, log2,
asinh, acosh, atanh, asech, acsch,
acoth)
acoth,
abs, sign)
F = typeof(func)
# base case
@eval function (op::$F)(t::TaylorScalar{T, 2}) where {T}
Expand Down
2 changes: 1 addition & 1 deletion src/derivative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ end
# Core APIs

# Added to help Zygote infer types
make_taylor(t0::T, t1::S, ::Val{N}) where {T, S, N} = TaylorScalar{T, N}(t0, T(t1))
make_taylor(t0::T, t1::S, ::Val{N}) where {T, S, N} = TaylorScalar{T, N}(t0, convert(T, t1))

@inline function derivative(f, x::T, ::Val{N}) where {T <: TN, N}
t = TaylorScalar{T, N}(x, one(x))
Expand Down
4 changes: 4 additions & 0 deletions src/scalar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,7 @@
@eval @inline $op(a::Number, b::TaylorScalar) = $op(promote(a, b)...)
end
transpose(t::TaylorScalar) = t

function Base.AbstractFloat(x::TaylorScalar{T, N}) where {T, N}
TaylorScalar{Float64, N}(convert(NTuple{N, Float64}, x.value))

Check warning on line 93 in src/scalar.jl

View check run for this annotation

Codecov / codecov/patch

src/scalar.jl#L92-L93

Added lines #L92 - L93 were not covered by tests
end
Loading