brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
104 / 104
100.0%
Branches:
42 / 64
65.6%
Functions:
16 / 16
100.0%

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
3/4
✓ Branch 0 taken 569 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 569 times.
590 if (y.rows != x.rows || y.cols != x.cols) y.resize(x.rows, x.cols);
89 590 const int n = x.size();
90
1/2
✓ Branch 0 taken 590 times.
✗ Branch 1 not taken.
590 if (n == 0) return;
91 590 const float* xp = x.host_f32();
92 590 float* yp = y.host_f32_mut();
93
2/2
✓ Branch 0 taken 19150721 times.
✓ Branch 1 taken 590 times.
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
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
20 if (dX.rows != x.rows || dX.cols != x.cols) dX.resize(x.rows, x.cols);
99 20 const int n = x.size();
100
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
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
2/2
✓ Branch 0 taken 6943 times.
✓ Branch 1 taken 20 times.
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
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
5 if (y.rows != x.rows || y.cols != x.cols) y.resize(x.rows, x.cols);
111 5 const int n = x.size();
112
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (n == 0) return;
113 5 const float* xp = x.host_f32();
114 5 float* yp = y.host_f32_mut();
115
2/2
✓ Branch 0 taken 1089 times.
✓ Branch 1 taken 5 times.
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
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
4 if (dX.rows != x.rows || dX.cols != x.cols) dX.resize(x.rows, x.cols);
121 4 const int n = x.size();
122
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
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
2/2
✓ Branch 0 taken 543 times.
✓ Branch 1 taken 4 times.
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
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (y.rows != x.rows || y.cols != x.cols) y.resize(x.rows, x.cols);
133 4 const int n = x.size();
134
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (n == 0) return;
135 4 const float* xp = x.host_f32();
136 4 float* yp = y.host_f32_mut();
137
2/2
✓ Branch 0 taken 577 times.
✓ Branch 1 taken 4 times.
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
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
4 if (dX.rows != x.rows || dX.cols != x.cols) dX.resize(x.rows, x.cols);
144 4 const int n = x.size();
145
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
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
2/2
✓ Branch 0 taken 543 times.
✓ Branch 1 taken 4 times.
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
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (y.rows != x.rows || y.cols != x.cols) y.resize(x.rows, x.cols);
156 4 const int n = x.size();
157
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (n == 0) return;
158 4 const float* xp = x.host_f32();
159 4 float* yp = y.host_f32_mut();
160
2/2
✓ Branch 0 taken 577 times.
✓ Branch 1 taken 4 times.
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
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
4 if (dX.rows != x.rows || dX.cols != x.cols) dX.resize(x.rows, x.cols);
167 4 const int n = x.size();
168
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
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
2/2
✓ Branch 0 taken 543 times.
✓ Branch 1 taken 4 times.
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