
    Pg                     Z    d dl mZ 	 d Zd Zd Zd Zd Zd Zd Zd	 Z	d
 Z
d Zd Zd Zy)    )OrderedDictc                 :    | j                         rt        d      y )NzzNYI: Named tensors don't support serialization. Please drop names via `tensor = tensor.rename(None)` before serialization.)	has_namesRuntimeError)tensors    c/var/www/html/suriana-translation/venv/lib/python3.12/site-packages/torch/_namedtensor_internals.pycheck_serializing_named_tensorr	      s&    M
 	
     c                 |    t        t        | j                        D cg c]  \  }}||n||f c}}      S c c}}w )zkReturns a map of { dim: dim_name } where dim is a name if the dim is named
    and the dim index otherwise.)r   	enumeratenames)r   idxnames      r   build_dim_mapr      s:     @I&,,@WX93#4	.X Xs   8
c                     t        | t              r| j                         } t        | d      s't        | t              st        dt        |              t        |       dk(  rt        d      t        |  S )N__iter__zBExpected namedshape to be OrderedDict or iterable of tuples, got: r   z!Expected namedshape to non-empty.)	
isinstancer   itemshasattrtupler   typelenzip)
namedshapes    r   unzip_namedshaper      sq    *k*%%'
:z*:j%3PPQUV`QaPbc
 	
 :!>??
r
   c                 
    | ryy)Nrename_rename )inplaces    r   namer_api_namer!   (   s    r
   c                 "    | t         k(  xs | dk(  S )Nz...)Ellipsis)items    r   is_ellipsisr%   /   s    8,tu},r
   c                     t        |       D cg c]  \  }}t        |      s| }}}t        |      dk\  rt        | d|  d      t        |      dk(  r|d   S y c c}}w )N   z1: More than one Ellipsis ('...') found in names (z-). This function supports up to one Ellipsis.   r   )r   r%   r   r   )r   fn_nameir   ellipsis_indicess        r   single_ellipsis_indexr,   3   s{    )25)9Oga[=NOO
!iHgBD
 	
 !"" Ps
   AAc                 $    || t        |      |z
   S )N)r   )numel_pre_globnumel_post_globr   s      r   expand_single_ellipsisr0   ?   s    #e*">??r
   c                 \    t        | t        |      | z
  dz
  |      }|d |  |z   || dz   d  z   S )Nr(   )r0   r   )ellipsis_idxr   tensor_namesglobbed_namess       r   replace_ellipsis_by_positionr5   C   sG    *c%j</!3\M ,-/%q8H8J2KKKr
   c                 <    t        | |      }|| S t        || |      S )zX
    Expands ... inside `names` to be equal to a list of names from `tensor_names`.
    )r,   r5   )r   r3   r)   r2   s       r   resolve_ellipsisr7   J   s*     )8L'e\JJr
   c           	          t        |      dk(  r|d   | j                  d |      S | j                  t        || j                  t	        |            |      S )Nr(   r   )r   _update_namesr7   r   r!   )r   r   r    s      r   update_names_with_listr:   T   sS    
5zQ58+##D'22nW.EF r
   c                 &   t        |       }|j                         D ]K  }||   }||j                         v r|||<    t        t        |       d| d| d| j                   d| d
       | j                  t        |j                               |      S )Nz: Tried to rename dim 'z	' to dim z in Tensor[z] but dim 'z' does not exist)r   keysr   r!   r   r9   r   values)r   
rename_mapr    dim_mapold_dimnew_dims         r   update_names_with_mappingrB   ^   s    F#G??$ W%glln$&GG!'*++B7)9);v||nKyHXZ  gnn&6 7AAr
   c           	          t        |      dkD  }t        |      }|r1|r/t        t        |       dt        |       dt        |       d      |s|st	        | ||      S |rt	        | ||      S t        | ||      S )a  There are two usages:

    tensor.rename(*names) returns a view on tensor with named dims `names`.
    `names` must be of length `tensor.dim()`; otherwise, if '...' is in `names`,
    then it is expanded greedily to be equal to the corresponding names from
    `tensor.names`.

    For example,
    ```
    >>> # xdoctest: +SKIP
    >>> x = torch.empty(2, 3, 5, 7, names=('N', 'C', 'H', 'W'))
    >>> x.rename('...', 'height', 'width').names
    ('N', 'C', 'height', 'width')

    >>> # xdoctest: +SKIP
    >>> x.rename('batch', '...', 'width').names
    ('batch', 'C', 'H', 'width')

    ```

    tensor.rename(**rename_map) returns a view on tensor that has rename dims
        as specified in the mapping `rename_map`.

    For example,
    ```
    >>> # xdoctest: +SKIP
    >>> x = torch.empty(2, 3, 5, 7, names=('N', 'C', 'H', 'W'))
    >>> x.rename(W='width', H='height').names
    ('N', 'C', 'height', 'width')

    ```

    Finally, tensor.rename has an in-place version called tensor.rename_.
    r   zW: This function takes either positional args or keyword args, but not both. Use tensor.z!(*names) to name dims and tensor.z(**rename_map) to rename dims.)r   boolr   r!   r:   rB   )r   r   r>   r    r   has_rename_pairss         r   update_namesrF   l   s    F E
QIJ'%g&' (>>LW>U=V W''5g'>&? @
 	
 -%feW==%feW==$VZAAr
   N)collectionsr   r	   r   r   r!   r%   r,   r0   r5   r7   r:   rB   rF   r   r
   r   <module>rH      sN    #
	-	@LKB3Br
   