o
    Xi"R                     @  s  d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
mZmZmZmZmZ d dlZd dlZd dlmZ eeZG dd dZe Zeejjeejjeejjeejjej ejj!ej"ejj!ej#ejj$ej%ejj$iZ&eejj'eejj(eejj)eejj(ej ejj*ej"ejj*ej#ejj+ej%ejj+iZ,dd ej-D d	d ej-D B d
d ej-D B Z.e	Z/ej0ddG dd dZ1ej0ddG dd dZ2ej0ddG dd dZ3d0ddZ4d1ddZ5d2d"d#Z6d3d%d&Z7d4d)d*Z8d5d,d-Z9ej0G d.d/ d/Z:dS )6    )annotationsN)AnyIteratorMappingOptionalSequenceTypeVarUnion)irc                   @  s   e Zd Zdd ZdS )_Emptyc                 C  s   dS )N_EMPTY_DEFAULT selfr   r   J/home/ubuntu/.local/lib/python3.10/site-packages/onnxscript/ir/_schemas.py__repr__   s   z_Empty.__repr__N)__name__
__module____qualname__r   r   r   r   r   r      s    r   c                 C     h | ]}t |qS r   r
   
TensorType.0dtyper   r   r   	<setcomp>7       r   c                 C     h | ]
}t t |qS r   )r
   SequenceTyper   r   r   r   r   r   8       c                 C  r   r   )r
   OptionalTyper   r   r   r   r   r   9   r   T)frozenc                   @  sb   e Zd ZU dZded< ded< dZded< dd
dZdddZeddddZ	eddddZ
dS )TypeConstraintParamzType constraint for a parameter.

    Attributes:
        name: Name of the parameter. E.g. "TFloat"
        allowed_types: Allowed types for the parameter.
    strnameset[ir.TypeProtocol]allowed_types descriptionreturnintc                 C  s   t | jt| jfS N)hashr$   tupler&   r   r   r   r   __hash__U   s   zTypeConstraintParam.__hash__c                 C  s&   d dd | jD }| j d| S )Nz | c                 s      | ]}t |V  qd S r+   r#   r   tr   r   r   	<genexpr>Y       z.TypeConstraintParam.__str__.<locals>.<genexpr>=)joinr&   r$   )r   allowed_types_strr   r   r   __str__X   s   zTypeConstraintParam.__str__c                 C  s   | |dd t jD |S )Nc                 S  r   r   r   r   r   r   r   r   ^   r   z1TypeConstraintParam.any_tensor.<locals>.<setcomp>)r
   DataTypeclsr$   r(   r   r   r   
any_tensor\   s   zTypeConstraintParam.any_tensorc                 C  s   | |t |S r+   )_ALL_VALUE_TYPESr:   r   r   r   	any_value`   s   zTypeConstraintParam.any_valueN)r)   r*   r)   r#   )r'   )r$   r#   r(   r#   r)   r"   )r   r   r   __doc____annotations__r(   r.   r8   classmethodr<   r>   r   r   r   r   r"   H   s   
 

r"   c                   @  sj   e Zd ZU dZded< ded< ded< ded< d	Zded
< dZded< eZded< dddZ	dddZ
dS )	Parameterz"A formal parameter of an operator.r#   r$   r"   type_constraintboolrequiredvariadicThomogeneous   r*   	min_arityr   defaultr)   c                 C  8   | j j}|  r| j d| d| j S | j d| S Nz: z = )rD   r$   has_defaultrK   r   type_strr   r   r   r8   r      zParameter.__str__c                 C  s
   | j tuS r+   )rK   r   r   r   r   r   rN   x      
zParameter.has_defaultNr?   r)   rE   )r   r   r   r@   rA   rH   rJ   r   rK   r8   rN   r   r   r   r   rC   e   s   
 
rC   c                   @  sJ   e Zd ZU dZded< ded< ded< dZd	ed
< dddZdddZdS )AttributeParameterzHA parameter in the function signature that represents an ONNX attribute.r#   r$   ir.AttributeTypetyperE   rF   Nzir.Attr | NonerK   r)   c                 C  rL   rM   )rV   r$   rN   rK   rO   r   r   r   r8      rQ   zAttributeParameter.__str__c                 C  s
   | j d uS r+   )rK   r   r   r   r   rN      rR   zAttributeParameter.has_defaultr?   rS   )r   r   r   r@   rA   rK   r8   rN   r   r   r   r   rT   |   s   
 
rT   rP   r#   r)   1ir.TensorType | ir.SequenceType | ir.OptionalTypec                 C  s   |  d}|d}tj|d   }ttjj}t|dd D ]*}|dkr.t|}q"|dkr8t|}q"|dkrBt	|}q"t
d| d	|  d
|S )zConverter a type_str from ONNX OpSchema to ir.TypeProtocol.

    A type str has the form of "tensor(float)" or composite type like "seq(tensor(float))".
    )(NtensorseqoptionalzUnknown type part: 'z' in type '')rstripsplitr
   r9   upperr   	UNDEFINEDreversedr   r    
ValueError)rP   striped
type_partsr   type_	type_partr   r   r   _get_type_from_str   s   
	
ri   param"onnx.defs.OpSchema.FormalParametertype_constraints!Mapping[str, TypeConstraintParam]c                 C  sb   | j |v r|| j  }nt| jt| j hd}t| j|| jtjjj	j
k| jtjjj	jk| j| jdS )z;Convert a formal parameter from ONNX OpSchema to Parameter.r$   r&   )r$   rD   rF   rG   rH   rJ   )rP   r"   r$   ri   rC   optiononnxdefsOpSchemaFormalParameterOptionr   Variadicis_homogeneousrJ   )rj   rl   rD   r   r   r   _convert_formal_parameter   s   

rv   rg   rV   rE   c                 C  sb   t | }|tu rtdt | v rdS |tu rdS ttdr/|tju r/tdt | v r/dS dS )z'Returns whether a type_ is an Optional.NT	UnionTypeF)	typing
get_originr	   rV   get_argsr   hasattrtypesrw   )rg   origin_typer   r   r   _is_optional   s   

r~   rU   c                 C  s   z?| t v r
t |  W S t| }|du rtjjW S |tjjttj	t
tjtfv r6t| d }|tv r;t| W S W tjjS W tjjS  tyS   tjd| dd Y tjjS w )z5Obtain the type of the attribute from a Python class.Nr   zTypeError when checking %s.T)exc_info)_PY_TYPE_TO_ATTR_TYPErx   ry   r
   AttributeTyperb   collectionsabcr   ListlistTupler-   rz   _LIST_TYPE_TO_ATTR_TYPE	TypeErrorloggerwarning)rg   r}   
inner_typer   r   r   get_attr_type   s2   



r   TypeAnnotationValue
str | Nonec                 C  s   t | tr| jS t| r*t| }|D ]}|tdu rqt|}|r&|  S d  S t| }t |trMt	|t
rMt| }t|d }|rKd| S dS dS )a  Returns the name of the type constraint for a given type annotation.

    Args:
        type_: A Python type.

    Returns:
        The name of the type constraint if it is a TypeVar.
        - Prefixes the name with "Sequence_" if the type annotation is a Sequence[].
    Nr   	Sequence_)
isinstancer   r   r~   rx   rz   rV   _get_type_constraint_namery   
issubclassr   )rg   subtypessubtypetype_param_namer}   r   r   r   r      s   




r   r%   c                 C  sd  | t jju rdd tjD S t| tr=t }| j }r)|D ]	}|	t
| q|S | j}|du r4t}|S |	t
| |S t| drLtt| jhS t| rmt }t| }|D ]}|tdu rcqZ|	t
| qZ|S t| }|tu rt }t| }|D ]}|tdusJ d|	t
| q|S t|trt|trt| }dd t
|d D S tS )z0Obtain the allowed types from a type annotation.c                 S  r   r   r   r   r   r   r   r     r   z:_get_allowed_types_from_type_annotation.<locals>.<setcomp>Nr   zIUnion should not contain None type because it is handled by _is_optional.c                 S  r   r   )r
   r   r1   r   r   r   r   <      
r   )
onnxscript
onnx_typesr   r
   r9   r   r   set__constraints__update'_get_allowed_types_from_type_annotation	__bound__r=   r{   r   r~   rx   rz   rV   ry   r	   r   r   )rg   r&   constraints
constraintboundr   r   r}   r   r   r   r     sP   







r   c                   @  s   e Zd ZU dZded< ded< ded< ded< ded	< ejd
d
dZded< dZded< dd Z	d-ddZ
d.ddZd/ddZd0ddZed1dd Zed2d"d#Zed3d&d'Ze	(	)	d4d5d+d,Zd(S )6OpSignaturea  Schema for an operator.

    Attributes:
        domain: Domain of the operator. E.g. "".
        name: Name of the operator. E.g. "Add".
        overload: Overload name of the operator.
        params: Input parameters. When the op is an ONNX function definition,
          the order is according to the function signature. This mean we can
          interleave ONNX inputs and ONNX attributes in the list.
        outputs: Output parameters.
    r#   domainr$   overloadz(Sequence[Parameter | AttributeParameter]paramsSequence[Parameter]outputsF)initreprz,Mapping[str, Parameter | AttributeParameter]
params_maprI   r*   since_versionc                 C  s   dd | j D | _d S )Nc                 S  s   i | ]}|j |qS r   )r$   r   rj   r   r   r   
<dictcomp>]  s    z-OpSignature.__post_init__.<locals>.<dictcomp>)r   r   r   r   r   r   __post_init__\  s   zOpSignature.__post_init__r)   Parameter | AttributeParameterc                 C  s
   | j | S r+   r   r   r$   r   r   r   get_  rR   zOpSignature.getrE   c                 C  s
   || j v S r+   r   r   r   r   r   __contains__b  rR   zOpSignature.__contains__(Iterator[Parameter | AttributeParameter]c                 C  s
   t | jS r+   )iterr   r   r   r   r   __iter__e  rR   zOpSignature.__iter__c              
   C  s   | j pd}| jrd| j nd}ddd | jD }ddd | jD }i }| jD ]}t|tr9|j||jj< q+| jD ]	}|j||jj< q=ddd |	 D }| d| j | d	| d
| d| 
S )Nz''z::r'   z, c                 s  r/   r+   r0   r   r   r   r   r3   l  r4   z&OpSignature.__str__.<locals>.<genexpr>c                 s  s    | ]	}t |jjV  qd S r+   )r#   rD   r$   r   r   r   r   r3   m  s    c                 s  r/   r+   r0   )r   rD   r   r   r   r3   t  s    
rY   z) -> (z) where )
r   r   r6   r   r   r   rC   rD   r$   values)r   r   r   r   r   rl   rj   type_constraints_strr   r   r   r8   h  s   




&zOpSignature.__str__c                 C     dd | j D S )zReturns the input parameters.c                 S     g | ]	}t |tr|qS r   )r   rC   r   r   r   r   
<listcomp>|      z&OpSignature.inputs.<locals>.<listcomp>r   r   r   r   r   inputsy     zOpSignature.inputsSequence[AttributeParameter]c                 C  r   )z!Returns the attribute parameters.c                 S  r   r   )r   rT   r   r   r   r   r     r   z*OpSignature.attributes.<locals>.<listcomp>r   r   r   r   r   
attributes~  r   zOpSignature.attributes	op_schemaonnx.defs.OpSchemac              	     s   dd |j D   fdd|jD }|j D ])}|jdur%tj|jnd}|dur/|j|_|	t
|jt|j|j|d q fdd|jD }| |j|jd|||jd	S )
z-Produce an OpSignature from an ONNX OpSchema.c                 S  s.   i | ]}|j t|j d d |jD |jdqS )c                 S  s   h | ]}t |qS r   )ri   )r   rP   r   r   r   r     s    z8OpSignature.from_op_schema.<locals>.<dictcomp>.<setcomp>)r$   r&   r(   )type_param_strr"   allowed_type_strsr(   )r   r   r   r   r   r     s    z.OpSignature.from_op_schema.<locals>.<dictcomp>c                      g | ]}t | qS r   rv   r   rl   r   r   r     r   z.OpSignature.from_op_schema.<locals>.<listcomp>Nr$   rV   rF   rK   c                   r   r   r   r   r   r   r   r     r   r'   r   r$   r   r   r   r   )rl   r   r   r   default_valuer
   serdedeserialize_attributer$   appendrT   r   rV   rF   r   r   r   )r;   r   r   rj   default_attrr   r   r   r   from_op_schema  s@   



	zOpSignature.from_op_schemaNr'   r   c                 C  sB  t |}t|}g }i }	|j D ]}
|
j|vrNtd|
j| t	
d|
j }||	|
j< |t|
j||
jt jju dd|
jt jjurH|
jntd q||
j }t| }tjjkr|
jt jjurnt|
j||
j}nd}|t|
j||
jt jju |d qt|}|du rd|
j }||	v r|	| }nt	|t|d}||	|< |t|
j||
jt jju dd|
jt jjur|
jntd q|d	}g }|du rnDt|tu rt|}n|g}t|D ]/\}}t| }|	v r|	| }nd
| }t	|t|d}||	|< |t||dddtd q| ||p|j||||dS )z=Produce an OpSignature from a function using type annotation.zDMissing annotation for parameter '%s' from %s. Treating as an Input.T_FT)r$   rD   rF   rG   rH   rK   Nr   rn   r)   TReturnr   )inspect	signaturerx   get_type_hints
parametersr   r$   r   debugr"   r>   r   rC   rK   emptyr   r   r
   r   rb   AttrrT   r   r   r   ry   r-   rz   	enumerater   )r;   funcr   r$   r   r   py_signature
type_hintsr   rl   rj   rD   rg   	attr_typerK   type_constraint_namereturn_typer   return_typesireturn_type_ireturn_param_namer   r   r   from_function  s   










zOpSignature.from_function)r$   r#   r)   r   )r$   r#   r)   rE   )r)   r   r?   )r)   r   )r)   r   )r   r   r)   r   )Nr'   rI   )
r   r#   r$   r   r   r#   r   r*   r)   r   )r   r   r   r@   rA   dataclassesfieldr   r   r   r   r   r   r8   propertyr   r   rB   r   r   r   r   r   r   r   D  s6   
 



0r   )rP   r#   r)   rW   )rj   rk   rl   rm   r)   rC   )rg   rV   r)   rE   )rg   rV   r)   rU   )rg   r   r)   r   )rg   r   r)   r%   );
__future__r   collections.abcr   r   r   loggingr|   rx   r   r   r   r   r   r   r	   rp   r   r
   	getLoggerr   r   r   r   floatr   FLOATr*   INTr#   STRINGrE   TensorTENSORTensorProtocolGraphGRAPHGraphProtocolr   FLOATSINTSSTRINGSTENSORSGRAPHSr   r9   r=   r   	dataclassr"   rC   rT   ri   rv   r~   r   r   r   r   r   r   r   r   <module>   sj   $













 



8