
    i3                    
   % S SK Jr  S SKrS SKrS SKrS SKJrJrJrJrJ	r	J
r
  S SKJs  Jr  S SKrS SKJr  S SKJrJrJr  S SKJr   " S S\5      rSS	 jrSS
 jrSS jrS S jrS!S jrS"S jrSr " S S5      rS#S jr 0 \RB                  \RD                  _\RF                  \RH                  _\RJ                  \RL                  _\RN                  \RP                  _\RR                  \RT                  _\RV                  \RX                  _\RZ                  \R\                  _\R^                  \R`                  _\Rb                  \Rd                  _\Rf                  \Rh                  _\Rj                  \Rl                  _\Rn                  \Rp                  _\Rr                  \Rt                  _\Rv                  \Rx                  _\Rz                  \R|                  _\R~                  \R                  _\R                  \R                  _\R                  \R                  \R                  \R                  0ErGS\HS'   \G V s0 s H	  n \GU    U _M     sn rIS\HS'   S$S jrJS%S jrKS&S jrLS'S jrM        S(S jrNS)S jrOgs  sn f )*    )annotationsN)DictList
NamedTupleOptionalTupleUnion)internal_assert)ExtraTensorInfo
ScalarTypeTensorShapeDynamism)
eval_shapec                      \ rS rSrSrg)AddressSpaceOverflowException    N)__name__
__module____qualname____firstlineno____static_attributes__r       U/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/executorch/exir/tensor.pyr   r      s    r   r   c                n    [         R                  " U 5      [        R                  R	                  U5      -  $ )z(
Assume the tensor is a contiguous one.
)mathprodtorch_utils_element_size)shapedtypes     r   num_bytes_from_shape_and_dtyper"      s'    
 99Uell88???r   c                    / nSn[        U 5       H;  nUR                  U5        [        U[        5      (       a  US:w  a  X#-  nM5  M7  X#-  nM=     [	        [        U5      5      $ )N   r   )reversedappend
isinstanceinttuple)r    stridesaccumszs       r   contiguous_stride_from_shaper-   %   sc    GEuou b#Qw  KE  '"##r   c                L  ^^ SSK JnJm  [        U 5       H   u  p#U" US:H  5      (       d  M  [	        S5      e    " U4S jS[
        5      m[        [        U 5      U4S jSS9 Vs/ s H  oDS   PM	     nn[        [        R                  " [        [           U5      5      $ s  snf )	ag  
Dimension order represents how dimensions are laid out in memory,
starting from the outer-most to the inner-most dimension.
Thus, the conversion from strides is done by sorting the strides
from larger to smaller since the dimension with the largest stride
is the outer-most and the dimension with the smallest stride is the inner-most.
For example, tensor with sizes = (3, 5, 2) and strides = (5, 1, 15), implies
dimension order of (2, 0, 1). Dimension order of (2, 0, 1) can be obtained
by sorting strides from large to smaller.

When strides do not convey dimension order unambiguously, dimension order
returned is dependent on stability of sort. In python same key elements are kept
in original order. Thus when strides = (4, 3, 1, 1) returned value is (0, 1, 2, 3)
Another example is: sizes = (1, 3, 1, 1) with strides = (3, 1, 3, 3), returned
value is (0, 2, 3, 1)
r   )guard_or_falseguard_size_obliviousz-0 in strides is not supported for ExecuTorch.c                  ^   > \ rS rSr% S\S'   U 4S jrU 4S jrU 4S jrU 4S jrU 4S jr	S	r
g
) dim_order_from_stride.<locals>.KO   r(   stridec                B   > T" U R                   UR                   :  5      $ Nr4   selfotherr0   s     r   __lt__'dim_order_from_stride.<locals>.K.__lt__R       'ell(BCCr   c                B   > T" U R                   UR                   :  5      $ r6   r7   r8   s     r   __gt__'dim_order_from_stride.<locals>.K.__gt__U   r=   r   c                B   > T" U R                   UR                   :*  5      $ r6   r7   r8   s     r   __le__'dim_order_from_stride.<locals>.K.__le__X       'u||(CDDr   c                B   > T" U R                   UR                   :  5      $ r6   r7   r8   s     r   __ge__'dim_order_from_stride.<locals>.K.__ge__[   rD   r   c                B   > T" U R                   UR                   :H  5      $ r6   r7   r8   s     r   __eq__'dim_order_from_stride.<locals>.K.__eq__^   rD   r   r   N)r   r   r   r   __annotations__r;   r?   rB   rF   rI   r   )r0   s   r   Kr2   O   s,    	D	D	E	E	E 	Er   rL   c                   > T" U S   5      $ )Nr$   r   )xrL   s    r   <lambda>'dim_order_from_stride.<locals>.<lambda>b   s    a!gr   T)keyreverse)%torch.fx.experimental.symbolic_shapesr/   r0   	enumerate
ValueErrorr   sortedr)   typingcastr   bytes)r4   r/   _sisorted_dimsrL   r0   s         @@r   dim_order_from_strider^   5   s    "
 &!!q&!!LMM "EJ E& Yv.4EtTT!T   U5\;788s   &B!c                   [        U 5      S:X  a  / $ [        R                  " U 5      n[        U 5      nSX!US-
     '   [        US-
  SS5       H<  nXUS-         S:X  a  X!US-         X!U   '   M"  XUS-         X!US-         -  X!U   '   M>     U$ )a  
Converts dim order to stride using sizes
e.g. if sizes = (2, 3, 4) and dim_order = (0, 1, 2) then strides = (12, 4, 1)
while for the same size if dim_order = (0, 2, 1) then strides = (12, 1, 3)
See executorch/runtime/core/exec_aten/util/dim_order_util.h for details
Args:
    sizes (Tuple[int]): sizes of the tensor
    dim_order (Tuple[bytes]): dim order of the tensor
Returns:
    Tuple[int]: stride
r   r$      )lencopydeepcopyrange)sizes	dim_orderr*   ndimr\   s        r   stride_from_dim_orderri   g   s     5zQ	mmE"Gu:D#$GdQh 4!8R$1q5!"a'$+a!e,<$=GaL!$)AE*:$;gPQTUPUFV>W$WGaL!	 %
 Nr   c                8    [         R                  " X-  5      U-  $ r6   )r   ceil)num	alignments     r   calculate_aligned_num_bytesrn      s    99S_%	11r   c                   [        S U  5       5      (       a  [        R                  $  [        U 5      n[        R                  $ ! [
        R                  R                  R                  R                   a    [        R                  s $ f = f)Nc              3  B   #    U  H  n[        U[        5      v   M     g 7fr6   )r'   r(   ).0r[   s     r   	<genexpr>,determine_tensor_dynanism.<locals>.<genexpr>   s     
-u!:aus   )allr   STATICr   DYNAMIC_BOUNDr   fxexperimentalsymbolic_shapesGuardOnDataDependentSymNodeDYNAMIC_UNBOUND)r    rZ   s     r   determine_tensor_dynanismr|      sk    

-u
---")))	75!A&444xx$$44PP 	7&666	7s   A AB	B	   c                  $   \ rS rSrSr\R                  SSSS4               SS jjr\SS j5       r	SS jr
SS jr\SSS	 jj5       rSS
 jr\SS j5       r\SS j5       r\SS j5       r\SS j5       r\SS j5       rSS jrSS jrSrg)
TensorSpec   zL
Captures the metadata for a given Tensor (ex. scalar type, storage, etc.).
FNc                8   Xl         XPl        [        U l        S U l        [        U5      U l        [        U5      U l        [        U R                  5      U l
        X`l        X0l        X@l        U R                  5         [        U R                  5      U l        Xpl        g r6   )scalar_typeconst	ALIGNMENTrm   storagelistr    r-   r4   r^   rg   requires_gradlayout	is_sparseinit_mem_planning_fieldsr|   shape_dynamismextra_tensor_info)r9   r!   r    r   r   r   r   r   s           r   __init__TensorSpec.__init__   sw     !
'7; $U
">u"E'<T[['I*"%%'3LTZZ3X!2r   c                l    [        U R                  U R                  5      n[        XR                  5      $ r6   )r"   r    r!   rn   rm   )r9   nbytess     r   allocated_memoryTensorSpec.allocated_memory   s%    /

DJJG*6>>BBr   c                &    Xl         U R                  $ r6   )rm   r   )r9   new_alignments     r   realignTensorSpec.realign   s    &$$$r   c                B    [        U R                  U R                  5      $ r6   )r"   r    r!   r9   s    r   r   TensorSpec.nbytes   s    -djj$**EEr   c                   U(       aL  UR                  5       nUR                  UR                  5       R                  5       :w  a  UR                  5       nU " UR                  UR
                  UR                  UUR                  S9nUR                  5       Ul        [        UR                  5      Ul
        UR                  Ul        U(       a  UR                  5       Ul        U$ S Ul        U$ )N)r!   r    r   r   r   )
contiguousr   untyped_storagecloner!   r    r   r   r4   r^   rg   r   r   )clstensorr   specs       r   from_tensorTensorSpec.from_tensor   s    &&(F}} 6 6 8 ? ? AA,,,,==&&
 mmo.t{{;#1138v--/ ?Cr   c                @    S S /U l         S U l        S U l        S U l        g r6   )lifetimemem_id
mem_obj_id
mem_offsetr   s    r   r   #TensorSpec.init_mem_planning_fields   s"    tr   c                    U R                   $ r6   )r   r   s    r   r!   TensorSpec.dtype   s    r   c                P    U R                   [        R                  R                  :g  $ r6   )r   schemar   ru   r   s    r   is_dynamic_shape_tensor"TensorSpec.is_dynamic_shape_tensor   s    ""f&@&@&G&GGGr   c                <    U R                   [        R                  :H  $ r6   )r   r   ru   r   s    r   is_static_shape_tensor!TensorSpec.is_static_shape_tensor   s    ""&9&@&@@@r   c                <    U R                   [        R                  :H  $ r6   )r   r   rv   r   s    r   is_upper_bound_tensor TensorSpec.is_upper_bound_tensor   s    ""&9&G&GGGr   c                <    U R                   [        R                  :H  $ r6   )r   r   r{   r   s    r   is_dynamic_unbound_tensor$TensorSpec.is_dynamic_unbound_tensor   s    ""&9&I&IIIr   c                   S[        U 5       SU R                   SU R                   3SU R                   SU R                   3-   SU R
                   SU R                   3-   SU R                   3-   S	U R                   3-   S
-   $ )NzTensorSpec(id=, const=z, scalar_type=z, allocated_memory=z	, mem_id=z, mem_offset=z, lifetime=, shape_dynamism=, shape=))	idr   r   r   r   r   r   r   r    r   s    r   debugTensorSpec.debug   s    RXJhtzzl.IYIYHZ[#D$9$9#:)DKK=QRdoo.k$--IJ "$"5"5!678 $**&	(
 	
r   c                    SU R                    SU R                   3SU R                   3-   SU R                   3-   SU R                   3-   SU R
                   SU R                   3-   S-   $ )	z#
Round-trippable printing function
zTensorSpec(dtype=r   z	, layout=z, is_sparse=r   r   z, requires_grad=r   )r   r    r   r   r   r   r   r   s    r   __repr__TensorSpec.__repr__   s    
   0 01$**F$++'(T^^,-. "$"5"5!678 $4T5G5G4HI	J
 	
r   )rm   r   rg   r   r   r   r   r   r   r   r   r   r    r   r   r4   )r!   torch.dtyper    
torch.Sizer   torch.layoutr   boolr   r   r   r   r   zOptional[ExtraTensorInfo]returnNone)r   r(   )r   r(   r   r(   )F)r   torch.Tensorr   r   r   r   )r   r   )r   r   )r   r   )r   str)r   r   r   r   __doc__r   stridedr   propertyr   r   r   classmethodr   r   r!   r   r   r   r   r   r   r   r   r   r   r   r      s     %}}#7;33 3 	3
 3 3 3 53 
32 C C%F  ,     H H A A H H J J

r   r   c                    [        [        U [        R                  5      S5        [        R                  S[        R
                  S0nX   $ )Nz#We only support torch.memory_formatr   r$   )r
   r'   r   memory_formatcontiguous_formatpreserve_format)r   tables     r   memory_format_enumr     sG    =%"5"56-
 	qE r   zDict[torch.dtype, ScalarType]scalar_type_tablezDict[ScalarType, torch.dtype]enum_to_scalar_mapc                \    [        [        U [        R                  5      S5        [        U    $ )Nz.We only support dtypes defined in Pytorch Core)r
   r'   r   r!   r   )r!   s    r   scalar_type_enumr   -  s(    5%++&(X U##r   c                    [         U    $ r6   )r   )enums    r   get_scalar_typer   5  s    d##r   c                N    [         R                  S[         R                  S0nX   $ )Nr   r$   )r   r   
sparse_coo)r   r   s     r   layout_enumr   9  s)     	q!E =r   c                    US:  a  [        SU S35      eUS-  nUS-	  nUS:  a  [        SU S35      e[        R                  " U UUS9nU$ )	z<
Creates the allocation_details object for creating tensors
r   zmem_offset z must not be negativel        l        z does not fit in 64 bits)	memory_idmemory_offset_lowmemory_offset_high)rU   r   r   AllocationDetails)r   r   r   r   allocation_infos        r   make_allocation_infor   B  s{     A~;zl2GHII"m4#r)W$+*%=>
 	
 ..+-O
 r   c                "       SS jnU" UR                   5      nU" UR                  5      n[        R                  " [	        UR
                  5      SUUUR                  U U[        UR                  5      UR                  UR                  S9
nU$ )z:
Converts the normal torch tensor to a flatbuffer tensor.
c                    [        U [        R                  5      (       d  [        U [        5      (       a  [	        U 5      $ [        U [
        5      (       a  U /$ U $ r6   )r'   r   Sizer)   r   r(   )rN   s    r   to_list"make_tensor_value.<locals>.to_list`  sC     a$$
1e(<(<7N33JHr   r   )
r   storage_offsetrf   rg   r   data_buffer_idxr   r   r   r   )rN   z-Union[torch.Size, int, List[int], Tuple[int]]r   z"Union[List[int], List[torch.Size]])r    rg   r   Tensorr   r   r   r   r   r   r   )r   r   r   r   tensor_sizetensor_dim_orderflatbuffer_tensors          r   make_tensor_valuer   W  s    8	+ $**%Kt~~.$T%5%56"((''4;;'**00 r   c                x   [        U R                  UR                  :H  SUR                   SU R                   S35        [        U R                  UR                  :H  SUR                   SU R                   S35        [        U R                  UR                  :H  SUR                   SU R                   S35        g )Nz9Tensor attribute 'is_sparse' is expected to be equal to 'z', actually got: ''z5Tensor attribute 'shape' is expected to be equal to 'z5Tensor attribute 'dtype' is expected to be equal to ')r
   r   r    r!   )r   r   s     r   
check_specr   }  s    DNN*
CDNNCSSeflfvfvewwxy 

"
?

|K]^d^j^j]kklm 

"
?

|K]^d^j^j]kklmr   )r    r   r!   r   r   r(   )r    r   r   
Tuple[int])r4   r   r   zTuple[bytes])rf   	List[int]rg   r   r   r   )rl   r(   rm   r(   r   r(   )r    r   r   r   )r   ztorch.memory_formatr   r(   )r!   r   r   r   )r   r   r   r   )r   r   r   r(   )r   r(   r   r(   r   zschema.AllocationDetails)r   r(   r   z"Optional[schema.AllocationDetails]r   r   r   zschema.Tensor)r   r   r   r   r   r   )P
__future__r   rc   r   rW   r   r   r   r   r   r	   executorch.exir.schemaexirr   r   executorch.exir.errorr
   r   r   r   executorch.exir.sym_utilr   	Exceptionr   r"   r-   r^   ri   rn   r|   r   r   r   uint8BYTEint8CHARint16SHORTint32INTint64LONGhalfHALFfloatFLOATdoubleDOUBLE	complex32	COMPLEX32	complex64	COMPLEX64
complex128
COMPLEX128r   BOOLqint8QINT8quint8QUINT8qint32QINT32bfloat16BFLOAT16quint4x2QUINT4x2uint16UINT16uint32UINT32r   rK   r   r   r   r   r   r   r   )rQ   s   0r   <module>r#     s   #    A A ' '  1 S S /	I 	@$ /9d227 	p
 p
f	 4	KK4	JJ
4 
KK!!4 
KK	4
 
KK4 
JJ
4 
KK!!4 
LL*##4 
OOZ))4 
OOZ))4 
j++4 
JJ
4 
KK!!4 
LL*##4 
LL*##4  
NNJ''!4" 
NNJ''#4$ 
LL*##	LL*##'4 0 0 ,=5+<CcC+<5 1 
$$*##7# # 	#Lk5s   J 