Deepity
Ultra-fast, SIMD-accelerated Predictive Coding
Loading...
Searching...
No Matches
ActivationDispatch.h
Go to the documentation of this file.
1#pragma once
2#include <cstddef>
5
14
15namespace Deep
16{
18 using ActivationFn = void (*)(float *, size_t);
20 using DerivativeFn = void (*)(float *, size_t, bool);
21 // Two-buffer variant: reads from src, writes the derivative directly
22 // into dst, in one pass. Eliminates the copy-then-derive pattern
23 // (cblas_scopy + DerivativeFn) callers currently need when they can't
24 // afford to mutate src in place -- src stays untouched throughout.
27 using DerivativeFn2 = void (*)(float *RESTRICT, const float *RESTRICT, size_t);
28
29 // Forward declarations only -- the real definitions (and their full
30 // docs) live in the sibling Relu.h/Gelu.h/Tanh.h/Sigmoid.h/Linear.h,
31 // one activation family per header. @copydoc isn't usable here: with
32 // both this declaration and the real one in the same Deep namespace,
33 // Doxygen resolves "Deep::relu" back to whichever declaration it's
34 // attached to, so it self-references instead of finding the other one.
35
37 static inline void relu(float *, size_t) noexcept;
39 static inline void gelu(float *, size_t) noexcept;
41 static inline void sigmoid(float *, size_t) noexcept;
43 static inline void e_sigmoid(float *, size_t) noexcept;
45 static inline void tanh(float *, size_t) noexcept;
47 static inline void linear(float *, size_t) noexcept;
48
50 static inline void dRelu(float *, size_t, bool) noexcept;
52 static inline void dGelu(float *, size_t, bool) noexcept;
54 static inline void dSigmoid(float *, size_t, bool) noexcept;
56 static inline void d_eSigmoid(float *, size_t, bool) noexcept;
58 static inline void dTanh(float *, size_t, bool) noexcept;
60 static inline void dLinear(float *, size_t, bool) noexcept;
61
63 static inline void dReluInto(float *RESTRICT, const float *RESTRICT, size_t) noexcept;
65 static inline void dGeluInto(float *RESTRICT, const float *RESTRICT, size_t) noexcept;
67 static inline void dSigmoidInto(float *RESTRICT, const float *RESTRICT, size_t) noexcept;
69 static inline void d_eSigmoidInto(float *RESTRICT, const float *RESTRICT, size_t) noexcept;
71 static inline void dTanhInto(float *RESTRICT, const float *RESTRICT, size_t) noexcept;
73 static inline void dLinearInto(float *RESTRICT, const float *RESTRICT, size_t) noexcept;
74
77 static inline ActivationFn To_Fn(ActivationType type)
78 {
79 switch (type)
80 {
82 return relu;
84 return gelu;
86 return sigmoid;
88 return e_sigmoid;
90 return tanh;
92 return linear;
94 default:
95 return nullptr;
96 }
97 }
98
102 static inline DerivativeFn To_dFn(ActivationType dType)
103 {
104 switch (dType)
105 {
107 return dRelu;
109 return dGelu;
111 return dSigmoid;
113 return d_eSigmoid;
115 return dTanh;
117 return dLinear;
119 default:
120 return nullptr;
121 }
122 }
123
127 {
128 switch (dType)
129 {
131 return dReluInto;
133 return dGeluInto;
135 return dSigmoidInto;
137 return d_eSigmoidInto;
139 return dTanhInto;
141 return dLinearInto;
143 default:
144 return nullptr;
145 }
146 }
147
153 {
154 if (fn == relu)
156 if (fn == gelu)
158 if (fn == sigmoid)
160 if (fn == e_sigmoid)
162 if (fn == tanh)
164 if (fn == linear)
167 }
168
173 {
174 if (dfn == dRelu)
176 if (dfn == dGelu)
178 if (dfn == dSigmoid)
180 if (dfn == d_eSigmoid)
182 if (dfn == dTanh)
184 if (dfn == dLinear)
187 }
188} // namespace Deep
The RESTRICT portability macro shared by every activation header. Split out so each per-activation he...
#define RESTRICT
Portability macro for the C++ restrict pointer qualifier (MSVC spells it differently from everyone el...
Definition ActivationMacros.h:16
Just the ActivationType enum, deliberately split out of Activations.h – zero dependency on <immintrin...
This file exists to provide both Tensor.h and IComputeBackend.h with device types without introducing...
Definition Backend.cpp:10
static void sigmoid(float *, size_t) noexcept
See Sigmoid.h.
static void d_eSigmoidInto(float *RESTRICT, const float *RESTRICT, size_t) noexcept
See Sigmoid.h.
static void d_eSigmoid(float *, size_t, bool) noexcept
See Sigmoid.h.
static ActivationType To_AType(ActivationFn fn)
Reverse lookup of To_Fn(): maps a function pointer back to its ActivationType, by identity comparison...
Definition ActivationDispatch.h:152
void(*)(float *RESTRICT, const float *RESTRICT, size_t) DerivativeFn2
Pointer to a two-buffer activation derivative: reads src, writes the derivative into dst,...
Definition ActivationDispatch.h:27
static void linear(float *, size_t) noexcept
See Linear.h.
static void dLinear(float *, size_t, bool) noexcept
See Linear.h.
static void dGelu(float *, size_t, bool) noexcept
See Gelu.h.
ActivationType
Identifies an activation function or its derivative, for dispatch tables and layer/backend APIs that ...
Definition ActivationType.h:25
@ TANH
Definition ActivationType.h:34
@ eSIGMOID
Definition ActivationType.h:32
@ dGELU
Definition ActivationType.h:29
@ dSIGMOID
Definition ActivationType.h:31
@ dTANH
Definition ActivationType.h:35
@ GELU
Definition ActivationType.h:28
@ d_eSIGMOID
Definition ActivationType.h:33
@ LINEAR
Definition ActivationType.h:36
@ SIGMOID
Definition ActivationType.h:30
@ NONE
Definition ActivationType.h:38
@ RELU
Definition ActivationType.h:26
@ dLINEAR
Definition ActivationType.h:37
@ dRELU
Definition ActivationType.h:27
static void dLinearInto(float *RESTRICT, const float *RESTRICT, size_t) noexcept
See Linear.h.
static void tanh(float *, size_t) noexcept
See Tanh.h.
static void gelu(float *, size_t) noexcept
See Gelu.h.
static void relu(float *, size_t) noexcept
See Relu.h.
static void dSigmoidInto(float *RESTRICT, const float *RESTRICT, size_t) noexcept
See Sigmoid.h.
void(*)(float *, size_t, bool) DerivativeFn
Pointer to an in-place activation derivative: f(buf, n, activated).
Definition ActivationDispatch.h:20
static void dReluInto(float *RESTRICT, const float *RESTRICT, size_t) noexcept
See Relu.h.
static DerivativeFn To_dFn(ActivationType dType)
Maps an ActivationType (a derivative variant, e.g. dRELU) to its concrete function pointer.
Definition ActivationDispatch.h:102
static void dSigmoid(float *, size_t, bool) noexcept
See Sigmoid.h.
static void dRelu(float *, size_t, bool) noexcept
See Relu.h.
static void dTanhInto(float *RESTRICT, const float *RESTRICT, size_t) noexcept
See Tanh.h.
static void e_sigmoid(float *, size_t) noexcept
See Sigmoid.h.
static void dTanh(float *, size_t, bool) noexcept
See Tanh.h.
static ActivationFn To_Fn(ActivationType type)
Maps an ActivationType to its concrete function pointer.
Definition ActivationDispatch.h:77
void(*)(float *, size_t) ActivationFn
Pointer to an in-place activation function: f(buf, n).
Definition ActivationDispatch.h:18
static void dGeluInto(float *RESTRICT, const float *RESTRICT, size_t) noexcept
See Gelu.h.
static DerivativeFn2 To_dFn2(ActivationType dType)
Dispatches to the two-buffer (dst, src, n) derivative variant for the given activation type – see Der...
Definition ActivationDispatch.h:126