o
    Me                     @   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__ GD:\Projects\ConvertPro\env\Lib\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                    s2     | d }|d u r| }td| d| d|S )NzRequires a z mesh to run test on .)get
ValueError)device_typemeshdt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>T   s    z'create_device_array.<locals>.<listcomp>)r"   r#   Zasarrayrangereshape)r$   r   device_countr   r/   r   create_device_arrayQ   s   

	r4   c                 C   s   t | |}t| S r   )r4   r"   Zraveltolist)r$   r   devicesr   r   r   create_device_list_   s   
r7   c                 C   s   t | }t || S r   )r"   r#   Zaranger2   )r$   r3   r   r   r   create_device_ids_arrayd   s   
r8   c                   C      t   d S r   )r   Z_reset_contextr   r   r   r   r   i      r   c                 C   s   t   tj| }|  dkr tj|d tj g|  dS |  dkr9tj|d tjjtdg|  dS | }td| )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_limitz8resetting logical device for non-supported device type: N)	r   r   r   r    upperZ set_logical_device_configurationZLogicalDeviceConfiguration_DEFAULT_GPU_MEMORY_LIMITr   )r   countr6   r   r   r   r   r!   m   s0   
r!   c                   C   r9   r   )dtensor_api_resetr   r   r   r   r      r:   r   )r)   numpyr"   Ztensorflow.compat.v2compatv2r   Zabsl.testingr   Ztensorflow.dtensor.pythonr   r>   Ztensorflow.python.eagerr   r<   testZTestCaser   r4   r7   r8   r   r!   r   r   r   r   r   <module>   s   5&