brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
586 / 631
92.9%
Branches:
631 / 1307
48.3%
Functions:
61 / 61
100.0%

src/backward_index_verify.cpp
Line Branch Exec Source
1 // ─── Issues #138 & #157: Direct Lifted SPIR-V & PTX Backward Shader Bounds,
2 // Barrier Uniformity & Cross-Backend Equivalence Proofs ──────────────────
3 //
4 // Directly lifts compiled Brass SPIR-V binaries via
5 // `brass::spirv::parse_binary` and verifies:
6 // 1. Structural SSA & CFG invariants (`brass::spirv::verify_spirv_structural`)
7 // 2. Static compute-shader safety (`brass::verify::prove_spirv_shader_safety`:
8 // workgroup shared-memory bounds, control-barrier & subgroup uniformity,
9 // and division/sqrt hazard freedom)
10 // 3. Lifted `PhysicalStorageBuffer64` & `PushConstant` CFG bounds-guard audit:
11 // verifies that every backward shader loads runtime bounds/pointers
12 // from `StorageClassPushConstant`, guards global buffer writes (`OpStore` /
13 // `OpAtomic*` into `StorageClassPhysicalStorageBuffer`) behind conditional
14 // bounds comparisons (`OpULessThan` / `OpUGreaterThanEqual` / `OpSLessThan`
15 // / `OpSGreaterThanEqual`), and performs no unguarded entry-block stores.
16 // 4. Symbolic lockstep memory-safety, barrier-uniformity, and `MIR == SPIR-V == PTX`
17 // equivalence proofs (`verify_spirv_memory_safety`, `verify_ptx_memory_safety`,
18 // `verify_spirv_ptx_equivalence` in `<brass/verify/gpu_equiv.hpp>`), plus
19 // negative lifted SPIR-V & PTX out-of-bounds refutation.
20 //
21 // All three GPU backends (CUDA PTX, Vulkan SPIR-V, Metal MSL) share the same
22 // single-source Brass MIR builders in `src/kernels/`. Forward/backward adjoint
23 // correspondence across CPU and all active GPU backends is verified by the
24 // randomized numerical adjoint oracle in `tests/test_backward_index_proofs.cpp`.
25
26 #include <brotensor/backward_verify.h>
27
28 #if BROTENSOR_HAS_BRASS_JIT
29 #include "kernels/mir_attn_small.h"
30 #include "kernels/mir_conv_cm.h"
31 #include "kernels/mir_conv_direct.h"
32 #include "kernels/mir_delta.h"
33 #include "kernels/mir_fa_bwd.h"
34 #include "kernels/mir_fa_bwd_cm.h"
35 #include "kernels/mir_fa_cm.h"
36 #include "kernels/mir_fa_rows.h"
37 #include "kernels/mir_gemm_simt.h"
38 #include "kernels/mir_elementwise.h"
39 #include "kernels/mir_gnorm.h"
40 #include "kernels/mir_lstm.h"
41 #include "kernels/mir_norm.h"
42 #include "kernels/mir_remap.h"
43 #include "kernels/mir_resample.h"
44 #include "kernels/mir_resample_bwd.h"
45 #include "kernels/mir_rope.h"
46 #include "kernels/mir_scatter.h"
47 #include "kernels/mir_vision.h"
48 #include "kernels/mir_xent.h"
49 #include "vulkan/shaders/op_codes.h"
50
51 #include <brass/codegen/kernel_jit.hpp>
52 #include <brass/mir/builder.hpp>
53 #include <brass/mir/module.hpp>
54 #include <brass/target/ptx/ptx_isel.hpp>
55 #include <brass/target/spirv/spirv_binary_reader.hpp>
56 #include <brass/target/spirv/spirv_ir.hpp>
57 #include <brass/target/spirv_target.hpp>
58 #include <brass/verify/gpu_equiv.hpp>
59 #include <brass/verify/gpu_kernel_prover.hpp>
60 #endif
61
62 #include <cstdint>
63 #include <span>
64 #include <sstream>
65 #include <string>
66 #include <unordered_set>
67 #include <vector>
68
69 namespace brotensor::verify {
70
71 #if BROTENSOR_HAS_BRASS_JIT
72
73 namespace {
74
75 6 void append_report(BackwardProofSuiteReport& dst, const BackwardProofSuiteReport& src) {
76 6 dst.entries.insert(dst.entries.end(), src.entries.begin(), src.entries.end());
77 6 }
78
79 108 struct LiftedSpirvCfgMetrics {
80 108 bool parse_ok = false;
81 108 bool structural_ok = false;
82 108 bool prover_safe = false;
83 108 bool has_compute_entry = false;
84 108 bool uses_psb64 = false;
85 108 std::uint32_t local_size_x = 0;
86 108 std::uint32_t local_size_y = 0;
87 108 std::uint32_t local_size_z = 0;
88 108 std::uint32_t num_functions = 0;
89 108 std::uint32_t num_blocks = 0;
90 108 std::uint32_t num_instructions = 0;
91 108 std::uint32_t push_constant_accesses = 0;
92 108 std::uint32_t psb_pointer_ops = 0;
93 108 std::uint32_t psb_loads = 0;
94 108 std::uint32_t psb_stores_and_atomics = 0;
95 108 std::uint32_t workgroup_accesses = 0;
96 108 std::uint32_t bounds_comparisons = 0;
97 108 std::uint32_t conditional_branches = 0;
98 108 bool entry_block_has_unguarded_psb_store = false;
99 std::string diagnostic;
100
101 108 bool all_ok() const noexcept {
102
4/8
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 108 times.
✗ Branch 7 not taken.
216 return parse_ok && structural_ok && prover_safe && has_compute_entry &&
103
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 uses_psb64 && (push_constant_accesses > 0) &&
104
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 (psb_pointer_ops > 0) && (psb_stores_and_atomics > 0) &&
105
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 108 times.
108 (bounds_comparisons > 0) && (conditional_branches > 0) &&
106 108 !entry_block_has_unguarded_psb_store;
107 }
108 };
109
110 171502 LiftedSpirvCfgMetrics audit_lifted_spirv_words(std::span<const std::uint32_t> words, const char* label) {
111 171502 LiftedSpirvCfgMetrics m;
112
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 171394 times.
171502 if (words.empty()) {
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 171394 times.
171394 m.diagnostic = "Empty SPIR-V binary";
114 ✗ return m;
115 }
116
2/2
✓ Branch 0 taken 171502 times.
✓ Branch 1 taken 171394 times.
108 brass::spirv::Module mod;
117 171502 std::string err;
118
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 171394 times.
171502 m.parse_ok = brass::spirv::parse_binary(words, mod, &err);
119
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 if (!m.parse_ok) {
120
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ m.diagnostic = "parse_binary failed: " + err;
121 ✗ return m;
122 }
123
124
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 m.structural_ok = brass::spirv::verify_spirv_structural(mod, &err);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 if (!m.structural_ok) {
126
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ m.diagnostic = "verify_spirv_structural failed: " + err;
127 ✗ return m;
128 }
129
130
2/2
✓ Branch 0 taken 171502 times.
✓ Branch 1 taken 171394 times.
108 brass::spirv::ComputeShaderAuditReport rep = brass::verify::prove_spirv_shader_safety(mod);
131
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 171394 times.
171502 m.prover_safe = rep.ok();
132 108 m.num_functions = rep.num_functions;
133 108 m.num_blocks = rep.num_blocks;
134 108 m.num_instructions = rep.num_instructions;
135 108 m.local_size_x = rep.local_size[0];
136 108 m.local_size_y = rep.local_size[1];
137 108 m.local_size_z = rep.local_size[2];
138
2/2
✓ Branch 0 taken 171502 times.
✓ Branch 1 taken 171394 times.
108 m.uses_psb64 = (mod.addressing() == spv::AddressingModelPhysicalStorageBuffer64);
139
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 171394 times.
171610 m.has_compute_entry = !mod.entry_points.empty() &&
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 mod.entry_points.front().model == spv::ExecutionModelGLCompute &&
141 108 m.local_size_x >= 1u;
142
143 // Collect storage classes for pointer types and global variables in the lifted module
144 171502 std::unordered_set<brass::spirv::Id> push_const_ptr_types;
145 171502 std::unordered_set<brass::spirv::Id> psb_ptr_types;
146 171502 std::unordered_set<brass::spirv::Id> workgroup_ptr_types;
147 171502 std::unordered_set<brass::spirv::Id> push_const_ids;
148 171502 std::unordered_set<brass::spirv::Id> psb_ptr_ids;
149 171502 std::unordered_set<brass::spirv::Id> workgroup_ids;
150
151
2/2
✓ Branch 0 taken 177574 times.
✓ Branch 1 taken 108 times.
177682 for (const auto& g : mod.globals) {
152
4/4
✓ Branch 0 taken 939 times.
✓ Branch 1 taken 176635 times.
✓ Branch 2 taken 181876 times.
✓ Branch 3 taken 182815 times.
177574 if (g.op == spv::OpTypePointer && g.operands.size() >= 2) {
153 182815 const auto sc = static_cast<spv::StorageClass>(g.operands[0].value);
154
2/2
✓ Branch 0 taken 561 times.
✓ Branch 1 taken 182254 times.
182815 if (sc == spv::StorageClassPushConstant) {
155
2/2
✓ Branch 0 taken 378 times.
✓ Branch 1 taken 181876 times.
182254 push_const_ptr_types.insert(g.result);
156
3/4
✓ Branch 0 taken 401 times.
✓ Branch 1 taken 160 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 401 times.
939 } else if (sc == spv::StorageClassPhysicalStorageBuffer ||
157 401 sc == spv::StorageClassStorageBuffer) {
158
1/2
✓ Branch 0 taken 160 times.
✗ Branch 1 not taken.
160 psb_ptr_types.insert(g.result);
159
2/2
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 182 times.
561 } else if (sc == spv::StorageClassWorkgroup) {
160
1/2
✓ Branch 0 taken 219 times.
✗ Branch 1 not taken.
219 workgroup_ptr_types.insert(g.result);
161 219 }
162
3/4
✓ Branch 0 taken 629 times.
✓ Branch 1 taken 4612 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 629 times.
359450 } else if (g.op == spv::OpVariable && !g.operands.empty()) {
163 629 const auto sc = static_cast<spv::StorageClass>(g.operands[0].value);
164
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 521 times.
629 if (sc == spv::StorageClassPushConstant) {
165
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 push_const_ids.insert(g.result);
166
2/2
✓ Branch 0 taken 207 times.
✓ Branch 1 taken 314 times.
629 } else if (sc == spv::StorageClassWorkgroup) {
167
1/2
✓ Branch 0 taken 207 times.
✗ Branch 1 not taken.
207 workgroup_ids.insert(g.result);
168 207 }
169 629 }
170 }
171
172
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 108 times.
216 for (const auto& fn : mod.functions) {
173
2/2
✓ Branch 0 taken 5649 times.
✓ Branch 1 taken 108 times.
5757 for (std::size_t bi = 0; bi < fn.blocks.size(); ++bi) {
174 5649 const auto& blk = fn.blocks[bi];
175
2/2
✓ Branch 0 taken 5649 times.
✓ Branch 1 taken 92810 times.
98459 for (const auto& inst : blk.insts) {
176
3/4
✓ Branch 0 taken 92810 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1565 times.
✓ Branch 3 taken 91245 times.
92810 if (push_const_ptr_types.count(inst.type)) {
177
1/2
✓ Branch 0 taken 1565 times.
✗ Branch 1 not taken.
1565 push_const_ids.insert(inst.result);
178 1565 }
179
3/4
✓ Branch 0 taken 92810 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 90938 times.
✓ Branch 3 taken 1872 times.
92810 if (psb_ptr_types.count(inst.type) || inst.op == spv::OpConvertUToPtr) {
180
2/2
✓ Branch 0 taken 1872 times.
✓ Branch 1 taken 90938 times.
92810 psb_ptr_ids.insert(inst.result);
181 1872 ++m.psb_pointer_ops;
182 1872 }
183
4/4
✓ Branch 0 taken 92810 times.
✓ Branch 1 taken 90938 times.
✓ Branch 2 taken 4424 times.
✓ Branch 3 taken 88386 times.
1872 if (workgroup_ptr_types.count(inst.type)) {
184
1/2
✓ Branch 0 taken 4424 times.
✗ Branch 1 not taken.
4424 workgroup_ids.insert(inst.result);
185 4424 }
186
187
6/7
✓ Branch 0 taken 61658 times.
✓ Branch 1 taken 7633 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6287 times.
✓ Branch 4 taken 8089 times.
✓ Branch 5 taken 6864 times.
✓ Branch 6 taken 2279 times.
92810 switch (inst.op) {
188 case spv::OpAccessChain:
189 case spv::OpInBoundsAccessChain:
190 case spv::OpPtrAccessChain:
191
1/2
✓ Branch 0 taken 6287 times.
✗ Branch 1 not taken.
6287 if (!inst.operands.empty()) {
192 6287 const auto base = inst.operands[0].value;
193
3/4
✓ Branch 0 taken 6287 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4722 times.
✓ Branch 3 taken 1565 times.
6287 if (push_const_ids.count(base)) ++m.push_constant_accesses;
194
3/4
✓ Branch 0 taken 6287 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1863 times.
✓ Branch 3 taken 4424 times.
6287 if (workgroup_ids.count(base)) ++m.workgroup_accesses;
195 6287 }
196 6287 break;
197 case spv::OpLoad:
198
1/2
✓ Branch 0 taken 6864 times.
✗ Branch 1 not taken.
6864 if (!inst.operands.empty()) {
199 6864 const auto ptr = inst.operands[0].value;
200
3/4
✓ Branch 0 taken 6864 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1565 times.
✓ Branch 3 taken 5299 times.
6864 if (push_const_ids.count(ptr)) ++m.push_constant_accesses;
201
3/4
✓ Branch 0 taken 6864 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5682 times.
✓ Branch 3 taken 1182 times.
6864 if (psb_ptr_ids.count(ptr)) ++m.psb_loads;
202
3/4
✓ Branch 0 taken 6864 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3813 times.
✓ Branch 3 taken 3051 times.
6864 if (workgroup_ids.count(ptr)) ++m.workgroup_accesses;
203 6864 }
204 6864 break;
205 case spv::OpStore:
206
1/2
✓ Branch 0 taken 2279 times.
✗ Branch 1 not taken.
2279 if (!inst.operands.empty()) {
207 2279 const auto ptr = inst.operands[0].value;
208
3/4
✓ Branch 0 taken 2279 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 690 times.
✓ Branch 3 taken 1589 times.
2279 if (psb_ptr_ids.count(ptr)) {
209 690 ++m.psb_stores_and_atomics;
210
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 690 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
690 if (bi == 0 && fn.blocks.size() > 1) {
211 ✗ m.entry_block_has_unguarded_psb_store = true;
212 ✗ }
213 690 }
214
3/4
✓ Branch 0 taken 2279 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1174 times.
✓ Branch 3 taken 1105 times.
2279 if (workgroup_ids.count(ptr)) ++m.workgroup_accesses;
215 2279 }
216 2279 break;
217 case spv::OpAtomicFAddEXT:
218 case spv::OpAtomicIAdd:
219 case spv::OpAtomicCompareExchange:
220 case spv::OpAtomicExchange:
221
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (!inst.operands.empty()) {
222 ✗ const auto ptr = inst.operands[0].value;
223
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (psb_ptr_ids.count(ptr)) {
224 ✗ ++m.psb_stores_and_atomics;
225
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (bi == 0 && fn.blocks.size() > 1) {
226 ✗ m.entry_block_has_unguarded_psb_store = true;
227 ✗ }
228 ✗ }
229 ✗ }
230 ✗ break;
231 case spv::OpULessThan:
232 case spv::OpULessThanEqual:
233 case spv::OpUGreaterThan:
234 case spv::OpUGreaterThanEqual:
235 case spv::OpSLessThan:
236 case spv::OpSLessThanEqual:
237 case spv::OpSGreaterThan:
238 case spv::OpSGreaterThanEqual:
239 case spv::OpIEqual:
240 case spv::OpINotEqual:
241 7633 ++m.bounds_comparisons;
242 7633 break;
243 case spv::OpBranchConditional:
244 case spv::OpSelect:
245 8089 ++m.conditional_branches;
246 8089 break;
247 default:
248 61658 break;
249 }
250 }
251 5649 }
252 }
253
254
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 std::ostringstream oss;
255
6/12
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 108 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 108 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 108 times.
✗ Branch 11 not taken.
108 oss << label << ": local_size=(" << m.local_size_x << "," << m.local_size_y << ","
256
3/6
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 108 times.
✗ Branch 5 not taken.
108 << m.local_size_z << "), blocks=" << m.num_blocks
257
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 << ", insts=" << m.num_instructions
258
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 << ", push_consts=" << m.push_constant_accesses
259
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 << ", psb_ptrs=" << m.psb_pointer_ops
260
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 << ", psb_loads=" << m.psb_loads
261
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 << ", psb_writes=" << m.psb_stores_and_atomics
262
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 << ", wg_accesses=" << m.workgroup_accesses
263
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 << ", bounds_cmp=" << m.bounds_comparisons
264
2/4
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 108 times.
✗ Branch 3 not taken.
108 << ", cond_br=" << m.conditional_branches;
265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 if (!rep.hazards.empty()) {
266
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ oss << " [hazard: " << rep.hazards.front().message << "]";
267 ✗ }
268
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 m.diagnostic = oss.str();
269 108 return m;
270 1392224 }
271
272 struct MirAuditAccum {
273 10 bool ok = true;
274 10 std::uint32_t insts = 0, writes = 0, cmp = 0, count = 0;
275 std::string first_failure;
276 brass::target::SpirvOptions opts;
277
278 30 explicit MirAuditAccum(std::uint32_t local_size_x) {
279 10 opts.stage = brass::target::SpirvStage::Compute;
280 10 opts.local_size_x = local_size_x;
281 10 opts.local_size_spec_constants = true;
282 20 }
283
284 template <typename Fn>
285 57 void check(const char* label, Fn&& build_fn) {
286 57 brass::Module mod("verify_mir");
287
19/38
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ 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 9 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 6 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 time.
✗ Branch 21 not taken.
✓ Branch 22 taken 6 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 2 times.
✗ 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 3 times.
✗ Branch 31 not taken.
✓ Branch 32 taken 3 times.
✗ Branch 33 not taken.
✓ Branch 34 taken 3 times.
✗ Branch 35 not taken.
✓ Branch 36 taken 6 times.
✗ Branch 37 not taken.
57 brass::Function* fn = build_fn(mod);
288
19/38
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ 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 9 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 6 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 time.
✗ Branch 21 not taken.
✓ Branch 22 taken 6 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 2 times.
✗ 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 3 times.
✗ Branch 31 not taken.
✓ Branch 32 taken 3 times.
✗ Branch 33 not taken.
✓ Branch 34 taken 3 times.
✗ Branch 35 not taken.
✓ Branch 36 taken 6 times.
✗ Branch 37 not taken.
57 brass::target::SpirvKernel sk = brass::target::SpirvTarget::compile(*fn, opts);
289
38/76
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ 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 9 times.
✗ Branch 33 not taken.
✓ Branch 34 taken 9 times.
✗ Branch 35 not taken.
✓ Branch 36 taken 6 times.
✗ Branch 37 not taken.
✓ Branch 38 taken 6 times.
✗ Branch 39 not taken.
✓ Branch 40 taken 1 time.
✗ Branch 41 not taken.
✓ Branch 42 taken 1 time.
✗ Branch 43 not taken.
✓ Branch 44 taken 6 times.
✗ Branch 45 not taken.
✓ Branch 46 taken 6 times.
✗ Branch 47 not taken.
✓ Branch 48 taken 2 times.
✗ Branch 49 not taken.
✓ Branch 50 taken 2 times.
✗ Branch 51 not taken.
✓ Branch 52 taken 1 time.
✗ Branch 53 not taken.
✓ Branch 54 taken 1 time.
✗ Branch 55 not taken.
✓ Branch 56 taken 1 time.
✗ Branch 57 not taken.
✓ Branch 58 taken 1 time.
✗ Branch 59 not taken.
✓ Branch 60 taken 3 times.
✗ Branch 61 not taken.
✓ Branch 62 taken 3 times.
✗ Branch 63 not taken.
✓ Branch 64 taken 3 times.
✗ Branch 65 not taken.
✓ Branch 66 taken 3 times.
✗ Branch 67 not taken.
✓ Branch 68 taken 3 times.
✗ Branch 69 not taken.
✓ Branch 70 taken 3 times.
✗ Branch 71 not taken.
✓ Branch 72 taken 6 times.
✗ Branch 73 not taken.
✓ Branch 74 taken 6 times.
✗ Branch 75 not taken.
57 LiftedSpirvCfgMetrics m = audit_lifted_spirv_words(sk.words, label);
290 57 insts += m.num_instructions;
291 57 writes += m.psb_stores_and_atomics;
292 57 cmp += m.bounds_comparisons;
293 57 ++count;
294
19/38
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ 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 9 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 6 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 1 time.
✗ Branch 21 not taken.
✓ Branch 22 taken 6 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 2 times.
✗ 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 3 times.
✗ Branch 31 not taken.
✓ Branch 32 taken 3 times.
✗ Branch 33 not taken.
✓ Branch 34 taken 3 times.
✗ Branch 35 not taken.
✓ Branch 36 taken 6 times.
✗ Branch 37 not taken.
57 if (!m.all_ok()) {
295 ✗ ok = false;
296
0/76
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 49 not taken.
✗ Branch 50 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 53 not taken.
✗ Branch 54 not taken.
✗ Branch 55 not taken.
✗ Branch 56 not taken.
✗ Branch 57 not taken.
✗ Branch 58 not taken.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
✗ Branch 61 not taken.
✗ Branch 62 not taken.
✗ Branch 63 not taken.
✗ Branch 64 not taken.
✗ Branch 65 not taken.
✗ Branch 66 not taken.
✗ Branch 67 not taken.
✗ Branch 68 not taken.
✗ Branch 69 not taken.
✗ Branch 70 not taken.
✗ Branch 71 not taken.
✗ Branch 72 not taken.
✗ Branch 73 not taken.
✗ Branch 74 not taken.
✗ Branch 75 not taken.
✗ if (first_failure.empty()) first_failure = m.diagnostic;
297 ✗ }
298 57 }
299
300 10 void finish(BackwardProofSuiteReport& rep, const std::string& name, const char* desc) const {
301 10 std::ostringstream oss;
302
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (ok) {
303
5/10
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 10 times.
✗ Branch 9 not taken.
10 oss << "Proven on " << count << " compiled MIR " << desc << " .spv binaries ("
304
6/12
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 10 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 10 times.
✗ Branch 11 not taken.
10 << insts << " insts, " << writes << " writes, " << cmp << " cmp, 0 hazards)";
305 10 } else {
306
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ oss << first_failure;
307 }
308
3/6
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
10 rep.entries.push_back({name, ok, oss.str()});
309 10 }
310 };
311
312 } // namespace
313
314 // ─── 1. Conv2D, ConvTranspose2D & ModulatedConv2D Lifted SPIR-V Audit ────────
315
316 1 BackwardProofSuiteReport prove_conv2d_adjoint_duality() {
317 1 BackwardProofSuiteReport rep;
318
319 #if BROTENSOR_HAS_BRASS_JIT
320 {
321
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 MirAuditAccum acc(kernels::kEwBlock);
322
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 for (Dtype dt : {Dtype::FP32, Dtype::FP16, Dtype::BF16}) {
323
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 for (std::uint32_t mode : {0u, 1u}) {
324
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 acc.check("mir_conv_direct", [mode, dt](brass::Module& m) {
325 6 return kernels::build_conv_direct_mir(m, mode, dt, true);
326 });
327 }
328 }
329
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 acc.finish(rep, "conv2d_lifted_spirv_conv_direct_f32_f16_bf16_bounds_and_safety", "conv_direct");
330 1 }
331 #endif
332 #if BROTENSOR_HAS_BRASS_JIT
333 {
334
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 MirAuditAccum acc(kernels::kEwBlock);
335
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 for (Dtype dt : {Dtype::FP32, Dtype::FP16, Dtype::BF16}) {
336
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 acc.check("mir_conv_simt", [dt](brass::Module& m) {
337 3 return kernels::build_conv_simt_mir(m, dt, false, true, true, 3, 3, 1, 1, 1, 1, 1, 1);
338 });
339 }
340
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 acc.finish(rep, "conv2d_lifted_spirv_conv_simt_bounds_and_barrier_safety", "conv_simt");
341 1 }
342 {
343
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 MirAuditAccum acc(128u);
344
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (Dtype dt : {Dtype::FP16, Dtype::BF16}) {
345
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 acc.check("mir_conv_cm", [dt](brass::Module& m) {
346 2 kernels::ConvCmSpec s;
347 2 s.dt = dt; s.cfg_idx = 2u; s.has_bias = true;
348 2 return kernels::build_conv_cm_mir(m, s);
349 });
350 }
351
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 acc.finish(rep, "conv2d_lifted_spirv_conv_cm_bounds_and_barrier_safety", "conv_cm");
352 1 }
353 #endif
354
355 // Prove tile interior bounds elimination & symbolic lockstep global/shared bounds
356 // via brass::verify::prove_tile_interior_bounds on conv implicit-GEMM tiles.
357 {
358 1 std::string diag;
359
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto tile_proof = brass::verify::prove_tile_interior_bounds(
360 /*tile_base_min=*/0, /*tile_base_max=*/1008,
361 /*tile_extent=*/16, /*dim_size=*/1024, &diag);
362
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 rep.entries.push_back({
363
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 "conv2d_implicit_gemm_tile_interior_bounds_bv",
364 1 tile_proof.is_proven(),
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
2 tile_proof.is_proven()
366
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 ? "Proven via prove_tile_interior_bounds: 0 <= tile_base + local_offset < 1024"
367
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ : diag
368 });
369 1 }
370
371 1 return rep;
372 1 }
373
374 // ─── 2. UpFirDn2D & Scatter-Add Lifted SPIR-V + PTX Equivalence Audit ───────
375
376 1 BackwardProofSuiteReport prove_upfirdn2d_adjoint_duality() {
377 1 BackwardProofSuiteReport rep;
378
379 {
380 1 bool all_ok = true;
381 1 std::uint32_t total_insts = 0, total_writes = 0, total_guards = 0, count = 0;
382 1 std::string first_failure;
383 1 brass::target::SpirvOptions spv_opts;
384 1 spv_opts.stage = brass::target::SpirvStage::Compute;
385 1 spv_opts.local_size_x = kernels::kVisionBlock;
386 1 spv_opts.local_size_spec_constants = true;
387
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 for (Dtype dt : {Dtype::FP32, Dtype::FP16, Dtype::BF16}) {
388
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (bool flip : {false, true}) {
389
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 brass::Module mir_mod("verify_upfirdn2d");
390
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 brass::Function* fn = kernels::build_vision_mir(mir_mod, MI_UPFIRDN, dt, flip, false);
391
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 brass::target::SpirvKernel sk = brass::target::SpirvTarget::compile(*fn, spv_opts);
392
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 LiftedSpirvCfgMetrics m = audit_lifted_spirv_words(sk.words, "mir_upfirdn2d");
393 6 total_insts += m.num_instructions;
394 6 total_writes += m.psb_stores_and_atomics;
395 6 total_guards += m.bounds_comparisons;
396 6 ++count;
397
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!m.all_ok()) {
398 ✗ all_ok = false;
399
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (first_failure.empty()) first_failure = m.diagnostic;
400 ✗ }
401 6 }
402 }
403
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::ostringstream oss;
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (all_ok) {
405
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 oss << "Proven on " << count << " brass-MIR compiled upfirdn2d .spv binaries (" << total_insts
406
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 << " lifted SPIR-V insts, " << total_writes << " guarded PhysicalStorageBuffer64 writes, "
407
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 << total_guards << " bounds checks)";
408 1 } else {
409
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ oss << first_failure;
410 }
411
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 rep.entries.push_back({"upfirdn2d_lifted_spirv_bounds_and_safety", all_ok, oss.str()});
412 1 }
413 {
414 1 bool all_ok = true;
415 1 std::uint32_t total_insts = 0, total_writes = 0, total_guards = 0, count = 0;
416 1 std::string first_failure;
417 1 brass::target::SpirvOptions spv_opts;
418 1 spv_opts.stage = brass::target::SpirvStage::Compute;
419 1 spv_opts.local_size_x = kernels::kScatterBlock;
420 1 spv_opts.local_size_spec_constants = true;
421
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 for (Dtype dt : {Dtype::FP32, Dtype::FP16, Dtype::BF16}) {
422
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 brass::Module mir_mod("verify_scatter_add");
423
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 brass::Function* fn = kernels::build_scatter_add_mir(mir_mod, SA_SUM, dt);
424
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 brass::target::SpirvKernel sk = brass::target::SpirvTarget::compile(*fn, spv_opts);
425
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 LiftedSpirvCfgMetrics m = audit_lifted_spirv_words(sk.words, "mir_scatter_add");
426 3 total_insts += m.num_instructions;
427 3 total_writes += m.psb_stores_and_atomics;
428 3 total_guards += m.bounds_comparisons;
429 3 ++count;
430
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (!m.all_ok()) {
431 ✗ all_ok = false;
432
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (first_failure.empty()) first_failure = m.diagnostic;
433 ✗ }
434 3 }
435
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::ostringstream oss;
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (all_ok) {
437
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 oss << "Proven on " << count << " brass-MIR compiled scatter_add .spv binaries ("
438
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 << total_insts << " lifted SPIR-V insts, " << total_writes
439
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 << " guarded PhysicalStorageBuffer64 writes, " << total_guards
440
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 << " bounds comparisons, 0 hazards)";
441 1 } else {
442
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ oss << "Lifted MIR scatter_add audit failure: " << first_failure;
443 }
444
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 rep.entries.push_back({"scatter_add_mir_lifted_spirv_bounds_and_safety", all_ok, oss.str()});
445 1 }
446
447 // Lower a guarded backward tap accumulation kernel to both SPIR-V and PTX
448 // and prove MIR == SPIR-V == PTX equivalence + global/shared memory safety.
449 {
450
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 brass::Module mod("bwd_tap_kernel");
451
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.
3 std::vector<brass::Type> params = {
452 3 brass::Type::ptr(), brass::Type::ptr(), brass::Type::i32()
453 };
454
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Function* fn = mod.create_function(
455
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 "bwd_tap_kernel", brass::Type::void_type(),
456 1 brass::Span<const brass::Type>(params));
457
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::codegen::KernelBuilder kb(mod, fn);
458 1 brass::Builder& b = kb.builder();
459
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 brass::BasicBlock* entry = b.append_block("entry");
460 1 b.position_at_end(entry);
461
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* dx_ptr = b.add_block_param(entry, brass::Type::ptr());
462
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* dy_ptr = b.add_block_param(entry, brass::Type::ptr());
463
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* n_elem = b.add_block_param(entry, brass::Type::i32());
464
465
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* gtid = kb.global_tid_x();
466
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 kb.if_then(b.build_ult(gtid, n_elem), [&] {
467 1 brass::Value* byte_off = b.build_shl(b.build_zext_i64(gtid), b.build_iconst_i64(2));
468 1 brass::Value* dy_val = b.build_load(brass::Type::f32(), b.build_add(dy_ptr, byte_off), 0);
469 1 b.build_store(brass::Type::f32(), b.build_add(dx_ptr, byte_off), 0, dy_val);
470 1 });
471
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 b.build_ret_void();
472
473 1 std::string eq_err;
474 brass::verify::EquivResult eq_res =
475
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::verify::verify_spirv_ptx_equivalence(*fn, &eq_err);
476
477 1 brass::target::SpirvOptions spv_opts;
478 1 spv_opts.local_size_x = 64;
479
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto sk = brass::target::SpirvTarget::compile(*fn, spv_opts);
480
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::spirv::Module lifted_spv;
481
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 bool parsed = brass::spirv::parse_binary(
482
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::span<const std::uint32_t>(sk.words.data(), sk.words.size()), lifted_spv);
483
484 1 brass::ptx::PtxISel ptx_isel;
485
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::ptx::Function ptx_fn = ptx_isel.lower(*fn);
486
487 1 brass::verify::GpuProverOptions prover_opts;
488 1 prover_opts.local_size_x = 64;
489 1 prover_opts.grid_size_x = 4;
490
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 prover_opts.buffers = {
491 {/*param_index=*/0, /*static_byte_size=*/0, /*length_param_index=*/2, /*elem_bytes=*/4, /*base_alignment=*/16},
492 {/*param_index=*/1, /*static_byte_size=*/0, /*length_param_index=*/2, /*elem_bytes=*/4, /*base_alignment=*/16},
493 };
494
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto spv_mem = brass::verify::verify_spirv_memory_safety(lifted_spv, prover_opts);
495
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto ptx_mem = brass::verify::verify_ptx_memory_safety(ptx_fn, prover_opts);
496
497
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 ok = parsed && eq_res.is_equivalent() && spv_mem.is_safe() && ptx_mem.is_safe();
498
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::ostringstream oss;
499
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 oss << "MIR==SPIR-V==PTX proven equivalent; global accesses proven: spv="
500
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 << spv_mem.global_accesses_proven << ", ptx=" << ptx_mem.global_accesses_proven;
501
4/16
✓ 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 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 rep.entries.push_back({
502
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 "upfirdn2d_lowered_spirv_and_ptx_lockstep_equiv_and_bounds",
503 1 ok,
504
2/12
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
1 ok ? oss.str() : (eq_err + " / " + spv_mem.diagnostic + " / " + ptx_mem.diagnostic)
505 });
506 1 }
507
508 1 return rep;
509 1 }
510
511 // ─── 3. Negative Lifted SPIR-V & PTX Out-of-Bounds Refutation ───────────────
512
513 1 BackwardProofSuiteReport refute_upfirdn2d_pre_995ac3c_bug() {
514 1 BackwardProofSuiteReport rep;
515
516 // Compile a compute kernel with an unshifted off-by-one index `tid_x + 1`
517 // addressing a workgroup tile of size `local_size_x = 64` into both SPIR-V
518 // and PTX, lift the SPIR-V binary via `parse_binary`, and verify that both
519 // `verify_spirv_memory_safety` and `verify_ptx_memory_safety` refute it
520 // with a concrete OOB counterexample at `tid_x == 63`.
521
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 brass::Module mod("refute_off_by_one_pad_shift");
522
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<brass::Type> params = {brass::Type::ptr()};
523
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Function* fn = mod.create_function(
524
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 "refute_off_by_one_pad_shift", brass::Type::void_type(),
525 1 brass::Span<const brass::Type>(params));
526
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::codegen::KernelBuilder kb(mod, fn);
527 1 brass::Builder& b = kb.builder();
528
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 brass::BasicBlock* entry = b.append_block("entry");
529 1 b.position_at_end(entry);
530
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 (void)b.add_block_param(entry, brass::Type::ptr());
531
532
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* tile = kb.shared_alloc_f32(64);
533
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* tid = kb.tid_x();
534 // Buggy unshifted index: tid + 1 (omitting -1 shift, so tid=63 accesses index 64 >= 64)
535
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 brass::Value* buggy_idx = b.build_add(tid, b.build_iconst_i32(1));
536
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 kb.shared_store_f32_indexed(tile, buggy_idx, b.build_fconst_f32(1.0f));
537
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 b.build_ret_void();
538
539 1 brass::target::SpirvOptions spv_opts;
540 1 spv_opts.local_size_x = 64;
541
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto sk = brass::target::SpirvTarget::compile(*fn, spv_opts);
542
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::spirv::Module lifted_spv;
543
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 const bool parsed = brass::spirv::parse_binary(
544
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::span<const std::uint32_t>(sk.words.data(), sk.words.size()), lifted_spv);
545
546 1 brass::verify::GpuProverOptions opts;
547 1 opts.local_size_x = 64;
548
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto spv_res = brass::verify::verify_spirv_memory_safety(lifted_spv, opts);
549
550 1 brass::ptx::PtxISel ptx_isel;
551
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::ptx::Function ptx_fn = ptx_isel.lower(*fn);
552
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto ptx_res = brass::verify::verify_ptx_memory_safety(ptx_fn, opts);
553
554
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
1 const bool spv_caught = parsed && spv_res.is_counterexample() && !spv_res.violations.empty();
555
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 const bool ptx_caught = ptx_res.is_counterexample() && !ptx_res.violations.empty();
556
557
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 rep.entries.push_back({
558
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 "lifted_spirv_refute_off_by_one_oob_index",
559 1 spv_caught,
560
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 spv_caught ? ("Refuted on lifted SPIR-V: " + spv_res.diagnostic) : "Failed to refute on SPIR-V"
561 });
562
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 rep.entries.push_back({
563
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 "lowered_ptx_refute_off_by_one_oob_index",
564 1 ptx_caught,
565
2/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1 ptx_caught ? ("Refuted on lowered PTX: " + ptx_res.diagnostic) : "Failed to refute on PTX"
566 });
567
568 1 return rep;
569 1 }
570
571 // ─── 4. Resample2D, Interp2D, Pad2D & Pooling Lifted SPIR-V Audit ───────────
572
573 1 BackwardProofSuiteReport prove_resample2d_adjoint_duality() {
574 1 BackwardProofSuiteReport rep;
575
576 #if BROTENSOR_HAS_BRASS_JIT
577 1 brass::target::SpirvOptions spv_opts;
578 1 spv_opts.stage = brass::target::SpirvStage::Compute;
579 1 spv_opts.local_size_x = kernels::kEwBlock;
580 1 spv_opts.local_size_spec_constants = true;
581
582 3 auto audit_mir_family = [&](const std::string& label, const auto& build_all) {
583 2 std::size_t total_insts = 0, total_writes = 0, total_guards = 0, count = 0;
584 2 bool all_ok = true;
585 2 std::string first_failure;
586
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
35 build_all([&](const char* kname, brass::Function* fn) {
587 33 brass::target::SpirvKernel sk = brass::target::SpirvTarget::compile(*fn, spv_opts);
588
4/8
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 15 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 15 times.
✗ Branch 7 not taken.
33 LiftedSpirvCfgMetrics m = audit_lifted_spirv_words(sk.words, kname);
589 33 total_insts += m.num_instructions;
590 33 total_writes += m.psb_stores_and_atomics;
591 33 total_guards += m.bounds_comparisons;
592 33 ++count;
593
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
33 if (!m.all_ok()) {
594 ✗ all_ok = false;
595
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ if (first_failure.empty()) first_failure = m.diagnostic;
596 ✗ }
597 33 });
598
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 std::ostringstream oss;
599
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 if (all_ok) {
600
8/16
✓ 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.
2 oss << "Proven on " << count << " brass-MIR compiled .spv binaries (" << total_insts
601
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 << " lifted SPIR-V insts, " << total_writes << " guarded PhysicalStorageBuffer64 writes, "
602
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 << total_guards << " bounds guards)";
603 2 } else {
604
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ oss << first_failure;
605 }
606
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 rep.entries.push_back({label, all_ok, oss.str()});
607 2 };
608
609
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 audit_mir_family("resample2d_lifted_spirv_resample_bwd_f32_f16_bf16_bounds_and_safety", [&](const auto& emit) {
610
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
4 for (Dtype dt : {Dtype::FP32, Dtype::FP16, Dtype::BF16}) {
611
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3 times.
72 for (const auto& [m, im] : std::vector<std::pair<std::uint32_t, std::uint32_t>>{
612 18 {RB_INTERP, 0u}, {RB_INTERP, 1u}, {RB_ADAPTIVE, 0u}, {RB_PAD, 0u}, {RB_PAD, 1u}, {RB_MAXPOOL, 0u}}) {
613
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
18 brass::Module mod("verify_rb");
614
4/8
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 18 times.
✗ Branch 7 not taken.
54 emit("mir_resample_bwd", kernels::build_resample_bwd_mir(mod, m, dt, im));
615 18 }
616 }
617 1 });
618
619
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 audit_mir_family("resample2d_lifted_spirv_resample_fwd_and_remap_bounds_and_safety", [&](const auto& emit) {
620
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
4 for (Dtype dt : {Dtype::FP32, Dtype::FP16, Dtype::BF16}) {
621 3 brass::Module r0("verify_rs");
622
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 emit("mir_resample", kernels::build_resample_mir(r0, RS_INTERP, dt, false, 1));
623
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 brass::Module r1("verify_rs_maxpool");
624
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 emit("mir_resample", kernels::build_resample_mir(r1, RS_MAXPOOL, dt, false, 0));
625
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 brass::Module r2("verify_rs_convex");
626
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 emit("mir_resample", kernels::build_resample_mir(r2, RS_CONVEX, dt, false, 0));
627 3 }
628
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 for (int es : {2, 4}) {
629
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 for (std::uint32_t m : {std::uint32_t(RM_PAD), std::uint32_t(RM_UNFOLD), std::uint32_t(RM_SCATTER_ROWS)}) {
630 6 brass::Module rm("verify_rm");
631
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 emit("mir_remap", kernels::build_remap_mir(rm, m, es));
632 6 }
633 }
634 1 });
635 #endif
636
637 1 return rep;
638 1 }
639
640 // ─── 5. RoPE, Activations, Norms & Recurrent Lifted SPIR-V + PTX Equivalence ─
641
642 1 BackwardProofSuiteReport prove_rope_adjoint_duality() {
643 1 BackwardProofSuiteReport rep;
644
645 {
646 1 bool all_ok = true;
647 1 std::uint32_t total_insts = 0, total_writes = 0, total_guards = 0, count = 0;
648 1 std::string first_failure;
649 1 brass::target::SpirvOptions spv_opts;
650 1 spv_opts.stage = brass::target::SpirvStage::Compute;
651 1 spv_opts.local_size_x = kernels::kRopeBlock;
652 1 spv_opts.local_size_spec_constants = true;
653
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 for (Dtype dt : {Dtype::FP32, Dtype::FP16, Dtype::BF16}) {
654
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 brass::Module mir_mod("verify_rope");
655
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 brass::Function* fn = kernels::build_rope_mir(mir_mod, ROPE_THETA, true, dt);
656
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 brass::target::SpirvKernel sk = brass::target::SpirvTarget::compile(*fn, spv_opts);
657
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 LiftedSpirvCfgMetrics m = audit_lifted_spirv_words(sk.words, "mir_rope");
658 3 total_insts += m.num_instructions;
659 3 total_writes += m.psb_stores_and_atomics;
660 3 total_guards += m.bounds_comparisons;
661 3 ++count;
662
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (!m.all_ok()) {
663 ✗ all_ok = false;
664
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (first_failure.empty()) first_failure = m.diagnostic;
665 ✗ }
666 3 }
667
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::ostringstream oss;
668
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (all_ok) {
669
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 oss << "Proven on " << count << " brass-MIR compiled rope .spv binaries ("
670
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 << total_insts << " lifted SPIR-V insts, " << total_writes
671
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 << " guarded PhysicalStorageBuffer64 writes, " << total_guards
672
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 << " bounds comparisons, 0 hazards)";
673 1 } else {
674
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ oss << "Lifted MIR rope audit failure: " << first_failure;
675 }
676
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 rep.entries.push_back({"rope_lifted_spirv_bounds_and_safety", all_ok, oss.str()});
677 1 }
678
679 {
680 1 bool all_ok = true;
681 1 std::uint32_t total_insts = 0, total_writes = 0, total_guards = 0, count = 0;
682 1 std::string first_failure;
683
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 for (Dtype dt : {Dtype::FP32, Dtype::FP16, Dtype::BF16}) {
684 3 const int lanes = (dt == Dtype::FP32) ? 4 : 8;
685
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (std::uint32_t op : {std::uint32_t(GOP_RELU), std::uint32_t(GOP_SILU)}) {
686
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 brass::Module mir_mod("verify_act_bwd");
687
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 brass::Function* fn = kernels::build_act_bwd_mir(mir_mod, op, dt, lanes);
688 6 brass::target::SpirvOptions spv_opts;
689 6 spv_opts.stage = brass::target::SpirvStage::Compute;
690 6 spv_opts.local_size_x = kernels::kEwBlock;
691 6 spv_opts.local_size_spec_constants = true;
692
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 brass::target::SpirvKernel sk = brass::target::SpirvTarget::compile(*fn, spv_opts);
693
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 LiftedSpirvCfgMetrics m = audit_lifted_spirv_words(sk.words, "mir_act_bwd");
694 6 total_insts += m.num_instructions;
695 6 total_writes += m.psb_stores_and_atomics;
696 6 total_guards += m.bounds_comparisons;
697 6 ++count;
698
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!m.all_ok()) {
699 ✗ all_ok = false;
700
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (first_failure.empty()) first_failure = m.diagnostic;
701 ✗ }
702 6 }
703 }
704
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::ostringstream oss;
705
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (all_ok) {
706
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 oss << "Proven on " << count << " brass-MIR compiled act_bwd .spv binaries ("
707
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 << total_insts << " lifted SPIR-V insts, " << total_writes
708
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 << " guarded PhysicalStorageBuffer64 writes, " << total_guards
709
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 << " bounds comparisons, 0 hazards)";
710 1 } else {
711
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ oss << "Lifted MIR act_bwd audit failure: " << first_failure;
712 }
713
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 rep.entries.push_back({"act_bwd_mir_lifted_spirv_bounds_and_safety", all_ok, oss.str()});
714 1 }
715
716 {
717
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 MirAuditAccum acc(kernels::kEwBlock);
718
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 acc.check("mir_lstm", [](brass::Module& m) { return kernels::build_lstm_mir(m, LS_FWD, true, true); });
719
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 acc.check("mir_lstm", [](brass::Module& m) { return kernels::build_lstm_mir(m, LS_BWD, false, true); });
720
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 acc.check("mir_delta", [](brass::Module& m) { return kernels::build_delta_rule_mir(m, 32u, 4u); });
721
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 acc.check("mir_xent", [](brass::Module& m) { return kernels::build_xent_mir(m, 0u, true, true); });
722
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 acc.check("mir_xent", [](brass::Module& m) { return kernels::build_xent_mir(m, 1u, true, false); });
723
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 acc.finish(rep, "norm_lstm_deltarule_xent_lifted_spirv_bounds_and_barrier_safety", "recurrent/xent");
724 1 }
725 {
726
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 MirAuditAccum acc(kernels::kNormBlock);
727
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 for (Dtype dt : {Dtype::FP32, Dtype::FP16, Dtype::BF16}) {
728 3 const int lanes = (dt == Dtype::FP32) ? 4 : 8;
729
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
12 for (std::uint32_t mode : {std::uint32_t(NORM_LN_BWD), std::uint32_t(NORM_RMS_BWD), std::uint32_t(NORM_SM_BWD)}) {
730
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 acc.check("mir_norm", [mode, dt, lanes](brass::Module& m) {
731 9 return kernels::build_norm_mir(m, mode, dt, dt, dt, {true, true, true, false, true}, lanes);
732 });
733 }
734
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 for (std::uint32_t mode : {std::uint32_t(GN_BWD_CH), std::uint32_t(GN_BWD_DX)}) {
735
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 acc.check("mir_gnorm", [mode, dt](brass::Module& m) {
736 6 return kernels::build_gnorm_mir(m, mode, dt, false, 1);
737 });
738 }
739 }
740
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 acc.check("mir_bnorm", [](brass::Module& m) { return kernels::build_bnorm_mir(m, BN_TRAIN_BWD); });
741
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 acc.finish(rep, "norm_mir_lifted_spirv_bounds_and_barrier_safety", "norm/gnorm/bnorm backward");
742 1 }
743
744 // Prove MIR == SPIR-V == PTX equivalence on a lowered RoPE pair-index kernel
745 // using brass::verify::verify_spirv_ptx_equivalence (gpu_equiv.hpp).
746 {
747
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 brass::Module mod("rope_pair_bwd");
748
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.
3 std::vector<brass::Type> params = {
749 3 brass::Type::ptr(), brass::Type::ptr(), brass::Type::i32()
750 };
751
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Function* fn = mod.create_function(
752
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 "rope_pair_bwd", brass::Type::void_type(),
753 1 brass::Span<const brass::Type>(params));
754
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::codegen::KernelBuilder kb(mod, fn);
755 1 brass::Builder& b = kb.builder();
756
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 brass::BasicBlock* entry = b.append_block("entry");
757 1 b.position_at_end(entry);
758
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* dx_ptr = b.add_block_param(entry, brass::Type::ptr());
759
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* dy_ptr = b.add_block_param(entry, brass::Type::ptr());
760
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* n_pairs = b.add_block_param(entry, brass::Type::i32());
761
762
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* pair_id = kb.global_tid_x();
763
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 kb.if_then(b.build_ult(pair_id, n_pairs), [&] {
764 1 brass::Value* idx0 = b.build_shl(pair_id, b.build_iconst_i32(1));
765 1 brass::Value* idx1 = b.build_add(idx0, b.build_iconst_i32(1));
766 1 brass::Value* off0 = b.build_shl(b.build_zext_i64(idx0), b.build_iconst_i64(2));
767 1 brass::Value* off1 = b.build_shl(b.build_zext_i64(idx1), b.build_iconst_i64(2));
768 1 brass::Value* dy0 = b.build_load(brass::Type::f32(), b.build_add(dy_ptr, off0), 0);
769 1 brass::Value* dy1 = b.build_load(brass::Type::f32(), b.build_add(dy_ptr, off1), 0);
770 1 b.build_store(brass::Type::f32(), b.build_add(dx_ptr, off0), 0, dy0);
771 1 b.build_store(brass::Type::f32(), b.build_add(dx_ptr, off1), 0, dy1);
772 1 });
773
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 b.build_ret_void();
774
775 1 std::string err;
776
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::verify::EquivResult eq = brass::verify::verify_spirv_ptx_equivalence(*fn, &err);
777
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 rep.entries.push_back({
778
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 "rope_pair_kernel_mir_spirv_ptx_symbolic_equivalence",
779 1 eq.is_equivalent(),
780
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
2 eq.is_equivalent()
781
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 ? "Proven via verify_spirv_ptx_equivalence: MIR == SPIR-V == PTX across all pair indices"
782
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ : err
783 });
784 1 }
785
786 1 return rep;
787 1 }
788
789 // ─── 6. FlashAttention & Dense Attention Lifted SPIR-V + PTX Audit ──────────
790
791 1 BackwardProofSuiteReport prove_flash_attention_mask_adjoint_duality() {
792 1 BackwardProofSuiteReport rep;
793
794 #if BROTENSOR_HAS_BRASS_JIT
795 {
796
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 MirAuditAccum acc(64u);
797
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (Dtype dt : {Dtype::FP16, Dtype::BF16}) {
798
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 for (std::uint32_t pass : {0u, 1u, 2u}) {
799
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 acc.check("mir_fa_bwd_cm", [dt, pass](brass::Module& m) {
800 6 kernels::FaBwdCmSpec s;
801 6 s.dt = dt; s.hd = 64u; s.nsg = 2u; s.bn = 16u; s.pass = pass; s.has_mask = true;
802 6 return kernels::build_fa_bwd_cm_mir(m, s);
803 });
804 }
805 }
806
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 acc.finish(rep, "flash_attention_lifted_spirv_fa_bwd_cm_safety", "fa_bwd_cm");
807 1 }
808 {
809
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 MirAuditAccum acc(64u);
810
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
3 for (Dtype dt : {Dtype::FP16, Dtype::BF16}) {
811
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 acc.check("mir_fa_cm", [dt](brass::Module& m) {
812 2 kernels::FaCmSpec s;
813 2 s.dt = dt; s.hd = 64u; s.bc = 16u; s.nsg = 2u; s.has_mask = true;
814 2 return kernels::build_fa_cm_mir(m, s);
815 });
816 }
817
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 acc.finish(rep, "flash_attention_lifted_spirv_fa_fwd_cm_safety", "fa_cm");
818 1 }
819 {
820
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 MirAuditAccum acc(kernels::kEwBlock);
821
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 acc.check("mir_attn_bwd", [](brass::Module& m) { return kernels::build_attn_bwd_mir(m, true, true); });
822
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 acc.check("mir_attn_aux", [](brass::Module& m) { return kernels::build_attn_aux_mir(m, 0u, Dtype::FP32, true); });
823
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 for (Dtype dt : {Dtype::FP32, Dtype::FP16, Dtype::BF16}) {
824
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 acc.check("mir_fa_combine", [dt](brass::Module& m) { return kernels::build_fa_combine_mir(m, dt); });
825
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 acc.check("mir_attn_sm", [dt](brass::Module& m) {
826 3 return kernels::build_attn_softmax_mir(m, dt, 2u, false, true, false);
827 });
828 }
829
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 acc.finish(rep, "flash_attention_lifted_spirv_mir_attn_bwd_combine_softmax_safety", "attention");
830 1 }
831 {
832
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 MirAuditAccum acc(kernels::kFaRowsBlock);
833
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 for (Dtype dt : {Dtype::FP32, Dtype::FP16, Dtype::BF16}) {
834
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 acc.check("mir_fa_rows", [dt](brass::Module& m) {
835 3 return kernels::build_fa_rows_mir(m, dt, 64u, 2u, 0u, true, true, true, true, true);
836 });
837 }
838
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 acc.finish(rep, "flash_attention_lifted_spirv_mir_fa_rows_safety", "fa_rows");
839 1 }
840 {
841
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 MirAuditAccum acc(128u);
842
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 3 times.
4 for (Dtype dt : {Dtype::FP32, Dtype::FP16, Dtype::BF16}) {
843
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 for (std::uint32_t pass : {0u, 1u}) {
844
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 acc.check("mir_fa_bwd", [dt, pass](brass::Module& m) {
845 6 return kernels::build_fa_bwd_mir(m, dt, 128u, 64u, 64u, 32u, 16u, 0u, true, pass);
846 });
847 }
848 }
849
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 acc.finish(rep, "flash_attention_lifted_spirv_mir_fa_bwd_safety", "fa_bwd");
850 1 }
851 #endif
852
853 // Verify PTX equivalence (`verify_ptx_equivalence`), shared-memory bounds
854 // (`verify_ptx_memory_safety`), and barrier/warp-shuffle uniformity
855 // (`verify_ptx_barrier_uniformity`) on a lowered PTX attention row-reduction kernel.
856 {
857
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 brass::Module mod("fa_bwd_ptx_row_reduce");
858
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 std::vector<brass::Type> params = {brass::Type::ptr(), brass::Type::ptr()};
859
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Function* fn = mod.create_function(
860
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 "fa_bwd_ptx_row_reduce", brass::Type::void_type(),
861 1 brass::Span<const brass::Type>(params));
862
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::codegen::KernelBuilder kb(mod, fn);
863 1 brass::Builder& b = kb.builder();
864
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 brass::BasicBlock* entry = b.append_block("entry");
865 1 b.position_at_end(entry);
866
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* out_ptr = b.add_block_param(entry, brass::Type::ptr());
867
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* in_ptr = b.add_block_param(entry, brass::Type::ptr());
868
869
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* smem = kb.shared_alloc_f32(256);
870
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* tid = kb.tid_x();
871
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 brass::Value* off = b.build_shl(b.build_zext_i64(tid), b.build_iconst_i64(2));
872
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 brass::Value* val = b.build_load(brass::Type::f32(), b.build_add(in_ptr, off), 0);
873
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 5 times.
6 for (std::uint32_t mask = 16; mask >= 1; mask >>= 1) {
874
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 val = b.build_add(val, kb.shfl_bfly_f32(val, mask));
875 5 }
876
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.shared_store_f32_indexed(smem, tid, val);
877
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 kb.sync();
878
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Value* loaded = kb.shared_load_f32_indexed(smem, tid);
879
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 b.build_store(brass::Type::f32(), b.build_add(out_ptr, off), 0, loaded);
880
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 b.build_ret_void();
881
882 1 brass::ptx::PtxISel isel;
883
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::ptx::Function ptx_fn = isel.lower(*fn);
884
885 1 std::string eq_err;
886 brass::verify::EquivResult ptx_eq =
887
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::verify::verify_ptx_equivalence(*fn, ptx_fn, &eq_err);
888
889 1 brass::verify::GpuProverOptions opts;
890 1 opts.local_size_x = 256;
891
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 opts.buffers.push_back({0, 1024, -1, 4, 16});
892 opts.buffers.push_back({1, 1024, -1, 4, 16});
893 auto ptx_mem = brass::verify::verify_ptx_memory_safety(ptx_fn, opts);
894 auto ptx_bar = brass::verify::verify_ptx_barrier_uniformity(ptx_fn, opts);
895
896 const bool ok = ptx_eq.is_equivalent() && ptx_mem.is_safe() && ptx_bar.is_uniform();
897 std::ostringstream oss;
898 oss << "Proven PTX equivalence + memory safety (shared_proven="
899 << ptx_mem.shared_accesses_proven << ", barriers_proven="
900 << ptx_bar.barriers_proven << ", collectives_proven="
901 << ptx_bar.collectives_proven << "); hand-written .cu/.mm covered by multi-backend adjoint dot-product oracle";
902 rep.entries.push_back({
903 "flash_attention_ptx_equiv_and_non_spirv_backend_coverage",
904 ok,
905 ok ? oss.str() : (eq_err + " / " + ptx_mem.diagnostic + " / " + ptx_bar.diagnostic)
906 });
907 1 }
908
909 1 return rep;
910 1 }
911
912 1 BackwardProofSuiteReport run_all_backward_index_proofs() {
913 1 BackwardProofSuiteReport all;
914
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 append_report(all, prove_conv2d_adjoint_duality());
915
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 append_report(all, prove_upfirdn2d_adjoint_duality());
916
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 append_report(all, refute_upfirdn2d_pre_995ac3c_bug());
917
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 append_report(all, prove_resample2d_adjoint_duality());
918
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 append_report(all, prove_rope_adjoint_duality());
919
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 append_report(all, prove_flash_attention_mask_adjoint_duality());
920 1 return all;
921 1 }
922
923 #else // !BROTENSOR_HAS_BRASS_JIT
924
925 BackwardProofSuiteReport prove_conv2d_adjoint_duality() { return {}; }
926 BackwardProofSuiteReport prove_upfirdn2d_adjoint_duality() { return {}; }
927 BackwardProofSuiteReport refute_upfirdn2d_pre_995ac3c_bug() { return {}; }
928 BackwardProofSuiteReport prove_resample2d_adjoint_duality() { return {}; }
929 BackwardProofSuiteReport prove_rope_adjoint_duality() { return {}; }
930 BackwardProofSuiteReport prove_flash_attention_mask_adjoint_duality() { return {}; }
931 BackwardProofSuiteReport run_all_backward_index_proofs() { return {}; }
932
933 #endif // BROTENSOR_HAS_BRASS_JIT
934
935 } // namespace brotensor::verify
936