brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
19 / 19
100.0%
Branches:
2 / 4
50.0%
Functions:
5 / 7
71.4%

include/brotensor/trace_autodiff.h
Line Branch Exec Source
1 #pragma once
2
3 // ─── brotensor::jit — Verified Fused Backward Trace JIT ─────────────────────
4 //
5 // Reverse-mode automatic differentiation (`TraceDAG::backward()`) for traced
6 // tensor expressions, with compile-time formal verification via Brass:
7 // 1. `mirEquivProven`: Formal equivalence proof (`brass::verify::verify_mir_equivalence`)
8 // between the unoptimized and optimized backward MIR functions.
9 // 2. `spirvPtxEquivProven`: Cross-ISA equivalence proof (`brass::verify::verify_spirv_ptx_equivalence`)
10 // proving semantic equivalence between the backward MIR, Vulkan SPIR-V, and
11 // NVIDIA PTX kernels.
12 // 3. `nanSafeProven`: Static safety proof (`brass::verify::prove_spirv_shader_safety`)
13 // certifying absence of unguarded division-by-zero, domain violations, and
14 // NaN/Inf propagation across all division/logarithm/square-root operations.
15
16 #include <brotensor/jit/trace.h>
17 #include <cstddef>
18 #include <cstdint>
19 #include <functional>
20 #include <string>
21 #include <vector>
22
23 namespace brotensor::jit {
24
25 class TraceDAG;
26
27 14 struct BackwardTraceVerificationReport {
28 14 bool mirEquivProven = false;
29 14 bool spirvPtxEquivProven = false;
30 14 bool nanSafeProven = false;
31 14 size_t forwardNodeCount = 0;
32 14 size_t rawBackwardNodeCount = 0;
33 14 size_t loweredBackwardNodeCount = 0;
34 14 size_t unoptimizedMirInsts = 0;
35 14 size_t optimizedMirInsts = 0;
36 14 size_t spirvWordCount = 0;
37 14 size_t ptxByteCount = 0;
38 std::string diagnostic;
39
40 14 bool all_proven() const noexcept {
41
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 14 times.
14 return mirEquivProven && spirvPtxEquivProven && nanSafeProven;
42 }
43 };
44
45 7 struct VerifiedBackwardTrace {
46 BackwardTraceVerificationReport verification;
47 7 size_t num_inputs = 0;
48 7 int rows = 0;
49 7 int cols = 0;
50 7 Device device = Device::CPU;
51 7 Dtype dtype = Dtype::FP32;
52 std::string ptx_source;
53 std::vector<uint32_t> spirv_words;
54
55 // Internal lowered backward DAG and per-input lowered DAGs (stored via
56 // shared_ptr to keep VerifiedBackwardTrace header-independent of internal
57 // src/jit headers).
58 struct Impl;
59 std::shared_ptr<Impl> impl;
60
61 // Evaluates the fused backward trace for all primal inputs given `primal_inputs`
62 // and upstream gradient `grad_output` (`dL/dy`).
63 // Compiles and executes a single fused backward kernel on the tensors' target
64 // backend (CPU AVX2/FMA, Vulkan SPIR-V compute shader, or CUDA PTX kernel).
65 std::vector<Tensor> execute(const std::vector<Tensor>& primal_inputs,
66 const Tensor& grad_output) const;
67
68 // Evaluates the single-input backward trace for `input_idx` on the target backend.
69 Tensor execute_for_input(int input_idx,
70 const std::vector<Tensor>& primal_inputs,
71 const Tensor& grad_output) const;
72 };
73
74 // Lowering pass that rewrites high-level TraceOpKind nodes (Const, Neg, Abs,
75 // Exp, Log, Sqrt, Rsqrt) into combinations of core fused elementwise ops
76 // with provably positive denominators and domain clamping.
77 TraceDAG canonicalize_trace_dag_for_codegen(const TraceDAG& dag);
78
79 // Runs the three compile-time formal verification checks on a backward TraceDAG:
80 // - verify_mir_equivalence(raw_mir, optimized_mir)
81 // - verify_spirv_ptx_equivalence(gpu_mir)
82 // - prove_spirv_shader_safety(spirv_words)
83 BackwardTraceVerificationReport verify_backward_trace_dag(const TraceDAG& bwd_dag);
84
85 // Builds a VerifiedBackwardTrace from an existing forward TraceDAG, running
86 // symbolic reverse-mode autodiff (`forward_dag.backward()`), lowering, PTX +
87 // SPIR-V code generation, and compile-time formal verification.
88 VerifiedBackwardTrace build_verified_backward_trace(const TraceDAG& forward_dag);
89
90 // Captures a forward TraceDAG by invoking `fn` with symbolic traced inputs
91 // shaped like `example_inputs`, differentiates it via `TraceDAG::backward()`,
92 // and verifies the resulting fused backward kernel across MIR, SPIR-V, and PTX.
93 VerifiedBackwardTrace trace_and_verify_backward(
94 const std::vector<Tensor>& example_inputs,
95 const std::function<Tensor(const std::vector<Tensor>&)>& fn);
96
97 } // namespace brotensor::jit
98
99 namespace brotensor {
100 using jit::BackwardTraceVerificationReport;
101 using jit::VerifiedBackwardTrace;
102 using jit::canonicalize_trace_dag_for_codegen;
103 using jit::verify_backward_trace_dag;
104 using jit::build_verified_backward_trace;
105 using jit::trace_and_verify_backward;
106 } // namespace brotensor
107