o
    s::j                     @  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 er,d 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d/ddZdd Zd-ddZd0d d!Zd1d#d$Zd2d+d,Z dS )3    )annotations)TYPE_CHECKINGAnyCallableOptionalSequenceN)irtensor)	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"   d/home/nk/hobo-godmode/plappi-mvp/.venv/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(   A   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#   r/   O   s   



r/   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(   r/   r	   Tensorr+   array)r1   r2   r"   r"   r#   cast_pyvalue_to_os_tensorc   s
   r5   get_type_infoCallable[[Any], Any]castCallable[[Any, Any], Any]op_signatureir.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*   )r8   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r8   type_bindingsr"   r#   
<listcomp>   s     zcast_inputs.<locals>.<listcomp>)
tupleinputs	enumeratelenvariadichomogeneousappendr   type_constraintr   )r6   r8   r:   argsexpected_inputsargs_typevarsir*   expectedrD   typeinfo	cast_argsr"   rE   r#   cast_inputso   s6   

rW   ir.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	   r3   r2   r)   r"   r"   r#   r6      s   z*dynamic_cast_inputs.<locals>.get_type_info)rW   r5   )r:   rP   r6   r"   r"   r#   dynamic_cast_inputs   s   rY   
converter_converter.Converter Optional[ir.schemas.OpSignature]rP   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)   rZ   r"   r#   r6      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)r`   r   _generate_unique_name_emit1)r*   rb   x_castra   r"   r#   	cast_like   s   z%static_cast_inputs.<locals>.cast_likeN)r*   r_   r   r_   )r*   r_   rb   r_   r   rc   )rW   )rZ   r:   rP   r6   ri   r"   ra   r#   static_cast_inputs   s   
	rj   r=   )
r   r   r   r   r   r   r   r   r   r   )r   r%   )r6   r7   r8   r9   r:   r;   r   r<   )r:   rX   )rZ   r[   r:   r\   rP   r]   r   r^   )!
__future__r   typingr   r   r   r   r   numpyr+   r   
onnxscriptr   r	   onnxscript._internalr
   	frozensetr   FLOATSINTSSTRINGSTENSORSGRAPHSSPARSE_TENSORSTYPE_PROTOSr   r$   r(   r/   r5   rW   rY   rj   r"   r"   r"   r#   <module>   s2   



7	