a
    +=ic                     @   s   d Z ddlZddlm  mZ ddlmZ ddl	m
Z ddlmZ dZG dd dejjejZd	d
 Zdd Zdd Zdd Zdd Zdd ZdS )z&Keras utilities for DTensor unit test.    N)parameterized)api)context   c                       s<   e Zd ZdZe fddZ fddZedd Z  Z	S )DTensorBaseTestz8Provides comparison helper for dtensor vs local results.c                    s   t t|   d S N)superr   
setUpClass)cls	__class__ h/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/keras/dtensor/test_util.pyr	      s    zDTensorBaseTest.setUpClassc                    s   t    t  t  d S r   )r   tearDownr   Z
async_waitreset_dtensor)selfr   r   r   r   #   s    
zDTensorBaseTest.tearDownc                    sh   t    fdd}d}tjdr@|d}tdt|  n|d}tdt|  t	  |S )a  Configs corresponding mesh given test context.

        If runs on a CPU mesh, set virtual device on CPU.
        If runs on a GPU mesh, sets virtual device on GPU with proper memory
        limits.
        if runs on a TPU mesh, initializes TPU system.

        Args:
          device_type_mesh_map: A dictionary containing device_type -> mesh
            mapping.

        Returns:
          A properly configured mesh for use in test.
        c                    s(     | d }|d u r$td| | f |S )Nz%Requires a %s mesh to run test on %s.)get
ValueError)device_typemeshdevice_type_mesh_mapr   r   get_mesh>   s    z0DTensorBaseTest.configTestMesh.<locals>.get_meshNGPUCPU)
reset_contexttfconfiglist_physical_devicesreset_logical_devicesnpprodshaper   Zensure_initialized)r   r   r   r   r   r   configTestMesh,   s    	zDTensorBaseTest.configTestMesh)
__name__
__module____qualname____doc__classmethodr	   r   staticmethodr#   __classcell__r   r   r   r   r      s   	r   c                    s,   t | }t  fddt|D | S )Nc                    s   g | ]}t jd  |dqS )zlocalhost/replica:0/task:0)Zjobr   Zdevice_index)r   Z
DeviceSpec).0ir   r   r   
<listcomp>V   s   z'create_device_array.<locals>.<listcomp>)r    r!   Zasarrayrangereshape)r"   r   device_countr   r-   r   create_device_arrayS   s    

	r2   c                 C   s   t | |}t| S r   )r2   r    Zraveltolist)r"   r   devicesr   r   r   create_device_lista   s    
r5   c                 C   s   t | }t || S r   )r    r!   Zaranger0   )r"   r1   r   r   r   create_device_ids_arrayf   s    
r6   c                   C   s   t   d S r   )r   Z_reset_contextr   r   r   r   r   k   s    r   c                 C   s~   t   tj| }|  dkr>tj|d tj g|  n<|  dkrntj|d tjjtdg|  ntd|  dS )aV  Resets logical devices for CPU/GPU.

    Logical devices can only be instantiated once on a particular context. For
    now, context re-use is triggering some function duplication errors, so we
    reset the context on each call.

    Args:
      device_type: The device_type to reset.
      count: numbers of virtual device to reset to.
    r   r   r   )Zmemory_limitz;resetting logical device for non-supported device type : %sN)	r   r   r   r   upperZ set_logical_device_configurationZLogicalDeviceConfiguration_DEFAULT_GPU_MEMORY_LIMITr   )r   countr4   r   r   r   r   o   s2    
r   c                   C   s   t   d S r   )dtensor_api_resetr   r   r   r   r      s    r   )r'   numpyr    Ztensorflow.compat.v2compatv2r   Zabsl.testingr   Ztensorflow.dtensor.pythonr   r:   Ztensorflow.python.eagerr   r8   testZTestCaser   r2   r5   r6   r   r   r   r   r   r   r   <module>   s   7&