src/jit/gpu_mir_emit_elementwise.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Elementwise trace kernel as brass MIR (spirv_emit.h): a grid-stride loop | ||
| 2 | // over `n` slots of `lanes` elements; every operand addressed by its | ||
| 3 | // broadcast kind, every buffer at its own dtype. | ||
| 4 | |||
| 5 | #include "gpu_mir_emit_detail.h" | ||
| 6 | #if BRASS_WITH_SPIRV | ||
| 7 | #include "trace_contract.h" | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #include <string> | ||
| 11 | #include <unordered_map> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | namespace brotensor::jit::gpu_mir { | ||
| 15 | |||
| 16 | 65 | brass::Function* build_elementwise_fn(brass::Module& mod, const TraceDAG& dag, | |
| 17 | const plan::ElementwisePlan& plan, int lanes, | ||
| 18 | const char* name, bool optimize) { | ||
| 19 | 65 | const std::size_t n_out = plan.outputs.size(); | |
| 20 | 65 | const std::size_t n_in = plan.inputs.size(); | |
| 21 | 65 | std::vector<Type> params(n_out + n_in, Type::ptr()); | |
| 22 | 65 | params.push_back(Type::i32()); | |
| 23 | 65 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 24 | |||
| 25 | 65 | KernelBuilder kb(mod, fn); | |
| 26 | 65 | Ctx c(kb); | |
| 27 | 65 | Builder& b = c.b; | |
| 28 | 65 | const std::vector<Value*> p = entry_params(c, fn); | |
| 29 | 65 | Value* n = p.back(); | |
| 30 | |||
| 31 | 65 | Value* i0 = kb.global_tid_x(); | |
| 32 | 65 | Value* stride = b.build_mul(kb.ntid_x(), kb.nctaid_x()); | |
| 33 | |||
| 34 | 130 | kb.for_range(i0, n, stride, [&](Value* i) { | |
| 35 | // Element index of this thread's first lane, its column when some | ||
| 36 | // operand broadcasts across rows, and its row when some operand | ||
| 37 | // broadcasts across columns. | ||
| 38 | 65 | Value* base = lanes > 1 ? b.build_shl(i, c.i32(log2_exact(lanes))) : i; | |
| 39 | 65 | Value* col = nullptr; | |
| 40 | 65 | Value* row = nullptr; | |
| 41 | 65 | if (plan.any_row_bcast && plan.any_col_bcast) { | |
| 42 | 32 | udiv_urem_const(c, base, plan.cols, row, col); | |
| 43 | 65 | } else if (plan.any_row_bcast) { | |
| 44 | 3 | if (is_pow2(plan.cols)) { | |
| 45 | 3 | col = b.build_and(base, c.i32(plan.cols - 1)); | |
| 46 | 3 | } else { | |
| 47 | ✗ | Value* q = nullptr; | |
| 48 | ✗ | udiv_urem_const(c, base, plan.cols, q, col); | |
| 49 | } | ||
| 50 | 33 | } else if (plan.any_col_bcast) { | |
| 51 | ✗ | row = udiv_const(c, base, plan.cols); | |
| 52 | ✗ | } | |
| 53 | // Byte offsets, one per (broadcast kind, element size) actually used. | ||
| 54 | 65 | std::unordered_map<int, Value*> full_off, row_off, col_off; | |
| 55 | 410 | auto offset_for = [&](BroadcastKind bk, Dtype dt) -> Value* { | |
| 56 | 345 | const int esz = plan::elem_bytes(dt); | |
| 57 | 643 | auto& tbl = (bk == BroadcastKind::Row) ? row_off | |
| 58 | 298 | : (bk == BroadcastKind::Col) ? col_off : full_off; | |
| 59 | 345 | auto it = tbl.find(esz); | |
| 60 | 345 | if (it != tbl.end()) return it->second; | |
| 61 | 229 | Value* idx = (bk == BroadcastKind::Row) ? col | |
| 62 | 97 | : (bk == BroadcastKind::Col) ? row : base; | |
| 63 | 132 | Value* off = byte_offset(c, idx, esz); | |
| 64 | 132 | tbl[esz] = off; | |
| 65 | 132 | return off; | |
| 66 | 345 | }; | |
| 67 | |||
| 68 | 65 | ValueMap vals; | |
| 69 | 301 | for (std::size_t k = 0; k < n_in; ++k) { | |
| 70 | 236 | const plan::BufferSpec& s = plan.inputs[k]; | |
| 71 | 236 | Value* ptr = p[n_out + k]; | |
| 72 | 236 | if (s.bcast == BroadcastKind::Scalar) { | |
| 73 | // One load feeds every lane. | ||
| 74 | 4 | Value* v = load_lanes(c, s.dtype, 1, ptr)[0]; | |
| 75 | 4 | vals[s.node_id] = std::vector<Value*>(static_cast<std::size_t>(lanes), v); | |
| 76 | 236 | } else if (s.bcast == BroadcastKind::Col) { | |
| 77 | // All lanes of this thread sit in the same row (build_plan | ||
| 78 | // requires cols % lanes == 0 when any_col_bcast is true). | ||
| 79 | 48 | Value* v = load_lanes(c, s.dtype, 1, b.build_add(ptr, offset_for(s.bcast, s.dtype)))[0]; | |
| 80 | 48 | vals[s.node_id] = std::vector<Value*>(static_cast<std::size_t>(lanes), v); | |
| 81 | 48 | } else { | |
| 82 | 184 | vals[s.node_id] = load_lanes(c, s.dtype, lanes, b.build_add(ptr, offset_for(s.bcast, s.dtype))); | |
| 83 | } | ||
| 84 | 236 | } | |
| 85 | |||
| 86 | 65 | vals = eval_chain(c, dag, lanes, std::move(vals)); | |
| 87 | |||
| 88 | 178 | for (std::size_t m = 0; m < n_out; ++m) { | |
| 89 | 113 | const plan::BufferSpec& s = plan.outputs[m]; | |
| 90 | 113 | Value* addr = b.build_add(p[m], offset_for(BroadcastKind::Full, s.dtype)); | |
| 91 | 113 | store_lanes(c, s.dtype, lanes, addr, vals.at(s.node_id)); | |
| 92 | 113 | } | |
| 93 | 65 | }); | |
| 94 | 65 | b.build_ret_void(); | |
| 95 | 65 | if (optimize) { | |
| 96 | #if BRASS_WITH_SPIRV | ||
| 97 | 64 | const brass::verify::GpuKernelContract contract = spirv::build_elementwise_contract(plan, lanes); | |
| 98 | 64 | optimize_function(*fn, &contract); | |
| 99 | #else | ||
| 100 | optimize_function(*fn); | ||
| 101 | #endif | ||
| 102 | 64 | } | |
| 103 | 65 | return fn; | |
| 104 | 65 | } | |
| 105 | |||
| 106 | 7 | bool ops_supported(const TraceDAG& dag) { | |
| 107 | 46 | for (const auto& n : dag.nodes()) { | |
| 108 | 39 | if (n.op == TraceOpKind::Input || n.op == TraceOpKind::RMSNorm || | |
| 109 | 15 | n.op == TraceOpKind::LayerNorm) { | |
| 110 | 24 | continue; | |
| 111 | } | ||
| 112 | 15 | if (!has_lane_form(n.op)) return false; | |
| 113 | } | ||
| 114 | 7 | return true; | |
| 115 | 7 | } | |
| 116 | |||
| 117 | } // namespace brotensor::jit::gpu_mir | ||
| 118 |