| Line |
Branch |
Exec |
Source |
| 1 |
|
|
#include <brotensor/backward_verify.h> |
| 2 |
|
|
#include <brotensor/ops.h> |
| 3 |
|
|
#include <brass/verify/bv_solver.hpp> |
| 4 |
|
|
|
| 5 |
|
|
#include <algorithm> |
| 6 |
|
|
#include <cmath> |
| 7 |
|
|
#include <cstdint> |
| 8 |
|
|
#include <cstring> |
| 9 |
|
|
#include <limits> |
| 10 |
|
|
#include <vector> |
| 11 |
|
|
|
| 12 |
|
|
namespace brotensor::verify { |
| 13 |
|
|
|
| 14 |
|
|
namespace { |
| 15 |
|
|
|
| 16 |
|
|
using brass::verify::BVContext; |
| 17 |
|
|
using brass::verify::BVNodeId; |
| 18 |
|
|
using brass::verify::BVOp; |
| 19 |
|
|
using brass::verify::SolveOutcome; |
| 20 |
|
|
using brass::verify::solve_nonzero; |
| 21 |
|
|
|
| 22 |
|
10 |
std::uint32_t float_to_bits(float f) noexcept { |
| 23 |
|
10 |
std::uint32_t u = 0; |
| 24 |
|
10 |
std::memcpy(&u, &f, sizeof(u)); |
| 25 |
|
10 |
return u; |
| 26 |
|
|
} |
| 27 |
|
|
|
| 28 |
|
5 |
bool prove_unsat(BVContext& ctx, BVNodeId violation, std::string& detail_out) { |
| 29 |
|
5 |
SolveOutcome res = solve_nonzero(ctx, violation); |
| 30 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
5 |
if (res.status == SolveOutcome::Unsat) { |
| 31 |
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
|
5 |
detail_out = "UNSAT (proven exact over 32-bit IEEE-754 bitvectors)"; |
| 32 |
|
5 |
return true; |
| 33 |
|
|
} |
| 34 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
if (res.status == SolveOutcome::Sat) { |
| 35 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
detail_out = "SAT counterexample found"; |
| 36 |
|
✗ |
return false; |
| 37 |
|
|
} |
| 38 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
detail_out = "Solver returned Unknown"; |
| 39 |
|
✗ |
return false; |
| 40 |
|
5 |
} |
| 41 |
|
|
|
| 42 |
|
14 |
bool tensor_has_no_nan(const Tensor& t) { |
| 43 |
|
14 |
const float* p = t.host_f32(); |
| 44 |
|
14 |
const int n = t.size(); |
| 45 |
2/2
✓ Branch 0 taken 510 times.
✓ Branch 1 taken 14 times.
|
524 |
for (int i = 0; i < n; ++i) { |
| 46 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 510 times.
|
510 |
if (std::isnan(p[i])) return false; |
| 47 |
|
510 |
} |
| 48 |
|
14 |
return true; |
| 49 |
|
14 |
} |
| 50 |
|
|
|
| 51 |
|
33 |
bool tensor_all_finite(const Tensor& t) { |
| 52 |
|
33 |
const float* p = t.host_f32(); |
| 53 |
|
33 |
const int n = t.size(); |
| 54 |
2/2
✓ Branch 0 taken 734 times.
✓ Branch 1 taken 33 times.
|
767 |
for (int i = 0; i < n; ++i) { |
| 55 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 734 times.
|
734 |
if (!std::isfinite(p[i])) return false; |
| 56 |
|
734 |
} |
| 57 |
|
33 |
return true; |
| 58 |
|
33 |
} |
| 59 |
|
|
|
| 60 |
|
|
} // namespace |
| 61 |
|
|
|
| 62 |
|
2 |
void sqrt_backward_clamped( |
| 63 |
|
|
const Tensor& x, |
| 64 |
|
|
const Tensor& dY, |
| 65 |
|
|
Tensor& dX, |
| 66 |
|
|
float eps) { |
| 67 |
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 |
const float eps_safe = (std::isnan(eps) || eps < 1e-12f) ? 1e-12f : eps; |
| 68 |
|
2 |
const int n = x.size(); |
| 69 |
|
2 |
dX.resize(x.rows, x.cols, Dtype::FP32); |
| 70 |
|
2 |
const float* xp = x.host_f32(); |
| 71 |
|
2 |
const float* dyp = dY.host_f32(); |
| 72 |
|
2 |
float* dxp = dX.host_f32_mut(); |
| 73 |
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 2 times.
|
38 |
for (int i = 0; i < n; ++i) { |
| 74 |
|
36 |
const float xv = xp[i]; |
| 75 |
|
36 |
const float dyv = dyp[i]; |
| 76 |
3/4
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 35 times.
|
36 |
if (std::isnan(xv) || std::isnan(dyv)) { |
| 77 |
|
1 |
dxp[i] = 0.0f; |
| 78 |
|
1 |
continue; |
| 79 |
|
|
} |
| 80 |
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 13 times.
|
35 |
const float x_clamped = xv < eps_safe ? eps_safe : xv; |
| 81 |
|
35 |
const float denom = 2.0f * std::sqrt(x_clamped); |
| 82 |
|
35 |
const float grad = dyv / denom; |
| 83 |
|
35 |
const float max_finite = std::numeric_limits<float>::max(); |
| 84 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
|
35 |
if (grad > max_finite) { |
| 85 |
|
✗ |
dxp[i] = max_finite; |
| 86 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
|
35 |
} else if (grad < -max_finite) { |
| 87 |
|
✗ |
dxp[i] = -max_finite; |
| 88 |
|
✗ |
} else { |
| 89 |
|
35 |
dxp[i] = grad; |
| 90 |
|
|
} |
| 91 |
|
35 |
} |
| 92 |
|
2 |
} |
| 93 |
|
|
|
| 94 |
|
1 |
BackwardProofSuiteReport prove_activation_backward_nan_freedom() { |
| 95 |
|
1 |
BackwardProofSuiteReport report; |
| 96 |
|
|
|
| 97 |
|
|
// 1. BVSolver proof: Clamping exponent input |z| <= 88.0f (0x42B00000) keeps |
| 98 |
|
|
// IEEE-754 float strictly below the single-precision expf overflow threshold |
| 99 |
|
|
// 88.722839f (0x42B17218), preventing 0 * Inf = NaN in SiLU/SwiGLU/QuickGELU/ELU. |
| 100 |
|
|
{ |
| 101 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
BVContext ctx; |
| 102 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto z_abs_bits = ctx.make_var("z_abs_bits", 32); |
| 103 |
|
1 |
const std::uint32_t kClamp88Bits = float_to_bits(88.0f); // 0x42B00000 |
| 104 |
|
1 |
const std::uint32_t kExpOverflowBits = float_to_bits(88.722839f); // 0x42B17218 |
| 105 |
|
|
|
| 106 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto pre = ctx.make_binary(BVOp::Ule, z_abs_bits, ctx.make_const(kClamp88Bits, 32)); |
| 107 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto exp_field = ctx.make_binary( |
| 108 |
|
|
BVOp::And, |
| 109 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
ctx.make_binary(BVOp::LShr, z_abs_bits, ctx.make_const(23, 32)), |
| 110 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
ctx.make_const(0xFF, 32)); |
| 111 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto is_nan_or_inf = ctx.make_binary(BVOp::Eq, exp_field, ctx.make_const(0xFF, 32)); |
| 112 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto overflows_exp = ctx.make_binary(BVOp::Uge, z_abs_bits, ctx.make_const(kExpOverflowBits, 32)); |
| 113 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto bad = ctx.make_binary(BVOp::Or, is_nan_or_inf, overflows_exp); |
| 114 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto violation = ctx.make_binary(BVOp::And, pre, bad); |
| 115 |
|
|
|
| 116 |
|
1 |
std::string detail; |
| 117 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
bool ok = prove_unsat(ctx, violation, detail); |
| 118 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
report.entries.push_back({ |
| 119 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
"activation_bwd.exp_clamp_88_no_overflow_bv", |
| 120 |
|
1 |
ok, |
| 121 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
detail |
| 122 |
|
|
}); |
| 123 |
|
1 |
} |
| 124 |
|
|
|
| 125 |
|
|
// 2. BVSolver proof: Clamping GELU/GeGLU input |x| <= 20.0f (0x41A00000) keeps |
| 126 |
|
|
// |x| strictly finite and well below cubic overflow threshold, preventing |
| 127 |
|
|
// 0 * Inf = NaN in sech^2(u) * du_dx. |
| 128 |
|
|
{ |
| 129 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
BVContext ctx; |
| 130 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto x_abs_bits = ctx.make_var("x_abs_bits", 32); |
| 131 |
|
1 |
const std::uint32_t kClamp20Bits = float_to_bits(20.0f); |
| 132 |
|
1 |
const std::uint32_t kTanhSatBits = float_to_bits(20.5f); |
| 133 |
|
|
|
| 134 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto pre = ctx.make_binary(BVOp::Ule, x_abs_bits, ctx.make_const(kClamp20Bits, 32)); |
| 135 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto exp_field = ctx.make_binary( |
| 136 |
|
|
BVOp::And, |
| 137 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
ctx.make_binary(BVOp::LShr, x_abs_bits, ctx.make_const(23, 32)), |
| 138 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
ctx.make_const(0xFF, 32)); |
| 139 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto is_nan_or_inf = ctx.make_binary(BVOp::Eq, exp_field, ctx.make_const(0xFF, 32)); |
| 140 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto exceeds_bound = ctx.make_binary(BVOp::Uge, x_abs_bits, ctx.make_const(kTanhSatBits, 32)); |
| 141 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto violation = ctx.make_binary( |
| 142 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
BVOp::And, pre, ctx.make_binary(BVOp::Or, is_nan_or_inf, exceeds_bound)); |
| 143 |
|
|
|
| 144 |
|
1 |
std::string detail; |
| 145 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
bool ok = prove_unsat(ctx, violation, detail); |
| 146 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
report.entries.push_back({ |
| 147 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
"activation_bwd.gelu_clamp_20_finite_bv", |
| 148 |
|
1 |
ok, |
| 149 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
detail |
| 150 |
|
|
}); |
| 151 |
|
1 |
} |
| 152 |
|
|
|
| 153 |
|
|
// 3. BVSolver proof: Clamping sqrt/log denominator x_clamped >= 1e-12f (0x2B8CBCCC) |
| 154 |
|
|
// and finite (<= FLT_MAX = 0x7F7FFFFF) guarantees strictly positive normal |
| 155 |
|
|
// IEEE-754 float (sign == 0, 1 <= exp <= 254), so sqrt(x_clamped) and 1/x_clamped |
| 156 |
|
|
// never divide by zero or take sqrt of a negative value. |
| 157 |
|
|
{ |
| 158 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
BVContext ctx; |
| 159 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto x_bits = ctx.make_var("x_bits", 32); |
| 160 |
|
1 |
const std::uint32_t kEpsBits = float_to_bits(1e-12f); |
| 161 |
|
1 |
const std::uint32_t kFltMaxBits = float_to_bits(std::numeric_limits<float>::max()); |
| 162 |
|
|
|
| 163 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto ge_eps = ctx.make_binary(BVOp::Uge, x_bits, ctx.make_const(kEpsBits, 32)); |
| 164 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto le_max = ctx.make_binary(BVOp::Ule, x_bits, ctx.make_const(kFltMaxBits, 32)); |
| 165 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto pre = ctx.make_binary(BVOp::And, ge_eps, le_max); |
| 166 |
|
|
|
| 167 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto sign_bit = ctx.make_binary(BVOp::LShr, x_bits, ctx.make_const(31, 32)); |
| 168 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto exp_field = ctx.make_binary( |
| 169 |
|
|
BVOp::And, |
| 170 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
ctx.make_binary(BVOp::LShr, x_bits, ctx.make_const(23, 32)), |
| 171 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
ctx.make_const(0xFF, 32)); |
| 172 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto is_neg = ctx.make_binary(BVOp::Ne, sign_bit, ctx.make_const(0, 32)); |
| 173 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto is_subnormal_or_zero = ctx.make_binary(BVOp::Eq, exp_field, ctx.make_const(0, 32)); |
| 174 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto is_nan_or_inf = ctx.make_binary(BVOp::Eq, exp_field, ctx.make_const(0xFF, 32)); |
| 175 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto bad = ctx.make_binary( |
| 176 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
BVOp::Or, is_neg, ctx.make_binary(BVOp::Or, is_subnormal_or_zero, is_nan_or_inf)); |
| 177 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto violation = ctx.make_binary(BVOp::And, pre, bad); |
| 178 |
|
|
|
| 179 |
|
1 |
std::string detail; |
| 180 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
bool ok = prove_unsat(ctx, violation, detail); |
| 181 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
report.entries.push_back({ |
| 182 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
"activation_bwd.sqrt_log_clamped_denom_normal_positive_bv", |
| 183 |
|
1 |
ok, |
| 184 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
detail |
| 185 |
|
|
}); |
| 186 |
|
1 |
} |
| 187 |
|
|
|
| 188 |
|
|
// 4. Exhaustive boundary-value sweep across all CPU activation backward kernels. |
| 189 |
|
|
{ |
| 190 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
const std::vector<float> test_vals = { |
| 191 |
|
|
0.0f, -0.0f, |
| 192 |
|
|
1e-38f, -1e-38f, // subnormal boundary |
| 193 |
|
|
1e-20f, -1e-20f, |
| 194 |
|
|
1e-7f, -1e-7f, |
| 195 |
|
|
0.5f, -0.5f, |
| 196 |
|
|
1.0f, -1.0f, |
| 197 |
|
|
20.0f, -20.0f, |
| 198 |
|
|
25.0f, -25.0f, |
| 199 |
|
|
50.0f, -50.0f, |
| 200 |
|
|
88.0f, -88.0f, // expf saturation boundary |
| 201 |
|
|
89.0f, -89.0f, |
| 202 |
|
|
100.0f, -100.0f, |
| 203 |
|
|
1e10f, -1e10f, |
| 204 |
|
|
1e20f, -1e20f, // extreme cubic overflow regime |
| 205 |
|
|
1e38f, -1e38f // near FLT_MAX |
| 206 |
|
|
}; |
| 207 |
|
1 |
const int n = static_cast<int>(test_vals.size()); |
| 208 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor x = Tensor::vec(n); |
| 209 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dy = Tensor::vec(n); |
| 210 |
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 30 times.
|
31 |
for (int i = 0; i < n; ++i) { |
| 211 |
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 |
x[i] = test_vals[static_cast<std::size_t>(i)]; |
| 212 |
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 |
dy[i] = 1.0f; |
| 213 |
|
30 |
} |
| 214 |
|
|
|
| 215 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dx = Tensor::vec(n); |
| 216 |
|
1 |
bool all_ok = true; |
| 217 |
|
1 |
std::string failed_op; |
| 218 |
|
|
|
| 219 |
|
12 |
auto check_op = [&](const char* name, auto&& fn) { |
| 220 |
11/22
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 time.
✗ Branch 13 not taken.
✓ Branch 14 taken 1 time.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 time.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 time.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 time.
✗ Branch 21 not taken.
|
11 |
if (!all_ok) return; |
| 221 |
|
11 |
dx.zero(); |
| 222 |
|
11 |
fn(); |
| 223 |
22/44
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 time.
✗ Branch 13 not taken.
✓ Branch 14 taken 1 time.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 time.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 time.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 time.
✗ Branch 21 not taken.
✓ Branch 22 taken 1 time.
✗ Branch 23 not taken.
✓ Branch 24 taken 1 time.
✗ Branch 25 not taken.
✓ Branch 26 taken 1 time.
✗ Branch 27 not taken.
✓ Branch 28 taken 1 time.
✗ Branch 29 not taken.
✓ Branch 30 taken 1 time.
✗ Branch 31 not taken.
✓ Branch 32 taken 1 time.
✗ Branch 33 not taken.
✓ Branch 34 taken 1 time.
✗ Branch 35 not taken.
✓ Branch 36 taken 1 time.
✗ Branch 37 not taken.
✓ Branch 38 taken 1 time.
✗ Branch 39 not taken.
✓ Branch 40 taken 1 time.
✗ Branch 41 not taken.
✓ Branch 42 taken 1 time.
✗ Branch 43 not taken.
|
11 |
if (!tensor_has_no_nan(dx) || !tensor_all_finite(dx)) { |
| 224 |
|
✗ |
all_ok = false; |
| 225 |
|
✗ |
failed_op = name; |
| 226 |
|
✗ |
} |
| 227 |
|
11 |
}; |
| 228 |
|
|
|
| 229 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
2 |
check_op("silu_backward", [&]() { silu_backward(x, dy, dx); }); |
| 230 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
2 |
check_op("gelu_backward_tanh", [&]() { gelu_backward(x, dy, dx); }); |
| 231 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
2 |
check_op("gelu_exact_backward", [&]() { gelu_exact_backward(x, dy, dx); }); |
| 232 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
2 |
check_op("relu_backward", [&]() { relu_backward(x, dy, dx); }); |
| 233 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
2 |
check_op("leaky_relu_backward", [&]() { leaky_relu_backward(x, dy, 0.01f, dx); }); |
| 234 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
2 |
check_op("elu_backward", [&]() { elu_backward(x, dy, 1.0f, dx); }); |
| 235 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
2 |
check_op("quick_gelu_backward", [&]() { quick_gelu_backward(x, dy, dx); }); |
| 236 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
2 |
check_op("tanh_backward", [&]() { tanh_backward(x, dy, dx); }); |
| 237 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
2 |
check_op("tanh_backward_batched", [&]() { tanh_backward_batched(x, dy, dx); }); |
| 238 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
2 |
check_op("sigmoid_backward", [&]() { sigmoid_backward(x, dy, dx); }); |
| 239 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
2 |
check_op("sqrt_backward_clamped", [&]() { sqrt_backward_clamped(x, dy, dx, 1e-12f); }); |
| 240 |
|
|
|
| 241 |
|
|
// SwiGLU & GeGLU backward (2D tensor [1, 2*n], moderate values + extreme gate side) |
| 242 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor x_glu = Tensor::mat(1, 2 * n); |
| 243 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dy_glu = Tensor::mat(1, n); |
| 244 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dx_glu = Tensor::mat(1, 2 * n); |
| 245 |
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 30 times.
|
31 |
for (int i = 0; i < n; ++i) { |
| 246 |
|
|
// In SwiGLU: out = silu(A) * B_half; in GeGLU: out = A * gelu(B_half). |
| 247 |
|
|
// Test extreme values in both halves with bounded partner to verify |
| 248 |
|
|
// gate derivative clamping prevents 0 * Inf = NaN. |
| 249 |
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 |
x_glu(0, i) = test_vals[static_cast<std::size_t>(i)]; |
| 250 |
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 |
x_glu(0, n + i) = 1.0f; |
| 251 |
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 |
dy_glu(0, i) = 1.0f; |
| 252 |
|
30 |
} |
| 253 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
swiglu_backward(x_glu, dy_glu, dx_glu); |
| 254 |
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
|
1 |
if (!tensor_has_no_nan(dx_glu) || !tensor_all_finite(dx_glu)) { |
| 255 |
|
✗ |
all_ok = false; |
| 256 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
failed_op = "swiglu_backward"; |
| 257 |
|
✗ |
} |
| 258 |
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 30 times.
|
31 |
for (int i = 0; i < n; ++i) { |
| 259 |
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 |
x_glu(0, i) = 1.0f; |
| 260 |
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
|
30 |
x_glu(0, n + i) = test_vals[static_cast<std::size_t>(i)]; |
| 261 |
|
30 |
} |
| 262 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
geglu_backward(x_glu, dy_glu, dx_glu); |
| 263 |
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
|
1 |
if (all_ok && (!tensor_has_no_nan(dx_glu) || !tensor_all_finite(dx_glu))) { |
| 264 |
|
✗ |
all_ok = false; |
| 265 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
failed_op = "geglu_backward"; |
| 266 |
|
✗ |
} |
| 267 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
geglu_exact_backward(x_glu, dy_glu, dx_glu); |
| 268 |
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
|
1 |
if (all_ok && (!tensor_has_no_nan(dx_glu) || !tensor_all_finite(dx_glu))) { |
| 269 |
|
✗ |
all_ok = false; |
| 270 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
failed_op = "geglu_exact_backward"; |
| 271 |
|
✗ |
} |
| 272 |
|
|
|
| 273 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
2 |
report.entries.push_back({ |
| 274 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
"activation_bwd.extreme_ieee754_sweep_nan_free", |
| 275 |
|
1 |
all_ok, |
| 276 |
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
all_ok ? "All 14 activation backward kernels NaN-free and finite across 30 extreme IEEE-754 inputs" |
| 277 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
: ("NaN or Inf detected in " + failed_op) |
| 278 |
|
|
}); |
| 279 |
|
1 |
} |
| 280 |
|
|
|
| 281 |
|
1 |
return report; |
| 282 |
|
1 |
} |
| 283 |
|
|
|
| 284 |
|
1 |
BackwardProofSuiteReport prove_norm_backward_nan_freedom() { |
| 285 |
|
1 |
BackwardProofSuiteReport report; |
| 286 |
|
|
|
| 287 |
|
|
// 1. BVSolver proof: For any variance var >= 0 (even var == 0 when x_i == const) |
| 288 |
|
|
// and any user eps (clamped to eps_safe = max(eps, 1e-12f)), the denominator |
| 289 |
|
|
// radicand rad = max(var + eps_safe, 1e-12f) has IEEE-754 bit pattern in |
| 290 |
|
|
// [0x2B8CBCCC, 0x7F7FFFFF], which is strictly > 0, normal, and finite. |
| 291 |
|
|
{ |
| 292 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
BVContext ctx; |
| 293 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto rad_bits = ctx.make_var("rad_bits", 32); |
| 294 |
|
1 |
const std::uint32_t kMinRadBits = float_to_bits(1e-12f); |
| 295 |
|
1 |
const std::uint32_t kMaxRadBits = float_to_bits(std::numeric_limits<float>::max()); |
| 296 |
|
|
|
| 297 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto pre = ctx.make_binary( |
| 298 |
|
|
BVOp::And, |
| 299 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
ctx.make_binary(BVOp::Uge, rad_bits, ctx.make_const(kMinRadBits, 32)), |
| 300 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
ctx.make_binary(BVOp::Ule, rad_bits, ctx.make_const(kMaxRadBits, 32))); |
| 301 |
|
|
|
| 302 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto sign_bit = ctx.make_binary(BVOp::LShr, rad_bits, ctx.make_const(31, 32)); |
| 303 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto exp_field = ctx.make_binary( |
| 304 |
|
|
BVOp::And, |
| 305 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
ctx.make_binary(BVOp::LShr, rad_bits, ctx.make_const(23, 32)), |
| 306 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
ctx.make_const(0xFF, 32)); |
| 307 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto is_zero_or_sub = ctx.make_binary(BVOp::Eq, exp_field, ctx.make_const(0, 32)); |
| 308 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto is_neg = ctx.make_binary(BVOp::Ne, sign_bit, ctx.make_const(0, 32)); |
| 309 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto is_nan_inf = ctx.make_binary(BVOp::Eq, exp_field, ctx.make_const(0xFF, 32)); |
| 310 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto bad = ctx.make_binary( |
| 311 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
BVOp::Or, is_zero_or_sub, ctx.make_binary(BVOp::Or, is_neg, is_nan_inf)); |
| 312 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto violation = ctx.make_binary(BVOp::And, pre, bad); |
| 313 |
|
|
|
| 314 |
|
1 |
std::string detail; |
| 315 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
bool ok = prove_unsat(ctx, violation, detail); |
| 316 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
report.entries.push_back({ |
| 317 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
"norm_bwd.zero_variance_inv_std_denom_strictly_positive_bv", |
| 318 |
|
1 |
ok, |
| 319 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
detail |
| 320 |
|
|
}); |
| 321 |
|
1 |
} |
| 322 |
|
|
|
| 323 |
|
|
// 2. Runtime verification on zero-variance / constant rows and degenerate eps <= 0 |
| 324 |
|
|
// across LayerNorm, RMSNorm, L2Norm, and GroupNorm backward passes. |
| 325 |
|
|
{ |
| 326 |
|
1 |
const int rows = 3; |
| 327 |
|
1 |
const int cols = 8; |
| 328 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor x = Tensor::mat(rows, cols); |
| 329 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dy = Tensor::mat(rows, cols); |
| 330 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor gamma = Tensor::vec(cols); |
| 331 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor beta = Tensor::vec(cols); |
| 332 |
|
|
|
| 333 |
|
|
// Row 0: all zeros (var = 0, ||x||_2 = 0) |
| 334 |
|
|
// Row 1: constant non-zero 42.0f (var = 0) |
| 335 |
|
|
// Row 2: large constant 1e6f (catastrophic cancellation in E[x^2] - E[x]^2) |
| 336 |
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
|
9 |
for (int c = 0; c < cols; ++c) { |
| 337 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
x(0, c) = 0.0f; |
| 338 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
x(1, c) = 42.0f; |
| 339 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
x(2, c) = 1e6f; |
| 340 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
dy(0, c) = static_cast<float>(c) * 0.1f; |
| 341 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
dy(1, c) = (c % 2 == 0) ? 1.0f : -1.0f; |
| 342 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
dy(2, c) = 0.5f; |
| 343 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
gamma[c] = 1.0f; |
| 344 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
beta[c] = 0.0f; |
| 345 |
|
8 |
} |
| 346 |
|
|
|
| 347 |
|
|
// Batched LayerNorm forward + backward with eps = 0.0f |
| 348 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor y_ln = Tensor::mat(rows, cols); |
| 349 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor xhat_ln = Tensor::mat(rows, cols); |
| 350 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor mean_ln = Tensor::vec(rows); |
| 351 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor rstd_ln = Tensor::vec(rows); |
| 352 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
layernorm_forward_batched_with_caches(x, gamma, beta, y_ln, xhat_ln, mean_ln, rstd_ln, 0.0f); |
| 353 |
|
|
|
| 354 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dx = Tensor::mat(rows, cols); |
| 355 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dgamma = Tensor::vec(cols); |
| 356 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dbeta = Tensor::vec(cols); |
| 357 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
layernorm_backward_batched_with_caches(dy, xhat_ln, gamma, rstd_ln, dx, dgamma, dbeta); |
| 358 |
4/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
|
2 |
bool ln_ok = tensor_all_finite(y_ln) && tensor_all_finite(dx) && |
| 359 |
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
|
1 |
tensor_all_finite(dgamma) && tensor_all_finite(dbeta); |
| 360 |
|
|
|
| 361 |
|
|
// Single-row LayerNorm with rstd = 0.0f and rstd = NaN |
| 362 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor x1 = Tensor::vec(cols); |
| 363 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dy1 = Tensor::vec(cols); |
| 364 |
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
|
9 |
for (int c = 0; c < cols; ++c) { |
| 365 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
x1[c] = 42.0f; |
| 366 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
dy1[c] = static_cast<float>(c); |
| 367 |
|
8 |
} |
| 368 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor y1 = Tensor::vec(cols); |
| 369 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor xhat1 = Tensor::vec(cols); |
| 370 |
|
1 |
float m1 = 0.0f, r1 = 0.0f; |
| 371 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
layernorm_forward(x1, gamma, beta, y1, xhat1, m1, r1, 0.0f); |
| 372 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dx1 = Tensor::vec(cols); |
| 373 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
dgamma.zero(); |
| 374 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
dbeta.zero(); |
| 375 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
layernorm_backward(dy1, xhat1, gamma, r1, dx1, dgamma, dbeta); |
| 376 |
6/12
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
|
2 |
ln_ok = ln_ok && tensor_all_finite(dx1) && tensor_all_finite(dgamma) && tensor_all_finite(dbeta); |
| 377 |
|
|
|
| 378 |
|
|
// RMSNorm backward with eps = 0.0f on zero / constant rows |
| 379 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
dx.zero(); |
| 380 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
dgamma.zero(); |
| 381 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
rms_norm_backward(x, gamma, dy, 0.0f, dx, dgamma); |
| 382 |
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
|
2 |
bool rms_ok = tensor_all_finite(dx) && tensor_all_finite(dgamma); |
| 383 |
|
|
|
| 384 |
|
|
// L2Norm backward with eps = 0.0f on zero / constant rows |
| 385 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
dx.zero(); |
| 386 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
l2_norm_backward(x, /*head_dim=*/4, /*num_heads=*/2, 0.0f, dy, dx); |
| 387 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
bool l2_ok = tensor_all_finite(dx); |
| 388 |
|
|
|
| 389 |
|
|
// GroupNorm backward with constant channels |
| 390 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor x_gn = Tensor::mat(1, 4 * 2 * 2); |
| 391 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dy_gn = Tensor::mat(1, 4 * 2 * 2); |
| 392 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor gamma_gn = Tensor::vec(4); |
| 393 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dx_gn = Tensor::mat(1, 4 * 2 * 2); |
| 394 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dgamma_gn = Tensor::vec(4); |
| 395 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dbeta_gn = Tensor::vec(4); |
| 396 |
3/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 1 time.
|
17 |
for (int i = 0; i < x_gn.size(); ++i) { |
| 397 |
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 |
x_gn[i] = 7.0f; |
| 398 |
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 |
dy_gn[i] = (i % 2 == 0) ? 1.0f : -0.5f; |
| 399 |
|
16 |
} |
| 400 |
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
5 |
for (int c = 0; c < 4; ++c) gamma_gn[c] = 1.0f; |
| 401 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
group_norm_backward(x_gn, gamma_gn, dy_gn, 1, 4, 2, 2, 2, 1e-5f, dx_gn, dgamma_gn, dbeta_gn); |
| 402 |
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
2 |
bool gn_ok = tensor_all_finite(dx_gn) && tensor_all_finite(dgamma_gn) && tensor_all_finite(dbeta_gn); |
| 403 |
|
|
|
| 404 |
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
|
1 |
const bool all_ok = ln_ok && rms_ok && l2_ok && gn_ok; |
| 405 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
report.entries.push_back({ |
| 406 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
"norm_bwd.constant_input_and_zero_eps_finite", |
| 407 |
|
1 |
all_ok, |
| 408 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
all_ok ? "LayerNorm, RMSNorm, L2Norm, GroupNorm backward all finite on zero-variance/zero-eps inputs" |
| 409 |
|
|
: "Non-finite gradient in normalization backward" |
| 410 |
|
|
}); |
| 411 |
|
1 |
} |
| 412 |
|
|
|
| 413 |
|
1 |
return report; |
| 414 |
|
1 |
} |
| 415 |
|
|
|
| 416 |
|
1 |
BackwardProofSuiteReport prove_attention_softmax_backward_nan_freedom() { |
| 417 |
|
1 |
BackwardProofSuiteReport report; |
| 418 |
|
|
|
| 419 |
|
|
// 1. BVSolver proof: In online/safe softmax, subtracting row max m = max_j s_j |
| 420 |
|
|
// ensures the maximal element has shifted score s_max - m == 0.0f, so its |
| 421 |
|
|
// exponential is exp(0.0f) == 1.0f (0x3F800000), and adding non-negative |
| 422 |
|
|
// terms sum_j exp(s_j - m) yields a denominator d >= 1.0f (0x3F800000) and |
| 423 |
|
|
// <= N * 1.0f. Thus the softmax denominator can never be 0.0f or subnormal. |
| 424 |
|
|
{ |
| 425 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
BVContext ctx; |
| 426 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto denom_bits = ctx.make_var("denom_bits", 32); |
| 427 |
|
1 |
const std::uint32_t kOneBits = float_to_bits(1.0f); // 0x3F800000 |
| 428 |
|
1 |
const std::uint32_t kMaxSeqBits = float_to_bits(1048576.0f); // 2^20 tokens |
| 429 |
|
|
|
| 430 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto pre = ctx.make_binary( |
| 431 |
|
|
BVOp::And, |
| 432 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
ctx.make_binary(BVOp::Uge, denom_bits, ctx.make_const(kOneBits, 32)), |
| 433 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
ctx.make_binary(BVOp::Ule, denom_bits, ctx.make_const(kMaxSeqBits, 32))); |
| 434 |
|
|
|
| 435 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto exp_field = ctx.make_binary( |
| 436 |
|
|
BVOp::And, |
| 437 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
ctx.make_binary(BVOp::LShr, denom_bits, ctx.make_const(23, 32)), |
| 438 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
ctx.make_const(0xFF, 32)); |
| 439 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto is_zero_or_sub = ctx.make_binary(BVOp::Eq, exp_field, ctx.make_const(0, 32)); |
| 440 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
auto is_nan_inf = ctx.make_binary(BVOp::Eq, exp_field, ctx.make_const(0xFF, 32)); |
| 441 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
auto violation = ctx.make_binary( |
| 442 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
BVOp::And, pre, ctx.make_binary(BVOp::Or, is_zero_or_sub, is_nan_inf)); |
| 443 |
|
|
|
| 444 |
|
1 |
std::string detail; |
| 445 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
bool ok = prove_unsat(ctx, violation, detail); |
| 446 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
report.entries.push_back({ |
| 447 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
"attention_bwd.safe_softmax_denom_ge_one_no_div_zero_bv", |
| 448 |
|
1 |
ok, |
| 449 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
detail |
| 450 |
|
|
}); |
| 451 |
|
1 |
} |
| 452 |
|
|
|
| 453 |
|
|
// 2. All-masked (-Inf / mask=0) softmax & FlashAttention backward verification: |
| 454 |
|
|
// When an entire row is masked with -Inf or mask=0, softmax_forward produces |
| 455 |
|
|
// 0.0f (not NaN from -Inf - (-Inf)), and flash_attention_backward produces |
| 456 |
|
|
// 0.0f gradients (not NaN). |
| 457 |
|
|
{ |
| 458 |
|
1 |
const float neg_inf = -std::numeric_limits<float>::infinity(); |
| 459 |
|
|
|
| 460 |
|
|
// 2a. Softmax forward + backward on all -Inf row |
| 461 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor scores = Tensor::vec(4); |
| 462 |
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
5 |
for (int i = 0; i < 4; ++i) scores[i] = neg_inf; |
| 463 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor probs = Tensor::vec(4); |
| 464 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
softmax_forward(scores, probs); |
| 465 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dy_sm = Tensor::vec(4); |
| 466 |
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
5 |
for (int i = 0; i < 4; ++i) dy_sm[i] = 1.0f; |
| 467 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dx_sm = Tensor::vec(4); |
| 468 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
softmax_backward(probs, dy_sm, dx_sm); |
| 469 |
3/6
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
|
2 |
bool sm_ok = tensor_all_finite(probs) && tensor_all_finite(dx_sm); |
| 470 |
|
|
|
| 471 |
|
|
// Also check softmax_rows_forward with an all -Inf row |
| 472 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor scores_mat = Tensor::mat(2, 4); |
| 473 |
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
|
5 |
for (int c = 0; c < 4; ++c) { |
| 474 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
scores_mat(0, c) = neg_inf; |
| 475 |
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 |
scores_mat(1, c) = static_cast<float>(c); |
| 476 |
|
4 |
} |
| 477 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor probs_mat = Tensor::mat(2, 4); |
| 478 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
softmax_rows_forward(scores_mat, probs_mat, 2, 4); |
| 479 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
2 |
sm_ok = sm_ok && tensor_all_finite(probs_mat); |
| 480 |
|
|
|
| 481 |
|
|
// 2b. FlashAttention backward with an all-masked key mask (d_mask[k] = 0.0f) |
| 482 |
|
1 |
const int Lq = 2, Lk = 4, D = 4, num_heads = 1; |
| 483 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor Q = Tensor::mat(Lq, D); |
| 484 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor K = Tensor::mat(Lk, D); |
| 485 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor V = Tensor::mat(Lk, D); |
| 486 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor O = Tensor::mat(Lq, D); |
| 487 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
Tensor dO = Tensor::mat(Lq, D); |
| 488 |
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
|
9 |
for (int i = 0; i < Lq * D; ++i) { |
| 489 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
Q[i] = 0.25f * static_cast<float>((i % 5) - 2); |
| 490 |
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 |
dO[i] = 0.1f * static_cast<float>((i % 3) + 1); |
| 491 |
|
8 |
} |
| 492 |
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 16 times.
|
17 |
for (int i = 0; i < Lk * D; ++i) { |
| 493 |
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 |
K[i] = 0.2f * static_cast<float>((i % 7) - 3); |
| 494 |
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 |
V[i] = 0.15f * static_cast<float>((i % 5) - 2); |
| 495 |
|
16 |
} |
| 496 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
const std::vector<float> all_zero_mask(Lk, 0.0f); |
| 497 |
|
|
|
| 498 |
|
1 |
Tensor dQ, dK, dV; |
| 499 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
flash_attention_backward(Q, K, V, O, dO, all_zero_mask.data(), num_heads, false, dQ, dK, dV); |
| 500 |
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
|
2 |
bool fa_ok = tensor_all_finite(dQ) && tensor_all_finite(dK) && tensor_all_finite(dV); |
| 501 |
3/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 1 time.
|
9 |
for (int i = 0; i < dQ.size(); ++i) { |
| 502 |
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 |
if (dQ[i] != 0.0f) fa_ok = false; |
| 503 |
|
8 |
} |
| 504 |
|
|
|
| 505 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
3 |
report.entries.push_back({ |
| 506 |
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
|
1 |
"attention_bwd.all_masked_row_neg_inf_zero_grad_nan_free", |
| 507 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 |
sm_ok && fa_ok, |
| 508 |
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
(sm_ok && fa_ok) |
| 509 |
|
|
? "Softmax & FlashAttention backward produce finite 0.0f gradients on all-masked rows" |
| 510 |
|
|
: "NaN or non-zero gradient on all-masked row" |
| 511 |
|
|
}); |
| 512 |
|
1 |
} |
| 513 |
|
|
|
| 514 |
|
1 |
return report; |
| 515 |
|
1 |
} |
| 516 |
|
|
|
| 517 |
|
1 |
BackwardProofSuiteReport run_all_backward_nan_freedom_proofs() { |
| 518 |
|
1 |
BackwardProofSuiteReport combined; |
| 519 |
|
4 |
auto append = [&](const BackwardProofSuiteReport& sub) { |
| 520 |
|
3 |
combined.entries.insert(combined.entries.end(), sub.entries.begin(), sub.entries.end()); |
| 521 |
|
3 |
}; |
| 522 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
append(prove_activation_backward_nan_freedom()); |
| 523 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
append(prove_norm_backward_nan_freedom()); |
| 524 |
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
1 |
append(prove_attention_softmax_backward_nan_freedom()); |
| 525 |
|
1 |
return combined; |
| 526 |
|
1 |
} |
| 527 |
|
|
|
| 528 |
|
|
} // namespace brotensor::verify |
| 529 |
|
|
|