o
    2h|x                     @   sJ  d dl Z d dlZd dlZd dl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 G dd de	jejZdd Zdd Zdd Z dd Z!dd Z"dd Z#dd Z$d d! Z%d"d# Z&d$d% Z'd&d' Z(d(d) Z)dS )*    N)Path)parameterized)backend)distribution)ops)tree)utils)is_float_dtype)standardize_dtype)clear_sessionKerasTensor)Loss)Model)traceback_utilsc                       s   e Zd ZdZ fddZdd Zdd Zd"d	d
Zd"ddZd#ddZ	d$ddZ
d$ddZd%ddZd%ddZd$ddZdd Zd$ddZ																						d&d d!Z  ZS )'TestCaseNc                    s   t  j|i | d S N)super__init__selfargskwargs	__class__ V/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/testing/test_case.pyr      s   zTestCase.__init__c                 C   s"   t dd t rt  d S d S )NF)free_memory)r   r   is_traceback_filtering_enableddisable_traceback_filteringr   r   r   r   setUp   s   
zTestCase.setUpc                    s   t   |  fdd  S )Nc                      s
   t  S r   )shutilrmtreer   temp_dirr   r   <lambda>(      
 z'TestCase.get_temp_dir.<locals>.<lambda>)tempfilemkdtemp
addCleanupr    r   r$   r   get_temp_dir&   s   zTestCase.get_temp_dirư>c                 C   sF   t |tjst|}t |tjst|}tjj|||||d d S )N)atolrtolerr_msg)
isinstancenpndarrayr   convert_to_numpytestingassert_allcloser   x1x2r-   r.   msgr   r   r   assertAllClose+   s
   

zTestCase.assertAllClosec                 C   sJ   z| j |||||d W n
 ty   Y d S w |pd}td| d| )N)r-   r.   r9    z+The two values are close at all elements. 
z
.
Values: )r:   AssertionErrorr6   r   r   r   assertNotAllClose2   s   zTestCase.assertNotAllClose   c                 C   sL   |pd}t |tjst|}t |tjst|}tjj||||d d S )Nr;   )decimalr/   )r0   r1   r2   r   r3   r4   assert_almost_equal)r   r7   r8   r?   r9   r   r   r   assertAlmostEqual<   s   

zTestCase.assertAlmostEqualc                 C   s   | j t|t||d t||D ]-\}}t|ttfs#t|ttfr,| j|||d qt|}t|}| j |||d qd S Nr9   )	assertEquallenzipr0   listtupleassertAllEqualr   r3   )r   r7   r8   r9   e1e2r   r   r   rI   D   s   

zTestCase.assertAllEqualc                 C   s   | j t|||d d S rB   )rD   rE   )r   iterableexpected_lenr9   r   r   r   	assertLenN   s   zTestCase.assertLenTc                 C   s   t |tr| |j| d S t dkr,dd l}|r#| ||j d S | ||j d S t dkrOdd l	m
  m} |rF| ||j d S | ||j d S | |dt  d d S )N
tensorflowr   jaxBackend z  does not support sparse tensors)r0   r   rD   sparser   rO   assertIsInstanceSparseTensorassertNotIsInstancejax.experimental.sparseexperimental	JAXSparseassertFalse)r   xrR   tf
jax_sparser   r   r   assertSparseQ   s    
zTestCase.assertSparsec                 C   st   t |tr| |j| d S t dkr,dd l}|r#| ||j d S | ||j d S | 	|dt  d d S )NrO   r   rQ   z  does not support ragged tensors)
r0   r   rD   raggedr   rO   rS   RaggedTensorrU   rY   )r   rZ   r^   r[   r   r   r   assertRaggedh   s   
zTestCase.assertRaggedc                 C   s\   t |drt|j}ntt|}t|}d| d| }|p#|}| j|||d d S )NdtypezBThe dtype of x does not match the expected one. Received: x.dtype=z and dtype=rC   )hasattrr   r
   ra   typerD   )r   rZ   ra   r9   x_dtypestandardized_dtypedefault_msgr   r   r   assertDTypex   s   

zTestCase.assertDTypec                 C   s    t | std| dd S )NzFile z does not exist)r   is_filer<   )r   pathr   r   r   assertFileExists   s   zTestCase.assertFileExistsc                 C   sX  ddl m} ddl m} ddl m} |j}| }t|}t|d d  }	|| ||}
W d    n1 s9w   Y  |
 }t|}| 	|| | 	t
|	t
t|
 ||}t|}|| |t|}
W d    n1 suw   Y  |
 }t|}| 	|| t|
d d  }|	|fD ]}d|v r|d q| 	t
|	t
| |
S )Nr   )custom_object_scope)deserialize_keras_object)serialize_keras_object__annotations__)keras.src.savingrk   rl   rm   r   
get_configto_json_with_tuplesdirfrom_configrD   setfrom_json_with_tuplesremove)r   instancecustom_objectsrk   rl   rm   clsconfigconfig_jsonref_dirrevived_instancerevived_configrevived_config_json
serializedserialized_jsonnew_dirlstr   r   r   run_class_serialization_test   s>   


z%TestCase.run_class_serialization_testFc           )   	      s  |dur|durt ddurdurt ddur$|du r$t d|dur0|dur0t d|p3i }|p7i }|dur|durt|tr^t|d r^|t jt|t|dd	 n=t|trz|t jt|	 t|	 dd	 n!t|t
r|t
 jt|t|dd	 n	t|tst d
|dur|du rtdd |}|dus|dur|du rt||
}|d$i |t|trՈd$i || n|fi | du rtjdu rtjdu rtjdu rtt|d$i || |durjj|dd	 fdd}d% 	fdd	}fdd}|dusE|dur|du rStdd |}n|}|d$i |t|trkjd$i | n| | |du rt
fdd|}nt||
}|d$i |t|trd$i ||}n|fi |}| ||dd |durǈ|}||j |r|d$i |jjt dd	 G dd d|} | d$i |jjdt dd	 |dus|dur|du rt||}|d$i |t|tr'd$i ||}!n|fi |}!||!dd |r?|||! |rRt dkrRddl}"|"j  }|r|d$i i |dditfd d|}#t|trd$i ||}!j!d$i |#}$n|fi |}!!|#}$t"t#|!t#|$D ]\}%}&t$|%j%}'|'|&j%d!|&j% d"|'  qj&D ]}(t$|(j%}'t'|'rˈ|'d# qdS dS dS )&a{
  Run basic checks on a layer.

        Args:
            layer_cls: The class of the layer to test.
            init_kwargs: Dict of arguments to be used to
                instantiate the layer.
            input_shape: Shape tuple (or list/dict of shape tuples)
                to call the layer on.
            input_dtype: Corresponding input dtype.
            input_sparse: Whether the input is a sparse tensor (this requires
                the backend to support sparse tensors).
            input_ragged: Whether the input is a ragged tensor (this requires
                the backend to support ragged tensors).
            input_data: Tensor (or list/dict of tensors)
                to call the layer on.
            call_kwargs: Dict of arguments to use when calling the
                layer (does not include the first input tensor argument)
            expected_output_shape: Shape tuple
                (or list/dict of shape tuples)
                expected as output.
            expected_output_dtype: dtype expected as output.
            expected_output_sparse: Whether the output is expected to be sparse
                (this requires the backend to support sparse tensors).
            expected_output_ragged: Whether the output is expected to be ragged
                (this requires the backend to support ragged tensors).
            expected_output: Expected output tensor -- only
                to be specified if input_data is provided.
            expected_num_trainable_weights: Expected number
                of trainable weights of the layer once built.
            expected_num_non_trainable_weights: Expected number
                of non-trainable weights of the layer once built.
            expected_num_seed_generators: Expected number of
                SeedGenerators objects of the layer once built.
            expected_num_losses: Expected number of loss tensors
                produced when calling the layer.
            supports_masking: If True, will check that the layer
                supports masking.
            expected_mask_shape: Expected mask shape tuple
                returned by compute_mask() (only supports 1 shape).
            custom_objects: Dict of any custom objects to be
                considered during deserialization.
            run_training_check: Whether to attempt to train the layer
                (if an input shape or input data was provided).
            run_mixed_precision_check: Whether to test the layer with a mixed
                precision dtype policy.
            assert_built_after_instantiation: Whether to assert `built=True`
                after the layer's instantiation.
        Nz=input_shape and input_data cannot be passed at the same time.zLexpected_output_shape and expected_output cannot be passed at the same time.z=In order to use expected_output, input_data must be provided.TzCIn order to use expected_mask_shape, supports_masking must be True.r   z4The number of input shapes and dtypes does not matchrC   z(The type of input_shape is not supportedc                 S   s   dS )Nfloat32r   )_r   r   r   r&   )  s    z)TestCase.run_layer_test.<locals>.<lambda>z!Unexpected supports_masking valuec                    s    | j d urj| jdd d ur j| jdd  d ur-j| j dd d ur;jt| dd t dkr\d ur^d ur`d urbj| j  dd d S d S d S d S d S )Nz&Unexpected number of trainable_weightsrC   z*Unexpected number of non_trainable_weightsz,Unexpected number of non_trainable_variablesz$Unexpected number of seed_generatorstorchz!Unexpected number of torch_params)	
assertTruebuiltrN   trainable_weightsnon_trainable_weightsnon_trainable_variablesget_seed_generatorsr   torch_paramslayer)$expected_num_non_trainable_variables"expected_num_non_trainable_weightsexpected_num_seed_generatorsexpected_num_trainable_weightsr   r   r   run_build_assertsS  sT   
z2TestCase.run_layer_test.<locals>.run_build_assertsFc           
   	      sL  d ur(dd }t |||}jtt |d dt dd | d d urOdd	 }t ||}jtt |d
 dt dd | d r^t |D ]}| qVrmt |D ]}| qe|rd urt	t	| t
t t |D ]\}}	j||	dd q d ur| j  d S d S d S )Nc                 S   sH   |j }t|t| krdS t| |D ]\}}|d ur!||kr! dS qdS )NFT)shaperE   rF   )expected_shaperZ   r   expected_dimdimr   r   r   verify_shape~  s   zITestCase.run_layer_test.<locals>.run_output_asserts.<locals>.verify_shapezExpected output shapes z but received c                 S      | j S r   )r   rZ   r   r   r   r&         zETestCase.run_layer_test.<locals>.run_output_asserts.<locals>.<lambda>rC   c                 S   s   | t |jkS r   )r   r
   ra   )expected_dtyperZ   r   r   r   verify_dtype  s   zITestCase.run_layer_test.<locals>.run_output_asserts.<locals>.verify_dtypezExpected output dtypes c                 S   r   r   ra   r   r   r   r   r&     r   zUnexpected output value)r   map_structure_up_tor   allflattenmap_structurer]   r`   rD   rc   rF   r:   rN   losses)
r   outputeagerr   shapes_matchr   dtypes_matchrZ   ref_vv)expected_num_lossesexpected_outputexpected_output_dtypeexpected_output_raggedexpected_output_shapeexpected_output_sparser   r   r   run_output_asserts{  sR   		z3TestCase.run_layer_test.<locals>.run_output_assertsc                    s   G dd dt }|| }||f t dkrttj   fdd}G dd dt}d}t d	kr7r7d
}|jd| |d |j| ddd d S )Nc                       s&   e Zd Z fddZdddZ  ZS )zETestCase.run_layer_test.<locals>.run_training_step.<locals>.TestModelc                    s   t    || _d S r   )r   r   r   )r   r   r   r   r   r     s   

zNTestCase.run_layer_test.<locals>.run_training_step.<locals>.TestModel.__init__Fc                 S   s   | j ||dS )N)trainingr   )r   rZ   r   r   r   r   call  s   zJTestCase.run_layer_test.<locals>.run_training_step.<locals>.TestModel.callF)__name__
__module____qualname__r   r   __classcell__r   r   r   r   	TestModel  s    r   r   c                   3   s    	  V  qr   r   r   datar   r   data_generator  s   zJTestCase.run_layer_test.<locals>.run_training_step.<locals>.data_generatorc                   @   s   e Zd ZdddZdS )zDTestCase.run_layer_test.<locals>.run_training_step.<locals>.TestLossNc                 S   s
   t |S r   )r   sum)r   y_truey_predsample_weightr   r   r   __call__  s   
zMTestCase.run_layer_test.<locals>.run_training_step.<locals>.TestLoss.__call__r   )r   r   r   r   r   r   r   r   TestLoss  s    r   autorO   Fsgd)	optimizerlossjit_compile   r   )steps_per_epochverbose)r   r   r   r   r3   r   compilefit)r   
input_dataoutput_datar   modelr   r   r   )input_sparser   r   run_training_step  s   z2TestCase.run_layer_test.<locals>.run_training_stepc                 S   s
   t | S r   )r   r   r   r   r   r   r&     r'   c                    s   t t| | j S r   )create_keras_tensorsr   r   ra   r   )input_raggedr   r   r   r&     s    )r   z9 is stateless, so it should be built after instantiation.c                   @   s   e Zd Zdd ZdS )z3TestCase.run_layer_test.<locals>.ModifiedBuildLayerc                 _   s   d S r   r   r   r   r   r   build  s   z9TestCase.run_layer_test.<locals>.ModifiedBuildLayer.buildN)r   r   r   r   r   r   r   r   ModifiedBuildLayer  s    r   zThe `build` of z= is overriden, so it should not be built after instantiation.r   ra   mixed_float16c                    s,   t | j jrt| jr jdS | jdS )Nr   )r   r   autocastr   r	   ra   compute_dtype)specr   r   r   r&   8  s    
zexpected output dtype z, got r   r   r   )(
ValueErrorr0   rH   is_shape_tuplerS   rD   rE   dictrt   keysrG   r   map_shape_structurecreate_eager_tensorsr   r   r   r   r   supports_maskingr   r   r   compute_maskr   r   r   rc   rY   r   r   cudais_availablecompute_output_specrF   r   r
   ra   weightsr	   ))r   	layer_clsinit_kwargsinput_shapeinput_dtyper   r   r   call_kwargsr   r   r   r   r   r   r   r   r   r   r   expected_mask_shaperx   run_training_checkrun_mixed_precision_check assert_built_after_instantiationr   r   r   build_shapekeras_tensor_inputskeras_tensor_outputsoutput_maskr   r   r   
input_specoutput_spectensorr   ra   weightr   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   run_layer_test   sN  K








(3.




	


	




zTestCase.run_layer_test)r,   r,   N)r>   Nr   )T)NNFFNNNNFFNNNNNNNNNTTF)r   r   r   maxDiffr   r!   r+   r:   r=   rA   rI   rN   r]   r`   rg   rj   r   r   r   r   r   r   r   r      sL    










&r   c                   C      t   dkot S )NrO   r   uses_gpur   r   r   r   tensorflow_uses_gpuX     r   c                   C   r   )NrP   r   r   r   r   r   jax_uses_gpu\  r   r   c                  C   s&   t   dkrdS ddlm}  |  dkS )Nr   Fr   
get_devicer   )r   keras.src.backend.torch.corer  r   r   r   r   torch_uses_gpu`  s   
r  c                  C   "   t  } tdd | D rdS dS )Nc                 s       | ]}| d V  qdS )gpuN
startswith.0dr   r   r   	<genexpr>k      zuses_gpu.<locals>.<genexpr>TFr   list_devicesanydevicesr   r   r   r   h  s   r   c                  C   r  )Nc                 s   r  )cpuNr  r	  r   r   r   r  r  r  zuses_cpu.<locals>.<genexpr>TFr  r  r   r   r   uses_cpup  s   r  c                    s:   t | tr fdd|  D S tfdd|  S )Nc              	      s.   i | ]\}}t |d t| | dqS )_shapera   rR   r^   )r   removesuffixr   r
  kr   ra   r^   rR   r   r   
<dictcomp>y  s    z(create_keras_tensors.<locals>.<dictcomp>c                    s   t | | dS )Nr  r   r   dt)r^   rR   r   r   r&     s    z&create_keras_tensors.<locals>.<lambda>)r0   r   itemsmap_shape_dtype_structurer   ra   rR   r^   r   r  r   r   w  s   
r   c                    s  ddl m ttg drtd |rKt dkr,dd lfdd nAt dkrBdd l	m
  m fd	d n+td
t  |rgt dkr^dd lfdd ntdt  fdd t| tr~ fdd|  D S t | S )Nr   random)float16r   float64int8uint8int16uint16int32uint32int64uint64z=dtype must be a standard float or int dtype. Received: dtype=rO   c                    sB   t jd}d||  |}t ||| dk } j|S )Nr      ffffff?)r1   r"  default_rngstandard_normalastypemultiplyrR   
from_denser   r  rngrZ   r[   r   r   	create_fn  s   z'create_eager_tensors.<locals>.create_fnrP   c                    sF   t jd}d||  |}t ||| dk } jj|ddS )Nr   r-  r.  r   )n_batch)r1   r"  r/  r0  r1  r2  BCOO	fromdenser4  )r\   r   r   r7       z#Sparse is unsupported with backend c                    sF   t jd}d||  |}t ||| dk } jj|ddS )Nr   r-  r.  )padding)r1   r"  r/  r0  r1  r2  r_   from_tensorr4  r6  r   r   r7    r;  z#Ragged is unsupported with backend c                    s   t j j| ddd |dS )Nr   r   r>   )r   castuniformr  r!  r   r   r7    s   c                    s(   i | ]\}}t |d  || qS )r  )r   r  r  )r7  ra   r   r   r    s    z(create_eager_tensors.<locals>.<dictcomp>)keras.src.backendr"  rt   r   r   
differencer   r   rO   rV   rW   rR   r0   r   r  r  r   r   )r7  ra   r\   r"  r[   r   r     s@   
r   c                 C   s    t | ttfotdd | D S )Nc                 s   s"    | ]}t |ttd fV  qd S r   )r0   intrc   r
  er   r   r   r    s    
z!is_shape_tuple.<locals>.<genexpr>)r0   rG   rH   r   r   r   r   r   r     s   r   c                    s   t |rt| S t|trfddt| D S t|tr/tfddt| D S t|tr@ fdd| D S td| d  )	z<Variant of tree.map_structure that operates on shape tuples.c                    s   g | ]
\}}t  ||qS r   r  r
  sr  fnr   r   
<listcomp>  s    z-map_shape_dtype_structure.<locals>.<listcomp>c                 3   s     | ]\}}t  ||V  qd S r   rE  rF  rH  r   r   r    s    
z,map_shape_dtype_structure.<locals>.<genexpr>c                    s"   i | ]\}}|t | | qS r   rE  r  ra   rI  r   r   r    s    z-map_shape_dtype_structure.<locals>.<dictcomp>z'Cannot map function to unknown objects z and )r   rH   r0   rG   rF   r   r  r   )rI  r   ra   r   rK  r   r    s"   



r  c                 C   sP   g }t  }| ddD ]}|jD ]}t||vr$|| |t| qq|S )z;Get a List of all seed generators in the layer recursively.T)rt   _flatten_layers_seed_generatorsidappendadd)r   seed_generatorsseen_idssublayersgr   r   r   r     s   

r   c                    s4    fdd G  fdddt j}|ddd| S )Nc                    sX   t | trdt| dS t | tr fdd| D S t | tr* fdd|  D S | S )NrH   )r   	__value__c                    s   g | ]} |qS r   r   rC  _tuple_encoder   r   rJ    s    z>to_json_with_tuples.<locals>._tuple_encode.<locals>.<listcomp>c                    s   i | ]	\}}| |qS r   r   )r
  keyvaluerV  r   r   r    s    z>to_json_with_tuples.<locals>._tuple_encode.<locals>.<dictcomp>)r0   rH   rG   r   r  objrV  r   r   rW    s   


z*to_json_with_tuples.<locals>._tuple_encodec                       s   e Zd Z fddZ  ZS )z6to_json_with_tuples.<locals>._PreserveTupleJsonEncoderc                    s   |}t  |S r   )r   encode)r   r[  )r   rW  r   r   r\  	  s   z=to_json_with_tuples.<locals>._PreserveTupleJsonEncoder.encode)r   r   r   r\  r   r   rV  r   r   _PreserveTupleJsonEncoder  s    r]  Tr-  )	sort_keysindent)jsonJSONEncoderr\  )rY  r]  r   rV  r   rq     s   	rq   c                 C   s   dd }t j| |dS )Nc                 S   s.   t | ts| S d| vsd| vr| S t| d S )Nr   rU  )r0   r   rH   rZ  r   r   r   _tuple_decode  s
   
z,from_json_with_tuples.<locals>._tuple_decode)object_hook)r`  loads)rY  rb  r   r   r   ru     s   ru   )*r`  r"   r(   unittestpathlibr   numpyr1   absl.testingr   	keras.srcr   r   r   r   r   keras.src.backend.commonr	   r
   %keras.src.backend.common.global_stater   %keras.src.backend.common.keras_tensorr   keras.src.losses.lossr   keras.src.modelsr   keras.src.utilsr   r   r   r   r  r   r  r   r   r   r  r   rq   ru   r   r   r   r   <module>   sH        DM