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 | 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 | 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 | 68 | const std::uint32_t lane_u32 = static_cast<std::uint32_t>(std::max(1, lanes)); | |
| 53 | |||
| 54 | 184 | for (std::uint32_t m = 0; m < n_out; ++m) { | |
| 55 | 116 | const plan::BufferSpec& s = plan.outputs[m]; | |
| 56 | 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 | 116 | contract.buffers.push_back(spec); | |
| 64 | 116 | } | |
| 65 | |||
| 66 | 312 | for (std::uint32_t k = 0; k < n_in; ++k) { | |
| 67 | 244 | const plan::BufferSpec& s = plan.inputs[k]; | |
| 68 | 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 | 244 | spec.base_alignment = buffer_align_for(s, lanes); | |
| 73 | 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 | 102 | spec.static_byte_size = buffer_byte_size_for(plan, s); | |
| 78 | } | ||
| 79 | 244 | contract.buffers.push_back(spec); | |
| 80 | 244 | } | |
| 81 | |||
| 82 | 68 | const std::uint64_t max_slots = | |
| 83 | 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 | 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 | 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 | 48 | const std::uint32_t lane_u32 = static_cast<std::uint32_t>(std::max(1, lanes)); | |
| 108 | |||
| 109 | 100 | for (std::uint32_t m = 0; m < n_out; ++m) { | |
| 110 | 52 | const plan::BufferSpec& s = ew.outputs[m]; | |
| 111 | 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 | 52 | spec.static_byte_size = static_cast<std::uint64_t>(std::max(1, ew.rows)) * | |
| 115 | 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 | 52 | contract.buffers.push_back(spec); | |
| 119 | 52 | } | |
| 120 | |||
| 121 | 186 | for (std::uint32_t k = 0; k < n_in; ++k) { | |
| 122 | 138 | const plan::BufferSpec& s = ew.inputs[k]; | |
| 123 | 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 | 138 | spec.static_byte_size = buffer_byte_size_for(ew, s); | |
| 127 | 138 | spec.elem_bytes = eb; | |
| 128 | 138 | spec.base_alignment = buffer_align_for(s, lanes); | |
| 129 | 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 | 6 | if (kernel.words.empty()) { | |
| 140 | ✗ | out.diagnostic = "empty SPIR-V module"; | |
| 141 | ✗ | return out; | |
| 142 | } | ||
| 143 | |||
| 144 | 6 | brass::spirv::Module spv_mod; | |
| 145 | 6 | std::string parse_err; | |
| 146 | 6 | if (!brass::spirv::parse_binary(kernel.words, spv_mod, &parse_err)) { | |
| 147 | ✗ | out.diagnostic = parse_err.empty() ? "failed to parse SPIR-V binary" : parse_err; | |
| 148 | ✗ | return out; | |
| 149 | } | ||
| 150 | |||
| 151 | 6 | const brass::verify::GpuProverOptions opts = contract.to_prover_options( | |
| 152 | /*loop_unroll_bound=*/2, /*max_sat_conflicts=*/4000); | ||
| 153 | 6 | const auto mem = brass::verify::verify_spirv_memory_safety(spv_mod, opts); | |
| 154 | 6 | const auto bar = brass::verify::verify_spirv_barrier_uniformity(spv_mod, opts); | |
| 155 | |||
| 156 | 6 | if (mem.is_safe() && bar.is_uniform()) { | |
| 157 | 6 | out.proven = true; | |
| 158 | 6 | out.receipt = brass::verify::issue_gpu_proof_receipt( | |
| 159 | 6 | kernel.words, contract, mem, bar, brass::HardeningTier::PureSmtZeroOverhead, 0); | |
| 160 | 6 | out.diagnostic = "verified via SPIR-V SMT prover"; | |
| 161 | 6 | return out; | |
| 162 | } | ||
| 163 | |||
| 164 | ✗ | std::ostringstream oss; | |
| 165 | ✗ | if (!mem.is_safe()) { | |
| 166 | ✗ | if (!mem.violations.empty() && !mem.violations.front().description.empty()) { | |
| 167 | ✗ | oss << mem.violations.front().description; | |
| 168 | ✗ | } else if (!mem.diagnostic.empty()) { | |
| 169 | ✗ | oss << mem.diagnostic; | |
| 170 | ✗ | } else { | |
| 171 | ✗ | oss << "SPIR-V memory safety check did not prove safety"; | |
| 172 | } | ||
| 173 | ✗ | } | |
| 174 | ✗ | if (!bar.is_uniform()) { | |
| 175 | ✗ | if (oss.tellp() > 0) oss << "; "; | |
| 176 | ✗ | if (!bar.divergences.empty() && !bar.divergences.front().description.empty()) { | |
| 177 | ✗ | oss << bar.divergences.front().description; | |
| 178 | ✗ | } else if (!bar.diagnostic.empty()) { | |
| 179 | ✗ | oss << bar.diagnostic; | |
| 180 | ✗ | } else { | |
| 181 | ✗ | oss << "SPIR-V barrier uniformity check failed"; | |
| 182 | } | ||
| 183 | ✗ | } | |
| 184 | ✗ | 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 | const brass::verify::GpuKernelContract contract = build_elementwise_contract(plan, lanes); | |
| 194 | 1 | brass::Module mod("mod_trace_tune"); | |
| 195 | 1 | const char* name = (lanes > 1) ? kEntryVec : kEntryScalar; | |
| 196 | 1 | brass::Function* ref_fn = gpu_mir::build_elementwise_fn(mod, dag, plan, lanes, name, /*optimize=*/false); | |
| 197 | 1 | if (!ref_fn) { | |
| 198 | ✗ | 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 | const std::vector<brass::verify::GpuTuneCandidate> candidates = { | |
| 218 | 1 | {"baseline-o1", base_opts, {}}, | |
| 219 | 1 | {"proof-guided-coalesce", pg_opts, {}}, | |
| 220 | 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 | 1 | brass::verify::tune_gpu_kernel_verified(*ref_fn, contract, candidates, tune_opts); | |
| 232 | 1 | if (!tres.found) { | |
| 233 | ✗ | out.diagnostic = "no candidate satisfied verified auto-tuning gate"; | |
| 234 | ✗ | return out; | |
| 235 | } | ||
| 236 | 1 | out.tuned = true; | |
| 237 | 1 | out.spirv = std::move(tres.best_spirv); | |
| 238 | 1 | out.receipt = tres.best_receipt; | |
| 239 | 1 | out.winning_candidate = tres.best_name; | |
| 240 | 1 | out.winning_cost = tres.best_cost; | |
| 241 | 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 | 4 | if (out_addrs.size() != out_bytes.size() || in_addrs.size() != in_bytes.size()) { | |
| 254 | ✗ | 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 | 4 | args.reserve(out_addrs.size() + in_addrs.size() + (slot_count ? 1u : 0u)); | |
| 259 | 8 | for (std::size_t i = 0; i < out_addrs.size(); ++i) { | |
| 260 | 4 | args.push_back(brass::verify::GpuDispatchArg::buffer(out_bytes[i], out_addrs[i])); | |
| 261 | 4 | } | |
| 262 | 16 | for (std::size_t i = 0; i < in_addrs.size(); ++i) { | |
| 263 | 12 | args.push_back(brass::verify::GpuDispatchArg::buffer(in_bytes[i], in_addrs[i])); | |
| 264 | 12 | } | |
| 265 | 4 | if (slot_count) { | |
| 266 | 4 | args.push_back(brass::verify::GpuDispatchArg::scalar(static_cast<std::uint64_t>(*slot_count))); | |
| 267 | 4 | } | |
| 268 | 4 | return brass::verify::validate_gpu_dispatch_contract(contract, args, err_out); | |
| 269 | 4 | } | |
| 270 | |||
| 271 | } // namespace brotensor::jit::spirv | ||
| 272 |