o
    ॵi9                     @   s    d dl mZ dd Zdd ZdS )    )Mappingc                 C   sj   ddl }	 t| ttfrt| dd | D S t| tr%dd |  D S t| |jr3|  }|	 S | S )z Numpify nested torch tensors.

    NOTE: If the type of input tensors is dict-like(Mapping, dict, OrderedDict, etc.), the return type will be dict.

    Args:
        tensors: Nested torch tensors.

    Returns:
        The numpify tensors.
    r   Nc                 s       | ]}t |V  qd S Ntorch_nested_numpify.0t r
   Q/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/utils/tensor_utils.py	<genexpr>       z'torch_nested_numpify.<locals>.<genexpr>c                 S      i | ]	\}}|t |qS r
   r   r   kr	   r
   r
   r   
<dictcomp>       z(torch_nested_numpify.<locals>.<dictcomp>)
torch
isinstancelisttupletyper   itemsTensorcpunumpy)tensorsr   r	   r
   r
   r   r      s   
r   c                 C   sb   ddl }	 t| ttfrt| dd | D S t| tr%dd |  D S t| |jr/|  S | S )z Detach nested torch tensors.

    NOTE: If the type of input tensors is dict-like(Mapping, dict, OrderedDict, etc.), the return type will be dict.

    Args:
        tensors: Nested torch tensors.

    Returns:
        The detached tensors.
    r   Nc                 s   r   r   torch_nested_detachr   r
   r
   r   r   .   r   z&torch_nested_detach.<locals>.<genexpr>c                 S   r   r
   r   r   r
   r
   r   r   0   r   z'torch_nested_detach.<locals>.<dictcomp>)	r   r   r   r   r   r   r   r   detach)r   r   r
   r
   r   r      s   
r   N)collections.abcr   r   r   r
   r
   r
   r   <module>   s   