include/brotensor/backward_verify.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | // Formal verification & static audit entrypoints for brotensor backward passes | ||
| 4 | // (Issues #138, #139, #140): | ||
| 5 | // 1. Adjoint index transpose duality & [0, numel) bounds proofs via BVSolver | ||
| 6 | // 2. SPIR-V & PTX compute shader lifter audit (structural, shared-memory bounds, | ||
| 7 | // barrier/subgroup uniformity, division/sqrt safety) | ||
| 8 | // 3. IEEE-754 gradient NaN-freedom proofs & boundary clamping verification | ||
| 9 | |||
| 10 | #include <brotensor/tensor.h> | ||
| 11 | |||
| 12 | #include <cstdint> | ||
| 13 | #include <string> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | namespace brotensor::verify { | ||
| 17 | |||
| 18 | 30 | struct ProofEntry { | |
| 19 | std::string name; | ||
| 20 | bool proven = false; | ||
| 21 | std::string detail; | ||
| 22 | }; | ||
| 23 | |||
| 24 | struct BackwardProofSuiteReport { | ||
| 25 | std::vector<ProofEntry> entries; | ||
| 26 | |||
| 27 | 2 | bool all_passed() const noexcept { | |
| 28 | 2 | if (entries.empty()) return false; | |
| 29 | 32 | for (const auto& e : entries) { | |
| 30 | 30 | if (!e.proven) return false; | |
| 31 | } | ||
| 32 | 2 | return true; | |
| 33 | 2 | } | |
| 34 | |||
| 35 | std::size_t passed_count() const noexcept { | ||
| 36 | std::size_t n = 0; | ||
| 37 | for (const auto& e : entries) { | ||
| 38 | if (e.proven) ++n; | ||
| 39 | } | ||
| 40 | return n; | ||
| 41 | } | ||
| 42 | }; | ||
| 43 | |||
| 44 | // ─── Issue #138: Adjoint Index Transpose Duality & OOB Bounds Proofs ───────── | ||
| 45 | |||
| 46 | BackwardProofSuiteReport prove_conv2d_adjoint_duality(); | ||
| 47 | BackwardProofSuiteReport prove_upfirdn2d_adjoint_duality(); | ||
| 48 | BackwardProofSuiteReport refute_upfirdn2d_pre_995ac3c_bug(); | ||
| 49 | BackwardProofSuiteReport prove_resample2d_adjoint_duality(); | ||
| 50 | BackwardProofSuiteReport prove_rope_adjoint_duality(); | ||
| 51 | BackwardProofSuiteReport prove_flash_attention_mask_adjoint_duality(); | ||
| 52 | BackwardProofSuiteReport run_all_backward_index_proofs(); | ||
| 53 | |||
| 54 | // ─── Issue #139: Vulkan SPIR-V & PTX Backward Shader Lifter Audit ─────────── | ||
| 55 | |||
| 56 | 220 | struct ShaderAuditSummary { | |
| 57 | std::string shader_name; | ||
| 58 | 220 | bool parse_ok = false; | |
| 59 | 220 | bool structural_ok = false; | |
| 60 | 220 | bool memory_safe = false; | |
| 61 | 220 | bool barriers_uniform = false; | |
| 62 | 220 | bool div_sqrt_safe = false; | |
| 63 | 220 | std::uint32_t num_functions = 0; | |
| 64 | 220 | std::uint32_t num_blocks = 0; | |
| 65 | 220 | std::uint32_t num_instructions = 0; | |
| 66 | 220 | std::uint32_t num_workgroup_arrays = 0; | |
| 67 | 220 | std::uint32_t shared_accesses_proven = 0; | |
| 68 | 220 | std::uint32_t global_accesses_proven = 0; | |
| 69 | 220 | std::uint32_t barriers_proven = 0; | |
| 70 | 220 | std::uint32_t collectives_proven = 0; | |
| 71 | std::string diagnostic; | ||
| 72 | |||
| 73 | 655 | bool ok() const noexcept { | |
| 74 | 1310 | return parse_ok && structural_ok && memory_safe && | |
| 75 | 655 | barriers_uniform && div_sqrt_safe; | |
| 76 | } | ||
| 77 | }; | ||
| 78 | |||
| 79 | 1 | struct BackwardShaderAuditReport { | |
| 80 | std::vector<ShaderAuditSummary> shaders; | ||
| 81 | 1 | bool negative_overflow_shader_caught = false; | |
| 82 | std::string negative_test_detail; | ||
| 83 | |||
| 84 | 1 | bool all_passed() const noexcept { | |
| 85 | 1 | if (shaders.empty() || !negative_overflow_shader_caught) return false; | |
| 86 | 219 | for (const auto& s : shaders) { | |
| 87 | 218 | if (!s.ok()) return false; | |
| 88 | } | ||
| 89 | 1 | return true; | |
| 90 | 1 | } | |
| 91 | }; | ||
| 92 | |||
| 93 | ShaderAuditSummary audit_spirv_binary_words( | ||
| 94 | const std::string& name, | ||
| 95 | const std::uint32_t* words, | ||
| 96 | std::size_t nwords); | ||
| 97 | |||
| 98 | BackwardShaderAuditReport run_backward_shader_audit(); | ||
| 99 | |||
| 100 | // ─── Issue #140: Gradient NaN-Freedom Proofs & Clamped Sqrt Backward ──────── | ||
| 101 | |||
| 102 | void sqrt_backward_clamped( | ||
| 103 | const Tensor& x, | ||
| 104 | const Tensor& dY, | ||
| 105 | Tensor& dX, | ||
| 106 | float eps = 1e-12f); | ||
| 107 | |||
| 108 | BackwardProofSuiteReport prove_activation_backward_nan_freedom(); | ||
| 109 | BackwardProofSuiteReport prove_norm_backward_nan_freedom(); | ||
| 110 | BackwardProofSuiteReport prove_attention_softmax_backward_nan_freedom(); | ||
| 111 | BackwardProofSuiteReport run_all_backward_nan_freedom_proofs(); | ||
| 112 | |||
| 113 | } // namespace brotensor::verify | ||
| 114 |