Skip to content

Commit

Permalink
Merge b258b28 into f0ca526
Browse files Browse the repository at this point in the history
  • Loading branch information
kali authored Oct 8, 2024
2 parents f0ca526 + b258b28 commit 686e23c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
7 changes: 6 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ fn assertions_options(command: clap::Command) -> clap::Command {
.arg(
Arg::new("approx")
.takes_value(true)
.possible_values(["exact", "close", "approximate", "very", "super"])
.possible_values(["exact", "close", "approximate", "very", "super", "ultra"])
.default_value("close")
.long("approx")
.help("Approximation level used in assertions."),
Expand Down Expand Up @@ -439,6 +439,11 @@ fn assertions_options(command: clap::Command) -> clap::Command {
.long("assert-output-count")
.help("Check the number of outputs found."),
)
.arg(
Arg::new("allow-missing-outputs")
.long("allow-missing-outputs")
.help("Allow missing output in checks")
)
.arg(
Arg::new("assert-op-count")
.takes_value(true)
Expand Down
7 changes: 5 additions & 2 deletions cli/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,8 @@ pub struct Assertions {
pub assert_outputs: bool,
pub assert_output_facts: Option<Vec<InferenceFact>>,
pub assert_op_count: Option<Vec<(String, usize)>>,
pub approximation: Approximation
pub approximation: Approximation,
pub allow_missing_outputs: bool,
}

impl Assertions {
Expand All @@ -1124,14 +1125,16 @@ impl Assertions {
.map(|mut args| Some((args.next()?.to_string(), args.next()?.parse().ok()?)))
.collect()
});
let allow_missing_outputs = sub.is_present("allow-missing-outputs");
let approximation = match sub.value_of("approx").unwrap() {
"exact" => Approximation::Exact,
"close" => Approximation::Close,
"approximate" => Approximation::Approximate,
"very" => Approximation::VeryApproximate,
"super" => Approximation::SuperApproximate,
"ultra" => Approximation::UltraApproximate,
_ => panic!()
};
Ok(Assertions { assert_outputs, assert_output_facts, assert_op_count, approximation })
Ok(Assertions { assert_outputs, assert_output_facts, assert_op_count, approximation, allow_missing_outputs })
}
}
37 changes: 23 additions & 14 deletions cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ pub fn check_outputs(got: &[Vec<TValue>], params: &Parameters) -> TractResult<()
params.tract_model.node_name(output.node)
};
// pick expected tensor values for this output
let exp = params
.tensors_values
.by_name(name)
.with_context(|| format!("Do not have reference value for output {name:?}"))?;
let exp = params.tensors_values.by_name(name);
if exp.is_none() {
if params.assertions.allow_missing_outputs {
warn!("Missing reference output in bundle for {name}");
continue;
} else {
bail!("Missing reference output in bundle for {name}");
}
}
let exp = exp.unwrap();
debug!("Output {}, expects {:?}", ix, exp);
let mut exp: TValue = exp.values.as_ref().with_context(|| {
format!("Output {name:?}: found reference info without value: {exp:?}")
Expand Down Expand Up @@ -84,15 +90,18 @@ pub fn check_inferred(got: &[InferenceFact], expected: &[InferenceFact]) -> Trac
Ok(())
}

pub fn clarify_tvalues(values: &TVec<TValue>) -> TVec<TValue>{
values.iter().map(|t| {
t.to_scalar::<Opaque>()
.and_then(|ot| ot.clarify_to_tensor().transpose())
.ok()
.flatten()
.map(|ot| ot.into_tvalue())
.unwrap_or(t.clone())
}).collect::<TVec<_>>()
pub fn clarify_tvalues(values: &TVec<TValue>) -> TVec<TValue> {
values
.iter()
.map(|t| {
t.to_scalar::<Opaque>()
.and_then(|ot| ot.clarify_to_tensor().transpose())
.ok()
.flatten()
.map(|ot| ot.into_tvalue())
.unwrap_or(t.clone())
})
.collect::<TVec<_>>()
}

pub fn clarify_typed_fact<'a>(fact: impl Into<Cow<'a, TypedFact>>) -> Cow<'a, TypedFact> {
Expand All @@ -102,7 +111,7 @@ pub fn clarify_typed_fact<'a>(fact: impl Into<Cow<'a, TypedFact>>) -> Cow<'a, Ty
.as_ref()
.and_then(|it| it.clarify_dt_shape())
.map(|(dt, s)| Cow::Owned(TypedFact::dt_shape(dt, s)))
.unwrap_or_else(|| fact)
.unwrap_or_else(|| fact)
} else {
fact
}
Expand Down
2 changes: 2 additions & 0 deletions data/src/tensor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pub enum Approximation {
Approximate,
VeryApproximate,
SuperApproximate,
UltraApproximate,
}

impl From<bool> for Approximation {
Expand All @@ -52,6 +53,7 @@ impl Approximation {
(Approximate, _) => (1e-4, 5e-4),
(VeryApproximate, _) => (5e-2, 1e-2),
(SuperApproximate, _) => (1e-1, 5e-2),
(UltraApproximate, _) => (2e-1, 1e-1),
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion nnef/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,10 @@ impl tract_core::prelude::Framework<ProtoModel, TypedModel> for Nnef {
};
for entry in tar.entries()? {
let mut entry = entry?;
let path = entry.path()?.to_path_buf();
let mut path = entry.path()?.to_path_buf();
if path.starts_with("./") {
path = path.strip_prefix("./")?.to_path_buf();
}
read_stream(&path, &mut entry, &mut resources, self)?;
}
proto_model_from_resources(resources)
Expand Down

0 comments on commit 686e23c

Please sign in to comment.