src/cpu/ops_impl.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ─── CPU op implementations ──────────────────────────────────────────────── | ||
| 2 | // | ||
| 3 | // Plain FP32 host loops. Ports the core ops formerly in src/cpu/ops.cpp to | ||
| 4 | // the unified `brotensor::Tensor` type. (MHA, batched ops, and masked mean-pool | ||
| 5 | // live in src/cpu/ops_mha_batched.cpp.) | ||
| 6 | |||
| 7 | #include <brotensor/tensor.h> | ||
| 8 | #include <brotensor/detail/cpu/thread_pool.h> | ||
| 9 | |||
| 10 | #include <algorithm> | ||
| 11 | #include <cassert> | ||
| 12 | #include <cmath> | ||
| 13 | #include <cstddef> | ||
| 14 | #include <cstdint> | ||
| 15 | #include <cstring> | ||
| 16 | #include <stdexcept> | ||
| 17 | #include <vector> | ||
| 18 | |||
| 19 | namespace brotensor::detail::cpu { | ||
| 20 | |||
| 21 | // ─── Dense layer ─────────────────────────────────────────────────────────── | ||
| 22 | |||
| 23 | 881 | void linear_forward(const ::brotensor::Tensor& W, const ::brotensor::Tensor& b, | |
| 24 | const ::brotensor::Tensor& x, ::brotensor::Tensor& y) { | ||
| 25 | 881 | const int out = W.rows; | |
| 26 | 881 | const int in = W.cols; | |
| 27 | assert(x.size() == in); | ||
| 28 | assert(b.size() == out); | ||
| 29 | |||
| 30 | 881 | if (y.rows != out || y.cols != 1 || y.dtype != ::brotensor::Dtype::FP32) { | |
| 31 | 804 | y.resize(out, 1, ::brotensor::Dtype::FP32); | |
| 32 | 804 | } | |
| 33 | |||
| 34 | 881 | const float* Wp = W.host_f32(); | |
| 35 | 881 | const float* xp = x.host_f32(); | |
| 36 | 881 | const float* bp = b.host_f32(); | |
| 37 | 881 | float* yp = y.host_f32_mut(); | |
| 38 | |||
| 39 | 5603 | for (int i = 0; i < out; ++i) { | |
| 40 | 4722 | float acc = bp[i]; | |
| 41 | 4722 | const float* row = Wp + static_cast<std::size_t>(i) * in; | |
| 42 | 51990 | for (int j = 0; j < in; ++j) acc += row[j] * xp[j]; | |
| 43 | 4722 | yp[i] = acc; | |
| 44 | 4722 | } | |
| 45 | 881 | } | |
| 46 | |||
| 47 | 20 | void linear_backward(const ::brotensor::Tensor& W, const ::brotensor::Tensor& x, | |
| 48 | const ::brotensor::Tensor& dY, | ||
| 49 | ::brotensor::Tensor& dX, ::brotensor::Tensor& dW, | ||
| 50 | ::brotensor::Tensor& dB) { | ||
| 51 | 20 | const int out = W.rows; | |
| 52 | 20 | const int in = W.cols; | |
| 53 | assert(x.size() == in); | ||
| 54 | assert(dY.size() == out); | ||
| 55 | assert(dW.rows == out && dW.cols == in); | ||
| 56 | assert(dB.size() == out); | ||
| 57 | |||
| 58 | 20 | if (dX.size() != in || dX.dtype != ::brotensor::Dtype::FP32) { | |
| 59 | 8 | dX.resize(in, 1, ::brotensor::Dtype::FP32); | |
| 60 | 8 | } | |
| 61 | |||
| 62 | 20 | const float* Wp = W.host_f32(); | |
| 63 | 20 | const float* xp = x.host_f32(); | |
| 64 | 20 | const float* dYp = dY.host_f32(); | |
| 65 | 20 | float* dXp = dX.host_f32_mut(); | |
| 66 | 20 | float* dWp = dW.host_f32_mut(); | |
| 67 | 20 | float* dBp = dB.host_f32_mut(); | |
| 68 | |||
| 69 | // dX = W^T * dY (overwrite) | ||
| 70 | 312 | for (int j = 0; j < in; ++j) dXp[j] = 0.0f; | |
| 71 | 264 | for (int i = 0; i < out; ++i) { | |
| 72 | 244 | const float g = dYp[i]; | |
| 73 | 244 | const float* row = Wp + static_cast<std::size_t>(i) * in; | |
| 74 | 19172 | for (int j = 0; j < in; ++j) dXp[j] += row[j] * g; | |
| 75 | 244 | } | |
| 76 | // dW += dY * x^T (outer product, accumulated) | ||
| 77 | 264 | for (int i = 0; i < out; ++i) { | |
| 78 | 244 | const float g = dYp[i]; | |
| 79 | 244 | float* row = dWp + static_cast<std::size_t>(i) * in; | |
| 80 | 19172 | for (int j = 0; j < in; ++j) row[j] += g * xp[j]; | |
| 81 | 244 | } | |
| 82 | // dB += dY | ||
| 83 | 264 | for (int i = 0; i < out; ++i) dBp[i] += dYp[i]; | |
| 84 | 20 | } | |
| 85 | |||
| 86 | // ─── Elementwise activations ─────────────────────────────────────────────── | ||
| 87 | |||
| 88 | 123 | static void ensure_match(const ::brotensor::Tensor& src, ::brotensor::Tensor& dst) { | |
| 89 | 123 | if (dst.rows != src.rows || dst.cols != src.cols || | |
| 90 | 122 | dst.dtype != ::brotensor::Dtype::FP32) { | |
| 91 | 1 | dst.resize(src.rows, src.cols, ::brotensor::Dtype::FP32); | |
| 92 | 1 | } | |
| 93 | 123 | } | |
| 94 | |||
| 95 | 18 | void relu_forward(const ::brotensor::Tensor& x, ::brotensor::Tensor& y) { | |
| 96 | 18 | ensure_match(x, y); | |
| 97 | 18 | const int n = x.size(); | |
| 98 | 18 | const float* xp = x.host_f32(); | |
| 99 | 18 | float* yp = y.host_f32_mut(); | |
| 100 | 1878 | for (int i = 0; i < n; ++i) yp[i] = xp[i] > 0.0f ? xp[i] : 0.0f; | |
| 101 | 18 | } | |
| 102 | |||
| 103 | 7 | void relu_backward(const ::brotensor::Tensor& x, const ::brotensor::Tensor& dY, | |
| 104 | ::brotensor::Tensor& dX) { | ||
| 105 | 7 | ensure_match(x, dX); | |
| 106 | 7 | const int n = x.size(); | |
| 107 | 7 | const float* xp = x.host_f32(); | |
| 108 | 7 | const float* dYp = dY.host_f32(); | |
| 109 | 7 | float* dXp = dX.host_f32_mut(); | |
| 110 | 1587 | for (int i = 0; i < n; ++i) dXp[i] = xp[i] > 0.0f ? dYp[i] : 0.0f; | |
| 111 | 7 | } | |
| 112 | |||
| 113 | 21 | void tanh_forward(const ::brotensor::Tensor& x, ::brotensor::Tensor& y) { | |
| 114 | 21 | ensure_match(x, y); | |
| 115 | 21 | const int n = x.size(); | |
| 116 | 21 | const float* xp = x.host_f32(); | |
| 117 | 21 | float* yp = y.host_f32_mut(); | |
| 118 | 2416 | for (int i = 0; i < n; ++i) yp[i] = std::tanh(xp[i]); | |
| 119 | 21 | } | |
| 120 | |||
| 121 | 7 | void tanh_backward(const ::brotensor::Tensor& y, const ::brotensor::Tensor& dY, | |
| 122 | ::brotensor::Tensor& dX) { | ||
| 123 | 7 | ensure_match(y, dX); | |
| 124 | 7 | const int n = y.size(); | |
| 125 | 7 | const float* yp = y.host_f32(); | |
| 126 | 7 | const float* dYp = dY.host_f32(); | |
| 127 | 7 | float* dXp = dX.host_f32_mut(); | |
| 128 | 1588 | for (int i = 0; i < n; ++i) { | |
| 129 | 1581 | const float yc = std::clamp(yp[i], -1.0f, 1.0f); | |
| 130 | 1581 | dXp[i] = dYp[i] * (1.0f - yc * yc); | |
| 131 | 1581 | } | |
| 132 | 7 | } | |
| 133 | |||
| 134 | 20 | void sigmoid_forward(const ::brotensor::Tensor& x, ::brotensor::Tensor& y) { | |
| 135 | 20 | ensure_match(x, y); | |
| 136 | 20 | const int n = x.size(); | |
| 137 | 20 | const float* xp = x.host_f32(); | |
| 138 | 20 | float* yp = y.host_f32_mut(); | |
| 139 | 1903 | for (int i = 0; i < n; ++i) { | |
| 140 | 1883 | const float xc = std::clamp(xp[i], -88.0f, 88.0f); | |
| 141 | 1883 | yp[i] = 1.0f / (1.0f + std::exp(-xc)); | |
| 142 | 1883 | } | |
| 143 | 20 | } | |
| 144 | |||
| 145 | 7 | void sigmoid_backward(const ::brotensor::Tensor& y, const ::brotensor::Tensor& dY, | |
| 146 | ::brotensor::Tensor& dX) { | ||
| 147 | 7 | ensure_match(y, dX); | |
| 148 | 7 | const int n = y.size(); | |
| 149 | 7 | const float* yp = y.host_f32(); | |
| 150 | 7 | const float* dYp = dY.host_f32(); | |
| 151 | 7 | float* dXp = dX.host_f32_mut(); | |
| 152 | 1588 | for (int i = 0; i < n; ++i) { | |
| 153 | 1581 | const float yc = std::clamp(yp[i], 0.0f, 1.0f); | |
| 154 | 1581 | dXp[i] = dYp[i] * yc * (1.0f - yc); | |
| 155 | 1581 | } | |
| 156 | 7 | } | |
| 157 | |||
| 158 | // ─── Softmax + xent ──────────────────────────────────────────────────────── | ||
| 159 | |||
| 160 | 441 | void softmax_forward(const ::brotensor::Tensor& logits, ::brotensor::Tensor& probs, | |
| 161 | const float* mask) { | ||
| 162 | 441 | ensure_match(logits, probs); | |
| 163 | 441 | const int n = logits.size(); | |
| 164 | 441 | const float* lp = logits.host_f32(); | |
| 165 | 441 | float* pp = probs.host_f32_mut(); | |
| 166 | |||
| 167 | 441 | float m = -1e30f; | |
| 168 | 1270 | for (int i = 0; i < n; ++i) { | |
| 169 | 829 | if (mask && mask[i] < 0.5f) continue; | |
| 170 | 572 | if (std::isfinite(lp[i]) && lp[i] > m) m = lp[i]; | |
| 171 | 572 | } | |
| 172 | 441 | float s = 0.0f; | |
| 173 | 1270 | for (int i = 0; i < n; ++i) { | |
| 174 | 1245 | if ((mask && mask[i] < 0.5f) || !std::isfinite(lp[i])) { pp[i] = 0.0f; continue; } | |
| 175 | 568 | pp[i] = std::exp(lp[i] - m); | |
| 176 | 568 | s += pp[i]; | |
| 177 | 568 | } | |
| 178 | 25 | const float inv = s > 0.0f ? 1.0f / s : 0.0f; | |
| 179 | 854 | for (int i = 0; i < n; ++i) pp[i] *= inv; | |
| 180 | 25 | } | |
| 181 | |||
| 182 | 30 | void softmax_rows_forward(const ::brotensor::Tensor& X, ::brotensor::Tensor& Y, | |
| 183 | int rows, int cols) { | ||
| 184 | 30 | if (Y.rows != X.rows || Y.cols != X.cols || Y.dtype != X.dtype) | |
| 185 | 28 | Y.resize(X.rows, X.cols, X.dtype); | |
| 186 | 30 | const float* xp = X.host_f32(); | |
| 187 | 30 | float* yp = Y.host_f32_mut(); | |
| 188 | 303 | for (int r = 0; r < rows; ++r) { | |
| 189 | 273 | const float* lp = xp + static_cast<std::size_t>(r) * cols; | |
| 190 | 273 | float* pp = yp + static_cast<std::size_t>(r) * cols; | |
| 191 | 273 | float m = -1e30f; | |
| 192 | 1452973 | for (int i = 0; i < cols; ++i) if (std::isfinite(lp[i]) && lp[i] > m) m = lp[i]; | |
| 193 | 273 | float s = 0.0f; | |
| 194 | 1452973 | for (int i = 0; i < cols; ++i) { | |
| 195 | 1452700 | pp[i] = std::isfinite(lp[i]) ? std::exp(lp[i] - m) : 0.0f; | |
| 196 | 1452700 | s += pp[i]; | |
| 197 | 1452700 | } | |
| 198 | 273 | const float inv = s > 0.0f ? 1.0f / s : 0.0f; | |
| 199 | 1452973 | for (int i = 0; i < cols; ++i) pp[i] *= inv; | |
| 200 | 273 | } | |
| 201 | 30 | } | |
| 202 | |||
| 203 | 12 | void softmax_backward(const ::brotensor::Tensor& probs, | |
| 204 | const ::brotensor::Tensor& dProbs, | ||
| 205 | ::brotensor::Tensor& dLogits) { | ||
| 206 | 12 | ensure_match(probs, dLogits); | |
| 207 | 12 | const int n = probs.size(); | |
| 208 | 12 | const float* pp = probs.host_f32(); | |
| 209 | 12 | const float* dp = dProbs.host_f32(); | |
| 210 | 12 | float* dz = dLogits.host_f32_mut(); | |
| 211 | 12 | float dot = 0.0f; | |
| 212 | 654 | for (int i = 0; i < n; ++i) { | |
| 213 | 642 | const float p = std::clamp(pp[i], 0.0f, 1.0f); | |
| 214 | 642 | if (p > 0.0f && std::isfinite(dp[i])) dot += dp[i] * p; | |
| 215 | 642 | } | |
| 216 | 654 | for (int i = 0; i < n; ++i) { | |
| 217 | 642 | const float p = std::clamp(pp[i], 0.0f, 1.0f); | |
| 218 | 642 | dz[i] = (p > 0.0f && std::isfinite(dp[i])) ? (p * (dp[i] - dot)) : 0.0f; | |
| 219 | 642 | } | |
| 220 | 12 | } | |
| 221 | |||
| 222 | 14 | float softmax_xent_segment(const float* lp, const float* tp, | |
| 223 | float* pp, float* dz, | ||
| 224 | int n, const float* mask) { | ||
| 225 | // Stable softmax over the segment. | ||
| 226 | 14 | float m = -1e30f; | |
| 227 | 557 | for (int i = 0; i < n; ++i) { | |
| 228 | 543 | if (mask && mask[i] < 0.5f) continue; | |
| 229 | 460 | if (lp[i] > m) m = lp[i]; | |
| 230 | 460 | } | |
| 231 | 14 | float s = 0.0f; | |
| 232 | 557 | for (int i = 0; i < n; ++i) { | |
| 233 | 543 | if (mask && mask[i] < 0.5f) { pp[i] = 0.0f; continue; } | |
| 234 | 460 | pp[i] = std::exp(lp[i] - m); | |
| 235 | 460 | s += pp[i]; | |
| 236 | 460 | } | |
| 237 | 14 | const float inv = s > 0.0f ? 1.0f / s : 0.0f; | |
| 238 | 557 | for (int i = 0; i < n; ++i) pp[i] *= inv; | |
| 239 | |||
| 240 | // xent + dLogits = (p - t). | ||
| 241 | 14 | float loss = 0.0f; | |
| 242 | 557 | for (int i = 0; i < n; ++i) { | |
| 243 | 543 | if (mask && mask[i] < 0.5f) { dz[i] = 0.0f; continue; } | |
| 244 | 460 | if (tp[i] > 0.0f) { | |
| 245 | 449 | const float p = pp[i] > 1e-12f ? pp[i] : 1e-12f; | |
| 246 | 449 | loss -= tp[i] * std::log(p); | |
| 247 | 449 | } | |
| 248 | 460 | dz[i] = pp[i] - tp[i]; | |
| 249 | 460 | } | |
| 250 | 14 | return loss; | |
| 251 | } | ||
| 252 | |||
| 253 | 3 | float softmax_xent(const ::brotensor::Tensor& logits, | |
| 254 | const ::brotensor::Tensor& target, | ||
| 255 | ::brotensor::Tensor& probs, ::brotensor::Tensor& dLogits, | ||
| 256 | const float* mask) { | ||
| 257 | 3 | ensure_match(logits, probs); | |
| 258 | 3 | ensure_match(logits, dLogits); | |
| 259 | 6 | return softmax_xent_segment(logits.host_f32(), target.host_f32(), | |
| 260 | 3 | probs.host_f32_mut(), dLogits.host_f32_mut(), | |
| 261 | 3 | logits.size(), mask); | |
| 262 | } | ||
| 263 | |||
| 264 | // ─── Scalar MSE ──────────────────────────────────────────────────────────── | ||
| 265 | |||
| 266 | 7 | float mse_scalar(float pred, float target, float& dPred) { | |
| 267 | 7 | const float d = pred - target; | |
| 268 | 7 | dPred = d; | |
| 269 | 7 | return 0.5f * d * d; | |
| 270 | } | ||
| 271 | |||
| 272 | // ─── Misc elementwise ────────────────────────────────────────────────────── | ||
| 273 | |||
| 274 | 870 | void add_inplace(::brotensor::Tensor& y, const ::brotensor::Tensor& x) { | |
| 275 | 870 | const int n = y.size(); | |
| 276 | assert(x.size() == n); | ||
| 277 | 870 | const float* xp = x.host_f32(); | |
| 278 | 870 | float* yp = y.host_f32_mut(); | |
| 279 | 19043475 | for (int i = 0; i < n; ++i) yp[i] += xp[i]; | |
| 280 | 870 | } | |
| 281 | |||
| 282 | 9 | void add_scalar_inplace(::brotensor::Tensor& y, float s) { | |
| 283 | 9 | const int n = y.size(); | |
| 284 | 9 | float* yp = y.host_f32_mut(); | |
| 285 | 2875 | for (int i = 0; i < n; ++i) yp[i] += s; | |
| 286 | 9 | } | |
| 287 | |||
| 288 | 23 | void add_channel_bias_inplace(::brotensor::Tensor& y, const ::brotensor::Tensor& bias, | |
| 289 | int C, int L) { | ||
| 290 | 23 | float* yp = y.host_f32_mut(); | |
| 291 | 23 | const float* bp = bias.host_f32(); | |
| 292 | 1138 | for (int c = 0; c < C; ++c) { | |
| 293 | 1115 | const float b = bp[c]; | |
| 294 | 1115 | float* row = yp + static_cast<std::size_t>(c) * L; | |
| 295 | 66215 | for (int i = 0; i < L; ++i) row[i] += b; | |
| 296 | 1115 | } | |
| 297 | 23 | } | |
| 298 | |||
| 299 | // ─── Xavier-uniform init ─────────────────────────────────────────────────── | ||
| 300 | |||
| 301 | // splitmix64 advanced by reference; deterministic, no external dep. | ||
| 302 | 105 | static inline uint64_t splitmix(uint64_t& s) { | |
| 303 | 105 | s += 0x9E3779B97F4A7C15ULL; | |
| 304 | 105 | uint64_t z = s; | |
| 305 | 105 | z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL; | |
| 306 | 105 | z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL; | |
| 307 | 105 | return z ^ (z >> 31); | |
| 308 | } | ||
| 309 | |||
| 310 | 105 | static inline float u01(uint64_t& s) { | |
| 311 | // top 24 bits → [0, 1) | ||
| 312 | 105 | return static_cast<float>((splitmix(s) >> 40)) / 16777216.0f; | |
| 313 | } | ||
| 314 | |||
| 315 | 3 | void xavier_init(::brotensor::Tensor& W, uint64_t& rng_state) { | |
| 316 | // fan-in = cols, fan-out = rows ; limit = sqrt(6 / (in + out)) | ||
| 317 | 3 | const float limit = std::sqrt(6.0f / static_cast<float>(W.rows + W.cols)); | |
| 318 | 3 | const int n = W.size(); | |
| 319 | 3 | float* wp = W.host_f32_mut(); | |
| 320 | 108 | for (int i = 0; i < n; ++i) { | |
| 321 | 105 | const float u = u01(rng_state); // [0,1) | |
| 322 | 105 | wp[i] = (u * 2.0f - 1.0f) * limit; // [-limit, +limit] | |
| 323 | 105 | } | |
| 324 | 3 | } | |
| 325 | |||
| 326 | // ─── Optimisers ──────────────────────────────────────────────────────────── | ||
| 327 | |||
| 328 | ✗ | void sgd_step(::brotensor::Tensor& param, ::brotensor::Tensor& grad, | |
| 329 | ::brotensor::Tensor& velocity, float lr, float momentum) { | ||
| 330 | ✗ | const int n = param.size(); | |
| 331 | assert(grad.size() == n && velocity.size() == n); | ||
| 332 | ✗ | float* p = param.host_f32_mut(); | |
| 333 | ✗ | const float* g = grad.host_f32(); | |
| 334 | ✗ | float* v = velocity.host_f32_mut(); | |
| 335 | ✗ | for (int i = 0; i < n; ++i) { | |
| 336 | ✗ | const float vi = momentum * v[i] + g[i]; | |
| 337 | ✗ | v[i] = vi; | |
| 338 | ✗ | p[i] -= lr * vi; | |
| 339 | ✗ | } | |
| 340 | ✗ | } | |
| 341 | |||
| 342 | 25 | void adam_step(::brotensor::Tensor& param, const ::brotensor::Tensor& grad, | |
| 343 | ::brotensor::Tensor& m, ::brotensor::Tensor& v, | ||
| 344 | float lr, float beta1, float beta2, float eps, int step) { | ||
| 345 | 25 | const int n = param.size(); | |
| 346 | assert(grad.size() == n && m.size() == n && v.size() == n); | ||
| 347 | 25 | const float inv_bc1 = 1.0f / (1.0f - std::pow(beta1, static_cast<float>(step))); | |
| 348 | 25 | const float inv_bc2 = 1.0f / (1.0f - std::pow(beta2, static_cast<float>(step))); | |
| 349 | 25 | float* p = param.host_f32_mut(); | |
| 350 | 25 | const float* g = grad.host_f32(); | |
| 351 | 25 | float* mp = m.host_f32_mut(); | |
| 352 | 25 | float* vp = v.host_f32_mut(); | |
| 353 | 6362 | for (int i = 0; i < n; ++i) { | |
| 354 | 6337 | const float gi = g[i]; | |
| 355 | 6337 | const float mi = beta1 * mp[i] + (1.0f - beta1) * gi; | |
| 356 | 6337 | const float vi = beta2 * vp[i] + (1.0f - beta2) * gi * gi; | |
| 357 | 6337 | mp[i] = mi; | |
| 358 | 6337 | vp[i] = vi; | |
| 359 | 6337 | const float m_hat = mi * inv_bc1; | |
| 360 | 6337 | const float v_hat = vi * inv_bc2; | |
| 361 | 6337 | p[i] -= lr * m_hat / (std::sqrt(v_hat) + eps); | |
| 362 | 6337 | } | |
| 363 | 25 | } | |
| 364 | |||
| 365 | // ─── Elementwise scale ───────────────────────────────────────────────────── | ||
| 366 | |||
| 367 | 281 | void scale_inplace(::brotensor::Tensor& y, float s) { | |
| 368 | 281 | const int n = y.size(); | |
| 369 | 281 | float* yp = y.host_f32_mut(); | |
| 370 | 3417 | for (int i = 0; i < n; ++i) yp[i] *= s; | |
| 371 | 281 | } | |
| 372 | |||
| 373 | // ─── LayerNorm ───────────────────────────────────────────────────────────── | ||
| 374 | |||
| 375 | 4 | void layernorm_forward(const ::brotensor::Tensor& x, | |
| 376 | const ::brotensor::Tensor& gamma, | ||
| 377 | const ::brotensor::Tensor& beta, | ||
| 378 | ::brotensor::Tensor& y, ::brotensor::Tensor& xhat, | ||
| 379 | float& mean_out, float& rstd_out, float eps) { | ||
| 380 | 4 | const int n = x.size(); | |
| 381 | 4 | if (y.rows != x.rows || y.cols != x.cols || | |
| 382 | 4 | y.dtype != ::brotensor::Dtype::FP32) { | |
| 383 | ✗ | y.resize(x.rows, x.cols, ::brotensor::Dtype::FP32); | |
| 384 | ✗ | } | |
| 385 | 4 | if (xhat.rows != x.rows || xhat.cols != x.cols || | |
| 386 | 4 | xhat.dtype != ::brotensor::Dtype::FP32) { | |
| 387 | ✗ | xhat.resize(x.rows, x.cols, ::brotensor::Dtype::FP32); | |
| 388 | ✗ | } | |
| 389 | 4 | if (n == 0) { mean_out = 0.0f; rstd_out = 0.0f; return; } | |
| 390 | |||
| 391 | 4 | const float* xp = x.host_f32(); | |
| 392 | 4 | const float* gp = gamma.host_f32(); | |
| 393 | 4 | const float* bp = beta.host_f32(); | |
| 394 | 4 | float* yp = y.host_f32_mut(); | |
| 395 | 4 | float* xhp = xhat.host_f32_mut(); | |
| 396 | |||
| 397 | 4 | float mean = 0.0f; | |
| 398 | 348 | for (int i = 0; i < n; ++i) mean += xp[i]; | |
| 399 | 4 | mean /= static_cast<float>(n); | |
| 400 | |||
| 401 | 4 | float var = 0.0f; | |
| 402 | 348 | for (int i = 0; i < n; ++i) { | |
| 403 | 344 | const float d = xp[i] - mean; | |
| 404 | 344 | var += d * d; | |
| 405 | 344 | } | |
| 406 | 4 | var /= static_cast<float>(n); | |
| 407 | 4 | const float eps_safe = std::max(eps, 1e-12f); | |
| 408 | 4 | const float denom = std::max(var + eps_safe, 1e-12f); | |
| 409 | 4 | const float rstd = std::isfinite(denom) ? (1.0f / std::sqrt(denom)) : 0.0f; | |
| 410 | |||
| 411 | 348 | for (int i = 0; i < n; ++i) { | |
| 412 | 344 | const float xh = (rstd > 0.0f && std::isfinite(mean)) ? ((xp[i] - mean) * rstd) : 0.0f; | |
| 413 | 344 | xhp[i] = xh; | |
| 414 | 344 | yp[i] = gp[i] * xh + bp[i]; | |
| 415 | 344 | } | |
| 416 | 4 | mean_out = std::isfinite(mean) ? mean : 0.0f; | |
| 417 | 4 | rstd_out = rstd; | |
| 418 | 4 | } | |
| 419 | |||
| 420 | 7 | void layernorm_backward(const ::brotensor::Tensor& dY, | |
| 421 | const ::brotensor::Tensor& xhat, | ||
| 422 | const ::brotensor::Tensor& gamma, float rstd, | ||
| 423 | ::brotensor::Tensor& dX, | ||
| 424 | ::brotensor::Tensor& dGamma, | ||
| 425 | ::brotensor::Tensor& dBeta) { | ||
| 426 | 7 | const int n = dY.size(); | |
| 427 | 7 | if (dX.rows != dY.rows || dX.cols != dY.cols || | |
| 428 | 7 | dX.dtype != ::brotensor::Dtype::FP32) { | |
| 429 | ✗ | dX.resize(dY.rows, dY.cols, ::brotensor::Dtype::FP32); | |
| 430 | ✗ | } | |
| 431 | 7 | if (n == 0) return; | |
| 432 | |||
| 433 | 7 | const float* dYp = dY.host_f32(); | |
| 434 | 7 | const float* xhp = xhat.host_f32(); | |
| 435 | 7 | const float* gp = gamma.host_f32(); | |
| 436 | 7 | float* dXp = dX.host_f32_mut(); | |
| 437 | 7 | float* dGp = dGamma.host_f32_mut(); | |
| 438 | 7 | float* dBp = dBeta.host_f32_mut(); | |
| 439 | |||
| 440 | // dGamma/dBeta accumulate; caller zeros. | ||
| 441 | 687 | for (int i = 0; i < n; ++i) { | |
| 442 | 680 | const float xhi = std::isfinite(xhp[i]) ? xhp[i] : 0.0f; | |
| 443 | 680 | dGp[i] += dYp[i] * xhi; | |
| 444 | 680 | dBp[i] += dYp[i]; | |
| 445 | 680 | } | |
| 446 | // sum_dxh, sum_dxh_xhat where dxh = dY * gamma. | ||
| 447 | 7 | float sum_dxh = 0.0f, sum_dxh_xhat = 0.0f; | |
| 448 | 687 | for (int i = 0; i < n; ++i) { | |
| 449 | 680 | const float xhi = std::isfinite(xhp[i]) ? xhp[i] : 0.0f; | |
| 450 | 680 | const float dxh = dYp[i] * gp[i]; | |
| 451 | 680 | sum_dxh += dxh; | |
| 452 | 680 | sum_dxh_xhat += dxh * xhi; | |
| 453 | 680 | } | |
| 454 | 7 | const float rstd_safe = (std::isfinite(rstd) && rstd >= 0.0f) ? rstd : 0.0f; | |
| 455 | 7 | const float nf = static_cast<float>(n); | |
| 456 | 7 | const float scale = rstd_safe / nf; | |
| 457 | 687 | for (int i = 0; i < n; ++i) { | |
| 458 | 680 | const float xhi = std::isfinite(xhp[i]) ? xhp[i] : 0.0f; | |
| 459 | 680 | const float dxh = dYp[i] * gp[i]; | |
| 460 | 680 | dXp[i] = (scale > 0.0f) ? (scale * (nf * dxh - sum_dxh - xhi * sum_dxh_xhat)) : 0.0f; | |
| 461 | 680 | } | |
| 462 | 7 | } | |
| 463 | |||
| 464 | // ─── Single-head scaled dot-product self-attention ───────────────────────── | ||
| 465 | |||
| 466 | 7 | void attention_forward(const ::brotensor::Tensor& X, | |
| 467 | const ::brotensor::Tensor& Wq, const ::brotensor::Tensor& Wk, | ||
| 468 | const ::brotensor::Tensor& Wv, const ::brotensor::Tensor& Wo, | ||
| 469 | const float* d_mask, | ||
| 470 | ::brotensor::Tensor& Q, ::brotensor::Tensor& K, | ||
| 471 | ::brotensor::Tensor& V, ::brotensor::Tensor& Attn, | ||
| 472 | ::brotensor::Tensor& Y_pre_Wo, ::brotensor::Tensor& O) { | ||
| 473 | using ::brotensor::Dtype; | ||
| 474 | 7 | const int N = X.rows; | |
| 475 | 7 | const int D = X.cols; | |
| 476 | 7 | if (Q.rows != N || Q.cols != D || Q.dtype != Dtype::FP32) Q.resize(N, D, Dtype::FP32); | |
| 477 | 7 | if (K.rows != N || K.cols != D || K.dtype != Dtype::FP32) K.resize(N, D, Dtype::FP32); | |
| 478 | 7 | if (V.rows != N || V.cols != D || V.dtype != Dtype::FP32) V.resize(N, D, Dtype::FP32); | |
| 479 | 7 | if (Attn.rows != N || Attn.cols != N || Attn.dtype != Dtype::FP32) Attn.resize(N, N, Dtype::FP32); | |
| 480 | 7 | if (Y_pre_Wo.rows != N || Y_pre_Wo.cols != D || Y_pre_Wo.dtype != Dtype::FP32) Y_pre_Wo.resize(N, D, Dtype::FP32); | |
| 481 | 7 | if (O.rows != N || O.cols != D || O.dtype != Dtype::FP32) O.resize(N, D, Dtype::FP32); | |
| 482 | 7 | if (N == 0 || D == 0) return; | |
| 483 | |||
| 484 | 7 | const float* Xp = X.host_f32(); | |
| 485 | 7 | const float* Wqp = Wq.host_f32(); | |
| 486 | 7 | const float* Wkp = Wk.host_f32(); | |
| 487 | 7 | const float* Wvp = Wv.host_f32(); | |
| 488 | 7 | const float* Wop = Wo.host_f32(); | |
| 489 | 7 | float* Qp = Q.host_f32_mut(); | |
| 490 | 7 | float* Kp = K.host_f32_mut(); | |
| 491 | 7 | float* Vp = V.host_f32_mut(); | |
| 492 | 7 | float* Ap = Attn.host_f32_mut(); | |
| 493 | 7 | float* Yp = Y_pre_Wo.host_f32_mut(); | |
| 494 | 7 | float* Op = O.host_f32_mut(); | |
| 495 | |||
| 496 | 73 | parallel_for(static_cast<std::size_t>(N), [&](std::size_t ii) { | |
| 497 | 66 | const int i = static_cast<int>(ii); | |
| 498 | 66 | const float* xr = Xp + static_cast<std::size_t>(i) * D; | |
| 499 | 2982 | for (int j = 0; j < D; ++j) { | |
| 500 | 2916 | const float* wq = Wqp + static_cast<std::size_t>(j) * D; | |
| 501 | 2916 | const float* wk = Wkp + static_cast<std::size_t>(j) * D; | |
| 502 | 2916 | const float* wv = Wvp + static_cast<std::size_t>(j) * D; | |
| 503 | 2916 | float aq = 0.0f, ak = 0.0f, av = 0.0f; | |
| 504 | 128927 | for (int k = 0; k < D; ++k) { | |
| 505 | 126011 | aq += xr[k] * wq[k]; | |
| 506 | 126011 | ak += xr[k] * wk[k]; | |
| 507 | 126011 | av += xr[k] * wv[k]; | |
| 508 | 126011 | } | |
| 509 | 2916 | const std::size_t idx = static_cast<std::size_t>(i) * D + j; | |
| 510 | 2916 | Qp[idx] = aq; | |
| 511 | 2916 | Kp[idx] = ak; | |
| 512 | 2916 | Vp[idx] = av; | |
| 513 | 2916 | } | |
| 514 | 66 | }); | |
| 515 | |||
| 516 | 7 | const float inv_sqrtd = 1.0f / std::sqrt(static_cast<float>(D)); | |
| 517 | |||
| 518 | 72 | parallel_for(static_cast<std::size_t>(N), [&](std::size_t ii) { | |
| 519 | 65 | const int i = static_cast<int>(ii); | |
| 520 | 65 | float* arow = Ap + static_cast<std::size_t>(i) * N; | |
| 521 | 65 | if (d_mask && d_mask[i] < 0.5f) { | |
| 522 | 198 | for (int j = 0; j < N; ++j) arow[j] = 0.0f; | |
| 523 | 14 | return; | |
| 524 | } | ||
| 525 | 51 | const float* qr = Qp + static_cast<std::size_t>(i) * D; | |
| 526 | 51 | float m = -1e30f; | |
| 527 | 625 | for (int j = 0; j < N; ++j) { | |
| 528 | 574 | if (d_mask && d_mask[j] < 0.5f) continue; | |
| 529 | 496 | const float* kr = Kp + static_cast<std::size_t>(j) * D; | |
| 530 | 496 | float s = 0.0f; | |
| 531 | 18338 | for (int k = 0; k < D; ++k) s += qr[k] * kr[k]; | |
| 532 | 496 | s *= inv_sqrtd; | |
| 533 | 496 | arow[j] = s; | |
| 534 | 496 | if (s > m) m = s; | |
| 535 | 496 | } | |
| 536 | 51 | float sum = 0.0f; | |
| 537 | 587 | for (int j = 0; j < N; ++j) { | |
| 538 | 536 | if (d_mask && d_mask[j] < 0.5f) { arow[j] = 0.0f; continue; } | |
| 539 | 458 | const float e = std::exp(arow[j] - m); | |
| 540 | 458 | arow[j] = e; | |
| 541 | 458 | sum += e; | |
| 542 | 458 | } | |
| 543 | 51 | const float inv = sum > 0.0f ? 1.0f / sum : 0.0f; | |
| 544 | 609 | for (int j = 0; j < N; ++j) arow[j] *= inv; | |
| 545 | 65 | }); | |
| 546 | |||
| 547 | 72 | parallel_for(static_cast<std::size_t>(N), [&](std::size_t ii) { | |
| 548 | 65 | const int i = static_cast<int>(ii); | |
| 549 | 65 | const float* arow = Ap + static_cast<std::size_t>(i) * N; | |
| 550 | 65 | float* yr = Yp + static_cast<std::size_t>(i) * D; | |
| 551 | 2572 | for (int k = 0; k < D; ++k) { | |
| 552 | 2507 | float acc = 0.0f; | |
| 553 | 34379 | for (int j = 0; j < N; ++j) | |
| 554 | 31872 | acc += arow[j] * Vp[static_cast<std::size_t>(j) * D + k]; | |
| 555 | 2507 | yr[k] = acc; | |
| 556 | 2507 | } | |
| 557 | 65 | }); | |
| 558 | |||
| 559 | 73 | parallel_for(static_cast<std::size_t>(N), [&](std::size_t ii) { | |
| 560 | 66 | const int i = static_cast<int>(ii); | |
| 561 | 66 | float* orow = Op + static_cast<std::size_t>(i) * D; | |
| 562 | 66 | if (d_mask && d_mask[i] < 0.5f) { | |
| 563 | 670 | for (int c = 0; c < D; ++c) orow[c] = 0.0f; | |
| 564 | 14 | return; | |
| 565 | } | ||
| 566 | 52 | const float* yr = Yp + static_cast<std::size_t>(i) * D; | |
| 567 | 2229 | for (int c = 0; c < D; ++c) { | |
| 568 | 2177 | const float* wr = Wop + static_cast<std::size_t>(c) * D; | |
| 569 | 2177 | float acc = 0.0f; | |
| 570 | 102571 | for (int k = 0; k < D; ++k) acc += yr[k] * wr[k]; | |
| 571 | 2177 | orow[c] = acc; | |
| 572 | 2177 | } | |
| 573 | 66 | }); | |
| 574 | 7 | } | |
| 575 | |||
| 576 | 7 | void attention_backward(const ::brotensor::Tensor& dO, | |
| 577 | const ::brotensor::Tensor& X, | ||
| 578 | const ::brotensor::Tensor& Q, const ::brotensor::Tensor& K, | ||
| 579 | const ::brotensor::Tensor& V, const ::brotensor::Tensor& Attn, | ||
| 580 | const ::brotensor::Tensor& Y_pre_Wo, | ||
| 581 | const ::brotensor::Tensor& Wq, const ::brotensor::Tensor& Wk, | ||
| 582 | const ::brotensor::Tensor& Wv, const ::brotensor::Tensor& Wo, | ||
| 583 | const float* d_mask, | ||
| 584 | ::brotensor::Tensor& dX, | ||
| 585 | ::brotensor::Tensor& dWq, ::brotensor::Tensor& dWk, | ||
| 586 | ::brotensor::Tensor& dWv, ::brotensor::Tensor& dWo) { | ||
| 587 | using ::brotensor::Dtype; | ||
| 588 | 7 | const int N = X.rows; | |
| 589 | 7 | const int D = X.cols; | |
| 590 | 7 | if (dX.rows != N || dX.cols != D || dX.dtype != Dtype::FP32) { | |
| 591 | ✗ | dX.resize(N, D, Dtype::FP32); | |
| 592 | ✗ | } | |
| 593 | 7 | if (N == 0 || D == 0) return; | |
| 594 | |||
| 595 | 7 | const float inv_sqrtd = 1.0f / std::sqrt(static_cast<float>(D)); | |
| 596 | |||
| 597 | 7 | const float* dOp = dO.host_f32(); | |
| 598 | 7 | const float* Xp = X.host_f32(); | |
| 599 | 7 | const float* Qp = Q.host_f32(); | |
| 600 | 7 | const float* Kp = K.host_f32(); | |
| 601 | 7 | const float* Vp = V.host_f32(); | |
| 602 | 7 | const float* Ap = Attn.host_f32(); | |
| 603 | 7 | const float* Yp = Y_pre_Wo.host_f32(); | |
| 604 | 7 | const float* Wqp = Wq.host_f32(); | |
| 605 | 7 | const float* Wkp = Wk.host_f32(); | |
| 606 | 7 | const float* Wvp = Wv.host_f32(); | |
| 607 | 7 | const float* Wop = Wo.host_f32(); | |
| 608 | 7 | float* dXp = dX.host_f32_mut(); | |
| 609 | 7 | float* dWqp = dWq.host_f32_mut(); | |
| 610 | 7 | float* dWkp = dWk.host_f32_mut(); | |
| 611 | 7 | float* dWvp = dWv.host_f32_mut(); | |
| 612 | 7 | float* dWop = dWo.host_f32_mut(); | |
| 613 | |||
| 614 | 7 | std::vector<float> dY(static_cast<std::size_t>(N) * D, 0.0f); | |
| 615 | 7 | std::vector<float> dAttn(static_cast<std::size_t>(N) * N, 0.0f); | |
| 616 | 7 | std::vector<float> dV(static_cast<std::size_t>(N) * D, 0.0f); | |
| 617 | 7 | std::vector<float> dScores(static_cast<std::size_t>(N) * N, 0.0f); | |
| 618 | 7 | std::vector<float> dQ(static_cast<std::size_t>(N) * D, 0.0f); | |
| 619 | 7 | std::vector<float> dK(static_cast<std::size_t>(N) * D, 0.0f); | |
| 620 | |||
| 621 | 73 | parallel_for(static_cast<std::size_t>(N), [&](std::size_t ii) { | |
| 622 | 66 | const int i = static_cast<int>(ii); | |
| 623 | 66 | const bool valid = !(d_mask && d_mask[i] < 0.5f); | |
| 624 | 3011 | for (int k = 0; k < D; ++k) { | |
| 625 | 2945 | float acc = 0.0f; | |
| 626 | 2945 | if (valid) { | |
| 627 | 107445 | for (int c = 0; c < D; ++c) | |
| 628 | 210406 | acc += Wop[static_cast<std::size_t>(c) * D + k] * | |
| 629 | 105203 | dOp[static_cast<std::size_t>(i) * D + c]; | |
| 630 | 2242 | } | |
| 631 | 2945 | dY[static_cast<std::size_t>(i) * D + k] = acc; | |
| 632 | 2945 | } | |
| 633 | 66 | }); | |
| 634 | 73 | for (int i = 0; i < N; ++i) { | |
| 635 | 66 | if (d_mask && d_mask[i] < 0.5f) continue; | |
| 636 | 52 | const float* dOi = dOp + static_cast<std::size_t>(i) * D; | |
| 637 | 52 | const float* Yi = Yp + static_cast<std::size_t>(i) * D; | |
| 638 | 2372 | for (int c = 0; c < D; ++c) { | |
| 639 | 2320 | const float dov = dOi[c]; | |
| 640 | 2320 | float* dWrow = dWop + static_cast<std::size_t>(c) * D; | |
| 641 | 134672 | for (int k = 0; k < D; ++k) { | |
| 642 | 132352 | dWrow[k] += dov * Yi[k]; | |
| 643 | 132352 | } | |
| 644 | 2320 | } | |
| 645 | 52 | } | |
| 646 | |||
| 647 | 73 | parallel_for(static_cast<std::size_t>(N), [&](std::size_t ii) { | |
| 648 | 66 | const int i = static_cast<int>(ii); | |
| 649 | 834 | for (int j = 0; j < N; ++j) { | |
| 650 | 768 | float acc = 0.0f; | |
| 651 | 34360 | for (int k = 0; k < D; ++k) | |
| 652 | 67184 | acc += dY[static_cast<std::size_t>(i) * D + k] * | |
| 653 | 33592 | Vp[static_cast<std::size_t>(j) * D + k]; | |
| 654 | 768 | dAttn[static_cast<std::size_t>(i) * N + j] = acc; | |
| 655 | 768 | } | |
| 656 | 66 | }); | |
| 657 | 73 | for (int i = 0; i < N; ++i) { | |
| 658 | 66 | const float* Ai = Ap + static_cast<std::size_t>(i) * N; | |
| 659 | 66 | const float* dYi = dY.data() + static_cast<std::size_t>(i) * D; | |
| 660 | 838 | for (int j = 0; j < N; ++j) { | |
| 661 | 772 | const float av = Ai[j]; | |
| 662 | 772 | float* dVrow = dV.data() + static_cast<std::size_t>(j) * D; | |
| 663 | 40772 | for (int k = 0; k < D; ++k) { | |
| 664 | 40000 | dVrow[k] += av * dYi[k]; | |
| 665 | 40000 | } | |
| 666 | 772 | } | |
| 667 | 66 | } | |
| 668 | |||
| 669 | 73 | parallel_for(static_cast<std::size_t>(N), [&](std::size_t ii) { | |
| 670 | 66 | const int i = static_cast<int>(ii); | |
| 671 | 66 | const float* prow = Ap + static_cast<std::size_t>(i) * N; | |
| 672 | 66 | const float* dprow = dAttn.data() + static_cast<std::size_t>(i) * N; | |
| 673 | 66 | float* drow = dScores.data() + static_cast<std::size_t>(i) * N; | |
| 674 | 66 | if (d_mask && d_mask[i] < 0.5f) { | |
| 675 | 198 | for (int j = 0; j < N; ++j) drow[j] = 0.0f; | |
| 676 | 14 | return; | |
| 677 | } | ||
| 678 | 52 | float dot = 0.0f; | |
| 679 | 624 | for (int j = 0; j < N; ++j) dot += dprow[j] * prow[j]; | |
| 680 | 600 | for (int j = 0; j < N; ++j) { | |
| 681 | 548 | if (d_mask && d_mask[j] < 0.5f) drow[j] = 0.0f; | |
| 682 | 470 | else drow[j] = prow[j] * (dprow[j] - dot) * inv_sqrtd; | |
| 683 | 548 | } | |
| 684 | 66 | }); | |
| 685 | |||
| 686 | 73 | parallel_for(static_cast<std::size_t>(N), [&](std::size_t ii) { | |
| 687 | 66 | const int i = static_cast<int>(ii); | |
| 688 | 2883 | for (int k = 0; k < D; ++k) { | |
| 689 | 2817 | float acc = 0.0f; | |
| 690 | 30554 | for (int j = 0; j < N; ++j) | |
| 691 | 55474 | acc += dScores[static_cast<std::size_t>(i) * N + j] * | |
| 692 | 27737 | Kp[static_cast<std::size_t>(j) * D + k]; | |
| 693 | 2817 | dQ[static_cast<std::size_t>(i) * D + k] = acc; | |
| 694 | 2817 | } | |
| 695 | 66 | }); | |
| 696 | 73 | for (int i = 0; i < N; ++i) { | |
| 697 | 66 | const float* dSi = dScores.data() + static_cast<std::size_t>(i) * N; | |
| 698 | 66 | const float* Qi = Qp + static_cast<std::size_t>(i) * D; | |
| 699 | 838 | for (int j = 0; j < N; ++j) { | |
| 700 | 772 | const float ds = dSi[j]; | |
| 701 | 772 | float* dKrow = dK.data() + static_cast<std::size_t>(j) * D; | |
| 702 | 40772 | for (int k = 0; k < D; ++k) { | |
| 703 | 40000 | dKrow[k] += ds * Qi[k]; | |
| 704 | 40000 | } | |
| 705 | 772 | } | |
| 706 | 66 | } | |
| 707 | |||
| 708 | 73 | for (int i = 0; i < N; ++i) { | |
| 709 | 66 | const float* Xi = Xp + static_cast<std::size_t>(i) * D; | |
| 710 | 66 | const float* dQi = dQ.data() + static_cast<std::size_t>(i) * D; | |
| 711 | 66 | const float* dKi = dK.data() + static_cast<std::size_t>(i) * D; | |
| 712 | 66 | const float* dVi = dV.data() + static_cast<std::size_t>(i) * D; | |
| 713 | 3042 | for (int j = 0; j < D; ++j) { | |
| 714 | 2976 | const float qj = dQi[j]; | |
| 715 | 2976 | const float kj = dKi[j]; | |
| 716 | 2976 | const float vj = dVi[j]; | |
| 717 | 2976 | float* dWqrow = dWqp + static_cast<std::size_t>(j) * D; | |
| 718 | 2976 | float* dWkrow = dWkp + static_cast<std::size_t>(j) * D; | |
| 719 | 2976 | float* dWvrow = dWvp + static_cast<std::size_t>(j) * D; | |
| 720 | 173472 | for (int k = 0; k < D; ++k) { | |
| 721 | 170496 | const float xv = Xi[k]; | |
| 722 | 170496 | dWqrow[k] += qj * xv; | |
| 723 | 170496 | dWkrow[k] += kj * xv; | |
| 724 | 170496 | dWvrow[k] += vj * xv; | |
| 725 | 170496 | } | |
| 726 | 2976 | } | |
| 727 | 66 | } | |
| 728 | |||
| 729 | 73 | parallel_for(static_cast<std::size_t>(N), [&](std::size_t ii) { | |
| 730 | 66 | const int i = static_cast<int>(ii); | |
| 731 | 3025 | for (int k = 0; k < D; ++k) { | |
| 732 | 2959 | float acc = 0.0f; | |
| 733 | 143041 | for (int j = 0; j < D; ++j) { | |
| 734 | 140082 | const std::size_t widx = static_cast<std::size_t>(j) * D + k; | |
| 735 | 280164 | acc += dQ[static_cast<std::size_t>(i) * D + j] * Wqp[widx] | |
| 736 | 140082 | + dK[static_cast<std::size_t>(i) * D + j] * Wkp[widx] | |
| 737 | 140082 | + dV[static_cast<std::size_t>(i) * D + j] * Wvp[widx]; | |
| 738 | 140082 | } | |
| 739 | 2959 | dXp[static_cast<std::size_t>(i) * D + k] = acc; | |
| 740 | 2959 | } | |
| 741 | 66 | }); | |
| 742 | 7 | } | |
| 743 | |||
| 744 | } // namespace brotensor::detail::cpu | ||
| 745 |