
    6bi                         S r SSKrSSKJs  Jr  SSKJr  SSK	J
r  SSKJr  Sr " S S\R                  R                   \R                   5      rS	 rS
 rS rS rS rS rg)z&Keras utilities for DTensor unit test.    N)parameterized)api)context   c                   R   ^  \ rS rSrSr\U 4S j5       rU 4S jr\S 5       r	Sr
U =r$ )DTensorBaseTest   z8Provides comparison helper for dtensor vs local results.c                 *   > [         [        U ]  5         g N)superr   
setUpClass)cls	__class__s    X/home/james-whalen/.local/lib/python3.13/site-packages/tf_keras/src/dtensor/test_util.pyr   DTensorBaseTest.setUpClass   s    os.0    c                 `   > [         TU ]  5         [        R                  " 5         [	        5         g r   )r   tearDownr   
async_waitreset_dtensor)selfr   s    r   r   DTensorBaseTest.tearDown#   s!     	r   c                 x  ^  [        5         U 4S jnSn[        R                  R                  S5      (       a7  U" S5      n[	        S[
        R                  " UR                  5       5      5        O6U" S5      n[	        S[
        R                  " UR                  5       5      5        [        R                  " 5         U$ )an  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                 Z   > TR                  U S 5      nUc  U n[        SU SU S35      eU$ )NzRequires a z mesh to run test on .)get
ValueError)device_typemeshdtdevice_type_mesh_maps      r   get_mesh0DTensorBaseTest.configTestMesh.<locals>.get_mesh>   s?    '++K>D|  ;rd2Gt1!MNNKr   NGPUCPU)
reset_contexttfconfiglist_physical_devicesreset_logical_devicesnpprodshaper   ensure_initialized)r!   r"   r   s   `  r   configTestMeshDTensorBaseTest.configTestMesh,   s      		 99**511E?D!%)>?E?D!%)>?""$r    )__name__
__module____qualname____firstlineno____doc__classmethodr   r   staticmethodr/   __static_attributes____classcell__)r   s   @r   r   r      s1    B1 1 ! !r   r   c                     [         R                  " U 5      n[         R                  " [        U5       Vs/ s H  n[        R
                  " SUUS9PM     sn5      R                  U 5      $ s  snf )Nzlocalhost/replica:0/task:0)jobr   device_index)r+   r,   asarrayranger'   
DeviceSpecreshape)r-   r   device_countis       r   create_device_arrayrD   Q   sh    775>L:: <(	
 ) MM0'
 )	
	 gen		
s    A*c                 `    [        X5      n[        R                  " U5      R                  5       $ r   )rD   r+   raveltolist)r-   r   devicess      r   create_device_listrI   _   s$    !%5G88G##%%r   c                 x    [         R                  " U 5      n[         R                  " U5      R                  U 5      $ r   )r+   r,   arangerA   )r-   rB   s     r   create_device_ids_arrayrL   d   s)    775>L99\"**511r   c                  .    [         R                  " 5         g r   )r   _reset_contextr1   r   r   r&   r&   i   s    r   c                    U R                  5       S;  a  [        SU  35      e[        5         [        R                  R                  S5      nU R                  5       S:X  ae  [        R                  R                  U 5      n[        R                  R                  US   [        R                  R                  [        S9/U-  5        [        R                  R                  US   [        R                  R                  5       /U-  5        g)a:  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gpuss       r   r*   r*   m   s     .0Fm
 	
 O99**51De#yy..{;
		22G		44!: 5 
 	
 II..QII002	
 	r   c                  .    [         R                  " 5         g r   )dtensor_api_resetr1   r   r   r   r      s    r   )r6   numpyr+   tensorflow.compat.v2compatv2r'   absl.testingr   tensorflow.dtensor.pythonr   rY   tensorflow.python.eagerr   rT   testTestCaser   rD   rI   rL   r&   r*   r   r1   r   r   <module>rd      sb    -  ! ! & 9 + 2bgg&&(>(> 2j&
2
%Pr   