Ë
    [^(h17  ã                  ó�  — d Z ddlmZ ddlZddlZddlZddlmZmZ ddlm	Z	 ddl
Z
ddl
mZ ddlmZ ddlmZ  ej                   d	«      Zd
dhZej&                   G d„ d«      «       Zdddœ	 	 	 	 	 	 	 	 	 	 	 dd„Zddœ	 	 	 	 	 	 	 dd„Zd d„Z	 d!	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d"d„Zd„ Zd#d„Zd$d„Zd%d„Zd&d„Zd'd„Zd(d„Zd(d„Z d(d„Z!y))zFUtilities for manipulating the torch.Graph object and the torchscript.é    )ÚannotationsN)ÚIterableÚSequence)ÚAny)Ú_C)ÚGLOBALS)Úregistrationz^(.+)_(([ifstgz])|(ty))$ÚinplaceÚatenc                  óÎ   — e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   ded<    ej                  e¬«      Zded<   dd„Z	ddœ	 	 	 	 	 dd„Z
ddœdd„ZeZddœ	 	 	 dd„Zy) ÚGraphContexta  Extra context for symbolic functions with all methods from torch.Graph.

    NOTE: This class is not meant for external consumption. Please do not depend on
    it outside of torch.onnx as the interface may evolve.

    Attributes:
        graph: The _C.Graph being constructed.
        block: The current _C.Block being constructed.
        opset: The opset version.
        original_node: Current node that is being converted from.
        params_dict: Mapping from graph initializer name to IValue.
        env: Mapping from Torch domain graph Value to ONNX domain graph Value.
        values_in_env: Set of all values in env, for constant-time lookups.
        new_nodes: List that tracks all new nodes that are added (used to make
            sure metadata is propagated to all new nodes).
    z_C.GraphÚgraphz_C.BlockÚblockÚintÚopsetú_C.NodeÚoriginal_nodezdict[str, _C.IValue]Úparams_dictzdict[_C.Value, _C.Value]Úenvzset[_C.Value]Úvalues_in_env)Údefault_factoryzlist[_C.Node]Ú	new_nodesc                ó.   — t        | j                  |«      S ©N)Úgetattrr   )ÚselfÚnames     ú\/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/onnx/_internal/jit_utils.pyÚ__getattr__zGraphContext.__getattr__7   s   € Ü�t—z‘z 4Ó(Ð(ó    é   ©Úoutputsc               ó&   — t        | |g|¢­d|i|¤ŽS )a  Creates an ONNX operator "opname", taking "raw_args" as inputs and "kwargs" as attributes.

        The set of operators and the inputs/attributes they take
        is documented at https://github.com/onnx/onnx/blob/master/docs/Operators.md

        Args:
            opname: The ONNX operator name, e.g., `Abs` or `Add`, or an operator qualified
                with a namespace, e.g., `aten::add`.
            raw_args: The inputs to the operator; usually provided
                as arguments to the `symbolic` definition.
            outputs: The number of outputs this operator returns.
                By default an operator is assumed to return a single output.
                If `outputs` is greater than one, this functions returns a tuple
                of output `Value`, representing each output of the ONNX operator
                in order.
            kwargs: The attributes of the ONNX operator, whose keys are named
                according to the following convention: `alpha_f` indicates
                the `alpha` attribute with type `f`.  The valid type specifiers are
                `f` (float), `i` (int), `s` (string) or `t` (Tensor).  An attribute
                specified with type float accepts either a single float, or a
                list of floats (e.g., you would say `dims_i` for a `dims` attribute
                that takes a list of integers).

        Returns:
            The value representing the single output of this operator (see the `outputs`
            keyword argument for multi-return nodes).
        r#   )Ú_add_op)r   Úopnamer#   Úraw_argsÚkwargss        r   ÚopzGraphContext.op:   s!   € ôF �t˜VÐJ hÒJ¸ÐJÀ6ÑJÐJr    Ú )Úoverload_namec               ó4   —  | j                   dg|¢­||dœ|¤ŽS )z~Generates an ONNX ATen op node.

        This function is for backward compatibility with the old symbolic functions.
        z
aten::ATen)Ú
operator_sÚoverload_name_s)r)   )r   Úoperatorr+   Úargsr(   s        r   Úaten_opzGraphContext.aten_op_   s9   € ð
 ˆt�w‰wØð
àñ
ð  Ø)ñ	
ð
 ñ
ð 	
r    c               óÔ   — |j                   j                  › d|j                  › �}|j                   j                  } t	        j
                  ||«      |«       t        | |g|¢­d|i|¤ŽS )aÚ  Creates an ONNX operator from onnx-script function, taking "raw_args" as inputs and "kwargs" as attributes.

        onnx-script repository: https://github.com/microsoft/onnx-script

        Args:
            onnx_fn: ONNXFunction from onnx-script; An example can be found at
                https://github.com/microsoft/onnx-script#example
            raw_args: The inputs to the operator; usually provided
                as arguments to the `symbolic` definition.
            outputs: The number of outputs this operator returns.
                By default an operator is assumed to return a single output.
                If `outputs` is greater than one, this functions returns a tuple
                of output `Value`, representing each output of the ONNX operator
                in order.
            kwargs: The attributes of the ONNX operator, whose keys are named
                according to the following convention: `alpha_f` indicates
                the `alpha` attribute with type `f`.  The valid type specifiers are
                `f` (float), `i` (int), `s` (string) or `t` (Tensor).  An attribute
                specified with type float accepts either a single float, or a
                list of floats (e.g., you would say `dims_i` for a `dims` attribute
                that takes a list of integers).

        Returns:
            The value representing the single output of this operator (see the `outputs`
            keyword argument for multi-return nodes).
        ú::r#   )r   Údomainr   Úversionr	   Úcustom_onnx_symbolicr%   )r   Úonnx_fnr#   r'   r(   Úsymbolic_nameÚopset_versions          r   Úonnxscript_opzGraphContext.onnxscript_opp   sf   € ðF #Ÿ=™=×/Ñ/Ð0°°7·<±<°.ÐAˆØŸ™×-Ñ-ˆàGŒ×)Ñ)¨-¸ÓGÈÔPä�t˜]ÐQ¨XÒQ¸wÐQÈ&ÑQÐQr    N)r   ÚstrÚreturnr   )r&   r;   r'   útorch.Tensor | _C.Valuer#   r   )r/   r;   r+   r;   )r'   r=   r#   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú__annotations__ÚdataclassesÚfieldÚlistr   r   r)   r1   Úatr:   © r    r   r   r      s«   … ñð" ƒOØƒOØƒJØÓØ%Ó%Ø	!Ó!Ø Ó Ø0˜{×0Ñ0ÀÔF€Iˆ}ÓFó)ð ñ	#Kàð#Kð +ð#Kð ó	#KðJ BDõ 
ð 
€Bð ñ	(Rð +ð(Rð ô	(Rr    r   r!   )r#   Ún_blocksc               óJ  —  | j                   |g|¢­d|i|¤Ž}t        |t        «      r|d   j                  «       }n|j                  «       }g }t	        |«      D ]:  }	|j                  «       }
t        j                  | |
¬«      }|j                  |«       Œ< |t        |«      |fS )aP  Creates an ONNX operator "opname", taking inputs and attributes.

    Args:
        graph_context: The context for the current graph.
        opname: The ONNX operator name, e.g., `Abs` or `Add`, or an operator qualified
            with a namespace, e.g., `aten::add`.
        inputs: The inputs to the operator.
        outputs: The number of outputs this operator returns.
            By default an operator is assumed to return a single output.
            If `outputs` is greater than one, this functions returns a tuple
            of output `Value`, representing each output of the ONNX operator
            in order.
        n_blocks: The number of sub-blocks to create in the node.
        attributes: The attributes of the ONNX operator.

    Returns:
        A tuple of (output_values, new_contexts, node) where:
            output_values: One or more output value of this operator
                (see the `outputs` keyword argument for multi-return nodes).
            new_contexts: A tuple of new graph contexts for each sub-block.
            node: The node representing the operator.
    r#   r   )r   )
r)   Ú
isinstancer   ÚnodeÚrangeÚaddBlockrC   ÚreplaceÚappendÚtuple)Úgraph_contextr&   r#   rH   ÚinputsÚ
attributesÚoutput_valuesrK   Únew_contextsÚ_Ú	new_blockÚnew_contexts               r   Úadd_op_with_blocksrY   ›   s§   € ð> %�M×$Ñ$ VÐT¨fÒT¸gÐTÈÑT€MÜ�-¤Ô*Ø˜QÑ×$Ñ$Ó&‰à×!Ñ!Ó#ˆà€LÜ�8‹_ò )ˆØ—M‘M“Oˆ	ä!×)Ñ)¨-¸yÔIˆØ×Ñ˜KÕ(ð	)ð œ% Ó-¨tÐ3Ð3r    r"   c          
     ó¸  — |D �cg c]  }t        | |«      ‘Œ }}|j                  «       D ��ci c]  \  }}|€Œ	||“Œ }	}}d|vrd|z   }t        | j                  |||	| j                  | j
                  |t        j                  ¬«      }
| j                  j                  |
«       |dk(  r|
j                  «       S t        |
j                  «       «      S c c}w c c}}w )a@  Creates an ONNX operator "opname", taking "args" as inputs and attributes "kwargs".

    The set of operators and the inputs/attributes they take
    is documented at https://github.com/onnx/onnx/blob/master/docs/Operators.md

    This function is monkey-patched onto Graph.

    Args:
        graph_context: The Torch Graph or Block.
        opname: The ONNX operator name, e.g., `Abs` or `Add`, or an operator qualified
            with a namespace, e.g., `aten::add`.
        args: The inputs to the operator; usually provided
            as arguments to the `symbolic` definition.
        outputs: The number of outputs this operator returns.
            By default an operator is assumed to return a single output.
            If `outputs` is greater than one, this functions returns a tuple
            of output `Value`, representing each output of the ONNX operator
            in order.
        kwargs: The attributes of the ONNX operator, whose keys are named
            according to the following convention: `alpha_f` indicates
            the `alpha` attribute with type `f`.  The valid type specifiers are
            `f` (float), `i` (int), `s` (string) or `t` (Tensor).  An attribute
            specified with type float accepts either a single float, or a
            list of floats (e.g., you would say `dims_i` for a `dims` attribute
            that takes a list of integers).

    Returns:
        (Union[_C.Value, Tuple[_C.Value, ...]])
        The value representing the single output of this operator (see the `outputs`
        keyword argument for multi-return nodes).
    r3   zonnx::)r   r9   Ú	n_outputsÚshape_inferencer!   )Ú_const_if_tensorÚitemsÚ_create_noder   r   r   r   Úonnx_shape_inferencer   rO   ÚoutputrP   r#   )rQ   r&   r#   r0   r(   ÚargrR   ÚkÚvrS   rK   s              r   r%   r%   Ê   sÚ   € ðL ?CÖC°sÔ˜}¨cÕ2ÐC€FÐCð $*§<¡<£>×C™4˜1˜a°Q±]�!�Q‘$ÐC€JÑCà�6ÑØ˜FÑ"ˆäØ×ÑØØØØ!×-Ñ-Ø#×)Ñ)ØÜ×4Ñ4ô	€Dð ×Ñ×"Ñ" 4Ô(à�!‚|Ø�{‰{‹}ÐÜ�—‘“Ó Ð ùò- Dùó Ds   …C­
C¸Cc                ó^   — |€|S t        |t        j                  «      r|S t        | d|¬«      S )Nzonnx::Constant)Úvalue_z)rJ   r   ÚValuer%   )rQ   rb   s     r   r]   r]   	  s/   € Ø
€{Øˆ
Ü�#”r—x‘xÔ Øˆ
ä�=Ð"2¸CÔ@Ð@r    c                óH  — t        | t        j                  «      r'| }|j                  |||«      }	|j	                  |	«      }	nTt        | t        j
                  «      r:| }
|
j                  ||«      }	|dkD  r!t        d|«      D ]  }|	j                  «        Œ t        	j                  «       «      }t        |«      |k(  sJ ‚|j                  d«      }t        |j                  «       «      D ]  \  }}|t        v rŒt!        |	|||¬«       Œ |rt        j"                  |	||«       |	S )z:Creates an node 'domain_op', taking inputs and attributes.r!   zaten::)r   )rJ   r   ÚGraphÚcreateÚ
insertNodeÚBlockÚaddNoderL   Ú	addOutputrP   r#   ÚlenÚ
startswithÚsortedr^   Ú_SKIP_NODE_ATTRIBUTESÚ_add_attributeÚ(_jit_pass_onnx_node_shape_type_inference)Úgraph_or_blockÚ	domain_oprR   rS   r   r9   r[   r\   r   rK   r   rV   Únode_outputsr   ÚkeyÚvalues                   r   r_   r_     s  € ô �.¤"§(¡(Ô+ØˆØ�|‰|˜I v¨yÓ9ˆØ×Ñ Ó%‰Ü	�N¤B§H¡HÔ	-ØˆØ�}‰}˜Y¨Ó/ˆð �qŠ=Ü˜1˜iÓ(ò !�Ø—‘Õ ð!ô ˜Ÿ™›Ó(€LÜˆ|Ó 	Ò)Ð)Ð)à×Ñ Ó)€Dô ˜Z×-Ñ-Ó/Ó0ò 4‰
ˆˆUØÔ'Ñ'ØÜ�t˜S %¨dÖ3ð4ñ Ü
×3Ñ3°D¸+À}ÔUØ€Kr    c                ór   — t        | t        «      xr& t        | t        t        t        j
                  f«       S r   )rJ   r   r;   ÚbytesÚtorchÚTensor)ry   s    r   Ú_is_onnx_listr~   9  s2   € Ü�eœXÓ&ò ¬zØ””UœEŸL™LÐ)ó0ð ,ð r    c                ó6   — | j                  «       dk(  sJ ‚| d   S )z,Convert a scalar tensor into a Python value.r!   r   )Únumel©Úxs    r   Ú_scalarrƒ   ?  s   € à�7‰7‹9˜Š>Ðˆ>ØˆQ‰4€Kr    c                óÞ   — t         j                  |«      }|€t        d|› d�«      ‚|j                  d«      |j                  d«      }}t	        |«      r|dz  } t        | |› d�«      ||«      S )z7Initializes the right attribute based on type of value.zInvalid attribute specifier 'z<' names must be suffixed with type, e.g. 'dim_i' or 'dims_i'r!   é   ÚsrV   )Ú_ATTR_PATTERNÚmatchÚ
ValueErrorÚgroupr~   r   )rK   rx   ry   r   Úmr   Úkinds          r   rs   rs   E  s   € ä×Ñ˜CÓ €AØ€yÜØ+¨C¨5ð 1Cð Có
ð 	
ð —‘˜“˜QŸW™W Q›Zˆ$€DÜ�UÔØ�‰ˆà$Œ7�4˜D˜6 ˜Ó$ T¨5Ó1Ð1r    c                óx   — | j                  «       j                  t        j                  j	                  «       «      S r   )ÚtypeÚisSubtypeOfr   Ú
TensorTypeÚgetr�   s    r   Ú
_is_tensorr’   U  s&   € Ø�6‰6‹8×Ñ¤§¡× 1Ñ 1Ó 3Ó4Ð4r    c                óž   — t        | «      sy t        j                  t        j                  | j                  «       «      }|j                  «       S r   )r’   ÚtypingÚcastr   r�   rŽ   Údevice)ry   Útensor_types     r   Úget_device_from_valuer˜   Y  s6   € Ü�eÔØÜ—+‘+œbŸm™m¨U¯Z©Z«\Ó:€KØ×ÑÓÐr    c                ó€   — d| vrt        d| › d�«      ‚| j                  dd«      \  }}d|v rt        d| › d�«      ‚||fS )z(Parse node kind into domain and Op name.r3   zNode kind: z& is invalid. '::' is not in node kind.r!   z) is invalid. '::' should only apear once.)r‰   Úsplit)rŒ   r4   r&   s      r   Úparse_node_kindr›   `  sY   € à�4ÑÜ˜; t fÐ,RÐSÓTÐTØ—Z‘Z  aÓ(�N€FˆFØˆv�~Ü˜; t fÐ,UÐVÓWÐWØ�6ˆ>Ðr    c                ó   — | dk(  S )ú Check if the domain is official.r   rG   ©r4   s    r   Úis_atenrŸ   j  ó   € à�VÑÐr    c                ó   — | dk(  S )r�   ÚprimrG   rž   s    r   Úis_primr£   o  r    r    c                ó   — | dk(  S )r�   ÚonnxrG   rž   s    r   Úis_onnxr¦   t  r    r    )rQ   r   r&   r;   rR   ú_C.Valuer#   r   rH   r   r<   z-tuple[Any, tuple[GraphContext, ...], _C.Node])rQ   r   r&   r;   r0   r=   r#   r   )rQ   r   )T)ru   z_C.Graph | _C.Blockrv   r;   rR   r   rS   Údictr   r¨   r9   r   r[   r   r\   Úboolr<   r   )r‚   ztorch.Tensor)rK   r   rx   r;   ry   r   r   r©   )r‚   r§   r<   r©   )ry   r§   r<   ztorch.device | None)rŒ   r;   r<   ztuple[str, str])r4   r;   r<   r©   )"rA   Ú
__future__r   rC   Úrer”   Úcollections.abcr   r   r   r|   r   Útorch.onnx._globalsr   Útorch.onnx._internalr	   Úcompiler‡   rr   Ú	dataclassr   rY   r%   r]   r_   r~   rƒ   rs   r’   r˜   r›   rŸ   r£   r¦   rG   r    r   ú<module>r±      su  ðá Lõ
 #ã Û 	Û ß .Ý ã Ý Ý 'Ý -ð �—
‘
Ð5Ó6€Ø" FÐ+Ð ð ×Ñ÷~Rð ~Ró ð~RðJ Øñ,4Øð,4àð,4ð ð,4ð ð	,4ð
 ð,4ð 3ó,4ðf ñ	<!Øð<!àð<!ð #ð<!ð ó	<!ó~Að" !ð$Ø'ð$àð$ð ð$ð ð	$ð
 ð$ð ð$ð ð$ð ð$ð ó$òNóó2ó 5ó óóó
ô
r    