Ë
    g^(hÀa ã            $       ó  — 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mZm	Z	m
Z
 d dlm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 g d	¢Z G d
„ de«      Zdej2                  fd„Zdej2                  fd„Zd„ Zdedeej2                  df   fd„Z	 d{dedeej2                  df   fd„Z	 d|de
ej2                  eej2                     f   de deej2                     fd„Z!d„ Z"d„ Z#	 d}de$eej2                  df      fd„Z% ede&¬«      d~d „«       Z'd!„ Z(	 d|de$ej2                     fd"„Z)d#e*e+e$ej2                     f   deej2                  df   fd$„Z,	 d|d%ej2                  d&e	ej2                     dej2                  fd'„Z-dd€d(„Z.	 d�deej2                  df   fd)„Z/ddd*œdeeej2                  df   df   fd+„Z0d,„ Z1d|d-„Z2d.„ Z3d/„ Z4d0„ Z5	 d|de$ej2                     fd1„Z6d2„ Z7d3„ Z8deeej2                  df   e e f   fd4„Z9d5Z:	 d‚deej2                  df   fd6„Z;d7„ Z< ed8e&¬«      dƒd:„«       Z=d;„ Z>de$e$e	ej2                           fd<„Z?de$e$e	ej2                           fd=„Z@de fd>„ZAd€d?„ZBde fd@„ZCdA„ ZDdBj‹                  «       ZFdCZG	 d|dD„ZHde fdE„ZIde fdF„ZJde fdG„ZKdH„ ZLde fdI„ZMdJ„ ZNdK„ ZO	 	 d„dePfdL„ZQdM„ ZRdN„ ZSdO„ ZTdP„ ZUdddddQœdR„ZVdS„ ZWdT„ ZXdU„ ZYd|dV„ZZdW„ Z[dX„ Z\dYj‹                  «       Z]dZ„ Z^d[„ Z_d\„ Z`dd]œd^„ZadddddQœd_„Zbd`daddbd9dbdddddbdddcœddede
e   f   deedfecdgecdhecdie djecdke dle dme dne doe dpe dqe dre	e    de f ds„Zddt„ Ze	 dd`dadddbd9dbddddbddduœddedef   deedve	e   dfecdgecdhecdwe die djecdke dle dme dxe dye dqe dre de f"dz„Zfy)…é    N)ÚIterable)Úproduct)ÚCallableÚOptionalÚUnion)Ú
deprecated)Ú_vmapÚvmap)Úis_tensor_like)Ú_TensorOrTensors)Ú	gradcheckÚgradgradcheckÚGradcheckErrorÚget_numerical_jacobianÚget_analytical_jacobianÚ)get_numerical_jacobian_wrt_specific_inputc                   ó   — e Zd ZdZy)r   z<Error raised by :func:`gradcheck` and :func:`gradgradcheck`.N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    úV/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/autograd/gradcheck.pyr   r      s   „ ÚGr   r   Úobjc                 ó–   — | j                   t        j                  t        j                  t        j                  t        j
                  hv S ©N)ÚlayoutÚtorchÚ
sparse_csrÚ
sparse_cscÚ
sparse_bsrÚ
sparse_bsc©r   s    r   Ú_is_sparse_compressed_tensorr%   "   s<   € Ø�:‰:Ü×ÑÜ×ÑÜ×ÑÜ×Ñð	ð ð r   c                 óT   — t        | «      xs | j                  t        j                  u S r   )r%   r   r   Ú
sparse_coor$   s    r   Ú_is_sparse_any_tensorr(   +   s!   € Ü'¨Ó,ÒN°·
±
¼e×>NÑ>NÐ0NÐNr   c                 ó`   — t        | «      xr" | j                  «       xs | j                  «       S r   )r   Úis_floating_pointÚ
is_complexr$   s    r   Ú_is_float_or_complex_tensorr,   /   s'   € Ü˜#ÓÒP C×$9Ñ$9Ó$;Ò$O¸s¿~¹~Ó?OÐPr   Úinput_tensorsÚreturn.c                 óÌ   — | D �cg c]J  }t        |«      r=|j                  r1|j                  |j                  «       |ft        j
                  ¬«      ‘ŒL }}t        |«      S c c}w )N©r   )r,   Úrequires_gradÚ	new_zerosÚnumelr   ÚstridedÚtuple)r-   Únumel_outputÚtÚouts       r   Ú_allocate_jacobians_with_inputsr9   3   s\   € ð öàÜ& qÔ)¨a¯oªoð 	
�‰�Q—W‘W“Y Ð-´e·m±mˆÕDð€Cð ô
 �‹:Ðùòs   …AA!Úoutput_tensorsc                 óÀ   — ||t         j                  dœ}| D �cg c]0  }t        |«      r# |j                  ||j	                  «       ffi |¤Ž‘Œ2 }}t        |«      S c c}w )N)ÚdtypeÚdevicer   )r   r4   r,   r2   r3   r5   )r:   Únumel_inputr<   r=   Úoptionsr7   r8   s          r   Ú _allocate_jacobians_with_outputsr@   C   sg   € ð ¨¼5¿=¹=ÑI€Gð  öàÜ& qÔ)ð 	ˆ�‰�[ !§'¡'£)Ð,Ñ8°Ó8ð€Cð ô
 �‹:Ðùòs   ™5AFÚxÚonly_requiring_gradc              #   óð   K  — t        | «      r| j                  s|s| –— y y t        | t        j                  j
                  «      r-t        | t        «      s| D ]  }t        ||«      E d {  –—†  Œ y y y 7 Œ	­wr   )r   r1   Ú
isinstanceÚcollectionsÚabcr   ÚstrÚ_iter_tensors)rA   rB   Úelems      r   rH   rH   S   so   è ø€ ô �aÔà�?Š?Ñ"5Ø‹Gð #6ä	�A”{—‘×/Ñ/Ô	0¼ÀAÄsÔ9KØò 	@ˆDÜ$ TÐ+>Ó?×?Ñ?ñ	@ð :LÐ	0à?ús   ‚A(A6Á*A4Á+
A6c                 ó¬  — t        | t        t        f«      r t        | «      t	        t
        | «      «      S t        | «      r,| j                  t        j                  t        j                  hv r| S | j                  t        j                  u �rÔ| j                  }| j                  «       j                  }t        j                  | j                   d | j#                  «        t        j$                  |¬«      }|j'                  «       j)                  «       j+                  |¬«      }t        j,                  |j/                  «       g| j                   | j#                  «       d  ¢­| j                  |¬«      }| j1                  «       j3                  «       }|j/                  «       dkD  rv|j5                  «       }|j7                  «       j9                  t        j:                  |||¬«      j=                  d«      «      j?                  d«      }|jA                  «       ||<   t        jB                  ||| j                   «      jE                  d«      jG                  | jH                  «      S tK        | «      �rÈ| j                  t        jL                  t        jN                  hv r| jA                  «       j                   dd nd }	| j                  t        jP                  t        jL                  hv r| jS                  «       n| jU                  «       }
t        | j1                  «       jW                  t        j                  ¬«      «      jW                  | j                  |	¬«      }|jA                  «       j/                  «       tY        d|jA                  «       j                   d   «      z  }|
j/                  «       |
j                   d	   z  }|j/                  «       tY        d||z  «      z  }||j[                  «       k7  r+t]        | j                  › d
|› d|j[                  «       › �«      ‚|jG                  | jH                  «      S t_        | «      rta        | j                  «      ‚| S )N©r<   r=   ©r<   r   é   Té   r0   )r   Ú	blocksizeéÿÿÿÿz densify failed: expected nnz=z	 but got )1rD   Úlistr5   ÚtypeÚmapÚ_densifyr   r   r   r4   Ú_mkldnnr'   r=   Ú_indicesr<   ÚonesÚshapeÚ
sparse_dimÚint8Únonzeror7   ÚtoÚzerosr3   ÚdetachÚcoalesceÚstrideÚindicesÚmulÚtensorÚ	unsqueezeÚsumÚvaluesÚsparse_coo_tensorÚ_coalesced_Úrequires_grad_r1   r%   r"   r#   r    Úcrow_indicesÚccol_indicesÚ	to_sparseÚmaxÚ_nnzÚAssertionErrorr(   ÚNotImplementedError)rA   r=   Úindices_dtypeÚtmpra   rf   Úx_coalescedr`   Úflat_indicesrO   Úcompressed_indicesÚrÚdense_numelÚbatch_numelÚsparse_numels                  r   rT   rT   _   s]  € ô �!”dœE�]Ô#ØŒt�A‹w”sœ8 QÓ'Ó(Ð(Ü˜AÔ !§(¡(¬u¯}©}¼e¿m¹mÐ.LÑ"LØˆØ	
�‰”U×%Ñ%Ò	%Ø—‘ˆØŸ
™
›×*Ñ*ˆÜ�j‰j˜Ÿ™Ð!1 1§<¡<£>Ð2¼%¿*¹*ÈVÔTˆØ—+‘+“-—/‘/Ó#×&Ñ&¨]Ð&Ó;ˆÜ—‘Ø�Y‰Y‹[Ð5˜1Ÿ7™7 1§<¡<£>Ð#3Ð4Ñ5¸Q¿W¹WÈVô
ˆð —h‘h“j×)Ñ)Ó+ˆØ×ÑÓ Ò"Ø—Z‘Z“\ˆFà×#Ñ#Ó%ß‘Ü—L‘L ¨}ÀVÔL×VÑVØóó÷
 ‘�Q“ð ð $/×#5Ñ#5Ó#7ˆF�<Ñ ä×#Ñ# G¨V°Q·W±WÓ=ß‰[˜Óß‰^˜AŸO™OÓ,ð	
ô
 
& aÕ	(ð �x‰xœE×,Ñ,¬e×.>Ñ.>Ð?Ñ?ð �H‰H‹J×Ñ˜Q˜qÑ!àð 	ð �x‰xœE×,Ñ,¬e×.>Ñ.>Ð?Ñ?ð �N‰NÔà—‘Ó!ð 	ô �Q—X‘X“Z×)Ñ)´×1AÑ1AÐ)ÓBÓC×MÑMØ—8‘8 yð Nó 
ˆð —h‘h“j×&Ñ&Ó(¬C°°1·8±8³:×3CÑ3CÀAÑ3FÓ,GÑGˆØ(×.Ñ.Ó0Ð4F×4LÑ4LÈRÑ4PÑPˆØ—w‘w“y¤C¨¨;¸Ñ+DÓ$EÑEˆØ˜1Ÿ6™6›8Ò#Ü Ø—8‘8�*Ð:¸<¸.È	ÐRS×RXÑRXÓRZÐQ[Ð\óð ð ×Ñ §¡Ó0Ð0Ü	˜qÔ	!Ü! !§(¡(Ó+Ð+Ø€Hr   c           
   #   ó†  ‡‡K  — t        | «      �rÜd„ }| j                  «       }t        | j                  «       «      }| j                  t
        j                  u r0| j                  «       j                  «       }| j                  «       }�n¸| j                  t
        j                  u rRt        j                  | j                  «       | j                  «       «      j                  «       }| j                  «       }�nJ| j                  t
        j                  u rTt        j                  | j!                  «       | j#                  «       d¬«      j                  «       }| j                  «       }�nÚ| j                  t
        j$                  u �rC| j                  «       }|j                  «       dd }t        j                  | j                  «       | j                  «       «      j'                  |d   |d   z  d«      j)                  t        j*                  || j,                  ¬«      j/                  dd«      «      j1                  t        j2                  t        j4                  t        j6                  || j,                  ¬«      «      «      j9                  d|«      «      j                  «       }|j;                  dd«      }|j                  d«      }�nz| j                  t
        j<                  u �rD| j                  «       }|j                  «       dd }t        j                  | j!                  «       | j#                  «       d¬«      j'                  |d   |d   z  d«      j)                  t        j*                  || j,                  ¬«      j/                  dd«      «      j1                  t        j2                  t        j4                  t        j6                  || j,                  ¬«      «      «      j9                  d|«      «      j                  «       }|j;                  dd«      }|j                  d«      }nt?        d	| j                  › d
�«      ‚ ||«      Š|j@                  }tC        |«      D ]‰  }||   }	tE        |j                  «       dd  D �
cg c]  }
tC        |
«      ‘Œ c}
Ž D ]O  }||   jG                  «       t        |«      z   ŠtI        ˆˆfd„tC        tK        |«      «      D «       «      }|	||f–— ŒQ Œ‹ y | j                  t
        jL                  k(  rVtO        tE        | j                  «       D �
cg c]  }
tC        |
«      ‘Œ c}
Ž «      D ]  \  }}| jQ                  «       }|||f–— Œ y | j@                  } tO        tE        | j                  «       D �
cg c]  }
tC        |
«      ‘Œ c}
Ž «      D ]  \  }}| ||f–— Œ y c c}
w c c}
w c c}
w ­w)Nc                 óx   — t        | «      }d}dg|z  }t        t        |«      «      D ]  }|||<   || |   z  }Œ |S )NrM   r   )ÚlenÚreversedÚrange)ÚsizeÚdimrr   r`   Úis        r   Ú
get_stridez _iter_tensor.<locals>.get_stride°   sP   € Ü�d“)ˆCØˆCØ�S˜3‘YˆFÜœe C›jÓ)ò �Ø��q‘	Ø�t˜A‘w‘‘ðð ˆMr   T)Ú	transposerM   rN   r   ©r=   é   z_iter_tensor for z inputc              3   ó4   •K  — | ]  }‰|   ‰|   z  –— Œ y ­wr   r   )Ú.0Úkra   Úx_strides     €€r   ú	<genexpr>z_iter_tensor.<locals>.<genexpr>õ   s   øè ø€ ÒQ¸˜G A™J¨°!©Õ4ÑQùs   ƒ))r(   rn   rQ   r   r   r   r'   rV   r7   Ú_valuesr    Ú _convert_indices_from_csr_to_coorj   Úcol_indicesrf   r!   rk   Úrow_indicesr"   Úrepeat_interleaveÚmul_rc   r=   ÚreshapeÚadd_ÚstackÚwhererW   ÚrepeatÚflattenr#   rp   Údatar~   r   Útolistre   r|   rU   Ú	enumerateÚto_dense)Úx_tensorr‚   Úx_nnzÚx_sizeÚ	x_indicesÚx_valuesÚx_block_valuesÚx_blocksizer�   Úx_valueÚmÚx_idxÚd_idxÚx_tensor_densera   r‰   s                 @@r   Ú_iter_tensorr§   �   s™  ùè ø€ ô" ˜XÕ&ò	ð —‘“ˆÜ�h—m‘m“oÓ&ˆØ�?‰?œe×.Ñ.Ñ.Ø ×)Ñ)Ó+×-Ñ-Ó/ˆIØ×'Ñ'Ó)ŠHØ�_‰_¤× 0Ñ 0Ñ0Ü×>Ñ>Ø×%Ñ%Ó'¨×)=Ñ)=Ó)?óç‰a‹cð ð  —‘Ó(ŠHØ�_‰_¤× 0Ñ 0Ñ0Ü×>Ñ>Ø×%Ñ%Ó'¨×)=Ñ)=Ó)?È4ôç‰a‹cð ð  —‘Ó(ŠHØ�_‰_¤× 0Ñ 0Ò0Ø%Ÿ_™_Ó.ˆNØ(×-Ñ-Ó/°°!Ð4ˆKä×6Ñ6Ø×)Ñ)Ó+¨X×-AÑ-AÓ-Có÷ #Ñ" ;¨q¡>°KÀ±NÑ#BÀAÓFß‘”e—l‘l ;°x·±ÔG×OÑOÐPQÐSTÓUÓVß‘Ü—K‘KÜŸ™¤E§J¡J¨{À8Ç?Á?Ô$SÓTóç‘f˜Q Ó&ó÷
 ‘“ð ð &×-Ñ-¨a°Ó3ˆHØ—M‘M !Ó$ŠEØ�_‰_¤× 0Ñ 0Ò0Ø%Ÿ_™_Ó.ˆNØ(×-Ñ-Ó/°°!Ð4ˆKä×6Ñ6Ø×)Ñ)Ó+¨X×-AÑ-AÓ-CÈtô÷ #Ñ" ;¨q¡>°KÀ±NÑ#BÀAÓFß‘”e—l‘l ;°x·±ÔG×OÑOÐPQÐSTÓUÓVß‘Ü—K‘KÜŸ™¤E§J¡J¨{À8Ç?Á?Ô$SÓTóç‘f˜Q Ó&ó÷
 ‘“ð ð &×-Ñ-¨a°Ó3ˆHØ—M‘M !Ó$‰Eä%Ð(9¸(¿/¹/Ð9JÈ&Ð&QÓRÐRÙ˜fÓ%ˆà—=‘=ˆÜ�u“ò 	,ˆAØ˜q‘kˆGÜ °X·]±]³_ÀQÀRÐ5HÖ"I°¤5¨¥8Ò"IÐJò ,�Ø# A™,×-Ñ-Ó/´$°u³+Ñ=�ÜÔQ¼eÄCÈÃKÓ>PÔQÓQ�Ø˜u eÐ+Ó+ñ,ñ	,ð 
�‰œEŸM™MÒ	)Ü%¤gÀ(Ç-Á-Ã/Ö/R¸Q´°aµÒ/RÐ&SÓTò 	/‰LˆE�5ð &×.Ñ.Ó0ˆNØ  %¨Ð.Ó.ñ		/ð —=‘=ˆÜ%¤gÀ(Ç-Á-Ã/Ö/R¸Q´°aµÒ/RÐ&SÓTò 	)‰LˆE�5Ø˜E 5Ð(Ó(ñ	)ùò #Jùò
 0Sùò 0Sùs,   „Q>WÒV2
ÒBWÔ&V7Ô8AWÖV<Ö'Wçü©ñÒMbP?c                 óz  — g }|€t         | t        |«      Ž «      }|st        d„ |D «       «      rt        d«      ‚|€|}t        |«      D ��cg c]   \  }}t	        |«      sŒ|j
                  sŒ|‘Œ" }	}}t        t        t        |d«      |	«      «      D ]  \  }\  }
}|t        | |||||
|¬«      gz  }Œ  |S c c}}w )aÉ  Compute the numerical Jacobian of `fn(inputs)` with respect to `target`.

    If not specified, targets are the input. Returns M * N Jacobians where N is the
    number of tensors in target that require grad and M is the number of non-integral
    outputs.

    Args:
        fn: the function to compute the jacobian for
        inputs: inputs to `fn`
        outputs: provide precomputed outputs to avoid one extra invocation of fn
        target: the Tensors wrt whom Jacobians are calculated (default=`inputs`)
        eps: the magnitude of the perturbation during finite differencing
             (default=`1e-3`)
        is_forward_ad: if this numerical jacobian is computed to be checked wrt
                       forward AD gradients (this is used for error checking only)

    Returns:
        A list of M N-tuples of tensors

    Note that `target` may not even be part of `input` to `fn`, so please be
    **very careful** in this to not clone `target`.
    c              3   ó<   K  — | ]  }|j                  «       –— Œ y ­wr   ©r+   ©r‡   Úos     r   rŠ   z*_get_numerical_jacobian.<locals>.<genexpr>   s   è ø€ Ò A°A §¡§Ñ Aùó   ‚zsExpected output to be non-complex. get_numerical_jacobian no longer supports functions that return complex outputs.T)ÚinputÚis_forward_ad)	Ú	_as_tupleÚanyÚ
ValueErrorr™   r   r1   ÚziprH   r   )ÚfnÚinputsÚoutputsÚtargetÚepsr°   Ú	jacobiansr�   ÚaÚinp_indicesÚinpÚinp_idxs               r   Ú_get_numerical_jacobianr¿     sç   € ð2 13€IØ€Ü™B¤	¨&Ó 1Ð2Ó3ˆÙœSÑ A¸Ô AÔAÜðEó
ð 	
ð €~Øˆä Ó'÷Ùˆa�¬>¸!Õ+<ÀÇÃŠð€Kñ ô '¤s¬=¸ÀÓ+FÈÓ'TÓUò 
Ñˆ‰>ˆC�ØÜ5ØØØØØØØ+ôð

ñ 
	
‰	ð
ð Ðùós   ÁB7Á!B7Á.B7aA  `get_numerical_jacobian` was part of PyTorch's private API and not meant to be exposed. We are deprecating it and it will be removed in a future version of PyTorch. If you have a specific use for this or feature request for this to be a stable API, please file us an issue at https://github.com/pytorch/pytorch/issues/new)Úcategoryc                 óp   ‡ — |dk7  rt        d«      ‚ˆ fd„}t        ||d||«      }t        d„ |D «       «      S )aÈ  Compute the numerical Jacobian for a given fn and its inputs.

    This is a Deprecated API.

    Args:
        fn: the function to compute the Jacobian for (must take inputs as a tuple)
        inputs: input to `fn`
        target: the Tensors wrt whom Jacobians are calculated (default=`input`)
        eps: the magnitude of the perturbation during finite differencing
             (default=`1e-3`)
        grad_out: defaults to 1.0.

    Returns:
        A list of Jacobians of `fn` (restricted to its first output) with respect to
        each input or target, if provided.

    Note that `target` may not even be part of `input` to `fn`, so please be
    **very careful** in this to not clone `target`.
    ç      ð?zaExpected grad_out to be 1.0. get_numerical_jacobian no longer supports values of grad_out != 1.0.c                  ó   •—  ‰| «      S r   r   )Úinpsrµ   s    €r   Úfn_pack_inpsz,get_numerical_jacobian.<locals>.fn_pack_inps]  s   ø€ Ù�$‹xˆr   Nc              3   ó&   K  — | ]	  }|d    –— Œ y­w)r   Nr   )r‡   Újacobian_for_each_outputs     r   rŠ   z)get_numerical_jacobian.<locals>.<genexpr>b  s   è ø€ ÒWÐ1IÐ)¨!Õ,ÑWùs   ‚)r³   r¿   r5   )rµ   r¶   r¸   r¹   Úgrad_outrÅ   rº   s   `      r   r   r   9  sH   ø€ ð: 	�CŠäð2ó
ð 	
ô
ô (¨°f¸dÀFÈCÓP€IäÑWÈYÔWÓWÐWr   c                 ó‚  ‡‡‡— t        |«      r·|j                  |j                  k(  sJ |j                  |j                  f«       ‚|j                  «       |j                  «       k(  s0J |j                  «       |j                  «       |j                  f«       ‚|j	                  «       }|j	                  «       }|j                  «       }|j                  «       }|j                  ||z
  «        | «       }|j                  ||z   «        | «       }|j                  |«       ˆˆfd„Št        ˆfd„t        ||«      D «       «      S )Nc                 ój   •—  ‰| |«       || z
  d‰z  z  }|j                  «       j                  d«      S )Nr…   rP   )r^   r‘   )r»   ÚbÚretÚnbhd_checks_fnÚnorm_vs      €€r   Úcomputez,_compute_numerical_gradient.<locals>.compute|  s6   ø€ Ù�q˜!ÔØ�1‰u˜˜V™Ñ$ˆØ�z‰z‹|×#Ñ# BÓ'Ð'r   c              3   ó6   •K  — | ]  \  }} ‰||«      –— Œ y ­wr   r   )r‡   r»   rË   rÏ   s      €r   rŠ   z._compute_numerical_gradient.<locals>.<genexpr>�  s   øè ø€ Ò=¡6 A q‘˜˜A—Ñ=ùó   ƒ)
r%   r   rn   rX   rf   r^   ÚcloneÚcopy_r5   r´   )	rµ   ÚentryÚvrÎ   rÍ   ÚorigÚoutaÚoutbrÏ   s	      ``   @r   Ú_compute_numerical_gradientrÙ   e  só   ú€ ô $ EÔ*ð �|‰|˜qŸx™xÒ'ÐA¨%¯,©,¸¿¹Ð)AÓAÐ'Ø�z‰z‹|˜qŸv™v›xÒ'ÐN¨%¯*©*«,¸¿¹»À%Ç+Á+Ð)NÓNÐ'à—‘“ˆØ�H‰H‹Jˆð —‘“ˆà�;‰;‹=€DØ	‡K�K��q‘ÔÙ‹4€DØ	‡K�K��q‘ÔÙ‹4€DØ	‡K�K�Ôõ(ô
 Ó=¬S°°t«_Ô=Ó=Ð=r   c                 ót  — g } | t        |t        «      r|d   n|«      }|rjt        |t        «      r | |d   dz  «      n
 | |dz  «      }t        ||«      D ]0  \  }}|j                  «       rJ ‚||dz  z   }	|j	                  |	«       Œ2 |S |D ]'  }|s|j                  «       rJ ‚|j	                  |«       Œ) |S )Nr   rM   ù              ð?)rD   r5   r´   r+   Úappend)
Újvp_fnÚdeltaÚinput_is_complexr°   ÚjvpsÚ	ds_dx_tupÚ	ds_dy_tupÚds_dxÚds_dyÚconj_w_ds
             r   Ú*_compute_numerical_jvps_wrt_specific_inputræ   „  sÖ   € ð  "€DÙ¤:¨e´UÔ#;�u˜Q’xÀÓG€Iáä%/°´uÔ%=‰F�5˜‘8˜b‘=Ô!Á6È%ÐRTÉ*ÓCUð 	ô   	¨9Ó5ò 	"‰LˆE�5Ø×'Ñ'Ô)Ð)Ð)à˜u r™zÑ)ˆHØ�K‰K˜Õ!ð		"ð €Kð ò 	ˆEÙ ¨×(8Ñ(8Ô(:Ð:Ð:Ø�K‰K˜Õð	ð €Kr   Újacobians_colsc                 óÌ   — t        |||j                  j                  r|j                  nd ¬«      }t        |«      D ]%  \  }}| j	                  «       D ]  \  }}||   ||<   Œ Œ' |S ©NrL   )r@   r<   r+   r™   Úitems)	rç   r·   r¯   r3   rº   r�   Újacobianrˆ   rÕ   s	            r   Ú_combine_jacobian_colsrì   Ÿ  sr   € ô
 1Ø�¨U¯[©[×-CÒ-C˜eŸkškÈô€Iô ! Ó+ò ‰ˆˆ8Ø"×(Ñ(Ó*ò 	‰DˆAˆqØ˜A™$ˆH�QŠKñ	ðð Ðr   r¯   Úmaybe_perturbed_inputc                 óŽ   — | j                   t        j                  k(  r|�|j                  «       S | S t	        | «      r|r|�|S | S | S r   )r   r   rU   Ú	to_mkldnnr(   )r¯   rí   Ú	fast_modes      r   Ú_prepare_inputrñ   ­  sS   € ð
 ‡|�|”u—}‘}Ò$à Ð,Ø(×2Ñ2Ó4Ð4àˆLÜ	˜uÔ	%ÙÐ.Ð:ð )Ð(àˆLð ˆr   c           
      ó  — |�dnd}| j                   |j                   k(  s(J d|› d|› d| j                   › d|j                   › d�	«       ‚| j                  |j                  k(  s(J d|› d|› d	| j                  › d|j                  › d�	«       ‚y )
Nzon index {idx} Ú zPExpected `func` to return outputs with the same shape when inputs are perturbed zby z, but got: shapes z and ú.zPExpected `func` to return outputs with the same dtype when inputs are perturbed z, but got: dtypes )rX   r<   )Úoutput1Úoutput2r¹   ÚidxÚon_indexs        r   Ú#_check_outputs_same_dtype_and_shaperù   Å  sª   € ð %( OÑ ¸€HØ�=‰=˜GŸM™MÒ)ð ð&Ø&. Z¨s°3°%ð 8Ø—=‘=�/  w§}¡} o°Qð	8óÐ)ð
 �=‰=˜GŸM™MÒ)ð ð&Ø&. Z¨s°3°%ð 8Ø—=‘=�/  w§}¡} o°Qð	8óÑ)r   c                 óL  — i }|€||   n|}|j                   sJ ‚t        |«      D ]b  \  }}	}
t        | |||«      }||	   }t        j                  t
        |	|¬«      }t        ||||«      }t        |||j                  «       |«      ||
<   Œd t        ||||j                  «       «      S )N)r÷   r¹   )r1   r§   Ú_with_prepare_inputsÚ	functoolsÚpartialrù   Ú_get_numerical_jvp_fnræ   r+   rì   r3   )rµ   Ú	input_idxr¶   r·   r¹   r¯   r°   Újacobian_colsrA   r÷   r¥   Ú
wrapped_fnÚinput_to_perturbrÍ   rÝ   s                  r   r   r   Õ  sÃ   € ð 46€MØ!& ˆF�9Ò°E€EØ×ÒÐÐÜ% eÓ,ò 
‰ˆˆ3�Ü)¨"¨f°iÀÓCˆ
Ø˜S™6ÐÜ"×*Ñ*Ü/°S¸cô
ˆô 'ØÐ(¨#¨~ó
ˆô  JØ�C˜Ÿ™›¨ó 
ˆ�eÒð
ô " -°¸%ÀÇÁÃÓOÐOr   )Úcheck_grad_dtypesÚall_uc                ó2  ‡— t         j                  j                  }t        d„ |D «       «      }t	        d„ |D «       «      rt        d«      ‚|rt        ˆfd„|D «       «      }nt        ˆfd„|D «       «      }|j                  «       5  g }g }	t        |«      D ]«  \  }
}t        |«      rŠ|j                  r~|j                  t         j                  k(  rt        d«      ‚|j                  |j                  «       t        j                  |«      «      }|j                  |j!                  |«      d   «       |	j                  |«       Œ­ |�rt        t#        ||«      «      D �]  \  }
\  }}|j%                  |j'                  |«      «       t)         | |	Ž «      }t+        t,        |«      }t        |«      D ]Ÿ  \  }}|j!                  |«      \  }}|r.|�,|j/                  «       |j/                  «       k7  rt1        d	«      ‚||
   |   j3                  d
«       |€||
   |   j5                  «        Œz||
   |   j%                  |j7                  d«      «       Œ¡ |j5                  «        �Œ �nt        |«      D �]  \  }
}t        t9        |j;                  «       D �cg c]  }t=        |«      ‘Œ c}Ž «      D ]É  \  }}d||<   t)         | |	Ž «      }t+        t,        |«      }t        |«      D ]Ž  \  }}|j!                  |«      \  }}|r.|�,|j/                  «       |j/                  «       k7  rt1        d	«      ‚|€||
   |   |   j5                  «        Œf||
   |   |   j%                  |j7                  d«      «       Œ� d||<   ŒË �Œ ddd«       |S c c}w # 1 sw Y   |S xY w)a«  Compute the analytical Jacobian using forward mode AD of `fn(inputs)` using forward mode AD with respect to `target`.

    Return N * M Jacobians where N is the number of tensors in target that require grad and
    M is the number of non-integral outputs.
    Contrary to other functions here, this function requires "inputs" to actually be used by the function.
    The computed value is expected to be wrong if the function captures the inputs by side effect instead of
    using the passed ones (many torch.nn tests do this).

    Args:
        fn: the function to compute the jacobian for
        inputs: inputs to `fn`
        outputs: provide precomputed outputs to avoid one extra invocation of fn
        check_grad_dtypes: if True, will check that the gradient dtype are valid
        all_u (optional): if provided, the Jacobian will be right multiplied with this vector

    Returns:
        A tuple of M N-tuples of tensors
    c              3   óR   K  — | ]  }t        |«      sŒ|j                  sŒ|–— Œ! y ­wr   )r   r1   ©r‡   r�   s     r   rŠ   z6_get_analytical_jacobian_forward_ad.<locals>.<genexpr>  s   è ø€ ÒU ¬~¸aÕ/@ÀQÇ_Ã_œ!ÑUùs   ‚'“' 'c              3   ó<   K  — | ]  }|j                  «       –— Œ y ­wr   r«   r  s     r   rŠ   z6_get_analytical_jacobian_forward_ad.<locals>.<genexpr>	  s   è ø€ Ò
1˜aˆ1�<‰<�>Ñ
1ùr®   zJExpected inputs to be non-complex for _get_analytical_jacobian_forward_ad.c              3   ó6   •K  — | ]  }t        ‰d «      –— Œ y­w)rM   N)r@   ©r‡   r�   r·   s     €r   rŠ   z6_get_analytical_jacobian_forward_ad.<locals>.<genexpr>  s   øè ø€ ò 
Ø=>Ô,¨W°a×8ñ
ùrÑ   c              3   óR   •K  — | ]  }t        ‰|j                  «       «      –— Œ  y ­wr   )r@   r3   r
  s     €r   rŠ   z6_get_analytical_jacobian_forward_ad.<locals>.<genexpr>  s$   øè ø€ ò 
ØEFÔ,¨W°a·g±g³i×@ñ
ùó   ƒ$'ú7MKLDNN inputs are not support for forward AD gradcheck.rM   Nz'Forward AD gradient has dtype mismatch.r   rP   rÂ   ç        )r   ÚautogradÚ
forward_adr5   r²   r³   Ú
dual_levelr™   r   r1   r   rU   Ú	make_dualr^   Ú
zeros_likerÜ   Úunpack_dualr´   rÓ   Úview_asr±   Úfilterr,   r+   r   Úsqueeze_Úzero_r‘   r   r   r~   )rµ   r¶   r·   r  r  ÚfwADÚtensor_inputsrº   Úfw_gradsÚdual_inputsr�   r½   Úfw_gradÚuÚraw_outputsÚdual_outputsÚindex_oÚd_oÚvalÚresr£   Úlin_idxÚgrad_idxs     `                    r   Ú#_get_analytical_jacobian_forward_adr'  ï  sƒ  ø€ ô, �>‰>×$Ñ$€DäÑU VÔUÓU€Mä
Ñ
1 =Ô
1Ô1ÜØXó
ð 	
ñ Üó 
ØBOô
ó 
‰	ô ó 
ØJWô
ó 
ˆ	ð 
�‰Ó	ñ ?,ØˆØˆÜ Ó'ò 	$‰FˆAˆsÜ˜cÔ" s×'8Ò'8Ø—:‘:¤§¡Ò.Ü$ØQóð ð —n‘n S§Z¡Z£\´5×3CÑ3CÀCÓ3HÓI�ð —‘ × 0Ñ 0°Ó 5°aÑ 8Ô9Ø×Ñ˜sÕ#ð	$ò ô $-¬S°¸5Ó-AÓ#Bó  ‘�‘<�G˜QØ—‘˜aŸi™i¨Ó0Ô1Ü'©¨KÐ(8Ó9�Ü%Ô&AÀ;ÓO�Ü$-¨lÓ$;ò E‘L�G˜SØ#×/Ñ/°Ó4‘H�C˜á)Ø˜OØŸN™NÓ,°·±Ó0@Ò@ä,Ð-VÓWÐWð ˜a‘L Ñ)×2Ñ2°1Ô5Ø�{Ø! !™ WÑ-×3Ñ3Õ5à! !™ WÑ-×3Ñ3°C·K±KÀ³OÕDðEð —‘–ò' ô, (¨Ó1ó ,‘
��7Ü)2Ü°·±³Ö?¨1œe A�hÒ?Ð@ó*ò ,Ñ%�G˜Xð ),�G˜HÑ%Ü"+©B°Ð,<Ó"=�KÜ#)Ô*EÀ{Ó#S�LÜ(1°,Ó(?ò R™˜ Ø#'×#3Ñ#3°CÓ#8™˜˜Sá-Ø # Ø #§¡Ó 0°C·N±NÓ4DÒ Dä"0Ø Ió#ð ð ˜;Ø% a™L¨Ñ1°'Ñ:×@Ñ@ÕBà% a™L¨Ñ1°'Ñ:×@Ñ@ÀÇÁÈRÃÕQðRð ),�G˜HÒ%ò+,ð,÷S?,ðB Ðùò- @÷W?,ðB Ðús    ÂHNÊNÊ&CNÎNÎNc                 ó¶   — | j                   t        j                  k(  r| j                  «       }|S t	        | «      r| j                  «       }|S | j                  }|S r   )r   r   rU   rš   r(   rÒ   r—   )r¯   r  s     r   Ú_get_input_to_perturbr)  [  s[   € ð ‡|�|”u—}‘}Ò$à Ÿ>™>Ó+Ðð Ðô 
˜uÔ	%ð !Ÿ;™;›=Ðð Ðð !Ÿ:™:ÐØÐr   c                 ó"   ‡ ‡‡‡‡— ˆˆ ˆˆˆfd„}|S )Nc                  ó�   •— t        ˆˆˆfd„t        t        ‰«      «      D «       «      } t        d„ t         ‰| Ž «      D «       «      S )Nc              3   óf   •K  — | ](  \  }}t        |«      rt        ||‰k(  r‰nd ‰«      n|–— Œ* y ­wr   )r   rñ   )r‡   r�   r»   rð   rÿ   r  s      €€€r   rŠ   z;_with_prepare_inputs.<locals>.wrapped_fn.<locals>.<genexpr>n  sC   øè ø€ ò 
ñ ��1ô ˜aÔ ô ˜1°!°y².Ñ.ÀdÈIÔVàóñ
ùs   ƒ.1c              3   ó<   K  — | ]  }|j                  «       –— Œ y ­wr   )rÒ   )r‡   r»   s     r   rŠ   z;_with_prepare_inputs.<locals>.wrapped_fn.<locals>.<genexpr>t  s   è ø€ Ò< 1�Q—W‘W—YÑ<ùr®   )r5   r™   r±   )r½   rð   rµ   rÿ   r  r¶   s    €€€€€r   r  z(_with_prepare_inputs.<locals>.wrapped_fnm  sB   ø€ Üõ 
ô "¤)¨FÓ"3Ó4ô	
ó 
ˆô Ñ<¬	±"°c°(Ó(;Ô<Ó<Ð<r   r   )rµ   r¶   rÿ   r  rð   r  s   ````` r   rû   rû   k  s   ü€ ÷=ð =ð Ðr   c                 ó   ‡ ‡‡‡— ˆˆˆˆ fd„}|S )Nc                 ó"   •— t        ‰‰| ‰‰«      S r   )rÙ   )rÞ   r¹   r  rÍ   r  s    €€€€r   rÝ   z%_get_numerical_jvp_fn.<locals>.jvp_fn{  s   ø€ Ü*ØÐ(¨%°°nó
ð 	
r   r   )r  r  r¹   rÍ   rÝ   s   ```` r   rþ   rþ   y  s   û€ ÷
ð
 €Mr   c                 óÎ   — t        | t        «      r8t        | d   «      s(| d   j                  |«      | d   j                  |«      fS | S t        | «      s| j                  |«      S | S ©Nr   rM   )rD   r5   r(   r‘   )r  rX   s     r   Ú_reshape_tensor_or_tupler2  ƒ  sb   € ä�!”UÔÜ$ Q q¡TÔ*Ø�a‘D—L‘L Ó'¨¨1©¯©°eÓ)<Ð=Ð=ð €Hô % QÔ'Ø—9‘9˜UÓ#Ð#Ø€Hr   c                 óL   — t        | t        «      r|| d   z  || d   z  fS || z  S r1  )rD   r5   )r  rˆ   s     r   Ú_mul_tensor_or_tupler4  Ž  s0   € Ü�!”UÔØ�A�a‘D‘˜!˜a ™d™(Ð#Ð#à�1‰uˆr   c                 ó  — ||   }t        |«      }t        | |||d«      }t        j                  t        |¬«      }	t        ||||	«      }
t        ||j                  «      }t        ||«      }t        |
||j                  «       |«      S )NT)r¹   )r)  rû   rü   rý   rù   rþ   r2  rX   r4  ræ   r+   )rµ   rÿ   r¶   r  r¹   r°   r¯   r  r  rÍ   rÝ   s              r   Ú%_get_numerical_jvp_wrt_specific_inputr6  •  s�   € ð �9Ñ€EÜ,¨UÓ3ÐÜ% b¨&°)Ð=MÈtÓT€JÜ×&Ñ&Ô'JÐPSÔT€NÜ" :Ð/?ÀÀnÓU€FÜ  Ð$4×$:Ñ$:Ó;€AÜ˜Q Ó$€AÜ5Ø��5×#Ñ#Ó% }óð r   c           	      ó¬  — g }t        ||«      D ]Â  \  }	}
t        | |	||
||«      }g }t        |«      }t        |«      t        |«      k(  sJ ‚t        ||«      D ]"  \  }}t	        |«      r|j                  |«       Œ#Œ$ |�Cg }t        ||«      D ]   \  }}|j                  t        ||«      «       Œ" |j                  |«       Œ²|j                  |«       ŒÄ |S r   )r´   r6  r±   r|   r,   rÜ   Ú_dot_with_type_promotion)rµ   r¶   r¼   Úfunc_outr  Úall_vr¹   r°   Úreduced_jacobiansr¾   r  Úall_JuÚfiltered_JuÚJuÚoutputÚjacobian_scalarsrÕ   s                    r   Ú_get_numerical_vJurA  ¤  sü   € ð 35ÐÜ˜+ uÓ-ò 2‰
ˆ�Ü6Ø�˜  C¨ó
ˆð ˆÜ˜XÓ&ˆÜ�6‹{œc (›mÒ+Ð+Ð+Ü˜f hÓ/ò 	‰JˆB�Ü*¨6Ô2Ø×"Ñ" 2Õ&ð ð	ð ÐØ35ÐÜ˜U KÓ0ò I‘��2Ø ×'Ñ'Ô(@ÀÀBÓ(GÕHðIà×$Ñ$Ð%5Õ6à×$Ñ$ [Õ1ð)2ð* Ðr   c                 ó    — t        | |«      D ]?  \  }}|j                  «       dk7  sŒ||z
  j                  «       j                  «       |kD  sŒ? y y)Nr   FT)r´   r3   Úabsrm   )Új1Új2ÚatolÚj1_xÚj2_xs        r   Ú_check_jacobians_equalrI  Á  sP   € ô ˜"˜b“kò ‰
ˆˆdØ�:‰:‹<˜1Ó $¨¡+×!2Ñ!2Ó!4×!8Ñ!8Ó!:¸TÓ!AÙðð r   c                 óR  — t        ||«      }t        t        |d«      «      }d}d}t        | «      D ]ð  \  }}||   }	||   }
t        |«      D ]Ó  \  }}|�$|j	                  «       |	j	                  «       k7  rd}n|�|j
                  |	j
                  k7  rd}|€|
d d …|f   j                  «        Œc|j                  t        j                  k(  s|j                  «       n|}|
d d …|f   j                  «       |j                  «       k(  sJ ‚|j                  d«      |
d d …|f<   ŒÕ Œò |||fS )NTFrP   )r9   rQ   rH   r™   r   r<   r  r   r   r4   rš   r3   r‘   )Úlist_of_list_of_tensorsr¶   Únumel_outputsÚout_jacobiansÚdiff_input_listÚcorrect_grad_sizesÚcorrect_grad_typesr�   Útensor_listr½   Úout_jacobianÚjrc   Údenses                 r   Ú_stack_and_check_tensorsrU  Ê  s9  € ô
 4°F¸MÓJ€MÜœ=¨°Ó6Ó7€OØÐØÐÜ#Ð$;Ó<ò 7‰ˆˆ;Ø˜aÑ ˆØ$ QÑ'ˆÜ" ;Ó/ò 	7‰IˆAˆvØÐ! f§k¡k£m°s·x±x³zÒ&AØ%*Ñ"ØÐ#¨¯©¸¿	¹	Ò(AØ%*Ð"Øˆ~ØšQ ˜TÑ"×(Ñ(Õ*ð .4¯]©]¼e¿m¹mÒ-K�F—O‘OÔ%ÐQWð ð $¢A q DÑ)×/Ñ/Ó1°U·[±[³]ÒBÐBÐBØ%*§]¡]°2Ó%6�šQ ˜TÒ"ñ	7ð7ð  Ð,Ð.@Ð@Ð@r   aù  

NOTE: If your op relies on non-deterministic operations i.e., it is listed here:
https://pytorch.org/docs/stable/generated/torch.use_deterministic_algorithms.html
this failure might be expected.

If you are adding a new operator, please file an issue and then use one of the
workarounds. The workaround depends on how your test invokes gradcheck/gradgradcheck.
If the test
- manually invokes gradcheck/gradgradcheck, then call gradcheck/gradgradcheck
  with `nondet_tol=<tol>` as a keyword argument.
- is OpInfo-based (e.g., in test_ops_gradients.py), then modify the OpInfo for the test
  to have `gradcheck_nondet_tol=<tol>`.
- is a Module test (e.g., in common_nn.py), then modify the corresponding
  module_test entry to have `gradcheck_nondet_tol=<tol>`
c                 ó  ‡‡— t        t        | d«      «      Šˆˆfd„}|r7t        |‰j                  «       |«      }t        |‰j                  «       |«      }n4t	        |‰j                  «       «      }t	        |‰j                  «       «      }|s‰j                  «       nd}	t        || |	«      \  }
}}t        || |	«      \  }}}t        |
||«      }|s|rt        d«      ‚|st        d«      ‚|st        d|› d�t        z   «      ‚|
S )NTc                 óL   •— t         j                  j                  ‰‰| dd¬«      S ©NT©Úretain_graphÚallow_unused©r   r  Úgrad©Úgrad_outputrN  r?  s    €€r   Úvjp_fnz5_check_analytical_jacobian_attributes.<locals>.vjp_fn  ó+   ø€ Ü�~‰~×"Ñ"Ø�O [¸tÐRVð #ó 
ð 	
r   rM   zGradient has dtype mismatchz&Analytical gradient has incorrect sizezÖBackward is not reentrant, i.e., running backward with same input and grad_output multiple times gives different values, although analytical gradient matches numerical gradient.The tolerance for nondeterminism was rô   )
rQ   rH   Ú(_get_analytical_vjps_wrt_specific_outputrÒ   Ú!_compute_analytical_jacobian_rowsr3   rU  rI  r   ÚFAILED_NONDET_MSG)r¶   r?  Ú
nondet_tolr  rð   rÕ   r`  Úvjps1Úvjps2Úoutput_numelÚ
jacobians1Útypes_okÚsizes_okÚ
jacobians2Ú_Ú	reentrantrN  s    `              @r   Ú%_check_analytical_jacobian_attributesro  ÷  s  ù€ ô œ=¨°Ó6Ó7€Oõ
ñ Ü8¸ÀÇÁÃÐQRÓSˆÜ8¸ÀÇÁÃÐQRÓS‰ä1°&¸&¿,¹,».ÓIˆÜ1°&¸&¿,¹,».ÓIˆá)2�6—<‘<”>¸€LÜ%=Øˆv�|ó&Ñ"€J�˜(ô 0°°v¸|ÓLÑ€J��1Ü& z°:¸zÓJ€IáÑ)ÜÐ:Ó;Ð;ÙÜÐEÓFÐFÙÜð4ð 5?°<¸qðBô EVñVó
ð 	
ð Ðr   c           
      óB  — g }t        ||«      D �]  \  }}t        | |||d|¬«      }	g }
t        |	|«      D ]Ó  \  }}|j                  j                  d«      }|j	                  «       rƒt        j                  |j                  «       «      }|j                  dd«      }|j                  dd«      }|
j                  |j                  |d   «      d|j                  |d   «      z  z   «       Œ´|
j                  |j                  |«      «       ŒÕ |j                  |
«       �Œ |S )NT)rð   rÕ   r   rP   rM   rÛ   )r´   ro  ÚTÚsqueezer+   r   Úview_as_realÚresolve_conjÚselectrÜ   Údot)r¶   r·   re  r  r:  r  r;  r?  rÕ   Úall_vJr@  ÚvJr  ÚtvÚtrÚtis                   r   Ú!_get_analytical_vJu_backward_moder|  #  s  € ð 35ÐÜ˜ %Ó(ó 3‰	ˆ�Ü6Ø�F˜JÐ(9ÀTÈQô
ˆð 02ÐÜ˜ Ó'ò 
	3‰EˆB�ð —‘—‘˜a“ˆBØ�}‰}ŒÜ×'Ñ'¨¯©Ó(9Ó:�Ø—Y‘Y˜r 1Ó%�Ø—Y‘Y˜r 1Ó%�Ø ×'Ñ'¨¯©¨q°©t«°r¸B¿F¹FÀ1ÀQÁ4»LÑ7HÑ(HÕIà ×'Ñ'¨¯©¨q«	Õ2ð
	3ð 	× Ñ Ð!1Ö2ð!3ð" Ðr   aB  `get_analytical_jacobian` was part of PyTorch's private API and not meant to be exposed. We are deprecating it and it will be removed in a future version of PyTorch. If you have a specific use for this or feature request for this to be a stable API, please file us an issue at https://github.com/pytorch/pytorch/issues/newr  c                 ó„  ‡‡— |dk7  rt        d«      ‚‰j                  «       rt        d«      ‚t        t        | d«      «      Šˆˆfd„}t	        |‰j                  «       «      }t	        |‰j                  «       «      }‰j                  «       }t        || |«      \  }}	}
t        || |«      \  }}}t        |||«      }|||
|	fS )NrÂ   zbExpected grad_out to be 1.0. get_analytical_jacobian no longer supports values of grad_out != 1.0.ztExpected output to be non-complex. get_analytical_jacobian no longer supports functions that return complex outputs.Tc                 óL   •— t         j                  j                  ‰‰| dd¬«      S rX  r\  r^  s    €€r   r`  z'get_analytical_jacobian.<locals>.vjp_fnT  ra  r   )	r³   r+   rQ   rH   rc  rÒ   r3   rU  rI  )r¶   r?  re  rÈ   r`  rf  rg  rh  ri  rj  rk  rl  rm  rn  rN  s    `            @r   r   r   ;  sÔ   ù€ ð 	�CŠäð2ó
ð 	
ð ×ÑÔÜðEó
ð 	
ô œ=¨°Ó6Ó7€Oõ
ô .¨f°f·l±l³nÓE€EÜ-¨f°f·l±l³nÓE€Eà—<‘<“>€LÜ%=Øˆv�|ó&Ñ"€J�˜(ô 0°°v¸|ÓLÑ€J��1Ü& z°:¸zÓJ€Ià�y (¨HÐ4Ð4r   c                 óB   — t        | ||   t        d«      d¬«      }||   S )NÚinfF)re  r  )ro  Úfloat)r¶   r·   rÿ   Ú
output_idxrº   s        r   Ú_get_analytical_jacobianrƒ  g  s.   € ô 6Ø�˜
Ñ#´°e³ÐPUô€Ið �YÑÐr   c           	      ó®  — t        j                  |t         j                  ¬«      }|j                  d«      }g }t	        |j                  «       «      D ]€  }|j                  «        d||<    | |«      }t        |«      D ]S  \  }}|dk(  r|j                  g «       ||xx   t        |t         j                  «      r|j                  «       nd gz  cc<   ŒU Œ‚ |S )N©Úmemory_formatrP   rÂ   r   )r   r  Úlegacy_contiguous_formatÚviewr~   r3   r  r™   rÜ   rD   ÚTensorrÒ   )	r`  Úsample_outputÚgrad_out_baseÚflat_grad_outÚjacobians_rowsrS  Úgrad_inputsr�   Úd_xs	            r   rc  rc  p  sÕ   € ô ×$Ñ$Ø¤U×%CÑ%Cô€Mð "×&Ñ& rÓ*€Mà9;€NÜ�=×&Ñ&Ó(Ó)ò 	ˆØ×ÑÔØˆ�aÑÙ˜]Ó+ˆÜ Ó,ò 	‰FˆAˆsØ�AŠvØ×%Ñ% bÔ)Ø˜1ÓÜ)¨#¬u¯|©|Ô<�—	‘	”À$ð"ñ Ôñ	ð		ð Ðr   c                 óÆ   —  | |j                  |j                  «      «      }|D �cg c]/  }t        |t        j                  «      r|j                  «       nd g‘Œ1 }}|S c c}w r   )r‘   rX   rD   r   r‰  rÒ   )r`  rŠ  rÕ   rŽ  ÚvjpÚvjpss         r   rb  rb  ‹  s[   € ñ ˜Ÿ™ =×#6Ñ#6Ó7Ó8€KàLWö0ØEHœ
 3¬¯©Ô5ˆ�‰Œ¸4Ò@ð0€Dð 0ð €Kùò0s   ¦4Ac           	      ód  — d}t        | «      D �]  \  }}t        |«      sŒ|j                  sŒ |j                  t        j
                  k(  s6|j                  t        j                  k(  st        j                  d|› d�«       |j                  r|j                  «       }nt        |«      r|j                  «       }n|}|j                  t        j                  urGt        d„ t!        |j#                  «       |j%                  «       «      D «       «      st'        d|› d�«      ‚d}�Œ |st)        d«      ‚y)	NFzInput #z± requires gradient and is not a double precision floating point or complex. This check will likely fail if all the inputs are not of double precision floating point or complex. c              3   ó:   K  — | ]  \  }}|d kD  xs |dk  –— Œ y­w)r   rM   Nr   )r‡   ÚstÚszs      r   rŠ   z _check_inputs.<locals>.<genexpr>ª  s,   è ø€ ò á˜˜Bð ˜‘FÒ%˜b A™gÓ%ñùs   ‚zThe záth input has a dimension with stride 0. gradcheck only supports inputs that are non-overlapping to be able to compute the numerical gradients correctly. You should call .contiguous on the input before passing it to gradcheck.Tzngradcheck expects at least one input tensor to require gradient, but none of the them have requires_grad=True.)r™   r   r1   r<   r   Úfloat64Ú
complex128ÚwarningsÚwarnÚ	is_sparser‹   r%   rf   r   rU   Úallr´   r`   r   ÚRuntimeErrorr³   )Útupled_inputsÚany_input_requiring_gradr÷   r½   Úcontents        r   Ú_check_inputsr¡  •  s  € à$ÐÜ˜mÓ,ó ,‰ˆˆSÜ˜#Õ 3×#4Ó#4Ø—I‘I¤§¡Ò.°#·)±)¼u×?OÑ?OÒ2OÜ—‘Ø˜c˜Uð #Jð Jôð �}Š}ØŸ+™+›-‘Ü-¨cÔ2ØŸ*™*›,‘à�ð �~‰~¤U§]¡]Ñ2Üñ ä"% g§n¡nÓ&6¸¿¹»Ó"Gôô ô 'Ø˜s˜eð $Sð Sóð ð (,Ò$ð9,ñ< $Üð<ó
ð 	
ð r   c                 óx   — t        d„ | D «       «      rt        d«      ‚t        d„ | D «       «      rt        d«      ‚y )Nc              3   óh   K  — | ]*  }t        |t        j                  «      sŒt        |«      –— Œ, y ­wr   )rD   r   r‰  r(   ©r‡   r7   s     r   rŠ   z!_check_outputs.<locals>.<genexpr>¿  s#   è ø€ Ò
T¨¼
À1ÄeÇlÁlÕ8SÔ  ×#Ñ
Tùs   ‚2¢2zySparse output is not supported at gradcheck yet. Please call to_dense(masked_grad=...) on the output of fn for gradcheck.c              3   óŒ   K  — | ]<  }t        |t        j                  «      sŒ|j                  t        j                  k(  –— Œ> y ­wr   )rD   r   r‰  r   rU   r¤  s     r   rŠ   z!_check_outputs.<locals>.<genexpr>Æ  s*   è ø€ Ò
U¨¼ÀAÄuÇ|Á|Õ9Tˆ1�8‰8”u—}‘}Õ$Ñ
Uùs
   ‚A¢"AzyMKLDNN output is not supported at gradcheck yet. Please call to_dense(masked_grad=...) on the output of fn for gradcheck.)r²   r³   )r·   s    r   Ú_check_outputsr¦  ¾  sN   € Ü
Ñ
T¨WÔ
TÔTô ðWó
ð 	
ô Ñ
U¨gÔ
UÔUÜðWó
ð 	
ð Vr   c                ó¤   — t        | ||||¬«      }|D ];  }|D ]4  }t        j                  |d«      j                  «       dkD  sŒ+t	        d«      ‚ Œ= y)N©r¹   r°   r   ú3Numerical gradient for function expected to be zeroT)r¿   r   Únere   r   )Úfuncr¶   r9  r¹   r°   Újacobians_all_inputs_outputsÚ%jacobians_all_outputs_and_fixed_inputrë   s           r   Ú _check_no_differentiable_outputsr®  Í  sm   € ô
 $;Øˆf�h C°}ô$Ð ð 2Nò Ð-Ø=ò 	ˆHÜ�x‰x˜ !Ó$×(Ñ(Ó*¨QÓ.Ü$ØIóð ñ	ðð r   c                 ó  — t        ||«      D ]q  \  }}t        | ||||«      }	|	D ]X  }
|
j                  «       dk(  rŒ|
t        j                  |
«      z
  j                  «       j                  «       |kD  sŒOt        d«      ‚ Œs y)Nr   r©  T)r´   r6  r3   r   r  rC  rm   r   )r«  r9  Ú
all_inputsÚinputs_indicesr  r¹   re  r¾   r  rà   Újvps              r   Ú%_check_no_differentiable_outputs_fastr³  Þ  s‘   € ô ˜.¨%Ó0ò ‰
ˆ�Ü4°T¸7ÀJÐPQÐSVÓWˆØò 	ˆCØ�y‰y‹{˜aÒØØ”e×&Ñ& sÓ+Ñ+×0Ñ0Ó2×6Ñ6Ó8¸:ÓEÜ$ØIóð ñ		ðð r   a�  
gradcheck or gradgradcheck failed while testing batched gradient computation.
This could have been invoked in a number of ways (via a test that calls
gradcheck/gradgradcheck directly or via an autogenerated test).

If you are adding a new operator, please file an issue and then use one of the
workarounds. The workaround depends on how your test invokes gradcheck/gradgradcheck.
If the test
- manually invokes gradcheck/gradgradcheck, then call gradcheck/gradgradcheck
  with `check_batched_grad=False` as a keyword argument.
- is OpInfo-based (e.g., in test_ops_gradients.py), then modify the OpInfo for the test
  to have `check_batched_grad=False` and/or `check_batched_gradgrad=False`.

If you're modifying an existing operator that supports batched grad computation,
or wish to make a new operator work with batched grad computation, please read
the following.

To compute batched grads (e.g., jacobians, hessians), we vmap over the backward
computation. The most common failure case is if there is a 'vmap-incompatible
operation' in the backward pass. Please see
NOTE: [How to write vmap-compatible backward formulas]
in the codebase for an explanation of how to fix this.
a  
gradcheck failed while testing batched gradient computation with forward-mode AD.
This test is enabled automatically when both `check_batched_grad=True`
and `check_forward_ad=True`, but can be disabled in the following ways
dependong on how the test was invoked (via a test that calls gradcheck
directly or via an autogenerated test).

If you are adding a new operator, please file an issue and then use one of the
workarounds. The workaround depends on how your test invokes gradcheck/gradgradcheck.
If the test
- manually invokes gradcheck/gradgradcheck, then call gradcheck/gradgradcheck
  with `check_batched_forward_grad=False` as a keyword argument.
- is OpInfo-based (e.g., in test_ops_gradients.py), then modify the OpInfo for the test
  to have `check_batched_forward_grad=False`
c                 óZ   — d| › d|› d|rt         nt        › d|› d|› d�j                  «       S )Nz
For output z and input z:

z

Got:
z

Expected:
ú
)ÚFAILED_BATCHED_GRAD_MSG_FWD_ADÚFAILED_BATCHED_GRAD_MSGÚstrip)r‚  rÿ   r$  Úexpr°   s        r   Ú!_get_failed_batched_grad_test_msgrº    sT   € ðØˆ<�{ 9 +ð .á#0ÕÔ6MÐ Nð Oð €ð ð €ð ð
÷ 
�EƒGð
r   c                 ó0  ‡ ‡‡‡‡— t         j                  j                  Št        ‰t        «      sJ ‚t        ‰«      D �]   \  ŠŠt        ‰«      r‰j                  sŒdt         j                  fˆˆ ˆˆˆfd„}t        ‰«      sŒEt        d«      D �cg c]  }t        j                  ‰«      ‘Œ }}|D �cg c]
  } ||«      ‘Œ }}t        |Ž D �cg c]  }t        j                  |«      ‘Œ }}	  t        |«      t        j                  |«      «      }t        t        ||«      «      D ]7  \  Š\  }
}t        j$                  |
|«      rŒ t!        t'        ‰‰|
|d¬«      «      ‚ �Œ# yc c}w c c}w c c}w # t        $ r}	t!        d|	› dt"        › �«      |	‚d }	~	ww xY w)NÚtangentc           	      ó  •‡— ‰
j                  «       5  ‰
j                  ‰j                  «       | «      Št        ˆˆfd„t	        ‰«      D «       «      }t         ‰	|Ž «      }g }|D ]�  }|€Œ‰
j                  |«      \  }}|�|j                  |«       Œ.|j                  t        j                  g |j                  |j                  ¬«      j                  |j                  «      «       Œƒ t        |«      cd d d «       S # 1 sw Y   y xY w)Nc              3   ól   •K  — | ]+  \  }}|‰k(  r‰nt        |«      r|j                  «       n|–— Œ- y ­wr   )r   r^   )r‡   r÷   r½   Údualrÿ   s      €€r   rŠ   z=_test_batched_grad_forward_ad.<locals>.jvp.<locals>.<genexpr>1  sA   øè ø€ ò )ñ !˜˜Sð ˜iÒ'ñ ä*8¸Ô*=˜#Ÿ*™*œ,À3óHñ)ùs   ƒ14rK   )r  r  r^   r5   r™   r±   r  rÜ   r   r]   r<   r=   ÚexpandrX   )r¼  Úinputs_with_dualr   rÌ   Údual_outputÚ
primal_outÚtangent_outr¿  Úcurrent_inputr«  r  rÿ   r¶   s          @€€€€€r   r²  z*_test_batched_grad_forward_ad.<locals>.jvp.  sô   ù€ Ø—‘Ó"ñ "Ø—~‘~ m×&:Ñ&:Ó&<¸gÓF�Ü#(ô )ô %.¨fÓ$5ô	)ó $Ð ô  )©Ð/?Ð)@ÓA�Ø�Ø#/ò �KØ"Ð*Ø Ø.2×.>Ñ.>¸{Ó.KÑ+�J Ø"Ð.ØŸ
™
 ;Õ/àŸ
™
Ü!ŸK™KØ "¨*×*:Ñ*:À:×CTÑCTôç$™f Z×%5Ñ%5Ó6õðô ˜S“z÷-"÷ "ò "ús   “CC<Ã<Dr…   ú(While computing batched gradients, got: ú

T©r°   )r   r  r  rD   r5   r™   r   r1   r‰  r,   r~   Ú
randn_liker´   r“   r	   r�  r   r¶  Úallcloserº  )r«  r¶   r²  rm  Útangentsr7   ÚexpectedÚshardsÚresultÚexr$  r¹  rÅ  r  rÿ   s   ``          @@@r   Ú_test_batched_grad_forward_adrÐ  &  s…  ü€ Ü�>‰>×$Ñ$€DÜ�fœeÔ$Ð$Ð$ä$-¨fÓ$5ó 3Ñ ˆ	�=Ü˜}Ô-°-×2MÒ2MØð	"œŸ™÷ 	"ñ 	"ô2 +¨=Ô9Øä=BÀ1»XÖF¸”E×$Ñ$ ]Õ3ÐFˆÐFØ$,Ö-˜q‘C˜•FÐ-ˆÐ-Ü69¸8°nÖE¨F”E—K‘K Õ'ÐEˆÐEð	Ø”U˜3“Z¤§¡¨HÓ 5Ó6ˆFô &/¬s°6¸8Ó/DÓ%Eò 	Ñ!ˆI‘z˜˜SÜ�~‰~˜c 3Ô'ØÜ Ü1Ø˜y¨#¨sÀ$ôóð ò	ðY3ðh ùò) GùÚ-ùÚEøô ò 	ä Ø:¸2¸$¸dÔCaÐBbÐcóàðûð	ús*   ÂE"Â1E'ÃE,Ã+$E1Å1	FÅ:FÆFc           	      ór  ‡‡— t        t        | d«      «      Št        j                  t        j
                  j                  |‰dd¬«      Šˆˆfd„}t        d«      D �cg c]  }t	        j                  |«      ‘Œ }}|D �cg c]
  } ||«      ‘Œ }}t        |Ž D �cg c]  }t	        j                  |«      ‘Œ }}t        j                  «       5  t        j                  dd¬«       t        j                  dd¬«       	  t        |«      t	        j                  |«      «      }		 d d d «       t%        t        	|«      «      D ]5  \  }\  }}t	        j&                  ||«      rŒ t!        t)        ||||«      «      ‚ yc c}w c c}w c c}w # t        $ r}
t!        d	|
› d
t"        › �«      |
‚d }
~
ww xY w# 1 sw Y   Œ�xY w)NTrY  c                 óP   •—  ‰| «      }t        d„ t        |‰«      D «       «      }|S )Nc              3   ó´   K  — | ]P  \  }}|�|nDt        j                  g |j                  |j                  ¬«      j	                  |j
                  «      –— ŒR y ­w)NrK   )r   r]   r<   r=   rÀ  rX   )r‡   r]  r½   s      r   rŠ   z2_test_batched_grad.<locals>.vjp.<locals>.<genexpr>r  sS   è ø€ ò 
ñ ��cð Ðñ ä—‘˜R s§y¡y¸¿¹ÔD×KÑKÈCÏIÉIÓVóWñ
ùs   ‚AA)r5   r´   )rÕ   ÚresultsrN  r]  s     €€r   r‘  z_test_batched_grad.<locals>.vjpp  s3   ø€ Ù�q“'ˆÜñ 
ô ! ¨/Ó:ô	
ó 
ˆð ˆr   r…   ÚignorezThere is a performance drop)ÚmessagezPlease use `torch.vmap`rÆ  rÇ  )rQ   rH   rü   rý   r   r  r]  r~   rÉ  r´   r“   r™  Úcatch_warningsÚfilterwarningsr
   r�  r   r·  r™   rÊ  rº  )r¯   r?  r‚  r‘  rm  Úgrad_outputsÚgOrÌ  rÍ  rÎ  rÏ  rÿ   r$  r¹  rN  r]  s                 @@r   Ú_test_batched_gradrÛ  a  s›  ù€ ô œ=¨°Ó5Ó6€OÜ×ÑÜ�‰×ÑØØØØô€Dõô 7<¸A³hÖ?°”E×$Ñ$ VÕ,Ð?€LÐ?à".Ö/˜B‘�B•Ð/€HÐ/Ü25°x°.ÖA¨”—‘˜FÕ#ÐA€HÐAô 
×	 Ñ	 Ó	"ñ Ü×Ñ Ð2OÕPÜ×Ñ Ð2KÕLð		Ø”T˜#“YœuŸ{™{¨<Ó8Ó9‰F÷	ô "+¬3¨v°xÓ+@Ó!Aò 
Ñˆ	‘:�C˜Ü�>‰>˜#˜sÔ#ØÜÜ-¨j¸)ÀSÈ#ÓNó
ð 	
ð
ð ùò7 @ùâ/ùÚAøô ò 	ô
 !Ø:¸2¸$¸dÔCZÐB[Ð\óàðûð	ú÷ð ús<   ÁE7Á?E<ÂFÃ/F-Ã<$FÆ	F*ÆF%Æ%F*Æ*F-Æ-F6c                 óö  — t        t        |d«      «      }|st        d«      ‚t        j                  j                  | || D �cg c]'  }t        j                  |t        j                  ¬«      ‘Œ) c}d¬«      }t        ||«      D �]n  \  }}|€Œ
t        |t        j                  «      �rv|j                  t        j                  k7  �rX|j                  |j                  k7  r=t        dt        |j                  «      z   dz   t        |j                  «      z   dz   «      ‚t        |«      r×t        |j                  «      j                  dd	«      j                  d
d	«      }|j!                  «       |j!                  «       k7  r0t        d|› d|j!                  «       › d|j!                  «       › �«      ‚|j#                  «       |j#                  «       k7  r0t        d|› d|j#                  «       › d|j#                  «       › �«      ‚|j%                  «       }|j%                  «       }|r4t        j&                  |t        j                  |«      «      s5t        d«      ‚|j)                  d«      j+                  «       st        d«      ‚|j,                  |j,                  k7  rt        d«      ‚|j.                  |j.                  k7  rt        d«      ‚|j1                  «       |j1                  «       k7  s�Œft        d«      ‚ yc c}w )NTú(no Tensors requiring grad found in inputr…  ©r[  zgrad is incorrect layout (z is not ú)ztorch.ró   Ú_coozgrad is z& tensor, but has incorrect sparse_dim z, expected z% tensor, but has incorrect dense_dim z&backward not multiplied by grad_outputr   zgrad is incorrect typezgrad is incorrect devicezgrad is incorrect size)rQ   rH   r   r   r  r]  r  r‡  r´   rD   r‰  r   r4   rG   r(   ÚreplacerY   Ú	dense_dimrš   rÊ  Úeqrœ  r<   r=   r   )	r·   r¶   ÚmaskedrN  r­   Úgrads_inputÚgiÚdiÚsparse_kinds	            r   Ú!_test_backward_mul_by_grad_outputré  ˜  s”  € ä*.¬}¸VÀTÓ/JÓ*K€OÙÜÐGÓHÐHÜ—.‘.×%Ñ%ØØð ö	
àô ×Ñ˜Q¬e×.LÑ.LÖMò	
ð ð &ó €Kô �k ?Ó3ó $;‰ˆˆBØˆ:ØÜ�bœ%Ÿ,™,Õ'¨B¯I©I¼¿¹Ó,FØ�y‰y˜BŸI™IÒ%Ü$Ø0Ü˜"Ÿ)™)“nñ%à ñ!ô ˜"Ÿ)™)“nñ%ð ñ	óð ô % RÔ(Ü! "§)¡)›n×4Ñ4°X¸rÓB×JÑJÈ6ÐSUÓV�Ø—=‘=“? b§m¡m£oÒ5Ü(Ø" ; -ð 0ØŸM™M›OÐ,¨K¸¿¹»Ð7HðJóð ð —<‘<“> R§\¡\£^Ò3Ü(Ø" ; -ð 0ØŸL™L›NÐ+¨;°r·|±|³~Ð6FðHóð ð —‘“ˆBØ—‘“ˆBÙÜ—>‘> "¤e×&6Ñ&6°rÓ&:Ô;Ü$Ð%MÓNÐNØ—‘�q“—‘”Ü Ð!IÓJÐJØ�8‰8�r—x‘xÒÜ Ð!9Ó:Ð:Ø�9‰9˜Ÿ	™	Ò!Ü Ð!;Ó<Ð<Ø�7‰7‹9˜Ÿ™›	Ô!Ü Ð!9Ó:Ð:ðI$;ðJ ùòW	
s   Á,K6c                 ó4  — t         j                  j                  }t        |«      \  }}t	        ||d¬«      \  }}}|j                  «       5  g }	g }
t        «       }t        |«      D ]¼  \  }}t        |«      r›|j                  r�|j                  t         j                  k(  rt        d«      ‚|j                  |j                  «       t        j                  |«      «      }|	j!                  |j#                  |«      d   «       |j%                  |«       |
j!                  |«       Œ¾ t        t'        |	|«      «      D ](  \  }\  }}|j)                  |j+                  |«      «       Œ* t        |«      D �]  \  }}||vrŒ|
|   }|j                  |j                  «       t        j                  |«      «      |
|<   t-         | |
Ž «      }t/        t0        |«      }|j                  «       |
|<   t-         | |
Ž «      }t/        t0        |«      }||
|<   t        t'        ||«      «      D ]_  \  }\  }}|j#                  |«      \  }}|j#                  |«      \  }}|€Œ4|€Œ7t        j2                  ||«      rŒNt5        d|d|dd|d	|«	      ‚ �Œ 	 d d d «       y# 1 sw Y   yxY w)
NT©Úuse_forward_adr  rM   z2Mismatch in tangent values for output with index: z when input: z! has an undefined tangent value. z Got: z but expected: )r   r  r  Ú_get_inp_tensorsÚ_make_vectorsr  Úsetr™   r   r1   r   rU   r³   r  r^   r  rÜ   r  Úaddr´   rÓ   r  r±   r  r,   rÊ  r   )r«  r·   r¶   r  Ú_inp_tensors_idxÚinp_tensorsÚ_all_vr  Ú_all_u_denser  r  Útensor_indicesr�   r½   r  r  r÷   Údual_inp_objr  Údual_outputs1Údual_outputs2r!  Úd_o1Úd_o2Ú_val1Úres1Ú_val2Úres2s                               r   Ú_test_undefined_forward_moderÿ  Î  sŠ  € Ü�>‰>×$Ñ$€Dä$4°VÓ$<Ñ!Ð�kÜ"/Ø�W¨Tô#Ñ€FˆE�<ð 
�‰Ó	ñ 7ØˆØˆÜ›ˆÜ Ó'ò 	$‰FˆAˆsÜ˜cÔ" s×'8Ò'8Ø—:‘:¤§¡Ò.Ü$ØQóð ð —n‘n S§Z¡Z£\´5×3CÑ3CÀCÓ3HÓI�ð —‘ × 0Ñ 0°Ó 5°aÑ 8Ô9Ø×"Ñ" 1Ô%Ø×Ñ˜sÕ#ð	$ô  )¬¨X°uÓ)=Ó>ò 	.‰OˆA‰|�˜Ø�M‰M˜!Ÿ)™) GÓ,Õ-ð	.ô " &Ó)ó "	‰HˆC�Ø˜.Ñ(ØØ& sÑ+ˆLð  $Ÿ~™~¨c¯j©j«l¼E×<LÑ<LÈSÓ<QÓRˆK˜ÑÜ#¡D¨+Ð$6Ó7ˆKÜ"Ô#>ÀÓLˆMð  #Ÿz™z›|ˆK˜ÑÜ#¡D¨+Ð$6Ó7ˆKÜ"Ô#>ÀÓLˆMð  ,ˆK˜Ñä)2´3°}ÀmÓ3TÓ)Uò Ñ%�™˜$ Ø"×.Ñ.¨tÓ4‘��tØ"×.Ñ.¨tÓ4‘��tà™¨©Ü Ÿ>™>¨$°Õ5Ü,ØPØ#Ø+ØØ?Ø$Ø Ø-Ø ó
ð 
òñ%"	÷+7ðp ÷q7ðp ús   ÁHJÉJÉJÉ-JÊJc           
      óŽ  ‡‡	‡
— t        t        |d«      «      Š	‰	st        d«      ‚d„ Š
ˆ	ˆ
fd„Št         | |Ž «      D �cg c]I  }t	        |t
        j                  «      r-t        j                  j                  j                  «       |«      ‘ŒK c}g}t        |d   «      dkD  r†t        t        |«      «      D ]o  }t         | |Ž «      }|j                  t        |«      D ��cg c]9  \  }}||k(  r-t        j                  j                  j                  «       |«      n|‘Œ; c}}«       Œq t        ˆfd„|D «       «      S c c}w c c}}w )NTrÝ  c                  ó.   — t        j                  d«       y )Na   Backwards compatibility: New undefined gradient support checking feature is enabled by default, but it may break existing callers of this function. If this is true for you, you can call this function with "check_undefined_grad=False" to disable the feature)r™  rš  r   r   r   Úwarn_bc_breakingz7_test_undefined_backward_mode.<locals>.warn_bc_breaking  s   € Ü�‰ðPõ	
r   c                 óz  •— | D �cg c]'  }t        j                  |t         j                  ¬«      ‘Œ) }}	 t         j                  j	                  | ‰|d¬«      }|D ]6  }|€Œ|j                  d«      j                  «       rŒ& ‰«        t        d«      ‚ yc c}w # t
        $ r} ‰«        t        d«      |‚d }~ww xY w)Nr…  TrÞ  z™Expected backward function to handle undefined output grads. Please look at "Notes about undefined output gradients" in "tools/autograd/derivatives.yaml"r   zºExpected all input grads to be undefined or zero when all output grads are undefined or zero. Please look at "Notes about undefined output gradients" in "tools/autograd/derivatives.yaml")	r   r  r‡  r  r]  r�  r   rã  rœ  )Úoutput_to_checkr­   Úgrads_outputrå  Úeræ  rN  r  s         €€r   Úcheck_undefined_grad_supportzC_test_undefined_backward_mode.<locals>.check_undefined_grad_support  sÓ   ø€ ð %ö
àô ×Ñ˜Q¬e×.LÑ.LÖMð
ˆð 
ð
	ÜŸ.™.×-Ñ-Ø °,ÈTð .ó ˆKð ò 	ˆBØ‘¨¯©¨q«¯©­Ù Ô"Ü$ð8óð ð	ð ùò1
øô ò 	ÙÔÜ ð4óð ð	ûð	ús   †,Bµ#B Â	B:Â"B5Â5B:r   rM   c              3   ó.   •K  — | ]  } ‰|«      –— Œ y ­wr   r   )r‡   r?  r  s     €r   rŠ   z0_test_undefined_backward_mode.<locals>.<genexpr>P  s   øè ø€ ÒS¸Ñ+¨F×3ÑSùs   ƒ)rQ   rH   r   Ú_differentiable_outputsrD   r   r‰  Ú_CÚ
_functionsÚUndefinedGradr|   r~   rÜ   r™   rœ  )r«  r·   r¶   r­   Úoutputs_to_checkÚundef_grad_idxr  r÷   r  rN  r  s           @@@r   Ú_test_undefined_backward_moder    s0  ú€ Ü*.¬}¸VÀTÓ/JÓ*K€OÙÜÐGÓHÐHò
õô> -©T°6¨]Ó;ö	
àä˜!œUŸ\™\Ô*ô �H‰H×Ñ×-Ñ-Ó/°Õ2ò	
ðÐô Ð˜AÑÓ !Ò#Ü#¤C¨£LÓ1ò 		ˆNÜ5±d¸F°mÓDˆOØ×#Ñ#ô
 #,¨OÓ"<÷	ñ ˜˜Qð ˜nÒ,ô —H‘H×'Ñ'×5Ñ5Ó7¸Ô:àñóõð		ô ÓSÐBRÔSÓSÐSùò+	
ùós   ¿AD<Ã >Ec                 ób   — t        | t        «      r| S t        | t        «      rt        | «      S | fS r   )rD   r5   rQ   ©rA   s    r   r±   r±   S  s+   € Ü�!”UÔØˆÜ	�A”tÔ	Ü�Q‹xˆàˆtˆr   c                 ó8   — t        d„ t        | «      D «       «      S )Nc              3   ó:   K  — | ]  }|j                   sŒ|–— Œ y ­wr   )r1   r¬   s     r   rŠ   z*_differentiable_outputs.<locals>.<genexpr>]  s   è ø€ Ò<�q¨A¯O«O”Ñ<ùs   ‚”)r5   r±   r  s    r   r	  r	  \  s   € ÜÑ<œI a›LÔ<Ó<Ð<r   c                 ó¤   — | xr |xr ||v }|xr |xr ||v }|rdnd}	|rdnd}
|s|sdnd|	› d|
› d�}|rd	nd}|d
|› d|d›d|d›d|› d| › d�z   S )NÚ	imaginaryÚrealr¶   r·   ró   zWhile considering the z part of complex z only, zcomputed with forward mode z	Jacobian zmismatch for output Údz with respect to input z,
numerical:z
analytical:rµ  r   )Ú
analyticalÚ	numericalr‚  rÿ   Úcomplex_indicesÚ	test_imagr°   Úout_is_complexÚinp_is_complexÚpartÚelementÚprefixÚmodes                r   Ú_get_notallclose_msgr"  `  s·   € ð Ð	ÒQ ÒQ°JÀ/Ð4Qð ð #ÒW ÒW¸9ÈÐ;W€NÙ#‰;¨€DÙ'‰h¨Y€Gñ ¡.ñ 	à% d VÐ+<¸W¸IÀWÐMð ñ
 -:Ñ(¸r€DàØ�d�VÐ/°
¸1¨~Ð=TÐU^Ð_`ÐTað bØ�K˜}¨Z¨L¸ð<ñ	<ðr   c                 ó$   — t        t        | Ž «      S r   )rQ   r´   )Úmatrix_of_tensorss    r   Ú
_transposer%  |  s   € ä”Ð&Ð'Ó(Ð(r   c                 ód   — d„ } || t         j                  «       || t         j                  «      fS )Nc                 ó   ‡ ‡— ˆ ˆfd„}|S )Nc                  óH   •— t         ‰| Ž «      }t        ˆfd„|D «       «      S )Nc              3   óR   •K  — | ]  }|j                  «       r ‰|«      n|–— Œ  y ­wr   r«   )r‡   r­   Úfn_to_applys     €r   rŠ   zU_real_and_imag_output.<locals>.apply_to_c_outs.<locals>.wrapped_fn.<locals>.<genexpr>‡  s"   øè ø€ ÒOÀQ¨1¯<©<¬>™ Qœ¸qÓ@ÑOùr  )r±   r5   )r¶   Úoutsrµ   r*  s     €€r   r  zB_real_and_imag_output.<locals>.apply_to_c_outs.<locals>.wrapped_fn…  s"   ø€ Ü™R ˜[Ó)ˆDÜÓOÈ$ÔOÓOÐOr   r   )rµ   r*  r  s   `` r   Úapply_to_c_outsz._real_and_imag_output.<locals>.apply_to_c_outs„  s   ù€ õ	Pð Ðr   )r   r  Úimag)rµ   r,  s     r   Ú_real_and_imag_outputr.  �  s*   € òñ ˜2œuŸz™zÓ*©O¸BÄÇ
Á
Ó,KÐKÐKr   c                 óB   ‡‡— ˆˆfd„} || d„ «      } || d„ «      }||fS )Nc                 ó   •‡ ‡— ˆˆ ˆˆfd„}|S )Nc                  óh   •— t        | «      }‰D ]  } ‰||   ‰|   «      ||<   Œ t         ‰|Ž «      S r   ©rQ   r±   )r¶   Ú
new_inputsÚshould_be_complexÚcomplex_inp_indicesrµ   r*  rž  s      €€€€r   r  zA_real_and_imag_input.<locals>.apply_to_c_inps.<locals>.wrapped_fn”  sN   ø€ Ü˜f›ˆJØ%8ò Ð!Ù0;ØÐ0Ñ1°=ÐARÑ3Só1�
Ð,Ò-ðô ™R ˜_Ó-Ð-r   r   )rµ   r*  r  r5  rž  s   `` €€r   Úapply_to_c_inpsz-_real_and_imag_input.<locals>.apply_to_c_inps“  s   ú€ ÷	.ð Ðr   c                 ó&   — | |j                   dz  z   S ©NrÛ   )r-  ©r½   rÖ   s     r   ú<lambda>z&_real_and_imag_input.<locals>.<lambda>ž  s   € °C¸$¿)¹)Àb¹.Ñ4H€ r   c                 ó&   — |j                   | dz  z   S r8  )r  r9  s     r   r:  z&_real_and_imag_input.<locals>.<lambda>Ÿ  s   € °D·I±IÀÀbÁÑ4H€ r   r   )rµ   r5  rž  r6  Úreal_fnÚimag_fns    ``   r   Ú_real_and_imag_inputr>  Ž  s/   ù€ õ
	ñ ˜bÑ"HÓI€GÙ˜bÑ"HÓI€GØ�GÐÐr   c                 ó  — t        |«      D ��cg c]  \  }}|j                  «       sŒ|‘Œ }}}t        d„ t        |«      D «       «      }|
rf|rTt	        |«      \  }} ||Ž }t        |«      } | ||||||||||d¬«        ||Ž }t        |«      } | ||||||||||¬«
       n | |||||||||«	       |	�r>t        |«      D ��cg c]"  \  }}t        |«      r|j                  «       r|‘Œ$ }}}|rát        |||«      \  }}|D �cg c]+  }t        |«      r|j                  «       r|j                  n|‘Œ- }} ||Ž }t        |«      } | ||||||||||dd¬«       |D �cg c]+  }t        |«      r|j                  «       r|j                  n|‘Œ- }} ||Ž }t        |«      } | ||||||||||d¬«       |rt        |||«       t        |||«       y y  | |||||||||d¬«
       |rt        |||«       y y y c c}}w c c}}w c c}w c c}w )Nc              3   ó<   K  — | ]  }|j                  «       –— Œ y ­wr   r«   r¬   s     r   rŠ   z'_gradcheck_real_imag.<locals>.<genexpr>³  s   è ø€ Ò M°A §¡§Ñ Mùr®   T)r  r  )r  )r  r  rì  )r  rì  rë  )r™   r+   r²   r±   r.  r	  r   r>  r-  r  rÿ  )Úgradcheck_fnr«  r9  rž  r·   r¹   ÚrtolrF  r  Úcheck_forward_adÚcheck_backward_adre  Úcheck_undefined_gradr�   r­   Úcomplex_out_indicesÚhas_any_complex_outputr<  r=  Úimag_func_outÚimag_outputsÚreal_func_outÚreal_outputsr½   r5  Úimag_inputsÚdiff_imag_func_outÚreal_inputsÚdiff_real_func_outs                                r   Ú_gradcheck_real_imagrP  £  s½  € ô *3°7Ó);×N¡  A¸q¿|¹|½~š1ÐNÐÑNÜ Ñ M¼À8Ó9LÔ MÓMÐÙÙ!Ü4°TÓ:ÑˆG�Wá# ]Ð3ˆMÜ2°=ÓAˆLÙØØØØØØØØ!ØØ 3Øõñ $ ]Ð3ˆMÜ2°=ÓAˆLÙØØØØØØØØ!ØØ 3öñ ØØØØØØØØ!Øô
ò ô $ MÓ2÷
á��3Ü˜cÔ" s§~¡~Ô'7ò ð
Ðñ 
ñ
 Ü3ØÐ)¨=ó ÑˆG�Wð )öàô +¨3Ô/°C·N±NÔ4D�—’È#ÑMðˆKð ñ $ [Ð1ˆMÜ!8¸Ó!GÐÙØØØØ"ØØØØ!ØØ 3ØØ#õð" )öàô +¨3Ô/°C·N±NÔ4D�—’È#ÑMðˆKð ñ $ [Ð1ˆMÜ!8¸Ó!GÐÙØØØØ"ØØØØ!ØØ 3Ø#õñ $Ü,¨W°mÀ[ÔQÜ,¨W°mÀ[ÕQð $ñ ØØØØØØØØ!ØØ#õñ $Ü,¨T°7¸MÕJð $ðG ùóc Oùód
ùòùò*s   �G4¨G4Ã'G:Ä0H Å0H)rì  r  r  rä  c	                óü  — t        |«      }|st        | ||||	¬«      S |r|n
t        |«      }t        t	        | ||||	¬«      «      }t        ||«      D ��cg c]  \  }}|j                  sŒ|‘Œ }}}|	r€t        | |||¬«      }t        |«      D ]b  \  }}t        |«      D ]O  \  }}||   |   }t        ||j                  |j                  «      ||«      rŒ6t        t        |||||
|d¬«      «      ‚ Œd yt        |«      D ]v  \  }}t        ||||«      }t        t        |||   «      «      D ]H  \  }\  }}t        ||j                  |j                  «      ||«      rŒ1t        t        |||||
|«      «      ‚ Œx yc c}}w )Nr¨  )r  TrÈ  )r±   r®  rT   r%  r¿   r´   r1   r'  r™   Ú_allclose_with_type_promotionr\   r=   r   r"  ro  )r«  r9  rž  r·   r¹   rB  rF  r  re  rì  r  r  rä  Útupled_inputs_numericalr  r­   ÚnjÚanalytical_forwardr�   Ú	n_per_outrS  Únr»   r  s                           r   Ú_slow_gradcheckrX  *  s¹  € ô  ˜Ó"€HÙÜ/Ø�- ¨sÀ.ô
ð 	
ñ 06™m¼8ÀMÓ;RÐäÜØØ#ØØØ(ô	
ó€Iô "% X¨yÓ!9×M™˜˜2¸Q¿_»_’ÐM€IÑMÙÜ@Ø�- Ð=Nô
Ðô & iÓ0ò 	‰LˆAˆyÜ! )Ó,ò ‘��1Ø& qÑ)¨!Ñ,�Ü4°Q¸¿¹¸Q¿X¹X»ÈÈdÕSÜ(Ü,Ø˜q ! Q¨¸ÐRVôóð ñð	ð* ô ˜gÓ&ò 		‰DˆAˆqÜ>Ø˜q *Ð.?óˆJô '¤s¨:°yÀ±|Ó'DÓEò ‘	�‘6�A�qÜ4°Q¸¿¹¸Q¿X¹X»ÈÈdÕSÜ(Ü,¨Q°°1°a¸È)ÓTóð ñð		ð ùó7 Ns   ÁE8Á*E8c                 óx   — | j                  «       dk(  r|j                  «       dk(  sJ ‚| |z  j                  «       S )NrM   )r€   re   )r  rÕ   s     r   r8  r8  k  s1   € Ø�5‰5‹7�aŠ<˜AŸE™E›G qšLÐ(Ð(Ø�‰E�;‰;‹=Ðr   c                 óÎ   — t        j                  | j                  |j                  «      }| j                  |¬«      } |j                  |¬«      }t        j                  | |||«      S ré   )r   Úpromote_typesr<   r\   rÊ  )r»   rË   rB  rF  Úpromoted_types        r   rR  rR  p  sQ   € Ü×'Ñ'¨¯©°·±Ó9€MØ	�‰�=ˆÓ!€AØ	�‰�=ˆÓ!€AÜ�>‰>˜!˜Q  dÓ+Ð+r   c                 ó’   — | t         j                  k(  rt         j                  S | t         j                  k(  rt         j                  S | S r   )r   r˜  r—  Ú	complex64Úfloat32rL   s    r   Ú_to_real_dtyper`  w  s5   € Ø”× Ñ Ò Ü�}‰}ÐØ	”%—/‘/Ò	!Ü�}‰}Ðàˆr   c                 óš  — | j                   t        j                  k(  rà| j                  «       }|rt	        | j
                  «      n| j
                  }t        j                  |j                  «       |¬«      j                  || j                  ¬«      j                  |j                  «      }||j                  «       z  }t        j                  | j                  «       || j                  «       | j                  ¬«      }|S t!        | «      �rK| j                   t        j"                  t        j$                  hv r!| j'                  «       | j)                  «       }}n | j+                  «       | j-                  «       }}| j/                  «       }|rt	        | j
                  «      n| j
                  }t        j                  |j                  «       |¬«      j                  || j                  ¬«      j                  |j                  «      }||j                  «       z  }t        j0                  |||| j                  «       | j                   | j                  ¬«      }|S |rt	        | j
                  «      n| j
                  }t        j                  | j                  «       |¬«      j                  || j                  ¬«      }||j                  «       z  }|S )N)Ú	generatorrK   r„   )r   r=   )r   r   r'   r‹   r`  r<   Úrandr3   r\   r=   rˆ  rX   Únormrg   rV   r   r%   r    r"   rj   r�   rk   rŽ   rf   Úsparse_compressed_tensor)	rA   rb  Údowncast_complexrŸ   r<   rf   Úvecru   Úplain_indicess	            r   Ú_vec_from_tensorri  €  s	  € ð 	‡x�x”5×#Ñ#Ò#ð —9‘9“;ˆÙ+;”˜qŸw™wÔ'ÀÇÁˆä�J‰J�x—~‘~Ó'°9Ô=ß‰R�e A§H¡HˆRÓ-ß‰T�(—.‘.Ó!ð 	ð
 	�&—+‘+“-ÑˆÜ×%Ñ% a§j¡j£l°F¸A¿F¹F»HÈQÏXÉXÔVˆð8 €Jô7 
& aÕ	(Ø�8‰8œ×(Ñ(¬%×*:Ñ*:Ð;Ñ;Ø01·±Ó0@À!Ç-Á-Ã/ Ñà01·±Ó0@À!Ç-Á-Ã/ ÐØ—8‘8“:ˆÙ+;”˜qŸw™wÔ'ÀÇÁˆä�J‰J�x—~‘~Ó'°9Ô=ß‰R�e A§H¡HˆRÓ-ß‰T�(—.‘.Ó!ð 	ð
 	�&—+‘+“-ÑˆÜ×,Ñ,ØØØØ�F‰F‹HØ—8‘8Ø—8‘8ô
ˆð €Jñ ,<”˜qŸw™wÔ'ÀÇÁˆÜ�j‰j˜Ÿ™›¨iÔ8×;Ñ;Ø §¡ð <ó 
ˆð 	ˆs�x‰x‹zÑˆØ€Jr   c                 óÚ   — t        | «      D ��cg c]   \  }}t        |«      r|j                  r||f‘Œ" }}}|D �cg c]  }|d   ‘Œ	 c}|D �cg c]  }|d   ‘Œ	 c}fS c c}}w c c}w c c}w r1  )r™   r   r1   )rž  r�   r7   Úinp_idx_tupÚtups        r   rí  rí  ®  st   € ô ˜mÓ,÷áˆAˆqÜ˜!Ô §¢ð 
ˆAŠð€Kñ ð
 *Ö*�sˆC�‹FÒ*¸{Ö,K¸¨S°«VÒ,KÐKÐKùóùò
 +ùÒ,Ks   �%A»A#ÁA(c                 ó¬   — t        |t        «      r|d   n|}|j                  «       }|€dn|j                  «       }| t        |«      z  t        |«      z  S )Nr   rÂ   )rD   r5   re   r�  )rF  r  rÕ   Úsum_uÚsum_vs        r   Ú_adjusted_atolrp  ·  sL   € ô ˜1œeÔ$ˆˆ!Š¨!€AØ�E‰E‹G€EØ�9‰C !§%¡%£'€EØ”%˜“,Ñ¤ u£Ñ-Ð-r   a§  
Fast gradcheck failed but element-wise differences are small. This means that the
test might've passed in slow_mode!

If you are adding a new operator, please file an issue and then use one of the
workarounds. The workaround depends on how your test invokes gradcheck/gradgradcheck:

If the test
- manually invokes gradcheck/gradgradcheck, then call gradcheck/gradgradcheck
  with `fast_mode=False` as a keyword argument.
- is OpInfo-based (e.g., in test_ops_gradients.py), then modify the OpInfo for the test
  to have `gradcheck_fast_mode=False`
- is a Module test (e.g., in common_nn.py), then modify the corresponding
  module_test entry to have `gradcheck_fast_mode=False`
c	                 óB  ‡ ‡‡‡— t        ‰ ‰|||¬«      ‰   ‰   }	|r$ˆ ˆˆˆfd„}
t        |
‰‰   f|‰   f«      d   d   }nt        ‰|‰‰«      }|	|z
  j                  «       j	                  «       }t        j                  ||	||«      }d|	› d|› d|› d�}|r	|t        z  }|S )Nr¨  c                 óF   •— t        ‰«      }| |‰<   t         ‰|Ž «      ‰   S r   r2  )r½   r3  r«  rÿ   r‚  rž  s     €€€€r   Únew_fnz,_run_slow_mode_and_get_error.<locals>.new_fnà  s,   ø€ Ü˜mÓ,ˆJØ$'ˆJ�yÑ!Ü™T :Ð.Ó/°
Ñ;Ð;r   r   a  
The above quantities relating the numerical and analytical jacobians are computed 
in fast mode. See: https://github.com/pytorch/pytorch/issues/53876 for more background 
about fast mode. Below, we recompute numerical and analytical jacobians in slow mode:

Numerical:
 z
Analytical:
z1

The max per-element difference (slow mode) is: z.
)r¿   r'  rƒ  rC  rm   r   rÊ  ÚFAST_FAIL_SLOW_OK_MSG)r«  rž  r·   rÿ   r‚  rB  rF  r¹   r°   Úslow_numericalrs  Úslow_analyticalÚslow_max_diffÚslow_allcloseÚmsgs   `` ``          r   Ú_run_slow_mode_and_get_errorrz  ×  sò   û€ ô -Øˆm˜W¨#¸]ôàñàñ€Nñ ÷	<ô
 >Ø�] 9Ñ-Ð/°'¸*Ñ2EÐ1Gó
à
ñàñ‰ô 3Ø˜7 I¨zó
ˆð
 $ oÑ5×:Ñ:Ó<×@Ñ@ÓB€Mä—N‘N ?°NÀDÈ$ÓO€Mð	ð 'Ð'ð (Ø'Ð(ð ):Ø:G¸Èð	Mð ñ àÔ$Ñ$ˆØ€Jr   c                 óZ   — t        | «      r| j                  «       j                  d«      S | S )NrP   )r(   rš   r‘   )rc   s    r   Ú_to_flat_dense_if_sparser|  ÿ  s'   € Ü˜VÔ$Ø�‰Ó ×(Ñ(¨Ó,Ð,àˆr   c                ó–  — t        j                  «       }d„ }g }g }| D ]…  } |||d«      }t        |«      }	|j                  «       r< |||d«      }
|j	                  ||
f«       t        |
«      }|j	                  |	|f«       Œd|j	                  |«       |j	                  |	«       Œ‡ |rd n|D �cg c]  } |||«      ‘Œ c}}|||fS c c}w )Nc                  óh   — t        j                  d«      5  t        | Ž cd d d «       S # 1 sw Y   y xY w)NÚcpu)r   r=   ri  )Úargss    r   Ú_vec_from_tensor_cpuz+_make_vectors.<locals>._vec_from_tensor_cpu
  s-   € ô �\‰\˜%Ó ñ 	+Ü# TÐ*÷	+÷ 	+ò 	+ús   –(¨1T)r   Ú	Generatorr|  r+   rÜ   )rò  r·   rì  Úg_cpur�  r  Úall_u_denser½   ÚurÚur_denseÚuiÚui_denser8   r:  s                 r   rî  rî    sß   € ä�O‰OÓ€Eò+ð €EØ€KØò 
)ˆÙ! # u¨dÓ3ˆÜ+¨BÓ/ˆØ�>‰>ÔÙ% c¨5°$Ó7ˆBØ�L‰L˜"˜b˜Ô"Ü/°Ó3ˆHØ×Ñ ¨(Ð3Õ4à�L‰L˜ÔØ×Ñ˜xÕ(ð
)ñ ñ 	à:AÖB°3Ñ" 3¨Õ.ÒBð 
ð
 �%˜Ð$Ð$ùò Cs   Â.CrÈ  c                óŠ  — t        |«      D ]µ  \  }}t        |«      D ]¢  \  }}|r	| |   |   }n| |   |   }|j                  |j                  ¬«      }t        |	||   |r||   nd «      }t	        ||j                  |j                  «      ||«      rŒtt        |||||||	|
|«	      }t        t        |||||||«      |z   «      ‚ Œ· y )Nr„   )r™   r\   r=   rp  rR  rz  r   r"  )Úall_analyticalÚall_numericalr  rž  r·   r«  r:  r  rB  rF  r¹   r  r°   r�   Úall_numerical_for_input_irS  rW  r»   Úupdated_atolÚjacobians_strs                       r   Ú!_check_analytical_numerical_equalr�  %  sî   € ô  )2°-Ó(@ò Ñ$ˆÐ$ÜÐ7Ó8ò 	‰DˆAˆqáØ" 1Ñ% aÑ(‘à" 1Ñ% aÑ(�Ø—‘˜AŸH™H�Ó%ˆAÜ)¨$°°a±Áe¸%Àº(ÐQUÓVˆLÜ0°°A·D±D¸¿¹³NÀDÈ,ÕWÜ <Ø˜-¨°!°Q¸¸dÀCÈó!�ô %Ü(Ø˜1˜a  O°YÀóð $ñ$óð ñ	ñr   c	                óR  — t        |«      \  }}t        |||	¬«      \  }}}|r|||fnt        |||f«      \  }}}t        | |||||||	¬«      }|	r|�J ‚t	        | |t        |«      ||¬«      }n#|st        | ||||||«       t        ||||||«      }t        |||
||| |||||||	¬«       y)Nrë  rÈ  )r  r  T)	rí  rî  rT   rA  r'  r±   r³  r|  r�  )r«  r9  r¶   r·   r¹   rB  rF  r  re  rì  r  r  rä  Úinp_tensors_idxrò  r:  r  r„  Úinputs_numericalÚall_u_numericalÚall_v_numericalÚnumerical_vJuÚanalytical_vJus                          r   Ú_fast_gradcheckr—  J  s  € ô" $4°FÓ#;Ñ €O�[ô !.Ø�W¨^ô!Ñ€Eˆ5�+ñ
 #)ˆ�˜Ñ¬h¸ÀÀuÐ7MÓ.Nñ 7Ð�o ô 'ØØØØØØØØ$ô	€Mñ Øˆ}Ðˆ}Ü<ØØÜ�hÓØØ/ô
‰ñ Ü1Ø�h ¨¸ÀÀZôô ;Ø�G˜ZÐ):¸EÀ;ó
ˆô &ØØØØØØØØØØØØØ$õð  r   g�íµ ÷Æ°>gñhãˆµøä>T)r¹   rF  rB  Úraise_exceptionre  rE  r  Úcheck_batched_gradÚcheck_batched_forward_gradrC  rD  rð   rä  r«  r¶   r¹   rF  rB  r˜  re  rE  r  r™  rš  rC  rD  rð   rä  c                óæ   — |s	|sJ d«       ‚|	r	|sJ d«       ‚|
r	|sJ d«       ‚t        «       j                  «       }|j                  d«       |s	 t        di |¤ŽS t        di |¤ŽS # t        $ r Y yw xY w)a‹  Check gradients computed via small finite differences against analytical
    gradients wrt tensors in :attr:`inputs` that are of floating point or complex type
    and with ``requires_grad=True``.

    The check between numerical and analytical gradients uses :func:`~torch.allclose`.

    For most of the complex functions we consider for optimization purposes, no notion of
    Jacobian exists. Instead, gradcheck verifies if the numerical and analytical values of
    the Wirtinger and Conjugate Wirtinger derivatives are consistent. Because the gradient
    computation is done under the assumption that the overall function has a real-valued
    output, we treat functions with complex output in a special way. For these functions,
    gradcheck is applied to two real-valued functions corresponding to taking the real
    components of the complex outputs for the first, and taking the imaginary components
    of the complex outputs for the second. For more details, check out
    :ref:`complex_autograd-doc`.

    .. note::
        The default values are designed for :attr:`input` of double precision.
        This check will likely fail if :attr:`input` is of less precision, e.g.,
        ``FloatTensor``.

    .. note::
        Gradcheck may fail when evaluated on non-differentiable points
        because the numerically computed gradients via finite differencing may differ
        those computed analytically (not necessarily because either is incorrect).
        For more context, see :ref:`non-differentiable-func-grad`.

    .. warning::
       If any checked tensor in :attr:`input` has overlapping memory, i.e.,
       different indices pointing to the same memory address (e.g., from
       :func:`torch.Tensor.expand`), this check will likely fail because the numerical
       gradients computed by point perturbation at such indices will change
       values at all other indices that share the same memory address.

    Args:
        func (function): a Python function that takes Tensor inputs and returns
            a Tensor or a tuple of Tensors
        inputs (tuple of Tensor or Tensor): inputs to the function
        eps (float, optional): perturbation for finite differences
        atol (float, optional): absolute tolerance
        rtol (float, optional): relative tolerance
        raise_exception (bool, optional): indicating whether to raise an exception if
            the check fails. The exception gives more information about the
            exact nature of the failure. This is helpful when debugging gradchecks.
        nondet_tol (float, optional): tolerance for non-determinism. When running
            identical inputs through the differentiation, the results must either match
            exactly (default, 0.0) or be within this tolerance.
        check_undefined_grad (bool, optional): if ``True``, check if undefined output grads
            are supported and treated as zeros, for ``Tensor`` outputs.
        check_batched_grad (bool, optional): if ``True``, check if we can compute
            batched gradients using prototype vmap support. Defaults to False.
        check_batched_forward_grad (bool, optional): if ``True``, checks if we can compute
            batched forward gradients using forward ad and prototype vmap support. Defaults to ``False``.
        check_forward_ad (bool, optional): if ``True``, check that the gradients computed with forward
            mode AD match the numerical ones. Defaults to ``False``.
        check_backward_ad (bool, optional): if ``False``, do not perform any checks that rely on
            backward mode AD to be implemented. Defaults to ``True``.
        fast_mode (bool, optional): Fast mode for gradcheck and gradgradcheck is currently only
            implemented for R to R functions. If none of the inputs and outputs are complex
            a faster implementation of gradcheck that no longer computes the entire jacobian
            is run; otherwise, we fall back to the slow implementation.
        masked (bool, optional): if ``True``, the gradients of unspecified elements of
            sparse tensors are ignored. Defaults to ``False``.
    Returns:
        ``True`` if all differences satisfy allclose condition

    zIExpected at least one of check_forward_ad or check_backward_ad to be TruezESetting check_batched_grad=True requires check_backward_ad to be TruezLSetting check_batched_forward_grad=True requires check_forward_ad to be Truer˜  Fr   )ÚlocalsÚcopyÚpopÚ_gradcheck_helperr   )r«  r¶   r¹   rF  rB  r˜  re  rE  r  r™  rš  rC  rD  rð   rä  r€  s                   r   r   r   ¢  s«   € ñl 	Ñ-ðSàRóSØ-ñ 	Ñ#4ðOàNóOð ñ 	#Ñ+;ðVàUóVð ô ‹8�=‰=‹?€DØ‡H�HÐÔÙð	Ü$Ñ, tÑ,Ð,ô !Ñ( 4Ñ(Ð(øô ò 	Ùð	ús   Á
A$ Á$	A0Á/A0c                 ó€  — t        |«      }t        |«        | |Ž }t        |«      }t        |«       t	        j
                  |rt        nt        |¬«      }t        || ||||||||
|||¬«       |	rt        | |«       |syt        |«      D ]  \  }}|sŒ	t        |||«       Œ t        |||«       |r|rt        | ||«       y)N)rä  )rC  rD  re  rE  T)r±   r¡  r	  r¦  rü   rý   r—  rX  rP  rÐ  r™   rÛ  ré  r  )r«  r¶   r¹   rF  rB  re  rE  r  r™  rš  rC  rD  rð   rä  rž  r9  r·   rA  r�   r­   s                       r   rŸ  rŸ    sÝ   € ô  ˜fÓ%€MÜ�-Ô á�]Ð#€HÜ% hÓ/€GÜ�7Ôä×$Ñ$Ù$�¬/À&ô€Lô ØØØØØØØØØØ)Ø+ØØ1õñ  "Ü% d¨MÔ:ñ Øä˜'Ó"ò 4‰ˆˆ1ÚÜ˜}¨a°Õ3ð4ô & g¨}¸fÔEáÑ 1Ü% d¨G°]ÔCØr   )r¹   rF  rB  Úgen_non_contig_grad_outputsr˜  re  rE  r  r™  Úcheck_fwd_over_revÚcheck_rev_over_revrð   rä  rÙ  r¡  r¢  r£  c                óþ  ‡ ‡‡‡‡— |s	|sJ d«       ‚|	r	|sJ d«       ‚|r	|sJ d«       ‚t        |«      }|€#t         ‰ |Ž «      }t        ˆfd„|D «       «      }nt        |«      }t        |«      Št	        |«      D ��ch c]   \  }}t        |«      sŒ|j                  sŒ|’Œ" c}}Št	        |«      D ��ch c]  \  }}|j                  sŒ|’Œ c}}Šˆˆˆ ˆfd„}t        |||z   ||||||	|
|||||¬«      S c c}}w c c}}w )a+  Check gradients of gradients computed via small finite differences
    against analytical gradients wrt tensors in :attr:`inputs` and
    :attr:`grad_outputs` that are of floating point or complex type and with
    ``requires_grad=True``.

    This function checks that backpropagating through the gradients computed
    to the given :attr:`grad_outputs` are correct.

    The check between numerical and analytical gradients uses :func:`~torch.allclose`.

    .. note::
        The default values are designed for :attr:`input` and
        :attr:`grad_outputs` of double precision. This check will likely fail if
        they are of less precision, e.g., ``FloatTensor``.

    .. warning::
       If any checked tensor in :attr:`input` and :attr:`grad_outputs` has
       overlapping memory, i.e., different indices pointing to the same memory
       address (e.g., from :func:`torch.Tensor.expand`), this check will likely fail
       because the numerical gradients computed by point perturbation at such
       indices will change values at all other indices that share the same
       memory address.

    Args:
        func (function): a Python function that takes Tensor inputs and returns
            a Tensor or a tuple of Tensors
        inputs (tuple of Tensor or Tensor): inputs to the function
        grad_outputs (tuple of Tensor or Tensor, optional): The gradients with
            respect to the function's outputs.
        eps (float, optional): perturbation for finite differences
        atol (float, optional): absolute tolerance
        rtol (float, optional): relative tolerance
        gen_non_contig_grad_outputs (bool, optional): if :attr:`grad_outputs` is
            ``None`` and :attr:`gen_non_contig_grad_outputs` is ``True``, the
            randomly generated gradient outputs are made to be noncontiguous
        raise_exception (bool, optional): indicating whether to raise an exception if
            the check fails. The exception gives more information about the
            exact nature of the failure. This is helpful when debugging gradchecks.
        nondet_tol (float, optional): tolerance for non-determinism. When running
            identical inputs through the differentiation, the results must either match
            exactly (default, 0.0) or be within this tolerance. Note that a small amount
            of nondeterminism in the gradient will lead to larger inaccuracies in
            the second derivative.
        check_undefined_grad (bool, optional): if True, check if undefined output grads
            are supported and treated as zeros
        check_batched_grad (bool, optional): if True, check if we can compute
            batched gradients using prototype vmap support. Defaults to False.
        fast_mode (bool, optional): if True, run a faster implementation of gradgradcheck that
            no longer computes the entire jacobian.
        masked (bool, optional): if True, the gradients of unspecified elements of
            sparse tensors are ignored (default, False).
    Returns:
        True if all differences satisfy allclose condition
    zLExpected at least one of check_fwd_over_rev or check_rev_over_rev to be TruezHSetting check_undefined_grad=True requires check_rev_over_rev to be TruezFSetting check_batched_grad=True requires check_rev_over_rev to be Truec           
   3   ó  •K  — | ]x  }t         j                  j                  |j                  |j	                  «       s|j                  «       r|j                  nt         j                  |j                  d dd‰¬«      –— Œz y­w)rP   rM   T)r<   r=   ÚlowÚhighr1   ÚnoncontiguousN)	r   ÚtestingÚmake_tensorrX   r*   r+   r<   Údoubler=   )r‡   rA   r¡  s     €r   rŠ   z gradgradcheck.<locals>.<genexpr>¢  sm   øè ø€ ò $
ð ô �M‰M×%Ñ%Ø—‘à×&Ñ&Ô(¨A¯L©L¬Nð —g’gä—\‘\Ø—x‘xØØØ"Ø9ð &÷ 
ñ$
ùs   ƒA>Bc                  óN  •— t        ˆfd„t        | d ‰	  «      D «       «      }t         ‰|Ž «      }t        ˆfd„t        | ‰	 d  «      D «       «      }t        ˆfd„t        |«      D «       «      }t        j                  j                  |||dd¬«      }t        d„ |D «       «      }|S )Nc              3   óP   •K  — | ]  \  }}|‰v r|j                  «       n|–— Œ y ­wr   ©ri   ©r‡   r�   rA   Údiff_input_args_indicess      €r   rŠ   z2gradgradcheck.<locals>.new_func.<locals>.<genexpr>À  s3   øè ø€ ò 
á��1ð #$Ð'>Ñ">ˆA×ÑÔÀAÓEñ
ùó   ƒ#&c              3   óP   •K  — | ]  \  }}|‰v r|j                  «       n|–— Œ y ­wr   r®  )r‡   r�   rA   Údiff_grad_output_indicess      €r   rŠ   z2gradgradcheck.<locals>.new_func.<locals>.<genexpr>Å  s3   øè ø€ ò 
á��1ð #$Ð'?Ñ"?ˆA×ÑÔÀQÓFñ
ùr±  c              3   ó2   •K  — | ]  \  }}|‰v sŒ|–— Œ y ­wr   r   r¯  s      €r   rŠ   z2gradgradcheck.<locals>.new_func.<locals>.<genexpr>É  s"   øè ø€ ò  
Ù�!�Q°1Ð8OÒ3OŒAñ 
ùs   ƒ�T)Úcreate_graphr[  c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wr   r   )r‡   Úgs     r   rŠ   z2gradgradcheck.<locals>.new_func.<locals>.<genexpr>Ï  s   è ø€ ÒD !°a±mœAÑDùs   ‚Š)r5   r™   r	  r   r  r]  )
r€  Ú
input_argsr·   rÙ  Údiff_input_argsrŽ  r³  r°  r«  Únum_outputss
         €€€€r   Únew_funczgradgradcheck.<locals>.new_func¾  s»   ø€ äó 
ä! $ }¨¨Ð"5Ó6ô
ó 
ˆ
ô *©$°
Ð*;Ó<ˆÜó 
ä! $¨ | }Ð"5Ó6ô
ó 
ˆô  ó  
Ü# JÓ/ô 
ó 
ˆô —n‘n×)Ñ)Ø�_ lÀÐTXð *ó 
ˆô ÑD {ÔDÓDˆØÐr   )r¹   rF  rB  r˜  re  rE  r  r™  rð   rC  rD  rä  )r±   r	  r5   r|   r™   r   r1   r   )r«  r¶   rÙ  r¹   rF  rB  r¡  r˜  re  rE  r  r™  r¢  r£  rð   rä  rž  r·   Útupled_grad_outputsr�   rA   r»  r³  r°  rº  s   `     `               @@@r   r   r   G  sO  ü€ ñT 	Ñ0ðVàUóVØ0ñ 	Ñ%7ðRàQóRð ñ 	Ñ#5ðPàOóPð ô ˜fÓ%€MàÐô *©$°Ð*>Ó?ˆÜ#ó $
ð ô$
ó 
Ñô (¨Ó5ÐäÐ)Ó*€Kô
   Ó.÷Ùˆa�´.ÀÕ2CÈÏËŠóÐô  Ð 3Ó4÷ Ùˆa�¸¿»Šó Ð÷ô( ØØÐ+Ñ+ØØØØ'ØØ1Ø+Ø-ØØ+Ø,Øôð ùó7ùó s   Á;C3ÂC3ÂC3Â2C9ÃC9)NN)F)NNr¨   F)Nr¨   rÂ   r   )r.   N)NF)FN)r  rÂ   )FF)grE   rü   r™  Úcollections.abcr   Ú	itertoolsr   Útypingr   r   r   Útyping_extensionsr   r   Útorch.testingÚtorch._vmap_internalsr	   r
   Útorch.overridesr   Útorch.typesr   Ú__all__r�  r   r‰  r%   r(   r,   r5   r9   r@   ÚboolrH   rT   r§   rQ   r¿   ÚFutureWarningr   rÙ   ræ   ÚdictÚintrì   rñ   rù   r   r'  r)  rû   rþ   r2  r4  r6  rA  rI  rU  rd  ro  r|  r   rƒ  rc  rb  r¡  r¦  r®  r³  r¸  r·  r¶  rº  rÐ  rÛ  ré  rÿ  r  r±   r	  rG   r"  r%  r.  r>  rP  rX  r8  rR  r`  ri  rí  rp  rt  rz  r|  rî  r�  r—  r�  r   rŸ  r   r   r   r   ú<module>rÊ     sm  ðã Û Û Ý $Ý ß ,Ñ ,Ý (ã Û ß -Ý *Ý (ò€ôH�\ô Hð e§l¡ló ðO˜uŸ|™|ó OòQðØðà
ˆ5�<‰<˜ÐÑóð" <@ñØðà
ˆ5�<‰<˜ÐÑóð" QVñ	@ØˆU�\‰\˜8 E§L¡LÑ1Ð1Ñ2ð	@ØIMð	@àˆe�l‰lÑó	@ò;ò|d)ðP DIñ2à	ˆ%�—‘˜cÐ!Ñ
"Ñ#ó2ñj ðCð
 ôò!Xóð!XòH>ð@ 49ñà	ˆ%�,‰,Ñóð6Ø˜˜d 5§<¡<Ñ0Ð0Ñ1ðà
ˆ5�<‰<˜ÐÑóð SXñØ�<‰<ðØ08¸¿¹Ñ0Fðà
‡\�\óô0ð" DIñPà
ˆ5�<‰<˜ÐÑóPð6 /4¸4òià
ˆ5�—‘˜sÐ"Ñ# SÐ(Ñ)óiòXó òòòð 27ñà	ˆ%�,‰,Ñóòò:ðAà
ˆ5�—‘˜sÐ"Ñ# T¨4Ð/Ñ0óAð8Ð ð$ GKñ)à
ˆ5�<‰<˜ÐÑó)òXñ0 ðCð
 ôò!5óð!5òH ðà	ˆ$ˆx˜Ÿ™Ñ%Ñ
&Ñ'óð6à	ˆ$ˆx˜Ÿ™Ñ%Ñ
&Ñ'óð& Dó &óR
ðà	óò"ð÷, 
�EƒGð- ð0"Ð ð$ 49óð 8°4ó 8ðv4°Tó 4ðn3À$ó 3òl@ðF?T¸Dó ?TòDò=ð Øñð 	óò8)ò

Lòò*DKðd ØØØô>òBò
,òó+ò\Lò.ð÷ 
�EƒGð ò"%òPò%ðZ ô"ð` ØØØôOðx ØØØ ØØ!%Ø#Ø$Ø',Ø"Ø"ØØ!ò!f)Ø
�3˜Ð.Ñ/Ð/Ñ
0ðf)àðf)ð 
ð	f)ð
 ðf)ð ðf)ð ðf)ð ðf)ð ðf)ð ðf)ð ðf)ð !%ðf)ð ðf)ð ðf)ð ðf)ð  �T‰Nð!f)ð" 
ó#f)òR9ð~ 04ðZð
 ØØØ(-Ø ØØ!%Ø#Ø$Ø$Ø#ØØò#ZØ
�3Ð(Ð(Ñ
)ðZàðZð Ð+Ñ,ðZð
 
ðZð ðZð ðZð "&ðZð ðZð ðZð ðZð ðZð ðZð ðZð ðZð  ð!Zð" ð#Zð$ 
ô%Zr   