src/cpu/bias_act.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ─── CPU bias_act (StyleGAN3) ─────────────────────────────────────────────── | ||
| 2 | // | ||
| 3 | // Fused per-channel bias + activation + gain + clamp. FP32 reference mirroring | ||
| 4 | // NVlabs `_bias_act_ref`. CPU is FP32-only. | ||
| 5 | // | ||
| 6 | // X: (N, C*HW) — HW is the flattened spatial size; channel c owns the | ||
| 7 | // contiguous block [c*HW, (c+1)*HW) within each row. | ||
| 8 | // b: (C,1) or null. act: 0 = linear, 1 = lrelu. clamp < 0 ⇒ no clamp. | ||
| 9 | // | ||
| 10 | // Forward: t = X + b[c]; y = act(t); y *= gain; if clamp>=0: clip(±clamp). | ||
| 11 | // Backward: recompute t; dt = dY*gain*act'(t)*(clamp active ? 0 : 1); | ||
| 12 | // dX = dt (overwrite); dB[c] += Σ dt (accumulate — caller zeros). | ||
| 13 | // | ||
| 14 | // The clamp gradient mask uses the PRE-clamp value y_pre = gain*act(t): where | ||
| 15 | // |y_pre| > clamp the output saturated, so the gradient is zero there. | ||
| 16 | |||
| 17 | #include <brotensor/tensor.h> | ||
| 18 | |||
| 19 | #include <cmath> | ||
| 20 | #include <stdexcept> | ||
| 21 | #include <string> | ||
| 22 | |||
| 23 | namespace brotensor::detail::cpu { | ||
| 24 | |||
| 25 | namespace { | ||
| 26 | |||
| 27 | constexpr int ACT_LINEAR = 0; | ||
| 28 | constexpr int ACT_LRELU = 1; | ||
| 29 | |||
| 30 | 135 | inline void check_fp32(const ::brotensor::Tensor& t, | |
| 31 | const char* op, const char* name) { | ||
| 32 |
1/2✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
|
135 | if (t.dtype != Dtype::FP32) { |
| 33 | ✗ | throw std::runtime_error(std::string("brotensor: ") + op + ": " + | |
| 34 | ✗ | name + " must be FP32 (CPU backend is " | |
| 35 | "FP32-only)"); | ||
| 36 | } | ||
| 37 | 135 | } | |
| 38 | |||
| 39 | 60 | inline void check_act(int act, const char* op) { | |
| 40 |
3/4✓ Branch 0 taken 32 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
|
60 | if (act != ACT_LINEAR && act != ACT_LRELU) { |
| 41 | ✗ | throw std::runtime_error(std::string("brotensor: ") + op + | |
| 42 | ": act must be 0 (linear) or 1 (lrelu)"); | ||
| 43 | } | ||
| 44 | 60 | } | |
| 45 | |||
| 46 | 6958 | inline float apply_act(float t, int act, float alpha) { | |
| 47 |
4/4✓ Branch 0 taken 5403 times.
✓ Branch 1 taken 1555 times.
✓ Branch 2 taken 2621 times.
✓ Branch 3 taken 2782 times.
|
6958 | if (act == ACT_LRELU) return t > 0.0f ? t : alpha * t; |
| 48 | 1555 | return t; // linear | |
| 49 | 6958 | } | |
| 50 | |||
| 51 | 4814 | inline float act_grad(float t, int act, float alpha) { | |
| 52 |
4/4✓ Branch 0 taken 3499 times.
✓ Branch 1 taken 1315 times.
✓ Branch 2 taken 1691 times.
✓ Branch 3 taken 1808 times.
|
4814 | if (act == ACT_LRELU) return t > 0.0f ? 1.0f : alpha; |
| 53 | 1315 | return 1.0f; // linear | |
| 54 | 4814 | } | |
| 55 | |||
| 56 | } // namespace | ||
| 57 | |||
| 58 | 30 | void bias_act_forward(const ::brotensor::Tensor& X, const ::brotensor::Tensor* b, | |
| 59 | int N, int C, int HW, int act, float alpha, | ||
| 60 | float gain, float clamp, ::brotensor::Tensor& Y) { | ||
| 61 | 30 | check_fp32(X, "bias_act_forward", "X"); | |
| 62 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | if (b) check_fp32(*b, "bias_act_forward", "b"); |
| 63 | 30 | check_act(act, "bias_act_forward"); | |
| 64 | 30 | const int cols = C * HW; | |
| 65 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (X.rows != N || X.cols != cols) { |
| 66 | ✗ | throw std::runtime_error("bias_act_forward: X shape mismatch"); | |
| 67 | } | ||
| 68 |
3/4✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
|
30 | if (b && b->size() != C) { |
| 69 | ✗ | throw std::runtime_error("bias_act_forward: b must have C elements"); | |
| 70 | } | ||
| 71 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
30 | if (Y.rows != N || Y.cols != cols || Y.dtype != Dtype::FP32) { |
| 72 | 30 | Y.resize(N, cols, Dtype::FP32); | |
| 73 | 30 | } | |
| 74 |
2/4✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
|
30 | if (N == 0 || cols == 0) return; |
| 75 | |||
| 76 | 30 | const float* Xp = X.host_f32(); | |
| 77 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | const float* Bp = b ? b->host_f32() : nullptr; |
| 78 | 30 | float* Yp = Y.host_f32_mut(); | |
| 79 | |||
| 80 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 72 times.
|
102 | for (int n = 0; n < N; ++n) { |
| 81 |
2/2✓ Branch 0 taken 254 times.
✓ Branch 1 taken 72 times.
|
326 | for (int c = 0; c < C; ++c) { |
| 82 |
2/2✓ Branch 0 taken 135 times.
✓ Branch 1 taken 119 times.
|
254 | const float bias_v = Bp ? Bp[c] : 0.0f; |
| 83 | 254 | const size_t base = (static_cast<size_t>(n) * C + c) * HW; | |
| 84 |
2/2✓ Branch 0 taken 4814 times.
✓ Branch 1 taken 254 times.
|
5068 | for (int k = 0; k < HW; ++k) { |
| 85 | 4814 | float y = apply_act(Xp[base + k] + bias_v, act, alpha) * gain; | |
| 86 |
2/2✓ Branch 0 taken 2670 times.
✓ Branch 1 taken 2144 times.
|
4814 | if (clamp >= 0.0f) { |
| 87 |
2/2✓ Branch 0 taken 219 times.
✓ Branch 1 taken 1925 times.
|
2144 | if (y < -clamp) y = -clamp; |
| 88 |
2/2✓ Branch 0 taken 1379 times.
✓ Branch 1 taken 546 times.
|
1925 | else if (y > clamp) y = clamp; |
| 89 | 2144 | } | |
| 90 | 4814 | Yp[base + k] = y; | |
| 91 | 4814 | } | |
| 92 | 254 | } | |
| 93 | 72 | } | |
| 94 | 30 | } | |
| 95 | |||
| 96 | 30 | void bias_act_backward(const ::brotensor::Tensor& dY, const ::brotensor::Tensor& X, | |
| 97 | const ::brotensor::Tensor* b, | ||
| 98 | int N, int C, int HW, int act, float alpha, | ||
| 99 | float gain, float clamp, | ||
| 100 | ::brotensor::Tensor& dX, ::brotensor::Tensor* dB) { | ||
| 101 | 30 | check_fp32(dY, "bias_act_backward", "dY"); | |
| 102 | 30 | check_fp32(X, "bias_act_backward", "X"); | |
| 103 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | if (b) check_fp32(*b, "bias_act_backward", "b"); |
| 104 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | if (dB) check_fp32(*dB, "bias_act_backward", "dB"); |
| 105 | 30 | check_act(act, "bias_act_backward"); | |
| 106 | 30 | const int cols = C * HW; | |
| 107 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (X.rows != N || X.cols != cols) { |
| 108 | ✗ | throw std::runtime_error("bias_act_backward: X shape mismatch"); | |
| 109 | } | ||
| 110 |
1/2✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 | if (dY.rows != N || dY.cols != cols) { |
| 111 | ✗ | throw std::runtime_error("bias_act_backward: dY shape mismatch"); | |
| 112 | } | ||
| 113 |
3/4✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
|
30 | if (b && b->size() != C) { |
| 114 | ✗ | throw std::runtime_error("bias_act_backward: b must have C elements"); | |
| 115 | } | ||
| 116 |
3/4✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
|
30 | if (dB && dB->size() != C) { |
| 117 | ✗ | throw std::runtime_error("bias_act_backward: dB must have C elements"); | |
| 118 | } | ||
| 119 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
30 | if (dX.rows != N || dX.cols != cols || dX.dtype != Dtype::FP32) { |
| 120 | 30 | dX.resize(N, cols, Dtype::FP32); | |
| 121 | 30 | } | |
| 122 |
2/4✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 30 times.
|
30 | if (N == 0 || cols == 0) return; |
| 123 | |||
| 124 | 30 | const float* dYp = dY.host_f32(); | |
| 125 | 30 | const float* Xp = X.host_f32(); | |
| 126 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | const float* Bp = b ? b->host_f32() : nullptr; |
| 127 | 30 | float* dXp = dX.host_f32_mut(); | |
| 128 |
2/2✓ Branch 0 taken 15 times.
✓ Branch 1 taken 15 times.
|
30 | float* dBp = dB ? dB->host_f32_mut() : nullptr; |
| 129 | |||
| 130 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 72 times.
|
102 | for (int n = 0; n < N; ++n) { |
| 131 |
2/2✓ Branch 0 taken 254 times.
✓ Branch 1 taken 72 times.
|
326 | for (int c = 0; c < C; ++c) { |
| 132 |
2/2✓ Branch 0 taken 135 times.
✓ Branch 1 taken 119 times.
|
254 | const float bias_v = Bp ? Bp[c] : 0.0f; |
| 133 | 254 | const size_t base = (static_cast<size_t>(n) * C + c) * HW; | |
| 134 | 254 | float db_acc = 0.0f; | |
| 135 |
2/2✓ Branch 0 taken 4814 times.
✓ Branch 1 taken 254 times.
|
5068 | for (int k = 0; k < HW; ++k) { |
| 136 | 4814 | const float t = Xp[base + k] + bias_v; | |
| 137 | 4814 | float dt = dYp[base + k] * gain * act_grad(t, act, alpha); | |
| 138 |
2/2✓ Branch 0 taken 2670 times.
✓ Branch 1 taken 2144 times.
|
4814 | if (clamp >= 0.0f) { |
| 139 | 2144 | const float y_pre = gain * apply_act(t, act, alpha); | |
| 140 |
4/4✓ Branch 0 taken 1925 times.
✓ Branch 1 taken 219 times.
✓ Branch 2 taken 546 times.
✓ Branch 3 taken 1379 times.
|
2144 | if (y_pre < -clamp || y_pre > clamp) dt = 0.0f; |
| 141 | 2144 | } | |
| 142 | 4814 | dXp[base + k] = dt; // overwrite | |
| 143 | 4814 | db_acc += dt; | |
| 144 | 4814 | } | |
| 145 |
2/2✓ Branch 0 taken 119 times.
✓ Branch 1 taken 135 times.
|
254 | if (dBp) dBp[c] += db_acc; // accumulate — caller zeros |
| 146 | 254 | } | |
| 147 | 72 | } | |
| 148 | 30 | } | |
| 149 | |||
| 150 | } // namespace brotensor::detail::cpu | ||
| 151 |