brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
78 / 80
97.5%
Branches:
16 / 44
36.4%
Functions:
10 / 10
100.0%

src/cpu/geglu.cpp
Line Branch Exec Source
1 // ─── CPU GEGLU ops (CHUNK 2) ───────────────────────────────────────────────
2 //
3 // FP32 scalar host implementations. Ports the GEGLU kernels in
4 // src/cuda/elementwise.cu — kernel math reproduced verbatim, FP32 path only.
5 //
6 // X is (B, 2D); split along the last dim into A = X[:, :D] (the value half)
7 // and B_half = X[:, D:] (the gate half).
8 // forward: Y(B, D) = A * gelu(B_half)
9 // backward: dX(B,2D): dX[:, :D] = dY * gelu(B_half)
10 // dX[:, D:] = dY * A * gelu'(B_half)
11 //
12 // Two variants: tanh-approximation GELU and exact (erf-based) GELU. Backward
13 // overwrites dX (the GPU kernel writes both halves directly).
14
15 #include <brotensor/tensor.h>
16
17 #include <algorithm>
18 #include <cmath>
19 #include <stdexcept>
20
21 namespace brotensor::detail::cpu {
22
23 namespace {
24
25 583 inline float gelu_tanh_scalar(float v) {
26 583 constexpr float kSqrt2OverPi = 0.7978845608f;
27 583 const float vc = std::clamp(v, -20.0f, 20.0f);
28 583 const float u = kSqrt2OverPi * (vc + 0.044715f * vc * vc * vc);
29 583 return 0.5f * v * (1.0f + std::tanh(u));
30 }
31
32 // Backward needs both gelu(v) and gelu'(v) for the same v; both are
33 // algebraically derivable from a single std::tanh evaluation, so compute it
34 // once here instead of calling gelu_tanh_scalar plus a separate gradient
35 // function (which would redo the tanh).
36 543 inline void gelu_tanh_value_grad(float v, float& g, float& gprime) {
37 543 constexpr float kSqrt2OverPi = 0.7978845608f;
38 543 const float vc = std::clamp(v, -20.0f, 20.0f);
39 543 const float u = kSqrt2OverPi * (vc + 0.044715f * vc * vc * vc);
40 543 const float t = std::tanh(u);
41 543 const float dudx = kSqrt2OverPi * (1.0f + 3.0f * 0.044715f * vc * vc);
42 543 g = 0.5f * v * (1.0f + t);
43 543 gprime = 0.5f * (1.0f + t) + 0.5f * vc * (1.0f - t * t) * dudx;
44 543 }
45
46 548 inline float gelu_exact_scalar(float v) {
47 548 constexpr float kInvSqrt2 = 0.70710678118654752440f;
48 548 return 0.5f * v * (1.0f + std::erf(v * kInvSqrt2));
49 }
50
51 // Backward needs both gelu(v) and gelu'(v) for the same v; both share the
52 // same std::erf evaluation (cdf_term), so compute it once here instead of
53 // calling gelu_exact_scalar plus a separate gradient function.
54 543 inline void gelu_exact_value_grad(float v, float& g, float& gprime) {
55 543 constexpr float kInvSqrt2 = 0.70710678118654752440f;
56 543 constexpr float kInvSqrt2Pi = 0.39894228040143267794f;
57 543 const float vc = std::clamp(v, -20.0f, 20.0f);
58 543 const float cdf_term = 0.5f * (1.0f + std::erf(vc * kInvSqrt2));
59 543 g = v * cdf_term;
60 543 const float pdf = kInvSqrt2Pi * std::exp(-0.5f * vc * vc);
61 543 gprime = cdf_term + vc * pdf;
62 543 }
63
64 template <typename GeluFn>
65 9 void geglu_forward_impl(const ::brotensor::Tensor& X, ::brotensor::Tensor& Y,
66 const char* op, GeluFn gelu) {
67
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (X.cols % 2 != 0) {
68
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ throw std::runtime_error(std::string(op) + ": X.cols must be even (2*D)");
69 }
70 9 const int B = X.rows;
71 9 const int D = X.cols / 2;
72
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
9 if (Y.rows != B || Y.cols != D) Y.resize(B, D);
73 9 const int total = B * D;
74
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (total == 0) return;
75 9 const float* Xp = X.host_f32();
76 9 float* Yp = Y.host_f32_mut();
77 9 const int two_d = 2 * D;
78
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 9 times.
58 for (int b = 0; b < B; ++b) {
79
2/2
✓ Branch 0 taken 1131 times.
✓ Branch 1 taken 49 times.
1180 for (int d = 0; d < D; ++d) {
80 1131 const float a = Xp[b * two_d + d];
81 1131 const float gv_raw = Xp[b * two_d + D + d];
82 1131 Yp[b * D + d] = a * gelu(gv_raw);
83 1131 }
84 49 }
85 9 }
86
87 template <typename GeluValueGradFn>
88 8 void geglu_backward_impl(const ::brotensor::Tensor& X,
89 const ::brotensor::Tensor& dY,
90 ::brotensor::Tensor& dX,
91 const char* op, GeluValueGradFn gelu_value_grad) {
92
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (X.cols % 2 != 0) {
93
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ throw std::runtime_error(std::string(op) + ": X.cols must be even (2*D)");
94 }
95 8 const int B = X.rows;
96 8 const int D = X.cols / 2;
97
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
8 if (dX.rows != B || dX.cols != 2 * D) dX.resize(B, 2 * D);
98 8 const int total = B * D;
99
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (total == 0) return;
100 8 const float* Xp = X.host_f32();
101 8 const float* dYp = dY.host_f32();
102 8 float* dXp = dX.host_f32_mut();
103 8 const int two_d = 2 * D;
104
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 8 times.
44 for (int b = 0; b < B; ++b) {
105
2/2
✓ Branch 0 taken 1086 times.
✓ Branch 1 taken 36 times.
1122 for (int d = 0; d < D; ++d) {
106 1086 const float a = Xp[b * two_d + d];
107 1086 const float bh = Xp[b * two_d + D + d];
108 1086 const float dy = dYp[b * D + d];
109 float g, gprime;
110 1086 gelu_value_grad(bh, g, gprime);
111 1086 dXp[b * two_d + d] = dy * g;
112 1086 dXp[b * two_d + D + d] = dy * a * gprime;
113 1086 }
114 36 }
115 8 }
116
117 } // namespace
118
119 5 void geglu_forward(const ::brotensor::Tensor& X, ::brotensor::Tensor& Y) {
120 5 geglu_forward_impl(X, Y, "geglu_forward", gelu_tanh_scalar);
121 5 }
122
123 4 void geglu_backward(const ::brotensor::Tensor& X, const ::brotensor::Tensor& dY,
124 ::brotensor::Tensor& dX) {
125 4 geglu_backward_impl(X, dY, dX, "geglu_backward", gelu_tanh_value_grad);
126 4 }
127
128 4 void geglu_exact_forward(const ::brotensor::Tensor& X, ::brotensor::Tensor& Y) {
129 4 geglu_forward_impl(X, Y, "geglu_exact_forward", gelu_exact_scalar);
130 4 }
131
132 4 void geglu_exact_backward(const ::brotensor::Tensor& X,
133 const ::brotensor::Tensor& dY,
134 ::brotensor::Tensor& dX) {
135 4 geglu_backward_impl(X, dY, dX, "geglu_exact_backward", gelu_exact_value_grad);
136 4 }
137
138 } // namespace brotensor::detail::cpu
139