brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
163 / 193
84.5%
Branches:
83 / 199
41.7%
Functions:
7 / 7
100.0%

src/jit/trace_contract.cpp
Line Branch Exec Source
1 #include "trace_contract.h"
2 #include "gpu_mir_emit_detail.h"
3
4 #include <brass/target/spirv/spirv_binary_reader.hpp>
5 #include <brass/verify/gpu_verified_tuner.hpp>
6
7 #include <algorithm>
8 #include <sstream>
9 #include <vector>
10
11 namespace brotensor::jit::spirv {
12
13 namespace {
14
15 240 std::uint64_t buffer_byte_size_for(const plan::ElementwisePlan& ew, const plan::BufferSpec& s) {
16 240 const std::uint64_t eb = static_cast<std::uint64_t>(std::max(1, plan::elem_bytes(s.dtype)));
17
4/5
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 124 times.
✓ Branch 3 taken 48 times.
✓ Branch 4 taken 64 times.
240 switch (s.bcast) {
18 case BroadcastKind::Scalar:
19 4 return eb;
20 case BroadcastKind::Row:
21 124 return static_cast<std::uint64_t>(std::max(1, ew.cols)) * eb;
22 case BroadcastKind::Col:
23 48 return static_cast<std::uint64_t>(std::max(1, ew.rows)) * eb;
24 64 case BroadcastKind::Full:
25 default:
26 64 return static_cast<std::uint64_t>(std::max<std::int64_t>(1, ew.numel)) * eb;
27 }
28 240 }
29
30 382 std::uint32_t buffer_align_for(const plan::BufferSpec& s, int lanes) {
31 382 const std::uint32_t eb = static_cast<std::uint32_t>(std::max(1, plan::elem_bytes(s.dtype)));
32 382 const std::uint32_t eff_lanes =
33
4/4
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 206 times.
✓ Branch 2 taken 124 times.
✓ Branch 3 taken 52 times.
382 (s.bcast == BroadcastKind::Full || s.bcast == BroadcastKind::Row)
34 330 ? static_cast<std::uint32_t>(std::max(1, lanes))
35 : 1u;
36 382 return eb * eff_lanes;
37 }
38
39 } // namespace
40
41 68 brass::verify::GpuKernelContract build_elementwise_contract(
42 const plan::ElementwisePlan& plan,
43 int lanes) {
44 68 brass::verify::GpuKernelContract contract;
45 68 contract.local_size_x = kEwBlock;
46 68 contract.local_size_y = 1;
47 68 contract.local_size_z = 1;
48
49 68 const std::uint32_t n_out = static_cast<std::uint32_t>(plan.outputs.size());
50 68 const std::uint32_t n_in = static_cast<std::uint32_t>(plan.inputs.size());
51 68 const std::uint32_t n_idx = n_out + n_in;
52
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 const std::uint32_t lane_u32 = static_cast<std::uint32_t>(std::max(1, lanes));
53
54
2/2
✓ Branch 0 taken 116 times.
✓ Branch 1 taken 68 times.
184 for (std::uint32_t m = 0; m < n_out; ++m) {
55 116 const plan::BufferSpec& s = plan.outputs[m];
56
2/4
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 116 times.
✗ Branch 3 not taken.
116 const std::uint32_t eb = static_cast<std::uint32_t>(std::max(1, plan::elem_bytes(s.dtype)));
57 116 brass::verify::GpuBufferSpec spec;
58 116 spec.param_index = m;
59 116 spec.length_param_index = static_cast<std::int32_t>(n_idx);
60 116 spec.elem_bytes = eb;
61 116 spec.elem_scale = lane_u32;
62 116 spec.base_alignment = eb * lane_u32;
63
1/2
✓ Branch 0 taken 116 times.
✗ Branch 1 not taken.
116 contract.buffers.push_back(spec);
64 116 }
65
66
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 244 times.
312 for (std::uint32_t k = 0; k < n_in; ++k) {
67 244 const plan::BufferSpec& s = plan.inputs[k];
68
2/4
✓ Branch 0 taken 244 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 244 times.
✗ Branch 3 not taken.
244 const std::uint32_t eb = static_cast<std::uint32_t>(std::max(1, plan::elem_bytes(s.dtype)));
69 244 brass::verify::GpuBufferSpec spec;
70 244 spec.param_index = n_out + k;
71 244 spec.elem_bytes = eb;
72
1/2
✓ Branch 0 taken 244 times.
✗ Branch 1 not taken.
244 spec.base_alignment = buffer_align_for(s, lanes);
73
2/2
✓ Branch 0 taken 102 times.
✓ Branch 1 taken 142 times.
244 if (s.bcast == BroadcastKind::Full) {
74 142 spec.length_param_index = static_cast<std::int32_t>(n_idx);
75 142 spec.elem_scale = lane_u32;
76 142 } else {
77
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 spec.static_byte_size = buffer_byte_size_for(plan, s);
78 }
79
1/2
✓ Branch 0 taken 244 times.
✗ Branch 1 not taken.
244 contract.buffers.push_back(spec);
80 244 }
81
82 68 const std::uint64_t max_slots =
83
2/4
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
68 static_cast<std::uint64_t>(std::max<std::int64_t>(1, plan.numel / std::max(1, lanes)));
84 68 brass::verify::GpuParamConstraint n_bound;
85 68 n_bound.param_index = n_idx;
86 68 n_bound.min_val = 1;
87 68 n_bound.max_val = max_slots;
88
1/2
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
68 contract.param_constraints.push_back(n_bound);
89 68 return contract;
90 68 }
91
92 48 brass::verify::GpuKernelContract build_row_norm_contract(
93 const plan::RowNormPlan& plan,
94 int lanes) {
95 48 const plan::ElementwisePlan& ew = plan.ew;
96 48 int tpr = 0;
97 48 int rpb = 0;
98 48 plan::row_norm_geometry(ew.cols, ew.rows, std::max(1, lanes), tpr, rpb);
99
100 48 brass::verify::GpuKernelContract contract;
101
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 contract.local_size_x = static_cast<std::uint32_t>(std::max(1, tpr * rpb));
102 48 contract.local_size_y = 1;
103 48 contract.local_size_z = 1;
104
105 48 const std::uint32_t n_out = static_cast<std::uint32_t>(ew.outputs.size());
106 48 const std::uint32_t n_in = static_cast<std::uint32_t>(ew.inputs.size());
107
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 const std::uint32_t lane_u32 = static_cast<std::uint32_t>(std::max(1, lanes));
108
109
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 48 times.
100 for (std::uint32_t m = 0; m < n_out; ++m) {
110 52 const plan::BufferSpec& s = ew.outputs[m];
111
2/4
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 52 times.
✗ Branch 3 not taken.
52 const std::uint32_t eb = static_cast<std::uint32_t>(std::max(1, plan::elem_bytes(s.dtype)));
112 52 brass::verify::GpuBufferSpec spec;
113 52 spec.param_index = m;
114
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 spec.static_byte_size = static_cast<std::uint64_t>(std::max(1, ew.rows)) *
115
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 static_cast<std::uint64_t>(std::max(1, ew.cols)) * eb;
116 52 spec.elem_bytes = eb;
117 52 spec.base_alignment = eb * lane_u32;
118
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 contract.buffers.push_back(spec);
119 52 }
120
121
2/2
✓ Branch 0 taken 138 times.
✓ Branch 1 taken 48 times.
186 for (std::uint32_t k = 0; k < n_in; ++k) {
122 138 const plan::BufferSpec& s = ew.inputs[k];
123
2/4
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 138 times.
✗ Branch 3 not taken.
138 const std::uint32_t eb = static_cast<std::uint32_t>(std::max(1, plan::elem_bytes(s.dtype)));
124 138 brass::verify::GpuBufferSpec spec;
125 138 spec.param_index = n_out + k;
126
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 spec.static_byte_size = buffer_byte_size_for(ew, s);
127 138 spec.elem_bytes = eb;
128
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 spec.base_alignment = buffer_align_for(s, lanes);
129
1/2
✓ Branch 0 taken 138 times.
✗ Branch 1 not taken.
138 contract.buffers.push_back(spec);
130 138 }
131
132 48 return contract;
133 48 }
134
135 6 TraceProofResult verify_and_issue_trace_receipt(
136 const brass::target::SpirvKernel& kernel,
137 const brass::verify::GpuKernelContract& contract) {
138 6 TraceProofResult out;
139
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (kernel.words.empty()) {
140
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ out.diagnostic = "empty SPIR-V module";
141 ✗ return out;
142 }
143
144
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 brass::spirv::Module spv_mod;
145 6 std::string parse_err;
146
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
6 if (!brass::spirv::parse_binary(kernel.words, spv_mod, &parse_err)) {
147
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.
✗ out.diagnostic = parse_err.empty() ? "failed to parse SPIR-V binary" : parse_err;
148 ✗ return out;
149 }
150
151
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 const brass::verify::GpuProverOptions opts = contract.to_prover_options(
152 /*loop_unroll_bound=*/2, /*max_sat_conflicts=*/4000);
153
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 const auto mem = brass::verify::verify_spirv_memory_safety(spv_mod, opts);
154
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 const auto bar = brass::verify::verify_spirv_barrier_uniformity(spv_mod, opts);
155
156
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 if (mem.is_safe() && bar.is_uniform()) {
157 6 out.proven = true;
158 6 out.receipt = brass::verify::issue_gpu_proof_receipt(
159
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 kernel.words, contract, mem, bar, brass::HardeningTier::PureSmtZeroOverhead, 0);
160
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 out.diagnostic = "verified via SPIR-V SMT prover";
161 6 return out;
162 }
163
164
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ std::ostringstream oss;
165
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (!mem.is_safe()) {
166
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (!mem.violations.empty() && !mem.violations.front().description.empty()) {
167
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ oss << mem.violations.front().description;
168
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (!mem.diagnostic.empty()) {
169
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ oss << mem.diagnostic;
170 ✗ } else {
171
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ oss << "SPIR-V memory safety check did not prove safety";
172 }
173 ✗ }
174
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (!bar.is_uniform()) {
175
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 (oss.tellp() > 0) oss << "; ";
176
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (!bar.divergences.empty() && !bar.divergences.front().description.empty()) {
177
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ oss << bar.divergences.front().description;
178
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ } else if (!bar.diagnostic.empty()) {
179
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ oss << bar.diagnostic;
180 ✗ } else {
181
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ oss << "SPIR-V barrier uniformity check failed";
182 }
183 ✗ }
184
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ out.diagnostic = oss.str();
185 ✗ return out;
186 6 }
187
188 1 TraceTuneResult tune_elementwise_spirv_verified(
189 const TraceDAG& dag,
190 const plan::ElementwisePlan& plan,
191 int lanes) {
192 1 TraceTuneResult out;
193
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 const brass::verify::GpuKernelContract contract = build_elementwise_contract(plan, lanes);
194
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 brass::Module mod("mod_trace_tune");
195 1 const char* name = (lanes > 1) ? kEntryVec : kEntryScalar;
196
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::Function* ref_fn = gpu_mir::build_elementwise_fn(mod, dag, plan, lanes, name, /*optimize=*/false);
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (!ref_fn) {
198
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ out.diagnostic = "failed to build reference elementwise MIR";
199 ✗ return out;
200 }
201
202 1 brass::GpuOptOptions base_opts;
203 1 base_opts.opt_level = 1;
204 1 base_opts.enable_fma = true;
205
206 1 brass::GpuOptOptions pg_opts;
207 1 pg_opts.opt_level = 2;
208 1 pg_opts.enable_fma = true;
209 1 pg_opts.enable_small_loop_unroll = true;
210 1 pg_opts.enable_load_coalesce = true;
211 1 pg_opts.enable_proof_guided_opt = true;
212 1 pg_opts.contract = &contract;
213
214 1 brass::GpuOptOptions super_opts = pg_opts;
215 1 super_opts.enable_gpu_superopt = true;
216
217
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 const std::vector<brass::verify::GpuTuneCandidate> candidates = {
218
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 {"baseline-o1", base_opts, {}},
219
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 {"proof-guided-coalesce", pg_opts, {}},
220
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 {"proof-guided-superopt", super_opts, {}},
221 };
222
223 1 brass::verify::GpuTuneOptions tune_opts;
224 1 tune_opts.spirv_opts.stage = brass::target::SpirvStage::Compute;
225 1 tune_opts.spirv_opts.local_size_x = kEwBlock;
226 1 tune_opts.loop_unroll_bound = 2;
227 1 tune_opts.max_sat_conflicts = 4000;
228 1 tune_opts.check_mir_equivalence = true;
229
230 const brass::verify::GpuTuneResult tres =
231
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 brass::verify::tune_gpu_kernel_verified(*ref_fn, contract, candidates, tune_opts);
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (!tres.found) {
233
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ out.diagnostic = "no candidate satisfied verified auto-tuning gate";
234 ✗ return out;
235 }
236 1 out.tuned = true;
237
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 out.spirv = std::move(tres.best_spirv);
238 1 out.receipt = tres.best_receipt;
239
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 out.winning_candidate = tres.best_name;
240 1 out.winning_cost = tres.best_cost;
241
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 out.diagnostic = "tuned winning candidate '" + out.winning_candidate + "'";
242 1 return out;
243 1 }
244
245 4 bool validate_trace_bind_args(
246 const brass::verify::GpuKernelContract& contract,
247 std::span<const std::uint64_t> out_addrs,
248 std::span<const std::uint64_t> in_addrs,
249 std::span<const std::uint64_t> out_bytes,
250 std::span<const std::uint64_t> in_bytes,
251 const std::uint32_t* slot_count,
252 std::string* err_out) {
253
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 if (out_addrs.size() != out_bytes.size() || in_addrs.size() != in_bytes.size()) {
254
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (err_out) *err_out = "mismatched address/byte-size span lengths for trace bind";
255 ✗ return false;
256 }
257 4 std::vector<brass::verify::GpuDispatchArg> args;
258
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 args.reserve(out_addrs.size() + in_addrs.size() + (slot_count ? 1u : 0u));
259
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 for (std::size_t i = 0; i < out_addrs.size(); ++i) {
260
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 args.push_back(brass::verify::GpuDispatchArg::buffer(out_bytes[i], out_addrs[i]));
261 4 }
262
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
16 for (std::size_t i = 0; i < in_addrs.size(); ++i) {
263
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 args.push_back(brass::verify::GpuDispatchArg::buffer(in_bytes[i], in_addrs[i]));
264 12 }
265
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (slot_count) {
266
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 args.push_back(brass::verify::GpuDispatchArg::scalar(static_cast<std::uint64_t>(*slot_count)));
267 4 }
268
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 return brass::verify::validate_gpu_dispatch_contract(contract, args, err_out);
269 4 }
270
271 } // namespace brotensor::jit::spirv
272