
    =pj                         S SK r S SKJr  SSKJr  SSKJrJrJrJ	r	J
r
Jr  SSKJrJrJr  SSKJr  SSKJr  / S	QrS
 rS rS rS rS r\" S5      S 5       rS rS r\" S5       " S S\5      5       rg)    N)ContextDecorator   )logging)DCU_WHITELISTGCU_WHITELISTMETAX_GPU_WHITELISTMLU_WHITELISTNPU_BLACKLISTXPU_WHITELIST)class_requires_depsfunction_requires_depsis_dep_available)DISABLE_DEV_MODEL_WL)import_paddle)	cpugpuxpunpumlugcudcuiluvatar_gpu	metax_gpuc                     U S:X  a  Ub  [        S5      eU(       a&  SR                  [        [        U5      5      nU  SU 3$ U  $ )Nr   z"`device_ids` must be None for CPUs,:)
ValueErrorjoinmapstr)device_type
device_idss     P/var/www/html/pdf-tiff/venv/lib/python3.13/site-packages/paddlex/utils/device.pyconstr_devicer$   ,   sL    e
 6=>>XXc#z23
a
|,,    c                  "   [        S5      (       a_  [        5       n U R                  R                  5       (       a5  U R                  R                  R                  5       S:  a  [        SS/5      $ g[        S5      (       aK  SS KnUR                  R                  5       (       a(  UR                  R                  (       a  [        SS/5      $ [        S5      (       a5  SS K
n[        UR                  5       5      nUS1-  (       a  [        SS/5      $ g)Npaddlepaddler   r   r   torchonnxruntimeCUDAExecutionProvider)r   r   deviceis_compiled_with_cudacudadevice_countr$   r(   is_availableversionr)   setget_available_providers)paddler(   ort	providerss       r#   get_default_devicer6   7   s    ''MM//11""//1A5 ,,  ::""$$);); ,,&&!3356	/00 ,,r%   c                    U R                  S5      n[        U5      S:  a  [        SU  35      e[        U5      S:X  a  US   Sp2OZUu  p#UR                  S5      nU H&  nUR                  5       (       a  M  [        SU 35      e   [	        [        [        U5      5      nUR                  5       nUR                  5       [        ;   d   eUS	:X  a  Ub  [        S
5      eX#4$ )parse_devicer      zInvalid device: r   r   Nr   z1Device ID must be an integer. Invalid device ID: r   z)No Device ID should be specified for CPUs)	splitlenr   isdigitlistr   intlowerSUPPORTED_DEVICE_TYPE)r+   partsr!   r"   	device_ids        r#   r8   r8   N   s     LLE
5zA~+F8455
5zQ"'(DZ"'%%c*
#I$$&& G	{S  $
 #c:./
##%K"7777e
 6DEE""r%   c                 ~    [        U 5      u  p#U(       a  [        U5      U:  d   e[        X#S U 5      $ [        X#5      $ N)r8   r;   r$   )r+   numr!   r"   s       r#   update_device_numrF   g   sA    *62K:#%%%[Tc*:;;[55r%   c                 2    [        U 5      u  p[        U5      $ rD   )r8   set_env_for_device_type)r+   r!   _s      r#   set_env_for_devicerJ   p   s    !&)NK";//r%   r'   c                    [        5       nS nU R                  5       S:X  a!  UR                  5       (       a  SS0nU" U5        U R                  5       S:X  a  SSSSS	S	S
.nU" U5        U R                  5       S:X  a  SSSSSS.nU" U5        U R                  5       S:X  a  SS0nU" U5        U R                  5       S:X  a  SSS.nU" U5        U R                  5       S:X  a  SS0nU" U5        g g )Nc                     U R                  5        H4  u  pU[        R                  U'   [        R                  " U SU S35        M6     g )Nz has been set to .)itemsosenvironr   debug)envskeyvals      r#   _set%set_env_for_device_type.<locals>._sety   s;    

HC!BJJsOMMSE!23%q9: %r%   r   FLAGS_conv_workspace_size_limit2000r   0auto_growthz0pad3d,pad3d_grad,set_value,set_value_with_tensorTrue)FLAGS_npu_jit_compileFLAGS_use_stride_kernelFLAGS_allocator_strategyCUSTOM_DEVICE_BLACK_LISTFLAGS_npu_scale_aclnnFLAGS_npu_split_aclnnr   11800pad3d)BKCL_FORCE_SYNCBKCL_TIMEOUTr]   XPU_BLACK_LISTXPU_PADDLE_CONV_FLOATr   r]   r   )r]   $FLAGS_use_stream_safe_cuda_allocatorr   )r   r?   is_compiled_with_rocm)r!   r3   rU   rR   s       r#   rH   rH   u   s   _F; e#(D(D(F(F16:T
e#%('*(5(Z%+%+
 	T
e#""'*%%(
 	T
k))3/T
e#'*47
 	T
e#)3/T
 $r%   c                    [         (       a  [        R                  " S5        g SnU S:X  a  U[        ;   d   SU S3U-   5       eg U S:X  a  U[        ;   d   SU S3U-   5       eg U S:X  a  U[
        ;   d   S	U S3U-   5       eg U S
:X  a  U[        ;  d   SU S3U-   5       eg U S:X  a  U[        ;   d   SU S3U-   5       eg U S:X  a  U[        ;   d   SU S3U-   5       eg g )NznSkip checking if model is supported on device because the flag `PADDLE_PDX_DISABLE_DEV_MODEL_WL` has been set.zWYou could set env `PADDLE_PDX_DISABLE_DEV_MODEL_WL` to `true` to disable this checking.r   z%The DCU device does not yet support `z` model!r   z%The MLU device does not yet support `r   z+The METAX_GPU device does not yet support `r   z%The NPU device does not yet support `r   z%The XPU device does not yet support `r   z%The GCU device does not yet support `)	r   r   warningr   r	   r   r
   r   r   )r!   
model_nametipss      r#   check_supported_device_typero      sF   |	
 	dDe]* 	
3J<xH4O	
* 
	]* 	
3J<xH4O	
* 
	#00 	
9*XNQUU	
0 
	. 	
3J<xH4O	
. 
	]* 	
3J<xH4O	
* 
	]* 	
3J<xH4O	
* 
r%   c                 2    [        U 5      u  p#[        X!5      $ rD   )r8   ro   )r+   rm   r!   rI   s       r#   check_supported_devicerq      s    !&)NK&{??r%   c                   *    \ rS rSrSrS rS rS rSrg)TemporaryDeviceChanger   z7
A context manager to temporarily change global device
c                 b    [        5       nXl        UR                  R                  5       U l        g rD   )r   
new_devicer+   
get_deviceoriginal_device)selfrv   r3   s      r#   __init__TemporaryDeviceChanger.__init__   s#    $%}}779r%   c                     [        5       nU R                  c  U $ UR                  R                  U R                  5        U $ rD   )r   rv   r+   
set_device)ry   r3   s     r#   	__enter__ TemporaryDeviceChanger.__enter__   s3    ??"K  1r%   c                 ~    [        5       nU R                  c  gUR                  R                  U R                  5        g)NF)r   rv   r+   r}   rx   )ry   exc_typeexc_valexc_tbr3   s        r#   __exit__TemporaryDeviceChanger.__exit__   s1    ??"  !5!56r%   )rv   rx   N)	__name__
__module____qualname____firstlineno____doc__rz   r~   r   __static_attributes__ r%   r#   rs   rs      s    :r%   rs   )rO   
contextlibr    r   custom_device_listr   r   r   r	   r
   r   depsr   r   r   flagsr   import_guardr   r@   r$   r6   r8   rF   rJ   rH   ro   rq   rs   r   r%   r#   <module>r      s    
 '   P O ' '
  .#260
 '* (*Z
D@
 ^$-  %r%   