Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optional second input for Squeeze (bad bad bad idea) #1155

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions onnx/src/ops/array/squeeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ pub fn squeeze(
#[derive(Debug, Clone, Hash)]
struct Squeeze13;



impl Expansion for Squeeze13 {
fn name(&self) -> Cow<str> {
"Squeeze13".into()
Expand All @@ -31,16 +29,29 @@ impl Expansion for Squeeze13 {
inputs: &'p [TensorProxy],
outputs: &'p [TensorProxy],
) -> InferenceResult {
check_input_arity(inputs, 2)?;
check_output_arity(outputs, 1)?;
s.equals(&outputs[0].datum_type, &inputs[0].datum_type)?;
s.given_2(&inputs[0].shape, &inputs[1].value, move |s, shape, axes| {
let axes =
axes.cast_to::<i64>()?.as_slice::<i64>()?.iter().map(|i| *i as isize).collect();
let op = tract_hir::ops::array::Squeeze::new(Some(axes));
let out_shape = op.output_shape(&shape)?;
s.equals(&outputs[0].shape, out_shape)
})
if inputs.len() == 2 {
s.given_2(&inputs[0].shape, &inputs[1].value, move |s, shape, axes| {
let axes =
axes.cast_to::<i64>()?.as_slice::<i64>()?.iter().map(|i| *i as isize).collect();
let op = tract_hir::ops::array::Squeeze::new(Some(axes));
let out_shape = op.output_shape(&shape)?;
s.equals(&outputs[0].shape, out_shape)
})
} else {
s.given(&inputs[0].shape, move |s, shape| {
let axes = shape
.iter()
.enumerate()
.filter(|(_, dim)| dim.is_one())
.map(|(pos, _)| pos as isize)
.collect();
let op = tract_hir::ops::array::Squeeze::new(Some(axes));
let out_shape = op.output_shape(&shape)?;
s.equals(&outputs[0].shape, out_shape)
})
}
}

fn wire(
Expand All @@ -49,14 +60,26 @@ impl Expansion for Squeeze13 {
model: &mut TypedModel,
inputs: &[OutletId],
) -> TractResult<TVec<OutletId>> {
if let Some(axes) = model.outlet_fact(inputs[1])?.konst.as_ref() {
let axes =
axes.cast_to::<i64>()?.as_slice::<i64>()?.iter().map(|i| *i as isize).collect();
if inputs.len() == 2 {
if let Some(axes) = model.outlet_fact(inputs[1])?.konst.as_ref() {
let axes =
axes.cast_to::<i64>()?.as_slice::<i64>()?.iter().map(|i| *i as isize).collect();
let op = tract_hir::ops::array::Squeeze::new(Some(axes));
op.wire(prefix, model, &inputs[0..1])
} else {
bail!("Need axes to be a constant")
}
} else {
let axes = model
.outlet_fact(inputs[0])?
.shape
.iter()
.enumerate()
.filter(|(_, dim)| dim.is_one())
.map(|(pos, _)| pos as isize)
.collect();
let op = tract_hir::ops::array::Squeeze::new(Some(axes));
op.wire(prefix, model, &inputs[0..1])
} else {
bail!("Need axes to be a constant")
}
}

}