src/cpu/swiglu.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ─── CPU SwiGLU ops (CHUNK 2) ────────────────────────────────────────────── | ||
| 2 | // | ||
| 3 | // FP32 scalar host implementations. Ports src/cuda/swiglu.cu — kernel math | ||
| 4 | // reproduced verbatim, FP32 path only. | ||
| 5 | // | ||
| 6 | // X is (B, 2D); split along the last dim into A = X[:, :D] (the value half | ||
| 7 | // that is gated through silu) and B_half = X[:, D:] (the linear half). | ||
| 8 | // forward: Y(B, D) = silu(A) * B_half | ||
| 9 | // backward: dX(B,2D): dX[:, :D] = dY * B_half * silu'(A) | ||
| 10 | // dX[:, D:] = dY * silu(A) | ||
| 11 | // | ||
| 12 | // Backward overwrites dX (the GPU kernel writes both halves directly). | ||
| 13 | |||
| 14 | #include <brotensor/tensor.h> | ||
| 15 | #include "cpu_jit.h" | ||
| 16 | |||
| 17 | #include <algorithm> | ||
| 18 | #include <cmath> | ||
| 19 | #include <stdexcept> | ||
| 20 | |||
| 21 | namespace brotensor::detail::cpu { | ||
| 22 | |||
| 23 | namespace { | ||
| 24 | |||
| 25 | ✗ | inline float silu_scalar(float v) { | |
| 26 | ✗ | const float vc = std::clamp(v, -88.0f, 88.0f); | |
| 27 | ✗ | return v / (1.0f + std::exp(-vc)); | |
| 28 | } | ||
| 29 | |||
| 30 | // Backward needs both silu(v) and silu'(v) for the same v; both derive from | ||
| 31 | // a single sigmoid evaluation (which itself needs one std::exp), so compute | ||
| 32 | // it once here instead of calling silu_scalar plus a separate gradient | ||
| 33 | // function (which would redo the std::exp). | ||
| 34 | 613 | inline void silu_value_grad(float v, float& s_out, float& sprime_out) { | |
| 35 | 613 | const float vc = std::clamp(v, -88.0f, 88.0f); | |
| 36 | 613 | const float s = 1.0f / (1.0f + std::exp(-vc)); | |
| 37 | 613 | s_out = v * s; | |
| 38 | 613 | sprime_out = s * (1.0f + vc * (1.0f - s)); | |
| 39 | 613 | } | |
| 40 | |||
| 41 | } // namespace | ||
| 42 | |||
| 43 | 367 | void swiglu_forward(const ::brotensor::Tensor& X, ::brotensor::Tensor& Y) { | |
| 44 | 367 | if (X.cols % 2 != 0) { | |
| 45 | ✗ | throw std::runtime_error("swiglu_forward: X.cols must be even (2*D)"); | |
| 46 | } | ||
| 47 | 367 | const int B = X.rows; | |
| 48 | 367 | const int D = X.cols / 2; | |
| 49 | 367 | if (Y.rows != B || Y.cols != D) Y.resize(B, D); | |
| 50 | 367 | const int total = B * D; | |
| 51 | 367 | if (total == 0) return; | |
| 52 | 367 | const float* Xp = X.host_f32(); | |
| 53 | 367 | float* Yp = Y.host_f32_mut(); | |
| 54 | |||
| 55 | 367 | if (jit::is_jit_available()) { | |
| 56 | 367 | jit::swiglu_forward(Xp, Yp, B, D); | |
| 57 | 367 | return; | |
| 58 | } | ||
| 59 | ✗ | const int two_d = 2 * D; | |
| 60 | ✗ | for (int b = 0; b < B; ++b) { | |
| 61 | ✗ | for (int d = 0; d < D; ++d) { | |
| 62 | ✗ | const float a = Xp[b * two_d + d]; | |
| 63 | ✗ | const float bh = Xp[b * two_d + D + d]; | |
| 64 | ✗ | Yp[b * D + d] = silu_scalar(a) * bh; | |
| 65 | ✗ | } | |
| 66 | ✗ | } | |
| 67 | 367 | } | |
| 68 | |||
| 69 | 6 | void swiglu_backward(const ::brotensor::Tensor& X, const ::brotensor::Tensor& dY, | |
| 70 | ::brotensor::Tensor& dX) { | ||
| 71 | 6 | if (X.cols % 2 != 0) { | |
| 72 | ✗ | throw std::runtime_error("swiglu_backward: X.cols must be even (2*D)"); | |
| 73 | } | ||
| 74 | 6 | const int B = X.rows; | |
| 75 | 6 | const int D = X.cols / 2; | |
| 76 | 6 | if (dX.rows != B || dX.cols != 2 * D) dX.resize(B, 2 * D); | |
| 77 | 6 | const int total = B * D; | |
| 78 | 6 | if (total == 0) return; | |
| 79 | 6 | const float* Xp = X.host_f32(); | |
| 80 | 6 | const float* dYp = dY.host_f32(); | |
| 81 | 6 | float* dXp = dX.host_f32_mut(); | |
| 82 | 6 | const int two_d = 2 * D; | |
| 83 | 34 | for (int b = 0; b < B; ++b) { | |
| 84 | 641 | for (int d = 0; d < D; ++d) { | |
| 85 | 613 | const float a = Xp[b * two_d + d]; | |
| 86 | 613 | const float bh = Xp[b * two_d + D + d]; | |
| 87 | 613 | const float dy = dYp[b * D + d]; | |
| 88 | float s, sp; | ||
| 89 | 613 | silu_value_grad(a, s, sp); | |
| 90 | 613 | dXp[b * two_d + d] = dy * bh * sp; | |
| 91 | 613 | dXp[b * two_d + D + d] = dy * s; | |
| 92 | 613 | } | |
| 93 | 28 | } | |
| 94 | 6 | } | |
| 95 | |||
| 96 | } // namespace brotensor::detail::cpu | ||
| 97 |