o
    Xi                     @  s   U d Z ddlmZ g dZddlZddlmZmZ ddlmZ	 ddl
Zddl
mZ er/ddlZdaded< dad	ed
< dddZdddZG dd dejZdS )a  Compatible adapters implementing the TensorProtocol interface for various framework tensor types.

This module provides public classes that implement the :class:`onnx_ir.TensorProtocol`
interface for various tensor types from popular deep learning frameworks.

You can use these classes to create tensors and use them in the IR graph like any other tensor.

Example::
    import torch
    import onnx_ir as ir

    # Create a PyTorch tensor
    torch_tensor = torch.tensor([1, 2, 3])

    # Wrap the PyTorch tensor in a TorchTensor object
    ir_tensor = ir.tensor_adapters.TorchTensor(torch_tensor)

    # Use the IR tensor in the graph
    attr = ir.AttrTensor("x", ir_tensor)
    print(attr)
    )annotations)from_torch_dtypeto_torch_dtypeTorchTensorN)TYPE_CHECKINGAny)_corez%dict[torch.dtype, ir.DataType] | None_TORCH_DTYPE_TO_ONNXz%dict[ir.DataType, torch.dtype] | None_ONNX_DTYPE_TO_TORCHdtypetorch.dtypereturnir.DataTypec                 C  sl  t du rddl}i |jtjj|jtjj|jtjj	|j
tjj|jtjj|jtjj|jtjj|jtjj|jtjj|jtjj|jtjj|jtjj|jtjj|jtjj|j tjj!|j"tjj#|j$tjj%|j&tjj'|j(tjj)ia t*|drtjj+t |j,< t*|drtjj-t |j.< t*|drtjj/t |j0< | t vrt1d|  dt2t 3  t |  S )z/Convert a PyTorch dtype to an ONNX IR DataType.Nr   float8_e8m0fnuint2uint2zUnsupported PyTorch dtype 'z/'. Please use a supported dtype from the list: )4r	   torchbfloat16irDataTypeBFLOAT16boolBOOL
complex128
COMPLEX128	complex64	COMPLEX64float16FLOAT16float32FLOATfloat64DOUBLEfloat8_e4m3fnFLOAT8E4M3FNfloat8_e4m3fnuzFLOAT8E4M3FNUZfloat8_e5m2
FLOAT8E5M2float8_e5m2fnuzFLOAT8E5M2FNUZint16INT16int32INT32int64INT64int8INT8uint8UINT8uint16UINT16uint32UINT32uint64UINT64hasattr
FLOAT8E8M0r   INT2r   UINT2r   	TypeErrorlistkeysr   r    rC   K/home/ubuntu/.local/lib/python3.10/site-packages/onnx_ir/tensor_adapters.pyr   5   sj   








	














r   c                 C  s  t du rddl}i tjj|jtjj|jtjj|j	tjj
|jtjj|jtjj|jtjj|jtjj|jtjj|jtjj|jtjj|jtjj|jtjj|jtjj|jtjj |j!tjj"|j#tjj$|j%tjj&|j'tjj(|j)ia t*|dr|j+t tjj,< t*|dr|j-t tjj.< t*|dr|j/t tjj0< | t vr| tjj,krt1dt2d|  dt3t 4  t |  S )	z/Convert an ONNX IR DataType to a PyTorch dtype.Nr   r   r   r   z}The requested DataType 'FLOAT8E8M0' is only supported in PyTorch 2.7+. Please upgrade your PyTorch version to use this dtype.z(Unsupported conversion from ONNX dtype 'z8' to torch. Please use a supported dtype from the list: )5r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r"   r!   r$   r#   r&   r%   r(   r'   r*   r)   r,   r+   r.   r-   r0   r/   r2   r1   r4   r3   r6   r5   r8   r7   r:   r9   r;   r   r<   r   r=   r   r>   
ValueErrorr?   r@   rA   rB   rC   rC   rD   r   a   sr   








	














r   c                      sT   e Zd Z	dd fddZdddZddddZdd ZdddZd ddZ  Z	S )!r   Ntensortorch.Tensorname
str | None
doc_stringc                   s   t  j|t|j||d d S )N)r   rH   rJ   )super__init__r   r   )selfrF   rH   rJ   	__class__rC   rD   rL      s   
zTorchTensor.__init__r   npt.NDArrayc                 C  s   dd l }|  | jtjjkr| j|jjdd| j S | jtjj	tjj
tjjtjjtjjhv rC| j|jjdd| j S | jtjjtjjhv r_| j|jjdd| j S | jjddS )Nr   T)force)r   r   r   r   r   rawviewr5   numpyr$   r&   r(   r*   r<   r3   r=   r>   )rM   r   rC   rC   rD   rT      s   """zTorchTensor.numpyr   r   copybool | Nonec                 C  s    ~|d u r	|   S |   |S N)rT   	__array__)rM   r   rU   rC   rC   rD   rX      s   zTorchTensor.__array__c                 C  s   ddl }|jj  | j   }W d   n1 sw   Y  t||jjj	r4t
d| j d|tj|  |  | fS )z4Get a ctypes byte array pointing to the tensor data.r   Nz.Cannot take content out from the FakeTensor ('z'). Please replace the tensor with a tensor backed by real data using ONNXProgram.apply_weights() or save the model without initializers by setting include_initializers=False.)torch._subclasses.fake_tensor_subclassesfake_tensorunset_fake_temporarilyrR   detachcpu
contiguous
isinstance
FakeTensorr?   rH   ctypesc_ubyteelement_sizenumelfrom_addressdata_ptr)rM   r   rF   rC   rC   rD   _get_cbytes   s   zTorchTensor._get_cbytesbytesc                 C  s   |   \}}t|S rW   )rh   ri   )rM   _datarC   rC   rD   tobytes   s   zTorchTensor.tobytesNonec                 C  s   |   \}}||S rW   )rh   write)rM   filerj   rk   rC   rC   rD   tofile   s   
zTorchTensor.tofile)NN)rF   rG   rH   rI   rJ   rI   )r   rP   )r   r   rU   rV   r   rP   )r   ri   )r   rm   )
__name__
__module____qualname__rL   rT   rX   rh   rl   rp   __classcell__rC   rC   rN   rD   r      s    

r   )r   r   r   r   )r   r   r   r   )__doc__
__future__r   __all__rb   typingr   r   numpy.typingnptonnx_irr   r   r   r	   __annotations__r
   r   r   Tensorr   rC   rC   rC   rD   <module>   s   

,2