src/cpu/ops_mha_batched.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ─── CPU multi-head attention, batched ops & masked mean-pool ────────────── | ||
| 2 | // | ||
| 3 | // Plain FP32 host loops decomposed from src/cpu/ops_impl.cpp. | ||
| 4 | |||
| 5 | #include <brotensor/tensor.h> | ||
| 6 | #include <brotensor/detail/cpu/thread_pool.h> | ||
| 7 | |||
| 8 | #include <algorithm> | ||
| 9 | #include <cassert> | ||
| 10 | #include <cmath> | ||
| 11 | #include <cstddef> | ||
| 12 | #include <cstdint> | ||
| 13 | #include <cstring> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | namespace brotensor::detail::cpu { | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | 10 | void ensure_match(const ::brotensor::Tensor& src, ::brotensor::Tensor& dst) { | |
| 21 | 10 | if (dst.rows != src.rows || dst.cols != src.cols || | |
| 22 | 10 | dst.dtype != ::brotensor::Dtype::FP32) { | |
| 23 | ✗ | dst.resize(src.rows, src.cols, ::brotensor::Dtype::FP32); | |
| 24 | ✗ | } | |
| 25 | 10 | } | |
| 26 | |||
| 27 | } // namespace | ||
| 28 | |||
| 29 | // ─── Multi-head self-attention ───────────────────────────────────────────── | ||
| 30 | |||
| 31 | 26 | void mha_forward(const ::brotensor::Tensor& X, | |
| 32 | const ::brotensor::Tensor& Wq, const ::brotensor::Tensor& Wk, | ||
| 33 | const ::brotensor::Tensor& Wv, const ::brotensor::Tensor& Wo, | ||
| 34 | const ::brotensor::Tensor* bq, const ::brotensor::Tensor* bk, | ||
| 35 | const ::brotensor::Tensor* bv, const ::brotensor::Tensor* bo, | ||
| 36 | const float* d_mask, int num_heads, | ||
| 37 | ::brotensor::Tensor& Qh, ::brotensor::Tensor& Kh, | ||
| 38 | ::brotensor::Tensor& Vh, ::brotensor::Tensor& Attnh, | ||
| 39 | ::brotensor::Tensor& Yconcat, ::brotensor::Tensor& O) { | ||
| 40 | using ::brotensor::Dtype; | ||
| 41 | 26 | const int K = X.rows; | |
| 42 | 26 | const int D = X.cols; | |
| 43 | 26 | const int H = num_heads; | |
| 44 | 26 | const int dh = (H > 0) ? D / H : 0; | |
| 45 | 26 | if (Qh.rows != H * K || Qh.cols != dh || Qh.dtype != Dtype::FP32) Qh.resize(H * K, dh, Dtype::FP32); | |
| 46 | 26 | if (Kh.rows != H * K || Kh.cols != dh || Kh.dtype != Dtype::FP32) Kh.resize(H * K, dh, Dtype::FP32); | |
| 47 | 26 | if (Vh.rows != H * K || Vh.cols != dh || Vh.dtype != Dtype::FP32) Vh.resize(H * K, dh, Dtype::FP32); | |
| 48 | 26 | if (Attnh.rows != H * K || Attnh.cols != K || Attnh.dtype != Dtype::FP32) Attnh.resize(H * K, K, Dtype::FP32); | |
| 49 | 26 | if (Yconcat.rows != K || Yconcat.cols != D || Yconcat.dtype != Dtype::FP32) Yconcat.resize(K, D, Dtype::FP32); | |
| 50 | 26 | if (O.rows != K || O.cols != D || O.dtype != Dtype::FP32) O.resize(K, D, Dtype::FP32); | |
| 51 | 26 | if (K == 0 || D == 0 || H == 0) return; | |
| 52 | |||
| 53 | 26 | const float* Xp = X.host_f32(); | |
| 54 | 26 | const float* Wqp = Wq.host_f32(); | |
| 55 | 26 | const float* Wkp = Wk.host_f32(); | |
| 56 | 26 | const float* Wvp = Wv.host_f32(); | |
| 57 | 26 | const float* Wop = Wo.host_f32(); | |
| 58 | 26 | float* Qp = Qh.host_f32_mut(); | |
| 59 | 26 | float* Kp = Kh.host_f32_mut(); | |
| 60 | 26 | float* Vp = Vh.host_f32_mut(); | |
| 61 | 26 | float* Ap = Attnh.host_f32_mut(); | |
| 62 | 26 | float* Yp = Yconcat.host_f32_mut(); | |
| 63 | 26 | float* Op = O.host_f32_mut(); | |
| 64 | |||
| 65 | 26 | const float* bqp = bq ? bq->host_f32() : nullptr; | |
| 66 | 26 | const float* bkp = bk ? bk->host_f32() : nullptr; | |
| 67 | 26 | const float* bvp = bv ? bv->host_f32() : nullptr; | |
| 68 | 26 | const float* bop = bo ? bo->host_f32() : nullptr; | |
| 69 | |||
| 70 | // Per-head Q/K/V projection: out(hh,i,j) = sum_k X(i,k) * W(hh*dh+j, k) | ||
| 71 | // + (b ? b[hh*dh+j] : 0). Biases are flat length-D vectors indexed by | ||
| 72 | // the full output column (hh*dh+j). Each head hh owns rows | ||
| 73 | // [hh*K, hh*K+K) of Qh/Kh/Vh exclusively (X/W*/b* read-only), so the | ||
| 74 | // head axis parallelizes with no cross-thread writes. | ||
| 75 | 133 | parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) { | |
| 76 | 107 | const int hh = static_cast<int>(hhi); | |
| 77 | 107 | const int row_off = hh * dh; | |
| 78 | 1255 | for (int i = 0; i < K; ++i) { | |
| 79 | 1148 | const float* xr = Xp + static_cast<std::size_t>(i) * D; | |
| 80 | 1148 | const std::size_t out_row = | |
| 81 | 1148 | (static_cast<std::size_t>(hh) * K + i) * dh; | |
| 82 | 13455 | for (int j = 0; j < dh; ++j) { | |
| 83 | 12307 | const int wrow = row_off + j; | |
| 84 | 12307 | const float* wq = Wqp + static_cast<std::size_t>(wrow) * D; | |
| 85 | 12307 | const float* wk = Wkp + static_cast<std::size_t>(wrow) * D; | |
| 86 | 12307 | const float* wv = Wvp + static_cast<std::size_t>(wrow) * D; | |
| 87 | 12307 | float aq = 0.0f, ak = 0.0f, av = 0.0f; | |
| 88 | 599630 | for (int k = 0; k < D; ++k) { | |
| 89 | 587323 | aq += xr[k] * wq[k]; | |
| 90 | 587323 | ak += xr[k] * wk[k]; | |
| 91 | 587323 | av += xr[k] * wv[k]; | |
| 92 | 587323 | } | |
| 93 | 12307 | if (bqp) aq += bqp[wrow]; | |
| 94 | 12307 | if (bkp) ak += bkp[wrow]; | |
| 95 | 12307 | if (bvp) av += bvp[wrow]; | |
| 96 | 12307 | Qp[out_row + j] = aq; | |
| 97 | 12307 | Kp[out_row + j] = ak; | |
| 98 | 12307 | Vp[out_row + j] = av; | |
| 99 | 12307 | } | |
| 100 | 1148 | } | |
| 101 | 107 | }); | |
| 102 | |||
| 103 | 26 | const float inv_sqrtdh = 1.0f / std::sqrt(static_cast<float>(dh)); | |
| 104 | |||
| 105 | // Per-head scores → masked row softmax → Attnh. Each hh owns rows | ||
| 106 | // [hh*K, hh*K+K) of Attnh exclusively, so parallelizes over hh. | ||
| 107 | 133 | parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) { | |
| 108 | 107 | const int hh = static_cast<int>(hhi); | |
| 109 | 1252 | for (int i = 0; i < K; ++i) { | |
| 110 | 1145 | float* arow = Ap + (static_cast<std::size_t>(hh) * K + i) * K; | |
| 111 | 1145 | if (d_mask && d_mask[i] < 0.5f) { | |
| 112 | 2760 | for (int j = 0; j < K; ++j) arow[j] = 0.0f; | |
| 113 | 184 | continue; | |
| 114 | } | ||
| 115 | 961 | const float* qr = Qp + (static_cast<std::size_t>(hh) * K + i) * dh; | |
| 116 | 961 | float m = -1e30f; | |
| 117 | 12096 | for (int j = 0; j < K; ++j) { | |
| 118 | 11135 | if (d_mask && d_mask[j] < 0.5f) continue; | |
| 119 | 10007 | const float* kr = Kp + (static_cast<std::size_t>(hh) * K + j) * dh; | |
| 120 | 10007 | float s = 0.0f; | |
| 121 | 121259 | for (int k = 0; k < dh; ++k) s += qr[k] * kr[k]; | |
| 122 | 10007 | s *= inv_sqrtdh; | |
| 123 | 10007 | arow[j] = s; | |
| 124 | 10007 | if (s > m) m = s; | |
| 125 | 10007 | } | |
| 126 | 961 | float sum = 0.0f; | |
| 127 | 12497 | for (int j = 0; j < K; ++j) { | |
| 128 | 11536 | if (d_mask && d_mask[j] < 0.5f) { arow[j] = 0.0f; continue; } | |
| 129 | 10408 | const float e = std::exp(arow[j] - m); | |
| 130 | 10408 | arow[j] = e; | |
| 131 | 10408 | sum += e; | |
| 132 | 10408 | } | |
| 133 | 961 | const float inv = sum > 0.0f ? 1.0f / sum : 0.0f; | |
| 134 | 12641 | for (int j = 0; j < K; ++j) arow[j] *= inv; | |
| 135 | 961 | } | |
| 136 | 107 | }); | |
| 137 | |||
| 138 | // Per-head Attn @ V → Yconcat(i, hh*dh+k). Each hh writes only its own | ||
| 139 | // column range [hh*dh, hh*dh+dh) of every row of Yconcat — different hh | ||
| 140 | // never touch the same bytes, so this parallelizes over hh. | ||
| 141 | 132 | parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) { | |
| 142 | 106 | const int hh = static_cast<int>(hhi); | |
| 143 | 1250 | for (int i = 0; i < K; ++i) { | |
| 144 | 1144 | const float* arow = Ap + (static_cast<std::size_t>(hh) * K + i) * K; | |
| 145 | 13010 | for (int k = 0; k < dh; ++k) { | |
| 146 | 11866 | float acc = 0.0f; | |
| 147 | 165927 | for (int j = 0; j < K; ++j) { | |
| 148 | 154061 | const float vv = Vp[(static_cast<std::size_t>(hh) * K + j) * dh + k]; | |
| 149 | 154061 | acc += arow[j] * vv; | |
| 150 | 154061 | } | |
| 151 | 11866 | Yp[static_cast<std::size_t>(i) * D + (hh * dh + k)] = acc; | |
| 152 | 11866 | } | |
| 153 | 1144 | } | |
| 154 | 106 | }); | |
| 155 | |||
| 156 | // Output projection O = Yconcat @ Wo^T + bo, zero invalid query rows. | ||
| 157 | // Each i owns row i of O exclusively (Yconcat/Wo/bo read-only), so this | ||
| 158 | // parallelizes over i. | ||
| 159 | 280 | parallel_for(static_cast<std::size_t>(K), [&](std::size_t ii) { | |
| 160 | 254 | const int i = static_cast<int>(ii); | |
| 161 | 254 | float* orow = Op + static_cast<std::size_t>(i) * D; | |
| 162 | 254 | if (d_mask && d_mask[i] < 0.5f) { | |
| 163 | 2173 | for (int c = 0; c < D; ++c) orow[c] = 0.0f; | |
| 164 | 40 | return; | |
| 165 | } | ||
| 166 | 214 | const float* yr = Yp + static_cast<std::size_t>(i) * D; | |
| 167 | 9937 | for (int c = 0; c < D; ++c) { | |
| 168 | 9723 | const float* wr = Wop + static_cast<std::size_t>(c) * D; | |
| 169 | 9723 | float acc = 0.0f; | |
| 170 | 462495 | for (int k = 0; k < D; ++k) acc += yr[k] * wr[k]; | |
| 171 | 9723 | if (bop) acc += bop[c]; | |
| 172 | 9723 | orow[c] = acc; | |
| 173 | 9723 | } | |
| 174 | 254 | }); | |
| 175 | 26 | } | |
| 176 | |||
| 177 | 19 | void mha_backward(const ::brotensor::Tensor& dO, | |
| 178 | const ::brotensor::Tensor& X, | ||
| 179 | const ::brotensor::Tensor& Qh, const ::brotensor::Tensor& Kh, | ||
| 180 | const ::brotensor::Tensor& Vh, const ::brotensor::Tensor& Attnh, | ||
| 181 | const ::brotensor::Tensor& Yconcat, | ||
| 182 | const ::brotensor::Tensor& Wq, const ::brotensor::Tensor& Wk, | ||
| 183 | const ::brotensor::Tensor& Wv, const ::brotensor::Tensor& Wo, | ||
| 184 | const float* d_mask, int num_heads, | ||
| 185 | ::brotensor::Tensor& dX, | ||
| 186 | ::brotensor::Tensor& dWq, ::brotensor::Tensor& dWk, | ||
| 187 | ::brotensor::Tensor& dWv, ::brotensor::Tensor& dWo, | ||
| 188 | ::brotensor::Tensor* dbq, ::brotensor::Tensor* dbk, | ||
| 189 | ::brotensor::Tensor* dbv, ::brotensor::Tensor* dbo) { | ||
| 190 | using ::brotensor::Dtype; | ||
| 191 | 19 | const int K = X.rows; | |
| 192 | 19 | const int D = X.cols; | |
| 193 | 19 | const int H = num_heads; | |
| 194 | 19 | const int dh = (H > 0) ? D / H : 0; | |
| 195 | 19 | if (dX.rows != K || dX.cols != D || dX.dtype != Dtype::FP32) { | |
| 196 | ✗ | dX.resize(K, D, Dtype::FP32); | |
| 197 | ✗ | } | |
| 198 | 19 | if (K == 0 || D == 0 || H == 0) return; | |
| 199 | |||
| 200 | 19 | const float inv_sqrtdh = 1.0f / std::sqrt(static_cast<float>(dh)); | |
| 201 | |||
| 202 | 19 | const float* dOp = dO.host_f32(); | |
| 203 | 19 | const float* Xp = X.host_f32(); | |
| 204 | 19 | const float* Qp = Qh.host_f32(); | |
| 205 | 19 | const float* Kp = Kh.host_f32(); | |
| 206 | 19 | const float* Vp = Vh.host_f32(); | |
| 207 | 19 | const float* Ap = Attnh.host_f32(); | |
| 208 | 19 | const float* Yp = Yconcat.host_f32(); | |
| 209 | 19 | const float* Wqp = Wq.host_f32(); | |
| 210 | 19 | const float* Wkp = Wk.host_f32(); | |
| 211 | 19 | const float* Wvp = Wv.host_f32(); | |
| 212 | 19 | const float* Wop = Wo.host_f32(); | |
| 213 | 19 | float* dXp = dX.host_f32_mut(); | |
| 214 | 19 | float* dWqp = dWq.host_f32_mut(); | |
| 215 | 19 | float* dWkp = dWk.host_f32_mut(); | |
| 216 | 19 | float* dWvp = dWv.host_f32_mut(); | |
| 217 | 19 | float* dWop = dWo.host_f32_mut(); | |
| 218 | |||
| 219 | 19 | std::vector<float> dYc(static_cast<std::size_t>(K) * D, 0.0f); | |
| 220 | 19 | std::vector<float> dAttn(static_cast<std::size_t>(H) * K * K, 0.0f); | |
| 221 | 19 | std::vector<float> dVh(static_cast<std::size_t>(H) * K * dh, 0.0f); | |
| 222 | 19 | std::vector<float> dScores(static_cast<std::size_t>(H) * K * K, 0.0f); | |
| 223 | 19 | std::vector<float> dQh(static_cast<std::size_t>(H) * K * dh, 0.0f); | |
| 224 | 19 | std::vector<float> dKh(static_cast<std::size_t>(H) * K * dh, 0.0f); | |
| 225 | |||
| 226 | // dYconcat (overwrite, zero on invalid rows). Each i owns row i of dYc | ||
| 227 | // exclusively (Wo/dO read-only), so this parallelizes over i. | ||
| 228 | 205 | parallel_for(static_cast<std::size_t>(K), [&](std::size_t ii) { | |
| 229 | 186 | const int i = static_cast<int>(ii); | |
| 230 | 186 | const bool valid = !(d_mask && d_mask[i] < 0.5f); | |
| 231 | 9484 | for (int k = 0; k < D; ++k) { | |
| 232 | 9298 | float acc = 0.0f; | |
| 233 | 9298 | if (valid) { | |
| 234 | 381056 | for (int c = 0; c < D; ++c) | |
| 235 | 747654 | acc += Wop[static_cast<std::size_t>(c) * D + k] * | |
| 236 | 373827 | dOp[static_cast<std::size_t>(i) * D + c]; | |
| 237 | 7229 | } | |
| 238 | 9298 | dYc[static_cast<std::size_t>(i) * D + k] = acc; | |
| 239 | 9298 | } | |
| 240 | 186 | }); | |
| 241 | // dWo (accumulate). | ||
| 242 | 205 | for (int i = 0; i < K; ++i) { | |
| 243 | 186 | if (d_mask && d_mask[i] < 0.5f) continue; | |
| 244 | 148 | const float* dOi = dOp + static_cast<std::size_t>(i) * D; | |
| 245 | 148 | const float* Yi = Yp + static_cast<std::size_t>(i) * D; | |
| 246 | 7508 | for (int c = 0; c < D; ++c) { | |
| 247 | 7360 | const float dov = dOi[c]; | |
| 248 | 7360 | float* dWrow = dWop + static_cast<std::size_t>(c) * D; | |
| 249 | 418496 | for (int k = 0; k < D; ++k) { | |
| 250 | 411136 | dWrow[k] += dov * Yi[k]; | |
| 251 | 411136 | } | |
| 252 | 7360 | } | |
| 253 | 148 | } | |
| 254 | |||
| 255 | // dbo[c] += sum over valid rows of dO[i,c] (caller zeros). | ||
| 256 | 19 | if (dbo) { | |
| 257 | 4 | float* dbop = dbo->host_f32_mut(); | |
| 258 | 44 | for (int i = 0; i < K; ++i) { | |
| 259 | 40 | if (d_mask && d_mask[i] < 0.5f) continue; | |
| 260 | 28 | const float* dOi = dOp + static_cast<std::size_t>(i) * D; | |
| 261 | 1660 | for (int c = 0; c < D; ++c) { | |
| 262 | 1632 | dbop[c] += dOi[c]; | |
| 263 | 1632 | } | |
| 264 | 28 | } | |
| 265 | 4 | } | |
| 266 | |||
| 267 | // Per-head dAttn and dVh. | ||
| 268 | 90 | parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) { | |
| 269 | 71 | const int hh = static_cast<int>(hhi); | |
| 270 | 851 | for (int i = 0; i < K; ++i) { | |
| 271 | 10907 | for (int j = 0; j < K; ++j) { | |
| 272 | 10127 | float acc = 0.0f; | |
| 273 | 128286 | for (int k = 0; k < dh; ++k) { | |
| 274 | 118159 | const float dy = dYc[static_cast<std::size_t>(i) * D + (hh * dh + k)]; | |
| 275 | 118159 | const float vv = Vp[(static_cast<std::size_t>(hh) * K + j) * dh + k]; | |
| 276 | 118159 | acc += dy * vv; | |
| 277 | 118159 | } | |
| 278 | 10127 | dAttn[(static_cast<std::size_t>(hh) * K + i) * K + j] = acc; | |
| 279 | 10127 | } | |
| 280 | 780 | } | |
| 281 | 848 | for (int i = 0; i < K; ++i) { | |
| 282 | 777 | const float* Arow = Ap + (static_cast<std::size_t>(hh) * K + i) * K; | |
| 283 | 777 | const float* dYseg = dYc.data() + static_cast<std::size_t>(i) * D + hh * dh; | |
| 284 | 11046 | for (int j = 0; j < K; ++j) { | |
| 285 | 10269 | const float av = Arow[j]; | |
| 286 | 10269 | float* dVrow = dVh.data() + (static_cast<std::size_t>(hh) * K + j) * dh; | |
| 287 | 138526 | for (int k = 0; k < dh; ++k) { | |
| 288 | 128257 | dVrow[k] += av * dYseg[k]; | |
| 289 | 128257 | } | |
| 290 | 10269 | } | |
| 291 | 777 | } | |
| 292 | 71 | }); | |
| 293 | |||
| 294 | // Per-head softmax backward → dScores. | ||
| 295 | 89 | parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) { | |
| 296 | 70 | const int hh = static_cast<int>(hhi); | |
| 297 | 842 | for (int i = 0; i < K; ++i) { | |
| 298 | 772 | const float* prow = Ap + (static_cast<std::size_t>(hh) * K + i) * K; | |
| 299 | 772 | const float* dprow = dAttn.data() + (static_cast<std::size_t>(hh) * K + i) * K; | |
| 300 | 772 | float* drow = dScores.data() + (static_cast<std::size_t>(hh) * K + i) * K; | |
| 301 | 772 | if (d_mask && d_mask[i] < 0.5f) { | |
| 302 | 2676 | for (int j = 0; j < K; ++j) drow[j] = 0.0f; | |
| 303 | 176 | continue; | |
| 304 | } | ||
| 305 | 596 | float dot = 0.0f; | |
| 306 | 8260 | for (int j = 0; j < K; ++j) dot += dprow[j] * prow[j]; | |
| 307 | 8182 | for (int j = 0; j < K; ++j) { | |
| 308 | 7586 | if (d_mask && d_mask[j] < 0.5f) drow[j] = 0.0f; | |
| 309 | 6509 | else drow[j] = prow[j] * (dprow[j] - dot) * inv_sqrtdh; | |
| 310 | 7586 | } | |
| 311 | 596 | } | |
| 312 | 70 | }); | |
| 313 | |||
| 314 | // Per-head dQh and dKh. | ||
| 315 | 90 | parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) { | |
| 316 | 71 | const int hh = static_cast<int>(hhi); | |
| 317 | 849 | for (int i = 0; i < K; ++i) { | |
| 318 | 9745 | for (int k = 0; k < dh; ++k) { | |
| 319 | 8967 | float acc = 0.0f; | |
| 320 | 117729 | for (int j = 0; j < K; ++j) { | |
| 321 | 108762 | const float ds = dScores[(static_cast<std::size_t>(hh) * K + i) * K + j]; | |
| 322 | 108762 | const float kk = Kp[(static_cast<std::size_t>(hh) * K + j) * dh + k]; | |
| 323 | 108762 | acc += ds * kk; | |
| 324 | 108762 | } | |
| 325 | 8967 | dQh[(static_cast<std::size_t>(hh) * K + i) * dh + k] = acc; | |
| 326 | 8967 | } | |
| 327 | 778 | } | |
| 328 | 850 | for (int i = 0; i < K; ++i) { | |
| 329 | 779 | const float* dSrow = dScores.data() + (static_cast<std::size_t>(hh) * K + i) * K; | |
| 330 | 779 | const float* Qseg = Qp + (static_cast<std::size_t>(hh) * K + i) * dh; | |
| 331 | 10993 | for (int j = 0; j < K; ++j) { | |
| 332 | 10214 | const float ds = dSrow[j]; | |
| 333 | 10214 | float* dKrow = dKh.data() + (static_cast<std::size_t>(hh) * K + j) * dh; | |
| 334 | 133391 | for (int k = 0; k < dh; ++k) { | |
| 335 | 123177 | dKrow[k] += ds * Qseg[k]; | |
| 336 | 123177 | } | |
| 337 | 10214 | } | |
| 338 | 779 | } | |
| 339 | 71 | }); | |
| 340 | |||
| 341 | // dbq/dbk/dbv accumulate: db[hh*dh+j] += sum_i dQh/dKh/dVh(hh,i,j). | ||
| 342 | 19 | if (dbq || dbk || dbv) { | |
| 343 | 4 | float* dbqp = dbq ? dbq->host_f32_mut() : nullptr; | |
| 344 | 4 | float* dbkp = dbk ? dbk->host_f32_mut() : nullptr; | |
| 345 | 4 | float* dbvp = dbv ? dbv->host_f32_mut() : nullptr; | |
| 346 | 16 | parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) { | |
| 347 | 12 | const int hh = static_cast<int>(hhi); | |
| 348 | 12 | float* dbq_row = dbqp ? dbqp + hh * dh : nullptr; | |
| 349 | 12 | float* dbk_row = dbkp ? dbkp + hh * dh : nullptr; | |
| 350 | 12 | float* dbv_row = dbvp ? dbvp + hh * dh : nullptr; | |
| 351 | 124 | for (int i = 0; i < K; ++i) { | |
| 352 | 140 | const std::size_t base = (static_cast<std::size_t>(hh) * K + i) * dh; | |
| 353 | 1410 | for (int j = 0; j < dh; ++j) { | |
| 354 | 1298 | if (dbq_row) dbq_row[j] += dQh[base + j]; | |
| 355 | 1298 | if (dbk_row) dbk_row[j] += dKh[base + j]; | |
| 356 | 1270 | if (dbv_row) dbv_row[j] += dVh[base + j]; | |
| 357 | 1270 | } | |
| 358 | 112 | } | |
| 359 | 16 | }); | |
| 360 | 4 | } | |
| 361 | |||
| 362 | // dWq/dWk/dWv accumulate: dW(hh*dh+j, k) += sum_i dQh(hh,i,j) * X(i,k). | ||
| 363 | 89 | parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) { | |
| 364 | 70 | const int hh = static_cast<int>(hhi); | |
| 365 | 848 | for (int i = 0; i < K; ++i) { | |
| 366 | 778 | const float* Xi = Xp + static_cast<std::size_t>(i) * D; | |
| 367 | 778 | const std::size_t base = (static_cast<std::size_t>(hh) * K + i) * dh; | |
| 368 | 10164 | for (int j = 0; j < dh; ++j) { | |
| 369 | 9386 | const int wrow = hh * dh + j; | |
| 370 | 9386 | const float qv = dQh[base + j]; | |
| 371 | 9386 | const float kv = dKh[base + j]; | |
| 372 | 9386 | const float vv = dVh[base + j]; | |
| 373 | 9386 | float* dWqrow = dWqp + static_cast<std::size_t>(wrow) * D; | |
| 374 | 9386 | float* dWkrow = dWkp + static_cast<std::size_t>(wrow) * D; | |
| 375 | 9386 | float* dWvrow = dWvp + static_cast<std::size_t>(wrow) * D; | |
| 376 | 467179 | for (int k = 0; k < D; ++k) { | |
| 377 | 457793 | const float xv = Xi[k]; | |
| 378 | 457793 | dWqrow[k] += qv * xv; | |
| 379 | 457793 | dWkrow[k] += kv * xv; | |
| 380 | 457793 | dWvrow[k] += vv * xv; | |
| 381 | 457793 | } | |
| 382 | 9386 | } | |
| 383 | 778 | } | |
| 384 | 70 | }); | |
| 385 | |||
| 386 | // dX(i,k) = sum over heads, j of dQh*Wq + dKh*Wk + dVh*Wv at (hh*dh+j, k). | ||
| 387 | 205 | parallel_for(static_cast<std::size_t>(K), [&](std::size_t ii) { | |
| 388 | 186 | const int i = static_cast<int>(ii); | |
| 389 | 9575 | for (int k = 0; k < D; ++k) { | |
| 390 | 9389 | float acc = 0.0f; | |
| 391 | 49988 | for (int hh = 0; hh < H; ++hh) { | |
| 392 | 434171 | for (int j = 0; j < dh; ++j) { | |
| 393 | 393572 | const int wrow = hh * dh + j; | |
| 394 | 393572 | const std::size_t widx = static_cast<std::size_t>(wrow) * D + k; | |
| 395 | 393572 | const float gq = dQh[(static_cast<std::size_t>(hh) * K + i) * dh + j]; | |
| 396 | 393572 | const float gk = dKh[(static_cast<std::size_t>(hh) * K + i) * dh + j]; | |
| 397 | 393572 | const float gv = dVh[(static_cast<std::size_t>(hh) * K + i) * dh + j]; | |
| 398 | 393572 | acc += gq * Wqp[widx] + gk * Wkp[widx] + gv * Wvp[widx]; | |
| 399 | 393572 | } | |
| 400 | 40599 | } | |
| 401 | 9389 | dXp[static_cast<std::size_t>(i) * D + k] = acc; | |
| 402 | 9389 | } | |
| 403 | 186 | }); | |
| 404 | 19 | } | |
| 405 | |||
| 406 | // ─── Concat / split ──────────────────────────────────────────────────────── | ||
| 407 | |||
| 408 | 2 | void concat_rows(const std::vector<const ::brotensor::Tensor*>& parts, | |
| 409 | ::brotensor::Tensor& out) { | ||
| 410 | 2 | int total = 0; | |
| 411 | 8 | for (const auto* p : parts) { | |
| 412 | 6 | if (p) total += p->size(); | |
| 413 | } | ||
| 414 | 2 | if (out.rows != total || out.cols != 1 || | |
| 415 | ✗ | out.dtype != ::brotensor::Dtype::FP32) { | |
| 416 | 2 | out.resize(total, 1, ::brotensor::Dtype::FP32); | |
| 417 | 2 | } | |
| 418 | 2 | if (total == 0) return; | |
| 419 | 2 | float* op = out.host_f32_mut(); | |
| 420 | 2 | int off = 0; | |
| 421 | 8 | for (const auto* p : parts) { | |
| 422 | 6 | if (!p) continue; | |
| 423 | 6 | const int n = p->size(); | |
| 424 | 6 | if (n == 0) continue; | |
| 425 | 6 | const float* pp = p->host_f32(); | |
| 426 | 71686 | for (int i = 0; i < n; ++i) op[off + i] = pp[i]; | |
| 427 | 6 | off += n; | |
| 428 | } | ||
| 429 | 2 | } | |
| 430 | |||
| 431 | ✗ | void split_rows(const ::brotensor::Tensor& in, | |
| 432 | const std::vector<::brotensor::Tensor*>& parts) { | ||
| 433 | ✗ | const float* ip = in.host_f32(); | |
| 434 | ✗ | int off = 0; | |
| 435 | ✗ | for (auto* p : parts) { | |
| 436 | ✗ | if (!p) continue; | |
| 437 | ✗ | const int n = p->size(); | |
| 438 | ✗ | if (n == 0) continue; | |
| 439 | ✗ | float* pp = p->host_f32_mut(); | |
| 440 | ✗ | for (int i = 0; i < n; ++i) pp[i] = ip[off + i]; | |
| 441 | ✗ | off += n; | |
| 442 | } | ||
| 443 | ✗ | } | |
| 444 | |||
| 445 | // ─── Batched elementwise / linear ────────────────────────────────────────── | ||
| 446 | |||
| 447 | ✗ | void add_inplace_batched(::brotensor::Tensor& Y_BD, | |
| 448 | const ::brotensor::Tensor& X_BD) { | ||
| 449 | ✗ | const int n = Y_BD.size(); | |
| 450 | assert(X_BD.size() == n); | ||
| 451 | ✗ | float* yp = Y_BD.host_f32_mut(); | |
| 452 | ✗ | const float* xp = X_BD.host_f32(); | |
| 453 | ✗ | for (int i = 0; i < n; ++i) yp[i] += xp[i]; | |
| 454 | ✗ | } | |
| 455 | |||
| 456 | 59 | void linear_forward_batched(const ::brotensor::Tensor& W, | |
| 457 | const ::brotensor::Tensor& bias, | ||
| 458 | const ::brotensor::Tensor& X_BD, | ||
| 459 | ::brotensor::Tensor& Y_BD) { | ||
| 460 | 59 | const int out_dim = W.rows; | |
| 461 | 59 | const int in_dim = W.cols; | |
| 462 | 59 | const int B = X_BD.rows; | |
| 463 | 59 | if (Y_BD.rows != B || Y_BD.cols != out_dim || | |
| 464 | 13 | Y_BD.dtype != ::brotensor::Dtype::FP32) { | |
| 465 | 46 | Y_BD.resize(B, out_dim, ::brotensor::Dtype::FP32); | |
| 466 | 46 | } | |
| 467 | 59 | if (B == 0 || out_dim == 0) return; | |
| 468 | |||
| 469 | 59 | const float* bp = bias.host_f32(); | |
| 470 | 59 | const float* Xp = X_BD.host_f32(); | |
| 471 | 59 | float* Yp = Y_BD.host_f32_mut(); | |
| 472 | |||
| 473 | // 16-bit weights (FP32 activations): widen per element. | ||
| 474 | 59 | if (W.dtype == ::brotensor::Dtype::FP16 || | |
| 475 | 56 | W.dtype == ::brotensor::Dtype::BF16) { | |
| 476 | 8 | const bool bf = (W.dtype == ::brotensor::Dtype::BF16); | |
| 477 | 8 | const std::uint16_t* Wp = static_cast<const std::uint16_t*>(W.data); | |
| 478 | 284 | for (int b = 0; b < B; ++b) { | |
| 479 | 276 | const float* xr = Xp + static_cast<std::size_t>(b) * in_dim; | |
| 480 | 276 | float* yr = Yp + static_cast<std::size_t>(b) * out_dim; | |
| 481 | 26858 | for (int i = 0; i < out_dim; ++i) { | |
| 482 | 26582 | const std::uint16_t* wr = Wp + static_cast<std::size_t>(i) * in_dim; | |
| 483 | 26582 | float acc = bp[i]; | |
| 484 | 2218912 | for (int j = 0; j < in_dim; ++j) { | |
| 485 | 2192330 | const float w = bf ? ::brotensor::bf16_bits_to_fp32(wr[j]) | |
| 486 | 1094584 | : ::brotensor::fp16_bits_to_fp32(wr[j]); | |
| 487 | 2192330 | acc += w * xr[j]; | |
| 488 | 2192330 | } | |
| 489 | 26582 | yr[i] = acc; | |
| 490 | 26582 | } | |
| 491 | 276 | } | |
| 492 | 8 | return; | |
| 493 | } | ||
| 494 | |||
| 495 | 51 | const float* Wp = W.host_f32(); | |
| 496 | 5475 | for (int b = 0; b < B; ++b) { | |
| 497 | 5424 | const float* xr = Xp + static_cast<std::size_t>(b) * in_dim; | |
| 498 | 5424 | float* yr = Yp + static_cast<std::size_t>(b) * out_dim; | |
| 499 | 4631269 | for (int i = 0; i < out_dim; ++i) { | |
| 500 | 4625845 | const float* wr = Wp + static_cast<std::size_t>(i) * in_dim; | |
| 501 | 4625845 | float acc = bp[i]; | |
| 502 | 4482736966 | for (int j = 0; j < in_dim; ++j) acc += wr[j] * xr[j]; | |
| 503 | 4625845 | yr[i] = acc; | |
| 504 | 4625845 | } | |
| 505 | 5424 | } | |
| 506 | 59 | } | |
| 507 | |||
| 508 | 26 | void linear_backward_batched(const ::brotensor::Tensor& W, | |
| 509 | const ::brotensor::Tensor& X_BD, | ||
| 510 | const ::brotensor::Tensor& dY_BD, | ||
| 511 | ::brotensor::Tensor& dX_BD, | ||
| 512 | ::brotensor::Tensor& dW, | ||
| 513 | ::brotensor::Tensor& dB) { | ||
| 514 | 26 | const int out_dim = W.rows; | |
| 515 | 26 | const int in_dim = W.cols; | |
| 516 | 26 | const int B = X_BD.rows; | |
| 517 | 26 | if (dX_BD.rows != B || dX_BD.cols != in_dim || | |
| 518 | 3 | dX_BD.dtype != ::brotensor::Dtype::FP32) { | |
| 519 | 23 | dX_BD.resize(B, in_dim, ::brotensor::Dtype::FP32); | |
| 520 | 23 | } | |
| 521 | 26 | if (B == 0) return; | |
| 522 | |||
| 523 | 26 | const float* Wp = W.host_f32(); | |
| 524 | 26 | const float* Xp = X_BD.host_f32(); | |
| 525 | 26 | const float* dYp = dY_BD.host_f32(); | |
| 526 | 26 | float* dXp = dX_BD.host_f32_mut(); | |
| 527 | 26 | float* dWp = dW.host_f32_mut(); | |
| 528 | 26 | float* dBp = dB.host_f32_mut(); | |
| 529 | |||
| 530 | // dX[b] = W^T * dY[b] (overwrite). | ||
| 531 | 2598 | for (int b = 0; b < B; ++b) { | |
| 532 | 2572 | const float* dyr = dYp + static_cast<std::size_t>(b) * out_dim; | |
| 533 | 2572 | float* dxr = dXp + static_cast<std::size_t>(b) * in_dim; | |
| 534 | 980141 | for (int j = 0; j < in_dim; ++j) dxr[j] = 0.0f; | |
| 535 | 1221878 | for (int i = 0; i < out_dim; ++i) { | |
| 536 | 1219306 | const float g = dyr[i]; | |
| 537 | 1219306 | const float* wr = Wp + static_cast<std::size_t>(i) * in_dim; | |
| 538 | 305332712 | for (int j = 0; j < in_dim; ++j) dxr[j] += wr[j] * g; | |
| 539 | 1219306 | } | |
| 540 | 2572 | } | |
| 541 | // dW += sum_b dY[b] * X[b]^T; dB += sum_b dY[b] (accumulate). | ||
| 542 | 2598 | for (int b = 0; b < B; ++b) { | |
| 543 | 2572 | const float* dyr = dYp + static_cast<std::size_t>(b) * out_dim; | |
| 544 | 2572 | const float* xr = Xp + static_cast<std::size_t>(b) * in_dim; | |
| 545 | 1221878 | for (int i = 0; i < out_dim; ++i) { | |
| 546 | 1219306 | const float g = dyr[i]; | |
| 547 | 1219306 | float* dwr = dWp + static_cast<std::size_t>(i) * in_dim; | |
| 548 | 305332712 | for (int j = 0; j < in_dim; ++j) dwr[j] += g * xr[j]; | |
| 549 | 1219306 | dBp[i] += g; | |
| 550 | 1219306 | } | |
| 551 | 2572 | } | |
| 552 | 26 | } | |
| 553 | |||
| 554 | ✗ | void relu_forward_batched(const ::brotensor::Tensor& X_BD, | |
| 555 | ::brotensor::Tensor& Y_BD) { | ||
| 556 | ✗ | ensure_match(X_BD, Y_BD); | |
| 557 | ✗ | const int n = X_BD.size(); | |
| 558 | ✗ | const float* xp = X_BD.host_f32(); | |
| 559 | ✗ | float* yp = Y_BD.host_f32_mut(); | |
| 560 | ✗ | for (int i = 0; i < n; ++i) yp[i] = xp[i] > 0.0f ? xp[i] : 0.0f; | |
| 561 | ✗ | } | |
| 562 | |||
| 563 | 3 | void relu_backward_batched(const ::brotensor::Tensor& X_BD, | |
| 564 | const ::brotensor::Tensor& dY_BD, | ||
| 565 | ::brotensor::Tensor& dX_BD) { | ||
| 566 | 3 | ensure_match(X_BD, dX_BD); | |
| 567 | 3 | const int n = X_BD.size(); | |
| 568 | 3 | const float* xp = X_BD.host_f32(); | |
| 569 | 3 | const float* dYp = dY_BD.host_f32(); | |
| 570 | 3 | float* dXp = dX_BD.host_f32_mut(); | |
| 571 | 2595 | for (int i = 0; i < n; ++i) dXp[i] = xp[i] > 0.0f ? dYp[i] : 0.0f; | |
| 572 | 3 | } | |
| 573 | |||
| 574 | 3 | void tanh_forward_batched(const ::brotensor::Tensor& X_BD, | |
| 575 | ::brotensor::Tensor& Y_BD) { | ||
| 576 | 3 | ensure_match(X_BD, Y_BD); | |
| 577 | 3 | const int n = X_BD.size(); | |
| 578 | 3 | const float* xp = X_BD.host_f32(); | |
| 579 | 3 | float* yp = Y_BD.host_f32_mut(); | |
| 580 | 2595 | for (int i = 0; i < n; ++i) yp[i] = std::tanh(xp[i]); | |
| 581 | 3 | } | |
| 582 | |||
| 583 | 4 | void tanh_backward_batched(const ::brotensor::Tensor& Y_BD, | |
| 584 | const ::brotensor::Tensor& dY_BD, | ||
| 585 | ::brotensor::Tensor& dX_BD) { | ||
| 586 | 4 | ensure_match(Y_BD, dX_BD); | |
| 587 | 4 | const int n = Y_BD.size(); | |
| 588 | 4 | const float* yp = Y_BD.host_f32(); | |
| 589 | 4 | const float* dYp = dY_BD.host_f32(); | |
| 590 | 4 | float* dXp = dX_BD.host_f32_mut(); | |
| 591 | 2626 | for (int i = 0; i < n; ++i) { | |
| 592 | 2622 | const float yc = std::clamp(yp[i], -1.0f, 1.0f); | |
| 593 | 2622 | dXp[i] = dYp[i] * (1.0f - yc * yc); | |
| 594 | 2622 | } | |
| 595 | 4 | } | |
| 596 | |||
| 597 | // ─── Batched per-sample losses ───────────────────────────────────────────── | ||
| 598 | |||
| 599 | 3 | void mse_vec_per_sample(const ::brotensor::Tensor& pred, | |
| 600 | const ::brotensor::Tensor& target, | ||
| 601 | ::brotensor::Tensor& dPred, | ||
| 602 | ::brotensor::Tensor& loss_per_sample) { | ||
| 603 | 3 | const int B = pred.size(); | |
| 604 | 3 | if (dPred.rows != pred.rows || dPred.cols != pred.cols || | |
| 605 | 3 | dPred.dtype != ::brotensor::Dtype::FP32) { | |
| 606 | ✗ | dPred.resize(pred.rows, pred.cols, ::brotensor::Dtype::FP32); | |
| 607 | ✗ | } | |
| 608 | 3 | if (loss_per_sample.rows != B || loss_per_sample.cols != 1 || | |
| 609 | 3 | loss_per_sample.dtype != ::brotensor::Dtype::FP32) { | |
| 610 | ✗ | loss_per_sample.resize(B, 1, ::brotensor::Dtype::FP32); | |
| 611 | ✗ | } | |
| 612 | 3 | if (B == 0) return; | |
| 613 | 3 | const float* pp = pred.host_f32(); | |
| 614 | 3 | const float* tp = target.host_f32(); | |
| 615 | 3 | float* dp = dPred.host_f32_mut(); | |
| 616 | 3 | float* lp = loss_per_sample.host_f32_mut(); | |
| 617 | 76 | for (int b = 0; b < B; ++b) { | |
| 618 | 73 | const float d = pp[b] - tp[b]; | |
| 619 | 73 | dp[b] = d; | |
| 620 | 73 | lp[b] = 0.5f * d * d; | |
| 621 | 73 | } | |
| 622 | 3 | } | |
| 623 | |||
| 624 | 6 | void softmax_xent_fused_batched(const ::brotensor::Tensor& logits_BL, | |
| 625 | const ::brotensor::Tensor& target_BL, | ||
| 626 | const float* d_mask_BL, | ||
| 627 | const int* d_head_offsets, | ||
| 628 | int n_heads, | ||
| 629 | ::brotensor::Tensor& probs_BL, | ||
| 630 | ::brotensor::Tensor& dLogits_BL, | ||
| 631 | ::brotensor::Tensor& loss_per_sample) { | ||
| 632 | 6 | const int B = logits_BL.rows; | |
| 633 | 6 | const int n_act = logits_BL.cols; | |
| 634 | 6 | if (probs_BL.rows != B || probs_BL.cols != n_act || | |
| 635 | 6 | probs_BL.dtype != ::brotensor::Dtype::FP32) { | |
| 636 | ✗ | probs_BL.resize(B, n_act, ::brotensor::Dtype::FP32); | |
| 637 | ✗ | } | |
| 638 | 6 | if (dLogits_BL.rows != B || dLogits_BL.cols != n_act || | |
| 639 | 6 | dLogits_BL.dtype != ::brotensor::Dtype::FP32) { | |
| 640 | ✗ | dLogits_BL.resize(B, n_act, ::brotensor::Dtype::FP32); | |
| 641 | ✗ | } | |
| 642 | 6 | if (loss_per_sample.rows != B || loss_per_sample.cols != 1 || | |
| 643 | 6 | loss_per_sample.dtype != ::brotensor::Dtype::FP32) { | |
| 644 | ✗ | loss_per_sample.resize(B, 1, ::brotensor::Dtype::FP32); | |
| 645 | ✗ | } | |
| 646 | 6 | if (B == 0 || n_act == 0 || n_heads <= 0) return; | |
| 647 | |||
| 648 | 6 | const float* lp = logits_BL.host_f32(); | |
| 649 | 6 | const float* tp = target_BL.host_f32(); | |
| 650 | 6 | float* pp = probs_BL.host_f32_mut(); | |
| 651 | 6 | float* dp = dLogits_BL.host_f32_mut(); | |
| 652 | 6 | float* lossp = loss_per_sample.host_f32_mut(); | |
| 653 | |||
| 654 | 40 | for (int b = 0; b < B; ++b) { | |
| 655 | // Overwrite loss before per-head accumulation. | ||
| 656 | 34 | float sample_loss = 0.0f; | |
| 657 | 34 | const int row_off = b * n_act; | |
| 658 | 103 | for (int h = 0; h < n_heads; ++h) { | |
| 659 | 69 | const int off = d_head_offsets[h]; | |
| 660 | 69 | const int end = d_head_offsets[h + 1]; | |
| 661 | 69 | const int len = end - off; | |
| 662 | 69 | const float* lr = lp + row_off + off; | |
| 663 | 69 | const float* tr = tp + row_off + off; | |
| 664 | 69 | const float* mr = d_mask_BL ? (d_mask_BL + row_off + off) : nullptr; | |
| 665 | 69 | float* pr = pp + row_off + off; | |
| 666 | 69 | float* dr = dp + row_off + off; | |
| 667 | |||
| 668 | 69 | float m = -1e30f; | |
| 669 | 2181 | for (int i = 0; i < len; ++i) { | |
| 670 | 2112 | if (mr && mr[i] < 0.5f) continue; | |
| 671 | 1584 | if (lr[i] > m) m = lr[i]; | |
| 672 | 1584 | } | |
| 673 | 69 | float sum = 0.0f; | |
| 674 | 2181 | for (int i = 0; i < len; ++i) { | |
| 675 | 2112 | if (mr && mr[i] < 0.5f) { pr[i] = 0.0f; continue; } | |
| 676 | 1584 | const float e = std::exp(lr[i] - m); | |
| 677 | 1584 | pr[i] = e; | |
| 678 | 1584 | sum += e; | |
| 679 | 1584 | } | |
| 680 | 69 | const float inv = sum > 0.0f ? 1.0f / sum : 0.0f; | |
| 681 | 2181 | for (int i = 0; i < len; ++i) { | |
| 682 | 2112 | if (mr && mr[i] < 0.5f) { dr[i] = 0.0f; continue; } | |
| 683 | 1584 | const float p = pr[i] * inv; | |
| 684 | 1584 | pr[i] = p; | |
| 685 | 1584 | const float t = tr[i]; | |
| 686 | 1584 | if (t > 0.0f) { | |
| 687 | 1584 | const float pc = p > 1e-12f ? p : 1e-12f; | |
| 688 | 1584 | sample_loss -= t * std::log(pc); | |
| 689 | 1584 | } | |
| 690 | 1584 | dr[i] = p - t; | |
| 691 | 1584 | } | |
| 692 | 69 | } | |
| 693 | 34 | lossp[b] = sample_loss; | |
| 694 | 34 | } | |
| 695 | 6 | } | |
| 696 | |||
| 697 | 7 | void bce_with_logits_fused_batched(const ::brotensor::Tensor& logits_BL, | |
| 698 | const ::brotensor::Tensor& target_BL, | ||
| 699 | const float* d_mask_BL, | ||
| 700 | float pos_weight, | ||
| 701 | ::brotensor::Tensor& probs_BL, | ||
| 702 | ::brotensor::Tensor& dLogits_BL, | ||
| 703 | ::brotensor::Tensor& loss_per_sample) { | ||
| 704 | 7 | const int B = logits_BL.rows; | |
| 705 | 7 | const int L = logits_BL.cols; | |
| 706 | 7 | if (probs_BL.rows != B || probs_BL.cols != L || | |
| 707 | 7 | probs_BL.dtype != ::brotensor::Dtype::FP32) { | |
| 708 | ✗ | probs_BL.resize(B, L, ::brotensor::Dtype::FP32); | |
| 709 | ✗ | } | |
| 710 | 7 | if (dLogits_BL.rows != B || dLogits_BL.cols != L || | |
| 711 | 7 | dLogits_BL.dtype != ::brotensor::Dtype::FP32) { | |
| 712 | ✗ | dLogits_BL.resize(B, L, ::brotensor::Dtype::FP32); | |
| 713 | ✗ | } | |
| 714 | 7 | if (loss_per_sample.rows != B || loss_per_sample.cols != 1 || | |
| 715 | 7 | loss_per_sample.dtype != ::brotensor::Dtype::FP32) { | |
| 716 | ✗ | loss_per_sample.resize(B, 1, ::brotensor::Dtype::FP32); | |
| 717 | ✗ | } | |
| 718 | 7 | if (B == 0 || L == 0) return; | |
| 719 | |||
| 720 | 7 | const float* lp = logits_BL.host_f32(); | |
| 721 | 7 | const float* tp = target_BL.host_f32(); | |
| 722 | 7 | float* pp = probs_BL.host_f32_mut(); | |
| 723 | 7 | float* dp = dLogits_BL.host_f32_mut(); | |
| 724 | 7 | float* lossp = loss_per_sample.host_f32_mut(); | |
| 725 | 7 | const float w = pos_weight; | |
| 726 | |||
| 727 | 32 | for (int b = 0; b < B; ++b) { | |
| 728 | 25 | float sample_loss = 0.0f; | |
| 729 | 25 | const int row_off = b * L; | |
| 730 | 25 | const float* lr = lp + row_off; | |
| 731 | 25 | const float* tr = tp + row_off; | |
| 732 | 25 | const float* mr = d_mask_BL ? (d_mask_BL + row_off) : nullptr; | |
| 733 | 25 | float* pr = pp + row_off; | |
| 734 | 25 | float* dr = dp + row_off; | |
| 735 | 79 | for (int i = 0; i < L; ++i) { | |
| 736 | 54 | if (mr && mr[i] < 0.5f) { | |
| 737 | 17 | pr[i] = 0.0f; | |
| 738 | 17 | dr[i] = 0.0f; | |
| 739 | 17 | continue; | |
| 740 | } | ||
| 741 | 37 | const float z = lr[i]; | |
| 742 | 37 | const float y = tr[i]; | |
| 743 | // softplus(x) = max(x, 0) + log1p(exp(-|x|)) | ||
| 744 | 37 | const float az = std::fabs(z); | |
| 745 | 37 | const float sp_neg = (z > 0.0f ? 0.0f : -z) + std::log1p(std::exp(-az)); | |
| 746 | 37 | const float sp_pos = (z > 0.0f ? z : 0.0f) + std::log1p(std::exp(-az)); | |
| 747 | 37 | sample_loss += w * y * sp_neg + (1.0f - y) * sp_pos; | |
| 748 | // s = sigmoid(z), numerically stable. | ||
| 749 | 37 | const float s = z >= 0.0f | |
| 750 | 22 | ? 1.0f / (1.0f + std::exp(-z)) | |
| 751 | 15 | : std::exp(z) / (1.0f + std::exp(z)); | |
| 752 | 37 | pr[i] = s; | |
| 753 | 37 | dr[i] = s * (w * y + 1.0f - y) - w * y; | |
| 754 | 37 | } | |
| 755 | 25 | lossp[b] = sample_loss; | |
| 756 | 25 | } | |
| 757 | 7 | } | |
| 758 | |||
| 759 | // ─── Masked mean-pool ────────────────────────────────────────────────────── | ||
| 760 | |||
| 761 | 4 | void build_slot_mask(const ::brotensor::Tensor& x, int offset, int K, int stride, | |
| 762 | ::brotensor::Tensor& mask) { | ||
| 763 | 4 | if (mask.rows != K || mask.cols != 1 || | |
| 764 | ✗ | mask.dtype != ::brotensor::Dtype::FP32) { | |
| 765 | 4 | mask.resize(K, 1, ::brotensor::Dtype::FP32); | |
| 766 | 4 | } | |
| 767 | 4 | const float* xp = x.host_f32(); | |
| 768 | 4 | float* mp = mask.host_f32_mut(); | |
| 769 | 77 | for (int k = 0; k < K; ++k) { | |
| 770 | 73 | mp[k] = (xp[offset + static_cast<std::size_t>(k) * stride] > 0.5f) | |
| 771 | ? 1.0f : 0.0f; | ||
| 772 | 73 | } | |
| 773 | 4 | } | |
| 774 | |||
| 775 | 4 | void copy_d2d(const ::brotensor::Tensor& src, int src_off, | |
| 776 | ::brotensor::Tensor& dst, int dst_off, int n) { | ||
| 777 | 4 | const float* sp = src.host_f32(); | |
| 778 | 4 | float* dp = dst.host_f32_mut(); | |
| 779 | 4 | std::memcpy(dp + dst_off, sp + src_off, static_cast<std::size_t>(n) * sizeof(float)); | |
| 780 | 4 | } | |
| 781 | |||
| 782 | 4 | void copy_d2d_strided(const ::brotensor::Tensor& src, int src_off, int src_pitch, | |
| 783 | ::brotensor::Tensor& dst, int dst_off, int dst_pitch, | ||
| 784 | int width, int height) { | ||
| 785 | 4 | const float* sp = src.host_f32() + src_off; | |
| 786 | 4 | float* dp = dst.host_f32_mut() + dst_off; | |
| 787 | 88 | for (int r = 0; r < height; ++r) { | |
| 788 | 168 | std::memcpy(dp + static_cast<std::size_t>(r) * dst_pitch, | |
| 789 | 84 | sp + static_cast<std::size_t>(r) * src_pitch, | |
| 790 | 84 | static_cast<std::size_t>(width) * sizeof(float)); | |
| 791 | 84 | } | |
| 792 | 4 | } | |
| 793 | |||
| 794 | 6 | void masked_mean_pool_forward(const ::brotensor::Tensor& X, const float* d_mask, | |
| 795 | ::brotensor::Tensor& y) { | ||
| 796 | 6 | const int K = X.rows; | |
| 797 | 6 | const int D = X.cols; | |
| 798 | 6 | if (y.rows != D || y.cols != 1 || y.dtype != ::brotensor::Dtype::FP32) { | |
| 799 | 6 | y.resize(D, 1, ::brotensor::Dtype::FP32); | |
| 800 | 6 | } | |
| 801 | 6 | const float* Xp = X.host_f32(); | |
| 802 | 6 | float* yp = y.host_f32_mut(); | |
| 803 | 390 | for (int j = 0; j < D; ++j) yp[j] = 0.0f; | |
| 804 | 6 | int num_valid = 0; | |
| 805 | 168 | for (int k = 0; k < K; ++k) { | |
| 806 | 162 | if (d_mask && d_mask[k] < 0.5f) continue; | |
| 807 | 82 | const float* row = Xp + static_cast<std::size_t>(k) * D; | |
| 808 | 6882 | for (int j = 0; j < D; ++j) yp[j] += row[j]; | |
| 809 | 82 | ++num_valid; | |
| 810 | 82 | } | |
| 811 | 6 | if (num_valid > 0) { | |
| 812 | 5 | const float inv = 1.0f / static_cast<float>(num_valid); | |
| 813 | 357 | for (int j = 0; j < D; ++j) yp[j] *= inv; | |
| 814 | 5 | } | |
| 815 | 6 | } | |
| 816 | |||
| 817 | 4 | void masked_mean_pool_backward(const ::brotensor::Tensor& dY, const float* d_mask, | |
| 818 | int K, ::brotensor::Tensor& dX) { | ||
| 819 | 4 | const int D = dY.size(); | |
| 820 | 4 | if (dX.rows != K || dX.cols != D || dX.dtype != ::brotensor::Dtype::FP32) { | |
| 821 | 4 | dX.resize(K, D, ::brotensor::Dtype::FP32); | |
| 822 | 4 | } | |
| 823 | 4 | const float* dYp = dY.host_f32(); | |
| 824 | 4 | float* dXp = dX.host_f32_mut(); | |
| 825 | 4 | int num_valid = 0; | |
| 826 | 126 | for (int k = 0; k < K; ++k) { | |
| 827 | 122 | if (!d_mask || d_mask[k] >= 0.5f) ++num_valid; | |
| 828 | 122 | } | |
| 829 | 4 | const float inv = num_valid > 0 ? 1.0f / static_cast<float>(num_valid) : 0.0f; | |
| 830 | 126 | for (int k = 0; k < K; ++k) { | |
| 831 | 122 | float* row = dXp + static_cast<std::size_t>(k) * D; | |
| 832 | 122 | const bool valid = (!d_mask || d_mask[k] >= 0.5f) && num_valid > 0; | |
| 833 | 12186 | for (int j = 0; j < D; ++j) row[j] = valid ? dYp[j] * inv : 0.0f; | |
| 834 | 122 | } | |
| 835 | 4 | } | |
| 836 | |||
| 837 | } // namespace brotensor::detail::cpu | ||
| 838 |