o
    XiV)                     @  s   d dl mZ g dZd dlZd dlZd dlmZmZmZ d dl	m
Z
 d dlZd dlmZmZmZmZ G dd dZe Zejd	d
 ZejddG dd dZejddG dd dZejddG dd dZd!ddZd"ddZejG dd  d ZdS )#    )annotations)OpSignature	ParameterAttributeParameterTypeConstraintParamN)IteratorMappingSequence)Any)_core_enums
_protocolsserdec                   @  s   e Zd ZdddZdS )_Emptyreturnstrc                 C     dS )N_EMPTY_DEFAULT selfr   r   C/home/ubuntu/.local/lib/python3.10/site-packages/onnx_ir/schemas.py__repr__      z_Empty.__repr__Nr   r   )__name__
__module____qualname__r   r   r   r   r   r      s    r   c                   C  s4   t dd tjD dd tjD B dd tjD B S )Nc                 S  s   h | ]}t |qS r   r   
TensorType.0dtyper   r   r   	<setcomp>"   s    z#_all_value_types.<locals>.<setcomp>c                 S     h | ]
}t t |qS r   )r   SequenceTyper   r    r   r   r   r#   #       c                 S  r$   r   )r   OptionalTyper   r    r   r   r   r#   $   r&   	frozensetr   DataTyper   r   r   r   _all_value_types   s   r+   T)frozenc                   @  s`   e Zd ZU dZded< ded< dZded< dd	 ZdddZeddddZ	eddddZ
dS )r   zType constraint for a parameter.

    Attributes:
        name: Name of the parameter. E.g. "TFloat"
        allowed_types: Allowed types for the parameter.
        description: Human-readable description of the type constraint.
    r   namez"frozenset[_protocols.TypeProtocol]allowed_types descriptionc                 C  s@   | j std| j dt| j tst| dt| j  d S d S )NzType constraint 'z&' must have at least one allowed type.r.   )r.   
ValueErrorr-   
isinstancer)   object__setattr__r   r   r   r   __post_init__6   s   z!TypeConstraintParam.__post_init__r   c                 C  s&   d dd | jD }| j d| S )Nz | c                 s      | ]}t |V  qd S Nr   )r!   tr   r   r   	<genexpr>@       z.TypeConstraintParam.__str__.<locals>.<genexpr>=)joinr.   r-   )r   allowed_types_strr   r   r   __str__?   s   zTypeConstraintParam.__str__c                 C  s   | |t dd tjD |S )Nc                 s  s    | ]}t |V  qd S r7   r   r    r   r   r   r:   F   s    z1TypeConstraintParam.any_tensor.<locals>.<genexpr>r(   clsr-   r0   r   r   r   
any_tensorC   s   zTypeConstraintParam.any_tensorc                 C  s   | |t  |S r7   )r+   r@   r   r   r   	any_valueI   s   zTypeConstraintParam.any_valueNr   )r/   )r-   r   r0   r   r   r   )r   r   r   __doc____annotations__r0   r5   r?   classmethodrB   rC   r   r   r   r   r   (   s   
 
	r   c                   @  s~   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ddZdddZdS )r   z"A formal parameter of an operator.r   r-   r   type_constraintboolrequiredvariadicThomogeneous   int	min_arityr
   defaultr   c                 C  8   | j j}|  r| j d| d| j S | j d| S Nz: z = )rG   r-   has_defaultrO   r   type_strr   r   r   r?   [      zParameter.__str__c                 C  s
   | j tuS r7   )rO   r   r   r   r   r   rR   a      
zParameter.has_defaultc                 C  r   )z_This parameter is an ONNX input or output parameter, as opposed to an ONNX attribute parameter.Tr   r   r   r   r   is_paramd      zParameter.is_paramc                 C  r   NFr   r   r   r   r   is_attributeh   r   zParameter.is_attributeNr   r   rH   )r   r   r   rD   rE   rK   rN   r   rO   r?   rR   rW   rZ   r   r   r   r   r   N   s   
 


r   c                   @  s^   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ddZdddZ	dS )r   zHA parameter in the function signature that represents an ONNX attribute.r   r-   z_enums.AttributeTypetyperH   rI   Nz_core.Attr | NonerO   r   c                 C  rP   rQ   )r\   r-   rR   rO   rS   r   r   r   r?   u   rU   zAttributeParameter.__str__c                 C  s
   | j d uS r7   )rO   r   r   r   r   rR   {   rV   zAttributeParameter.has_defaultc                 C  r   rY   r   r   r   r   r   rW   ~   r   zAttributeParameter.is_paramc                 C  r   )z_This parameter is an ONNX attribute parameter, as opposed to an ONNX input or output parameter.Tr   r   r   r   r   rZ      rX   zAttributeParameter.is_attributer   r[   )
r   r   r   rD   rE   rO   r?   rR   rW   rZ   r   r   r   r   r   l   s   
 


r   rT   r   r   :_core.TensorType | _core.SequenceType | _core.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 )zConvert a type_str from ONNX OpSchema to _protocols.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   r*   upperr   r   	UNDEFINEDreversedr%   r'   r1   )rT   stripped
type_partsr"   type_	type_partr   r   r   _get_type_from_str   s   
	
rn   param"onnx.defs.OpSchema.FormalParametertype_constraints!Mapping[str, TypeConstraintParam]c                 C  sf   | j |v r|| j  }nt| jtt| j f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-   rG   rI   rJ   rK   rN   )rT   r   r-   r)   rn   r   optiononnxdefsOpSchemaFormalParameterOptionOptionalVariadicis_homogeneousrN   )ro   rq   rG   r   r   r   _convert_formal_parameter   s   
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dZ
d,ddZd-ddZd.dd Zed/d!d"Zed0d$d%Zed1d(d)ZdS )2r   a   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.
        since_version: The version of the operator set. E.g. 1.
    r   domainr-   overloadz(Sequence[Parameter | AttributeParameter]paramsSequence[Parameter]outputsF)initreprz,Mapping[str, Parameter | AttributeParameter]
params_maprL   rM   since_versionc                 C  sN   i }| j D ]}|j|v rtd|jd| jd| j|||j< q|| _d S )NzDuplicate parameter name z in OpSignature ::)r~   r-   r1   r|   r   )r   r   ro   r   r   r   r5      s   



zOpSignature.__post_init__NrO   %Parameter | AttributeParameter | Noner   c                 C  s   | j ||S r7   )r   get)r   r-   rO   r   r   r   r      s   zOpSignature.getrH   c                 C  s
   || j v S r7   )r   )r   r-   r   r   r   __contains__   rV   zOpSignature.__contains__(Iterator[Parameter | AttributeParameter]c                 C  s
   t | jS r7   )iterr~   r   r   r   r   __iter__   rV   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''r   r/   z, c                 s  r6   r7   r8   r!   ro   r   r   r   r:      r;   z&OpSignature.__str__.<locals>.<genexpr>c                 s  s    | ]	}t |jjV  qd S r7   )r   rG   r-   r   r   r   r   r:      s    c                 s  r6   r7   r8   )r!   rG   r   r   r   r:          
r_   z) -> (z) where )
r|   r}   r=   r~   r   r2   r   rG   r-   values)r   r|   r}   r~   r   rq   ro   type_constraints_strr   r   r   r?      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   )r2   r   r   r   r   r   
<listcomp>      z&OpSignature.inputs.<locals>.<listcomp>r~   r   r   r   r   inputs      zOpSignature.inputsSequence[AttributeParameter]c                 C  r   )z!Returns the attribute parameters.c                 S  r   r   )r2   r   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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  s2   i | ]}|j t|j td d |jD |jdqS )c                 s  r6   r7   )rn   )r!   rT   r   r   r   r:     r   z8OpSignature.from_op_schema.<locals>.<dictcomp>.<genexpr>)r-   r.   r0   )type_param_strr   r)   allowed_type_strsr0   )r!   
constraintr   r   r   
<dictcomp>  s    z.OpSignature.from_op_schema.<locals>.<dictcomp>c                      g | ]}t | qS r   r{   r   rq   r   r   r         
z.OpSignature.from_op_schema.<locals>.<listcomp>N)r-   r\   rI   rO   c                   r   r   r   r   r   r   r   r   ,  r   r/   )r|   r-   r}   r~   r   r   )rq   r   r   r   default_valuer   deserialize_attributer-   appendr   r   AttributeTyper\   rI   r   r|   r   )rA   r   r~   ro   default_attrr   r   r   r   from_op_schema  s@   



	zOpSignature.from_op_schemar7   )r-   r   rO   r   r   r   )r-   r   r   rH   )r   r   r   )r   r   )r   r   )r   r   r   r   )r   r   r   rD   rE   dataclassesfieldr   r   r5   r   r   r   r?   propertyr   r   rF   r   r   r   r   r   r      s.   
 


r   )rT   r   r   r]   )ro   rp   rq   rr   r   r   )
__future__r   __all__r   	functoolscollections.abcr   r   r	   typingr
   rt   onnx_irr   r   r   r   r   r   cacher+   	dataclassr   r   r   rn   r{   r   r   r   r   r   <module>   s,   


%


 