.xshape.shape_builder_impl

class experimental_experiment.xshape.shape_builder_impl.BasicShapeBuilder(verbose: int = 0, opset: int | None = None)[source]

Implements a basic class doing shape inference in an ONNX model.

A couple of environment variables can be set to help debugging any issue.

  • ONNXSTOPSHAPE=<name>: raises an exception when name receives a shape.

  • ONNXSTOPTYPE=<name>: raises an exception when name receives a type.

  • ONNXDYNDIM=<name>: raises an exception when dimension name is used

  • ONNXCST=1: shows which constant is requested

  • ONNXSHAPECOMPUTE=1: raises an exception when a shape is missing

  • ONNXSTOPVALUESHAPE=<name>: more information in function dealing with shapes

add_dynamic_dimension(name: str)[source]

Adds a dynamic dimension.

get_constant(name: str, exc: bool = True, computed_value: bool = False, as_shape: bool = False, multiple_outputs: bool = False) ndarray | NodeProto[source]

The method returns the constant name. It is a tensor (numpy array) or a NodeProto which must be evaluated. If computed_value is True, the NodeProto is evaluated with the ReferenceEvaluator.

Parameters:
  • name – constant name

  • exc – raise an exception if anything is impossible to do

  • computed_value – compute the value if not a constant

  • as_shape – returns a tuple for a shape

  • multiple_outputs – allow multiple outputs

Returns:

value

get_debug_msg(limit: int = 1000) str[source]

Returns a string providing as much information as possible to help the developper understand why a conversion failed.

Parameters:

limit – limit the string if the model is big

Returns:

many pieces of informations about the on going conversion

get_rank(name: str) int[source]

Returns the rank of a result.

get_shape(name: str) Tuple[int | torch.SymInt | torch.SymFloat | float | str, ...][source]

Returns the shape of a result.

get_type(name: str) int[source]

Returns the type of a result.

has_rank(name: str) bool[source]

Tells if a result has a rank.

has_shape(name: str, full=False) bool[source]

Tells if a result has a shape. If full is True, it returns True if the shape exists and if it is a static shape with all dimensions > 0.

has_type(name: str) bool | int[source]

Tells if a result has a type. This should be always true.

is_constant(name: str) bool[source]

Tells if a result is a constant.

register_constraint_dimension(dim_name: str, value: Any)[source]

Registers a constraint on a dimension.

Parameters:
  • dim_name – dimension name

  • value – value to register

register_dynamic_objects_from_dim(dim: str)[source]

Registers all the dynamic objects required in a dimension.

run_model(model: ModelProto | GraphProto, functions: Dict[Tuple[str, str], FunctionProto] | None = None, exc: bool = False)[source]

Runs inference over a model or a graph.

run_node(node: NodeProto, exc: bool = False)[source]

Uses shapes availables in the ShapeBuilder to infer the output shapes and types.

run_value_info(info: ValueInfoProto, is_input: bool)[source]

Fills ShapeBuilder with information coming from an input or output.

set_constant(name: str, value: TensorProto | NodeProto) bool[source]

Tells if a result is a constant.

set_rank(name: str, value: int) bool[source]

Sets the rank for a result.

Parameters:
  • name – result name

  • value – rank

Returns:

True if there is no rank conflict

set_shape(name: str, shape: Tuple[int | torch.SymInt | torch.SymFloat | float | str, ...], exc: bool = False, **_kwargs)[source]

Sets the shape for a result. It is exists, it checks the new shape is equal to the existing one.

Parameters:
  • name – result name

  • shape – shape

  • exc – raise an exception if inconsistency

set_type(name: str, dtype: int, exc: bool = True) bool[source]

Sets the shape for a result. It is exists, it checks the new shape is equal to the existing one.

Parameters:
  • name – name

  • dtype – element type (an integer, ONNX), 0 (unknonw is a possible value)

  • exc – raises an exception

Returns:

returns True if there is no type conflict

set_value_shape(name: str, value: Any, equal_to: Tuple[str, str] | None = None)[source]

Sets the value for a shape result.

Parameters:
  • name – name

  • value – it cannot be empty

  • equal_to – if specified, the value is also equal to this value

A value can be a string (for an unknown shape, a tuple for a shape, an integer for a single scalar.

value_as_shape(name: str) bool[source]

Returns the value of a result if it is a shape.