src/cpu/layernorm_inference.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ─── CPU inference-batched layernorm + causal-mask helper (CHUNK 1) ──────── | ||
| 2 | // | ||
| 3 | // FP32 scalar host implementations. | ||
| 4 | // | ||
| 5 | // layernorm_forward_inference_batched — per-row LayerNorm over an (R, D) | ||
| 6 | // tensor. Ports src/cuda/layernorm.cu's | ||
| 7 | // layernorm_forward_inference_batched_kernel: mean/variance per row, | ||
| 8 | // rstd = 1/sqrt(var + eps), y = gamma * xhat + beta. FP32 only. | ||
| 9 | // | ||
| 10 | // build_causal_mask_row — fills an (L, 1) mask: mask[k] = (k <= q) ? 1 : 0. | ||
| 11 | // Ports src/cuda/elementwise.cu's causal_mask_row_kernel. Its only | ||
| 12 | // Tensor operand is the output; the dispatcher resolves the device | ||
| 13 | // from `mask` itself (see src/ops.cpp::build_causal_mask_row). | ||
| 14 | |||
| 15 | #include <brotensor/tensor.h> | ||
| 16 | #include <brotensor/detail/cpu/thread_pool.h> | ||
| 17 | #include "cpu_jit.h" | ||
| 18 | |||
| 19 | #include <algorithm> | ||
| 20 | #include <cmath> | ||
| 21 | #include <cstddef> | ||
| 22 | |||
| 23 | namespace brotensor::detail::cpu { | ||
| 24 | |||
| 25 | 394 | void layernorm_forward_inference_batched(const ::brotensor::Tensor& X_RD, | |
| 26 | const ::brotensor::Tensor& gamma, | ||
| 27 | const ::brotensor::Tensor* beta, | ||
| 28 | ::brotensor::Tensor& Y_RD, | ||
| 29 | float eps) { | ||
| 30 | 394 | const int R = X_RD.rows; | |
| 31 | 394 | const int D = X_RD.cols; | |
| 32 | 394 | if (Y_RD.rows != R || Y_RD.cols != D || | |
| 33 | 363 | Y_RD.dtype != ::brotensor::Dtype::FP32) { | |
| 34 | 31 | Y_RD.resize(R, D, ::brotensor::Dtype::FP32); | |
| 35 | 31 | } | |
| 36 | 394 | if (R == 0 || D == 0) return; | |
| 37 | |||
| 38 | 394 | const float* xp = X_RD.host_f32(); | |
| 39 | 394 | const float* gp = gamma.host_f32(); | |
| 40 | 394 | const float* bp = beta ? beta->host_f32() : nullptr; | |
| 41 | 394 | float* yp = Y_RD.host_f32_mut(); | |
| 42 | |||
| 43 | 394 | if (bp && jit::is_jit_available() && D >= 8) { | |
| 44 | 328 | jit::layernorm_forward_inference_batched(xp, gp, bp, eps, yp, R, D); | |
| 45 | 328 | return; | |
| 46 | } | ||
| 47 | |||
| 48 | 66 | const float invD = 1.0f / static_cast<float>(D); | |
| 49 | // Each row owns Y's row exclusively (gamma/beta are read-only shared), | ||
| 50 | // so this parallelizes across rows with no cross-thread writes. | ||
| 51 | 664 | parallel_for(static_cast<std::size_t>(R), [&](std::size_t rowi) { | |
| 52 | 598 | const int row = static_cast<int>(rowi); | |
| 53 | 598 | const float* xr = xp + static_cast<std::size_t>(row) * D; | |
| 54 | 598 | float* yr = yp + static_cast<std::size_t>(row) * D; | |
| 55 | |||
| 56 | // Two passes: mean, then the sum of squared deviations from it. | ||
| 57 | // E[x^2] - E[x]^2 is one pass but cancels catastrophically once a row's | ||
| 58 | // mean dominates its spread — a CLIP text row whose embedding sits near | ||
| 59 | // 395 has both terms at ~1.6e5 in FP32, so their difference is noise and | ||
| 60 | // lands negative about as often as not, making rstd NaN. Summing | ||
| 61 | // deviations keeps every term non-negative, so var cannot go negative. | ||
| 62 | 598 | float sum = 0.0f; | |
| 63 | 303185 | for (int i = 0; i < D; ++i) sum += xr[i]; | |
| 64 | 598 | const float mean = sum * invD; | |
| 65 | |||
| 66 | 598 | float sumsq = 0.0f; | |
| 67 | 302400 | for (int i = 0; i < D; ++i) { | |
| 68 | 301802 | const float d = xr[i] - mean; | |
| 69 | 301802 | sumsq += d * d; | |
| 70 | 301802 | } | |
| 71 | 598 | const float var = sumsq * invD; | |
| 72 | 598 | const float eps_safe = std::max(eps, 1e-12f); | |
| 73 | 598 | const float rstd = 1.0f / std::sqrt(std::max(var + eps_safe, 1e-12f)); | |
| 74 | |||
| 75 | 273356 | for (int i = 0; i < D; ++i) { | |
| 76 | 272758 | const float xh = (xr[i] - mean) * rstd; | |
| 77 | 272758 | yr[i] = bp ? (gp[i] * xh + bp[i]) : (gp[i] * xh); | |
| 78 | 272758 | } | |
| 79 | 598 | }); | |
| 80 | 394 | } | |
| 81 | |||
| 82 | 2 | void layernorm_forward_inference_batched_fp16(const ::brotensor::Tensor& X_RD, | |
| 83 | const ::brotensor::Tensor& gamma, | ||
| 84 | const ::brotensor::Tensor* beta, | ||
| 85 | ::brotensor::Tensor& Y_RD, | ||
| 86 | float eps) { | ||
| 87 | 2 | const int R = X_RD.rows; | |
| 88 | 2 | const int D = X_RD.cols; | |
| 89 | 2 | if (Y_RD.rows != R || Y_RD.cols != D || | |
| 90 | ✗ | Y_RD.dtype != ::brotensor::Dtype::FP16) { | |
| 91 | 2 | Y_RD.resize(R, D, ::brotensor::Dtype::FP16); | |
| 92 | 2 | } | |
| 93 | 2 | if (R == 0 || D == 0) return; | |
| 94 | |||
| 95 | 2 | const uint16_t* xp = X_RD.host_fp16(); | |
| 96 | 2 | const uint16_t* gp = gamma.host_fp16(); | |
| 97 | 2 | const uint16_t* bp = beta ? beta->host_fp16() : nullptr; | |
| 98 | 2 | uint16_t* yp = Y_RD.host_fp16_mut(); | |
| 99 | |||
| 100 | 2 | const float invD = 1.0f / static_cast<float>(D); | |
| 101 | 18 | parallel_for(static_cast<std::size_t>(R), [&](std::size_t rowi) { | |
| 102 | 16 | const int row = static_cast<int>(rowi); | |
| 103 | 16 | const uint16_t* xr = xp + static_cast<std::size_t>(row) * D; | |
| 104 | 16 | uint16_t* yr = yp + static_cast<std::size_t>(row) * D; | |
| 105 | |||
| 106 | 16 | float sum = 0.0f; | |
| 107 | 1040 | for (int i = 0; i < D; ++i) sum += ::brotensor::fp16_bits_to_fp32(xr[i]); | |
| 108 | 16 | const float mean = sum * invD; | |
| 109 | |||
| 110 | 16 | float sumsq = 0.0f; | |
| 111 | 1040 | for (int i = 0; i < D; ++i) { | |
| 112 | 1024 | const float d = ::brotensor::fp16_bits_to_fp32(xr[i]) - mean; | |
| 113 | 1024 | sumsq += d * d; | |
| 114 | 1024 | } | |
| 115 | 16 | const float var = sumsq * invD; | |
| 116 | 16 | const float eps_safe = std::max(eps, 1e-12f); | |
| 117 | 16 | const float rstd = 1.0f / std::sqrt(std::max(var + eps_safe, 1e-12f)); | |
| 118 | |||
| 119 | 1040 | for (int i = 0; i < D; ++i) { | |
| 120 | 1024 | const float xh = (::brotensor::fp16_bits_to_fp32(xr[i]) - mean) * rstd; | |
| 121 | 1024 | const float g = ::brotensor::fp16_bits_to_fp32(gp[i]); | |
| 122 | 1024 | const float b = bp ? ::brotensor::fp16_bits_to_fp32(bp[i]) : 0.0f; | |
| 123 | 1024 | yr[i] = ::brotensor::fp32_to_fp16_bits(g * xh + b); | |
| 124 | 1024 | } | |
| 125 | 16 | }); | |
| 126 | 2 | } | |
| 127 | |||
| 128 | 9 | void layernorm_forward_batched_with_caches(const ::brotensor::Tensor& X_RD, | |
| 129 | const ::brotensor::Tensor& gamma, | ||
| 130 | const ::brotensor::Tensor& beta, | ||
| 131 | ::brotensor::Tensor& Y_RD, | ||
| 132 | ::brotensor::Tensor& Xhat_RD, | ||
| 133 | ::brotensor::Tensor& Mean_R, | ||
| 134 | ::brotensor::Tensor& Rstd_R, | ||
| 135 | float eps) { | ||
| 136 | 9 | const int R = X_RD.rows; | |
| 137 | 9 | const int D = X_RD.cols; | |
| 138 | 9 | if (Y_RD.rows != R || Y_RD.cols != D || | |
| 139 | 1 | Y_RD.dtype != ::brotensor::Dtype::FP32) { | |
| 140 | 8 | Y_RD.resize(R, D, ::brotensor::Dtype::FP32); | |
| 141 | 8 | } | |
| 142 | 9 | if (Xhat_RD.rows != R || Xhat_RD.cols != D || | |
| 143 | 1 | Xhat_RD.dtype != ::brotensor::Dtype::FP32) { | |
| 144 | 8 | Xhat_RD.resize(R, D, ::brotensor::Dtype::FP32); | |
| 145 | 8 | } | |
| 146 | 9 | if (Mean_R.rows != R || Mean_R.cols != 1 || | |
| 147 | 1 | Mean_R.dtype != ::brotensor::Dtype::FP32) { | |
| 148 | 8 | Mean_R.resize(R, 1, ::brotensor::Dtype::FP32); | |
| 149 | 8 | } | |
| 150 | 9 | if (Rstd_R.rows != R || Rstd_R.cols != 1 || | |
| 151 | 1 | Rstd_R.dtype != ::brotensor::Dtype::FP32) { | |
| 152 | 8 | Rstd_R.resize(R, 1, ::brotensor::Dtype::FP32); | |
| 153 | 8 | } | |
| 154 | 9 | if (R == 0 || D == 0) return; | |
| 155 | |||
| 156 | 9 | const float* xp = X_RD.host_f32(); | |
| 157 | 9 | const float* gp = gamma.host_f32(); | |
| 158 | 9 | const float* bp = beta.host_f32(); | |
| 159 | 9 | float* yp = Y_RD.host_f32_mut(); | |
| 160 | 9 | float* hp = Xhat_RD.host_f32_mut(); | |
| 161 | 9 | float* mp = Mean_R.host_f32_mut(); | |
| 162 | 9 | float* sp = Rstd_R.host_f32_mut(); | |
| 163 | |||
| 164 | 9 | const float invD = 1.0f / static_cast<float>(D); | |
| 165 | // Each row owns its own slice of Y/Xhat/Mean/Rstd exclusively, so this | ||
| 166 | // parallelizes across rows with no cross-thread writes. | ||
| 167 | 137 | parallel_for(static_cast<std::size_t>(R), [&](std::size_t rowi) { | |
| 168 | 128 | const int row = static_cast<int>(rowi); | |
| 169 | 128 | const float* xr = xp + static_cast<std::size_t>(row) * D; | |
| 170 | 128 | float* yr = yp + static_cast<std::size_t>(row) * D; | |
| 171 | 128 | float* hr = hp + static_cast<std::size_t>(row) * D; | |
| 172 | |||
| 173 | // Two-pass variance, for the cancellation reason spelled out in | ||
| 174 | // layernorm_forward_inference_batched above. | ||
| 175 | 128 | float sum = 0.0f; | |
| 176 | 25738 | for (int i = 0; i < D; ++i) sum += xr[i]; | |
| 177 | 128 | const float mean = sum * invD; | |
| 178 | |||
| 179 | 128 | float sumsq = 0.0f; | |
| 180 | 25015 | for (int i = 0; i < D; ++i) { | |
| 181 | 24887 | const float d = xr[i] - mean; | |
| 182 | 24887 | sumsq += d * d; | |
| 183 | 24887 | } | |
| 184 | 128 | const float var = sumsq * invD; | |
| 185 | 128 | const float eps_safe = std::max(eps, 1e-12f); | |
| 186 | 128 | const float rstd = 1.0f / std::sqrt(std::max(var + eps_safe, 1e-12f)); | |
| 187 | |||
| 188 | 128 | mp[row] = mean; | |
| 189 | 128 | sp[row] = rstd; | |
| 190 | |||
| 191 | 25283 | for (int i = 0; i < D; ++i) { | |
| 192 | 25155 | const float xh = (xr[i] - mean) * rstd; | |
| 193 | 25155 | hr[i] = xh; | |
| 194 | 25155 | yr[i] = gp[i] * xh + bp[i]; | |
| 195 | 25155 | } | |
| 196 | 128 | }); | |
| 197 | 9 | } | |
| 198 | |||
| 199 | 9 | void layernorm_backward_batched_with_caches(const ::brotensor::Tensor& dY_RD, | |
| 200 | const ::brotensor::Tensor& Xhat_RD, | ||
| 201 | const ::brotensor::Tensor& gamma, | ||
| 202 | const ::brotensor::Tensor& Rstd_R, | ||
| 203 | ::brotensor::Tensor& dX_RD, | ||
| 204 | ::brotensor::Tensor& dGamma, | ||
| 205 | ::brotensor::Tensor& dBeta) { | ||
| 206 | 9 | const int R = dY_RD.rows; | |
| 207 | 9 | const int D = dY_RD.cols; | |
| 208 | 9 | if (dX_RD.rows != R || dX_RD.cols != D || | |
| 209 | 9 | dX_RD.dtype != ::brotensor::Dtype::FP32) { | |
| 210 | ✗ | dX_RD.resize(R, D, ::brotensor::Dtype::FP32); | |
| 211 | ✗ | } | |
| 212 | 9 | if (R == 0 || D == 0) return; | |
| 213 | |||
| 214 | 9 | const float* dyp = dY_RD.host_f32(); | |
| 215 | 9 | const float* hp = Xhat_RD.host_f32(); | |
| 216 | 9 | const float* gp = gamma.host_f32(); | |
| 217 | 9 | const float* sp = Rstd_R.host_f32(); | |
| 218 | 9 | float* dxp = dX_RD.host_f32_mut(); | |
| 219 | 9 | float* dgp = dGamma.host_f32_mut(); | |
| 220 | 9 | float* dbp = dBeta.host_f32_mut(); | |
| 221 | |||
| 222 | 9 | const float nf = static_cast<float>(D); | |
| 223 | |||
| 224 | // dGamma/dBeta accumulate dgp[i]/dbp[i] summed over every row — a shared | ||
| 225 | // reduction across the row axis, not disjoint per row. So only the dX | ||
| 226 | // pass (fully owned by its own row) is parallelized here; dGamma/dBeta | ||
| 227 | // are accumulated afterwards in a separate, single-threaded pass (cheap: | ||
| 228 | // dyr/hr are already materialized, no recomputation needed). | ||
| 229 | 136 | parallel_for(static_cast<std::size_t>(R), [&](std::size_t rowi) { | |
| 230 | 127 | const int row = static_cast<int>(rowi); | |
| 231 | 127 | const float* dyr = dyp + static_cast<std::size_t>(row) * D; | |
| 232 | 127 | const float* hr = hp + static_cast<std::size_t>(row) * D; | |
| 233 | 127 | float* dxr = dxp + static_cast<std::size_t>(row) * D; | |
| 234 | 127 | const float rstd = sp[row]; | |
| 235 | |||
| 236 | 127 | float sum_dxh = 0.0f; | |
| 237 | 127 | float sum_dxh_xhat = 0.0f; | |
| 238 | 24819 | for (int i = 0; i < D; ++i) { | |
| 239 | 24692 | const float dxh = dyr[i] * gp[i]; | |
| 240 | 24692 | sum_dxh += dxh; | |
| 241 | 24692 | sum_dxh_xhat += dxh * hr[i]; | |
| 242 | 24692 | } | |
| 243 | 127 | const float scale = rstd / nf; | |
| 244 | 24321 | for (int i = 0; i < D; ++i) { | |
| 245 | 24194 | const float dxh = dyr[i] * gp[i]; | |
| 246 | 24194 | dxr[i] = scale * (nf * dxh - sum_dxh - hr[i] * sum_dxh_xhat); | |
| 247 | 24194 | } | |
| 248 | 127 | }); | |
| 249 | |||
| 250 | // Single-threaded: dGamma/dBeta reduce across rows, so they cannot be | ||
| 251 | // split across threads without a race on dgp[i]/dbp[i]. | ||
| 252 | 137 | for (int row = 0; row < R; ++row) { | |
| 253 | 128 | const float* dyr = dyp + static_cast<std::size_t>(row) * D; | |
| 254 | 128 | const float* hr = hp + static_cast<std::size_t>(row) * D; | |
| 255 | 26345 | for (int i = 0; i < D; ++i) { | |
| 256 | 26217 | const float g = dyr[i]; | |
| 257 | 26217 | dgp[i] += g * hr[i]; | |
| 258 | 26217 | dbp[i] += g; | |
| 259 | 26217 | } | |
| 260 | 128 | } | |
| 261 | 9 | } | |
| 262 | |||
| 263 | 5 | void build_causal_mask_row(int L, int q, ::brotensor::Tensor& mask) { | |
| 264 | 5 | if (mask.rows != L || mask.cols != 1 || | |
| 265 | ✗ | mask.dtype != ::brotensor::Dtype::FP32) { | |
| 266 | 5 | mask.resize(L, 1, ::brotensor::Dtype::FP32); | |
| 267 | 5 | } | |
| 268 | 5 | if (L <= 0) return; | |
| 269 | 5 | float* mp = mask.host_f32_mut(); | |
| 270 | 330 | for (int k = 0; k < L; ++k) mp[k] = (k <= q) ? 1.0f : 0.0f; | |
| 271 | 5 | } | |
| 272 | |||
| 273 | } // namespace brotensor::detail::cpu | ||
| 274 |