
    AVha9                        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ZddlmZ ddl	m
Z
 ddlZddlmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z# ddl$mZ% ddl$m&Z' ddl(m)Z) ddl*m+Z, ddl-m.Z.  e!j^                          dZ0dZ1d Z2d Z3d Z4d Z5d Z6d Z7d  Z8d! Z9ejt                  Z: G d" d#e,jv                  e
jv                        Z<d$ Z=d% Z>g d&Z?y)'z$Utility methods for DTensor testing.    N)flags)parameterized)accelerator_util)api)config)layout)
numpy_util)is_gpu_present)is_tpu_present)preferred_device_type)use_multi_device_mode)test_backend_util)DTENSOR_TEST_UTIL_BACKEND)DTensorTestUtilBackend)DTensorTestBackendConfigurator)	v2_compat)context)device)resource_variable_ops)test)numpy_compatgh㈵>i   c                 V    | dk(  xr# dt         j                  j                  dd      k7  S )zReturns True when device_type is TPU and environment variable is set.

  Args:
    device_type: A str representing the type of device on the mesh.

  Returns:
    bool: True when device_type is TPU and environment variable is set.
  TPU0DTENSOR_TEST_USE_XLA_SPMD)osenvirongetdevice_types    Y/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/dtensor/python/tests/test_util.pyget_use_xla_spmdr"   =   s1     
	 
#!3* #     c                 t    t        j                  |       }t        j                  |      j                  |       S N)npprodarangereshape)shapedevice_counts     r!   create_device_ids_arrayr,   K   s)    ,	<	 	(	(	//r#   c           
          t        j                  |       }t        j                  t	        |      D cg c]  }t        j                  d||       c}      j                  |       S c c}w )Nzlocalhost/replica:0/task:0)jobr    device_index)r&   r'   r   
np_asarrayrange	tf_device
DeviceSpecr)   )r*   r    r+   is       r!   create_device_arrayr5   P   se    ,		 	  $)#6	"   *!" 

 WU^ "s   A'c                 `    t        | |      }t        j                  |      j                         S r%   )r5   r&   raveltolist)r*   r    devicess      r!   create_device_listr:   Z   s&    {3'	'		!	!	##r#   c                  ,    t        j                          y r%   )r   _reset_context r#   r!   reset_contextr>   _   s    	r#   c                 j   t                t        j                  |       }| j                         dvrt	        d| z        |t        |      k  r*|d| }t        j                  || j                                t        |      D ]  \  }}|dz   |z  t        |      z  ||z  t        |      z  z
  }|dkD  sJ g }t        |      D ]V  }| j                         dk(  rt        j                  t        |      }nt        j                         }|j                  |       X t        j                  ||        y)	aH  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.
  )CPUGPUz;resetting logical device for non-supported device type : %sNr      r   rA   )memory_limitexperimental_device_ordinal)r>   	tf_configlist_physical_devicesupper
ValueErrorlenset_visible_devices	enumerater1   r   LogicalDeviceConfiguration_DEFAULT_GPU_MEMORY_LIMITappend set_logical_device_configuration)	r    countr9   r4   r   nconfigsordinal
dev_configs	            r!   reset_logical_devicesrU   c   s0    /++K8'.
 '( ) ) S\fuoG!!'{7H7H7JKW% @ia	
Q%3w<'!e)s7|*CCAq5L5G8 !					%772(/1
 779
nnZ ! ..vw?@r#   c                 n    t        j                  |       }d }|D cg c]  } ||      s| c}S c c}w )z'Returns a list of local logial devices.c                    t         j                  j                  | j                        }|j                  |j                  dk(  ry|j                  t        j                         k(  r"|j                  t        j                         k(  ryy)N	localhostTF)	r2   r3   from_stringnamer.   r   job_nametask	client_id)r   specs     r!   is_localz,list_local_logical_devices.<locals>.is_local   sa    ++FKK8Dxx488{2	V__&	&4998H8H8J+Jr#   )rE   list_logical_devices)r    r9   r_   ds       r!   list_local_logical_devicesrb      s5    
 **;7' 	,!	,,	,s   22c                  *    t        j                         S r%   )r   is_tfrt_enabledr=   r#   r!   rd   rd      s    		 	 	""r#   c                       e Zd ZdZe fd       Z fdZ fdZede	j                  e	j                  ej                  f   dej                  fd       Z	 dde	j                  e   d	efd
Zd	efdZ fdZd	efdZefdZ xZS )DTensorBaseTestz8Provides comparison helper for dtensor vs local results.c                 *    t         t        |           y r%   )superrf   
setUpClass)cls	__class__s    r!   ri   zDTensorBaseTest.setUpClass   s    	/3*,r#   c                 B    t         |           t        |       | _        y r%   )rh   setUpr   _backend_configuratorselfrk   s    r!   rm   zDTensorBaseTest.setUp   s    	GMO!?!EDr#   c                    	 t        j                          t                | j                  j	                          t
        |           y # t                | j                  j	                          t
        |           w xY wr%   )r   
async_waitreset_dtensorrn   tearDownrh   ro   s    r!   rt   zDTensorBaseTest.tearDown   s^     o
  ))+g o
  ))+gs   A
 
4A>device_type_mesh_mapreturnc                     t                 fd}d}t               r( |d      }t                t        j                  d       nt	        j
                  d      rK |d      }t        dt        j                  |j                                      t        j                  d       nJ |d      }t        dt        j                  |j                                      t        j                  d       t        j                  |       |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                 T    j                  | d       }|t        d| d| d      |S )NzRequires a z mesh to run test on .)r   rH   )r    meshru   s     r!   get_meshz0DTensorBaseTest.configTestMesh.<locals>.get_mesh   s6    !%%k48d	%{4 5 	5kr#   Nr   rA   r@   )r>   r   r   initialize_accelerator_systemrE   rF   rU   r&   r'   r*   r   config_test_mesh)ru   r{   rz   s   `  r!   configTestMeshzDTensorBaseTest.configTestMesh   s      O De_do44U;		(	(	/e_dE2774::<#8944U;e_dE2774::<#8944U;&&t,Kr#   r    reasonc                    t        t        j                         D cg c]  }|j                   c}      }|D ]  }|dk(  rFt	               r<|| j                  |       n(t        t        |            |k7  r| j                  |       |dk(  r#t        |      dk(  rd|v r| j                  |       |dk(  s|d|v s| j                  |        yc c}w )a  Skip the test for the specific device_type.

    Args:
      device_type: list of device types, one of "CPU", "GPU", or "TPU".
      reason: string that describe the reason for skipping the test.
      unless_device_count_equals_to: Optional int. This parameter only works if
        device_type is "TPU". If set, the test will be skipped unless the number
        of TPUs equals to the specified count.
    r   Nr@   rB   rA   )setrE   rF   r    r   skipTestrI   rb   )rp   r    r   unless_device_count_equals_tora   physical_device_typesr   s          r!   skipForDeviceTypez!DTensorBaseTest.skipForDeviceType   s       ) ? ? AB1BD 	5^-(0
--
+ 56
--
	5S
!$%&*/3H*Hf	5U&;;f 	Cs   Cc                 <    t               r| j                  |       y y r%   )rd   r   rp   r   s     r!   skipForTfrtzDTensorBaseTest.skipForTfrt  s    
mmF r#   c                 p    t        | d      r| j                  j                          t        |   |       y )Nrn   )hasattrrn   rt   rh   r   )rp   r   rk   s     r!   r   zDTensorBaseTest.skipTest  s-    t,-
  ))+	GVr#   c                 P    t        j                         r| j                  |       y y r%   )r   backend_is_pwr   r   s     r!   skipForPathwayszDTensorBaseTest.skipForPathways  s    
mmF r#   c                    t        t        |      t        j                        r|j	                         }||j                         }t        j                  |      j                         }|j                  d      }|dk7  r0|j                  ||d       dk7  r|d| }|d|j                  d       }| j                  t        j                  |      |dj                  ||             t        j                  |      }t        j                  |      D 	cg c]  }	|	j                          }
}	| j                  |j                  |j                         t        j                   |      }| j                  |j"                  |j"                  |       | j%                  ||||       d }|j&                  j)                         }i }|j+                         D ]  \  }}|| ||      <    |j&                  j,                  D cg c]  }||j.                  vs| }}t1        |
      D ]T  \  }}||   }|D ]E  }||   dk7  st3        j4                  |      }d||<   | ||         }| j%                  ||
|          G V yc c}	w c c}w )	z+Asserts DTensor is of the particular value.Nzmesh:z:=======
expected layout is
  {}

while got layout is
  {}
)msg)atolrtolc                     t        j                  t        | j                         d             }t	        j
                  |      S )zHash key for Python dict.c                     | d   S )Nr   r=   )xs    r!   <lambda>zFDTensorBaseTest.assertDTensorEqual.<locals>.hash_key.<locals>.<lambda>Z  s
    AaD r#   )key)collectionsOrderedDictsorteditemsjsondumps)locra   s     r!   hash_keyz4DTensorBaseTest.assertDTensorEqual.<locals>.hash_keyW  s/     
!
!&."I
JaZZ]r#   r   )
issubclasstyper   BaseResourceVariablevalue	to_stringr   fetch_layoutfindassertEqualformatunpacknumpyassertAllEqualr*   r	   to_numpydtypeassertAllCloserz   unravel_indexr   	dim_namessharding_specsrK   copydeepcopy)rp   expected_resultexpected_layoutresult_dtensortolexpected_strgot_strindex_for_meshr   tunpackedr   offset_to_mesh_loc_dictmesh_loc_to_offset_dictoffsetr   r   replicated_dimstensormesh_locdim_shardings                        r!   assertDTensorEqualz"DTensorBaseTest.assertDTensorEqual  s    ^3HHJ%++-n" %..0l  0::<g#((1n	2	',,
~
'#),.#/ $O^407<<01



>
*
PvlG,   n-F#&::n#=>a	>H> 	--~/C/CD((8N
 	~33^
 	cL* %kk779 .446 6/5hsm,6
 ;;((AV5J5J,JO 
 $H- 	8(0h) 8,L!Q&]]8,(#$(<
 *8H+=>& 

fhv&6
78	8k ?`s   I9<I>I>r%   )__name__
__module____qualname____doc__classmethodri   rm   rt   staticmethodtypingDictText
layout_libMeshr~   Liststrr   r   r   r   DEFAULT_TOLr   __classcell__)rk   s   @r!   rf   rf      s    @- -F
 ("KKZ__(DE(( (\ %)	;;s# < C  b8r#   rf   c                      t        j                  |  D cg c]/  }dj                  d |D              gt        d |D        d      1 c}S c c}w )z)Makes a product of names parameters list. c              3   &   K   | ]	  }|d      yw)r   Nr=   .0ps     r!   	<genexpr>zproduct.<locals>.<genexpr>z  s       t    c              3   &   K   | ]	  }|d d   yw)rB   Nr=   r   s     r!   r   zproduct.<locals>.<genexpr>{  s      $2() %&abE $2r   r=   )	itertoolsproductjoinsum)listselts     r!   r   r   t  si     ""E*	
  
ww   8" $2-0$235 78
  
s   4Ac                  ,    t        j                          y)zuResets the singleton DTensor Device.

  This behavior is not generally exposed and only meant to be used in tests.
  N)r   _resetr=   r#   r!   rs   rs     s    
 **,r#   )r   r   r   r,   r5   r:   r>   rU   rb   rd   FLAGSrf   r   rs   r   r
   r   )@r   r   r   r   r   r   r   abslr   absl.testingr   r   r&   tensorflow.dtensor.pythonr   r   r   r   r   r	    tensorflow.dtensor.python.configr
   r   r   r   tensorflow.dtensor.python.testsr   1tensorflow.dtensor.python.tests.test_backend_namer   r   1tensorflow.dtensor.python.tests.test_backend_utilr   tensorflow.python.compatr   tensorflow.python.eagerr   tensorflow.python.frameworkrE   r   r2   tensorflow.python.opsr   tensorflow.python.platformr   tf_testtensorflow.python.utilr   enable_v2_behaviorr   rM   r"   r,   r5   r:   r>   rU   rb   rd   r   TestCaserf   r   rs   __all__r=   r#   r!   <module>r      s    +     	   &  7 ) , : 0 ; ; B B = W T \ . + ; ; 7 6 / 	     0
$
"@J-$# 	P8g&&(>(> P8f
r#   