pub fn infer_shape_broadcasting(shapes: &[&ShapeFactoid]) -> TractResult<Option<ShapeFactoid>> {
    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: Option<TDim> = None;
        let mut unknown = 0;

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

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

        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(GenericFactoid::Any);
        } else if let Some(previous) = previous {
            output_shape.push(GenericFactoid::Only(previous.clone()));
        } else {
            output_shape.push(GenericFactoid::Only(1.into()));
        }
    }

    output_shape.reverse();

    Ok(Some(ShapeFactoid::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: &[&ShapeFactoid]) -> TractResult<Option<ShapeFactoid>> {
    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: Option<TDim> = None;
        let mut unknown = 0;
        for shape in shapes.iter() {
            let rank = shape.rank().concretize().unwrap() as usize;
            let shape: TVec<DimFact> = shape.dims().cloned().collect();
            if i >= rank {
                continue;
            }
            match &shape[rank - i - 1] {
                GenericFactoid::Any => unknown += 1,
                GenericFactoid::Only(ref d) if d.is_one() => (),
                GenericFactoid::Only(ref d) => {
                    if previous.is_some() && previous.as_ref() != Some(d) {
                        bail!(
                            "Invalid shape (broadcasting): {:?} is not compatible with {:?}.",
                            d,
                            previous
                        )
                    } else {
                        previous = Some(d.clone())
                    }
                }
            };
        }
        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(GenericFactoid::Any);
        } else if let Some(previous) = previous {
            output_shape.push(GenericFactoid::Only(previous.clone()));
        } else {
            output_shape.push(GenericFactoid::Only(1.into()));
        }
    }
    output_shape.reverse();
    Ok(Some(ShapeFactoid::closed(output_shape)))
}
pub fn infer_shape_broadcasting ( shapes : & [ & ShapeFactoid ] ) -> TractResult < Option < ShapeFactoid > > { 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 : Option < TDim > = None ; let mut unknown = 0 ; for shape in shapes . iter ( ) { let rank = shape . rank ( ) . concretize ( ) . unwrap ( ) as usize ; let shape : TVec < DimFact > = shape . dims ( ) . cloned ( ) . collect ( ) ; if i >= rank { continue ; } match & shape [ rank - i - 1 ] { GenericFactoid :: Any => unknown += 1 , GenericFactoid :: Only ( ref d ) if d . is_one ( ) => ( ) , GenericFactoid :: Only ( ref d ) => { if previous . is_some ( ) && previous . as_ref ( ) != Some ( d ) { bail ! ( " " d previous ) } else { previous = Some ( d . clone ( ) ) } } } ; } 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 ( GenericFactoid :: Any ) ; } else if let Some ( previous ) = previous { output_shape . push ( GenericFactoid :: Only ( previous . clone ( ) ) ) ; } else { output_shape . push ( GenericFactoid :: Only ( 1 . into ( ) ) ) ; } } output_shape . reverse ( ) ; Ok ( Some ( ShapeFactoid :: closed ( output_shape ) ) ) }
Infers basic shape facts in the case of broadcasting operators.
Infers basic shape facts in the case of broadcasting operators .
shapes
&[&ShapeFactoid]
shapes
&[&ShapeFactoid]