
     Ti                        % S r SSKJr  / SQrSSKrSSKJrJr  SSKJr	  SSK
rSSK
Jr  \(       a  SSKrSqS\S'   SqS	\S
'   SS jrSS jr " S S\R&                  5      rg)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_TORCHc           	        [         Gcy  SSKn0 UR                  [        R                  R
                  _UR                  [        R                  R                  _UR                  [        R                  R                  _UR                  [        R                  R                  _UR                  [        R                  R                  _UR                  [        R                  R                  _UR                   [        R                  R"                  _UR$                  [        R                  R&                  _UR(                  [        R                  R*                  _UR,                  [        R                  R.                  _UR0                  [        R                  R2                  _UR4                  [        R                  R6                  _UR8                  [        R                  R:                  _UR<                  [        R                  R>                  _UR@                  [        R                  RB                  _URD                  [        R                  RF                  _URH                  [        R                  RJ                  _URL                  [        R                  RN                  URP                  [        R                  RR                  0Eq [U        US5      (       a+  [        R                  RV                  [         URX                  '   [U        US5      (       a+  [        R                  RZ                  [         UR\                  '   [U        US5      (       a+  [        R                  R^                  [         UR`                  '   U [         ;  a,  [c        SU  S[e        [         Rg                  5       5       35      e[         U    $ )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dtyper   s     V/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/onnx_ir/tensor_adapters.pyr   r   5   s    # 
NNBKK00 
JJ(( 
 bkk44 
 OOR[[22	 

 MM2;;.. 
 MM2;;,, 
 MM2;;-- 
 !9!9 
 !!2;;#=#= 
 r{{55 
 !!2;;#=#= 
 KK** 
 KK** 
 KK** 
 JJ(( 
  KK**! 
" LL"++,,# 
$ LL"++,,LL"++,,' 
* 5*++9;9O9O !5!565&!!/1{{/?/? ,5'""020A0A -(()% 1;(--/013
 	

  &&    c           	        [         Gcy  SSKn0 [        R                  R                  UR
                  _[        R                  R                  UR                  _[        R                  R                  UR                  _[        R                  R                  UR                  _[        R                  R                  UR                  _[        R                  R                  UR                  _[        R                  R                   UR"                  _[        R                  R$                  UR&                  _[        R                  R(                  UR*                  _[        R                  R,                  UR.                  _[        R                  R0                  UR2                  _[        R                  R4                  UR6                  _[        R                  R8                  UR:                  _[        R                  R<                  UR>                  _[        R                  R@                  URB                  _[        R                  RD                  URF                  _[        R                  RH                  URJ                  _[        R                  RL                  URN                  [        R                  RP                  URR                  0Eq [U        US5      (       a+  URV                  [         [        R                  RX                  '   [U        US5      (       a+  URZ                  [         [        R                  R\                  '   [U        US5      (       a+  UR^                  [         [        R                  R`                  '   U [         ;  aU  U [        R                  RX                  :X  a  [c        S5      e[e        SU  S[g        [         Ri                  5       5       35      e[         U    $ )	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+   r.   r-   r0   r/   r2   r1   r4   r3   r6   r5   r8   r7   r9   r   r:   r   r;   r   r<   
ValueErrorr=   r>   r?   r@   s     rB   r   r   a   s    # 
KK  %.. 
KKejj 
 KK""E$4$4 
 KK!!5??	 

 KK 
 KKu}} 
 KK 
 KK$$e&9&9 
 KK&&(=(= 
 KK""E$5$5 
 KK&&(=(= 
 KKu{{ 
 KKu{{ 
 KKu{{ 
 KKejj 
  KKu{{! 
" KK# 
$ KKKK' 
, 5*++;@;O;O !7!785&!!5:ZZ !1!125'""6;kk !2!23((BKK***I  6ug >;(--/013
 	

  &&rC   c                  n   ^  \ rS rSr S	     S
U 4S jjjrSS jrS	SS jjrS rSS jrSS jr	Sr
U =r$ )r      c                J   > [         TU ]  U[        UR                  5      X#S9  g )N)rA   name
doc_string)super__init__r   rA   )selftensorrI   rJ   	__class__s       rB   rL   TorchTensor.__init__   s(     	*6<<8t 	 	
rC   c                D   SS K nU   U R                  [        R                  R                  :X  aY  U R
                  R                  UR                  5      R                  SS9R                  U R                  R                  5       5      $ U R                  [        R                  R                  [        R                  R                  [        R                  R                  [        R                  R                  [        R                  R                  1;   aY  U R
                  R                  UR                  5      R                  SS9R                  U R                  R                  5       5      $ U R                  [        R                  R                  [        R                  R                   1;   aY  U R
                  R                  UR                  5      R                  SS9R                  U R                  R                  5       5      $ U R
                  R                  SS9$ )Nr   T)force)r   rA   r   r   r   rawviewr3   numpyr"   r$   r&   r(   r:   r1   r;   r<   )rM   r   s     rB   rU   TorchTensor.numpy   sc   ::---88==.4444@EEdjjFVFVFXYY::KK$$KK&&KK""KK&&KK""
 
 88==-33$3?DDTZZEUEUEWXX::"++**BKK,=,=>>88==-33$3?DDTZZEUEUEWXXxx~~D~))rC   c                h    AUc  U R                  5       $ U R                  5       R                  U5      $ N)rU   	__array__)rM   rA   copys      rB   rY   TorchTensor.__array__   s-    =::<zz|%%e,,rC   c                $   SSK nUR                  R                  R                  5          U R                  R                  5       R                  5       R                  5       nSSS5        [        WUR                  R                  R                  5      (       a  [        SU R                   S35      eU[        R                  UR                  5       -  UR                  5       -  R!                  UR#                  5       5      4$ ! , (       d  f       N= f)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_temporarilyrS   detachcpu
contiguous
isinstance
FakeTensorr=   rI   ctypesc_ubyteelement_sizenumelfrom_addressdata_ptr)rM   r   rN   s      rB   _get_cbytesTorchTensor._get_cbytes   s    ,**AACXX__&**,779F D fe//;;FFGG@ L` `  )<)<)>>O]]OO
 
 	
 DCs   7D
Dc                <    U R                  5       u  p[        U5      $ rX   )rl   bytes)rM   _datas      rB   tobytesTorchTensor.tobytes   s     ""$T{rC   c                H    U R                  5       u  p#UR                  U5      $ rX   )rl   write)rM   filerp   rq   s       rB   tofileTorchTensor.tofile   s!    ""$zz$rC    )NN)rN   ztorch.TensorrI   
str | NonerJ   rz   )returnnpt.NDArray)rA   r   rZ   zbool | Noner{   r|   )r{   ro   )r{   None)__name__
__module____qualname____firstlineno__rL   rU   rY   rl   rr   rw   __static_attributes____classcell__)rO   s   @rB   r   r      sF    VZ
"
*4
IS
 
*&-
(   rC   r   )rA   torch.dtyper{   ir.DataType)rA   r   r{   r   )__doc__
__future__r   __all__rf   typingr   r   numpy.typingnptonnx_irr   r	   r   r
   __annotations__r   r   r   Tensorr   ry   rC   rB   <module>r      sd   4 #  %    ?C ; B>B ; B)'X/'d? %,, ? rC   