L i;ddlZddlZddlZddlZddlmZmZddlmZm Z m Z m Z m Z ddl mZddlZ ddlZdZGddeZej,d ej.d ej0d ej2d ej4d ej6d ej8d iZej=ejAejBejDejFejHejJfeej0dd de ejLejNfde eejNe(e)e)ffde(e)e)ffdZ*ddde ejLejNfde e)de e)de(edfde(e)e)ff dZ+ddddde,de e e,e e,ge,ffde e,de e(e-fdZ.dddddde,de e e,e e,ge,ffde e,d e)d!e e e-e(e-dffde)d"e)d#e e e-e(e-dffde)de,fd$Z/dd%d&e e0e-e)e1fd'e e0e-e)e1fde)de)de e e,e e,ge,ffde,f d(Z2dd%d&ejLd'ejLd)ejLde)de)de e e,e e,ge,fff d*Z3Gd+d,eZ4Gd-d.ejjZ6Gd/d0e6Z7Gd1d2e6Z8Gd3d4e6Z9Gd5d6e6Z:Gd7d8e6Z;ejj fejjxfdd9d&ed'ed:ee=e6d;e(e=dfde6fd>Z?e7fejj fejjxfd?d&ed'ed:ee=e6d;e(e=dfdefd@Z@ddddddddddA d&ed'edBe0de e)de e)dCe0dDe0dEe0dFe0dGe0dHe e e,e e,ge,fffdIZAedJeBK dMd&ed'ede e)de e)dCe0dHe,ddfdLZCy#e$rdZdZYwxYw)NN) CollectionSequence)AnyCallableNoReturnOptionalUnion) deprecatedTFc zeZdZdZdddeededeedfdd ffd Z d de e ee egeffdefd Z xZS) ErrorMetazBInternal testing exception that makes that carries error metadata.idtypemsgr.returnNcNt|d||_||_||_y)NzIf you are a user and see this message during normal operation please file an issue at https://github.com/pytorch/pytorch/issues. If you are a developer and working on the comparison functions, please `raise ErrorMeta.to_error()` for user facing errors.)super__init__rrr)selfrrr __class__s _/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/torch/testing/_comparison.pyrzErrorMeta.__init__s.  &  ct|tsU|j}|jr(|ddj d|jDz }t |r||n|}|j |S)Nz The failure occurred for item c34K|]}t|gywNstr.0items r z%ErrorMeta.to_error..,sMn^bcSWRXkMn) isinstancerrrjoincallabler)rr generated_msgs rto_errorzErrorMeta.to_error&sg#s# HHMww#EbggMnfjfmfmMnFnEo!pp (0 #m$=Cyy~rr)__name__ __module__ __qualname____doc__r Exceptionrtuplerrrr rr) __classcell__rs@rr r stLIK O *- 6;CHo   AE E#xs ';";<=   rr )MbP?h㈵>)gMb?r3)gkNuϵ>r3)Hz>r4dtype_precisionsinputsr6rc g}|D]{}t|tjr|j|j9t|tjr|j|et dt |d|xst}t|Dcgc]}|j|dc}\}}t|t|fScc}w)aReturns the default absolute and relative testing tolerances for a set of inputs based on the dtype. See :func:`assert_close` for a table of the default tolerance for each dtype. Returns: (Tuple[float, float]): Loosest tolerances of all input dtypes. z2Expected a torch.Tensor or a torch.dtype, but got z instead.)r9) r%torchTensorappenddtype TypeErrorr_DTYPE_PRECISIONSzipgetmax)r6r7dtypesinputr=rtolsatolss rdefault_tolerancesrGIsF eU\\ * MM%++ & u{{ + MM% DT%[MQZ[  (<+<fUU)--eZ@UVLE5 u:s5z !!VsC r rrtolatolr.ch|du|duz rttd|dndd|||||fSt|S)aGets absolute and relative to be used for numeric comparisons. If both ``rtol`` and ``atol`` are specified, this is a no-op. If both are not specified, the return value of :func:`default_tolerances` is used. Raises: ErrorMeta: With :class:`ValueError`, if only ``rtol`` or ``atol`` is specified. Returns: (Tuple[float, float]): Valid absolute and relative tolerances. NzGBoth 'rtol' and 'atol' must be either specified or omitted, but got no rHrI.r)r ValueErrorrG)rHrIrr7s rget_tolerancesrMcsd"  & $(L&f=Q @    d.Tz!6**r) identifierextrafirst_mismatch_idxdefault_identifierrNrOrPc||}nt|r||}|d}|r||jdz }| |d|dz }|jS)aMakes a mismatch error message for bitwise values. Args: default_identifier (str): Default description of the compared values, e.g. "Tensor-likes". identifier (Optional[Union[str, Callable[[str], str]]]): Optional identifier that overrides ``default_identifier``. Can be passed as callable in which case it will be called with ``default_identifier`` to create the description at runtime. extra (Optional[str]): Extra information to be placed after the message header and the mismatch statistics. first_mismatch_idx (Optional[tuple[int]]): the index of the first mismatch, for each dimension. z are not 'equal'!  z)The first mismatched element is at index z. )r'strip)rQrNrOrPrs r_make_bitwise_mismatch_msgrUsq"' *  23 L- .C  %++-##% :;M:NcRR 99;r)rNrO abs_diff_idx rel_diff_idxabs_diffrVrel_diffrWc r |dk(xr|dk( dtdtdtttt tdffdtdtf fd } ||}nt |r||}|d rd nd d } |r| |jd z } | | d|||z } | | d|||z } | jS)agMakes a mismatch error message for numeric values. Args: default_identifier (str): Default description of the compared values, e.g. "Tensor-likes". identifier (Optional[Union[str, Callable[[str], str]]]): Optional identifier that overrides ``default_identifier``. Can be passed as callable in which case it will be called with ``default_identifier`` to create the description at runtime. extra (Optional[str]): Extra information to be placed after the message header and the mismatch statistics. abs_diff (float): Absolute difference. abs_diff_idx (Optional[Union[int, Tuple[int, ...]]]): Optional index of the absolute difference. atol (float): Allowed absolute tolerance. Will only be added to mismatch statistics if it or ``rtol`` are ``> 0``. rel_diff (float): Relative difference. rel_diff_idx (Optional[Union[int, Tuple[int, ...]]]): Optional index of the relative difference. rtol (float): Allowed relative tolerance. Will only be added to mismatch statistics if it or ``atol`` are ``> 0``. rrdiffidx.tolrcj||jd|}n d|d|d|}s |d|dz }|dzS)Nz difference: z Greatest z at index z (up to z allowed)rS)title)rr[r\r]requalitys r make_diff_msgz)_make_mismatch_msg..make_diff_msgsX ;ZZ\N-v6CdV=jFC XcU), ,CTzrz are not equalclosez! rSabsolute)rr[r\r]relative)rfloatrr intr/r'rT) rQrNrOrXrVrIrYrWrHrarr`s @r_make_mismatch_msgrhs:qy&TQYH   eCsCx01 2    ' *  23 L X'7!C5 IC  %++-##=jx\t TTC=jx\t TTC 99;rrNactualexpectedc t||z }|dk(r tdn |t|z }td|d|d|d||||S)a"Makes a mismatch error message for scalars. Args: actual (Union[bool, int, float, complex]): Actual scalar. expected (Union[bool, int, float, complex]): Expected scalar. rtol (float): Relative tolerance. atol (float): Absolute tolerance. identifier (Optional[Union[str, Callable[[str], str]]]): Optional description for the scalars. Can be passed as callable in which case it will be called by the default value to create the description at runtime. Defaults to "Scalars". rinfScalarsz Expected z but got rK)rQrNrOrXrIrYrH)absrfrh)rjrkrHrIrNrXrYs rmake_scalar_mismatch_msgrps^&6H$%H'1}uU|(S]2JH $(9VHA6   rmatchescDdtdttdfffd }j}|ttjz }d|d|d||z dd } |j j rZ|j jd k(rAttjd d j} td|| | S|j} |j} j} |j j sT|j js>| jtj} | jtj} tj| | z }d || <tj |d \}}|tj| z }d || <tj |d \}}t#d|| |j%|t|||j%|t|| S)aMakes a mismatch error message for tensors. Args: actual (torch.Tensor): Actual tensor. expected (torch.Tensor): Expected tensor. matches (torch.Tensor): Boolean mask of the same shape as ``actual`` and ``expected`` that indicates the location of matches. rtol (float): Relative tolerance. atol (float): Absolute tolerance. identifier (Optional[Union[str, Callable[[str], str]]]): Optional description for the tensors. Can be passed as callable in which case it will be called by the default value to create the description at runtime. Defaults to "Tensor-likes". flat_indexr.cjsyg}jdddD]$}t||\}}|}|j|&t|dddS)Nr )shapedivmodr<r/)rs inverse_indexsizedivmodrqs runravel_flat_indexz4make_tensor_mismatch_msg..unravel_flat_indexsg}} MM$B$' &Dj$/HCJ   % & ]4R4())rzMismatched elements: z / z (z.1%)F)as_tuplerz Tensor-likes)rQrNrOrP) rQrNrOrXrVrIrYrWrH)rgr/numelr:sumr=is_floating_pointitemsizenonzerotolistrUflatten is_complextoint64rorBrhr")rjrkrqrHrIrNr|number_of_elementstotal_mismatchesrOrP actual_flat expected_flat matches_flatrX max_abs_diffmax_abs_diff_flat_idxrY max_rel_diffmax_rel_diff_flat_idxs ` rmake_tensor_mismatch_msgrs. *s *uS#X *!)C '0B,CC  015G4HI 1 1# 6a 9 ||%%&,,*?*?1*D"5=='E#J1#M#T#T#VW)-!1   .."K$$&M??$L << ) )&,,2I2I"nnU[[1 %((5 yy}45HH\*/))Ha*@'L'%))M22HH\*/))Ha*@'L' )""$',A(BC ""$',A(BC   rceZdZdZy)UnsupportedInputszhException to be raised during the construction of a :class:`Pair` in case it doesn't support the inputs.N)r*r+r,r-r rrrrSsrrrc eZdZdZdddededeedfded d f d Zed efd Z ed ede e ee dfffdZ ddde e dedeedfd efdZej"ddZd ee eeeefffdZd efdZy )PairaABC for all comparison pairs to be used in conjunction with :func:`assert_equal`. Each subclass needs to overwrite :meth:`Pair.compare` that performs the actual comparison. Each pair receives **all** options, so select the ones applicable for the subclass and forward the rest to the super class. Raising an :class:`UnsupportedInputs` during constructions indicates that the pair is not able to handle the inputs and the next pair type will be tried. All other errors should be raised as :class:`ErrorMeta`. After the instantiation, :meth:`Pair._make_error_meta` can be used to automatically handle overwriting the message with a user supplied one and id handling. r rrjrkr.unknown_parametersrNc <||_||_||_||_yr)rjrkr_unknown_parameters)rrjrkrrs rrz Pair.__init__ds!   #5 rctr)rr rr_inputs_not_supportedzPair._inputs_not_supportedqsrr7clscXtfd|Dstjyy)zcChecks if all inputs are instances of a given class and raise :class:`UnsupportedInputs` otherwise.c36K|]}t|ywr)r%)r!rDrs rr#z0Pair._check_inputs_isinstance..xs>e:eS)>sN)allrr)rr7s` r_check_inputs_isinstancezPair._check_inputs_isinstanceus$>v>>  & & (?rrrc\t|||st|dr|j|)aRaises an :class:`ErrorMeta` from a given exception type and message and the stored id. .. warning:: If you use this before the ``super().__init__(...)`` call in the constructor, you have to pass the ``id`` explicitly. rr)r hasattrr)rrrrs r_failz Pair._fail{s+cRGD$-=>rc|j |j1|jtd|jd|jyy)NzNone mismatch:  is not rjrkrrrs rrzNonePair.compares@ # (= JJ/$++ht}}o V )>rr)r*r+r,r-rrrr0r1s@rrrs,#?s?c?s?t? rrc eZdZdZdededeedfdeddf fd Zedeedffd Z dededeedfdee e ffd Z d edeedfde fd Z ddZ xZS) BooleanPairzPair for :class:`bool` inputs. .. note:: If ``numpy`` is available, also handles :class:`numpy.bool_` inputs. rjrkr.rrNc V|j|||\}}t|||fi|y)Nr)_process_inputsrr)rrjrkrrrs rrzBooleanPair.__init__s6 //R/H >-=>rcptg}tr|jtjt |Sr)bool HAS_NUMPYr<npbool_r/rrs r_supported_typeszBooleanPair._supported_typess&&  JJrxx Szrclj||jfd||fD\}}||fS)Nrc3DK|]}j|ywrN)_to_bool)r! bool_likerrs rr#z.BooleanPair._process_inputs..s# 09DMM)M +  rrrrjrkrs` `rrzBooleanPair._process_inputssF %%fhDDh=O xrrct|tr|St|tjr|j St t dt|d|)NzUnknown boolean type rKr)r%rrrr"r r>r)rrrs rrzBooleanPair._to_boolsP i &   288 ,>># #24 ?2C1E" rc|j|jur1|jtd|jd|jyy)NzBooleans mismatch: rrrs rrzBooleanPair.compares> ;;dmm + JJ%dkk](4==/J  ,rr)r*r+r,r-rr/rpropertyrrrrrrr0r1s@rrrs ? ? ? #s(O ?  ?  ?%c "2   %( 16sCx tTz  #eCHo$rrceZdZdZeej eeje ejiZ e e jZdddddddedede ed fd eed eed ed ededdffdZede ed ffdZdedede ed fde eeee feeee fffdZdede ed fdeeee ffdZddZdeefdZxZS) NumberPairaPair for Python number (:class:`int`, :class:`float`, and :class:`complex`) inputs. .. note:: If ``numpy`` is available, also handles :class:`numpy.number` inputs. Kwargs: rtol (Optional[float]): Relative tolerance. If specified ``atol`` must also be specified. If omitted, default values based on the type are selected with the below table. atol (Optional[float]): Absolute tolerance. If specified ``rtol`` must also be specified. If omitted, default values based on the type are selected with the below table. equal_nan (bool): If ``True``, two ``NaN`` values are considered equal. Defaults to ``False``. check_dtype (bool): If ``True``, the type of the inputs will be checked for equality. Defaults to ``False``. The following table displays correspondence between Python number type and the ``torch.dtype``'s. See :func:`assert_close` for the corresponding tolerances. +------------------+-------------------------------+ | ``type`` | corresponding ``torch.dtype`` | +==================+===============================+ | :class:`int` | :attr:`~torch.int64` | +------------------+-------------------------------+ | :class:`float` | :attr:`~torch.float64` | +------------------+-------------------------------+ | :class:`complex` | :attr:`~torch.complex64` | +------------------+-------------------------------+ r NF)rrHrI equal_nan check_dtyperjrkr.rHrIrrrrc |j|||\}}t |||fd|i|t||fD cgc]} |jt | c} |||d\|_|_||_||_ ycc} w)NrrrHrIr) rrrrM_TYPE_TO_DTYPErrHrIrr) rrjrkrrHrIrrrrDrs rrzNumberPair.__init__)s //R/H EbE4DE-=CX.Ls# 4?DOOKBO / rrrs` `rrzNumberPair._process_inputsHsF %%fhDr)rrrs rrzNumberPair._to_numberQs] K;##% %  T%7%7 8 1${2C1DAF2 rc .|jrmt|jt|jurC|j t dt|jdt|jd|j|jk(ry|j r?tj|jr tj|jryt|j|jz }|j|jt|jzz}tj|r||kry|j t t|j|j|j|jy)NzThe (d)types do not match: rrK)rHrI)rrrjrkrrrcmathisnanrorIrHisfiniterp)rrX tolerances rrzNumberPair.compare]s   T[[ 1dmm9L L JJ-d4;;.?-@T$--EXDYYZ[  ;;$-- '  >>ekk$++65;;t}};U t{{T]]23II C ,> >> >>( #I(=   $ T]]  rcy)N)rHrIrrr rs rrzNumberPair.extra_reprws rr)r*r+r,r-rgr:rrffloat64complex complex128rr/keysrrrrrrrrr rrrrrrr0r1s@rrrsz: U[[ u}}!!N .--/0M! $ $!''' #s(O ' uo 'uo''' ' '0%c "2   %( 16sCx uS%()5eW1D+EE F   ',S#X  sE7" #  4 HSM rrceZdZdZdddddddddd deded eed fd ed eed eededededededeffdZ deded eed fd edee je jff dZ dede jfdZ de jd eed fddfdZd(dZde jde jddfdZde jde jdee je jffdZde jde jddfdZde jde jd ed ededdf dZde jde jd ed ededdf d Zde jde jd ed ededdf d!Zddd"de jde jded#eeeeegeffddf d$Zdd%de jde jd ed eded#eeeeegeffddfd&Zdeefd'ZxZS))TensorLikePairaPair for :class:`torch.Tensor`-like inputs. Kwargs: allow_subclasses (bool): rtol (Optional[float]): Relative tolerance. If specified ``atol`` must also be specified. If omitted, default values based on the type are selected. See :func:assert_close: for details. atol (Optional[float]): Absolute tolerance. If specified ``rtol`` must also be specified. If omitted, default values based on the type are selected. See :func:assert_close: for details. equal_nan (bool): If ``True``, two ``NaN`` values are considered equal. Defaults to ``False``. check_device (bool): If ``True`` (default), asserts that corresponding tensors are on the same :attr:`~torch.Tensor.device`. If this check is disabled, tensors on different :attr:`~torch.Tensor.device`'s are moved to the CPU before being compared. check_dtype (bool): If ``True`` (default), asserts that corresponding tensors have the same ``dtype``. If this check is disabled, tensors with different ``dtype``'s are promoted to a common ``dtype`` (according to :func:`torch.promote_types`) before being compared. check_layout (bool): If ``True`` (default), asserts that corresponding tensors have the same ``layout``. If this check is disabled, tensors with different ``layout``'s are converted to strided tensors before being compared. check_stride (bool): If ``True`` and corresponding tensors are strided, asserts that they have the same stride. r TNF) rallow_subclassesrHrIr check_devicer check_layout check_striderjrkr.rrHrIrrrrrrc  |j||||\}}t |||fd|i| t|||||j\|_|_||_||_| |_ | |_ | |_ y)N)rrrr) rrrrMrrHrIrrrrr)rrjrkrrrHrIrrrrrrrs rrzTensorLikePair.__init__s  // H6F0  EbE4DE- H4dtww  49#(&((rrc0t|t|xst|t|}|sj|s&t|t|urjfd||fD\}}||fD]}j||||fS)Nc3@K|]}j|ywr) _to_tensor)r!rDrs rr#z1TensorLikePair._process_inputs..sSuDOOE2Ssr)r%rr_check_supported)rrjrkrrdirectly_relatedtensors` rrzTensorLikePair._process_inputss&fd8n=  d6lB   & & (DLX$F  & & (S@RSx( 1F  ! !&R ! 0 1xr tensor_likect|tjr|S tj|S#t$r|j YywxYwr)r%r:r; as_tensorr.r)rrs rrzTensorLikePair._to_tensorsE k5<< 0  )??;/ / )  & & ( )s3AArc0|jtjtjtjtj tj tjtjhvrttd|j|y)NzUnsupported tensor layout r) layoutr:stridedjagged sparse_coo sparse_csr sparse_csc sparse_bsr sparse_bscr rL)rrrs rrzTensorLikePair._check_supportedsu == MM LL               !  8HR  rc|j|j}}|j||td||fDry|j ||\}}|j ||y)Nc3NK|]}|jjdk(yw)metaN)devicer)r!rDs rr#z)TensorLikePair.compare..sKuu||  F*Ks#%)rjrk_compare_attributesany_equalize_attributes_compare_values)rrjrks rrzTensorLikePair.compares`;;    2 K8JK K 44VXF VX.rc>dtdtdtdtffd }|j|jk7r|d|j|j|j|jk7r|d|j|jnS|jrG|j |j k7r&|d|j |j |j |j k7r+jr|d |j |j np|j tjk(rSjrG|j|jk7r&|d |j|jjr7|j|jk7r|d |j|jjr9|j|jk7r|d |j|jy y y )aChecks if the attributes of two tensors match. Always checks - the :attr:`~torch.Tensor.shape`, - whether both inputs are quantized or not, - and if they use the same quantization scheme. Checks for - :attr:`~torch.Tensor.layout`, - :meth:`~torch.Tensor.stride`, - :attr:`~torch.Tensor.device`, and - :attr:`~torch.Tensor.dtype` are optional and can be disabled through the corresponding ``check_*`` flag during construction of the pair. attribute_name actual_valueexpected_valuerc Fjtd|d|d|dy)NzThe values for attribute 'z' do not match: rrK)rr)rrrrs rraise_mismatch_errorz@TensorLikePair._compare_attributes..raise_mismatch_errors2 JJ,^,<> ) &,, G   ("7"7 7  3 3X5J5J  V^^%59I9I9K%K fnn.>@P@P@R S ==HOO +  $Xv}}hooN MMU]] *!! 8??#44 V]]_hoo>O P   (//!A 6==(// J     > &,, G!? rc|js |jr |j}|j}|j|jk7r |j}|j}|j|jk7r|j}|j}|tj tj tjfvrtj}|tj tj tjfvrtj}t j||}|j|}|j|}|j|jk7r^|jtjk7r|jn|}|jtjk7r|jn|}||fS)a_Equalizes some attributes of two tensors for value comparison. If ``actual`` and ``expected`` are ... - ... not on the same :attr:`~torch.Tensor.device`, they are moved CPU memory. - ... not of the same ``dtype``, they are promoted to a common ``dtype`` (according to :func:`torch.promote_types`). - ... not of the same ``layout``, they are converted to strided tensors. Args: actual (Tensor): Actual tensor. expected (Tensor): Expected tensor. Returns: (Tuple[Tensor, Tensor]): Equalized tensors. )is_mpscpurr=r:uint64uint32uint16r promote_typesrrr to_dense)rrjrk actual_dtypeexpected_dtyper=s rrz#TensorLikePair._equalize_attributessC, ==HOOZZ\F||~H ==HOO +ZZ\F||~H <<8>> )!<.bitwise_compesg3;$#+#&vZv6QQ 99') :rrHrIr)r_compare_quantized_values is_sparse_compare_sparse_coo_valuesrr:r r rr!_compare_sparse_compressed_valuesr valuesr.r=rrr;rfrrr rrrHrIr)rrjrk compare_fnr/s` rrzTensorLikePair._compare_valuesTs]   77J   88J ]]               ??J ]]ell *%}}0AHF;;J \\ + + 0E0E0JJN   ,,         %U3#0D+D%EF  2&J;;J H499499 rcj|j|j|j|||dS)aCompares quantized tensors by comparing the :meth:`~torch.Tensor.dequantize`'d variants for closeness. .. note:: A detailed discussion about why only the dequantized variant is checked for closeness rather than checking the individual quantization parameters for closeness and the integer representation for equality can be found in https://github.com/pytorch/pytorch/issues/68548. c(d|jS)Nz Quantized )lower)rQs rz:TensorLikePair._compare_quantized_values..sJ?Q?W?W?Y>Z2[rr-)r. dequantizerrjrkrHrIrs rr1z(TensorLikePair._compare_quantized_valuess@"11        ![ 2  rc.|j|jk7r8|jtd|jd|j|j|jk7r8|jtd|jd|j|j |j |j d|j |j|j|||dy) zCompares sparse COO tensors by comparing - the number of sparse dimensions, - the number of non-zero elements (nnz) for equality, - the indices for equality, and - the values for closeness. zFThe number of sparse dimensions in sparse COO tensors does not match: rzEThe number of specified values in sparse COO tensors does not match: zSparse COO indicesrizSparse COO valuesr-N) sparse_dimrr_nnz_compare_regular_values_equal_indicesr._valuesr<s rr3z)TensorLikePair._compare_sparse_coo_valuess    ("5"5"7 7 JJ\((*+40C0C0E/FH  ;;=HMMO + JJ[{{}oT(--/):<  ** OO     + + ** NN     * + rc tjdtjjtjjftj dtjj tjjftjdtjjtjjftjdtjj tjjfi|j\}}}|j|jk7r;|jtd|d|jd|j||} ||} tj| j| j} |j!| j#| | j#| d|d |j$ |j!||j#| ||j#| d|d |j$ |j'|j)|j)|||d|d  y )zCompares sparse compressed tensors by comparing - the number of non-zero elements (nnz) for equality, - the plain indices for equality, - the compressed indices for equality, and - the values for closeness. CSRCSCBSRBSCz)The number of specified values in sparse z tensors does not match: rzSparse  riz valuesr-N)r:r r; crow_indices col_indicesr  ccol_indices row_indicesrrrr?rrr'r=r@rr*r.r5) rrjrkrHrIr format_namecompressed_indices_methodplain_indices_methodactual_compressed_indicesexpected_compressed_indices indices_dtypes rr4z0TensorLikePair._compare_sparse_compressed_valuess5"    )) ((    )) ((    )) ((    )) ((!H * --+HD .0D. ;;=HMMO + JJ? }Le{{}oT(--/):< %>f$E!&?&I#++ % + +-H-N-N  ** % ( ( 7 ' * *= 9  Q/H/Q/Q.RS + **  ( + +M :  * - -m <  Q/C/L/L.MN + ** MMO OO   W5 + r)rrNrNc2|j||dd||y)z.Checks if the values of two tensors are equal.rr-N)r.)rrjrkrrNs rr@z,TensorLikePair._compare_regular_values_equals$ ** H11 j + rricPtj|||||}tj|ry|jtjgk(r-t |j |j |||}nt||||||}|jt|y)zHChecks if the values of two tensors are close up to a desired tolerance.r0N)rHrIrN) r:iscloserrvSizerpr"rrr) rrjrkrHrIrrNrqrs rr.z,TensorLikePair._compare_regular_values_close*s-- H4di  99W   <<5::b> )*  % C+'4JC >3'rcy)N)rHrIrrrrrr rs rrzTensorLikePair.extra_reprIs rr)r*r+r,r-rr/rrrfrr:r;rrrrrrrr1r3r4r rrr@r.rrr0r1s@rrrs4!!% $ $! !"))) #s(O )  )uo)uo)))))) )<  %( 16sCx TX u||U\\) * ")c)ell) u|| E#s(O PT /7H 7H,,7H  7Hr3 ll3 .3ll3 u||U\\) *3 j0 ell0 ell0 t0 d   ,,        4.  . ,,.  .  . .  . `K  K ,,K  K  K K  K d AE     ,,    U3#(<#<=>     *BF( (,,(  (  ((U3#(<#<=>( (>  HSM  rr)sequence_types mapping_typesr pair_typesrXrYoptionsc t||rt|tst||rt|tsst|}t|}||k7rttd|d||g} t |D].} | j t|| || f|||g|| d|0| St||rt||rt|j} t|j} | | k7r4| | z } | | z }ttdt| dt||| }tjt5t|}dddg} |D].}| j t||||f|||g||d|0| S|D]} |||fd|i|gcStt&dt%|dt%|d|#1swYxYw#t$rYVt$rt$rj}td |j d d j#d |Dd t%|j d|dt%|j d|d |d}~wwxYw)aOriginates pairs from the individual inputs. ``actual`` and ``expected`` can be possibly nested :class:`~collections.abc.Sequence`'s or :class:`~collections.abc.Mapping`'s. In this case the pairs are originated by recursing through them. Args: actual (Any): Actual input. expected (Any): Expected input. pair_types (Sequence[Type[Pair]]): Sequence of pair types that will be tried to construct with the inputs. First successful pair will be used. sequence_types (Tuple[Type, ...]): Optional types treated as sequences that will be checked elementwise. mapping_types (Tuple[Type, ...]): Optional types treated as mappings that will be checked elementwise. id (Tuple[Any, ...]): Optional id of a pair that will be included in an error message. **options (Any): Options passed to each pair during construction. Raises: ErrorMeta: With :class`AssertionError`, if the inputs are :class:`~collections.abc.Sequence`'s, but their length does not match. ErrorMeta: With :class`AssertionError`, if the inputs are :class:`~collections.abc.Mapping`'s, but their set of keys do not match. ErrorMeta: With :class`TypeError`, if no pair is able to handle the inputs. ErrorMeta: With any expected exception that happens during the construction of a pair. Returns: (List[Pair]): Originated pairs. z&The length of the sequences mismatch: rr)rZrXrYrzKThe keys of the mappings do not match: Missing keys in the actual mapping: z( Additional keys in the actual mapping: NrzOriginating a z () at item rc34K|]}t|gywrrr s rr#z"originate_pairs..sKg\`CQUPVKKgr$z with z(): z and = resulted in the unexpected exception above. If you are a user and see this message during normal operation please file an issue at https://github.com/pytorch/pytorch/issues. If you are a developer and working on the comparison functions, please except the previous error and raise an expressive `ErrorMeta` instead.z5No comparison pair was able to handle inputs of type z and rK)r%rlenr rrangeextendoriginate_pairssetrsorted contextlibsuppressr.r RuntimeErrorr*r&rr>)rjrkrZrXrYrr[ actual_len expected_lenpairsr\ actual_keys expected_keys missing_keysadditional_keysrkey pair_typers rrbrbUsN 6>*63' x 08S)[ 8}  %8 DW  $ C LL3KSM *#1"/!zSz    FM *z(M/R&++-( HMMO, - '(;6L)M9O;;A,;O:PQ>>D_>U=VX '   + $dd   s+ G G"G" I(-I(>A%I##I(rZrXrYc Nd} t||f|||d|}g} |D]} | j| g} | jS#t$r}|jdd}~wwxYw#t$r}| j |Yd}~jd}~wt $r} t d| d| d} ~ wwxYw)aPAsserts that inputs are equal. ``actual`` and ``expected`` can be possibly nested :class:`~collections.abc.Sequence`'s or :class:`~collections.abc.Mapping`'s. In this case the comparison happens elementwise by recursing through them. Args: actual (Any): Actual input. expected (Any): Expected input. pair_types (Sequence[Type[Pair]]): Sequence of :class:`Pair` types that will be tried to construct with the inputs. First successful pair will be used. Defaults to only using :class:`ObjectPair`. sequence_types (Tuple[Type, ...]): Optional types treated as sequences that will be checked elementwise. mapping_types (Tuple[Type, ...]): Optional types treated as mappings that will be checked elementwise. **options (Any): Options passed to each pair during construction. TrqNz Comparing r^)rbr r)rr<r.rgpop) rjrkrZrXrYr[__tracebackhide__rj error_meta error_metaspairrs rnot_close_error_metasrxs0 .   ")'    $&K  LLN4-K ?? A .!!#-. +   z * * &``  s:AA$ A! AA!$ B$-B B$BB$) rrHrIrrrrrrrrrrrrrc d} t||ttttf|||||||| |  } | r| dj | y)a2Asserts that ``actual`` and ``expected`` are close. If ``actual`` and ``expected`` are strided, non-quantized, real-valued, and finite, they are considered close if .. math:: \lvert \text{actual} - \text{expected} \rvert \le \texttt{atol} + \texttt{rtol} \cdot \lvert \text{expected} \rvert Non-finite values (``-inf`` and ``inf``) are only considered close if and only if they are equal. ``NaN``'s are only considered equal to each other if ``equal_nan`` is ``True``. In addition, they are only considered close if they have the same - :attr:`~torch.Tensor.device` (if ``check_device`` is ``True``), - ``dtype`` (if ``check_dtype`` is ``True``), - ``layout`` (if ``check_layout`` is ``True``), and - stride (if ``check_stride`` is ``True``). If either ``actual`` or ``expected`` is a meta tensor, only the attribute checks will be performed. If ``actual`` and ``expected`` are sparse (either having COO, CSR, CSC, BSR, or BSC layout), their strided members are checked individually. Indices, namely ``indices`` for COO, ``crow_indices`` and ``col_indices`` for CSR and BSR, or ``ccol_indices`` and ``row_indices`` for CSC and BSC layouts, respectively, are always checked for equality whereas the values are checked for closeness according to the definition above. If ``actual`` and ``expected`` are quantized, they are considered close if they have the same :meth:`~torch.Tensor.qscheme` and the result of :meth:`~torch.Tensor.dequantize` is close according to the definition above. ``actual`` and ``expected`` can be :class:`~torch.Tensor`'s or any tensor-or-scalar-likes from which :class:`torch.Tensor`'s can be constructed with :func:`torch.as_tensor`. Except for Python scalars the input types have to be directly related. In addition, ``actual`` and ``expected`` can be :class:`~collections.abc.Sequence`'s or :class:`~collections.abc.Mapping`'s in which case they are considered close if their structure matches and all their elements are considered close according to the above definition. .. note:: Python scalars are an exception to the type relation requirement, because their :func:`type`, i.e. :class:`int`, :class:`float`, and :class:`complex`, is equivalent to the ``dtype`` of a tensor-like. Thus, Python scalars of different types can be checked, but require ``check_dtype=False``. Args: actual (Any): Actual input. expected (Any): Expected input. allow_subclasses (bool): If ``True`` (default) and except for Python scalars, inputs of directly related types are allowed. Otherwise type equality is required. rtol (Optional[float]): Relative tolerance. If specified ``atol`` must also be specified. If omitted, default values based on the :attr:`~torch.Tensor.dtype` are selected with the below table. atol (Optional[float]): Absolute tolerance. If specified ``rtol`` must also be specified. If omitted, default values based on the :attr:`~torch.Tensor.dtype` are selected with the below table. equal_nan (Union[bool, str]): If ``True``, two ``NaN`` values will be considered equal. check_device (bool): If ``True`` (default), asserts that corresponding tensors are on the same :attr:`~torch.Tensor.device`. If this check is disabled, tensors on different :attr:`~torch.Tensor.device`'s are moved to the CPU before being compared. check_dtype (bool): If ``True`` (default), asserts that corresponding tensors have the same ``dtype``. If this check is disabled, tensors with different ``dtype``'s are promoted to a common ``dtype`` (according to :func:`torch.promote_types`) before being compared. check_layout (bool): If ``True`` (default), asserts that corresponding tensors have the same ``layout``. If this check is disabled, tensors with different ``layout``'s are converted to strided tensors before being compared. check_stride (bool): If ``True`` and corresponding tensors are strided, asserts that they have the same stride. msg (Optional[Union[str, Callable[[str], str]]]): Optional error message to use in case a failure occurs during the comparison. Can also passed as callable in which case it will be called with the generated message and should return the new message. Raises: ValueError: If no :class:`torch.Tensor` can be constructed from an input. ValueError: If only ``rtol`` or ``atol`` is specified. AssertionError: If corresponding inputs are not Python scalars and are not directly related. AssertionError: If ``allow_subclasses`` is ``False``, but corresponding inputs are not Python scalars and have different types. AssertionError: If the inputs are :class:`~collections.abc.Sequence`'s, but their length does not match. AssertionError: If the inputs are :class:`~collections.abc.Mapping`'s, but their set of keys do not match. AssertionError: If corresponding tensors do not have the same :attr:`~torch.Tensor.shape`. AssertionError: If ``check_layout`` is ``True``, but corresponding tensors do not have the same :attr:`~torch.Tensor.layout`. AssertionError: If only one of corresponding tensors is quantized. AssertionError: If corresponding tensors are quantized, but have different :meth:`~torch.Tensor.qscheme`'s. AssertionError: If ``check_device`` is ``True``, but corresponding tensors are not on the same :attr:`~torch.Tensor.device`. AssertionError: If ``check_dtype`` is ``True``, but corresponding tensors do not have the same ``dtype``. AssertionError: If ``check_stride`` is ``True``, but corresponding strided tensors do not have the same stride. AssertionError: If the values of corresponding tensors are not close according to the definition above. The following table displays the default ``rtol`` and ``atol`` for different ``dtype``'s. In case of mismatching ``dtype``'s, the maximum of both tolerances is used. +---------------------------+------------+----------+ | ``dtype`` | ``rtol`` | ``atol`` | +===========================+============+==========+ | :attr:`~torch.float16` | ``1e-3`` | ``1e-5`` | +---------------------------+------------+----------+ | :attr:`~torch.bfloat16` | ``1.6e-2`` | ``1e-5`` | +---------------------------+------------+----------+ | :attr:`~torch.float32` | ``1.3e-6`` | ``1e-5`` | +---------------------------+------------+----------+ | :attr:`~torch.float64` | ``1e-7`` | ``1e-7`` | +---------------------------+------------+----------+ | :attr:`~torch.complex32` | ``1e-3`` | ``1e-5`` | +---------------------------+------------+----------+ | :attr:`~torch.complex64` | ``1.3e-6`` | ``1e-5`` | +---------------------------+------------+----------+ | :attr:`~torch.complex128` | ``1e-7`` | ``1e-7`` | +---------------------------+------------+----------+ | :attr:`~torch.quint8` | ``1.3e-6`` | ``1e-5`` | +---------------------------+------------+----------+ | :attr:`~torch.quint2x4` | ``1.3e-6`` | ``1e-5`` | +---------------------------+------------+----------+ | :attr:`~torch.quint4x2` | ``1.3e-6`` | ``1e-5`` | +---------------------------+------------+----------+ | :attr:`~torch.qint8` | ``1.3e-6`` | ``1e-5`` | +---------------------------+------------+----------+ | :attr:`~torch.qint32` | ``1.3e-6`` | ``1e-5`` | +---------------------------+------------+----------+ | other | ``0.0`` | ``0.0`` | +---------------------------+------------+----------+ .. note:: :func:`~torch.testing.assert_close` is highly configurable with strict default settings. Users are encouraged to :func:`~functools.partial` it to fit their use case. For example, if an equality check is needed, one might define an ``assert_equal`` that uses zero tolerances for every ``dtype`` by default: >>> import functools >>> assert_equal = functools.partial(torch.testing.assert_close, rtol=0, atol=0) >>> assert_equal(1e-9, 1e-10) Traceback (most recent call last): ... AssertionError: Scalars are not equal! Expected 1e-10 but got 1e-09. Absolute difference: 9.000000000000001e-10 Relative difference: 9.0 Examples: >>> # tensor to tensor comparison >>> expected = torch.tensor([1e0, 1e-1, 1e-2]) >>> actual = torch.acos(torch.cos(expected)) >>> torch.testing.assert_close(actual, expected) >>> # scalar to scalar comparison >>> import math >>> expected = math.sqrt(2.0) >>> actual = 2.0 / math.sqrt(2.0) >>> torch.testing.assert_close(actual, expected) >>> # numpy array to numpy array comparison >>> import numpy as np >>> expected = np.array([1e0, 1e-1, 1e-2]) >>> actual = np.arccos(np.cos(expected)) >>> torch.testing.assert_close(actual, expected) >>> # sequence to sequence comparison >>> import numpy as np >>> # The types of the sequences do not have to match. They only have to have the same >>> # length and their elements have to match. >>> expected = [torch.tensor([1.0]), 2.0, np.array(3.0)] >>> actual = tuple(expected) >>> torch.testing.assert_close(actual, expected) >>> # mapping to mapping comparison >>> from collections import OrderedDict >>> import numpy as np >>> foo = torch.tensor(1.0) >>> bar = 2.0 >>> baz = np.array(3.0) >>> # The types and a possible ordering of mappings do not have to match. They only >>> # have to have the same set of keys and their elements have to match. >>> expected = OrderedDict([("foo", foo), ("bar", bar), ("baz", baz)]) >>> actual = {"baz": baz, "bar": bar, "foo": foo} >>> torch.testing.assert_close(actual, expected) >>> expected = torch.tensor([1.0, 2.0, 3.0]) >>> actual = expected.clone() >>> # By default, directly related instances can be compared >>> torch.testing.assert_close(torch.nn.Parameter(actual), expected) >>> # This check can be made more strict with allow_subclasses=False >>> torch.testing.assert_close( ... torch.nn.Parameter(actual), expected, allow_subclasses=False ... ) Traceback (most recent call last): ... TypeError: No comparison pair was able to handle inputs of type and . >>> # If the inputs are not directly related, they are never considered close >>> torch.testing.assert_close(actual.numpy(), expected) Traceback (most recent call last): ... TypeError: No comparison pair was able to handle inputs of type and . >>> # Exceptions to these rules are Python scalars. They can be checked regardless of >>> # their type if check_dtype=False. >>> torch.testing.assert_close(1.0, 1, check_dtype=False) >>> # NaN != NaN by default. >>> expected = torch.tensor(float("Nan")) >>> actual = expected.clone() >>> torch.testing.assert_close(actual, expected) Traceback (most recent call last): ... AssertionError: Scalars are not close! Expected nan but got nan. Absolute difference: nan (up to 1e-05 allowed) Relative difference: nan (up to 1.3e-06 allowed) >>> torch.testing.assert_close(actual, expected, equal_nan=True) >>> expected = torch.tensor([1.0, 2.0, 3.0]) >>> actual = torch.tensor([1.0, 4.0, 5.0]) >>> # The default error message can be overwritten. >>> torch.testing.assert_close( ... actual, expected, msg="Argh, the tensors are not close!" ... ) Traceback (most recent call last): ... AssertionError: Argh, the tensors are not close! >>> # If msg is a callable, it can be used to augment the generated message with >>> # extra information >>> torch.testing.assert_close( ... actual, expected, msg=lambda msg: f"Header\n\n{msg}\n\nFooter" ... ) Traceback (most recent call last): ... AssertionError: Header Tensor-likes are not close! Mismatched elements: 2 / 3 (66.7%) Greatest absolute difference: 2.0 at index (1,) (up to 1e-05 allowed) Greatest relative difference: 1.0 at index (1,) (up to 1.3e-06 allowed) Footer T) rZrrHrIrrrrrrrN)rxrrrrr)) rjrkrrHrIrrrrrrrtrvs r assert_closerz%shp'      *  !!! #K(!n%%c**rz`torch.testing.assert_allclose()` is deprecated since 1.12 and will be removed in a future release. Please use `torch.testing.assert_close()` instead. You can find detailed upgrade instructions in https://github.com/pytorch/pytorch/issues/61844.)categoryc t|tjstj|}t|tjs!tj||j}|C|At ||tj dtjdtjdi\}}tjj|||||ddd|xsd y) a5 .. warning:: :func:`torch.testing.assert_allclose` is deprecated since ``1.12`` and will be removed in a future release. Please use :func:`torch.testing.assert_close` instead. You can find detailed upgrade instructions `here `_. )r=N)r2r2)g-C6?r3)r3g:0yE>r5TF)rHrIrrrrr) r%r:r;rr=rGfloat16float32rtestingrz)rjrkrHrIrrs rassert_allcloser8s* fell +f% h -<< = | '   | | | d MM   K4 r)NNTr)Drrcollections.abc collectionsrerrtypingrrrrr typing_extensionsr r:numpyrrModuleNotFoundErrorr.r r}bfloat16r~r complex32 complex64rr?updatedictfromkeysquint8quint2x4quint4x2qint8qint32r;r=r/rfrGrMrrgrUrhrrrprrABCrrrrrrMappingrrrbrxrz FutureWarningrr rrrs 0;;( I  B MM= NNM MM> MM< OO] OO^ lMM u~~u~~u{{ELLQ%--(JN" 5<<, -"tEKKue|1D$DEF" 5%<"< + 5<<, -+ 5/+ 5/+ c3h +  5%< +F>B/3 sHcUCZ$889: C=  !s, D>B:>;?;;sHcUCZ$889:; C= ;  ; 5eCHo!567 ; ;;5eCHo!567; ; ;H>B  $UG+ ,D#ug-.     sHcUCZ$889:  N>BP LLPllP\\P  P  PsHcUCZ$889:Pfs sL/377L/^LL2 t  3$3lx x vR TR t)4(@(@'B'2'>'>&@G GGd$ G $)$ G s# G c3hGG $ZG\)3}(3(@(@'B'2'>'>&@ C CCd$ C $)$ C s# CC )_CT"  6:P+ P+P+ P+ 5/ P+ 5/ P+P+P+P+P+P+ %XseSj112 3P+f e !  ) )) 5/) 5/ )  ) ) )  )[1I BsO OO