
    rh.              !          d Z ddlZddlmZ ddlmZmZ ddlZddlm	Z	 dZ
dZdZd	Zd
ZdZej                    G d d             Zej$                  j'                  ddd      ddddddeeej(                        dededeeeej.                  f      dee   dee   deeeef      dee   dee   dee   dee   dee   d ed!ee   d"ej(                  fd#       Zej6                  ddddddeej(                     dededeeeej.                  f      dee   dee   deeeef      dee   dee   dee   dee   dee   d ed!ee   d"ej(                  fd$       Zej$                  j'                  d%dd&      ddddddeeej(                        ded'ee   d(eeeeej.                  f         dee   dee   deeeef      dee   dee   dee   dee   dee   d ed!ee   d"eej(                     fd)       Zej6                  ddddddeej(                     ded'ee   d(eeeeej.                  f         dee   dee   deeeef      dee   dee   dee   dee   dee   d ed!ee   d"eej(                     fd*       Zy)+a4  Implementation of symbolic FX ops to represent arbitrary ONNX ops.

This module provides a way to create symbolic FX operators that can represent
arbitrary ONNX operators.

The operators are called "symbolic" because they don't do any actual computation
but instead serve as placeholders in the computation graph.

Each implementation contains two parts: A "real" implementation that produce all
zeros based on the input shape and dtype, and a "fake" implementation that does more
or less the same thing but is required by the `torch.library.custom_op` interface.
    N)Sequence)OptionalUnion)_dtype_mappingsifsisfsssc                      e Zd ZU dZee   ed<   ee   ed<   eeeef      ed<   ee   ed<   ee	   ed<   ee   ed<   e
deeeee	eeee   ee	   ee   ee   f   f   d	d fd
       Zd	eeeee	eee   ee	   ee   f   f   fdZy)EncodedAttrsa  Class to encode attributes from dictionary into lists of FX compatible attributes.

    Since FX does not support dictionaries, we need to encode the attributes into
    lists. This class provides a way to encode and decode the attributes.

    Attributes:
        attr_keys: List of attribute keys.
        attr_types: List of attribute types. Values can be "i" (int), "f" (float),
            "s" (string), "is" (int sequence), "fs" (float sequence), or "ss" (string sequence).
        attr_pos: List of tuples representing the start and end positions of each
            attribute in the corresponding list.
        attr_ints: List of integer attributes.
        attr_floats: List of float attributes.
        attr_strs: List of string attributes.
    	attr_keys
attr_typesattr_pos	attr_intsattr_floats	attr_strsattrsreturnc           	      	    | g g g g g g       }t        |j                               D ]  \  }\  }}|j                  j                  |       t	        |t
              rpt        |j                        }|j                  j                  |       |j                  j                  ||dz   f       |j                  j                  t               t	        |t              rqt        |j                        }|j                  j                  |       |j                  j                  ||dz   f       |j                  j                  t               &t	        |t              rqt        |j                        }|j                  j                  |       |j                  j                  ||dz   f       |j                  j                  t                t	        |t"              rt        |      dk(  rt%        d|       t'        d |D              rt        |j                        }|j                  j)                  |D cg c]  }t        |       c}       |j                  j                  ||t        |      z   f       |j                  j                  t*               wt	        |d   t
              rt        |j                        }|j                  j)                  |D cg c]  }t        |       c}       |j                  j                  ||t        |      z   f       |j                  j                  t,               t	        |d   t              rt        |j                        }|j                  j)                  |D cg c]  }t        |       c}       |j                  j                  ||t        |      z   f       |j                  j                  t.               t%        d|       t%        d| dt1        |              t        |j                        t        |j                        k(  s3J d	t        |j                         d
t        |j                                t        |j                        t        |j                        k(  s3J dt        |j                         d
t        |j                                |S c c}w c c}w c c}w )N)r   r   r   r   r   r      r   zEmpty sequence for attribute c              3   <   K   | ]  }t        |t                y w)N)
isinstancefloat).0elems     p/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/onnx/ops/_symbolic_impl.py	<genexpr>z)EncodedAttrs.from_dict.<locals>.<genexpr>e   s     =4z$.=s   z(Unsupported sequence type for attribute zUnsupported attribute type for z: z5Mismatch between number of attribute keys and types: z != z9Mismatch between number of attribute keys and positions: )	enumerateitemsr   appendr   intlenr   r   r   	_INT_TYPEr   r   _FLOAT_TYPEstrr   _STRING_TYPEr   
ValueErroranyextend_FLOAT_SEQ_TYPE_INT_SEQ_TYPE_STRING_SEQ_TYPEtype)clsr   encodedr   kv	start_posr   s           r   	from_dictzEncodedAttrs.from_dict8   s   " 
 #5;;=1 &	SIAv1$$Q'!S! 1 12	!!((+  ''IM(BC"")))4Au% 3 34	##**1-  ''IM(BC""))+6As# 1 12	!!((+  ''IM(BC"")),7Ax(q6Q;$'DQC%HII=1== #G$7$7 8I''../Jd/JK$$++Y	CF8J,KL&&--o>!c* #G$5$5 6I%%,,A-FDc$i-FG$$++Y	CF8J,KL&&--m<!c* #G$5$5 6I%%,,A-FDc$i-FG$$++Y	CF8J,KL&&--.>?$'OPQs%STT #B1#RQy!QRRM&	SN 7$$%W-?-?)@@ 	
CCHYHYDZC[[_`cdkdvdv`w_xy	
@ 7$$%W-=-=)>> 	
GGL]L]H^G__cdghohxhxdycz{	
> / 0K
 .G
 .Gs   <S
 S
S
c                    i }t        | j                        D ][  \  }}| j                  |   }|t        k(  r#| j                  | j
                  |   d      ||<   B|t        k(  r#| j                  | j
                  |   d      ||<   n|t        k(  r#| j                  | j
                  |   d      ||<   |t        k(  r3| j                  | j
                  |   d   | j
                  |   d    ||<   |t        k(  r4| j                  | j
                  |   d   | j
                  |   d    ||<   |t        k(  r4| j                  | j
                  |   d   | j
                  |   d    ||<   Pt        d|        |S )zNConvert the encoded attributes back to a dictionary for creating an ONNX node.r   r   zUnsupported attribute type: )r    r   r   r%   r   r   r&   r   r(   r   r,   r-   r.   r)   )selfr   r   key	attr_types        r   to_dictzEncodedAttrs.to_dict   s   0  	  / 	MFAs*II%!^^DMM!,<Q,?@c
k)!--dmmA.>q.ABc
l*!^^DMM!,<Q,?@c
o-!--dmmA.>q.ADMMRSDTUVDWXc
m+!^^DMM!,<Q,?$--PQBRSTBUVc
..!^^DMM!,<Q,?$--PQBRSTBUVc
 #?	{!KLL	M      N)__name__
__module____qualname____doc__listr'   __annotations__tupler#   r   classmethoddictr   boolr   r5   r:    r;   r   r   r      s      CyS	5c?##CyeCyE 	

E 
E EN)	IKI	
	

)r;   r   zonnx_symbolic::_symbolicrF   a  (Tensor?[] inputs, str op_type, int onnx_dtype, *, SymInt[] shape, str[] attr_keys, str[] attr_types, int[][] attr_pos, int[] attr_ints, float[] attr_floats, str[] attr_strs, str[] metadata_props_keys, str[] metadata_props_values, str domain='', int? version=None) -> Tensor)mutates_argsschema )metadata_props_keysmetadata_props_valuesdomainversioninputsop_type
onnx_dtypeshaper   r   r   r   r   r   rJ   rK   rL   rM   r   c                    t        j                  t        j                  v fd       t        j                  |t        j                           S )Nc                  \      dt        t        j                  j                                S Nz3 is invalid as an ONNX data type. Valid values are r@   r   ONNX_DTYPE_TO_TORCH_DTYPEkeysrP   s   r   <lambda>z_symbolic.<locals>.<lambda>   I    :,QRVWf  XA  XA  XF  XF  XH  SI  RJ  K r;   dtypetorch_checkr   rV   zerosrN   rO   rP   rQ   r   r   r   r   r   r   rJ   rK   rL   rM   s     `           r   	_symbolicrb      sH    8 
LLo??? 	K ;;_>>zJ r;   c                    t        j                  t        j                  v fd       t        j                  |t        j                           S )Nc                  \      dt        t        j                  j                                S rT   rU   rX   s   r   rY   z_.<locals>.<lambda>   rZ   r;   r[   r]   ra   s     `           r   _re      sH    $ 
LLo??? 	K ;;_>>zJ r;   z"onnx_symbolic::_symbolic_multi_outa  (Tensor?[] inputs, str op_type, int[] onnx_dtypes, *, SymInt[][] shapes, str[] attr_keys, str[] attr_types, int[][] attr_pos, int[] attr_ints, float[] attr_floats, str[] attr_strs, str[] metadata_props_keys, str[] metadata_props_values, str domain='', int? version=None) -> Tensor[]onnx_dtypesshapesc                V   g }t        j                  t              t              k(  fd       t              D ]e  \  }t        j                  t        j
                  v fd       |j                  t        j                  |t        j
                                  g |S )Nc                  :    dt               dt                dS NzNumber of shapes (z$) must match number of ONNX dtypes ()r$   rf   rg   s   r   rY   z%_symbolic_multi_out.<locals>.<lambda>  %    $S[M1UVYZeVfUgghi r;   c                  \      dt        t        j                  j                                S rT   rU   rX   s   r   rY   z%_symbolic_multi_out.<locals>.<lambda>  I    zl"UVZ[j  \E  \E  \J  \J  \L  WM  VN  O r;   r[   r^   r_   r$   zipr   rV   r"   r`   rN   rO   rf   rg   r   r   r   r   r   r   rJ   rK   rL   rM   outputsrQ   rP   s     ``            @r   _symbolic_multi_outru      s    8 G	LLFs;''i !5 	
z/CCC O	
 	KK_FFzR	
	
 Nr;   c                V   g }t        j                  t              t              k(  fd       t              D ]e  \  }t        j                  t        j
                  v fd       |j                  t        j                  |t        j
                                  g |S )Nc                  :    dt               dt                dS rj   rl   rm   s   r   rY   z_.<locals>.<lambda>1  rn   r;   c                  \      dt        t        j                  j                                S rT   rU   rX   s   r   rY   z_.<locals>.<lambda>6  rp   r;   r[   rq   rs   s     ``            @r   re   re     s    $ G	LLFs;''i !5 
z/CCC O	
 	KK_FFzR	

 Nr;   )r?   dataclassescollections.abcr   typingr   r   r^   torch.onnx.opsr   r%   r&   r(   r-   r,   r.   	dataclassr   library	custom_opTensorr'   r#   SymIntrB   r   rb   register_fakere   r@   ru   rF   r;   r   <module>r      s    $ "  * 	  I I IX 		  
. *,+-!Xell+, 
 E#u||+,- }  uS#X' } % } "# $C=  c]  \\!
4  *,+-!U\\" 
 E#u||+,- }  uS#X' } % } "# $C=  c]  \\! 8 (		  
. *,+-! Xell+,   # 
 XeC$5678  }    uS#X'  }  %  }  "#  $C=    c]   
%,,! 
 F "" *,+-!"U\\""" #"
 XeC$5678" }" " uS#X'" }" %" }" "#" $C=" " c]"  
%,,!" #"r;   