o
    i e                     @   s   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d Zdd	 ZG d
d dZe rVG dd deejjZG dd dejjZe ZejZnG dd deZdadd Zdd Zdd Zdd ZdS )z"Library for multi-process testing.    N)app)logging)testc                   C   s   t tjdkodtjd v S )z*Returns whether the test is run under OSS.   bazelr   )lensysargv r
   r
   ]/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/distribute/multi_process_lib.pyis_oss   s   r   c                  C   sB   dd t jD } t r| rdS t jdkrt dkrdS t jdkS )Nc                 S   s   g | ]	}| d r|qS )z--tpu)
startswith).0argr
   r
   r   
<listcomp>$   s    z_is_enabled.<locals>.<listcomp>F)      Linuxwin32)r   r	   r   version_infoplatformsystem)tpu_argsr
   r
   r   _is_enabled!   s   

r   c                       s(   e Zd ZdZ fddZdd Z  ZS )_AbslProcessz'A process that runs using absl.app.run.c                    s.   t t| j|i | t| d| _| j| _d S )Nrun)superr   __init__getattr	_run_impl_run_with_abslr   selfargskwargs	__class__r
   r   r   /   s   z_AbslProcess.__init__c                    s   t  fdd d S )Nc                    s      S )N)r   )_r"   r
   r   <lambda>6   s    z-_AbslProcess._run_with_absl.<locals>.<lambda>)r   r   r(   r
   r(   r   r    5   s   z_AbslProcess._run_with_absl)__name__
__module____qualname____doc__r   r    __classcell__r
   r
   r%   r   r   ,   s    r   c                   @   s   e Zd ZdZdS )AbslForkServerProcessz^An absl-compatible Forkserver process.

    Note: Forkserver is not available in windows.
    N)r*   r+   r,   r-   r
   r
   r
   r   r/   ;   s    r/   c                   @   s   e Zd ZdZeZdS )AbslForkServerContextabsl_forkserverN)r*   r+   r,   _namer/   Processr
   r
   r
   r   r0   B   s    r0   c                   @   s   e Zd ZdZdd ZdS )r3   z7A process that skips test (until windows is supported).c                 O   s   ~~t d)NzBTODO(b/150264776): Windows is not supported in MultiProcessRunner.)unittestSkipTestr!   r
   r
   r   r   N   s   zProcess.__init__N)r*   r+   r,   r-   r   r
   r
   r
   r   r3   K   s    r3   Fc                  C   st   t jd dr.dd } | d}|s| d}|du r)tdt jd tj td	|t jd< t	 
t jd  dS )
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                 S   s   dt jd v rD| t jd v rFt jd d t jd |  }tjd dd  ddd}tj|| |}t	d| t
|tjrB|S d S d S d S )	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_pathr
   r
   r   
guess_pathb   s   
z'_set_spawn_exe_path.<locals>.guess_pathorg_tensorflow	org_kerasNz:Cannot determine binary path. sys.argv[0]=%s os.environ=%szCannot determine binary path)r   r	   endswithr   errorr;   r<   RuntimeErrormultiprocessingget_contextset_executable)rG   r>   r
   r
   r   _set_spawn_exe_pathW   s   

rP   c                  C   sr   dt jdd v ot jt jdd  d} | sdS t jt jdd  }t jdd t _t| t d dS )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cmdr
   r
   r   _if_spawn_run_and_exit   s   rV   c                   C   s,   da dtjd< t rt  t  t  dS )z<Main function to be called within `__main__` of a test file.TtrueTF_FORCE_GPU_ALLOW_GROWTHN)_test_main_calledr;   r<   r   rP   rV   r   mainr
   r
   r
   r   	test_main   s   
r[   c                   C   s   t S )z*Returns whether the module is initialized.)rY   r
   r
   r
   r   initialized   s   r\   )r-   rM   r;   r   r   r4   abslr   r   tensorflow.python.eagerr   r   r   r   contextForkServerProcessr/   ForkServerContextr0   r3   objectrY   rP   rV   r[   r\   r
   r
   r
   r   <module>   s2   	*