pub fn infer_shape_broadcasting(shapes: &[&ShapeFact]) -> TractResult<Option<ShapeFact>> {
    if shapes.iter().any(|s| s.is_open()) {
        debug!("Can't infer shape for broadcasting operators when some inputs have an open shape.");
        return Ok(None);
    }

    let bound = shapes.iter().map(|s| s.rank().concretize().unwrap()).max().unwrap() as usize;

    let mut output_shape: TVec<DimFact> = tvec![];

    for i in 0..bound {
        let mut previous = None;
        let mut unknown = 0;

        for shape in shapes.iter() {
            let rank = shape.rank().concretize().unwrap() as usize;
            let shape: TVec<DimFact> = shape.dims().collect();
            if i >= rank {
                continue;
            }

            match shape[rank - i - 1] {
                GenericFact::Any => unknown += 1,
                GenericFact::Only(d) if d.is_one() => (),
                GenericFact::Only(d) => {
                    if previous.is_some() && previous != Some(d) {
                        bail!(
                            "Invalid shape (broadcasting): {:?} is not compatible with {:?}.",
                            d,
                            previous
                        )
                    } else {
                        previous = Some(d)
                    }
                }
            };
        }

        if unknown > 1 {
            debug!("Can't infer shape (broadcasting): there are multiple unknown values at same index.");
            return Ok(None);
        } else if unknown == 1 && previous != None {
            debug!("Can't infer shape (broadcasting): there are both unknown and known values at same index.");
            return Ok(None);
        } else if unknown == 1 && previous == None {
            output_shape.push(GenericFact::Any);
        } else if let Some(previous) = previous {
            output_shape.push(GenericFact::Only(previous));
        } else {
            output_shape.push(GenericFact::Only(1.into()));
        }
    }

    output_shape.reverse();

    Ok(Some(ShapeFact::closed(output_shape)))
}
/// Infers basic shape facts in the case of broadcasting operators.
Infers basic shape facts in the case of broadcasting operators.
Infers basic shape facts in the case of broadcasting operators .
pub fn infer_shape_broadcasting(shapes: &[&ShapeFact]) -> TractResult<Option<ShapeFact>> {
    if shapes.iter().any(|s| s.is_open()) {
        debug!("Can't infer shape for broadcasting operators when some inputs have an open shape.");
        return Ok(None);
    }
    let bound = shapes.iter().map(|s| s.rank().concretize().unwrap()).max().unwrap() as usize;
    let mut output_shape: TVec<DimFact> = tvec![];
    for i in 0..bound {
        let mut previous = None;
        let mut unknown = 0;
        for shape in shapes.iter() {
            let rank = shape.rank().concretize().unwrap() as usize;
            let shape: TVec<DimFact> = shape.dims().collect();
            if i >= rank {
                continue;
            }
            match shape[rank - i - 1] {
                GenericFact::Any => unknown += 1,
                GenericFact::Only(d) if d.is_one() => (),
                GenericFact::Only(d) => {
                    if previous.is_some() && previous != Some(d) {
                        bail!(
                            "Invalid shape (broadcasting): {:?} is not compatible with {:?}.",
                            d,
                            previous
                        )
                    } else {
                        previous = Some(d)
                    }
                }
            };
        }
        if unknown > 1 {
            debug!("Can't infer shape (broadcasting): there are multiple unknown values at same index.");
            return Ok(None);
        } else if unknown == 1 && previous != None {
            debug!("Can't infer shape (broadcasting): there are both unknown and known values at same index.");
            return Ok(None);
        } else if unknown == 1 && previous == None {
            output_shape.push(GenericFact::Any);
        } else if let Some(previous) = previous {
            output_shape.push(GenericFact::Only(previous));
        } else {
            output_shape.push(GenericFact::Only(1.into()));
        }
    }
    output_shape.reverse();
    Ok(Some(ShapeFact::closed(output_shape)))
}
pub fn infer_shape_broadcasting ( shapes : & [ & ShapeFact ] ) -> TractResult < Option < ShapeFact > > { if shapes . iter ( ) . any ( | s | s . is_open ( ) ) { debug ! ( " " ) ; return Ok ( None ) ; } let bound = shapes . iter ( ) . map ( | s | s . rank ( ) . concretize ( ) . unwrap ( ) ) . max ( ) . unwrap ( ) as usize ; let mut output_shape : TVec < DimFact > = tvec ! [ ] ; for i in 0 .. bound { let mut previous = None ; let mut unknown = 0 ; for shape in shapes . iter ( ) { let rank = shape . rank ( ) . concretize ( ) . unwrap ( ) as usize ; let shape : TVec < DimFact > = shape . dims ( ) . collect ( ) ; if i >= rank { continue ; } match shape [ rank - i - 1 ] { GenericFact :: Any => unknown += 1 , GenericFact :: Only ( d ) if d . is_one ( ) => ( ) , GenericFact :: Only ( d ) => { if previous . is_some ( ) && previous != Some ( d ) { bail ! ( " " d previous ) } else { previous = Some ( d ) } } } ; } if unknown > 1 { debug ! ( " " ) ; return Ok ( None ) ; } else if unknown == 1 && previous != None { debug ! ( " " ) ; return Ok ( None ) ; } else if unknown == 1 && previous == None { output_shape . push ( GenericFact :: Any ) ; } else if let Some ( previous ) = previous { output_shape . push ( GenericFact :: Only ( previous ) ) ; } else { output_shape . push ( GenericFact :: Only ( 1 . into ( ) ) ) ; } } output_shape . reverse ( ) ; Ok ( Some ( ShapeFact :: closed ( output_shape ) ) ) }
Infers basic shape facts in the case of broadcasting operators.
Infers basic shape facts in the case of broadcasting operators .
shapes
&[&ShapeFact]
shapes
&[&ShapeFact]