brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
283 / 338
83.7%
Branches:
132 / 252
52.4%
Functions:
28 / 29
96.6%

src/jit/trace_dag.cpp
Line Branch Exec Source
1 #include "trace_dag.h"
2 #include <cstring>
3 #include <stdexcept>
4
5 namespace brotensor::jit {
6
7 ✗ const char* op_kind_name(TraceOpKind op) {
8
0/32
✗ 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.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ switch (op) {
9 ✗ case TraceOpKind::Input: return "Input";
10 ✗ case TraceOpKind::Add: return "Add";
11 ✗ case TraceOpKind::Sub: return "Sub";
12 ✗ case TraceOpKind::Mul: return "Mul";
13 ✗ case TraceOpKind::Div: return "Div";
14 ✗ case TraceOpKind::AddScalar: return "AddScalar";
15 ✗ case TraceOpKind::SubScalar: return "SubScalar";
16 ✗ case TraceOpKind::MulScalar: return "MulScalar";
17 ✗ case TraceOpKind::DivScalar: return "DivScalar";
18 ✗ case TraceOpKind::ScalarSub: return "ScalarSub";
19 ✗ case TraceOpKind::ScalarDiv: return "ScalarDiv";
20 ✗ case TraceOpKind::FMA: return "FMA";
21 ✗ case TraceOpKind::SiLU: return "SiLU";
22 ✗ case TraceOpKind::GELU: return "GELU";
23 ✗ case TraceOpKind::ReLU: return "ReLU";
24 ✗ case TraceOpKind::RMSNorm: return "RMSNorm";
25 ✗ case TraceOpKind::LayerNorm: return "LayerNorm";
26 ✗ case TraceOpKind::Modulate: return "Modulate";
27 ✗ case TraceOpKind::Tanh: return "Tanh";
28 ✗ case TraceOpKind::Sigmoid: return "Sigmoid";
29 ✗ case TraceOpKind::Copy: return "Copy";
30 ✗ case TraceOpKind::Where: return "Where";
31 ✗ case TraceOpKind::Clamp: return "Clamp";
32 ✗ case TraceOpKind::Cast: return "Cast";
33 ✗ case TraceOpKind::Const: return "Const";
34 ✗ case TraceOpKind::Neg: return "Neg";
35 ✗ case TraceOpKind::Exp: return "Exp";
36 ✗ case TraceOpKind::Log: return "Log";
37 ✗ case TraceOpKind::Sqrt: return "Sqrt";
38 ✗ case TraceOpKind::Rsqrt: return "Rsqrt";
39 ✗ case TraceOpKind::Abs: return "Abs";
40 }
41 ✗ return "Unknown";
42 ✗ }
43
44 1191 BroadcastKind broadcast_of(int node_rows, int node_cols, int rows, int cols) {
45
5/6
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 1107 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 78 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
1191 if (node_rows == 1 && node_cols == 1 && rows * cols != 1) {
46 6 return BroadcastKind::Scalar;
47 }
48
4/6
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 1107 times.
✓ Branch 2 taken 78 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 78 times.
1185 if (node_rows == 1 && node_cols == cols && rows != 1) {
49 78 return BroadcastKind::Row;
50 }
51
5/6
✓ Branch 0 taken 1107 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 116 times.
✓ Branch 3 taken 991 times.
✓ Branch 4 taken 62 times.
✓ Branch 5 taken 54 times.
1107 if (node_rows == rows && node_cols == 1 && cols != 1) {
52 54 return BroadcastKind::Col;
53 }
54 // Anything else is addressed linearly. A node whose element count does not
55 // match the dominant shape is rejected by the compiler before it gets
56 // here (see trace_compiler_cuda.cpp's plan builder).
57 1053 return BroadcastKind::Full;
58 1191 }
59
60 44 bool TraceDAG::slot_matches(int slot, const Tensor& t) const {
61
2/4
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 44 times.
44 if (slot < 0 || slot >= static_cast<int>(nodes_.size())) return false;
62 44 const TraceNode& n = nodes_[static_cast<std::size_t>(slot)];
63
3/6
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 44 times.
✗ Branch 5 not taken.
88 return n.buffer == t.data && n.rows == t.rows && n.cols == t.cols &&
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 44 times.
44 n.dtype == t.dtype && n.device == t.device;
65 44 }
66
67 111 int TraceDAG::register_input(const Tensor& t) {
68 111 auto it = input_ptr_to_slot_.find(t.data);
69
2/4
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 111 times.
111 if (it != input_ptr_to_slot_.end() && slot_matches(it->second, t)) {
70 ✗ return it->second;
71 }
72
73 111 int id = static_cast<int>(nodes_.size());
74 111 TraceNode node;
75 111 node.id = id;
76 111 node.op = TraceOpKind::Input;
77 111 node.buffer = t.data;
78 111 node.device = t.device;
79 111 node.dtype = t.dtype;
80 111 node.rows = t.rows;
81 111 node.cols = t.cols;
82 111 node.is_inplace = false;
83 111 node.is_live_at_end = false;
84
85
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 nodes_.push_back(std::move(node));
86
1/2
✓ Branch 0 taken 111 times.
✗ Branch 1 not taken.
111 input_ptr_to_slot_[t.data] = id;
87 111 return id;
88 111 }
89
90 3878 int TraceDAG::add_node(TraceOpKind op, const std::vector<int>& inputs, float scalar,
91 void* buffer, const Device& dev, Dtype dt, int rows, int cols,
92 float scalar2) {
93 3878 int id = static_cast<int>(nodes_.size());
94 3878 TraceNode node;
95 3878 node.id = id;
96 3878 node.op = op;
97
1/2
✓ Branch 0 taken 3878 times.
✗ Branch 1 not taken.
3878 node.inputs = inputs;
98 3878 node.scalar = scalar;
99 3878 node.scalar2 = scalar2;
100 3878 node.buffer = buffer;
101 3878 node.device = dev;
102 3878 node.dtype = dt;
103 3878 node.rows = rows;
104 3878 node.cols = cols;
105 3878 node.is_inplace = false;
106 3878 node.is_live_at_end = false;
107
108
1/2
✓ Branch 0 taken 3878 times.
✗ Branch 1 not taken.
3878 nodes_.push_back(std::move(node));
109 3878 return id;
110 3878 }
111
112 3 int TraceDAG::add_inplace_node(TraceOpKind op, int target_slot, int arg_slot,
113 void* buffer, float scalar, float scalar2) {
114 3 int id = static_cast<int>(nodes_.size());
115 3 const auto& target_node = nodes_.at(target_slot);
116
117 3 TraceNode node;
118 3 node.id = id;
119 3 node.op = op;
120
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 if (arg_slot >= 0) {
121
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 node.inputs = {target_slot, arg_slot};
122 2 } else {
123
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 node.inputs = {target_slot};
124 }
125 3 node.scalar = scalar;
126 3 node.scalar2 = scalar2;
127 3 node.buffer = buffer;
128 3 node.device = target_node.device;
129 3 node.dtype = target_node.dtype;
130 3 node.rows = target_node.rows;
131 3 node.cols = target_node.cols;
132 3 node.is_inplace = true;
133 3 node.inplace_target_slot = target_slot;
134 3 node.is_live_at_end = true;
135
136
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 nodes_.push_back(std::move(node));
137 3 return id;
138 3 }
139
140 35 void TraceDAG::analyze_liveness() {
141
2/2
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 35 times.
289 for (auto& n : nodes_) {
142 254 n.outputs.clear();
143 }
144
2/2
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 35 times.
289 for (const auto& n : nodes_) {
145
2/2
✓ Branch 0 taken 242 times.
✓ Branch 1 taken 254 times.
496 for (int in_id : n.inputs) {
146
2/4
✓ Branch 0 taken 242 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 242 times.
242 if (in_id >= 0 && in_id < static_cast<int>(nodes_.size())) {
147 242 nodes_[in_id].outputs.push_back(n.id);
148 242 }
149 }
150 }
151
2/2
✓ Branch 0 taken 254 times.
✓ Branch 1 taken 35 times.
289 for (auto& n : nodes_) {
152
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 108 times.
254 if (n.op == TraceOpKind::Input) {
153 108 n.is_live_at_end = false;
154
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 143 times.
254 } else if (n.is_inplace) {
155 3 n.is_live_at_end = true;
156
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 109 times.
146 } else if (n.outputs.empty()) {
157 34 n.is_live_at_end = true;
158 34 } else {
159 109 n.is_live_at_end = false;
160 }
161 }
162 35 }
163
164 144 void TraceDAG::dominant_shape(int& rows, int& cols) const {
165 144 rows = 0;
166 144 cols = 0;
167 144 int64_t best = 0;
168
2/2
✓ Branch 0 taken 3285 times.
✓ Branch 1 taken 144 times.
3429 for (const auto& n : nodes_) {
169 3285 const int64_t numel = static_cast<int64_t>(n.rows) * static_cast<int64_t>(n.cols);
170
2/2
✓ Branch 0 taken 3139 times.
✓ Branch 1 taken 146 times.
3285 if (numel > best) {
171 146 best = numel;
172 146 rows = n.rows;
173 146 cols = n.cols;
174 146 }
175 }
176 144 }
177
178 53 uint64_t TraceDAG::compute_hash() const {
179 // 64-bit FNV-1a hash
180 53 uint64_t hash = 14695981039346656037ULL;
181 12188 auto hash_val = [&hash](uint64_t val) {
182 12135 hash ^= val;
183 12135 hash *= 1099511628211ULL;
184 12135 };
185
186 53 hash_val(nodes_.size());
187
2/2
✓ Branch 0 taken 989 times.
✓ Branch 1 taken 53 times.
1042 for (const auto& n : nodes_) {
188 989 hash_val(static_cast<uint64_t>(n.op));
189 989 hash_val(static_cast<uint64_t>(n.device.type));
190 989 hash_val(static_cast<uint64_t>(n.device.index));
191 989 hash_val(static_cast<uint64_t>(n.dtype));
192 989 hash_val(static_cast<uint64_t>(n.rows));
193 989 hash_val(static_cast<uint64_t>(n.cols));
194 989 hash_val(n.is_inplace ? 1ULL : 0ULL);
195 989 hash_val(n.is_live_at_end ? 1ULL : 0ULL);
196
197 989 uint32_t scalar_bits = 0;
198 989 std::memcpy(&scalar_bits, &n.scalar, sizeof(float));
199 989 hash_val(scalar_bits);
200
201 989 uint32_t scalar2_bits = 0;
202 989 std::memcpy(&scalar2_bits, &n.scalar2, sizeof(float));
203 989 hash_val(scalar2_bits);
204
205 989 hash_val(n.inputs.size());
206
2/2
✓ Branch 0 taken 1203 times.
✓ Branch 1 taken 989 times.
2192 for (int in_id : n.inputs) {
207 1203 hash_val(static_cast<uint64_t>(in_id));
208 }
209 }
210 53 return hash;
211 }
212
213 46 std::string TraceDAG::key_material() const {
214 46 std::string out;
215
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 out.reserve(16 + nodes_.size() * 64);
216 14858 auto put = [&out](uint64_t v, int bytes) {
217
2/2
✓ Branch 0 taken 51323 times.
✓ Branch 1 taken 14812 times.
66135 for (int i = 0; i < bytes; ++i) out.push_back(static_cast<char>((v >> (8 * i)) & 0xFFu));
218 14812 };
219 1848 auto put_f32 = [&put](float f) {
220 1802 uint32_t bits = 0;
221 1802 std::memcpy(&bits, &f, sizeof(bits));
222 1802 put(bits, 4);
223 1802 };
224
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 put(nodes_.size(), 8);
225
2/2
✓ Branch 0 taken 901 times.
✓ Branch 1 taken 46 times.
947 for (const auto& n : nodes_) {
226
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put(static_cast<uint32_t>(n.id), 4);
227
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put(static_cast<uint64_t>(n.op), 1);
228
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put(static_cast<uint64_t>(n.device.type), 4);
229
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put(static_cast<uint32_t>(n.device.index), 4);
230
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put(static_cast<uint64_t>(n.dtype), 4);
231
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put(static_cast<uint32_t>(n.rows), 4);
232
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put(static_cast<uint32_t>(n.cols), 4);
233
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put(n.is_inplace ? 1u : 0u, 1);
234
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put(static_cast<uint32_t>(n.inplace_target_slot), 4);
235
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put(n.is_live_at_end ? 1u : 0u, 1);
236
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put_f32(n.scalar);
237
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put_f32(n.scalar2);
238
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put(n.inputs.size(), 4);
239
3/4
✓ Branch 0 taken 901 times.
✓ Branch 1 taken 1103 times.
✓ Branch 2 taken 1103 times.
✗ Branch 3 not taken.
2004 for (int in_id : n.inputs) put(static_cast<uint32_t>(in_id), 4);
240
1/2
✓ Branch 0 taken 901 times.
✗ Branch 1 not taken.
901 put(n.outputs.size(), 4);
241
3/4
✓ Branch 0 taken 901 times.
✓ Branch 1 taken 1049 times.
✓ Branch 2 taken 1049 times.
✗ Branch 3 not taken.
1950 for (int out_id : n.outputs) put(static_cast<uint32_t>(out_id), 4);
242 }
243 46 return out;
244 46 }
245
246 38 void TraceDAG::reset() {
247 38 nodes_.clear();
248 38 input_ptr_to_slot_.clear();
249 38 }
250
251 556 TraceContext& TraceContext::current() {
252
2/2
✓ Branch 0 taken 549 times.
✓ Branch 1 taken 7 times.
556 static thread_local TraceContext instance;
253 556 return instance;
254 }
255
256 namespace {
257
258 // The hook Tensor calls from its move ctor / move assignment / destructor.
259 // Installed by begin(), which is the earliest point at which a symbolic
260 // tensor can exist, so there is no static-initialisation order to get wrong.
261 113 void slot_retarget(int slot, const Tensor* from, Tensor* to) {
262 113 TraceContext::current().retarget(slot, from, to);
263 113 }
264
265 1 bool clamp_inplace_hook(Tensor& y, float lo, float hi) {
266 1 auto& ctx = TraceContext::current();
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (!ctx.is_active()) return false;
268 1 const int slot_y = ctx.get_or_register_slot(y);
269 1 ctx.record_inplace_op(TraceOpKind::Clamp, slot_y, -1, lo, y, hi);
270 1 return true;
271 1 }
272
273 47 bool cast_out_hook(const Tensor& src, Tensor& dst, Dtype out_dtype) {
274 47 auto& ctx = TraceContext::current();
275
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1 time.
47 if (!ctx.is_active()) return false;
276 1 const int slot_src = ctx.get_or_register_slot(src);
277 1 ctx.record_cast_out(slot_src, src, dst, out_dtype);
278 1 return true;
279 47 }
280
281 } // namespace
282
283 113 void TraceContext::retarget(int slot, const Tensor* from, Tensor* to) noexcept {
284
1/2
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
113 auto it = slot_to_tensor_.find(slot);
285 // A stale entry — the slot was already materialised or re-tracked — must
286 // not be clobbered by a tensor that no longer stands for it.
287
4/8
✓ Branch 0 taken 113 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 113 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 113 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 113 times.
113 if (it == slot_to_tensor_.end() || it->second != from) return;
288
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 105 times.
113 if (to) {
289
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 it->second = to;
290 8 } else {
291
1/2
✓ Branch 0 taken 105 times.
✗ Branch 1 not taken.
105 slot_to_tensor_.erase(it);
292 }
293 113 }
294
295 72 void TraceContext::release_tracked_() noexcept {
296
5/8
✓ Branch 0 taken 78 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 72 times.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
78 for (auto& kv : slot_to_tensor_) {
297 6 Tensor* t = kv.second;
298
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!t) continue;
299 // It never owned anything, and its value was never written anywhere,
300 // so the honest result is an empty tensor rather than a shape with no
301 // storage behind it.
302 6 t->jit_slot = -1;
303 6 t->rows = 0;
304 6 t->cols = 0;
305 }
306 72 slot_to_tensor_.clear();
307 72 }
308
309 36 void TraceContext::begin() {
310
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (active_) {
311
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor::jit: Nested begin_trace() calls are not permitted");
312 }
313 36 detail::jit_slot_retarget = &slot_retarget;
314 36 detail::jit_clamp_inplace_hook = &clamp_inplace_hook;
315 36 detail::jit_cast_out_hook = &cast_out_hook;
316 36 dag_.reset();
317 36 active_ptr_to_slot_.clear();
318 36 release_tracked_();
319 36 active_ = true;
320 36 }
321
322 2 void TraceContext::discard() {
323 2 dag_.reset();
324 2 active_ptr_to_slot_.clear();
325 2 release_tracked_();
326 2 active_ = false;
327 2 }
328
329 34 void TraceContext::materialize_() {
330
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 34 times.
284 for (TraceNode& n : dag_.nodes()) {
331
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 214 times.
250 if (!n.is_live_at_end) continue;
332
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 32 times.
36 if (n.buffer != nullptr) continue; // caller already owns the storage
333
334 32 auto it = slot_to_tensor_.find(n.id);
335
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
32 if (it == slot_to_tensor_.end() || it->second == nullptr) {
336 // The caller dropped the Tensor, so nothing can read this value.
337 // Leave it an interior value of the fused kernel rather than
338 // allocating a buffer no one will look at.
339 ✗ n.is_live_at_end = false;
340 ✗ continue;
341 }
342
343 32 Tensor* held = it->second;
344 32 Tensor buf = Tensor::empty_on(n.device, n.rows, n.cols, n.dtype);
345
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (buf.data == nullptr) {
346 ✗ n.is_live_at_end = false;
347 ✗ continue;
348 }
349 // Drop the tracking first: the move assignment below must see an
350 // ordinary tensor on both sides, or it would retarget the slot it is
351 // in the middle of retiring.
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 slot_to_tensor_.erase(it);
353 32 held->jit_slot = -1;
354 32 *held = std::move(buf);
355 32 n.buffer = held->data;
356 32 }
357 34 }
358
359 34 TraceDAG TraceContext::end() {
360
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (!active_) {
361
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor::jit: end_trace() called without matching begin_trace()");
362 }
363 34 dag_.analyze_liveness();
364 34 materialize_();
365 34 active_ = false;
366 34 active_ptr_to_slot_.clear();
367 // Whatever is still tracked stands for a value the fused kernel only ever
368 // holds in registers; there is nothing to hand back.
369 34 release_tracked_();
370
371 34 bool any_output = false;
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 247 times.
247 for (const TraceNode& n : dag_.nodes()) {
373
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 213 times.
247 if (n.is_live_at_end) { any_output = true; break; }
374 }
375
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (!any_output) {
376 ✗ dag_.reset();
377
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error(
378 "brotensor::jit: the trace has no observable result — every value it "
379 "produced was dropped before end_trace(), so there is nothing for the "
380 "fused kernel to write. Keep the result Tensor alive across "
381 "end_trace(), store() it into a buffer you own, or write in place "
382 "into one of the inputs");
383 }
384 34 return std::move(dag_);
385 ✗ }
386
387 262 int TraceContext::get_or_register_slot(const Tensor& t) {
388 // A symbolic intermediate already *is* a node: it names its own slot, and
389 // it has no buffer to key on.
390
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 153 times.
262 if (t.jit_slot >= 0) return t.jit_slot;
391
392 // Everything else is caller-owned storage, keyed on its address. The
393 // address is stable for the trace's lifetime because the caller owns it —
394 // but a slot recorded earlier can still be stale if that tensor was freed
395 // and its address reused, so confirm the node still describes the tensor
396 // in hand before reusing its slot.
397 153 auto it = active_ptr_to_slot_.find(t.data);
398
4/4
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 44 times.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 109 times.
153 if (it != active_ptr_to_slot_.end() && dag_.slot_matches(it->second, t)) {
399 44 return it->second;
400 }
401 109 int slot = dag_.register_input(t);
402 109 active_ptr_to_slot_[t.data] = slot;
403 109 return slot;
404 262 }
405
406 143 Tensor TraceContext::record_op(TraceOpKind op, const std::vector<int>& in_slots,
407 float scalar, const Device& dev, Dtype dt,
408 int rows, int cols, float scalar2) {
409 286 const int slot = dag_.add_node(op, in_slots, scalar, /*buffer=*/nullptr,
410 143 dev, dt, rows, cols, scalar2);
411 143 Tensor out;
412 143 out.device = dev;
413 143 out.dtype = dt;
414 143 out.rows = rows;
415 143 out.cols = cols;
416 143 out.jit_slot = slot;
417 // With copy elision `out` is already the caller's object; without it, the
418 // move ctor retargets this entry onto wherever it lands.
419
1/2
✓ Branch 0 taken 143 times.
✗ Branch 1 not taken.
143 slot_to_tensor_[slot] = &out;
420 143 return out;
421 143 }
422
423 1 void TraceContext::record_store(int src_slot, const Tensor& dst) {
424
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (dst.jit_slot >= 0) {
425
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error(
426 "brotensor::jit: store() needs a destination the caller owns; its "
427 "target is a traced intermediate, which has no buffer to write to");
428 }
429
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 const int slot = dag_.add_node(TraceOpKind::Copy, {src_slot}, 0.0f, dst.data,
430 1 dst.device, dst.dtype, dst.rows, dst.cols);
431 1 active_ptr_to_slot_[dst.data] = slot;
432 1 }
433
434 1 void TraceContext::record_cast_out(int src_slot, const Tensor& src, Tensor& dst, Dtype out_dtype) {
435
2/8
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1 if (dst.jit_slot < 0 && dst.data != nullptr && dst.device == src.device &&
436
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ dst.rows == src.rows && dst.cols == src.cols && dst.dtype == out_dtype) {
437
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ const int slot = dag_.add_node(TraceOpKind::Cast, {src_slot}, 0.0f, dst.data,
438 ✗ dst.device, out_dtype, dst.rows, dst.cols);
439 ✗ active_ptr_to_slot_[dst.data] = slot;
440 ✗ return;
441 }
442
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 dst = record_op(TraceOpKind::Cast, {src_slot}, 0.0f, src.device, out_dtype,
443 1 src.rows, src.cols);
444 1 }
445
446 3 int TraceContext::record_inplace_op(TraceOpKind op, int target_slot, int arg_slot,
447 float scalar, Tensor& target_tensor,
448 float scalar2) {
449 6 int slot = dag_.add_inplace_node(op, target_slot, arg_slot, target_tensor.data,
450 3 scalar, scalar2);
451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (target_tensor.jit_slot >= 0) {
452 // In-place on a traced intermediate: it has no buffer, and from here
453 // on it stands for the node this op just produced.
454 ✗ slot_to_tensor_.erase(target_tensor.jit_slot);
455 ✗ target_tensor.jit_slot = slot;
456 ✗ slot_to_tensor_[slot] = &target_tensor;
457 ✗ } else {
458 3 active_ptr_to_slot_[target_tensor.data] = slot;
459 }
460 3 return slot;
461 }
462
463 } // namespace brotensor::jit
464