
    9i                     h    S SK r S SKJr  S SKJrJr  S SKJr  \" 5       rS r	\S	S j5       r
S rS rg)
    N)contextmanager)Devices
Frameworks)
get_loggerc                 j   SnU b  U S:w  d   U5       eU R                  5       n U R                  S5      n[        U5      S::  d   U5       eU c   eUS   S;   d   U5       eUS   nSn[        U5      S:  a  [        US   5      nUS	:X  a  [        R
                  nU[        R
                  :X  a  Uc  SnX44$ )
aE  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_ids        W/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/utils/device.pyverify_devicer      s     oG"{b'8A'A8##%KS!Dt9>"7">"""7,,5g5,q'KI
4y1}QL	fkkgkk!i&7	!!    c              #   .  #    [        U5      u  p#U [        R                  :X  a  SSKnU[        R
                  :X  a4  UR                  R                  5       (       d  [        R                  S5        [        R                  nU[        R                  :X  a  UR                  S5         Sv   SSS5        gU[        R
                  :X  a"  UR                  SU 35         Sv   SSS5        ggU [        R                  :X  ap  SSKnU[        R
                  :X  aS  UR                  R                  5       (       a  UR                  R                  SU 35        O[        R                  S5        Sv   gSv   g! , (       d  f       g= f! , (       d  f       g= f7f)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$   s         r   device_placementr(   '   s     " +;7KJMM!'++%bgg.F.F.H.HLLGIkk'++%8$ %$ gkk)YYi[9: ;: * 
j&&	&'++%zz&&((

%%i[&9:HJ# %$ ;:s7   B#F%E3*1FF BF3
F=F
FFc                     SSK n[        U 5      u  p#SnU[        R                  :X  a8  SnUR                  R                  5       (       d  [        R                  S5        SnU(       a  UR                  SU 35      nU$ UR                  S5      nU$ )zPcreate 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#   s         r   create_devicer,   U   s     *;7KHgkk!zz&&((KKCDHi[12 M e$Mr   c                  *   SS K n SSK Jn  U R                  R                  5       (       aX  UR                  5       (       a@  UR	                  5       (       a+  S[
        R                  ;   a  S[
        R                  S    3nOSnOSnU R                  U5      $ )Nr   )distributed
LOCAL_RANKr   zcuda:0r   )r$   r.   r   r%   is_initializedosenvironr#   )r$   distr   s      r   
get_devicer4   l   sw    )zz  4#6#6 $
 $
bjj(

< 89:I I	<<	""r   )zgpu:0)r1   
contextlibr   modelscope.utils.constantr   r   modelscope.utils.loggerr   r!   r   r(   r,   r4    r   r   <module>r9      s<    	 % 9 .	"8 * *Z.#r   