a
    h                     @   sp   d dl mZ dd Zdd Zdd Zdd	 Zd
d Zdd Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )    )OrderedDictc                 C   s   |   rtdd S )NzzNYI: Named tensors don't support serialization. Please drop names via `tensor = tensor.rename(None)` before serialization.)	has_namesRuntimeErrortensor r   J/var/www/auris/lib/python3.9/site-packages/torch/_namedtensor_internals.pycheck_serializing_named_tensor   s    r	   c                 C   s   t dd t| jD S )zkReturns a map of { dim: dim_name } where dim is a name if the dim is named
    and the dim index otherwise.c                 S   s$   g | ]\}}|d u r|n||fqS Nr   ).0idxnamer   r   r   
<listcomp>       z!build_dim_map.<locals>.<listcomp>)r   	enumeratenamesr   r   r   r   build_dim_map   s    r   c                 C   sT   t | tr|  } t| ds8t | ts8tdt|  t| dkrLt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)Z
namedshaper   r   r   unzip_namedshape   s    
r   c                 C   s   | rdS dS d S )NZrename_renamer   )inplacer   r   r   namer_api_name(   s    r   c                 C   s   | t kp| dkS )Nz...)Ellipsis)itemr   r   r   is_ellipsis/   s    r!   c                 C   sJ   dd t | D }t|dkr2t| d|  dt|dkrF|d S d S )Nc                 S   s   g | ]\}}t |r|qS r   )r!   )r   ir   r   r   r   r   4   r   z)single_ellipsis_index.<locals>.<listcomp>   z1: More than one Ellipsis ('...') found in names (z-). This function supports up to one Ellipsis.   r   )r   r   r   )r   fn_nameZellipsis_indicesr   r   r   single_ellipsis_index3   s    r&   c                 C   s   || t ||  S r
   )r   )Znumel_pre_globZnumel_post_globr   r   r   r   expand_single_ellipsis?   s    r'   c                 C   s8   t | t||  d |}|d |  | || d d   S )Nr$   )r'   r   )ellipsis_idxr   tensor_namesZglobbed_namesr   r   r   replace_ellipsis_by_positionC   s    r*   c                 C   s"   t | |}|du r| S t|| |S )zX
    Expands ... inside `names` to be equal to a list of names from `tensor_names`.
    N)r&   r*   )r   r)   r%   r(   r   r   r   resolve_ellipsisJ   s    
r+   c                 C   s>   t |dkr$|d d u r$| d |S | t|| jt||S )Nr$   r   )r   _update_namesr+   r   r   )r   r   r   r   r   r   update_names_with_listT   s
    r-   c                 C   st   t | }| D ]N}|| }|| v r2|||< qtt| d| d| d| j d| d
q| t| |S )Nz: Tried to rename dim 'z	' to dim z in Tensor[z] but dim 'z' does not exist)r   keysr   r   r   r,   r   values)r   
rename_mapr   Zdim_mapZold_dimZnew_dimr   r   r   update_names_with_mapping^   s    
r1   c                 C   sr   t |dk}t|}|rB|rBtt| dt| dt| d|sV|sVt| ||S |rft| ||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-   r1   )r   r   r0   r   r   Zhas_rename_pairsr   r   r   update_namesl   s    #
r3   N)collectionsr   r	   r   r   r   r!   r&   r'   r*   r+   r-   r1   r3   r   r   r   r   <module>   s   


