
    sh17                       S r SSKJr  SSKrSSKrSSKrSSKJrJr  SSKJ	r	  SSK
r
SSK
Jr  SSKJr  SSKJr  \R                   " S	5      rS
S1r\R&                   " S S5      5       rSSS.           SS jjrSS.       SS jjrS S jr S!                 S"S jjrS rS#S jrS$S jrS%S jrS&S jrS'S jrS(S jrS(S jr S(S jr!g))zFUtilities for manipulating the torch.Graph object and the torchscript.    )annotationsN)IterableSequence)Any)_C)GLOBALS)registrationz^(.+)_(([ifstgz])|(ty))$inplaceatenc                      \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'   S\S'   S\S'   S\S'   \R                  " \S9r	S\S'   SS jr
SS.     SS jjrSS.S S jjr\rSS.   S!S jjrSrg)"GraphContext   a  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                .    [        U R                  U5      $ N)getattrr   )selfnames     r/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/onnx/_internal/jit_utils.py__getattr__GraphContext.__getattr__7   s    tzz4((       outputsc               $    [        X/UQ7SU0UD6$ )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   opGraphContext.op:   s    F tJhJJ6JJr"    )overload_namec               4    U R                   " S/UQ7UUS.UD6$ )znGenerates 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_opGraphContext.aten_op_   s7    
 ww

  )	

 
 	
r"   c                   UR                   R                   SUR                   3nUR                   R                  n[        R
                  " XV5      " U5        [        X/UQ7SU0UD6$ )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_opGraphContext.onnxscript_opp   s`    F #==//07<<.A--))-GPtQXQwQ&QQr"    N)r   strreturnr   )r(   rA   r)   torch.Tensor | _C.Valuer%   r   )r2   rA   r.   rA   )r)   rC   r%   r   )__name__
__module____qualname____firstlineno____doc____annotations__dataclassesfieldlistr   r    r+   r4   atr>   __static_attributes__r@   r"   r   r   r      s    " OOJ%%	!!  *00FI}F) 	#K#K +#K 	#KJ BD 
 
B 	(R +(R 	(R (Rr"   r   r#   )r%   n_blocksc               R   U R                   " U/UQ7SU0UD6n[        U[        5      (       a  US   R                  5       nOUR                  5       n/ n[	        U5       H8  n	UR                  5       n
[        R                  " X
S9nUR                  U5        M:     U[        U5      U4$ )a  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addBlockrJ   replaceappendtuple)graph_contextr(   r%   rO   inputs
attributesoutput_valuesrR   new_contexts_	new_blocknew_contexts               r   add_op_with_blocksr`      s    > "$$VTfTgTTM-**Q$$&!!#L8_MMO	!))-IK(	  %-t33r"   r$   c                  U Vs/ s H  n[        X5      PM     nnUR                  5        VVs0 s H  u  pxUc  M
  Xx_M     n	nnSU;  a  SU-   n[        U R                  UUU	U R                  U R
                  U[        R                  S9n
U R                  R                  U
5        US:X  a  U
R                  5       $ [        U
R                  5       5      $ s  snf s  snnf )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).
r7   zonnx::)r   r=   	n_outputsshape_inferencer#   )_const_if_tensoritems_create_noder   r   r   r   onnx_shape_inferencer   rV   outputrW   r%   )rX   r(   r%   r3   r*   argrY   kvrZ   rR   s              r   r'   r'      s    L ?CCds}2dFC $*<<>C>41Q$!$>JC6F"!--#))44	D ""4(!|{{}  - D Ds   C	CCc                d    Uc  U$ [        U[        R                  5      (       a  U$ [        U SUS9$ )Nzonnx::Constant)value_z)rQ   r   Valuer'   )rX   ri   s     r   rd   rd   	  s2    
{
#rxx  
="2C@@r"   c                h   [        U [        R                  5      (       a&  U nUR                  XU5      n	UR	                  U	5      n	O[[        U [        R
                  5      (       a<  U n
U
R                  X5      n	US:  a#  [        SU5       H  nU	R                  5         M     [        W	R                  5       5      n[        U5      U:X  d   eUR                  S5      n[        UR                  5       5       H  u  pU[        ;   a  M  [!        XXS9  M     U(       a  [        R"                  " XU5        U	$ )z:Creates an node 'domain_op', taking inputs and attributes.r#   zaten::)r   )rQ   r   Graphcreate
insertNodeBlockaddNoderS   	addOutputrW   r%   len
startswithsortedre   _SKIP_NODE_ATTRIBUTES_add_attribute(_jit_pass_onnx_node_shape_type_inference)graph_or_block	domain_oprY   rZ   r   r=   rb   rc   r   rR   r   r]   node_outputsr   keyvalues                   r   rf   rf     s    ."((++||Iy9%	NBHH	-	-}}Y/ q=1i(  ) (L|	))))D Z--/0
''t%3 1 
33D}UKr"   c                    [        U [        5      =(       a*    [        U [        [        [        R
                  45      (       + $ r   )rQ   r   rA   bytestorchTensor)r   s    r   _is_onnx_listr   9  s2    eX& zUELL)0 , r"   c                8    U R                  5       S:X  d   eU S   $ )z,Convert a scalar tensor into a Python value.r#   r   )numelxs    r   _scalarr   ?  s    779>>Q4Kr"   c                    [         R                  U5      nUc  [        SU S35      eUR                  S5      UR                  S5      pe[	        U5      (       a  US-  n[        X S35      " XR5      $ )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#      sr]   )_ATTR_PATTERNmatch
ValueErrorgroupr   r   )rR   r   r   r   mr   kinds          r   rz   rz   E  sz    C Ay+C5 1C C
 	
 QWWQZ$U46$T11r"   c                x    U R                  5       R                  [        R                  R	                  5       5      $ r   )typeisSubtypeOfr   
TensorTypegetr   s    r   
_is_tensorr   U  s&    668 1 1 344r"   c                    [        U 5      (       d  g [        R                  " [        R                  U R                  5       5      nUR                  5       $ r   )r   typingcastr   r   r   device)r   tensor_types     r   get_device_from_valuer   Y  s9    e++bmmUZZ\:Kr"   c                    SU ;  a  [        SU  S35      eU R                  SS5      u  pSU;   a  [        SU  S35      eX4$ )z(Parse node kind into domain and Op name.r7   zNode kind: z& is invalid. '::' is not in node kind.r#   z) is invalid. '::' should only apear once.)r   split)r   r8   r(   s      r   parse_node_kindr   `  sU    4;tf,RSTTZZa(NFv~;tf,UVWW>r"   c                    U S:H  $ ) Check if the domain is official.r   r@   r8   s    r   is_atenr   j      Vr"   c                    U S:H  $ )r   primr@   r   s    r   is_primr   o  r   r"   c                    U S:H  $ )r   onnxr@   r   s    r   is_onnxr   t  r   r"   )rX   r   r(   rA   rY   _C.Valuer%   r   rO   r   rB   z-tuple[Any, tuple[GraphContext, ...], _C.Node])rX   r   r(   rA   r3   rC   r%   r   )rX   r   )T)r|   z_C.Graph | _C.Blockr}   rA   rY   r   rZ   dictr   r   r=   r   rb   r   rc   boolrB   r   )r   ztorch.Tensor)rR   r   r   rA   r   r   r   r   )r   r   rB   r   )r   r   rB   ztorch.device | None)r   rA   rB   ztuple[str, str])r8   rA   rB   r   )"rH   
__future__r   rJ   rer   collections.abcr   r   r   r   r   torch.onnx._globalsr   torch.onnx._internalr	   compiler   ry   	dataclassr   r`   r'   rd   rf   r   r   rz   r   r   r   r   r   r   r@   r"   r   <module>r      ss   L
 #  	  .    ' - 

56"F+  ~R ~R ~RJ ,4,4,4 ,4 	,4
 ,4 3,4f 	<!<!<! #<! 	<!~A" !$'$$ $ 	$
 $ $ $ $ $N2 5 

r"   