tlparse 0.4.8

Parse TORCH_LOG logs produced by PyTorch torch.compile
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
use crate::templates::TEMPLATE_QUERY_PARAM_SCRIPT;
use crate::{types::*, ParseConfig};
use fxhash::FxHashMap;
use html_escape::encode_text;
use std::cell::RefCell;
use std::collections::HashSet;
use std::ffi::{OsStr, OsString};
use std::path::Path;
use std::path::PathBuf;
use tinytemplate::TinyTemplate;

use serde_json::Value;

fn format_json_pretty(payload: &str) -> Result<String, anyhow::Error> {
    match serde_json::from_str::<Value>(payload) {
        Ok(value) => Ok(serde_json::to_string_pretty(&value)?),
        Err(_) => {
            // If failed to parse json string, use the raw payload
            Ok(payload.to_string())
        }
    }
}

use std::sync::OnceLock;
use syntect::highlighting::ThemeSet;
use syntect::parsing::SyntaxSet;

struct SyntectResources {
    syntax_set: SyntaxSet,
    theme_set: ThemeSet,
}

fn syntect_resources() -> &'static SyntectResources {
    static RESOURCES: OnceLock<SyntectResources> = OnceLock::new();
    RESOURCES.get_or_init(|| SyntectResources {
        syntax_set: SyntaxSet::load_defaults_newlines(),
        theme_set: ThemeSet::load_defaults(),
    })
}

// Re-export types from types.rs for external use
pub use crate::types::{CompileId, EmptyMetadata, Envelope, GraphRuntime, Metadata, OpRuntime};

pub enum ParserOutput {
    File(PathBuf, String),       // File to be saved on disk
    GlobalFile(PathBuf, String), // Like file, but don't give a unique suffix
    PayloadFile(PathBuf),        // File using payload directly from log entry
    PayloadReformatFile(PathBuf, fn(&str) -> Result<String, anyhow::Error>), // File using reformatted payload from log entry
    Link(String, String), // External href to (name, url) (linked in compile_directory, not returned)
}

// Each parser returns a list of files to save and links to render in compile directory
pub type ParserResults = Vec<ParserOutput>;

/**
 * StructuredLogParser
 * Parses a structured log and returns a vec of file outputs.
 * Implement this trait to add your own analyses.
 *
 * 'e is the lifetime of the envelope being parsed
 */
pub trait StructuredLogParser {
    // If this returns Some value, the parser will be run on that metadata.
    // Otherwise, it will be skipped.
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>>;

    // Take a log input and the metadata you asked for, return a set of files to write
    fn parse<'e>(
        &self,
        lineno: usize,                  // Line number from log
        metadata: Metadata<'e>,         // Metadata from get_metadata
        rank: Option<u32>,              // Rank of the log
        compile_id: &Option<CompileId>, // Compile ID of the envelope
        payload: &str,                  // Payload from the log (empty string when None)
    ) -> anyhow::Result<ParserResults>;

    // Name of the parser, for error logging
    fn name(&self) -> &'static str;
}

// Helper function to build file path with compile ID directory
pub fn build_file_path(filename: &str, lineno: usize, compile_id: &Option<CompileId>) -> PathBuf {
    let compile_id_dir: PathBuf = compile_id
        .as_ref()
        .map_or(format!("unknown_{lineno}"), |cid| cid.as_directory_name())
        .into();
    let subdir = PathBuf::from(compile_id_dir);
    subdir.join(filename)
}

// Takes a filename and a payload and writes that payload into a the file
fn simple_file_output(
    filename: &str,
    lineno: usize,
    compile_id: &Option<CompileId>,
    payload: &str,
) -> anyhow::Result<ParserResults> {
    let f = build_file_path(filename, lineno, compile_id);
    Ok(Vec::from([ParserOutput::File(f, String::from(payload))]))
}

// Takes a filename and returns PayloadFile output that uses payload directly from log entry
fn payload_file_output(
    filename: &str,
    lineno: usize,
    compile_id: &Option<CompileId>,
) -> anyhow::Result<ParserResults> {
    let f = build_file_path(filename, lineno, compile_id);
    Ok(Vec::from([ParserOutput::PayloadFile(f)]))
}

// Takes a filename and formatter function, returns PayloadReformatFile output that uses reformatted payload from log entry
fn payload_reformat_file_output(
    filename: &str,
    lineno: usize,
    compile_id: &Option<CompileId>,
    formatter: fn(&str) -> Result<String, anyhow::Error>,
) -> anyhow::Result<ParserResults> {
    let f = build_file_path(filename, lineno, compile_id);
    Ok(Vec::from([ParserOutput::PayloadReformatFile(f, formatter)]))
}

/**
 * Parser for simple output dumps where the metadata is a sentinel {}
 */
pub struct SentinelFileParser {
    filename: &'static str,
    get_sentinel: fn(&Envelope) -> Option<&EmptyMetadata>,
}
impl SentinelFileParser {
    pub fn new(
        filename: &'static str,
        get_sentinel: fn(&Envelope) -> Option<&EmptyMetadata>,
    ) -> Self {
        Self {
            filename,
            get_sentinel,
        }
    }
}
impl StructuredLogParser for SentinelFileParser {
    fn name(&self) -> &'static str {
        self.filename
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        (self.get_sentinel)(e).map(|m| Metadata::Empty(m))
    }
    fn parse<'e>(
        &self,
        lineno: usize,
        _metadata: Metadata<'e>,
        _rank: Option<u32>,
        compile_id: &Option<CompileId>,
        _payload: &str,
    ) -> anyhow::Result<ParserResults> {
        payload_file_output(&format!("{}.txt", self.filename), lineno, compile_id)
    }
}

/**
 * Generic parser for graph_dump entries
 */
pub struct GraphDumpParser;
impl StructuredLogParser for GraphDumpParser {
    fn name(&self) -> &'static str {
        "graph_dump" // ToDO: more specific?
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        if let Some(graph_dump) = &e.graph_dump {
            if graph_dump.name.starts_with("vllm_") {
                // Skip vLLM-specific graph dumps (handled by parsers under src/vllm)
                return None;
            }
        }
        e.graph_dump.as_ref().map(|m| Metadata::GraphDump(m))
    }
    fn parse<'e>(
        &self,
        lineno: usize,
        metadata: Metadata<'e>,
        _rank: Option<u32>,
        compile_id: &Option<CompileId>,
        _payload: &str,
    ) -> anyhow::Result<ParserResults> {
        if let Metadata::GraphDump(metadata) = metadata {
            let filename: PathBuf = {
                let mut r = OsString::from(&metadata.name);
                r.push(OsStr::new(".txt"));
                r.into()
            };
            payload_file_output(&filename.to_string_lossy(), lineno, compile_id)
        } else {
            Err(anyhow::anyhow!("Expected GraphDump metadata"))
        }
    }
}

// Same as SentinelFileParser, but can log the size of the graph
pub struct DynamoOutputGraphParser;
impl StructuredLogParser for DynamoOutputGraphParser {
    fn name(&self) -> &'static str {
        "dynamo_output_graph"
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        e.dynamo_output_graph
            .as_ref()
            .map(|m| Metadata::DynamoOutputGraph(m))
    }
    fn parse<'e>(
        &self,
        lineno: usize,
        _metadata: Metadata<'e>, // TODO: log size of graph
        _rank: Option<u32>,
        compile_id: &Option<CompileId>,
        _payload: &str,
    ) -> anyhow::Result<ParserResults> {
        payload_file_output("dynamo_output_graph.txt", lineno, compile_id)
    }
}

pub struct DynamoGuardParser<'t> {
    tt: &'t TinyTemplate<'t>,
}
impl StructuredLogParser for DynamoGuardParser<'_> {
    fn name(&self) -> &'static str {
        "dynamo_guards"
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        e.dynamo_guards.as_ref().map(|m| Metadata::Empty(m))
    }
    fn parse<'e>(
        &self,
        lineno: usize,
        _metadata: Metadata<'e>,
        _rank: Option<u32>,
        compile_id: &Option<CompileId>,
        payload: &str,
    ) -> anyhow::Result<ParserResults> {
        let filename = format!("{}.html", self.name());
        let guards = serde_json::from_str::<Vec<DynamoGuard>>(payload)?;
        let guards_context = DynamoGuardsContext {
            guards,
            qps: TEMPLATE_QUERY_PARAM_SCRIPT,
        };
        let output = self.tt.render(&filename, &guards_context)?;
        simple_file_output(&filename, lineno, compile_id, &output)
    }
}

pub struct InductorOutputCodeParser {
    // If true we output the code as plain text, otherwise we output it as rendered html
    plain_text: bool,
}

impl InductorOutputCodeParser {
    pub fn new(config: &ParseConfig) -> Self {
        InductorOutputCodeParser {
            plain_text: config.plain_text,
        }
    }
}

impl StructuredLogParser for InductorOutputCodeParser {
    fn name(&self) -> &'static str {
        "inductor_output_code"
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        e.inductor_output_code
            .as_ref()
            .map(|m| Metadata::InductorOutputCode(m))
    }

    fn parse<'e>(
        &self,
        lineno: usize,
        metadata: Metadata<'e>,
        _rank: Option<u32>,
        compile_id: &Option<CompileId>,
        payload: &str,
    ) -> anyhow::Result<ParserResults> {
        if let Metadata::InductorOutputCode(metadata) = metadata {
            let filename = metadata
                .filename
                .as_ref()
                .and_then(|p| Path::file_stem(p))
                .map_or_else(
                    || {
                        if self.plain_text {
                            PathBuf::from("inductor_output_code.txt")
                        } else {
                            PathBuf::from("inductor_output_code.html")
                        }
                    },
                    |stem| {
                        let mut r = OsString::from("inductor_output_code_");
                        r.push(stem);
                        if self.plain_text {
                            r.push(OsStr::new(".txt"));
                        } else {
                            r.push(OsStr::new(".html"));
                        }
                        r.into()
                    },
                );

            if self.plain_text {
                payload_file_output(&filename.to_string_lossy(), lineno, compile_id)
            } else {
                let output_content = match generate_html_output(payload) {
                    Ok(html) => html,
                    Err(_e) => {
                        return Err(anyhow::anyhow!("Failed to parse inductor code to html"))
                    }
                };
                simple_file_output(
                    &filename.to_string_lossy(),
                    lineno,
                    compile_id,
                    &output_content,
                )
            }
        } else {
            Err(anyhow::anyhow!("Expected InductorOutputCode metadata"))
        }
    }
}

fn generate_html_output(payload: &str) -> Result<String, anyhow::Error> {
    let res = syntect_resources();
    let syntax = res.syntax_set.find_syntax_by_extension("py").unwrap();
    let html = syntect::html::highlighted_html_for_string(
        &payload,
        &res.syntax_set,
        &syntax,
        &res.theme_set.themes["InspiredGitHub"],
    );
    Ok(html?)
}

pub struct OptimizeDdpSplitChildParser;
impl StructuredLogParser for OptimizeDdpSplitChildParser {
    fn name(&self) -> &'static str {
        "optimize_ddp_split_child"
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        e.optimize_ddp_split_child
            .as_ref()
            .map(|m| Metadata::OptimizeDdpSplitChild(m))
    }

    fn parse<'e>(
        &self,
        lineno: usize,
        metadata: Metadata<'e>,
        _rank: Option<u32>,
        compile_id: &Option<CompileId>,
        _payload: &str,
    ) -> anyhow::Result<ParserResults> {
        if let Metadata::OptimizeDdpSplitChild(m) = metadata {
            let filename = format!("optimize_ddp_split_child_{}.txt", m.name);
            payload_file_output(&filename, lineno, compile_id)
        } else {
            Err(anyhow::anyhow!("Expected OptimizeDdpSplitChild metadata"))
        }
    }
}

pub struct LinkParser;
impl StructuredLogParser for LinkParser {
    fn name(&self) -> &'static str {
        "link_parser"
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        e.link.as_ref().map(|m| Metadata::Link(m))
    }

    fn parse<'e>(
        &self,
        _lineno: usize,
        metadata: Metadata<'e>,
        _rank: Option<u32>,
        _compile_id: &Option<CompileId>,
        _payload: &str,
    ) -> anyhow::Result<ParserResults> {
        if let Metadata::Link(m) = metadata {
            Ok(Vec::from([ParserOutput::Link(
                m.name.clone(),
                m.url.clone(),
            )]))
        } else {
            Err(anyhow::anyhow!("Expected Link Metadata"))
        }
    }
}

fn format_stack(stack: &StackSummary, caption: &str, open: bool) -> String {
    let mut trie = StackTrieNode::default();
    trie.insert_no_terminal(stack.to_vec());
    trie.fmt(None, caption, open).unwrap()
}

// HashMap requires cloning the key for tuple lookups. If this becomes a perf
// bottleneck, switch to a 2-level cache (FxHashMap<StackSummary, FxHashMap<String, String>>)
// which wouldn't require cloning the key.
fn format_stack_cached(
    cache: &mut FxHashMap<(StackSummary, String), String>,
    stack: &StackSummary,
    caption: &str,
) -> String {
    let key = (stack.clone(), caption.to_string());
    if let Some(cached) = cache.get(&key) {
        return cached.clone();
    }
    let result = format_stack(stack, caption, false);
    cache.insert(key, result.clone());
    result
}

pub struct CompilationMetricsParser<'t> {
    pub tt: &'t TinyTemplate<'t>,
    pub stack_index: &'t RefCell<StackIndex>,
    pub symbolic_shape_specialization_index: &'t RefCell<SymbolicShapeSpecializationIndex>,
    pub guard_added_fast_index: &'t RefCell<GuardAddedFastIndex>,
    pub create_symbol_index: &'t RefCell<CreateSymbolIndex>,
    pub unbacked_symbol_index: &'t RefCell<UnbackedSymbolIndex>,
    pub output_files: &'t Vec<OutputFile>,
    pub compile_id_dir: &'t PathBuf,
}
impl StructuredLogParser for CompilationMetricsParser<'_> {
    fn name(&self) -> &'static str {
        "compilation_metrics"
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        e.compilation_metrics
            .as_ref()
            .map(|m| Metadata::CompilationMetrics(m))
    }
    fn parse<'e>(
        &self,
        lineno: usize,
        metrics: Metadata<'e>,
        _rank: Option<u32>,
        compile_id: &Option<CompileId>,
        _payload: &str,
    ) -> anyhow::Result<ParserResults> {
        let filename = format!("{}.html", self.name());
        let mut stack_cache: FxHashMap<(StackSummary, String), String> = FxHashMap::default();
        if let Metadata::CompilationMetrics(m) = metrics {
            let id = compile_id
                .clone()
                .map_or("(unknown) ".to_string(), |c| format!("{cid} ", cid = c));
            let mut cid = compile_id.clone();
            if let Some(c) = cid.as_mut() {
                if let Some(_frame_id) = c.frame_compile_id {
                    // data migration for old logs that don't have attempt
                    c.attempt = Some(0);
                }
            }
            let stack_html = self
                .stack_index
                .borrow()
                .get(&cid)
                .map_or("".to_string(), |stack| format_stack(stack, "Stack", false));
            let mini_stack_html = if let (Some(name), Some(filename), Some(line)) =
                (&m.co_name, &m.co_filename, m.co_firstlineno)
            {
                format_stack(
                    &Vec::from([FrameSummary {
                        uninterned_filename: Some(filename.clone()),
                        filename: u32::MAX,
                        line: line,
                        name: name.clone(),
                        loc: None,
                    }]),
                    "Stack",
                    false,
                )
            } else {
                "".to_string()
            };
            let specializations: Vec<_> = self
                .symbolic_shape_specialization_index
                .borrow_mut()
                .remove(&cid)
                .unwrap_or_default()
                .drain(..)
                .map(|spec| {
                    let user_stack = spec.user_stack.unwrap_or_default();
                    let stack = spec.stack.unwrap_or_default();
                    SymbolicShapeSpecializationContext {
                        symbol: spec.symbol.unwrap_or("".to_string()),
                        sources: spec.sources.unwrap_or_default(),
                        value: spec.value.unwrap_or("".to_string()),
                        user_stack_html: format_stack_cached(
                            &mut stack_cache,
                            &user_stack,
                            "User Stack",
                        ),
                        stack_html: format_stack_cached(
                            &mut stack_cache,
                            &stack,
                            "Framework Stack",
                        ),
                    }
                })
                .collect();
            let guards_added_fast: Vec<_> = self
                .guard_added_fast_index
                .borrow_mut()
                .remove(&cid)
                .unwrap_or_default()
                .drain(..)
                .map(|guard| {
                    let user_stack = guard.user_stack.unwrap_or_default();
                    let stack = guard.stack.unwrap_or_default();
                    GuardAddedFastContext {
                        expr: guard.expr.unwrap_or("".to_string()),
                        user_stack_html: format_stack_cached(
                            &mut stack_cache,
                            &user_stack,
                            "User Stack",
                        ),
                        stack_html: format_stack_cached(
                            &mut stack_cache,
                            &stack,
                            "Framework Stack",
                        ),
                    }
                })
                .collect();
            let create_symbols: Vec<_> = self
                .create_symbol_index
                .borrow_mut()
                .remove(&cid)
                .unwrap_or_default()
                .drain(..)
                .map(|sym| {
                    let user_stack = sym.user_stack.unwrap_or_default();
                    let stack = sym.stack.unwrap_or_default();
                    CreateSymbolContext {
                        symbol: sym.symbol.unwrap_or("".to_string()),
                        val: sym.val.unwrap_or("".to_string()),
                        vr: sym.vr.unwrap_or("".to_string()),
                        source: sym.source.unwrap_or("".to_string()),
                        user_stack_html: format_stack_cached(
                            &mut stack_cache,
                            &user_stack,
                            "User Stack",
                        ),
                        stack_html: format_stack_cached(
                            &mut stack_cache,
                            &stack,
                            "Framework Stack",
                        ),
                    }
                })
                .collect();
            let unbacked_symbols: Vec<_> = self
                .unbacked_symbol_index
                .borrow_mut()
                .remove(&cid)
                .unwrap_or_default()
                .drain(..)
                .map(|sym| {
                    let user_stack = sym.user_stack.unwrap_or_default();
                    let stack = sym.stack.unwrap_or_default();
                    UnbackedSymbolContext {
                        symbol: sym.symbol.unwrap_or("".to_string()),
                        vr: sym.vr.unwrap_or("".to_string()),
                        user_stack_html: format_stack_cached(
                            &mut stack_cache,
                            &user_stack,
                            "User Stack",
                        ),
                        stack_html: format_stack_cached(
                            &mut stack_cache,
                            &stack,
                            "Framework Stack",
                        ),
                    }
                })
                .collect();
            let remove_prefix = |x: &String| -> String {
                // url is X_Y_Z/<rest>. Get the rest of the string for the link
                // on compilation metrics page
                let parts: Vec<_> = x.split("/").collect();
                let new_str: String = parts[1..].join("");
                new_str
            };
            let output_files: Vec<OutputFile> = self
                .output_files
                .iter()
                .map(|o| OutputFile {
                    url: remove_prefix(&o.url),
                    name: remove_prefix(&o.name),
                    number: o.number.clone(),
                    suffix: o.suffix.clone(),
                    readable_url: o.readable_url.as_ref().map(|u| remove_prefix(u)),
                })
                .collect();
            let extra_metrics: Vec<ExtraMetricContext> = m
                .extra
                .iter()
                .map(|(key, value)| {
                    let value_html = match value {
                        serde_json::Value::String(s) => {
                            if s.len() > 200 {
                                format!("<details><summary>(click to expand)</summary><pre>{}</pre></details>",
                                    html_escape::encode_text(s))
                            } else {
                                html_escape::encode_text(s).to_string()
                            }
                        }
                        serde_json::Value::Null => "null".to_string(),
                        other => {
                            let s = other.to_string();
                            if s.len() > 200 {
                                format!("<details><summary>(click to expand)</summary><pre>{}</pre></details>",
                                    html_escape::encode_text(&s))
                            } else {
                                html_escape::encode_text(&s).to_string()
                            }
                        }
                    };
                    ExtraMetricContext {
                        key: key.clone(),
                        value_html,
                    }
                })
                .collect();
            let context = CompilationMetricsContext {
                css: crate::CSS,
                m: &m,
                compile_id: id,
                stack_html: stack_html,
                mini_stack_html: mini_stack_html,
                symbolic_shape_specializations: specializations,
                guards_added_fast: guards_added_fast,
                create_symbols: create_symbols,
                unbacked_symbols: unbacked_symbols,
                output_files: &output_files,
                compile_id_dir: &self.compile_id_dir,
                extra_metrics: extra_metrics,
                qps: TEMPLATE_QUERY_PARAM_SCRIPT,
            };
            let output = self.tt.render(&filename, &context)?;
            simple_file_output(&filename, lineno, compile_id, &output)
        } else {
            Err(anyhow::anyhow!("Expected CompilationMetrics metadata"))
        }
    }
}

pub struct AOTAutogradBackwardCompilationMetricsParser<'t> {
    tt: &'t TinyTemplate<'t>,
}
impl StructuredLogParser for AOTAutogradBackwardCompilationMetricsParser<'_> {
    fn name(&self) -> &'static str {
        "aot_autograd_backward_compilation_metrics"
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        e.aot_autograd_backward_compilation_metrics
            .as_ref()
            .map(|m| Metadata::AOTAutogradBackwardCompilationMetrics(m))
    }
    fn parse<'e>(
        &self,
        lineno: usize,
        metrics: Metadata<'e>,
        _rank: Option<u32>,
        compile_id: &Option<CompileId>,
        _payload: &str,
    ) -> anyhow::Result<ParserResults> {
        let filename = format!("{}.html", self.name());
        if let Metadata::AOTAutogradBackwardCompilationMetrics(m) = metrics {
            let id = compile_id
                .clone()
                .map_or("(unknown) ".to_string(), |c| format!("{cid} ", cid = c));
            let context = AOTAutogradBackwardCompilationMetricsContext {
                css: crate::CSS,
                m: &m,
                compile_id: id,
                qps: TEMPLATE_QUERY_PARAM_SCRIPT,
            };
            let output = self.tt.render(&filename, &context)?;
            simple_file_output(&filename, lineno, compile_id, &output)
        } else {
            Err(anyhow::anyhow!(
                "Expected AOTAutogradBackwardCompilationMetrics metadata"
            ))
        }
    }
}

pub struct BwdCompilationMetricsParser<'t> {
    tt: &'t TinyTemplate<'t>,
}
impl StructuredLogParser for BwdCompilationMetricsParser<'_> {
    fn name(&self) -> &'static str {
        "bwd_compilation_metrics"
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        e.bwd_compilation_metrics
            .as_ref()
            .map(|m| Metadata::BwdCompilationMetrics(m))
    }
    fn parse<'e>(
        &self,
        lineno: usize,
        metrics: Metadata<'e>,
        _rank: Option<u32>,
        compile_id: &Option<CompileId>,
        _payload: &str,
    ) -> anyhow::Result<ParserResults> {
        let filename = format!("{}.html", self.name());
        if let Metadata::BwdCompilationMetrics(m) = metrics {
            let id = compile_id
                .clone()
                .map_or("(unknown) ".to_string(), |c| format!("{cid} ", cid = c));
            let context = BwdCompilationMetricsContext {
                css: crate::CSS,
                m: &m,
                compile_id: id,
                qps: TEMPLATE_QUERY_PARAM_SCRIPT,
            };
            let output = self.tt.render(&filename, &context)?;
            simple_file_output(&filename, lineno, compile_id, &output)
        } else {
            Err(anyhow::anyhow!("Expected BwdCompilationMetrics metadata"))
        }
    }
}

pub struct DumpFileParser;
impl StructuredLogParser for DumpFileParser {
    fn name(&self) -> &'static str {
        "dump_file"
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        e.dump_file.as_ref().map(|m| Metadata::DumpFile(m))
    }
    fn parse<'e>(
        &self,
        _lineno: usize,
        metadata: Metadata<'e>,
        _rank: Option<u32>,
        _compile_id: &Option<CompileId>,
        payload: &str,
    ) -> anyhow::Result<ParserResults> {
        if let Metadata::DumpFile(metadata) = metadata {
            let mb_fx_id = extract_eval_with_key_id(&metadata.name);
            let filename = if let Some(fx_id) = mb_fx_id {
                format!("eval_with_key_{}.html", fx_id)
            } else {
                format!("{}.html", metadata.name)
            };
            let subdir = PathBuf::from("dump_file");
            let f = subdir.join(filename);
            Ok(Vec::from([ParserOutput::GlobalFile(
                f,
                anchor_source(payload),
            )]))
        } else {
            Err(anyhow::anyhow!("Expected DumpFile metadata"))
        }
    }
}

pub fn anchor_source(text: &str) -> String {
    let lines: Vec<&str> = text.lines().collect();
    let mut html = String::from(
        r#"<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Source Code</title>
    <style>
        pre {
            counter-reset: line;
        }
        pre span {
            display: block;
        }
        pre span:before {
            counter-increment: line;
            content: counter(line);
            display: inline-block;
            padding: 0 .5em;
            margin-right: .5em;
            color: #888;
        }
        pre span:target {
            background-color: #ffff00;
        }
    </style>
</head>
<body>
    <pre>"#,
    );

    for (i, line) in lines.iter().enumerate() {
        let line_number = i + 1;
        html.push_str(&format!(
            r#"<span id="L{}">{}</span>"#,
            line_number,
            encode_text(line)
        ));
    }

    html.push_str(&format!(
        "</pre>{TEMPLATE_QUERY_PARAM_SCRIPT}</body></html>"
    ));
    html
}

pub fn read_runtime_estimations(
    out_path: &PathBuf,
    rank_nums: &[u32],
) -> anyhow::Result<Vec<GraphRuntime>> {
    read_artifacts(
        out_path,
        rank_nums,
        "inductor_runtime_and_tensor_meta",
        |content, rank, graph| {
            #[derive(serde::Deserialize)]
            struct RuntimeJson {
                ops: Vec<OpRuntime>,
            }

            let json: RuntimeJson = serde_json::from_str(content)?;
            Ok((!json.ops.is_empty()).then(|| GraphRuntime {
                rank,
                graph,
                ops: json.ops,
            }))
        },
    )
}

/// Reads inductor_tlparse_tensor_meta*.json from each rank/graph, canonicalizes the JSON,
/// computes a fingerprint per graph, and returns entries for each graph
pub fn read_tensor_meta_fingerprints(
    out_path: &PathBuf,
    rank_nums: &[u32],
) -> anyhow::Result<Vec<TensorMetaFingerprint>> {
    read_artifacts(
        out_path,
        rank_nums,
        "inductor_runtime_and_tensor_meta",
        |content, rank, graph| {
            // Canonicalize JSON: parse Value and serialize compact to ensure stable formatting
            let json_value: serde_json::Value = serde_json::from_str(content)?;
            let canonical_json = serde_json::to_string(&json_value)?;
            Ok(Some(TensorMetaFingerprint {
                rank,
                graph,
                fingerprint: canonical_json,
            }))
        },
    )
}

/// Reads collective schedule artifacts from processed rank directories
/// Handles multiple graphs per rank
pub fn read_collective_schedules(
    out_path: &PathBuf,
    rank_nums: &[u32],
) -> anyhow::Result<Vec<CollectiveSchedule>> {
    read_artifacts(
        out_path,
        rank_nums,
        "inductor_collective_schedule",
        |content, rank, graph| {
            let ops: Vec<String> = serde_json::from_str(content)?;
            Ok((!ops.is_empty()).then(|| CollectiveSchedule { rank, graph, ops }))
        },
    )
}

pub fn check_collectives_parity(out_path: &PathBuf, rank_nums: &[u32]) -> anyhow::Result<()> {
    use regex::Regex;
    use std::{collections::HashMap, fs};

    // Match c10d functional calls: torch.ops._c10d_functional.<op>.default(
    let call_re = Regex::new(
        r"torch\s*\.\s*ops\s*\.\s*_?c10d_functional\s*\.\s*([A-Za-z0-9_]+)\s*\.\s*default\s*\(",
    )?;
    let comment_re = Regex::new(r"(?m)#.*$|//.*$|(?s)/\*.*?\*/")?;
    let html_tag_re = Regex::new(r"(?s)<[^>]*>")?;

    for &rank in rank_nums {
        let rank_dir = out_path.join(format!("rank_{rank}"));
        if !rank_dir.exists() {
            continue;
        }

        // Map compile directory (graph folder) name prefix -> compile ID
        let dir_to_compile_id: HashMap<String, String> =
            fs::read_to_string(rank_dir.join("compile_directory.json"))
                .ok()
                .and_then(|s| serde_json::from_str::<serde_json::Value>(&s).ok())
                .and_then(|v| {
                    v.as_object().map(|obj| {
                        obj.iter().fold(HashMap::new(), |mut m, (cid, entry)| {
                            if let Some(arts) = entry.get("artifacts").and_then(|x| x.as_array()) {
                                for a in arts {
                                    if let Some(url) = a.get("url").and_then(|x| x.as_str()) {
                                        if let Some((prefix, _)) = url.split_once('/') {
                                            m.entry(prefix.to_string())
                                                .or_insert_with(|| cid.to_string());
                                        }
                                    }
                                }
                            }
                            m
                        })
                    })
                })
                .unwrap_or_default();

        let mut report = crate::types::CollectivesParityReport {
            description: "Difference of # of collectives in scheduler and inductor output code and missing wait collectives"
                .to_string(),
            graphs: Vec::new(),
        };

        for compile_dir in fs::read_dir(&rank_dir)?
            .flatten()
            .map(|e| e.path())
            .filter(|p| p.is_dir())
        {
            let (mut schedule_path, mut code_path) = (None, None);
            for p in fs::read_dir(&compile_dir)?.flatten().map(|e| e.path()) {
                let stem = p.file_stem().and_then(|s| s.to_str()).unwrap_or("");
                if p.extension() == Some(OsStr::new("json"))
                    && stem.starts_with("inductor_collective_schedule")
                {
                    schedule_path = Some(p);
                } else if stem.starts_with("inductor_output_code") && code_path.is_none() {
                    code_path = Some(p);
                }
            }

            let (Some(schedule), Some(code)) = (schedule_path, code_path) else {
                continue;
            };

            let raw_ops: Vec<String> =
                serde_json::from_str(&fs::read_to_string(schedule)?).unwrap_or_default();
            // Extract and normalize op names from schedule
            let normalize_op = |op: &str| -> Option<&'static str> {
                let op = op.trim_end_matches('_');
                [
                    "all_reduce",
                    "reduce_scatter",
                    "all_gather",
                    "broadcast",
                    "all_to_all",
                ]
                .iter()
                .find(|&&name| op.contains(name))
                .copied()
                .or_else(|| {
                    (op.contains("reduce")
                        && !op.contains("all_reduce")
                        && !op.contains("reduce_scatter"))
                    .then_some("reduce")
                })
            };

            let mut schedule_counts: HashMap<&str, usize> = HashMap::new();
            for op in &raw_ops {
                if let Some(normalized) = normalize_op(op) {
                    *schedule_counts.entry(normalized).or_insert(0) += 1;
                }
            }

            // Code counts: strip tags and comments, then count calls
            let code_clean = comment_re
                .replace_all(&html_tag_re.replace_all(&fs::read_to_string(code)?, ""), "")
                .into_owned();
            let mut code_counts: HashMap<&str, usize> = HashMap::new();
            let mut wait_count = 0usize;
            for cap in call_re.captures_iter(&code_clean) {
                let op = cap.get(1).unwrap().as_str();
                if op == "wait_tensor" {
                    wait_count += 1;
                } else if let Some(normalized) = normalize_op(op) {
                    *code_counts.entry(normalized).or_insert(0) += 1;
                }
            }
            let collective_total: usize = code_counts.values().sum();
            let missing_waits = collective_total.saturating_sub(wait_count);

            // Compute offset over union of all detected ops
            let mut all_ops: std::collections::HashSet<&str> =
                schedule_counts.keys().copied().collect();
            all_ops.extend(code_counts.keys().copied());
            let offset: usize = all_ops
                .iter()
                .map(|&n| {
                    schedule_counts
                        .get(n)
                        .copied()
                        .unwrap_or(0)
                        .abs_diff(code_counts.get(n).copied().unwrap_or(0))
                })
                .sum();

            if offset > 0 || missing_waits > 0 {
                let graph = compile_dir
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("unknown")
                    .to_string();
                let compile_id = dir_to_compile_id
                    .get(&graph)
                    .cloned()
                    .unwrap_or_else(|| "unknown".to_string());
                report.graphs.push(crate::types::GraphCollectivesParity {
                    graph,
                    compile_id,
                    offset,
                    missing_waits,
                });
            }
        }

        fs::write(
            rank_dir.join("collectives_parity.json"),
            serde_json::to_string_pretty(&report)?,
        )?;
    }

    Ok(())
}

/// Parses a prefixed JSON file from each multi-rank output directory.
/// It finds the first matching file, calls `parse_fn` on its contents,
/// and collects the `Some(T)` results into a vector.
fn read_artifacts<T>(
    out_path: &PathBuf,
    rank_nums: &[u32],
    file_prefix: &str,
    parse_fn: impl Fn(&str, u32, String) -> anyhow::Result<Option<T>>,
) -> anyhow::Result<Vec<T>> {
    use anyhow::Context;
    use std::fs;

    let mut results = Vec::new();

    for &rank in rank_nums {
        let rank_dir = out_path.join(format!("rank_{rank}"));

        // Skip missing rank directories (some ranks may not have collective schedules)
        if !rank_dir.exists() {
            continue;
        }

        for entry in fs::read_dir(&rank_dir)?
            .flatten()
            .filter(|e| e.path().is_dir())
        {
            let compile_dir = entry.path();

            let file = fs::read_dir(&compile_dir)?.flatten().find(|e| {
                let path = e.path();
                path.extension() == Some(OsStr::new("json"))
                    && path
                        .file_stem()
                        .and_then(|s| s.to_str())
                        .map_or(false, |s| s.starts_with(file_prefix))
            });

            if let Some(file) = file {
                let content = fs::read_to_string(file.path())
                    .with_context(|| format!("Reading {file_prefix} for rank {rank}"))?;

                let graph = compile_dir
                    .file_name()
                    .and_then(|n| n.to_str())
                    .unwrap_or("unknown")
                    .to_string();

                if let Some(result) = parse_fn(&content, rank, graph)? {
                    results.push(result);
                }
            }
        }
    }

    Ok(results)
}

pub struct ArtifactParser;
impl StructuredLogParser for ArtifactParser {
    fn name(&self) -> &'static str {
        "artifact"
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        e.artifact.as_ref().map(|m| Metadata::Artifact(m))
    }
    fn parse<'e>(
        &self,
        lineno: usize,
        metadata: Metadata<'e>,
        _rank: Option<u32>,
        compile_id: &Option<CompileId>,
        _payload: &str,
    ) -> anyhow::Result<ParserResults> {
        if let Metadata::Artifact(metadata) = metadata {
            match metadata.encoding.as_str() {
                "string" => {
                    let filename = format!("{}.txt", metadata.name);
                    payload_file_output(&filename, lineno, compile_id)
                }
                "json" => {
                    let filename: String = format!("{}.json", metadata.name);
                    payload_reformat_file_output(&filename, lineno, compile_id, format_json_pretty)
                }
                _ => Err(anyhow::anyhow!(
                    "Unsupported encoding: {}",
                    metadata.encoding
                )),
            }
        } else {
            Err(anyhow::anyhow!("Expected Artifact metadata"))
        }
    }
}

pub struct MemoizerArtifactsParser;
impl StructuredLogParser for MemoizerArtifactsParser {
    fn name(&self) -> &'static str {
        "memoizer_artifacts"
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        e.memoizer_artifacts
            .as_ref()
            .map(|m| Metadata::MemoizerArtifacts(m))
    }
    fn parse<'e>(
        &self,
        lineno: usize,
        _metadata: Metadata<'e>,
        _rank: Option<u32>,
        compile_id: &Option<CompileId>,
        _payload: &str,
    ) -> anyhow::Result<ParserResults> {
        payload_reformat_file_output(
            "memoizer_artifacts.json",
            lineno,
            compile_id,
            format_json_pretty,
        )
    }
}

fn render_sym_expr_trie(
    expr: u64,
    sym_expr_info_index: &SymExprInfoIndex,
    depth: usize,
    visited: &mut HashSet<u64>,
) -> Option<String> {
    if visited.contains(&expr) {
        return None;
    }
    visited.insert(expr);

    let sym_expr_info = sym_expr_info_index.get(&expr)?;
    let binding = Vec::new();
    let sym_expr_args_id = sym_expr_info.argument_ids.as_ref().unwrap_or(&binding);

    let mut children_elements = Vec::new();
    for arg_id in sym_expr_args_id {
        if let Some(child_element) =
            render_sym_expr_trie(*arg_id, sym_expr_info_index, depth + 1, visited)
        {
            children_elements.push(child_element);
        }
    }

    let mut sym_expr_trie_html = format!(
        r#"
<div style="margin-left: {}px;">
    <div style="padding: 16px; border: 1px solid #ccc; border-radius: 8px; box-shadow: 2px 2px 5px rgba(0,0,0,0.1); background-color: white;">
        <h3 style="font-weight: bold; font-size: 1.25rem;">{}</h3>
        <div style="margin-top: 8px;">
            <p><span style="font-weight: bold;">Method:</span> {}</p>
            <p><span style="font-weight: bold;">Arguments:</span> {}</p>
            <div style="margin-top: 8px; font-size: 0.875rem;">
            {}
            {}
            </div>
        </div>
    </div>
</div>
"#,
        depth * 20,
        sym_expr_info.result.as_ref().unwrap_or(&"".to_string()),
        sym_expr_info.method.as_ref().unwrap_or(&"".to_string()),
        sym_expr_info
            .arguments
            .as_ref()
            .unwrap_or(&Vec::new())
            .join(", "),
        format_stack(
            &sym_expr_info.user_stack.as_ref().unwrap_or(&Vec::new()),
            "User Stack",
            true
        ),
        format_stack(
            &sym_expr_info.stack.as_ref().unwrap_or(&Vec::new()),
            "Stack",
            false
        ),
    );
    if !children_elements.is_empty() {
        for child_element in children_elements {
            sym_expr_trie_html.push_str(&child_element);
        }
    }
    Some(sym_expr_trie_html)
}

pub struct PropagateRealTensorsParser<'t> {
    pub tt: &'t TinyTemplate<'t>,
    pub sym_expr_info_index: &'t SymExprInfoIndex,
}
impl StructuredLogParser for PropagateRealTensorsParser<'_> {
    fn name(&self) -> &'static str {
        "guard_added"
    }
    fn get_metadata<'e>(&self, e: &'e Envelope) -> Option<Metadata<'e>> {
        if let Some(m) = e.propagate_real_tensors_provenance.as_ref() {
            return Some(Metadata::SymbolicShapePropagateRealTensor(m));
        }
        if let Some(g) = e.guard_added.as_ref() {
            return Some(Metadata::SymbolicShapePropagateRealTensor(g));
        }
        return None;
    }
    fn parse<'e>(
        &self,
        lineno: usize,
        metadata: Metadata<'e>,
        _rank: Option<u32>,
        compile_id: &Option<CompileId>,
        _payload: &str,
    ) -> anyhow::Result<ParserResults> {
        if let Metadata::SymbolicShapePropagateRealTensor(m) = metadata {
            let filename = "symbolic_guard_information.html";
            let framework_stack_html = format_stack(
                &m.stack.as_ref().unwrap_or(&Vec::new()),
                "Framework Stack",
                false,
            );
            let user_stack_html = format_stack(
                &m.user_stack.as_ref().unwrap_or(&Vec::new()),
                "User Stack",
                true,
            );
            let locals_html = format!(
                "{}",
                m.frame_locals.as_ref().unwrap_or(&FrameLocals::default())
            );

            let mut visited = HashSet::new();
            let sym_expr_trie_html = render_sym_expr_trie(
                m.expr_node_id.unwrap(),
                self.sym_expr_info_index,
                0,
                &mut visited,
            )
            .unwrap_or("".to_string());

            let context = SymbolicGuardContext {
                css: crate::CSS,
                expr: m.expr.clone().unwrap(),
                user_stack_html: user_stack_html,
                framework_stack_html: framework_stack_html,
                sym_expr_trie_html: sym_expr_trie_html,
                locals_html: locals_html,
            };
            let output = self.tt.render(&filename, &context)?;
            simple_file_output(&filename, lineno, compile_id, &output)
        } else {
            Err(anyhow::anyhow!(
                "Expected SymbolicShapePropagateRealTensor metadata"
            ))
        }
    }
}

// Register your parser here
pub fn default_parsers<'t>(
    tt: &'t TinyTemplate<'t>,
    parser_config: &ParseConfig,
) -> Vec<Box<dyn StructuredLogParser + 't>> {
    // We need to use Box wrappers here because vecs in Rust need to have known size
    if parser_config.export {
        return vec![Box::new(SentinelFileParser::new("exported_program", |e| {
            e.exported_program.as_ref()
        }))];
    }

    let result: Vec<Box<dyn StructuredLogParser>> = vec![
        Box::new(SentinelFileParser::new("optimize_ddp_split_graph", |e| {
            e.optimize_ddp_split_graph.as_ref()
        })),
        Box::new(SentinelFileParser::new("compiled_autograd_graph", |e| {
            e.compiled_autograd_graph.as_ref()
        })),
        Box::new(SentinelFileParser::new("aot_forward_graph", |e| {
            e.aot_forward_graph.as_ref()
        })),
        Box::new(SentinelFileParser::new("aot_backward_graph", |e| {
            e.aot_backward_graph.as_ref()
        })),
        Box::new(SentinelFileParser::new("aot_inference_graph", |e| {
            e.aot_inference_graph.as_ref()
        })),
        Box::new(SentinelFileParser::new("aot_joint_graph", |e| {
            e.aot_joint_graph.as_ref()
        })),
        Box::new(SentinelFileParser::new("inductor_post_grad_graph", |e| {
            e.inductor_post_grad_graph.as_ref()
        })),
        Box::new(SentinelFileParser::new("inductor_pre_grad_graph", |e| {
            e.inductor_pre_grad_graph.as_ref()
        })),
        Box::new(SentinelFileParser::new("dynamo_cpp_guards_str", |e| {
            e.dynamo_cpp_guards_str.as_ref()
        })),
        Box::new(GraphDumpParser),
        Box::new(DynamoOutputGraphParser),
        Box::new(DynamoGuardParser { tt }),
        Box::new(InductorOutputCodeParser::new(parser_config)),
        Box::new(OptimizeDdpSplitChildParser),
        Box::new(AOTAutogradBackwardCompilationMetricsParser { tt }), // TODO: use own tt instances
        Box::new(BwdCompilationMetricsParser { tt }),                 // TODO: use own tt instances
        Box::new(LinkParser),
        Box::new(ArtifactParser),
        Box::new(DumpFileParser),
    ];

    result
}