o
    ?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.   Zbazelr   )lensysargv r	   r	   o/home/www/facesmatcher.com/pyenv/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)Z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 )AbslForkServerContextZabsl_forkserverN)r(   r)   r*   _namer-   Processr	   r	   r	   r
   r.   B   s    r.   c                   @   s   e Zd ZdZdd ZdS )r0   z7A process that skips test (until windows is supported).c                 O   s   ~~t d)NzBTODO(b/150264776): Windows is not supported in MultiProcessRunner.)unittestZSkipTestr   r	   r	   r
   r   N   s   zProcess.__init__N)r(   r)   r*   r+   r   r	   r	   r	   r
   r0   K   s    r0   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   ZTEST_TARGET   :/r   zGuessed test binary path: %s)r   r   rfindosenvironreplacepathjoinr   infoaccessX_OK)Zpackage_rootZpackage_root_basebinaryZpossible_pathr	   r	   r
   
guess_pathb   s   
z'_set_spawn_exe_path.<locals>.guess_pathZorg_tensorflowZ	org_kerasNz:Cannot determine binary path. sys.argv[0]=%s os.environ=%szCannot determine binary path)r   r   endswithr   errorr6   r7   RuntimeErrormultiprocessingZget_contextZset_executable)r?   r9   r	   r	   r
   _set_spawn_exe_pathW   s   

rD   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)Z
is_spawnedcmdr	   r	   r
   _if_spawn_run_and_exit   s   rI   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trueZTF_FORCE_GPU_ALLOW_GROWTHN)_test_main_calledr6   r7   r   rD   rI   r   mainr	   r	   r	   r
   	test_main   s   
rM   c                   C   s   t S )z*Returns whether the module is initialized.)rK   r	   r	   r	   r
   initialized   s   rN   )r+   rC   r6   r   r   r1   Zabslr   r   Ztensorflow.python.eagerr   r   r   r   contextZForkServerProcessr-   ZForkServerContextr.   r0   objectrK   rD   rI   rM   rN   r	   r	   r	   r
   <module>   s2   	*