Skip to content

Commit

Permalink
Error logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesjwu committed Apr 2, 2024
1 parent 65ec2a9 commit f5cace0
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
26 changes: 22 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,28 @@ pub fn parse_path(path: &PathBuf, config: ParseConfig) -> anyhow::Result<ParseOu

for parser in &all_parsers {
if let Some(md) = parser.get_metadata(&e) {
let results = parser.parse(lineno, md, e.rank, &e.compile_id, &payload)?;
for (filename, out) in results {
output.push((filename.clone(), out));
compile_directory.push(filename);
let results = parser.parse(lineno, md, e.rank, &e.compile_id, &payload);
match results {
Ok(results) => {
for (filename, out) in results {
output.push((filename.clone(), out));
compile_directory.push(filename);
}
}
Err(err) => {
match parser.name() {
"dynamo_guards" => {
eprintln!("Failed to parse guards json: {}", err);
stats.fail_dynamo_guards_json += 1;
}
name => {
eprintln!("Parser {name} failed: {err}");
stats.fail_parser += 1;
}
}
}
}

}
}

Expand Down Expand Up @@ -220,6 +237,7 @@ pub fn parse_path(path: &PathBuf, config: ParseConfig) -> anyhow::Result<ParseOu
+ stats.fail_payload_md5
+ stats.other_rank
+ stats.fail_dynamo_guards_json
+ stats.fail_parser
> 0)
{
// Report something went wrong
Expand Down
43 changes: 32 additions & 11 deletions src/parsers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ use tinytemplate::TinyTemplate;
* 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 individual envelope
* '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<ParseOutput>;

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

// Takes a filename and a payload and writes that payload into a the file
Expand Down Expand Up @@ -59,6 +64,9 @@ pub struct SentinelFileParser {
}
}
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))
}
Expand All @@ -69,13 +77,16 @@ impl StructuredLogParser for SentinelFileParser {
compile_id: &Option<CompileId>,
payload: &str
) -> anyhow::Result<ParseOutput> {
simple_file_output(self.filename, lineno, compile_id, payload)
simple_file_output(&format!("{}.txt",self.filename), lineno, compile_id, payload)
}
}

// 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))
}
Expand All @@ -94,6 +105,9 @@ 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))
}
Expand All @@ -104,16 +118,19 @@ impl StructuredLogParser for DynamoGuardParser<'_> {
compile_id: &Option<CompileId>,
payload: &str
) -> anyhow::Result<ParseOutput> {
let filename = "dynamo_guards.html";
let filename = format!("{}.html", self.name());
let guards = serde_json::from_str::<Vec<DynamoGuard>>(payload)?;
let guards_context = DynamoGuardsContext { guards };
let output = self.tt.render(filename, &guards_context)?;
simple_file_output(filename, lineno, compile_id, &output)
let output = self.tt.render(&filename, &guards_context)?;
simple_file_output(&filename, lineno, compile_id, &output)
}
}

pub struct InductorOutputCodeParser;
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))
}
Expand Down Expand Up @@ -148,6 +165,9 @@ impl StructuredLogParser for InductorOutputCodeParser {

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))
}
Expand All @@ -172,16 +192,17 @@ impl StructuredLogParser for OptimizeDdpSplitChildParser {
pub fn all_parsers<'t>(tt: &'t TinyTemplate<'t>) -> Vec<Box<dyn StructuredLogParser + 't>> {
// We need to use Box wrappers here because vecs in Rust need to have known size
let result : Vec<Box<dyn StructuredLogParser>> = vec![
Box::new(SentinelFileParser::new("optimize_ddp_split_graph.txt", |e| e.optimize_ddp_split_graph.as_ref())),
Box::new(SentinelFileParser::new("compiled_autograd_graph.txt", |e| e.compiled_autograd_graph.as_ref())),
Box::new(SentinelFileParser::new("aot_forward_graph.txt", |e| e.aot_forward_graph.as_ref())),
Box::new(SentinelFileParser::new("aot_backward_graph.txt", |e| e.aot_backward_graph.as_ref())),
Box::new(SentinelFileParser::new("aot_joint_graph.txt", |e| e.aot_joint_graph.as_ref())),
Box::new(SentinelFileParser::new("inductor_post_grad_graph.txt", |e| e.inductor_post_grad_graph.as_ref())),
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_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(DynamoOutputGraphParser),
Box::new(DynamoGuardParser { tt }),
Box::new(InductorOutputCodeParser),
Box::new(OptimizeDdpSplitChildParser),
];

result
}
2 changes: 2 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub struct Stats {
pub fail_json: u64,
pub fail_payload_md5: u64,
pub fail_dynamo_guards_json: u64,
pub fail_parser: u64,
}

#[derive(Debug, Hash, Eq, PartialEq, Deserialize, Serialize)]
Expand Down Expand Up @@ -171,6 +172,7 @@ pub struct InductorOutputCodeMetadata {
pub filename: Option<PathBuf>,
}

#[derive(Debug)]
pub enum Metadata<'e> {
Empty(&'e EmptyMetadata),
DynamoOutputGraph(&'e DynamoOutputGraphMetadata),
Expand Down

0 comments on commit f5cace0

Please sign in to comment.