o
    Xi%                     @  s   d dl mZ d dlmZmZmZmZmZ d dlZ	d dl
Z
d dlmZmZ d dlmZ er2d dlmZ eejjejjejjejjejjejjejjhZ	d.d/ddZd0ddZdd Zd.ddZd1d!d"Z d2d$d%Z!d3d,d-Z"dS )4    )annotations)TYPE_CHECKINGAnyCallableOptionalSequenceN)irtensor)_schemas)	converterkeystrvaluer   name_generatorCallable[[], str]	attr_typeir.AttributeType | Nonereturnir.Attrc                 C  s   t |tr|s|du rtd|tvrtdtj| |g dS |tjjkr8t |tj	s8tj
| tj|| ddS tjj| ||dS )aj  Helper function to create an ONNX AttributeProto.

    * Empty lists can be attribute values, provided the attribute type is specified
    and is a list type.
    * Scalar-values like 1.0 as well as lists like [1, -1] to be specified
    when the attribute type is TensorProto by automatically converting the value
    into a 0-D or 1-D tensor respectively.
    Nz6Attribute type must be specified for empty list value.z>Empty list value is only allowed for repeated attribute types.)nametyper   )r   )r   r   )r   )
isinstancelist
ValueError_REPEATED_ATTRIBUTE_TYPESr   AttrAttributeTypeTENSORonnxTensorProto
AttrTensorr	   convenienceconvert_attribute)r   r   r   r    r#   Q/home/ubuntu/.local/lib/python3.10/site-packages/onnxscript/_internal/autocast.pypyvalue_to_onnx_attribute#   s   r%   boolc                 C  s2   t | tttfr
dS t | tr| rt| d S dS )zChecks if a runtime parameter value needs to be promoted into an onnxscript value.
    This is the runtime-equivalent of the promotion of literal constants into ONNX values
    in the static converter.
    Tr   F)r   r&   intfloatr   _promotablexr#   r#   r$   r)   B   s
   r)   c                 C  sd   t | trtjS t | trtjS t | trtjS t | tr)| r%t	| d S t
dtdt|  )zReturn np.dtype to use when converting a python value to an onnxscript tensor.
    Note that int constants are treated as int64, as that is the common type in ONNX
    for shape/index values.
    r   z+Cannot determine target type for empty listzValue of unexpected type )r   r&   npbool_r'   int64r(   float32r   
_get_dtyper   	TypeErrorr   )pyvaluer#   r#   r$   r0   P   s   



r0   c                 C  s0   t | r|du rt| }ttj| |dS | S )zPromotes python values into onnxscript tensors.
    The optional argument dtype specifies the desired np.dtype of the tensor,
    used only when a non-standard onnxscript-value is promoted into one.
    N)dtype)r)   r0   r	   Tensorr,   array)r2   r3   r#   r#   r$   cast_pyvalue_to_os_tensord   s
   r6   get_type_infoCallable[[Any], Any]castCallable[[Any, Any], Any]op_signature_schemas.OpSignature | Nonetuple[Any, ...]c                   s   |du rt  fdd|D S |j}i g }t|D ]N\}}|t|k r)|| }n$|d jr>|d }|js=||df qntdt| dt| d|jj	}	d|	vra| |}
|
dura|
|	< |||	f q fd	d
|D }t |S )a  Uses schema specification to support a limited form of auto-casting.

    * Scalars are promoted to tensors.
    * Further. they are cast to the required type when used in ops with other
    tensor inputs that are required to be of same type.
    Thus, in "A+1" or "Add(A, 1)", the value 1 will be converted to the same
    type as A.

    This is used by the converter in a static-mode, as well as by the eager-mode
    execution in a dynamic-mode.
    Nc                 3  s    | ]} |d V  qd S Nr#   ).0r+   )r9   r#   r$   	<genexpr>   s    zcast_inputs.<locals>.<genexpr>zNumber of actual parameters z% exceeds number of formal parameters .(c                   s    g | ]\}} | |qS r#   )get)r?   r+   typevarr9   type_bindingsr#   r$   
<listcomp>   s     zcast_inputs.<locals>.<listcomp>)
tupleinputs	enumeratelenvariadichomogeneousappendr   type_constraintr   )r7   r9   r;   argsexpected_inputsargs_typevarsir+   expectedrE   typeinfo	cast_argsr#   rF   r$   cast_inputsp   s6   

rX   _schemas.OpSignaturec                 C  s   dd }t |t| |S )z.Used for autocast during eager-mode execution.c                 S  s   t | tjr	| jS d S r>   )r   r	   r4   r3   r*   r#   r#   r$   r7      s   z*dynamic_cast_inputs.<locals>.get_type_info)rX   r6   )r;   rQ   r7   r#   r#   r$   dynamic_cast_inputs   s   rZ   
converter_converter.ConverterOptional[_schemas.OpSignature]rQ   Sequence[Optional[ir.Value]]tuple[str, ...]c                   s*   d fdd}d fdd	}t ||||S )zUsed for autocast during script-translation.
    This is meant to transform expressions like "Add(X, 1)" to "Add(X, CastLike(1, X))"
    Polymorphic constants (like 0 and 1) are cast to the type of other operands as needed.
    r+   Optional[ir.Value]r   c                   s   | du s
  | jrdS | S )zReturns x back if x can serve as the target-type for a cast (as the second
        argument of CastLike) and None otherwise. In the expression "Add(X, 1), 1 is
        castable, while X can serve as the target-type.
        N)is_castabler   r*   r[   r#   r$   r7      s   z)static_cast_inputs.<locals>.get_type_infoyOptional[str]c                   sJ   | d u rd S   | jr#|d ur# | j d} |gd| |gS | S )N_castCastLike)ra   r   generate_unique_nameemit1)r+   rc   x_castrb   r#   r$   	cast_like   s   z%static_cast_inputs.<locals>.cast_likeN)r+   r`   r   r`   )r+   r`   rc   r`   r   rd   )rX   )r[   r;   rQ   r7   rj   r#   rb   r$   static_cast_inputs   s   
	rk   r>   )
r   r   r   r   r   r   r   r   r   r   )r   r&   )r7   r8   r9   r:   r;   r<   r   r=   )r;   rY   )r[   r\   r;   r]   rQ   r^   r   r_   )#
__future__r   typingr   r   r   r   r   numpyr,   r   
onnxscriptr   r	   onnxscript.irr
   onnxscript._internalr   	frozensetr   FLOATSINTSSTRINGSTENSORSGRAPHSSPARSE_TENSORSTYPE_PROTOSr   r%   r)   r0   r6   rX   rZ   rk   r#   r#   r#   r$   <module>   s4   



7	