Skip to content

Commit

Permalink
Add option for plain text Formatting (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
drisspg authored Jul 15, 2024
1 parent c5da30a commit 588dd9d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub struct Cli {
/// Be more chatty
#[arg(short, long)]
verbose: bool,
/// Some parsers will write output as rendered html for prettier viewing.
/// Enabiling this option will enforce output as plain text for easier diffing
#[arg(short, long)]
plain_text: bool,
}

fn main() -> anyhow::Result<()> {
Expand All @@ -57,6 +61,7 @@ fn main() -> anyhow::Result<()> {
custom_parsers: Vec::new(),
custom_header_html: cli.custom_header_html,
verbose: cli.verbose,
plain_text: cli.plain_text,
};

let output = parse_path(&path, config)?;
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub struct ParseConfig {
pub custom_parsers: Vec<Box<dyn crate::parsers::StructuredLogParser>>,
pub custom_header_html: String,
pub verbose: bool,
pub plain_text: bool,
}

impl Default for ParseConfig {
Expand All @@ -37,6 +38,7 @@ impl Default for ParseConfig {
custom_parsers: Vec::default(),
custom_header_html: String::default(),
verbose: false,
plain_text: false,
}
}
}
Expand Down Expand Up @@ -218,7 +220,7 @@ pub fn parse_path(path: &PathBuf, config: ParseConfig) -> anyhow::Result<ParseOu
})
.peekable();

let mut all_parsers = default_parsers(&tt);
let mut all_parsers = default_parsers(&tt, &config);
all_parsers.extend(config.custom_parsers);

while let Some((lineno, line)) = iter.next() {
Expand Down
53 changes: 42 additions & 11 deletions src/parsers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::types::*;
use crate::{types::*, ParseConfig};
use std::cell::RefCell;
use std::ffi::{OsStr, OsString};
use std::path::Path;
Expand Down Expand Up @@ -190,7 +190,19 @@ impl StructuredLogParser for DynamoGuardParser<'_> {
}
}

pub struct InductorOutputCodeParser;
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"
Expand All @@ -215,24 +227,40 @@ impl StructuredLogParser for InductorOutputCodeParser {
.as_ref()
.and_then(|p| Path::file_stem(p))
.map_or_else(
|| PathBuf::from("inductor_output_code.html"),
|| {
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);
r.push(OsStr::new(".html"));
if self.plain_text {
r.push(OsStr::new(".txt"));
} else {
r.push(OsStr::new(".html"));
}
r.into()
},
);
// Generate HTML output and handle potential errors
let html_payload = match generate_html_output(payload) {
Ok(html) => html,
Err(_e) => return Err(anyhow::anyhow!("Failed to parse inductor code to html")),
let output_content = if self.plain_text {
payload.to_string()
} else {
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,
&html_payload,
&output_content,
)
} else {
Err(anyhow::anyhow!("Expected InductorOutputCode metadata"))
Expand Down Expand Up @@ -514,7 +542,10 @@ impl StructuredLogParser for ArtifactParser {
}

// Register your parser here
pub fn default_parsers<'t>(tt: &'t TinyTemplate<'t>) -> Vec<Box<dyn StructuredLogParser + 't>> {
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
let result: Vec<Box<dyn StructuredLogParser>> = vec![
Box::new(SentinelFileParser::new("optimize_ddp_split_graph", |e| {
Expand All @@ -541,7 +572,7 @@ pub fn default_parsers<'t>(tt: &'t TinyTemplate<'t>) -> Vec<Box<dyn StructuredLo
Box::new(GraphDumpParser),
Box::new(DynamoOutputGraphParser),
Box::new(DynamoGuardParser { tt }),
Box::new(InductorOutputCodeParser),
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
Expand Down

0 comments on commit 588dd9d

Please sign in to comment.