|
1 |
| -# descriptor |
2 |
| - |
3 |
| -mutable struct ActivationDesc |
4 |
| - ptr::cudnnActivationDescriptor_t |
| 1 | +""" |
| 2 | + cudnnActivationForward(x; mode, nanOpt, coef, alpha) |
| 3 | + cudnnActivationForward(x, d::cudnnActivationDescriptor; alpha) |
| 4 | + cudnnActivationForward!(y, x; mode, nanOpt, coef, alpha, beta) |
| 5 | + cudnnActivationForward!(y, x, d::cudnnActivationDescriptor; alpha, beta) |
| 6 | +
|
| 7 | +Return the result of the specified elementwise activation operation applied to `x`. |
| 8 | +Optionally `y` holds the result and `d` specifies the operation. `y` should be similar to |
| 9 | +`x` if specified. Keyword arguments `alpha=1, beta=0` can be used for scaling, i.e. `y .= |
| 10 | +alpha*op.(x1) .+ beta*y`. The following keyword arguments specify the operation if `d` is |
| 11 | +not given: |
| 12 | +
|
| 13 | +* `mode = CUDNN_ACTIVATION_RELU`: Options are SIGMOID, RELU, TANH, CLIPPED_RELU, ELU, IDENTITY |
| 14 | +* `nanOpt = CUDNN_NOT_PROPAGATE_NAN`: NAN propagation policy, the other option is `CUDNN_PROPAGATE_NAN` |
| 15 | +* `coef=1`: When the activation mode is set to CUDNN_ACTIVATION_CLIPPED_RELU, this input specifies the clipping threshold; and when the activation mode is set to CUDNN_ACTIVATION_ELU, this input specifies the α parameter. |
| 16 | +""" |
| 17 | +cudnnActivationForward, cudnnActivationForward! |
| 18 | + |
| 19 | + |
| 20 | +# Public methods |
| 21 | +cudnnActivationForward(x; o...) = cudnnActivationForwardWithDefaults(x; o...) |
| 22 | +cudnnActivationForward!(y, x; o...) = cudnnActivationForwardWithDefaults(x; y, o...) |
| 23 | +cudnnActivationForward(x, d::cudnnActivationDescriptor; o...) = cudnnActivationForwardWithDefaults(x; activationDesc=d, o...) |
| 24 | +cudnnActivationForward!(y, x, d::cudnnActivationDescriptor; o...) = cudnnActivationForwardWithDefaults(x; y, activationDesc=d, o...) |
| 25 | + |
| 26 | + |
| 27 | +# Private method |
| 28 | +function cudnnActivationForwardWithDefaults( |
| 29 | + x; |
| 30 | + y = similar(x), |
| 31 | + mode::cudnnActivationMode_t = CUDNN_ACTIVATION_RELU, |
| 32 | + nanOpt::cudnnNanPropagation_t = CUDNN_NOT_PROPAGATE_NAN, |
| 33 | + coef::Real=1, |
| 34 | + activationDesc::cudnnActivationDescriptor = cudnnActivationDescriptor(mode, nanOpt, Cdouble(coef)), |
| 35 | + alpha::Real=1, |
| 36 | + beta::Real=0, |
| 37 | + xDesc::cudnnTensorDescriptor = cudnnTensorDescriptor(x), |
| 38 | + yDesc::cudnnTensorDescriptor = xDesc, |
| 39 | +) |
| 40 | + T = eltype(x) |
| 41 | + alpha, beta = scalingParameter(T,alpha), scalingParameter(T,beta) |
| 42 | + cudnnActivationForwardAD(x; activationDesc, alpha, xDesc, beta, yDesc, y) |
5 | 43 | end
|
6 | 44 |
|
7 |
| -unsafe_free!(ad::ActivationDesc)=cudnnDestroyActivationDescriptor(ad.ptr) |
8 |
| - |
9 |
| -Base.unsafe_convert(::Type{cudnnActivationDescriptor_t}, ad::ActivationDesc)=ad.ptr |
10 | 45 |
|
11 |
| -function ActivationDesc(mode, coeff, reluNanOpt=CUDNN_NOT_PROPAGATE_NAN) |
12 |
| - ad = Ref{cudnnActivationDescriptor_t}() |
13 |
| - cudnnCreateActivationDescriptor(ad) |
14 |
| - cudnnSetActivationDescriptor(ad[],mode,reluNanOpt,coeff) |
15 |
| - this = ActivationDesc(ad[]) |
16 |
| - finalizer(unsafe_free!, this) |
17 |
| - return this |
| 46 | +# AD method: |
| 47 | +function cudnnActivationForwardAD(x; activationDesc, alpha, xDesc, beta, yDesc, y) |
| 48 | + cudnnActivationForward(handle(), activationDesc, alpha, xDesc, x, beta, yDesc, y) |
| 49 | + return y |
18 | 50 | end
|
19 | 51 |
|
20 | 52 |
|
21 |
| -# wrappers |
22 |
| - |
23 |
| -function cudnnActivationForward(x::DenseCuArray{T,N}, y::DenseCuArray{T,N}=x; |
24 |
| - mode=CUDNN_ACTIVATION_RELU, # CUDNN_ACTIVATION_IDENTITY will not work |
25 |
| - coeff=false, reluNanOpt=CUDNN_NOT_PROPAGATE_NAN, alpha=true, |
26 |
| - beta=false) where {T,N} |
27 |
| - cudnnActivationForward(handle(), ActivationDesc(mode, T(coeff), reluNanOpt), |
28 |
| - scalingParameter(T, alpha), TensorDesc(x), x, |
29 |
| - scalingParameter(T, beta ), TensorDesc(y), y) |
30 |
| - return y |
31 |
| -end |
32 |
| - |
33 |
| -function cudnnActivationBackward(x::DenseCuArray{T,N}, dx::DenseCuArray{T,N}, |
34 |
| - y::DenseCuArray{T,N}, dy::DenseCuArray{T,N}=dx; |
35 |
| - mode=CUDNN_ACTIVATION_RELU, # CUDNN_ACTIVATION_IDENTITY will not work |
36 |
| - coeff=false, reluNanOpt=CUDNN_NOT_PROPAGATE_NAN, alpha=1, |
37 |
| - beta=false) where {T,N} |
38 |
| - cudnnActivationBackward(handle(), ActivationDesc(mode, T(coeff), reluNanOpt), |
39 |
| - scalingParameter(T, alpha), TensorDesc( y), y, |
40 |
| - TensorDesc(dy), dy, |
41 |
| - TensorDesc( x), x, |
42 |
| - scalingParameter(T, beta ), TensorDesc(dx), dx) |
43 |
| - return dx |
| 53 | +# Deprecated: |
| 54 | +function cudnnActivationForward(x::DenseCuArray{T,N}, y::DenseCuArray{T,N}; o...) where {T,N} |
| 55 | + @warn "`cudnnActivationForward(x,y)` is deprecated, please use one of the methods in `@doc cudnnActivationForward`." maxlog=1 |
| 56 | + cudnnActivationForward!(y, x; o...) |
44 | 57 | end
|
0 commit comments