
    BVh                     Z   d Z ddlZddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 d Zd Z G d d	      Z e       rS G d
 deej                  j                        Z G d dej                  j"                        Z e       Zej&                  Zn G d de      Zdad Zd Zd Zd Zy)z"Library for multi-process testing.    N)app)logging)testc                  h    t        t        j                        dk\  xr dt        j                  d   v S )z*Returns whether the test is run under OSS.   bazelr   )lensysargv     ^/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/distribute/multi_process_lib.pyis_ossr      s'    	SXX!		6388A; 66r   c                      t         j                  D  cg c]  } | j                  d      s|  }} t               r|ryt         j                  dk(  rt        j                         dk(  ryt         j
                  dk7  S c c} w )Nz--tpuF)      Linuxwin32)r
   r   
startswithr   version_infoplatformsystem)argtpu_argss     r   _is_enabledr   !   sd     !XXAc)@cA(AX(HOO$5$@		   Bs
   A;A;c                   (     e Zd ZdZ fdZd Z xZS )_AbslProcessz'A process that runs using absl.app.run.c                 p    t        t        | 
  |i | t        | d      | _        | j
                  | _        y )Nrun)superr   __init__getattr	_run_impl_run_with_abslr   )selfargskwargs	__class__s      r   r!   z_AbslProcess.__init__/   s2    	,&77T5)DN""DHr   c                 6     t        j                   fd       y )Nc                 $    j                         S )N)r#   )_r%   s    r   <lambda>z-_AbslProcess._run_with_absl.<locals>.<lambda>6   s    dnn& r   )r   r   )r%   s   `r   r$   z_AbslProcess._run_with_absl5   s    GG&'r   )__name__
__module____qualname____doc__r!   r$   __classcell__)r(   s   @r   r   r   ,   s    /#(r   r   c                       e Zd ZdZy)AbslForkServerProcessz^An absl-compatible Forkserver process.

    Note: Forkserver is not available in windows.
    N)r-   r.   r/   r0   r   r   r   r3   r3   ;   s    r   r3   c                       e Zd ZdZeZy)AbslForkServerContextabsl_forkserverN)r-   r.   r/   _namer3   Processr   r   r   r5   r5   B   s    E#Gr   r5   c                       e Zd ZdZd Zy)r8   z7A process that skips test (until windows is supported).c                 0    ~~t        j                  d      )NzBTODO(b/150264776): Windows is not supported in MultiProcessRunner.)unittestSkipTest)r%   r&   r'   s      r   r!   zProcess.__init__N   s    

NP Pr   N)r-   r.   r/   r0   r!   r   r   r   r8   r8   K   s    APr   r8   Fc                     t         j                  d   j                  d      rkd }  | d      }|s | d      }|At        j                  dt         j                  d   t
        j                         t        d      |t         j                  d<   t        j                         j                  t         j                  d          y)	a  Set the path to the executable for spawned processes.

  This utility searches for the binary the parent process is using, and sets
  the executable of multiprocessing's context accordingly.

  Raises:
    RuntimeError: If the binary path cannot be determined.
  r   z.pyc                    dt         j                  d   v r| t         j                  d   v rt         j                  d   d t         j                  d   j                  |        }t        j                  d   dd  j                  ddd      }t        j                  j                  || |      }t        j                  d|       t        j                  |t        j                        r|S y y y )	Nz	bazel-outr   TEST_TARGET   :/r   zGuessed test binary path: %s)r
   r   rfindosenvironreplacepathjoinr   infoaccessX_OK)package_rootpackage_root_basebinarypossible_paths       r   
guess_pathz'_set_spawn_exe_path.<locals>.guess_pathb   s     
	#(C  HHQK(H!):):<)HIM*12.66sCC%6%+-3]C99]BGG,
 )D	#r   org_tensorflow	org_kerasNz:Cannot determine binary path. sys.argv[0]=%s os.environ=%szCannot determine binary path)r
   r   endswithr   errorrD   rE   RuntimeErrormultiprocessingget_contextset_executable)rP   rG   s     r   _set_spawn_exe_pathrY   W   s     	XXa[% $ &'D$d|mm
F
((1+rzz# 788CHHQK..sxx{;r   c                     dt         j                  dd v xrB t         j                  t         j                  j                  d      dz      j                  d      } | syt         j                  t         j                  j                  d      dz      }t         j                  dd t         _        t	        |       t        j
                  d       y)zDIf spawned process, run requested spawn task and exit. Else a no-op.z-cr   Nzfrom multiprocessing.r   )r
   r   indexr   execexit)
is_spawnedcmds     r   _if_spawn_run_and_exitr`      s     $ @- &J'>?  

%)*# XXa]#( s)((1+r   c                      da dt        j                  d<   t               rt	                t                t        j                          y)z<Main function to be called within `__main__` of a test file.TtrueTF_FORCE_GPU_ALLOW_GROWTHN)_test_main_calledrD   rE   r   rY   r`   r   mainr   r   r   	test_mainrf      s5     ,2"**()] ))+r   c                      t         S )z*Returns whether the module is initialized.)rd   r   r   r   initializedrh      s    	r   )r0   rV   rD   r   r
   r;   abslr   r   tensorflow.python.eagerr   r   r   r   contextForkServerProcessr3   ForkServerContextr5   r8   objectrd   rY   r`   rf   rh   r   r   r   <module>ro      s    )  	  
    (7
!
( 
( =l-55GG$o55GG $ *+/##'P P  '<T4r   