§
    2�Åj·   ã                  ó    — d Z ddlmZ ddlmZmZ ddlZddlm	Z	 erddl
mZ ddlmZmZ  G d„ d	e¦  «        Z G d
„ d¦  «        Zdd„Zdd„ZdS )aE  Optional runtime validation of static shape annotations.

This module implements the runtime semantics described in
``docs/ShapeAnnotationSemantics.md``: each symbolic dimension name
(``dim_param``) occurring in a model is treated as existentially quantified
once per inference run. A :class:`SymbolBindings` instance is a partial
map from such names to the (non-negative integer) value they were first
observed to take during a run; every later occurrence of the same name is
checked against that first binding.

As a convenience, alongside shape checking this module also validates a
tensor-typed value's element type (``elem_type``) against its declared
``TensorProto.DataType``, since this is a simple, purely local check (it
does not involve symbolic dimensions or bindings) that ONNX's static
checker already performs, but which is otherwise easy to violate
undetected at runtime (e.g. a kernel producing the wrong dtype).

This module only supports today's single, global namespace of symbolic
dimension names (a name means the same thing everywhere it occurs in a
model, including inside nested subgraphs) and only validates tensor-typed
values. Locally scoped names (for example, a name local to one iteration of
a ``Loop`` body, or to one element of a ``Sequence``) are not addressed; see
``docs/ShapeAnnotationSemantics.md`` for context.
é    )Úannotations)ÚTYPE_CHECKINGÚAnyN)Útensor_dtype_to_np_dtype)ÚSequence)ÚTensorShapeProtoÚ	TypeProtoc                  ó   — e Zd ZdZdS )ÚShapeAnnotationErrorzYRaised when a value's actual runtime shape violates its declared static shape annotation.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    úXC:\Projects\Historical\venv\Lib\site-packages\onnx/reference/shape_annotation_checker.pyr   r   +   s   € € € € € ØcÐcÐcÐcr   r   c                  ó"   — e Zd ZdZdd„Zdd„ZdS )ÚSymbolBindingsa‹  Tracks the runtime value bound to each symbolic dimension name (``dim_param``) during one inference run.

    A single instance of this class should be used for exactly one call to
    :meth:`onnx.reference.ReferenceEvaluator.run`, since a symbolic
    dimension name is existentially quantified once per run: different runs
    of the same model may bind the same name to different values.
    ÚreturnÚNonec                ó   — i | _         d S )N)Ú	_bindings)Úselfs    r   Ú__init__zSymbolBindings.__init__8   s   € Ø)+ˆŒˆˆr   ÚdimúTensorShapeProto.DimensionÚactualÚintÚcontextÚstrc          
     ó@  — |                      d¦  «        }|dk    r*|j        |k    rt          |› d|j        › d|› d�¦  «        ‚dS |dk    rP|j        }| j                             |¦  «        }|€|| j        |<   dS ||k    rt          |› d|›d	|› d
|› d�¦  «        ‚dS dS )aL  Checks a single dimension against an actual (runtime) axis size, binding it if unbound.

        Args:
            dim: the declared ``Dimension`` (``dim_value``, ``dim_param``, or neither).
            actual: the actual runtime size of the corresponding axis.
            context: a human-readable description of the value and axis being checked, used
                only for error messages.

        Raises:
            ShapeAnnotationError: if ``dim`` has a ``dim_value`` that disagrees with ``actual``,
                or a ``dim_param`` already bound to a different value.
        ÚvalueÚ	dim_valuez: declared dimension value z' does not match actual dimension value ú.Ú	dim_paramNz: symbolic dimension z was previously bound to z,, but this value has actual dimension value )Ú
WhichOneofr#   r   r%   r   Úget)r   r   r   r   ÚwhichÚnameÚbounds          r   Úcheck_and_bindzSymbolBindings.check_and_bind;   s  € ð —’˜wÑ'Ô'ˆØ�KÒÐØŒ} Ò&Ð&Ý*Øð 8ð 8¸3¼=ð 8ð 8Ø.4ð8ð 8ð 8ñô ð ð 'Ð&ð
 �kÒ!Ð!Ø”=ˆDØ”N×&Ò& tÑ,Ô,ˆEØˆ}Ø'-�”˜tÑ$Ð$Ð$Ø˜&’�Ý*Øð Kð K°Tð Kð KÐV[ð Kð KØAGðKð Kð Kñô ð ð "Ð!ð
 !�r   N)r   r   )r   r   r   r   r   r    r   r   )r   r   r   r   r   r+   r   r   r   r   r   /   sF   € € € € € ðð ð,ð ,ð ,ð ,ðð ð ð ð ð r   r   Úshape_protoúTensorShapeProto | NoneÚactual_shapeúSequence[int]Úbindingsr)   r    r   r   c                ób  — | €dS t          | j        ¦  «        }t          |¦  «        }||k    r)t          d|›d|› d|› dt          |¦  «        › d�	¦  «        ‚t	          t          | j        |d¬¦  «        ¦  «        D ]3\  }\  }}|                     |t          |¦  «        d|›d	|› �¬
¦  «         Œ4dS )a=  Checks an actual runtime tensor shape against a declared static shape annotation.

    This implements the per-value check described in
    ``docs/ShapeAnnotationSemantics.md``: a missing ``shape_proto`` (unknown rank)
    is always satisfied; a rank mismatch always fails; otherwise each axis is
    checked (and, for symbolic dimensions, bound) via ``bindings``.

    Args:
        shape_proto: the declared ``TensorShapeProto``, or ``None`` if the value's
            type declares no shape (unknown rank).
        actual_shape: the actual runtime shape of the value.
        bindings: the :class:`SymbolBindings` for the current inference run.
        name: the name of the value being checked, used only for error messages.

    Raises:
        ShapeAnnotationError: if the actual shape does not satisfy the annotation.
    NúValue z%: declared shape annotation has rank z , but the actual value has rank z (shape z).F)Ústrictz, axis )r   )Úlenr   r   ÚtupleÚ	enumerateÚzipr+   r   )	r,   r.   r0   r)   Údeclared_rankÚactual_rankÚaxisr   r   s	            r   Úcheck_shaper;   _   s  € ð. ÐØˆÝ˜œÑ(Ô(€MÝ�lÑ#Ô#€KØ˜Ò#Ð#Ý"ðZ�Tð Zð ZÀ-ð Zð ZØ-8ðZð ZÝBGÈÑBUÔBUðZð Zð Zñ
ô 
ð 	
õ  )ÝˆKŒO˜\°%Ð8Ñ8Ô8ñ ô  ð 
ð 
Ñˆ‰mˆs�Fð 	×ÒØ•�V‘”Ð&D¨tÐ&DÐ&D¸dÐ&DÐ&Dð 	 ñ 	
ô 	
ð 	
ð 	
ð
ð 
r   Ú
type_protoúTypeProto | Noner"   r   c           	     óþ  — | �|                       d¦  «        sdS t          |dd¦  «        }|€dS | j        }|j        r�t          |dd¦  «        }|�}	 t	          |j        ¦  «        }n# t
          $ r d}Y nw xY w|t          j        t          ¦  «        k    ot          |dd¦  «        dk    }|�!||k    r|st          d|›d|› d	|› d
�¦  «        ‚|                      d¦  «        r|j
        nd}	t          |	|||¦  «         dS )a<  Checks a runtime value against its declared ``TypeProto``, if it declares a tensor shape.

    Only tensor-typed values with a ``.shape`` attribute (e.g. a NumPy array) are
    checked in this initial implementation; other value kinds (sequences, maps,
    optionals, or a missing/absent type) are silently skipped. See the module
    docstring for the scope of what this checks.

    Args:
        type_proto: the value's declared ``TypeProto``, or ``None`` if not known.
        value: the actual runtime value produced during execution.
        bindings: the :class:`SymbolBindings` for the current inference run.
        name: the name of the value being checked, used only for error messages.

    Raises:
        ShapeAnnotationError: if the value's shape or element type does not
            match its declared type.
    NÚtensor_typeÚshapeÚdtypeÚkindÚUr2   z: declared element type z$ does not match actual element type r$   )ÚHasFieldÚgetattrr?   Ú	elem_typer   ÚKeyErrorÚnprA   Úobjectr   r@   r;   )
r<   r"   r0   r)   r@   r?   Úactual_dtypeÚdeclared_dtypeÚstring_unicoder,   s
             r   Úcheck_value_against_typerM   ‡   sz  € ð. Ð ×!4Ò!4°]Ñ!CÔ!CÐØˆÝ�E˜7 DÑ)Ô)€EØ€}ð 	ˆØÔ(€KØÔð Ý˜u g¨tÑ4Ô4ˆØÐ#ð&Ý!9¸+Ô:OÑ!PÔ!P��øÝð &ð &ð &ð "&���ð&øøøð ¥"¤(­6Ñ"2Ô"2Ò2ð ?Ý˜L¨&°$Ñ7Ô7¸3Ò>ð ð
 Ð*Ø  NÒ2Ð2Ø&ð 3õ +ðA˜Tð Að A¸^ð Að AØ1=ðAð Að Añô ð ð (3×';Ò';¸GÑ'DÔ'DÐN�+Ô#Ð#È$€KÝ�˜U H¨dÑ3Ô3Ð3Ð3Ð3s   ÁA& Á&A5Á4A5)
r,   r-   r.   r/   r0   r   r)   r    r   r   )
r<   r=   r"   r   r0   r   r)   r    r   r   )r   Ú
__future__r   Útypingr   r   ÚnumpyrH   Úonnx.helperr   Úcollections.abcr   Úonnx.onnx_pbr   r	   ÚRuntimeErrorr   r   r;   rM   r   r   r   ú<module>rU      s  ððð ð2 #Ð "Ð "Ð "Ð "Ð "à %Ð %Ð %Ð %Ð %Ð %Ð %Ð %à Ð Ð Ð à 0Ð 0Ð 0Ð 0Ð 0Ð 0àð 9Ø(Ð(Ð(Ð(Ð(Ð(à8Ð8Ð8Ð8Ð8Ð8Ð8Ð8ðdð dð dð dð d˜<ñ dô dð dð+ð +ð +ð +ð +ñ +ô +ð +ð`%
ð %
ð %
ð %
ðP84ð 84ð 84ð 84ð 84ð 84r   