o
    iy
                     @   s6   d dl Z d dlZd dlZd dlZdddZdd ZdS )    NFc                    s  t | tr fdd|  D S t| r0t | ts0t|  fddt| D  S t | trKt| turKt|  fdd| D  S t | ttfrbt|  fdd| D S t | t	j
rstt|  S t | tjr|  S | S )z'Change the device of object recursivelyc              	      s"   i | ]\}}|t | qS  	to_device.0kvcopydevicedtypenon_blockingr   T/home/ubuntu/.local/lib/python3.10/site-packages/espnet2/torch_utils/device_funcs.py
<dictcomp>   s    zto_device.<locals>.<dictcomp>c                       g | ]
}t | qS r   r   r   r   r	   r   r   
<listcomp>   s    zto_device.<locals>.<listcomp>c                    r   r   r   r   or	   r   r   r          c                 3   s     | ]}t | V  qd S Nr   r   r	   r   r   	<genexpr>   s    zto_device.<locals>.<genexpr>)
isinstancedictitemsdataclassesis_dataclasstypeastupletuplelistnpndarrayr   torch
from_numpyTensorto)datar   r   r   r
   r   r	   r   r      s*   
 r   c                    s   t | tr fdd|  D S t | tr(t| tur(t|  fdd| D  S t | tttfr=t|  fdd| D S t | tjrKt	t
|  S t | t
jr`|  dkr[| d } |  S t | trot
j| gt
j d	S t | tr~t
j| gt
j d	S | du rdS tt|  d
 | S )a  Change object to gatherable in torch.nn.DataParallel recursively

    The difference from to_device() is changing to torch.Tensor if float or int
    value is found.

    The restriction to the returned value in DataParallel:
        The object must be
        - torch.cuda.Tensor
        - 1 or more dimension. 0-dimension-tensor sends warning.
        or a list, tuple, dict.

    c                    s   i | ]
\}}|t | qS r   force_gatherabler   r   r   r   r   2   r   z$force_gatherable.<locals>.<dictcomp>c                    s   g | ]}t | qS r   r(   r   r*   r   r   r   5   s    z$force_gatherable.<locals>.<listcomp>c                 3   s    | ]}t | V  qd S r   r(   r   r*   r   r   r   7   s    z#force_gatherable.<locals>.<genexpr>r   N)r   r   z& may not be gatherable by DataParallel)r   r   r   r   r   r    setr!   r"   r)   r#   r$   r%   dimr&   floattensorintlongwarningswarn)r'   r   r   r*   r   r)   $   s(   



r)   )NNFF)r   r1   numpyr!   r#   r   r)   r   r   r   r   <module>   s    
