trueno 0.17.4

High-performance SIMD compute library with GPU support for matrix operations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! Comprehensive to_dot() and to_ascii_tree() coverage tests.

use super::super::*;

// ================================================================
// Comprehensive to_dot() coverage - all node types and edge types
// ================================================================

#[test]
fn test_to_dot_all_node_types() {
    let mut graph = ExecutionGraph::new();

    // Layer node
    graph.add_node(ExecutionNode::Layer { index: 0 });

    // Brick node
    graph.add_node(ExecutionNode::Brick { id: BrickId::RmsNorm, timing_ns: 5000, elements: 2048 });

    // Kernel node
    graph.add_node(ExecutionNode::Kernel {
        name: "batched_q4k_gemv".into(),
        ptx_hash: 0xDEADBEEF,
        grid: (32, 1, 1),
        block: (256, 1, 1),
        shared_mem: 4096,
        timing_ns: Some(1500),
        arithmetic_intensity: None,
        achieved_tflops: None,
    });

    // Function node with file/line
    graph.add_node(ExecutionNode::Function {
        name: "forward_pass".into(),
        file: Some("model.rs".into()),
        line: Some(42),
    });

    // Function node without file/line
    graph.add_node(ExecutionNode::Function { name: "dispatch".into(), file: None, line: None });

    // Transfer H2D
    graph.add_node(ExecutionNode::Transfer {
        src: "host".into(),
        dst: "gpu0".into(),
        bytes: 1_000_000,
        direction: TransferDirection::H2D,
        timing_ns: Some(500),
    });

    // Transfer D2H
    graph.add_node(ExecutionNode::Transfer {
        src: "gpu0".into(),
        dst: "host".into(),
        bytes: 2_000_000,
        direction: TransferDirection::D2H,
        timing_ns: None,
    });

    // Transfer D2D
    graph.add_node(ExecutionNode::Transfer {
        src: "gpu0".into(),
        dst: "gpu1".into(),
        bytes: 500_000,
        direction: TransferDirection::D2D,
        timing_ns: Some(200),
    });

    // AsyncTask
    graph.add_node(ExecutionNode::AsyncTask {
        name: "prefetch_weights".into(),
        poll_count: 5,
        yield_count: 3,
        total_poll_ns: 10_000,
    });

    // AsyncTask with zero polls
    graph.add_node(ExecutionNode::AsyncTask {
        name: "idle_task".into(),
        poll_count: 0,
        yield_count: 0,
        total_poll_ns: 0,
    });

    let dot = graph.to_dot();

    // Structure
    assert!(dot.starts_with("digraph ExecutionGraph {\n"));
    assert!(dot.ends_with("}\n"));
    assert!(dot.contains("rankdir=TB"));

    // Layer
    assert!(dot.contains("Layer 0"));
    assert!(dot.contains("fillcolor=lightblue"));

    // Brick
    assert!(dot.contains("RmsNorm"));
    assert!(dot.contains("fillcolor=lightgreen"));

    // Kernel
    assert!(dot.contains("batched_q4k_gemv"));
    assert!(dot.contains("<<<32,256,1>>>"));
    assert!(dot.contains("fillcolor=lightyellow"));

    // Function with location
    assert!(dot.contains("forward_pass"));
    assert!(dot.contains("model.rs:42"));
    assert!(dot.contains("fillcolor=lightgray"));

    // Function without location
    assert!(dot.contains("dispatch"));

    // Transfers
    assert!(dot.contains("H2D"));
    assert!(dot.contains("D2H"));
    assert!(dot.contains("D2D"));
    assert!(dot.contains("fillcolor=lightsalmon"));

    // AsyncTask
    assert!(dot.contains("prefetch_weights"));
    assert!(dot.contains("polls:5"));
    assert!(dot.contains("yields:3"));
    assert!(dot.contains("fillcolor=lightcyan"));
}

#[test]
fn test_to_dot_all_edge_types() {
    let mut graph = ExecutionGraph::new();

    let n0 = graph.add_node(ExecutionNode::Layer { index: 0 });
    let n1 = graph.add_node(ExecutionNode::Layer { index: 1 });
    let n2 = graph.add_node(ExecutionNode::Layer { index: 2 });
    let n3 = graph.add_node(ExecutionNode::Layer { index: 3 });
    let n4 = graph.add_node(ExecutionNode::Layer { index: 4 });
    let n5 = graph.add_node(ExecutionNode::Layer { index: 5 });

    graph.add_edge(n0, n1, EdgeType::Calls);
    graph.add_edge(n0, n2, EdgeType::Contains);
    graph.add_edge(n2, n3, EdgeType::Launches);
    graph.add_edge(n3, n4, EdgeType::Sequence);
    graph.add_edge(n4, n5, EdgeType::DependsOn);
    graph.add_edge(n1, n5, EdgeType::Transfer { bytes: 1024, direction: TransferDirection::H2D });

    let dot = graph.to_dot();

    assert!(dot.contains("style=solid"));
    assert!(dot.contains("style=dashed"));
    assert!(dot.contains("style=bold,color=red"));
    assert!(dot.contains("style=dotted"));
    assert!(dot.contains("style=solid,color=blue"));
    assert!(dot.contains("style=bold,color=orange"));
}

#[test]
fn test_to_dot_empty_graph() {
    let graph = ExecutionGraph::new();
    let dot = graph.to_dot();
    assert!(dot.contains("digraph ExecutionGraph"));
    assert!(dot.contains("}\n"));
    // No nodes or edges
    assert!(!dot.contains("n0"));
}

// ================================================================
// Comprehensive to_ascii_tree() coverage
// ================================================================

#[test]
fn test_to_ascii_tree_empty_graph() {
    let graph = ExecutionGraph::new();
    let tree = graph.to_ascii_tree();
    assert_eq!(tree, "(empty graph)");
}

#[test]
fn test_to_ascii_tree_single_node() {
    let mut graph = ExecutionGraph::new();
    graph.add_node(ExecutionNode::Layer { index: 7 });
    let tree = graph.to_ascii_tree();
    assert!(tree.contains("Layer 7"));
}

#[test]
fn test_to_ascii_tree_multiple_roots() {
    let mut graph = ExecutionGraph::new();
    graph.add_node(ExecutionNode::Layer { index: 0 });
    graph.add_node(ExecutionNode::Layer { index: 1 });
    graph.add_node(ExecutionNode::Layer { index: 2 });

    let tree = graph.to_ascii_tree();
    assert!(tree.contains("Execution Graph"));
    assert!(tree.contains("Layer 0"));
    assert!(tree.contains("Layer 1"));
    assert!(tree.contains("Layer 2"));
    // Should have tree connectors
    assert!(tree.contains("\u{251c}\u{2500}\u{2500}") || tree.contains("\u{2514}\u{2500}\u{2500}"));
}

#[test]
fn test_to_ascii_tree_all_node_types() {
    let mut graph = ExecutionGraph::new();

    let root = graph.add_node(ExecutionNode::Layer { index: 0 });

    let brick = graph.add_node(ExecutionNode::Brick {
        id: BrickId::QkvProjection,
        timing_ns: 3000,
        elements: 1024,
    });
    graph.add_edge(root, brick, EdgeType::Contains);

    let kernel = graph.add_node(ExecutionNode::Kernel {
        name: "gemv_kernel".into(),
        ptx_hash: 0x1234,
        grid: (4, 1, 1),
        block: (128, 1, 1),
        shared_mem: 2048,
        timing_ns: None,
        arithmetic_intensity: None,
        achieved_tflops: None,
    });
    graph.add_edge(brick, kernel, EdgeType::Launches);

    let func = graph.add_node(ExecutionNode::Function {
        name: "compute".into(),
        file: Some("ops.rs".into()),
        line: Some(100),
    });
    graph.add_edge(root, func, EdgeType::Contains);

    let func_no_loc =
        graph.add_node(ExecutionNode::Function { name: "init".into(), file: None, line: None });
    graph.add_edge(root, func_no_loc, EdgeType::Contains);

    let transfer = graph.add_node(ExecutionNode::Transfer {
        src: "cpu".into(),
        dst: "gpu".into(),
        bytes: 8192,
        direction: TransferDirection::H2D,
        timing_ns: Some(250),
    });
    graph.add_edge(root, transfer, EdgeType::Contains);

    let transfer_no_timing = graph.add_node(ExecutionNode::Transfer {
        src: "gpu".into(),
        dst: "cpu".into(),
        bytes: 4096,
        direction: TransferDirection::D2H,
        timing_ns: None,
    });
    graph.add_edge(root, transfer_no_timing, EdgeType::Contains);

    let async_task = graph.add_node(ExecutionNode::AsyncTask {
        name: "prefetch".into(),
        poll_count: 3,
        yield_count: 1,
        total_poll_ns: 5000,
    });
    graph.add_edge(root, async_task, EdgeType::Contains);

    let async_zero = graph.add_node(ExecutionNode::AsyncTask {
        name: "idle".into(),
        poll_count: 0,
        yield_count: 0,
        total_poll_ns: 0,
    });
    graph.add_edge(root, async_zero, EdgeType::Contains);

    let tree = graph.to_ascii_tree();

    assert!(tree.contains("Layer 0"));
    assert!(tree.contains("QkvProjection"));
    assert!(tree.contains("gemv_kernel"));
    assert!(tree.contains("compute (ops.rs:100)"));
    assert!(tree.contains("init"));
    assert!(tree.contains("H2D: cpu"));
    assert!(tree.contains("D2H: gpu"));
    assert!(tree.contains("prefetch"));
    assert!(tree.contains("idle"));
    // Brick should show timing info
    assert!(tree.contains("\u{00b5}s"));
    // Kernel should show launch config
    assert!(tree.contains("<<<"));
}

#[test]
fn test_to_ascii_tree_deep_hierarchy() {
    let mut graph = ExecutionGraph::new();
    let _l0 = graph.push_scope(ExecutionNode::Layer { index: 0 });
    let _b1 = graph.add_node_in_scope(ExecutionNode::Brick {
        id: BrickId::RmsNorm,
        timing_ns: 100,
        elements: 64,
    });
    let _b2 = graph.add_node_in_scope(ExecutionNode::Brick {
        id: BrickId::AttentionScore,
        timing_ns: 200,
        elements: 128,
    });
    graph.pop_scope();

    let tree = graph.to_ascii_tree();
    assert!(tree.contains("Layer 0"));
    assert!(tree.contains("RmsNorm"));
    assert!(tree.contains("AttentionScore"));
    // Both children should have tree connectors
    assert!(tree.contains("\u{251c}\u{2500}\u{2500}") || tree.contains("\u{2514}\u{2500}\u{2500}"));
}

#[test]
fn test_to_ascii_tree_nested_launches() {
    let mut graph = ExecutionGraph::new();

    let layer = graph.add_node(ExecutionNode::Layer { index: 0 });
    let brick = graph.add_node(ExecutionNode::Brick {
        id: BrickId::GateProjection,
        timing_ns: 5000,
        elements: 2048,
    });
    graph.add_edge(layer, brick, EdgeType::Contains);

    let k1 = graph.add_node(ExecutionNode::Kernel {
        name: "kernel_a".into(),
        ptx_hash: 0,
        grid: (1, 1, 1),
        block: (64, 1, 1),
        shared_mem: 0,
        timing_ns: None,
        arithmetic_intensity: None,
        achieved_tflops: None,
    });
    graph.add_edge(brick, k1, EdgeType::Launches);

    let k2 = graph.add_node(ExecutionNode::Kernel {
        name: "kernel_b".into(),
        ptx_hash: 0,
        grid: (2, 1, 1),
        block: (32, 1, 1),
        shared_mem: 512,
        timing_ns: None,
        arithmetic_intensity: None,
        achieved_tflops: None,
    });
    graph.add_edge(brick, k2, EdgeType::Launches);

    let tree = graph.to_ascii_tree();
    assert!(tree.contains("Layer 0"));
    assert!(tree.contains("GateProjection"));
    assert!(tree.contains("kernel_a"));
    assert!(tree.contains("kernel_b"));
}

// ================================================================
// node_to_dot_label: Function partial location coverage
// ================================================================

/// Function with file but no line — (Some, None) branch in node_to_dot_label.
#[test]
fn test_to_dot_function_file_no_line() {
    let mut graph = ExecutionGraph::new();
    graph.add_node(ExecutionNode::Function {
        name: "partial_func".into(),
        file: Some("module.rs".into()),
        line: None,
    });

    let dot = graph.to_dot();
    // (Some, None) should produce empty loc string — no file:line in label
    assert!(dot.contains("partial_func"));
    assert!(
        !dot.contains("module.rs"),
        "Function with file but no line should not show file in DOT label"
    );
}

/// Function with no file but has line — (None, Some) branch in node_to_dot_label.
#[test]
fn test_to_dot_function_no_file_has_line() {
    let mut graph = ExecutionGraph::new();
    graph.add_node(ExecutionNode::Function {
        name: "orphan_func".into(),
        file: None,
        line: Some(99),
    });

    let dot = graph.to_dot();
    assert!(dot.contains("orphan_func"));
    // (None, Some(99)) should produce empty loc string
    assert!(!dot.contains(":99"), "Function with no file should not show :99 in DOT label");
}

// ================================================================
// to_ascii_tree: Function partial location coverage in ASCII tree
// ================================================================

/// ASCII tree: Function with file but no line.
#[test]
fn test_to_ascii_tree_function_file_no_line() {
    let mut graph = ExecutionGraph::new();
    let root = graph.add_node(ExecutionNode::Layer { index: 0 });
    let func = graph.add_node(ExecutionNode::Function {
        name: "setup".into(),
        file: Some("config.rs".into()),
        line: None,
    });
    graph.add_edge(root, func, EdgeType::Contains);

    let tree = graph.to_ascii_tree();
    assert!(tree.contains("setup"));
    // (Some, None) branch: empty location string
    assert!(
        !tree.contains("config.rs"),
        "Function with file but no line should not show file: {}",
        tree
    );
}

/// ASCII tree: Function with no file but has line.
#[test]
fn test_to_ascii_tree_function_no_file_has_line() {
    let mut graph = ExecutionGraph::new();
    let root = graph.add_node(ExecutionNode::Layer { index: 0 });
    let func = graph.add_node(ExecutionNode::Function {
        name: "teardown".into(),
        file: None,
        line: Some(55),
    });
    graph.add_edge(root, func, EdgeType::Contains);

    let tree = graph.to_ascii_tree();
    assert!(tree.contains("teardown"));
    assert!(!tree.contains(":55"), "Function with no file should not show line number: {}", tree);
}

// ================================================================
// node_to_dot_label: AsyncTask zero polls (0% efficiency) in DOT
// ================================================================

/// Verify DOT output for AsyncTask with zero polls shows 0% efficiency.
#[test]
fn test_to_dot_async_task_zero_polls_efficiency() {
    let mut graph = ExecutionGraph::new();
    graph.add_node(ExecutionNode::AsyncTask {
        name: "zero_poll_task".into(),
        poll_count: 0,
        yield_count: 0,
        total_poll_ns: 0,
    });

    let dot = graph.to_dot();
    assert!(dot.contains("zero_poll_task"));
    assert!(dot.contains("polls:0"));
    assert!(dot.contains("0%"), "Zero polls should show 0% efficiency in DOT: {}", dot);
}

/// Verify DOT output for AsyncTask with nonzero polls shows correct efficiency.
#[test]
fn test_to_dot_async_task_nonzero_polls_efficiency() {
    let mut graph = ExecutionGraph::new();
    graph.add_node(ExecutionNode::AsyncTask {
        name: "busy_task".into(),
        poll_count: 4,
        yield_count: 2,
        total_poll_ns: 8000,
    });

    let dot = graph.to_dot();
    assert!(dot.contains("busy_task"));
    assert!(dot.contains("polls:4"));
    assert!(dot.contains("yields:2"));
    // 100.0 / 4 = 25%
    assert!(dot.contains("25%"), "4 polls should show 25% efficiency: {}", dot);
}

// ================================================================
// to_tree_node: empty graph (Empty Graph branch)
// ================================================================

#[cfg(feature = "presentar-tui")]
#[test]
fn test_to_tree_node_empty_graph() {
    let graph = ExecutionGraph::new();
    let tree = graph.to_tree_node();
    assert_eq!(tree.label, "Empty Graph");
    assert!(tree.children.is_empty());
}