brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
250 / 261
95.8%
Branches:
134 / 208
64.4%
Functions:
7 / 7
100.0%

src/cpu/lstm.cpp
Line Branch Exec Source
1 // CPU implementation of the LSTM ops (ops/lstm.h): the inference recurrence,
2 // the training forward with per-step caching, and full back-propagation-
3 // through-time.
4 //
5 // PyTorch nn.LSTM weight layout: W_ih (4H,I), W_hh (4H,H), gate-row blocks in
6 // order [input, forget, cell, output]; gate g uses tanh, the rest sigmoid.
7 // CPU-resident, FP32 throughout.
8
9 #include <brotensor/tensor.h>
10
11 #include <cmath>
12 #include <stdexcept>
13 #include <string>
14 #include <vector>
15
16 namespace brotensor::detail::cpu {
17
18 namespace {
19
20 224472 inline float sigmoidf(float x) { return 1.0f / (1.0f + std::exp(-x)); }
21
22 11637 void require_cpu_f32(const ::brotensor::Tensor& t, const char* what) {
23
1/2
✓ Branch 0 taken 11637 times.
✗ Branch 1 not taken.
11637 if (t.device != ::brotensor::Device::CPU ||
24 11637 t.dtype != ::brotensor::Dtype::FP32) {
25
0/12
✗ 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.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ throw std::runtime_error(std::string("lstm: ") + what +
26 " must be CPU FP32");
27 }
28 11637 }
29
30 // An optional bias/state tensor: present iff non-null and non-empty.
31 4454 const float* opt_ptr(const ::brotensor::Tensor* t, const char* what,
32 int rows, int cols) {
33
3/4
✓ Branch 0 taken 4412 times.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4412 times.
4454 if (!t || t->empty()) return nullptr;
34 4412 require_cpu_f32(*t, what);
35
1/2
✓ Branch 0 taken 4412 times.
✗ Branch 1 not taken.
4412 if (t->rows != rows || t->cols != cols) {
36
0/12
✗ 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.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ throw std::runtime_error(std::string("lstm: ") + what + " has wrong shape");
37 }
38 4412 return t->host_f32();
39 4454 }
40
41 // One direction of lstm_recurrence: h_t into Y's columns [col0, col0 + H).
42 9 void lstm_recur_dir(const float* gates, const float* whh, int T, int H, bool reverse,
43 float* y, int ldy, int col0) {
44 9 const int G = 4 * H;
45 9 std::vector<float> c(static_cast<std::size_t>(H), 0.0f);
46
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 9 times.
122 for (int step = 0; step < T; ++step) {
47
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 61 times.
113 const int t = reverse ? T - 1 - step : step;
48
2/2
✓ Branch 0 taken 104 times.
✓ Branch 1 taken 9 times.
217 const float* hprev = step == 0 ? nullptr
49
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 56 times.
104 : y + static_cast<std::size_t>(reverse ? t + 1 : t - 1) * ldy + col0;
50 113 const float* g = gates + static_cast<std::size_t>(t) * G;
51 113 float* h = y + static_cast<std::size_t>(t) * ldy + col0;
52
2/2
✓ Branch 0 taken 19385 times.
✓ Branch 1 taken 113 times.
19498 for (int n = 0; n < H; ++n) {
53 19385 float z[4] = {g[n], g[H + n], g[2 * H + n], g[3 * H + n]};
54
2/2
✓ Branch 0 taken 577 times.
✓ Branch 1 taken 18808 times.
19385 if (hprev) {
55
2/2
✓ Branch 0 taken 75232 times.
✓ Branch 1 taken 18808 times.
94040 for (int q = 0; q < 4; ++q) {
56 75232 const float* w = whh + static_cast<std::size_t>(q * H + n) * H;
57 75232 float acc = 0.0f;
58
2/2
✓ Branch 0 taken 18892640 times.
✓ Branch 1 taken 75232 times.
18967872 for (int k = 0; k < H; ++k) acc += w[k] * hprev[k];
59 75232 z[q] += acc;
60 75232 }
61 18808 }
62
2/4
✓ Branch 0 taken 19385 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19385 times.
✗ Branch 3 not taken.
19385 c[n] = sigmoidf(z[1]) * c[n] + sigmoidf(z[0]) * std::tanh(z[2]);
63
1/2
✓ Branch 0 taken 19385 times.
✗ Branch 1 not taken.
19385 h[n] = sigmoidf(z[3]) * std::tanh(c[n]);
64 19385 }
65 113 }
66 9 }
67
68 } // namespace
69
70 5 void lstm_recurrence(const ::brotensor::Tensor& gates_f, const ::brotensor::Tensor& W_hh_f,
71 const ::brotensor::Tensor* gates_r, const ::brotensor::Tensor* W_hh_r,
72 ::brotensor::Tensor& Y) {
73 5 require_cpu_f32(gates_f, "gates");
74 5 require_cpu_f32(W_hh_f, "W_hh");
75
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 if (gates_r) {
76 4 require_cpu_f32(*gates_r, "gates_r");
77 4 require_cpu_f32(*W_hh_r, "W_hh_r");
78 4 }
79 5 const int T = gates_f.rows, H = W_hh_f.cols;
80 5 float* y = Y.host_f32_mut();
81 5 lstm_recur_dir(gates_f.host_f32(), W_hh_f.host_f32(), T, H, false, y, Y.cols, 0);
82
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 if (gates_r) lstm_recur_dir(gates_r->host_f32(), W_hh_r->host_f32(), T, H, true, y, Y.cols, H);
83 5 }
84
85 910 void lstm_forward_train(const ::brotensor::Tensor& X, const ::brotensor::Tensor& W_ih,
86 const ::brotensor::Tensor& W_hh,
87 const ::brotensor::Tensor* b_ih, const ::brotensor::Tensor* b_hh,
88 const ::brotensor::Tensor* h0, const ::brotensor::Tensor* c0,
89 int T, int B,
90 ::brotensor::Tensor& Y, ::brotensor::Tensor& gates,
91 ::brotensor::Tensor& C,
92 ::brotensor::Tensor* hT, ::brotensor::Tensor* cT) {
93 910 require_cpu_f32(X, "X");
94 910 require_cpu_f32(W_ih, "W_ih");
95 910 require_cpu_f32(W_hh, "W_hh");
96 910 const int H = W_hh.cols;
97 910 const int I = W_ih.cols;
98 910 const int G = 4 * H;
99
1/2
✓ Branch 0 taken 910 times.
✗ Branch 1 not taken.
910 if (T <= 0 || B <= 0)
100
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("lstm_forward_train: T and B must be > 0");
101
1/2
✓ Branch 0 taken 910 times.
✗ Branch 1 not taken.
910 if (W_ih.rows != G || W_hh.rows != G)
102
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("lstm_forward_train: W_ih/W_hh must have 4*H rows");
103
1/2
✓ Branch 0 taken 910 times.
✗ Branch 1 not taken.
910 if (X.rows != T * B || X.cols != I)
104
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("lstm_forward_train: X must be (T*B, I)");
105
106 910 const float* bih = opt_ptr(b_ih, "b_ih", G, 1);
107 910 const float* bhh = opt_ptr(b_hh, "b_hh", G, 1);
108 910 const float* h0p = opt_ptr(h0, "h0", B, H);
109 910 const float* c0p = opt_ptr(c0, "c0", B, H);
110
111 910 Y.resize(T * B, H);
112 910 gates.resize(T * B, G);
113 910 C.resize(T * B, H);
114
2/2
✓ Branch 0 taken 904 times.
✓ Branch 1 taken 6 times.
910 if (hT) hT->resize(B, H);
115
2/2
✓ Branch 0 taken 904 times.
✓ Branch 1 taken 6 times.
910 if (cT) cT->resize(B, H);
116
117 910 const float* xp = X.host_f32();
118 910 const float* wih = W_ih.host_f32();
119 910 const float* whh = W_hh.host_f32();
120 910 float* yp = Y.host_f32_mut();
121 910 float* gp = gates.host_f32_mut();
122 910 float* cp = C.host_f32_mut();
123
124
2/2
✓ Branch 0 taken 3711 times.
✓ Branch 1 taken 910 times.
4621 for (int t = 0; t < T; ++t) {
125
2/2
✓ Branch 0 taken 7313 times.
✓ Branch 1 taken 3711 times.
11024 for (int b = 0; b < B; ++b) {
126 7313 const std::size_t row = static_cast<std::size_t>(t) * B + b;
127 7313 const float* x = xp + row * I;
128
4/4
✓ Branch 0 taken 5502 times.
✓ Branch 1 taken 1811 times.
✓ Branch 2 taken 1796 times.
✓ Branch 3 taken 15 times.
7313 const float* hprev = (t == 0) ? (h0p ? h0p + static_cast<std::size_t>(b) * H : nullptr)
129 5502 : yp + (static_cast<std::size_t>(t - 1) * B + b) * H;
130
4/4
✓ Branch 0 taken 5502 times.
✓ Branch 1 taken 1811 times.
✓ Branch 2 taken 1796 times.
✓ Branch 3 taken 15 times.
7313 const float* cprev = (t == 0) ? (c0p ? c0p + static_cast<std::size_t>(b) * H : nullptr)
131 5502 : cp + (static_cast<std::size_t>(t - 1) * B + b) * H;
132 7313 float* grow = gp + row * G; // post-activation [i|f|g|o]
133 7313 float* crow = cp + row * H;
134 7313 float* hrow = yp + row * H;
135
136
2/2
✓ Branch 0 taken 55439 times.
✓ Branch 1 taken 7313 times.
62752 for (int n = 0; n < H; ++n) {
137
2/2
✓ Branch 0 taken 55255 times.
✓ Branch 1 taken 184 times.
55439 float zi = bih ? bih[n] : 0.0f;
138
2/2
✓ Branch 0 taken 55255 times.
✓ Branch 1 taken 184 times.
55439 float zf = bih ? bih[H + n] : 0.0f;
139
2/2
✓ Branch 0 taken 55255 times.
✓ Branch 1 taken 184 times.
55439 float zg = bih ? bih[2 * H + n] : 0.0f;
140
2/2
✓ Branch 0 taken 55255 times.
✓ Branch 1 taken 184 times.
55439 float zo = bih ? bih[3 * H + n] : 0.0f;
141
2/2
✓ Branch 0 taken 184 times.
✓ Branch 1 taken 55255 times.
55439 if (bhh) { zi += bhh[n]; zf += bhh[H + n]; zg += bhh[2 * H + n]; zo += bhh[3 * H + n]; }
142
143 55439 const float* wi = wih + static_cast<std::size_t>(n) * I;
144 55439 const float* wf = wih + static_cast<std::size_t>(H + n) * I;
145 55439 const float* wg = wih + static_cast<std::size_t>(2 * H + n) * I;
146 55439 const float* wo = wih + static_cast<std::size_t>(3 * H + n) * I;
147
2/2
✓ Branch 0 taken 12235852 times.
✓ Branch 1 taken 55439 times.
12291291 for (int j = 0; j < I; ++j) {
148 12235852 const float xj = x[j];
149 12235852 zi += wi[j] * xj; zf += wf[j] * xj; zg += wg[j] * xj; zo += wo[j] * xj;
150 12235852 }
151
2/2
✓ Branch 0 taken 605 times.
✓ Branch 1 taken 54834 times.
55439 if (hprev) {
152 54834 const float* ui = whh + static_cast<std::size_t>(n) * H;
153 54834 const float* uf = whh + static_cast<std::size_t>(H + n) * H;
154 54834 const float* ug = whh + static_cast<std::size_t>(2 * H + n) * H;
155 54834 const float* uo = whh + static_cast<std::size_t>(3 * H + n) * H;
156
2/2
✓ Branch 0 taken 4903694 times.
✓ Branch 1 taken 54834 times.
4958528 for (int m = 0; m < H; ++m) {
157 4903694 const float hm = hprev[m];
158 4903694 zi += ui[m] * hm; zf += uf[m] * hm; zg += ug[m] * hm; zo += uo[m] * hm;
159 4903694 }
160 54834 }
161
162 55439 const float ig = sigmoidf(zi);
163 55439 const float fg = sigmoidf(zf);
164 55439 const float gg = std::tanh(zg);
165 55439 const float og = sigmoidf(zo);
166
2/2
✓ Branch 0 taken 54834 times.
✓ Branch 1 taken 605 times.
55439 const float cprev_n = cprev ? cprev[n] : 0.0f;
167 55439 const float cn = fg * cprev_n + ig * gg;
168
169 55439 grow[n] = ig; grow[H + n] = fg; grow[2 * H + n] = gg; grow[3 * H + n] = og;
170 55439 crow[n] = cn;
171 55439 hrow[n] = og * std::tanh(cn);
172 55439 }
173 7313 }
174 3711 }
175
176
2/2
✓ Branch 0 taken 904 times.
✓ Branch 1 taken 6 times.
910 if (hT) {
177 6 float* p = hT->host_f32_mut();
178
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (int b = 0; b < B; ++b)
179
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 12 times.
77 for (int n = 0; n < H; ++n)
180 65 p[static_cast<std::size_t>(b) * H + n] =
181 77 yp[(static_cast<std::size_t>(T - 1) * B + b) * H + n];
182 6 }
183
2/2
✓ Branch 0 taken 904 times.
✓ Branch 1 taken 6 times.
910 if (cT) {
184 6 float* p = cT->host_f32_mut();
185
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
18 for (int b = 0; b < B; ++b)
186
2/2
✓ Branch 0 taken 65 times.
✓ Branch 1 taken 12 times.
77 for (int n = 0; n < H; ++n)
187 65 p[static_cast<std::size_t>(b) * H + n] =
188 77 cp[(static_cast<std::size_t>(T - 1) * B + b) * H + n];
189 6 }
190 910 }
191
192 407 void lstm_backward(const ::brotensor::Tensor& X, const ::brotensor::Tensor& W_ih,
193 const ::brotensor::Tensor& W_hh,
194 const ::brotensor::Tensor* h0, const ::brotensor::Tensor* c0,
195 const ::brotensor::Tensor& Y, const ::brotensor::Tensor& gates,
196 const ::brotensor::Tensor& C,
197 const ::brotensor::Tensor& dY, int T, int B,
198 ::brotensor::Tensor& dX, ::brotensor::Tensor& dW_ih,
199 ::brotensor::Tensor& dW_hh,
200 ::brotensor::Tensor* db_ih, ::brotensor::Tensor* db_hh,
201 ::brotensor::Tensor* dh0, ::brotensor::Tensor* dc0) {
202 407 require_cpu_f32(X, "X");
203 407 require_cpu_f32(W_ih, "W_ih");
204 407 require_cpu_f32(W_hh, "W_hh");
205 407 require_cpu_f32(Y, "Y");
206 407 require_cpu_f32(gates, "gates");
207 407 require_cpu_f32(C, "C");
208 407 require_cpu_f32(dY, "dY");
209 407 const int H = W_hh.cols;
210 407 const int I = W_ih.cols;
211 407 const int G = 4 * H;
212
1/2
✓ Branch 0 taken 407 times.
✗ Branch 1 not taken.
407 if (T <= 0 || B <= 0)
213
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("lstm_backward: T and B must be > 0");
214
1/2
✓ Branch 0 taken 407 times.
✗ Branch 1 not taken.
407 if (W_ih.rows != G || W_hh.rows != G)
215
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("lstm_backward: W_ih/W_hh must have 4*H rows");
216
1/2
✓ Branch 0 taken 407 times.
✗ Branch 1 not taken.
407 if (X.rows != T * B || X.cols != I)
217
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("lstm_backward: X must be (T*B, I)");
218
1/2
✓ Branch 0 taken 407 times.
✗ Branch 1 not taken.
407 if (dY.rows != T * B || dY.cols != H)
219
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("lstm_backward: dY must be (T*B, H)");
220
221 // Parameter grads accumulate — require pre-sized buffers, do NOT zero them.
222 407 require_cpu_f32(dW_ih, "dW_ih");
223 407 require_cpu_f32(dW_hh, "dW_hh");
224
1/2
✓ Branch 0 taken 407 times.
✗ Branch 1 not taken.
407 if (dW_ih.rows != G || dW_ih.cols != I)
225
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("lstm_backward: dW_ih must be (4H, I), zeroed");
226
1/2
✓ Branch 0 taken 407 times.
✗ Branch 1 not taken.
407 if (dW_hh.rows != G || dW_hh.cols != H)
227
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("lstm_backward: dW_hh must be (4H, H), zeroed");
228
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 407 times.
✓ Branch 2 taken 407 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
407 if (db_ih) { require_cpu_f32(*db_ih, "db_ih"); if (db_ih->rows != G || db_ih->cols != 1) throw std::runtime_error("lstm_backward: db_ih must be (4H, 1), zeroed"); }
229
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 407 times.
✓ Branch 2 taken 407 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
407 if (db_hh) { require_cpu_f32(*db_hh, "db_hh"); if (db_hh->rows != G || db_hh->cols != 1) throw std::runtime_error("lstm_backward: db_hh must be (4H, 1), zeroed"); }
230
231 407 const float* h0p = opt_ptr(h0, "h0", B, H);
232 407 const float* c0p = opt_ptr(c0, "c0", B, H);
233
234 407 dX.resize(T * B, I);
235
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 7 times.
407 if (dh0) dh0->resize(B, H);
236
2/2
✓ Branch 0 taken 400 times.
✓ Branch 1 taken 7 times.
407 if (dc0) dc0->resize(B, H);
237
238 407 const float* xp = X.host_f32();
239 407 const float* yp = Y.host_f32();
240 407 const float* gp = gates.host_f32();
241 407 const float* cp = C.host_f32();
242 407 const float* dyp = dY.host_f32();
243 407 const float* wih = W_ih.host_f32();
244 407 const float* whh = W_hh.host_f32();
245 407 float* dxp = dX.host_f32_mut();
246 407 float* dwih = dW_ih.host_f32_mut();
247 407 float* dwhh = dW_hh.host_f32_mut();
248
1/2
✓ Branch 0 taken 407 times.
✗ Branch 1 not taken.
407 float* dbih = db_ih ? db_ih->host_f32_mut() : nullptr;
249
1/2
✓ Branch 0 taken 407 times.
✗ Branch 1 not taken.
407 float* dbhh = db_hh ? db_hh->host_f32_mut() : nullptr;
250
251 407 std::fill(dxp, dxp + static_cast<std::size_t>(T) * B * I, 0.0f);
252
253 // Recurrent grad carried backward in time: dL/dh_{t} and dL/dc_{t} arriving
254 // from step t+1. After the loop these hold dL/dh0 and dL/dc0.
255 407 std::vector<float> dh_next(static_cast<std::size_t>(B) * H, 0.0f);
256
1/2
✓ Branch 0 taken 407 times.
✗ Branch 1 not taken.
407 std::vector<float> dc_next(static_cast<std::size_t>(B) * H, 0.0f);
257
1/2
✓ Branch 0 taken 407 times.
✗ Branch 1 not taken.
407 std::vector<float> dh_new(static_cast<std::size_t>(H));
258
259
2/2
✓ Branch 0 taken 1625 times.
✓ Branch 1 taken 407 times.
2032 for (int t = T - 1; t >= 0; --t) {
260
2/2
✓ Branch 0 taken 1625 times.
✓ Branch 1 taken 3252 times.
4877 for (int b = 0; b < B; ++b) {
261 3252 const std::size_t row = static_cast<std::size_t>(t) * B + b;
262 3252 const float* grow = gp + row * G;
263 3252 const float* crow = cp + row * H;
264 3252 const float* x = xp + row * I;
265 3252 const float* dyrow = dyp + row * H;
266
4/4
✓ Branch 0 taken 2438 times.
✓ Branch 1 taken 814 times.
✓ Branch 2 taken 811 times.
✓ Branch 3 taken 3 times.
3252 const float* hprev = (t == 0) ? (h0p ? h0p + static_cast<std::size_t>(b) * H : nullptr)
267 2438 : yp + (static_cast<std::size_t>(t - 1) * B + b) * H;
268
4/4
✓ Branch 0 taken 2438 times.
✓ Branch 1 taken 814 times.
✓ Branch 2 taken 811 times.
✓ Branch 3 taken 3 times.
3252 const float* cprev = (t == 0) ? (c0p ? c0p + static_cast<std::size_t>(b) * H : nullptr)
269 2438 : cp + (static_cast<std::size_t>(t - 1) * B + b) * H;
270 3252 float* dxrow = dxp + row * I;
271 3252 float* dh_b = dh_next.data() + static_cast<std::size_t>(b) * H;
272 3252 float* dc_b = dc_next.data() + static_cast<std::size_t>(b) * H;
273
274
1/2
✓ Branch 0 taken 3252 times.
✗ Branch 1 not taken.
3252 std::fill(dh_new.begin(), dh_new.end(), 0.0f);
275
276
2/2
✓ Branch 0 taken 16287 times.
✓ Branch 1 taken 3252 times.
19539 for (int n = 0; n < H; ++n) {
277 16287 const float ig = grow[n], fg = grow[H + n], gg = grow[2 * H + n], og = grow[3 * H + n];
278 16287 const float tc = std::tanh(crow[n]);
279 16287 const float dh = dyrow[n] + dh_b[n];
280 16287 const float do_ = dh * tc;
281 16287 const float dc = dh * og * (1.0f - tc * tc) + dc_b[n];
282
2/2
✓ Branch 0 taken 16273 times.
✓ Branch 1 taken 14 times.
16287 const float cprev_n = cprev ? cprev[n] : 0.0f;
283 16287 const float df = dc * cprev_n;
284 16287 const float di = dc * gg;
285 16287 const float dg = dc * ig;
286 16287 const float dcprev = dc * fg;
287
288 // Gate pre-activation grads (sigmoid'/tanh' on cached outputs).
289 16287 const float dzi = di * ig * (1.0f - ig);
290 16287 const float dzf = df * fg * (1.0f - fg);
291 16287 const float dzg = dg * (1.0f - gg * gg);
292 16287 const float dzo = do_ * og * (1.0f - og);
293
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16287 times.
16287 if (dbih) { dbih[n] += dzi; dbih[H + n] += dzf; dbih[2 * H + n] += dzg; dbih[3 * H + n] += dzo; }
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16287 times.
16287 if (dbhh) { dbhh[n] += dzi; dbhh[H + n] += dzf; dbhh[2 * H + n] += dzg; dbhh[3 * H + n] += dzo; }
296
297 // Input path: dX = W_ih^T·dz ; dW_ih += dz·x^T
298 16287 const float* wi = wih + static_cast<std::size_t>(n) * I;
299 16287 const float* wf = wih + static_cast<std::size_t>(H + n) * I;
300 16287 const float* wg = wih + static_cast<std::size_t>(2 * H + n) * I;
301 16287 const float* wo = wih + static_cast<std::size_t>(3 * H + n) * I;
302 16287 float* dwi = dwih + static_cast<std::size_t>(n) * I;
303 16287 float* dwf = dwih + static_cast<std::size_t>(H + n) * I;
304 16287 float* dwg = dwih + static_cast<std::size_t>(2 * H + n) * I;
305 16287 float* dwo = dwih + static_cast<std::size_t>(3 * H + n) * I;
306
2/2
✓ Branch 0 taken 49398 times.
✓ Branch 1 taken 16287 times.
65685 for (int j = 0; j < I; ++j) {
307 49398 dxrow[j] += wi[j] * dzi + wf[j] * dzf + wg[j] * dzg + wo[j] * dzo;
308 49398 const float xj = x[j];
309 49398 dwi[j] += dzi * xj; dwf[j] += dzf * xj; dwg[j] += dzg * xj; dwo[j] += dzo * xj;
310 49398 }
311
312 // Recurrent path: dh_prev = W_hh^T·dz ; dW_hh += dz·h_prev^T
313 16287 const float* ui = whh + static_cast<std::size_t>(n) * H;
314 16287 const float* uf = whh + static_cast<std::size_t>(H + n) * H;
315 16287 const float* ug = whh + static_cast<std::size_t>(2 * H + n) * H;
316 16287 const float* uo = whh + static_cast<std::size_t>(3 * H + n) * H;
317 16287 float* dui = dwhh + static_cast<std::size_t>(n) * H;
318 16287 float* duf = dwhh + static_cast<std::size_t>(H + n) * H;
319 16287 float* dug = dwhh + static_cast<std::size_t>(2 * H + n) * H;
320 16287 float* duo = dwhh + static_cast<std::size_t>(3 * H + n) * H;
321
2/2
✓ Branch 0 taken 81633 times.
✓ Branch 1 taken 16287 times.
97920 for (int m = 0; m < H; ++m) {
322 81633 dh_new[m] += ui[m] * dzi + uf[m] * dzf + ug[m] * dzg + uo[m] * dzo;
323
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 81567 times.
81633 if (hprev) {
324 81567 const float hm = hprev[m];
325 81567 dui[m] += dzi * hm; duf[m] += dzf * hm; dug[m] += dzg * hm; duo[m] += dzo * hm;
326 81567 }
327 81633 }
328
329 // dL/dc_{t-1} for this unit (cell carry is per-unit, no coupling).
330 16287 dc_b[n] = dcprev;
331 16287 }
332
333 // Swap in the freshly accumulated dL/dh_{t-1} for the earlier step.
334
2/2
✓ Branch 0 taken 16287 times.
✓ Branch 1 taken 3252 times.
19539 for (int m = 0; m < H; ++m) dh_b[m] = dh_new[m];
335 3252 }
336 1625 }
337
338
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 400 times.
407 if (dh0) {
339
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 float* p = dh0->host_f32_mut();
340
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 7 times.
82 for (std::size_t i = 0; i < dh_next.size(); ++i) p[i] = dh_next[i];
341 7 }
342
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 400 times.
407 if (dc0) {
343
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 float* p = dc0->host_f32_mut();
344
2/2
✓ Branch 0 taken 75 times.
✓ Branch 1 taken 7 times.
82 for (std::size_t i = 0; i < dc_next.size(); ++i) p[i] = dc_next[i];
345 7 }
346 407 }
347
348 } // namespace brotensor::detail::cpu
349