src/cpu/activations.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ─── CPU activation ops (CHUNK 2) ────────────────────────────────────────── | ||
| 2 | // | ||
| 3 | // FP32 scalar host implementations. Ports the GPU activation kernels in | ||
| 4 | // src/cuda/elementwise.cu — kernel math reproduced verbatim, FP32 path only. | ||
| 5 | // | ||
| 6 | // silu — x * sigmoid(x) | ||
| 7 | // gelu — tanh-approximation GELU (PyTorch approximate="tanh") | ||
| 8 | // gelu_exact — erf-based GELU (PyTorch approximate="none") | ||
| 9 | // quick_gelu — x * sigmoid(1.702 * x) (OpenAI CLIP) | ||
| 10 | // | ||
| 11 | // Each has a matching backward op. Outputs are sized to mirror the GPU op | ||
| 12 | // (resize if shape/dtype differs); backward writes dX (overwrite, not | ||
| 13 | // accumulate — matches the GPU which writes dX[i] directly). | ||
| 14 | |||
| 15 | #include <brotensor/tensor.h> | ||
| 16 | |||
| 17 | #include <algorithm> | ||
| 18 | #include <cmath> | ||
| 19 | |||
| 20 | namespace brotensor::detail::cpu { | ||
| 21 | |||
| 22 | namespace { | ||
| 23 | |||
| 24 | 19150721 | inline float silu_scalar(float v) { | |
| 25 | 19150721 | const float vc = std::clamp(v, -88.0f, 88.0f); | |
| 26 | 19150721 | return v / (1.0f + std::exp(-vc)); | |
| 27 | } | ||
| 28 | |||
| 29 | 6943 | inline float silu_grad_scalar(float v) { | |
| 30 | // d/dx [x * sigmoid(x)] = sigmoid(x) * (1 + x * (1 - sigmoid(x))). | ||
| 31 | 6943 | const float vc = std::clamp(v, -88.0f, 88.0f); | |
| 32 | 6943 | const float s = 1.0f / (1.0f + std::exp(-vc)); | |
| 33 | 6943 | return s * (1.0f + vc * (1.0f - s)); | |
| 34 | } | ||
| 35 | |||
| 36 | 1089 | inline float gelu_tanh_scalar(float v) { | |
| 37 | // GELU with tanh approximation (matches PyTorch's approximate="tanh"). | ||
| 38 | 1089 | constexpr float kSqrt2OverPi = 0.7978845608f; | |
| 39 | 1089 | const float vc = std::clamp(v, -20.0f, 20.0f); | |
| 40 | 1089 | const float u = kSqrt2OverPi * (vc + 0.044715f * vc * vc * vc); | |
| 41 | 1089 | return 0.5f * v * (1.0f + std::tanh(u)); | |
| 42 | } | ||
| 43 | |||
| 44 | 543 | inline float gelu_tanh_grad_scalar(float v) { | |
| 45 | // Derivative of gelu_tanh_scalar w.r.t. v. | ||
| 46 | 543 | constexpr float kSqrt2OverPi = 0.7978845608f; | |
| 47 | 543 | const float vc = std::clamp(v, -20.0f, 20.0f); | |
| 48 | 543 | const float u = kSqrt2OverPi * (vc + 0.044715f * vc * vc * vc); | |
| 49 | 543 | const float t = std::tanh(u); | |
| 50 | 543 | const float dudx = kSqrt2OverPi * (1.0f + 3.0f * 0.044715f * vc * vc); | |
| 51 | 543 | return 0.5f * (1.0f + t) + 0.5f * vc * (1.0f - t * t) * dudx; | |
| 52 | } | ||
| 53 | |||
| 54 | 577 | inline float gelu_exact_scalar(float v) { | |
| 55 | // Exact GELU: 0.5 * x * (1 + erf(x / sqrt(2))). | ||
| 56 | 577 | constexpr float kInvSqrt2 = 0.70710678118654752440f; | |
| 57 | 577 | return 0.5f * v * (1.0f + std::erf(v * kInvSqrt2)); | |
| 58 | } | ||
| 59 | |||
| 60 | 543 | inline float gelu_exact_grad_scalar(float v) { | |
| 61 | // d/dx [0.5*x*(1+erf(x/√2))] = 0.5*(1+erf(x/√2)) + x*φ(x). | ||
| 62 | 543 | constexpr float kInvSqrt2 = 0.70710678118654752440f; | |
| 63 | 543 | constexpr float kInvSqrt2Pi = 0.39894228040143267794f; // 1/sqrt(2π) | |
| 64 | 543 | const float vc = std::clamp(v, -20.0f, 20.0f); | |
| 65 | 543 | const float cdf_term = 0.5f * (1.0f + std::erf(vc * kInvSqrt2)); | |
| 66 | 543 | const float pdf = kInvSqrt2Pi * std::exp(-0.5f * vc * vc); | |
| 67 | 543 | return cdf_term + vc * pdf; | |
| 68 | } | ||
| 69 | |||
| 70 | 577 | inline float quick_gelu_scalar(float v) { | |
| 71 | // OpenAI CLIP's QuickGELU: x * sigmoid(1.702 * x). | ||
| 72 | 577 | const float vc = std::clamp(v, -50.0f, 50.0f); | |
| 73 | 577 | return v / (1.0f + std::exp(-1.702f * vc)); | |
| 74 | } | ||
| 75 | |||
| 76 | 543 | inline float quick_gelu_grad_scalar(float v) { | |
| 77 | // d/dx [x * sigmoid(1.702*x)] = s + x * 1.702 * s * (1 - s). | ||
| 78 | 543 | const float vc = std::clamp(v, -50.0f, 50.0f); | |
| 79 | 543 | const float s = 1.0f / (1.0f + std::exp(-1.702f * vc)); | |
| 80 | 543 | return s + vc * 1.702f * s * (1.0f - s); | |
| 81 | } | ||
| 82 | |||
| 83 | } // namespace | ||
| 84 | |||
| 85 | // ─── silu ────────────────────────────────────────────────────────────────── | ||
| 86 | |||
| 87 | 590 | void silu_forward(const ::brotensor::Tensor& x, ::brotensor::Tensor& y) { | |
| 88 | 590 | if (y.rows != x.rows || y.cols != x.cols) y.resize(x.rows, x.cols); | |
| 89 | 590 | const int n = x.size(); | |
| 90 | 590 | if (n == 0) return; | |
| 91 | 590 | const float* xp = x.host_f32(); | |
| 92 | 590 | float* yp = y.host_f32_mut(); | |
| 93 | 19151311 | for (int i = 0; i < n; ++i) yp[i] = silu_scalar(xp[i]); | |
| 94 | 590 | } | |
| 95 | |||
| 96 | 20 | void silu_backward(const ::brotensor::Tensor& x, const ::brotensor::Tensor& dY, | |
| 97 | ::brotensor::Tensor& dX) { | ||
| 98 | 20 | if (dX.rows != x.rows || dX.cols != x.cols) dX.resize(x.rows, x.cols); | |
| 99 | 20 | const int n = x.size(); | |
| 100 | 20 | if (n == 0) return; | |
| 101 | 20 | const float* xp = x.host_f32(); | |
| 102 | 20 | const float* dyp = dY.host_f32(); | |
| 103 | 20 | float* dxp = dX.host_f32_mut(); | |
| 104 | 6963 | for (int i = 0; i < n; ++i) dxp[i] = dyp[i] * silu_grad_scalar(xp[i]); | |
| 105 | 20 | } | |
| 106 | |||
| 107 | // ─── gelu (tanh approximation) ───────────────────────────────────────────── | ||
| 108 | |||
| 109 | 5 | void gelu_forward(const ::brotensor::Tensor& x, ::brotensor::Tensor& y) { | |
| 110 | 5 | if (y.rows != x.rows || y.cols != x.cols) y.resize(x.rows, x.cols); | |
| 111 | 5 | const int n = x.size(); | |
| 112 | 5 | if (n == 0) return; | |
| 113 | 5 | const float* xp = x.host_f32(); | |
| 114 | 5 | float* yp = y.host_f32_mut(); | |
| 115 | 1094 | for (int i = 0; i < n; ++i) yp[i] = gelu_tanh_scalar(xp[i]); | |
| 116 | 5 | } | |
| 117 | |||
| 118 | 4 | void gelu_backward(const ::brotensor::Tensor& x, const ::brotensor::Tensor& dY, | |
| 119 | ::brotensor::Tensor& dX) { | ||
| 120 | 4 | if (dX.rows != x.rows || dX.cols != x.cols) dX.resize(x.rows, x.cols); | |
| 121 | 4 | const int n = x.size(); | |
| 122 | 4 | if (n == 0) return; | |
| 123 | 4 | const float* xp = x.host_f32(); | |
| 124 | 4 | const float* dyp = dY.host_f32(); | |
| 125 | 4 | float* dxp = dX.host_f32_mut(); | |
| 126 | 547 | for (int i = 0; i < n; ++i) dxp[i] = dyp[i] * gelu_tanh_grad_scalar(xp[i]); | |
| 127 | 4 | } | |
| 128 | |||
| 129 | // ─── gelu_exact (erf-based) ──────────────────────────────────────────────── | ||
| 130 | |||
| 131 | 4 | void gelu_exact_forward(const ::brotensor::Tensor& x, ::brotensor::Tensor& y) { | |
| 132 | 4 | if (y.rows != x.rows || y.cols != x.cols) y.resize(x.rows, x.cols); | |
| 133 | 4 | const int n = x.size(); | |
| 134 | 4 | if (n == 0) return; | |
| 135 | 4 | const float* xp = x.host_f32(); | |
| 136 | 4 | float* yp = y.host_f32_mut(); | |
| 137 | 581 | for (int i = 0; i < n; ++i) yp[i] = gelu_exact_scalar(xp[i]); | |
| 138 | 4 | } | |
| 139 | |||
| 140 | 4 | void gelu_exact_backward(const ::brotensor::Tensor& x, | |
| 141 | const ::brotensor::Tensor& dY, | ||
| 142 | ::brotensor::Tensor& dX) { | ||
| 143 | 4 | if (dX.rows != x.rows || dX.cols != x.cols) dX.resize(x.rows, x.cols); | |
| 144 | 4 | const int n = x.size(); | |
| 145 | 4 | if (n == 0) return; | |
| 146 | 4 | const float* xp = x.host_f32(); | |
| 147 | 4 | const float* dyp = dY.host_f32(); | |
| 148 | 4 | float* dxp = dX.host_f32_mut(); | |
| 149 | 547 | for (int i = 0; i < n; ++i) dxp[i] = dyp[i] * gelu_exact_grad_scalar(xp[i]); | |
| 150 | 4 | } | |
| 151 | |||
| 152 | // ─── quick_gelu ──────────────────────────────────────────────────────────── | ||
| 153 | |||
| 154 | 4 | void quick_gelu_forward(const ::brotensor::Tensor& x, ::brotensor::Tensor& y) { | |
| 155 | 4 | if (y.rows != x.rows || y.cols != x.cols) y.resize(x.rows, x.cols); | |
| 156 | 4 | const int n = x.size(); | |
| 157 | 4 | if (n == 0) return; | |
| 158 | 4 | const float* xp = x.host_f32(); | |
| 159 | 4 | float* yp = y.host_f32_mut(); | |
| 160 | 581 | for (int i = 0; i < n; ++i) yp[i] = quick_gelu_scalar(xp[i]); | |
| 161 | 4 | } | |
| 162 | |||
| 163 | 4 | void quick_gelu_backward(const ::brotensor::Tensor& x, | |
| 164 | const ::brotensor::Tensor& dY, | ||
| 165 | ::brotensor::Tensor& dX) { | ||
| 166 | 4 | if (dX.rows != x.rows || dX.cols != x.cols) dX.resize(x.rows, x.cols); | |
| 167 | 4 | const int n = x.size(); | |
| 168 | 4 | if (n == 0) return; | |
| 169 | 4 | const float* xp = x.host_f32(); | |
| 170 | 4 | const float* dyp = dY.host_f32(); | |
| 171 | 4 | float* dxp = dX.host_f32_mut(); | |
| 172 | 547 | for (int i = 0; i < n; ++i) dxp[i] = dyp[i] * quick_gelu_grad_scalar(xp[i]); | |
| 173 | 4 | } | |
| 174 | |||
| 175 | } // namespace brotensor::detail::cpu | ||
| 176 |