o
    ॵi                     @   s`   d dl Z d dlmZ d dlmZmZ d dlmZ e Zdd Z	eddd	Z
d
d Zdd ZdS )    N)contextmanager)Devices
Frameworks)
get_loggerc                 C   s   d}| dur
| dksJ ||   } | d}t|dks!J || dus'J |d dv s1J ||d }d}t|dkrCt|d }|d	krJtj}|tjkrU|du rUd}||fS )
a^   Verify device is valid, device should be either cpu, cuda, gpu, cuda:X or gpu:X.

    Args:
        device (str):  device str, should be either cpu, cuda, gpu, gpu:X or cuda:X
            where X is the ordinal for gpu device.

    Return:
        device info (tuple):  device_type and device_id, if device_id is not set, will use 0 as default.
    z^device should be either cpu, cuda, gpu, gpu:X or cuda:X where X is the ordinal for gpu device.N :   r   )cpucudagpu   r
   )lowersplitlenintr   r   )device_nameerr_msgelesdevice_type	device_id r   K/home/ubuntu/.local/lib/python3.10/site-packages/modelscope/utils/device.pyverify_device   s    

r   gpu:0c                 c   s&   t |\}}| tjkrgddl}|tjkr|j st	d tj
}|tj
krB|d dV  W d   dS 1 s;w   Y  dS |tjkre|d|  dV  W d   dS 1 s^w   Y  dS dS | tjkrddl}|tjkr|j r|jd|  nt	d dV  dS dV  dS )a   Device placement function, allow user to specify which device to place model or tensor
    Args:
        framework (str):  tensorflow or pytorch.
        device (str):  gpu or cpu to use, if you want to specify certain gpu,
            use gpu:$gpu_id or cuda:$gpu_id.

    Returns:
        Context manager

    Examples:

        >>> # Requests for using model on cuda:0 for gpu
        >>> with device_placement('pytorch', device='gpu:0'):
        >>>     model = Model.from_pretrained(...)
    r   Nz5tensorflow: cuda is not available, using cpu instead.z/CPU:0z/device:gpu:cuda:z2pytorch: cuda is not available, using cpu instead.)r   r   tf
tensorflowr   r   testis_gpu_availableloggerdebugr	   devicetorchr
   is_available
set_device)	frameworkr   r   r   r   r"   r   r   r   device_placement'   s8   

"
"




r&   c                 C   sd   ddl }t| \}}d}|tjkrd}|j std d}|r+|d| }|S |d}|S )z] create torch device

    Args:
        device_name (str):  cpu, gpu, gpu:0, cuda:0 etc.
    r   NFTz)cuda is not available, using cpu instead.r   r	   )	r"   r   r   r   r
   r#   r   infor!   )r   r"   r   r   use_cudar!   r   r   r   create_deviceU   s   



r)   c                  C   s^   dd l } ddl m} | j r(| r%| r%dtjv r%dtjd  }nd}nd}| |S )Nr   )distributed
LOCAL_RANKr   zcuda:0r	   )r"   r*   r
   r#   is_initializedosenvironr!   )r"   distr   r   r   r   
get_devicel   s   


r0   )r   )r-   
contextlibr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r   r   r&   r)   r0   r   r   r   r   <module>   s   -