o
    oiZ.                     @   s>  d Z ddlZddlZddlZddlmZmZ ddlm	Z	 ddl
mZ ddlmZmZmZmZmZmZmZmZmZ ddlZddlmZ ddlmZ dd	lmZmZmZm Z m!Z! dd
l"m#Z# e$de% ej&de%dd e$de% g dZ'e#ddddde(fddZ)e#dddddede(fddZ*e#dddddZde+de+ded edef
d!d"Z,d[de+de+d$e-defd%d&Z.ej/d'fd(ed ed)e(deee0f fd*d+Z1ed,Z2d-ee2ef ded edee2ef fd.d/Z3ded0ed1e+d2e+def
d3d4Z4de+defd5d6Z5d[de+d$e-defd7d8Z6ej7d9ej8d:ej9d;ej/d<iZ:G d=d> d>eZ;dej9fdee d edee0ef fd?d@Z<dAee deee0ef  fdBdCZ=ddDdeee0ef  dAedefdEdFZ>dej?d ede-fdGdHZ@	Id\dej?dJe0dKe-dLe-de-f
dMdNZAdOedee-e-f fdPdQZBdddRdSedTedUee- dVee- dWeddfdXdYZdS )]z8The testing package contains testing-specific utilities.    N)ABCabstractmethod)deepcopy)product)	AnyCallableDictIteratorOptionalSequenceTupleTypeVarUnion	gradcheck)assert_close)DeviceDtypeTensoreyetensor)
deprecatedalwaysa6  Since kornia 0.7.2 the `kornia.testing` module is deprecated and will be removed in kornia 0.8.0 (dec 2024). Most of these functionalities will be removed from kornia package and will be part of the tests of kornia. Some functionalities which we think is important to keep will be moved to other kornia module.   )category
stackleveldefault)r   create_eye_batchtensor_to_gradcheck_varxla_is_availablezkornia.utils.xla_is_availablez0.7.2zYThe `kornia.testing` module is deprecated and will be removed in kornia 0.8.0 (dec 2024).)extra_reasonreturnc                   C   s   t jddur
dS dS )z6Return whether `torch_xla` is available in the system.	torch_xlaNTF)	importlibutil	find_spec r&   r&   K/home/ubuntu/.local/lib/python3.10/site-packages/kornia/testing/__init__.pyr   2   s   r   zkornia.utils.is_mps_tensor_safexc                 C   s   dt | jv S )z'Return whether tensor is on MPS device.mps)strdevice)r(   r&   r&   r'   is_mps_tensor_safe>   s   r,   zkornia.utils.misc.eye_like
batch_sizeeye_sizer+   dtypec                 C   s"   t |||dd||| ddS )z3Create a batch of identity matrices of shape Bx3x3.r+   r/      )r   viewexpand)r-   r.   r+   r/   r&   r&   r'   r   H   s   "r   MbP?std_valc                 C   s*   t | ||}t| |}||| | S )z5Create a batch of random homographies of shape Bx3x3.)torchFloatTensorr   uniform_)r-   r.   r6   stdr   r&   r&   r'   create_random_homographyR   s   
r;   Tr   requires_gradc                 C   s&   t | stt| | ||S )zConvert the input tensor to a valid variable to check the gradient.

    `gradcheck` needs 64-bit floating point and requires gradient.
    )r7   	is_tensorAssertionErrortyperequires_grad_)r   r/   r<   r&   r&   r'   r   Y   s   
r   Tdatac                 C   s8   i }|   D ]\}}t|tr|||n|||< q|S N)items
isinstancer   to)rB   r+   r/   outkeyvalr&   r&   r'   dict_toh   s    rJ   yhwc                 C   s8   t | | d|d | d |d | d f  S )z+Compute the absolute error between patches..   )r7   absmean)r(   rK   rL   rM   r&   r&   r'   compute_patch_erroro   s   8rQ   c                 C   s6   t g dg dg dgddd}|| dd}|S )z@Create a batch of rectified fundamental matrices of shape Bx3x3.)        rR   rR   )rR   rR   g      )rR   g      ?rR   r1      )r   r3   r4   )r-   F_rectF_repeatr&   r&   r'   #create_rectified_fundamental_matrixt   s   $rV   c                 C   s6   t | }t| d|}t| d|}|ddd| | S )z=Create a batch of random fundamental matrices of shape Bx3x3.rS   r   r   r1   )rV   r;   permute)r-   r6   rT   H_leftH_rightr&   r&   r'    create_random_fundamental_matrix{   s   rZ   )q?r[   )x#O?r\   )-C6?h㈵>)r^   r^   c                   @   s<  e Zd ZedededdfddZedededdfddZedededdfd	d
Zededede	de
f ddfddZededdfddZedededdfddZe			d$dededee dee deddfddZedddde	deejeej f f deejeej f ded ed!e
defd"d#ZdS )%
BaseTesterr+   r/   r!   Nc                 C      t dNzImplement a stupid routine.NotImplementedErrorselfr+   r/   r&   r&   r'   
test_smoke      zBaseTester.test_smokec                 C   r`   ra   rb   rd   r&   r&   r'   test_exception   rg   zBaseTester.test_exceptionc                 C   r`   ra   rb   rd   r&   r&   r'   test_cardinality   rg   zBaseTester.test_cardinalitytorch_optimizer.c                 C   r`   ra   rb   )re   r+   r/   rj   r&   r&   r'   test_dynamo   rg   zBaseTester.test_dynamoc                 C   r`   ra   rb   )re   r+   r&   r&   r'   test_gradcheck   rg   zBaseTester.test_gradcheckc                 C   s   d S rC   r&   rd   r&   r&   r'   test_module   s   zBaseTester.test_moduleFactualexpectedrtolatollow_tolerancec           	      C   s   t | dr| j} t |dr|j}d| jjv sd|jjv r d\}}|du rW|du rWt| jd\}}t|jd\}}t||t||}}|rLt	|n|}|rUt	|n|}t
| |||dS )a  Assert that `actual` and `expected` are close.

        Args:
            actual: Actual input.
            expected: Expected input.
            rtol: Relative tolerance.
            atol: Absolute tolerance.
            low_tolerance:
                This parameter allows to reduce tolerance. Half the decimal places.
                Example, 1e-4 -> 1e-2 or 1e-6 -> 1e-3

        rB   xla){Gz?rt   NrR   rR   rp   rq   )hasattrrB   r+   r?   _DTYPE_PRECISIONSgetr/   maxmathsqrtr   )	rn   ro   rp   rq   rr   actual_rtolactual_atolexpected_rtolexpected_atolr&   r&   r'   r      s   

zBaseTester.assert_closeTraise_exception	fast_modefuncinputsr   r   kwargsc                K   s   t | |f||d|S )Nr   r   )r   r   r   r   r   r&   r&   r'   r      s   	zBaseTester.gradcheck)NNF)__name__
__module____qualname__r   r   r   rf   rh   ri   r   r   rk   rl   rm   staticmethodr   r
   floatboolr   r   r7   r   r   r&   r&   r&   r'   r_      s\    $&r_   c                 C   sr  ddl m} | d u rtd} d}d}|||}|d | |}| }|d dd | |}|d dd | |}	|d	 dd | |}
|d	 dd | |}|d
 dd | |}|d
 dd | |}||dd dd d f |dd dd d f }||}|d | |}|d dd | |}|d dd | |}||||	|
|||||||dS )Nr   )epipolarcpur      KRr1   tP.rS   points3dpoints2d)K1K2R1R2t1t2P1P2FXx1x2)	kornia.geometryr   r7   r+   generate_scenerF   clonefundamental_from_projectionsnormalize_transformation)r+   r/   epi	num_views
num_pointsscener   r   r   r   r   r   r   r   F_matr   r   r   r&   r&   r'   generate_two_view_random_scene   s@   
0
r   possible_parametersc                  +   s>       } fdd|D }t| D ]
}tt||V  qdS )z-Create cartesian product of given parameters.c                    s   g | ]} | qS r&   r&   ).0parameter_namer   r&   r'   
<listcomp>  s    z3cartesian_product_of_parameters.<locals>.<listcomp>N)keysr   dictzip)r   parameter_namespossible_valuesparam_combinationr&   r   r'   cartesian_product_of_parameters  s   r   )r   c                 k   s`    | d u ri } t | tstdt|  | D ]\}}|D ]}t| }|||< |V  qqd S )Nzdefault should be a dict not a )rE   r   r>   r?   rD   r   )r   r   r   r   v	param_setr&   r&   r'   "default_with_one_parameter_changed  s   
r   c                 C   s    d| j v rdS |tjkrdS dS )Nrs   rt   r5   r]   )r?   r7   float16r0   r&   r&   r'   _get_precision%  s
   

r   r]   device_targettol_valtol_val_defaultc                 C   s*   |dvrt d| d|| jv r|S |S )N)r   cudars   r)   zInvalid device name: .)
ValueErrorr?   )r+   r   r   r   r&   r&   r'   _get_precision_by_name-  s
   
r   r   c                  G   s&   t dd | D  \}}t|t|fS )Nc                 S   s    g | ]}t t|jd qS )ru   )rx   ry   r7   	as_tensorr/   )r   input_r&   r&   r'   r   :  s     z'_default_tolerances.<locals>.<listcomp>)r   rz   )r   rtolsatolsr&   r&   r'   _default_tolerances9  s   r   rv   rn   ro   rp   rq   r   c                K   s:   |du r|du rt | |\}}t| |f||ddd|S )z9Assert two tensors are similar within provided tolerance.NF)rp   rq   check_stride	equal_nan)r   _assert_close)rn   ro   rp   rq   r   r&   r&   r'   r   >  s   	r   )NN)r5   )r]   )C__doc__importlib.utilr#   r{   warningsabcr   r   copyr   	itertoolsr   typingr   r   r   r	   r
   r   r   r   r   r7   torch.autogradr   torch.testingr   r   kornia.corer   r   r   r   r   kornia.utils.helpersr   simplefilterDeprecationWarningwarn__all__r   r   r,   intr   r   r;   float64r*   r   rA   rJ   rQ   rV   rZ   bfloat16r   float32rx   r_   r   r   r   r+   r   r   r   r&   r&   r&   r'   <module>   s   ,	"

*
	*L"6(		
