brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
129 / 142
90.8%
Branches:
114 / 165
69.1%
Functions:
7 / 8
87.5%

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
2/3
✓ Branch 0 taken 452 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2228 times.
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
1/2
✓ Branch 0 taken 238 times.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 135 times.
✗ Branch 1 not taken.
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
4/4
✓ Branch 0 taken 83 times.
✓ Branch 1 taken 290 times.
✓ Branch 2 taken 238 times.
✓ Branch 3 taken 135 times.
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
3/4
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 75 times.
✓ Branch 2 taken 135 times.
✗ Branch 3 not taken.
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
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
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
2/2
✓ Branch 0 taken 1377 times.
✓ Branch 1 taken 48 times.
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
4/4
✓ Branch 0 taken 1213 times.
✓ Branch 1 taken 164 times.
✓ Branch 2 taken 74 times.
✓ Branch 3 taken 1139 times.
1377 if (!is_in && !is_out) continue;
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 238 times.
238 if (!dtype_supported(n.dtype)) return false;
56
57 238 const BroadcastKind bk = broadcast_of(n.rows, n.cols, rows, cols);
58
3/4
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 69 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 169 times.
238 if (bk == BroadcastKind::Full &&
59 169 static_cast<std::int64_t>(n.rows) * n.cols != plan.numel) {
60 ✗ return false;
61 }
62
3/4
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 164 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 74 times.
238 if (is_out && bk != BroadcastKind::Full) return false;
63
64 238 lanes = std::min(lanes, vec_width_for(n.dtype));
65
2/2
✓ Branch 0 taken 190 times.
✓ Branch 1 taken 48 times.
238 if (bk == BroadcastKind::Row) plan.any_row_bcast = true;
66
2/2
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 19 times.
238 if (bk == BroadcastKind::Col) plan.any_col_bcast = true;
67
68 238 const BufferSpec spec{n.id, n.dtype, bk};
69
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 164 times.
238 if (is_in) plan.inputs.push_back(spec);
70
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 74 times.
238 if (is_out) plan.outputs.push_back(spec);
71 }
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (plan.outputs.empty()) return false;
73
74
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 if (plan.numel % lanes != 0) lanes = 1;
75
3/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
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
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 23 times.
132 for (const auto& n : nodes) {
84
4/4
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 90 times.
109 if (n.op == TraceOpKind::RMSNorm || n.op == TraceOpKind::LayerNorm) {
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (norm) return false; // more than one reduction
86 19 norm = &n;
87 19 }
88 }
89
3/4
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19 times.
23 if (!norm || norm->inputs.empty()) return false;
90
91
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (!build_plan(dag, plan.ew)) return false;
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (plan.ew.cols != norm->cols) return false;
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (plan.ew.rows != norm->rows) return false;
94
95 48 auto input_index = [&](int node_id) {
96
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 for (std::size_t i = 0; i < plan.ew.inputs.size(); ++i) {
97
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 52 times.
81 if (plan.ew.inputs[i].node_id == node_id) return static_cast<int>(i);
98 52 }
99 ✗ return -1;
100 29 };
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (norm->inputs.size() > 1) {
102 19 plan.gamma_input = input_index(norm->inputs[1]);
103
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (plan.gamma_input < 0) return false;
104 19 }
105
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 10 times.
19 if (norm->inputs.size() > 2) {
106 10 plan.beta_input = input_index(norm->inputs[2]);
107
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (plan.x_node >= 0) in_pre[static_cast<std::size_t>(plan.x_node)] = 1;
120
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 19 times.
104 for (std::size_t i = nodes.size(); i-- > 0;) {
121
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 35 times.
85 if (!in_pre[i]) continue;
122
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 35 times.
51 for (int src : nodes[i].inputs) {
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
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
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 85 times.
104 for (const auto& n : nodes) {
129
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 56 times.
85 if (n.op == TraceOpKind::Input) continue;
130
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 10 times.
29 if (n.id == norm->id) continue;
131
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 if (in_pre[static_cast<std::size_t>(n.id)]) {
132
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 plan.pre_ids.push_back(n.id);
133 8 } else {
134
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 plan.post_ids.push_back(n.id);
135 }
136 }
137
138
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 plan.pre_input.assign(plan.ew.inputs.size(), 0);
139
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 56 times.
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
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 plan.pre_output.assign(plan.ew.outputs.size(), 0);
144
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 19 times.
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
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 8 times.
19 if (!plan.pre_ids.empty()) {
154 8 int x_out = -1;
155
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 8 times.
18 for (std::size_t i = 0; i < plan.ew.outputs.size(); ++i) {
156
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
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
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 8 times.
9 for (int pid : plan.post_ids) {
160
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 for (int src : nodes[static_cast<std::size_t>(pid)].inputs) {
161
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (src < 0) continue;
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (src == plan.x_node) continue;
163
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (nodes[static_cast<std::size_t>(src)].op == TraceOpKind::Input) continue;
164
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (in_pre[static_cast<std::size_t>(src)]) post_only_needs_x = false;
165 }
166 }
167
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
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
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17 times.
19 if (plan.x_output < 0) {
175
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
34 for (char w : plan.pre_output) {
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
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
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 plan.post_input.assign(plan.ew.inputs.size(), plan.x_output >= 0 ? 0 : 1);
183
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2 times.
19 if (plan.x_output >= 0) {
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (plan.gamma_input >= 0) plan.post_input[static_cast<std::size_t>(plan.gamma_input)] = 1;
185
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (plan.beta_input >= 0) plan.post_input[static_cast<std::size_t>(plan.beta_input)] = 1;
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 for (int pid : plan.post_ids) {
187
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int src : nodes[static_cast<std::size_t>(pid)].inputs) {
188
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (src < 0) continue;
189
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (std::size_t i = 0; i < plan.ew.inputs.size(); ++i) {
190
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (const auto& n : dag.nodes()) {
206
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (n.dtype != Dtype::FP32) return false;
207 }
208 ✗ return true;
209 ✗ }
210
211 } // namespace brotensor::jit::plan
212