src/jit/trace_plan.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Device-neutral fusion plans (trace_plan.h). Moved out of the CUDA trace | ||
| 2 | // compiler so the Vulkan compiler builds exactly the same plans. | ||
| 3 | |||
| 4 | #include "trace_plan.h" | ||
| 5 | |||
| 6 | #include <algorithm> | ||
| 7 | #include <cstddef> | ||
| 8 | #include <cstdint> | ||
| 9 | |||
| 10 | namespace brotensor::jit::plan { | ||
| 11 | |||
| 12 | 2680 | int elem_bytes(Dtype d) { | |
| 13 | 2680 | switch (d) { | |
| 14 | 2228 | case Dtype::FP32: return 4; | |
| 15 | case Dtype::FP16: | ||
| 16 | 452 | case Dtype::BF16: return 2; | |
| 17 | ✗ | default: return 0; | |
| 18 | } | ||
| 19 | 2680 | } | |
| 20 | |||
| 21 | 238 | bool dtype_supported(Dtype d) { return elem_bytes(d) != 0; } | |
| 22 | |||
| 23 | 238 | int vec_width_for(Dtype d) { | |
| 24 | 238 | const int e = elem_bytes(d); | |
| 25 | 238 | return e ? 16 / e : 1; | |
| 26 | } | ||
| 27 | |||
| 28 | 135 | void row_norm_geometry(int cols, int rows, int lanes, int& tpr, int& rpb) { | |
| 29 | 135 | if (lanes < 1) lanes = 1; | |
| 30 | 135 | const int per_row = (cols + lanes - 1) / lanes; // threads to cover a row | |
| 31 | 135 | int t = 32; | |
| 32 | 373 | while (t < per_row && t < kRowThreads) t <<= 1; | |
| 33 | 135 | int r = kRowThreads / t; | |
| 34 | // The out-of-range exit has to be block-uniform, or the threads that stay | ||
| 35 | // hit a barrier the ones that left never will. | ||
| 36 | 135 | while (r > 1 && rows % r != 0) r >>= 1; | |
| 37 | 135 | tpr = t; | |
| 38 | 135 | rpb = r; | |
| 39 | 135 | } | |
| 40 | |||
| 41 | 48 | bool build_plan(const TraceDAG& dag, ElementwisePlan& plan) { | |
| 42 | 48 | int rows = 0, cols = 0; | |
| 43 | 48 | dag.dominant_shape(rows, cols); | |
| 44 | 48 | if (rows <= 0 || cols <= 0) return false; | |
| 45 | |||
| 46 | 48 | plan.rows = rows; | |
| 47 | 48 | plan.cols = cols; | |
| 48 | 48 | plan.numel = static_cast<std::int64_t>(rows) * cols; | |
| 49 | |||
| 50 | 48 | int lanes = 8; | |
| 51 | 1425 | for (const auto& n : dag.nodes()) { | |
| 52 | 1377 | const bool is_in = (n.op == TraceOpKind::Input); | |
| 53 | 1377 | const bool is_out = n.is_live_at_end; | |
| 54 | 1377 | if (!is_in && !is_out) continue; | |
| 55 | 238 | if (!dtype_supported(n.dtype)) return false; | |
| 56 | |||
| 57 | 238 | const BroadcastKind bk = broadcast_of(n.rows, n.cols, rows, cols); | |
| 58 | 238 | if (bk == BroadcastKind::Full && | |
| 59 | 169 | static_cast<std::int64_t>(n.rows) * n.cols != plan.numel) { | |
| 60 | ✗ | return false; | |
| 61 | } | ||
| 62 | 238 | if (is_out && bk != BroadcastKind::Full) return false; | |
| 63 | |||
| 64 | 238 | lanes = std::min(lanes, vec_width_for(n.dtype)); | |
| 65 | 238 | if (bk == BroadcastKind::Row) plan.any_row_bcast = true; | |
| 66 | 238 | if (bk == BroadcastKind::Col) plan.any_col_bcast = true; | |
| 67 | |||
| 68 | 238 | const BufferSpec spec{n.id, n.dtype, bk}; | |
| 69 | 238 | if (is_in) plan.inputs.push_back(spec); | |
| 70 | 238 | if (is_out) plan.outputs.push_back(spec); | |
| 71 | } | ||
| 72 | 48 | if (plan.outputs.empty()) return false; | |
| 73 | |||
| 74 | 48 | if (plan.numel % lanes != 0) lanes = 1; | |
| 75 | 48 | if ((plan.any_row_bcast || plan.any_col_bcast) && cols % lanes != 0) lanes = 1; | |
| 76 | 48 | plan.vec = lanes; | |
| 77 | 48 | return true; | |
| 78 | 48 | } | |
| 79 | |||
| 80 | 23 | bool build_row_norm_plan(const TraceDAG& dag, RowNormPlan& plan) { | |
| 81 | 23 | const auto& nodes = dag.nodes(); | |
| 82 | 23 | const TraceNode* norm = nullptr; | |
| 83 | 132 | for (const auto& n : nodes) { | |
| 84 | 109 | if (n.op == TraceOpKind::RMSNorm || n.op == TraceOpKind::LayerNorm) { | |
| 85 | 19 | if (norm) return false; // more than one reduction | |
| 86 | 19 | norm = &n; | |
| 87 | 19 | } | |
| 88 | } | ||
| 89 | 23 | if (!norm || norm->inputs.empty()) return false; | |
| 90 | |||
| 91 | 19 | if (!build_plan(dag, plan.ew)) return false; | |
| 92 | 19 | if (plan.ew.cols != norm->cols) return false; | |
| 93 | 19 | if (plan.ew.rows != norm->rows) return false; | |
| 94 | |||
| 95 | 48 | auto input_index = [&](int node_id) { | |
| 96 | 81 | for (std::size_t i = 0; i < plan.ew.inputs.size(); ++i) { | |
| 97 | 81 | if (plan.ew.inputs[i].node_id == node_id) return static_cast<int>(i); | |
| 98 | 52 | } | |
| 99 | ✗ | return -1; | |
| 100 | 29 | }; | |
| 101 | 19 | if (norm->inputs.size() > 1) { | |
| 102 | 19 | plan.gamma_input = input_index(norm->inputs[1]); | |
| 103 | 19 | if (plan.gamma_input < 0) return false; | |
| 104 | 19 | } | |
| 105 | 19 | if (norm->inputs.size() > 2) { | |
| 106 | 10 | plan.beta_input = input_index(norm->inputs[2]); | |
| 107 | 10 | if (plan.beta_input < 0) return false; | |
| 108 | 10 | } | |
| 109 | |||
| 110 | 19 | plan.x_node = norm->inputs[0]; | |
| 111 | 19 | plan.norm_node = norm->id; | |
| 112 | 19 | plan.reduce = (norm->op == TraceOpKind::LayerNorm) ? RowReduce::Mean : RowReduce::RMS; | |
| 113 | 19 | plan.eps = norm->scalar > 0.0f ? norm->scalar : 1e-5f; | |
| 114 | |||
| 115 | // Reverse reachability from the reduction's data input. Node ids are | ||
| 116 | // assigned in creation order, which is topological, so one backward sweep | ||
| 117 | // is enough. | ||
| 118 | 19 | std::vector<char> in_pre(nodes.size(), 0); | |
| 119 | 19 | if (plan.x_node >= 0) in_pre[static_cast<std::size_t>(plan.x_node)] = 1; | |
| 120 | 104 | for (std::size_t i = nodes.size(); i-- > 0;) { | |
| 121 | 85 | if (!in_pre[i]) continue; | |
| 122 | 51 | for (int src : nodes[i].inputs) { | |
| 123 | 16 | if (src >= 0) in_pre[static_cast<std::size_t>(src)] = 1; | |
| 124 | } | ||
| 125 | } | ||
| 126 | 19 | in_pre[static_cast<std::size_t>(norm->id)] = 0; | |
| 127 | |||
| 128 | 104 | for (const auto& n : nodes) { | |
| 129 | 85 | if (n.op == TraceOpKind::Input) continue; | |
| 130 | 29 | if (n.id == norm->id) continue; | |
| 131 | 10 | if (in_pre[static_cast<std::size_t>(n.id)]) { | |
| 132 | 8 | plan.pre_ids.push_back(n.id); | |
| 133 | 8 | } else { | |
| 134 | 2 | plan.post_ids.push_back(n.id); | |
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | 19 | plan.pre_input.assign(plan.ew.inputs.size(), 0); | |
| 139 | 75 | for (std::size_t i = 0; i < plan.ew.inputs.size(); ++i) { | |
| 140 | 56 | const int id = plan.ew.inputs[i].node_id; | |
| 141 | 56 | plan.pre_input[i] = in_pre[static_cast<std::size_t>(id)] ? 1 : 0; | |
| 142 | 56 | } | |
| 143 | 19 | plan.pre_output.assign(plan.ew.outputs.size(), 0); | |
| 144 | 40 | for (std::size_t i = 0; i < plan.ew.outputs.size(); ++i) { | |
| 145 | 21 | const int id = plan.ew.outputs[i].node_id; | |
| 146 | 21 | plan.pre_output[i] = in_pre[static_cast<std::size_t>(id)] ? 1 : 0; | |
| 147 | 21 | } | |
| 148 | |||
| 149 | // Pass two can skip the pre chain entirely when its result is one of the | ||
| 150 | // trace's own outputs — pass one writes it, pass two reads it back — and | ||
| 151 | // when nothing downstream of the reduction needs any *other* pre value. | ||
| 152 | 19 | plan.x_output = -1; | |
| 153 | 19 | if (!plan.pre_ids.empty()) { | |
| 154 | 8 | int x_out = -1; | |
| 155 | 18 | for (std::size_t i = 0; i < plan.ew.outputs.size(); ++i) { | |
| 156 | 10 | if (plan.ew.outputs[i].node_id == plan.x_node) x_out = static_cast<int>(i); | |
| 157 | 10 | } | |
| 158 | 8 | bool post_only_needs_x = true; | |
| 159 | 9 | for (int pid : plan.post_ids) { | |
| 160 | 2 | for (int src : nodes[static_cast<std::size_t>(pid)].inputs) { | |
| 161 | 1 | if (src < 0) continue; | |
| 162 | 1 | if (src == plan.x_node) continue; | |
| 163 | 1 | if (nodes[static_cast<std::size_t>(src)].op == TraceOpKind::Input) continue; | |
| 164 | 1 | if (in_pre[static_cast<std::size_t>(src)]) post_only_needs_x = false; | |
| 165 | } | ||
| 166 | } | ||
| 167 | 8 | if (x_out >= 0 && post_only_needs_x) plan.x_output = x_out; | |
| 168 | 8 | } | |
| 169 | |||
| 170 | // Without the reload, pass two replays the pre chain from its inputs — | ||
| 171 | // and pass one has already overwritten whatever it stored. An in-place | ||
| 172 | // pre chain (`x += p` with x a pass-one output) would replay as | ||
| 173 | // x + p + p. Only the reload form is correct once pass one stores. | ||
| 174 | 19 | if (plan.x_output < 0) { | |
| 175 | 34 | for (char w : plan.pre_output) { | |
| 176 | 17 | if (w) return false; | |
| 177 | } | ||
| 178 | 17 | } | |
| 179 | |||
| 180 | // What pass two loads: with the reload it is the reduction's gamma/beta | ||
| 181 | // plus whatever the post chain reads; without it, everything. | ||
| 182 | 19 | plan.post_input.assign(plan.ew.inputs.size(), plan.x_output >= 0 ? 0 : 1); | |
| 183 | 19 | if (plan.x_output >= 0) { | |
| 184 | 2 | if (plan.gamma_input >= 0) plan.post_input[static_cast<std::size_t>(plan.gamma_input)] = 1; | |
| 185 | 2 | if (plan.beta_input >= 0) plan.post_input[static_cast<std::size_t>(plan.beta_input)] = 1; | |
| 186 | 2 | for (int pid : plan.post_ids) { | |
| 187 | ✗ | for (int src : nodes[static_cast<std::size_t>(pid)].inputs) { | |
| 188 | ✗ | if (src < 0) continue; | |
| 189 | ✗ | for (std::size_t i = 0; i < plan.ew.inputs.size(); ++i) { | |
| 190 | ✗ | if (plan.ew.inputs[i].node_id == src) plan.post_input[i] = 1; | |
| 191 | ✗ | } | |
| 192 | } | ||
| 193 | } | ||
| 194 | 2 | } | |
| 195 | |||
| 196 | // The row kernel's stride loop takes `vec` columns per thread per step. | ||
| 197 | // build_plan already reduced `vec` to the narrowest dtype in play; the row | ||
| 198 | // form additionally needs D to divide evenly so a vector never straddles a | ||
| 199 | // row boundary. | ||
| 200 | 19 | if (plan.ew.cols % plan.ew.vec != 0) plan.ew.vec = 1; | |
| 201 | 19 | return true; | |
| 202 | 23 | } | |
| 203 | |||
| 204 | ✗ | bool all_fp32(const TraceDAG& dag) { | |
| 205 | ✗ | for (const auto& n : dag.nodes()) { | |
| 206 | ✗ | if (n.dtype != Dtype::FP32) return false; | |
| 207 | } | ||
| 208 | ✗ | return true; | |
| 209 | ✗ | } | |
| 210 | |||
| 211 | } // namespace brotensor::jit::plan | ||
| 212 |