apr-cli 0.29.1

CLI tool for APR model inspection, debugging, and 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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! Data flow visualization (GH-122)
//!
//! Toyota Way: Genchi Genbutsu - Go and see how data flows.
//! Visualize tensor transformations through model layers.
//!
//! Usage:
//!   apr flow model.apr --layer "decoder.layers.0.cross_attn"
//!   apr flow model.apr --component encoder
//!   apr flow model.apr --all

use crate::error::CliError;
use aprender::format::rosetta::RosettaStone;
use aprender::serialization::apr::AprReader;
use colored::Colorize;
use std::path::Path;

/// Flow component type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum FlowComponent {
    /// Full model flow
    Full,
    /// Encoder only
    Encoder,
    /// Decoder only
    Decoder,
    /// Self-attention
    SelfAttention,
    /// Cross-attention (encoder-decoder)
    CrossAttention,
    /// Feed-forward network
    Ffn,
}

impl std::str::FromStr for FlowComponent {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "full" | "all" => Ok(Self::Full),
            "encoder" | "enc" => Ok(Self::Encoder),
            "decoder" | "dec" => Ok(Self::Decoder),
            "self_attn" | "self-attn" | "selfattn" => Ok(Self::SelfAttention),
            "cross_attn" | "cross-attn" | "crossattn" | "encoder_attn" => Ok(Self::CrossAttention),
            "ffn" | "mlp" | "feedforward" => Ok(Self::Ffn),
            _ => Err(format!("Unknown component: {s}")),
        }
    }
}

/// Run the flow command
pub(crate) fn run(
    apr_path: &Path,
    layer_filter: Option<&str>,
    component: FlowComponent,
    verbose: bool,
    json_output: bool,
) -> Result<(), CliError> {
    if !apr_path.exists() {
        return Err(CliError::FileNotFound(apr_path.to_path_buf()));
    }

    // GH-259: All formats via Rosetta Stone (handles GGUF, SafeTensors, APR v2)
    let rosetta = RosettaStone::new();
    let report = rosetta
        .inspect(apr_path)
        .map_err(|e| CliError::InvalidFormat(format!("Failed to inspect: {e}")))?;

    let tensor_names: Vec<String> = report.tensors.iter().map(|t| t.name.clone()).collect();
    let arch = detect_architecture(&tensor_names);

    // PMAT-265: JSON output mode
    if json_output {
        return output_flow_json(apr_path, &report, &tensor_names, arch, component);
    }

    // APR reader for verbose tensor stats (only available for APR format)
    let apr_reader = AprReader::open(apr_path).ok();

    println!(
        "{} {} ({}, {})",
        "Model:".bold(),
        apr_path
            .file_name()
            .and_then(|s| s.to_str())
            .unwrap_or("model")
            .cyan(),
        arch.yellow(),
        format!("{}", report.format).cyan()
    );
    println!();

    match component {
        FlowComponent::Full => {
            print_full_flow(apr_reader.as_ref(), &tensor_names, arch, verbose);
        }
        FlowComponent::Encoder => {
            print_encoder_flow(apr_reader.as_ref(), &tensor_names, verbose);
        }
        FlowComponent::Decoder => {
            print_decoder_flow(apr_reader.as_ref(), &tensor_names, verbose);
        }
        FlowComponent::CrossAttention => {
            print_cross_attention_flow(apr_reader.as_ref(), &tensor_names, layer_filter, verbose);
        }
        FlowComponent::SelfAttention => {
            print_self_attention_flow(apr_reader.as_ref(), &tensor_names, layer_filter, verbose);
        }
        FlowComponent::Ffn => {
            print_ffn_flow(apr_reader.as_ref(), &tensor_names, layer_filter, verbose);
        }
    }

    Ok(())
}

/// Detect model architecture from tensor naming patterns
fn detect_architecture(tensor_names: &[String]) -> &'static str {
    // Handle both "encoder.*" and "model.encoder.*" naming conventions
    let has_encoder = tensor_names
        .iter()
        .any(|n| n.starts_with("encoder") || n.starts_with("model.encoder"));
    let has_decoder = tensor_names
        .iter()
        .any(|n| n.starts_with("decoder") || n.starts_with("model.decoder"));
    let has_cross_attn = tensor_names
        .iter()
        .any(|n| n.contains("encoder_attn") || n.contains("cross_attn"));

    // PMAT-265: Detect decoder-only LLM patterns (model.layers.*, blk.*)
    let has_model_layers = tensor_names
        .iter()
        .any(|n| n.starts_with("model.layers.") || n.starts_with("blk."));
    let has_lm_head = tensor_names
        .iter()
        .any(|n| n == "lm_head.weight" || n == "output.weight");
    let has_transformer_h = tensor_names.iter().any(|n| n.starts_with("transformer.h."));

    if has_encoder && has_decoder && has_cross_attn {
        "encoder-decoder (Whisper/T5)"
    } else if has_encoder && !has_decoder {
        "encoder-only (BERT)"
    } else if has_decoder && !has_encoder {
        "decoder-only (GPT/LLaMA)"
    } else if has_model_layers && has_lm_head {
        "decoder-only (LLaMA/Qwen2)"
    } else if has_transformer_h {
        "decoder-only (GPT-2)"
    } else if has_model_layers {
        "decoder-only (transformer)"
    } else {
        "unknown"
    }
}

/// Count the number of distinct layers matching the given prefixes.
///
/// Looks for tensor names starting with any of the `prefixes`, strips the prefix,
/// parses the first dot-separated segment as a layer index, and returns `max + 1`.
fn count_layers_by_prefixes(tensor_names: &[String], prefixes: &[&str]) -> usize {
    tensor_names
        .iter()
        .filter(|n| prefixes.iter().any(|p| n.starts_with(p)))
        .filter_map(|n| {
            prefixes
                .iter()
                .find_map(|p| n.strip_prefix(p))
                .and_then(|s| s.split('.').next())
                .and_then(|s| s.parse::<usize>().ok())
        })
        .max()
        .map_or(0, |n| n + 1)
}

/// PMAT-265: Output flow data as JSON
fn output_flow_json(
    path: &Path,
    report: &aprender::format::rosetta::InspectionReport,
    tensor_names: &[String],
    architecture: &str,
    component: FlowComponent,
) -> Result<(), CliError> {
    use std::collections::BTreeMap;

    let filename = path.file_name().and_then(|s| s.to_str()).unwrap_or("model");

    // Group tensors by component
    let mut components: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
    for name in tensor_names {
        let group = if name.starts_with("encoder.") || name.starts_with("model.encoder.") {
            "encoder"
        } else if name.starts_with("decoder.")
            || name.starts_with("model.decoder.")
            || name.starts_with("model.layers.")
            || name.starts_with("blk.")
        {
            "decoder"
        } else if name.contains("embed") || name.contains("token_embd") {
            "embedding"
        } else if name == "lm_head.weight" || name == "output.weight" {
            "lm_head"
        } else {
            "other"
        };
        components.entry(group).or_default().push(name);
    }

    // Count layers
    let n_encoder_layers =
        count_layers_by_prefixes(tensor_names, &["encoder.layers.", "model.encoder.layers."]);
    let n_decoder_layers = count_layers_by_prefixes(
        tensor_names,
        &[
            "decoder.layers.",
            "model.decoder.layers.",
            "model.layers.",
            "blk.",
        ],
    );

    // Build JSON manually to avoid serde dependency
    let component_str = format!("{component:?}").to_lowercase();
    let component_counts: String = components
        .iter()
        .map(|(k, v)| format!("    \"{k}\": {}", v.len()))
        .collect::<Vec<_>>()
        .join(",\n");

    println!("{{");
    println!("  \"file\": \"{filename}\",");
    println!("  \"format\": \"{}\",", report.format);
    println!("  \"architecture\": \"{architecture}\",");
    println!("  \"component\": \"{component_str}\",");
    println!("  \"total_tensors\": {},", tensor_names.len());
    println!("  \"encoder_layers\": {n_encoder_layers},");
    println!("  \"decoder_layers\": {n_decoder_layers},");
    println!("  \"tensor_groups\": {{");
    println!("{component_counts}");
    println!("  }}");
    println!("}}");

    Ok(())
}

/// Print full model flow — dispatches based on detected architecture
fn print_full_flow(
    _reader: Option<&AprReader>,
    tensor_names: &[String],
    arch: &str,
    verbose: bool,
) {
    println!(
        "{}",
        "═══════════════════════════════════════════════════════════════".dimmed()
    );
    println!("{}", "                     FULL MODEL DATA FLOW".bold());
    println!(
        "{}",
        "═══════════════════════════════════════════════════════════════".dimmed()
    );
    println!();

    if arch.starts_with("decoder-only") {
        print_decoder_only_flow(tensor_names, verbose);
    } else {
        print_encoder_decoder_flow(tensor_names, verbose);
    }
}

/// Print encoder-decoder (Whisper/T5) full flow
fn print_encoder_decoder_flow(tensor_names: &[String], verbose: bool) {
    // Input
    println!("{}  audio [N, samples]", "INPUT".green().bold());
    println!("");
    println!("");

    // Mel spectrogram
    println!("{}  mel_spectrogram()", "┌─────────────────┐".blue());
    println!("{}  [N, 80, T]      {}", "".blue(), "".blue());
    println!("{}", "└────────┬────────┘".blue());
    println!("");
    println!("");

    // Encoder
    print_encoder_block(tensor_names, verbose);

    // Decoder
    print_decoder_block(tensor_names, verbose);

    // Output
    println!("");
    println!("");
    println!("{}  tokens [N, seq_len]", "OUTPUT".green().bold());
}

/// Print decoder-only (LLaMA/Qwen/GPT) full flow
fn print_decoder_only_flow(tensor_names: &[String], verbose: bool) {
    // GH-530: Warn that verbose flow output is not yet implemented
    if verbose {
        eprintln!(
            "Warning: --verbose is not yet implemented for flow visualization. Flag ignored."
        );
    }
    let n_layers =
        count_layers_by_prefixes(tensor_names, &["model.layers.", "blk.", "transformer.h."]);

    // Detect FFN type from tensor names
    let has_gate_proj = tensor_names
        .iter()
        .any(|n| n.contains("gate_proj") || n.contains("ffn_gate") || n.contains("w1"));
    let ffn_type = if has_gate_proj { "SwiGLU" } else { "GELU" };

    // Detect GQA from tensor names
    let has_gqa = tensor_names
        .iter()
        .any(|n| n.contains("k_proj") || n.contains("attn_k"));

    // Input
    println!("{}  tokens [batch, seq_len]", "INPUT".green().bold());
    println!("");
    println!("");

    // Token embedding
    println!("┌───────────────────────────────────────────────────────────┐");
    println!(
        "{}",
        "Token Embed".yellow()
    );
    println!("│  embed_tokens.weight → [batch, seq_len, hidden_dim]      │");
    println!("└────────────────────────────┬──────────────────────────────┘");
    println!("");
    println!("");

    // Transformer blocks
    if n_layers > 0 {
        println!("┌───────────────────────────────────────────────────────────┐");
        println!(
            "{} × {}",
            "Transformer Layers".cyan().bold(),
            n_layers.to_string().green().bold()
        );
        println!("│  ┌─────────────────────────────────────────────────────┐ │");
        println!(
            "│  │  {}{} → + residual          │ │",
            "RMSNorm".blue(),
            if has_gqa {
                "GQA Self-Attention + RoPE".yellow()
            } else {
                "Self-Attention + RoPE".yellow()
            }
        );
        println!(
            "│  │  {}{} → + residual                      │ │",
            "RMSNorm".blue(),
            ffn_type.yellow()
        );
        println!("│  └─────────────────────────────────────────────────────┘ │");
        println!("└────────────────────────────┬──────────────────────────────┘");
    }

    // Final norm
    println!("");
    println!("");
    println!("┌───────────────────────────────────────────────────────────┐");
    println!(
        "{}",
        "Final RMSNorm".blue()
    );
    println!("└────────────────────────────┬──────────────────────────────┘");

    // LM head
    println!("");
    println!("");
    println!("┌───────────────────────────────────────────────────────────┐");
    println!(
        "{} → logits [batch, seq_len, vocab_size]               │",
        "LM Head".yellow()
    );
    println!("└────────────────────────────┬──────────────────────────────┘");
    println!("");
    println!("");
    println!("{}  next_token_id", "OUTPUT".green().bold());
}

/// Print encoder flow
fn print_encoder_flow(reader: Option<&AprReader>, tensor_names: &[String], verbose: bool) {
    // GH-530: Warn that reader data is not yet used for encoder flow
    if reader.is_some() {
        eprintln!("Warning: --verbose with reader data is not yet implemented for encoder flow.");
    }
    println!(
        "{}",
        "═══════════════════════════════════════════════════════════════".dimmed()
    );
    println!("{}", "                       ENCODER DATA FLOW".bold());
    println!(
        "{}",
        "═══════════════════════════════════════════════════════════════".dimmed()
    );
    println!();

    print_encoder_block(tensor_names, verbose);
}

fn print_encoder_block(tensor_names: &[String], _verbose: bool) {
    // Count encoder layers
    let n_layers = tensor_names
        .iter()
        .filter(|n| n.starts_with("encoder.layers."))
        .filter_map(|n| {
            n.strip_prefix("encoder.layers.")
                .and_then(|s| s.split('.').next())
                .and_then(|s| s.parse::<usize>().ok())
        })
        .max()
        .map_or(0, |n| n + 1);

    println!("┌─────────────────────────────────────────────────────────────┐");
    println!(
        "{}",
        "ENCODER".cyan().bold()
    );
    println!("├─────────────────────────────────────────────────────────────┤");

    // Conv layers
    if tensor_names.iter().any(|n| n.contains("conv1")) {
        println!("│  mel [N, 80, T]                                             │");
        println!("│       │                                                     │");
        println!("│       ▼                                                     │");
        println!("│  ┌─────────┐                                                │");
        println!(
            "│  │ {} │ kernel=3, stride=1 → [N, d_model, T]         │",
            "Conv1".yellow()
        );
        println!("│  └────┬────┘                                                │");
        println!("│       ▼                                                     │");
        println!("│  ┌─────────┐                                                │");
        println!(
            "│  │ {} │ kernel=3, stride=2 → [N, d_model, T/2]       │",
            "Conv2".yellow()
        );
        println!("│  └────┬────┘                                                │");
        println!("│       ▼                                                     │");
        println!("│  ┌──────────────────┐                                       │");
        println!(
            "│  │ {} │ + positional_embedding             │",
            "Pos Embed".yellow()
        );
        println!("│  └────────┬─────────┘                                       │");
    }

    // Transformer layers
    if n_layers > 0 {
        println!("│            │                                                │");
        println!("│            ▼                                                │");
        println!("│  ┌─────────────────────────────────────────────────┐        │");
        println!(
            "│  │  {} × {}                                   │        │",
            "Encoder Layers".cyan(),
            n_layers
        );
        println!("│  │  ┌─────────────────────────────────────────┐    │        │");
        println!("│  │  │ ln1 → self_attn → + residual            │    │        │");
        println!("│  │  │ ln2 → ffn → + residual                  │    │        │");
        println!("│  │  └─────────────────────────────────────────┘    │        │");
        println!("│  └─────────────────────────────────────────────────┘        │");
    }

    println!("│            │                                                │");
    println!("│            ▼                                                │");
    println!(
        "{} [N, T', d_model]                          │",
        "encoder_output".green()
    );
    println!("└─────────────────────────────────────────────────────────────┘");
    println!("");
    println!("             │ (used as K,V for cross-attention)");
    println!("");
}

/// Print decoder flow
fn print_decoder_flow(reader: Option<&AprReader>, tensor_names: &[String], verbose: bool) {
    // GH-530: Warn that reader data is not yet used for decoder flow
    if reader.is_some() {
        eprintln!("Warning: --verbose with reader data is not yet implemented for decoder flow.");
    }
    println!(
        "{}",
        "═══════════════════════════════════════════════════════════════".dimmed()
    );
    println!("{}", "                       DECODER DATA FLOW".bold());
    println!(
        "{}",
        "═══════════════════════════════════════════════════════════════".dimmed()
    );
    println!();

    print_decoder_block(tensor_names, verbose);
}

fn print_decoder_block(tensor_names: &[String], _verbose: bool) {
    // Count decoder layers (encoder-decoder models: decoder.layers.*, model.decoder.layers.*)
    let n_layers =
        count_layers_by_prefixes(tensor_names, &["decoder.layers.", "model.decoder.layers."]);

    println!("┌─────────────────────────────────────────────────────────────┐");
    println!(
        "{}",
        "DECODER".magenta().bold()
    );
    println!("├─────────────────────────────────────────────────────────────┤");
    println!("│  tokens [N, seq_len]                                        │");
    println!("│       │                                                     │");
    println!("│       ▼                                                     │");
    println!("│  ┌──────────────────┐                                       │");
    println!(
        "│  │ {} │ + positional_embedding              │",
        "Token Embed".yellow()
    );
    println!("│  └────────┬─────────┘                                       │");

    if n_layers > 0 {
        println!("│            │                                                │");
        println!("│            ▼                                                │");
        println!("│  ┌─────────────────────────────────────────────────┐        │");
        println!(
            "│  │  {} × {}                                   │        │",
            "Decoder Layers".magenta(),
            n_layers
        );
        println!("│  │  ┌─────────────────────────────────────────┐    │        │");
        println!(
            "│  │  │ ln1 → {} → + residual      │    │        │",
            "self_attn (causal)".yellow()
        );
        println!(
            "│  │  │ ln2 → {} → + residual          │    │        │",
            "cross_attn".cyan()
        );
        println!("│  │  │       └── Q from decoder                │    │        │");
        println!(
            "│  │  │       └── K,V from {} │    │        │",
            "encoder_output".green()
        );
        println!("│  │  │ ln3 → ffn → + residual                  │    │        │");
        println!("│  │  └─────────────────────────────────────────┘    │        │");
        println!("│  └─────────────────────────────────────────────────┘        │");
    }

    println!("│            │                                                │");
    println!("│            ▼                                                │");
    println!("│  ┌──────────────────┐                                       │");
    println!(
        "│  │ {} │ → logits [N, seq_len, vocab]           │",
        "LM Head".yellow()
    );
    println!("│  └────────┬─────────┘                                       │");
    println!("└─────────────────────────────────────────────────────────────┘");
}

include!("flow_print_cross.rs");
include!("flow_03.rs");