
     TiM                       S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKJrJ	r	J
r
JrJrJrJrJr  S SKrS SKrS SKrS SKJr  S SKJrJrJr  S SKJr  S SKJr  S SKJr  \" S	5      r \" S
5      r!S S!S jjr"\RF                  RH                  RJ                  RL                  \'\RF                  RH                  RJ                  RP                  \)\RF                  RH                  RJ                  RT                  \+\RF                  RH                  RJ                  RX                  S\RF                  RH                  RJ                  RZ                  S\RF                  RH                  RJ                  R\                  S\RF                  RH                  RJ                  R^                  S\RF                  RH                  RJ                  R`                  \\'   \RF                  RH                  RJ                  Rb                  \\)   \RF                  RH                  RJ                  Rd                  \\+   \RF                  RH                  RJ                  Rf                  S\RF                  RH                  RJ                  Rh                  S\RF                  RH                  RJ                  Rj                  S\RF                  RH                  RJ                  Rl                  S0r7\8" 5       r9\Rt                  " \;5      r< " S S5      r=S"S jr>S#S jr?\R                   " S S\5      5       rA " S S\A5      rB\R                  " SSS9 " S S5      5       rD " S S\B\\!\ 4   5      rE " S S\B5      rF " S S5      rG " S S\G5      rHg)$    )annotationsN)AnyCallableClassVarGenericOptionalProtocolSequenceTypeVar)	ParamSpec)	ast_utils	irbuilder
sourceinfo)	converter)_schemas)ONNXType_R_Pc                   US:X  a  SnX4[         R                  R                  ;  a8  [        S [         R                  R                  R	                  5        5       5      $ [         R                  R                  X4   n[        US5      $ )z=Selects a suitable ONNX ir_version for a given opset version. ai.onnxc              3  @   #    U  H  u  pUS    S:X  d  M  Uv   M     g7f)r   r   N ).0kvs      Z/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnxscript/_internal/values.py	<genexpr>$select_ir_version.<locals>.<genexpr>,   s#      
C$!qtyGXAACs   	
   )onnxhelperOP_SET_ID_VERSION_MAPmaxitems)versiondomainrequired_min_versions      r   select_ir_versionr)   '   sw    | A AA 
++;;AAC
 
 	
  ;;<<V_M#R((    c                     ^  \ rS rSr% SrS\S'   S\S'   0 rS\S'   SU 4S	 jjrSSS
 jjrS r	S r
S rSS jrSS jrS rSS jrSrU =r$ )OpsetJ   a  Represents an ONNX Opset, which consists of a domain name, a version.

It also contains a set of operations. This represents an Opset defined
in the ONNX schema registry and the operations are retrieved from the
ONNX schema registry. It also stores function definitions created for
ops in the corresponding Opset.

Only a single instance of Opset is created for a given (domain, version) pair.
strr'   intr&   z,ClassVar[dict[tuple[type, str, int], Opset]]cachec                   > XU4nU R                   R                  U5      nU(       a  U$ [        TU ]  U 5      nXl        X%l        0 Ul        XPR                   U'   U$ N)r0   getsuper__new__r'   r&   function_defs)clsr'   r&   keyexistinginstance	__class__s         r   r5   Opset.__new__Y   sX    G$99==%O7?3' "!#!		#r*   c                    g r2   r   )selfr'   r&   s      r   __init__Opset.__init__e   s    r*   c                l    U R                   R                   SU R                  < SU R                  < S3$ )N(z, ))r;   __name__r'   r&   r>   s    r   __repr__Opset.__repr__i   s/    ..))*!DKK?"T\\<LANNr*   c                     [         R                  R                  XR                  U R                  5      n[        XU5      $ ! [         a     g f = fr2   )r!   defs
get_schemar&   r'   Op	Exception)r>   opnameschemas      r   __getitem__Opset.__getitem__l   sD    	YY))&,,LFdF++ 		s   ?A 
AAc                     [         R                  R                  XR                  U R                  5        g! [
         a     gf = f)NTF)r!   rI   rJ   r&   r'   rL   )r>   rM   s     r   __contains__Opset.__contains__s   s<    	II  t{{C   		s   47 
AAc                    U R                   $ r2   )r'   rE   s    r   __str__Opset.__str__{   s    {{r*   c                     [         R                  R                  XR                  U R                  5      n[        XU5      $ ! [         a  n[        SU S35      UeS nAff = f)Nz
Attribute z not found.)r!   rI   rJ   r&   r'   rK   rL   AttributeError)r>   attrrN   excs       r   __getattr__Opset.__getattr__~   s[    	JYY))$dkkJFd&)) 	J :dV;!?@cI	Js   ?A 
A!AA!c                4    XR                   UR                  '   g r2   )r6   name)r>   funs     r   add_function_defOpset.add_function_def   s    '*388$r*   c                t    [        U5      nU(       a%  US   c  UR                  5         U(       a  US   c  M  U$ )zTrims 'None' values from the end of the inputs list. This is used to support
omitting optional inputs when no more required inputs follow to prepare a valid call
against the Op. Used by the static opset code generator.
)listpop)r>   _inputs
input_lists       r   _prepare_inputsOpset._prepare_inputs   s7     &\
Z^3NN Z^3r*   r   )r'   r.   r&   r/   )NN)r'   zOptional[str]r&   zOptional[int]returnr.   )rY   r.   rl   rK   )rf   zonnx.defs.OpSchema)rD   
__module____qualname____firstlineno____doc____annotations__r0   r5   r?   rF   rO   rR   rU   r[   r`   ri   __static_attributes____classcell__r;   s   @r   r,   r,   J   sS     KL:<E7<
OJ+	 	r*   r,   c                    U R                   [        R                  R                  :X  a  [        $ [        R
                  R                  U 5      $ )z+Get the default value of an ONNX attribute.)typer!   AttributeProto	UNDEFINED_EmptyDefaultr"   get_attribute_value)
attr_protos    r   _get_attribute_valuer|      s5    $--777;;**:66r*   c                8    U R                   R                  S5      $ )Ntypeinfo)metar3   )vars    r   	_typeinfor      s    88<<
##r*   c                  T    \ rS rSrSr\SS j5       r\S	S j5       r\S
S j5       rSr	g)OpLike   z2A protocol for objects that have an ONNX OpSchema.c                    g r2   r   rE   s    r   r^   OpLike.name   s    r*   c                    g r2   r   rE   s    r   opsetOpLike.opset   s    !r*   c                    g r2   r   rE   s    r   op_signatureOpLike.op_signature   s    >Ar*   r   Nrk   rl   r,   rl   zOptional[_schemas.OpSignature])
rD   rm   rn   ro   rp   propertyr^   r   r   rr   r   r*   r   r   r      s/    < ! !A Ar*   r   c                      \ rS rSrSr S       SS jjrS r\SS j5       r\SS j5       r	\SS j5       r
\SS	 j5       r\R                  SS
 j5       rSrg)rK      a  Represents an ONNX op instance (for example, the MatMul op from ONNX opset version 13).

It belongs to a particular Opset and has a name.

Attributes:
    opset: The Opset that this op belongs to.
    name: The name of the op.
    op_schema: The ONNX OpSchema for the op.
Nc                    Xl         X l        U   Ub  X0l        O X   =nb  UR                  U l        OS U l        S U l        U R                  c  [
        R                  SUU5        g g )NzRAn OpSchema was not provided for Op '%s' and there is not one found in opset '%s'.)_opset_name
_op_schema	op_schema
_signatureloggerdebug)r>   r   r^   r   ops        r   r?   Op.__init__   sh     
 'OKb, llDO"DO:>??"LL8	 #r*   c                   SSK Jn  UR                  5       n[        US5      (       aB  U R                  c  [        SU R                   S35      eUR                  U R                  X5      $ UR                  5       R                  XU5      $ )Nr   	evaluatorevalzOpSchema not found for op 'z'.)	onnxscript._internalr   defaulthasattrr   
ValueErrorr^   r   eval_op)r>   argskwargsr   default_evaluators        r   __call__Op.__call__   sx    2%--/$f--~~% #>tyyk!LMM$))$..$GG  "**4v>>r*   c                    U R                   $ r2   )r   rE   s    r   r^   Op.name   s    zzr*   c                    U R                   $ r2   )r   rE   s    r   r   Op.opset   s    {{r*   c                    U R                   $ r2   )r   rE   s    r   r   Op.op_schema   s    r*   c                    U R                   b  U R                   $ U R                  c  g[        R                  R	                  U R                  5      U l         U R                   $ )!Returns the signature of this op.N)r   r   r   OpSignaturefrom_op_schemarE   s    r   r   Op.op_signature   sK     ??&??">>!"..==dnnMr*   c                    Xl         g r2   r   r>   values     r   r   r          r*   )r   r   r   r   r2   )r   r,   r^   r.   r   Optional[onnx.defs.OpSchema]rl   Nonerk   r   )rl   r   r   r   z_schemas.OpSignature)rD   rm   rn   ro   rp   r?   r   r   r^   r   r   r   setterrr   r   r*   r   rK   rK      s     RV"%2N	,
?       	 	    r*   rK   F)repreqc                  8    \ rS rSr% SrS\S'   S\S'   S\S'   S	rg
)OnnxClosure   zRRepresents a nested function used as a graph-valued attribute for an ONNX op call.irbuilder.IRFunctionfunction_irztypes.FrameTypeframer   functionr   N)rD   rm   rn   ro   rp   rq   rr   r   r*   r   r   r      s    \%% Mr*   r   c                     ^  \ rS rSrSr          SU 4S jjr\SS j5       r\R                  SS j5       rSS jr	SS jr
SS jrS	 r     S           SS
 jjrSrU =r$ )OnnxFunctioni	  a  Represents an ONNX op for which a function-body has been defined in onnxscript.

Attributes:
    opset: Opset the function belongs to.
    name: Name of the function.
    function: Python function.
    function_ir: Python code parsed as an :class:`irbuilder.IRFunction`.
    source: Source code used to generate the function.
    kwargs: Additional properties used to construct a ModelProto.
    op_schema: Generated ONNX OpSchema for this op.
c                  > U=(       d    [        UR                  S5      n[        TU ]  XR                  5        X l        X0l        UR                  U R                  R                  S'   X@l	        XPl
        [        R                  R                  U R
                  U R                  R                  U R                  S9U l        [        R                   " X5        SU l        g)a5  Constructs an OnnxFunction.

Args:
    opset: opset the function belongs to
    pyfun: python function
    irfun: python code parsed by class
        :class:`onnxscript.converter.Converter`
    source: source code used to generate the function
    kwargs: additional properties used to construct a ModelProto
   opset_versionr'   r^   FN)r,   r'   r4   r?   r^   r   r   r&   r   sourcer   r   r   from_functionr   	functoolsupdate_wrapper	traceable)r>   r   pyfunirfunr   r   r;   s         r   r?   OnnxFunction.__init__  s    $ /u||Q/

+ 16o."..<<MM$"2"2"9"9		 = 

 	  - r*   c                    U R                   $ r   r   rE   s    r   r   OnnxFunction.op_signature:       r*   c                    Xl         g r2   r   r   s     r   r   r   ?  r   r*   c                N    SSK Jn  UR                  5       R                  XU5      $ )z=Implements an eager-mode execution of an onnxscript function.r   r   )r   r   r   eval_function)r>   r   r   r   s       r   r   OnnxFunction.__call__C  s!    2  "00VDDr*   c                P    U R                   R                   SU R                  < S3$ NrB   rC   )r;   rD   r   rE   s    r   rF   OnnxFunction.__repr__I  s%    ..))*!DMM+<A>>r*   c                6    U R                   R                  5       $ )z7Converts the function into :class:`onnx.FunctionProto`.)r   to_function_protorE   s    r   r   OnnxFunction.to_function_protoL  s    1133r*   c                    U R                   R                  (       a6  [        S U R                   R                   5       5      (       a  [        S5      e0 U R                  EUEnU R
                  " S0 UD6$ )z4Converts the function into :class:`onnx.ModelProto`.c              3  <   #    U  H  oR                   S L v   M     g 7fr2   )r   )r   rY   s     r   r   .OnnxFunction.to_model_proto.<locals>.<genexpr>R  s      *
+A4JJ$+As   zBA function with required attributes cannot be exported as a model.r   )r   attrsanyr   r   _to_model_proto)r>   r   merged_kw_argss      r   to_model_protoOnnxFunction.to_model_protoP  sq    !!c *
+/+;+;+A+A*
 '
 '
 T  3DKK262##5n55r*   c                   U R                   R                  5       nUR                  5        Vs/ s H  oR                   PM     n	nU R                   R                  R	                  5       n
U
R
                  R                  5       nU	 Ha  nUR                  nUb#  X;  a  UR                  R                  SS5      X'   SU;  d  M=  SUR
                  ;   d  MO  UR
                  S   US'   Mc     SU;  a&  Ub  UO[        R                  R                  5       US'   SU;   a  UR                  S5      nO[        US   5      n[        R                   " XS9nU	 H  nXR"                  UR%                  5       '   M!     [        R&                  " U5      nUR(                  SS2	 UR(                  R+                  UR-                  5        VVs/ s H  u  nn[        R.                  " UUS9PM     snn5        UR                  nU(       ak  UR0                  R+                  UR-                  5        VVs/ s H4  u  nn[        R2                  R5                  UUR7                  5       5      PM6     snn5        Ub  UR8                   HD  nUR;                  S5      (       a  M  UR<                  R?                  UR7                  5       5        MF     UR@                   HD  nUR;                  S5      (       a  M  UR<                  R?                  UR7                  5       5        MF     UbI  [C        UR8                  U5       H/  u  nnUR<                  R?                  UR7                  5       5        M1     UbI  [C        UR@                  U5       H/  u  nnUR<                  R?                  UR7                  5       5        M1     UR-                  5        H  u  nn[E        UUU5        M     U$ s  snf s  snnf s  snnf )	aC  Converts this instance into a `onnx.ModelProto`.

Args:
    io_types: When specified, all the inputs/outputs of the model
        are set to be of this type.
    input_types: When specified, all the inputs of the model
        are set to be of the corresponding type in this list.
    output_types: When specified, all the outputs of the model
        are set to be of the corresponding type in this list.
    value_infos: A dictionary mapping intermediate variable names to ONNX types.
        Used to set value_info for intermediate variables.
    opset_version: The standard opset version to use for the model if it
        cannot be inferred. Otherwise defaults to the current opset version.
    kwargs: Additional parameters given to function :func:`onnx.helper.make_model`.

Returns:
    An instance of :class:`onnx.ModelProto`.
Nr   r   r   
ir_version)r   )r'   r&   rv   )#r   get_called_functionsvaluesgraphcloneopset_importscopyr'   r   r3   r!   rI   onnx_opset_versionre   r)   irModel	functions
identifierto_protoopset_importextendr%   OperatorSetIdProto
value_infor"   make_value_infoto_type_protoinputHasFieldrv   CopyFromoutputzipsetattr)r>   io_typesinput_typesoutput_typesvalue_infosr   r   sub_functionsfuncir_functions
main_graphr   r'   r   modelmodel_protor&   r   r^   rv   r   r  r   r   s                           r   r   OnnxFunction._to_model_protoa  sK   8 ((==?HUH\H\H^*_H^+;+;H^*_ %%++113
"00557 D[[F!f&A(,		oq(I%&21C1C+C$($6$6r$:b! ! ]" "/!:		@\@\@^ "
 6!L1J*=+<=J ; D15OODOO-. ! kk%( $$Q'  '' (5':':'<'<OFG ''vwG'<	
 !!## '2&7&7&9&9
d KK//d6H6H6JK&9 ~~f--JJ''(>(>(@A %  ,,v..KK(()?)?)AB ' ""5;;<t

##D$6$6$89  =# #ELL, ?$$T%7%7%9: !@ LLNDAqKA& # Q +`Ls   O"O
&;O 
)r   r   r   r   r   r   )
r   zOptional[Opset]r   r   r   r   r   r.   r   zdict[str, Any]r   r   )r   z_P.argsr   z	_P.kwargsrl   r   rk   )rl   zonnx.FunctionProto)NNNNN)r  zOptional[ONNXType]r  Optional[Sequence[ONNXType]]r  r  r	  zdict[str, ONNXType] | Noner   z
int | Nonerl   zonnx.ModelProto)rD   rm   rn   ro   rp   r?   r   r   r   r   rF   r   r   r   rr   rs   rt   s   @r   r   r   	  s    
"" " $	"
 " "H      E?46& (,485926$(e$e 2e 3	e
 0e "e 
e er*   r   c                     ^  \ rS rSrSrS
U 4S jjrS rS r\SS j5       r	\SS j5       r
\
R                  SS j5       r
S	rU =r$ )TracedOnnxFunctioni  zaTracedOnnxFunction.

Attributes:
    name: Name of the op. E.g. "aten::add".
    func: Function.
c                   > [         TU ]  XR                  5        X l        [        R
                  R                  U R                  SU R                  S9U l        [        R                  " X5        g )N_tracedr   )r4   r?   rD   r  r   r   r   r^   r   r   r   )r>   r   r  r;   s      r   r?   TracedOnnxFunction.__init__  sU    .	"..<<IIidii = 

 	  ,r*   c                &    U R                   " U0 UD6$ r2   )r  )r>   r   r   s      r   r   TracedOnnxFunction.__call__  s    yy$)&))r*   c                P    U R                   R                   SU R                  < S3$ r   )r;   rD   r  rE   s    r   rF   TracedOnnxFunction.__repr__  s$    ..))*!DII=::r*   c                   [         R                  " U R                  5      u  p[        R                  " U R                  5      n[        R
                  " U R                  5      nUR                  R                  5       nUR                  UR                  5        [        R                  " U R                  UUS9nUR                  U5      $ )zWReturn the function_ir.

This function IR contains only the signature of the function.
)r   global_namesr   )r   get_src_and_astr  inspect	getmodulegetclosurevars__dict__r   update	nonlocalsconverter_module	Converterr   translate_function_signature)r>   srcfunc_astmoduleclosurer  r   s          r   r   TracedOnnxFunction.function_ir  s     "11$))<""499-((3++-G--.$..++%
	 55h??r*   c                    U R                   $ r   r   rE   s    r   r   TracedOnnxFunction.op_signature  r   r*   c                    Xl         g r2   r   r   s     r   r   r-    r   r*   )r   r  )r   r,   r  r   )rl   r   r   r   )rD   rm   rn   ro   rp   r?   r   rF   r   r   r   r   rr   rs   rt   s   @r   r  r    s[    -*; @ @$      r*   r  c                  "    \ rS rSrSrSS jrSrg)SymbolValuei  a  Represents script-time value information about named variables used in a script.

At translation-time, the (local) variables of a script, including its parameters,
are bound to a SymbolValue.

SymbolValues fall into the following categories:

AttrRef: Function parameters of attribute-kind, also mapped to ONNX attributes

Dynamic: values computed at runtime (of tensor type, for now) mapped to NodeArgs.
Dynamic values include input-parameters of the script, as well intermediate
values computed in the script.

For example, consider the following script definition:
::

    @script()
    def ThresholdedRelu(X, alpha: float):
        zero = op.CastLike(0, X)
        return op.Where(X > alpha, X, zero)

Here, `X` has a Dynamic value, `alpha` has an AttrRef value, and `zero`
has a Dynamic value.

Scripts may also contain references to global variables, but the translator
does not associate a SymbolValue with them. The python value of global variables
is used directly in the translation, and such global variables are intended
to be used for limited purposes, namely:
* To identify an opset
* To represent constant-values, translated into ONNX constants.
c                    [        U[        R                  5      (       d  [        S[	        U5      < S35      eXl        X l        g)z
Initializes SymbolValue.

Arguments:
    value: The value bound to a python variable in a script.
    info: source-location information for error-messages/debugging
z/info must be of type sourceinfo.SourceInfo not .N)
isinstancer   
SourceInfo	TypeErrorrv   r   info)r>   r   r6  s      r   r?   SymbolValue.__init__  s<     $
 5 566MdSWj^[\]^^
	r*   )r6  r   N)r   r   r6  sourceinfo.SourceInforl   r   )rD   rm   rn   ro   rp   r?   rr   r   r*   r   r0  r0    s    @r*   r0  c                  ,   ^  \ rS rSrSU 4S jjrSrU =r$ )AttrRefi-  c                0   > [         TU ]  X5        X l        g)zInitializes AttrRef.

Arguments:
    attr: An ir.Attr representing the attribute-parameter
    as_bool: Whether the attribute is to be interpreted as a bool type (represented as int in ONNX)
    info: for debugging use.
N)r4   r?   as_bool)r>   rY   r<  r6  r;   s       r   r?   AttrRef.__init__.  s     	$r*   )r<  )rY   zir.Attrr<  boolr6  r8  rl   r   )rD   rm   rn   ro   r?   rr   rs   rt   s   @r   r:  r:  -  s    	 	r*   r:  )r   )r&   r/   r'   r.   rl   r/   )r{   zonnx.AttributeProtorl   r   )r   zir.Valuerl   r   )I
__future__r   dataclassesr   r  loggingtypestypingr   r   r   r   r   r	   r
   r   r!   	onnx.defsonnx_irr   typing_extensionsr   r   r   r   r   r   r$  onnxscript.irr   onnxscript.onnx_typesr   r   r   r)   rI   OpSchemaAttrTypeFLOATfloatINTr/   STRINGr.   TENSORGRAPHSPARSE_TENSOR
TYPE_PROTOFLOATSINTSSTRINGSTENSORSGRAPHSSPARSE_TENSORSTYPE_PROTOS_ATTRIBUTE_TYPE_TO_PYTHON_TYPEobjectry   	getLoggerrD   r   r,   r|   r   runtime_checkabler   rK   	dataclassr   r   r  r0  r:  r   r*   r   <module>r_     s   #      	 	 	    ' A A > " *T]t_	) 	II%%uII##SII&&II&&II%%tII--tII**DII&&II$$hsmII''#II''II&&II..II++T" $ 			8	$G GZ7$ 
BX 
B 
BG  G T Ee,  -}2wr2v }@2  2 j, ,^
k 
r*   