o
    s::j/                     @  sx  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mZm	Z	m
Z
mZmZ d dlZd dlZe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'e(dd ej)D dd ej)D B dd ej)D B Z*eZ+d#ddZ,d$ddZ-d%ddZ.d&ddZ/			d'd(d!d"Z0dS ))    )annotationsN)AnyOptionalSequenceTypeVarUnionc                 C     h | ]}t |qS  ir
TensorType.0dtyper	   r	   ]/home/nk/hobo-godmode/plappi-mvp/.venv/lib/python3.10/site-packages/onnxscript/ir/_schemas.py	<setcomp>-       r   c                 C     h | ]
}t t |qS r	   )r   SequenceTyper   r   r	   r	   r   r   .       c                 C  r   r	   )r   OptionalTyper   r   r	   r	   r   r   /   r   type_typereturnboolc                 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   r   get_argsr   hasattrtypesr   )r   origin_typer	   r	   r   _is_optional>   s   

r"   ir.AttributeTypec                 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_TYPEr   r   r   AttributeType	UNDEFINEDcollectionsabcr   ListlistTupletupler   _LIST_TYPE_TO_ATTR_TYPE	TypeErrorloggerwarning)r   r!   
inner_typer	   r	   r   get_attr_typeQ   s2   



r3   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   __name__r"   r   r   r   _get_type_constraint_namer   
issubclassr   )r   subtypessubtypetype_param_namer!   r	   r	   r   r9   i   s   




r9   frozenset[ir.TypeProtocol]c                 C  s  | t jju rtdd tjD S t| trHt }| j	 }r-|D ]	}|
t| qt|S | j}|du r=|
t t|S |
t| t|S t| drYttt| jhS t| r|t }t| }|D ]}|tdu rpqg|
t| qgt|S t| }|tu rt }t| }|D ]}|tdusJ d|
t| qt|S t|trt|trt| }tdd t|d D S tS )	z0Obtain the allowed types from a type annotation.c                 s  s    | ]}t |V  qd S )Nr
   r   r	   r	   r   	<genexpr>   s    z:_get_allowed_types_from_type_annotation.<locals>.<genexpr>Nr   zIUnion should not contain None type because it is handled by _is_optional.c                 S  r   r	   )r   r   )r   tr	   r	   r   r      r   z:_get_allowed_types_from_type_annotation.<locals>.<setcomp>r   )
onnxscript
onnx_typesr   	frozensetr   DataTyper7   r   set__constraints__update'_get_allowed_types_from_type_annotation	__bound___ALL_VALUE_TYPESr   r   r"   r   r   r   r   r   r:   r   )r   allowed_typesconstraints
constraintboundr;   r<   r!   r	   r	   r   rH      sP   







rH       domainstrnameoverloadsince_versionintir.schemas.OpSignaturec                 C  s~  t | }t| }g }i }|j D ]}	|	j|vrYtd|	j| t	j
jd|	j }
|
||	j< |	jt jjur>d|	jini }|t	j
jd|	j|
|	jt jju ddd| q||	j }t| }t	jjkr|	jt jjuryt	|	j||	j}nd}|t	j
j|	j||	jt jju |d qt|}|du rd|	j }||v r|| }
nt	j
j|t|d	}
|
||< |	jt jjurd|	jini }|t	j
jd|	j|
|	jt jju ddd| q|d
}g }|du rnHt|tu rt|}n|g}t|D ]3\}}t| }|v r|| }
nd| }t	j
j|t|d	}
|
||< |t	j
j||
dddd qt	j
j||p8| 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_defaultFT)rS   type_constraintrequiredvariadichomogeneousN)rS   r   r[   rY   )rS   rK   r   TReturn)rQ   rS   rT   paramsoutputsrU   r	   )inspect	signaturer   get_type_hints
parametersvaluesrS   r0   debugr   schemasTypeConstraintParam	any_valuerY   	Parameteremptyappendr3   r&   r'   AttrAttributeParameterr9   rH   getr   r-   r   	enumerateOpSignaturer8   )funcrQ   rS   rT   rU   py_signature
type_hintsr_   type_constraintsparamrZ   default_kwargsr   	attr_typerY   type_constraint_namereturn_typer`   return_typesireturn_type_ireturn_param_namer	   r	   r   op_signature_from_function   s   
	












r   )r   r   r   r   )r   r   r   r#   )r   r4   r   r5   )r   r4   r   r>   )NrO   rP   )
rQ   rR   rS   r5   rT   rR   rU   rV   r   rW   )1
__future__r   collections.abcr(   ra   loggingr    r   r   r   r   r   r   onnx_irr   rA   	getLoggerr8   r0   floatr&   FLOATrV   INTrR   STRINGr   TensorTENSORTensorProtocolGraphGRAPHGraphProtocolr%   FLOATSINTSSTRINGSTENSORSGRAPHSr.   rC   rD   rJ   r4   r"   r3   r9   rH   r   r	   r	   r	   r   <module>   sX   












;