o
    i 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__ J/var/www/myenv/lib/python3.10/site-packages/keras/src/dtensor/test_util.pyr	      s   zDTensorBaseTest.setUpClassc                    s   t    t  t  d S r   )r   tearDownr   
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   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)jobr   device_index)r   
DeviceSpec).0ir   r   r   
<listcomp>T   s    z'create_device_array.<locals>.<listcomp>)r#   r$   asarrayrangereshape)r%   r   device_countr   r4   r   create_device_arrayQ   s   

	r:   c                 C   s   t | |}t| S r   )r:   r#   raveltolist)r%   r   devicesr   r   r   create_device_list_   s   
r>   c                 C   s   t | }t || S r   )r#   r$   aranger8   )r%   r9   r   r   r   create_device_ids_arrayd   s   
r@   c                   C      t   d S r   )r   _reset_contextr   r   r   r   r   i      r   c                 C   s   |   dvrtd|  t  tjd}|   dkr3tj| }tj|d tjjtdg|  tj|d tj g|  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   z8resetting logical device for non-supported device type: r   r   r   )memory_limitN)	upperr   r   r   r    r!    set_logical_device_configurationLogicalDeviceConfiguration_DEFAULT_GPU_MEMORY_LIMIT)r   countcpusgpusr   r   r   r"   m   s4   
r"   c                   C   rA   r   )dtensor_api_resetr   r   r   r   r      rC   r   )r+   numpyr#   tensorflow.compat.v2compatv2r   absl.testingr   tensorflow.dtensor.pythonr   rL   tensorflow.python.eagerr   rH   testTestCaser   r:   r>   r@   r   r"   r   r   r   r   r   <module>   s   5(