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 | 185 | inline void check_fp32(const ::brotensor::Tensor& t, | |
| 31 | const char* op, const char* name) { | ||
| 32 |
1/2✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
|
185 | 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 | 185 | } | |
| 38 | |||
| 39 | 84 | inline void check_act(int act, const char* op) { | |
| 40 |
3/4✓ Branch 0 taken 44 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
|
84 | 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 | 84 | } | |
| 45 | |||
| 46 | 46456 | inline float apply_act(float t, int act, float alpha) { | |
| 47 |
4/4✓ Branch 0 taken 39334 times.
✓ Branch 1 taken 7122 times.
✓ Branch 2 taken 17497 times.
✓ Branch 3 taken 21837 times.
|
46456 | if (act == ACT_LRELU) return t > 0.0f ? t : alpha * t; |
| 48 | 7122 | return t; // linear | |
| 49 | 46456 | } | |
| 50 | |||
| 51 | 27780 | inline float act_grad(float t, int act, float alpha) { | |
| 52 |
4/4✓ Branch 0 taken 20898 times.
✓ Branch 1 taken 6882 times.
✓ Branch 2 taken 9391 times.
✓ Branch 3 taken 11507 times.
|
27780 | if (act == ACT_LRELU) return t > 0.0f ? 1.0f : alpha; |
| 53 | 6882 | return 1.0f; // linear | |
| 54 | 27780 | } | |
| 55 | |||
| 56 | } // namespace | ||
| 57 | |||
| 58 | 42 | 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 | 42 | check_fp32(X, "bias_act_forward", "X"); | |
| 62 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
|
42 | if (b) check_fp32(*b, "bias_act_forward", "b"); |
| 63 | 42 | check_act(act, "bias_act_forward"); | |
| 64 | 42 | const int cols = C * HW; | |
| 65 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | 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 21 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
42 | 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 42 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
42 | if (Y.rows != N || Y.cols != cols || Y.dtype != Dtype::FP32) { |
| 72 | 42 | Y.resize(N, cols, Dtype::FP32); | |
| 73 | 42 | } | |
| 74 |
2/4✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 42 times.
|
42 | if (N == 0 || cols == 0) return; |
| 75 | |||
| 76 | 42 | const float* Xp = X.host_f32(); | |
| 77 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
|
42 | const float* Bp = b ? b->host_f32() : nullptr; |
| 78 | 42 | float* Yp = Y.host_f32_mut(); | |
| 79 | |||
| 80 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 84 times.
|
126 | for (int n = 0; n < N; ++n) { |
| 81 |
2/2✓ Branch 0 taken 286 times.
✓ Branch 1 taken 84 times.
|
370 | for (int c = 0; c < C; ++c) { |
| 82 |
2/2✓ Branch 0 taken 151 times.
✓ Branch 1 taken 135 times.
|
286 | const float bias_v = Bp ? Bp[c] : 0.0f; |
| 83 | 286 | const size_t base = (static_cast<size_t>(n) * C + c) * HW; | |
| 84 |
2/2✓ Branch 0 taken 27780 times.
✓ Branch 1 taken 286 times.
|
28066 | for (int k = 0; k < HW; ++k) { |
| 85 | 27780 | float y = apply_act(Xp[base + k] + bias_v, act, alpha) * gain; | |
| 86 |
2/2✓ Branch 0 taken 9104 times.
✓ Branch 1 taken 18676 times.
|
27780 | if (clamp >= 0.0f) { |
| 87 |
2/2✓ Branch 0 taken 1171 times.
✓ Branch 1 taken 17505 times.
|
18676 | if (y < -clamp) y = -clamp; |
| 88 |
2/2✓ Branch 0 taken 16078 times.
✓ Branch 1 taken 1427 times.
|
17505 | else if (y > clamp) y = clamp; |
| 89 | 18676 | } | |
| 90 | 27780 | Yp[base + k] = y; | |
| 91 | 27780 | } | |
| 92 | 286 | } | |
| 93 | 84 | } | |
| 94 | 42 | } | |
| 95 | |||
| 96 | 42 | 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 | 42 | check_fp32(dY, "bias_act_backward", "dY"); | |
| 102 | 42 | check_fp32(X, "bias_act_backward", "X"); | |
| 103 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
|
42 | if (b) check_fp32(*b, "bias_act_backward", "b"); |
| 104 |
2/2✓ Branch 0 taken 25 times.
✓ Branch 1 taken 17 times.
|
42 | if (dB) check_fp32(*dB, "bias_act_backward", "dB"); |
| 105 | 42 | check_act(act, "bias_act_backward"); | |
| 106 | 42 | const int cols = C * HW; | |
| 107 |
1/2✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
|
42 | 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 42 times.
✗ Branch 1 not taken.
|
42 | 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 21 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
|
42 | 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 17 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
|
42 | 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 42 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
42 | if (dX.rows != N || dX.cols != cols || dX.dtype != Dtype::FP32) { |
| 120 | 42 | dX.resize(N, cols, Dtype::FP32); | |
| 121 | 42 | } | |
| 122 |
2/4✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 42 times.
|
42 | if (N == 0 || cols == 0) return; |
| 123 | |||
| 124 | 42 | const float* dYp = dY.host_f32(); | |
| 125 | 42 | const float* Xp = X.host_f32(); | |
| 126 |
2/2✓ Branch 0 taken 21 times.
✓ Branch 1 taken 21 times.
|
42 | const float* Bp = b ? b->host_f32() : nullptr; |
| 127 | 42 | float* dXp = dX.host_f32_mut(); | |
| 128 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 25 times.
|
42 | float* dBp = dB ? dB->host_f32_mut() : nullptr; |
| 129 | |||
| 130 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 84 times.
|
126 | for (int n = 0; n < N; ++n) { |
| 131 |
2/2✓ Branch 0 taken 286 times.
✓ Branch 1 taken 84 times.
|
370 | for (int c = 0; c < C; ++c) { |
| 132 |
2/2✓ Branch 0 taken 151 times.
✓ Branch 1 taken 135 times.
|
286 | const float bias_v = Bp ? Bp[c] : 0.0f; |
| 133 | 286 | const size_t base = (static_cast<size_t>(n) * C + c) * HW; | |
| 134 | 286 | float db_acc = 0.0f; | |
| 135 |
2/2✓ Branch 0 taken 27780 times.
✓ Branch 1 taken 286 times.
|
28066 | for (int k = 0; k < HW; ++k) { |
| 136 | 27780 | const float t = Xp[base + k] + bias_v; | |
| 137 | 27780 | float dt = dYp[base + k] * gain * act_grad(t, act, alpha); | |
| 138 |
2/2✓ Branch 0 taken 9104 times.
✓ Branch 1 taken 18676 times.
|
27780 | if (clamp >= 0.0f) { |
| 139 | 18676 | const float y_pre = gain * apply_act(t, act, alpha); | |
| 140 |
4/4✓ Branch 0 taken 17505 times.
✓ Branch 1 taken 1171 times.
✓ Branch 2 taken 1427 times.
✓ Branch 3 taken 16078 times.
|
18676 | if (y_pre < -clamp || y_pre > clamp) dt = 0.0f; |
| 141 | 18676 | } | |
| 142 | 27780 | dXp[base + k] = dt; // overwrite | |
| 143 | 27780 | db_acc += dt; | |
| 144 | 27780 | } | |
| 145 |
2/2✓ Branch 0 taken 147 times.
✓ Branch 1 taken 139 times.
|
286 | if (dBp) dBp[c] += db_acc; // accumulate — caller zeros |
| 146 | 286 | } | |
| 147 | 84 | } | |
| 148 | 42 | } | |
| 149 | |||
| 150 | } // namespace brotensor::detail::cpu | ||
| 151 |