
    sg                        d dl Z d dlZddlmZ ddlmZmZ g ZddZddZ	d Z
d Zd	 Zd
 Zd Zd ZddZd Zg dZeD  ci c]&  }  eej(                  j*                  |        e|       ( c} ZeD  ci c]  }  ee|        e|        c} ZeD  ci c]  }  eej0                  |        e|        c} Z eej7                               Z eej7                               Z eej7                               Zd Zd Z yc c} w c c} w c c} w )    N   )is_masked_tensor)as_masked_tensormasked_tensorc                 h    || j                         S | j                  | d      j                         S )NT)allmasked_fill)datamasks     W/var/www/html/venv/lib/python3.12/site-packages/torch/masked/maskedtensor/reductions.py_masked_all_allr      s1    |xxzTE4(,,..    c                     |t        j                  | ||      S t        j                  | j                  | d      ||      S )NdimkeepdimT)torchr   r	   )r
   r   r   r   s       r   _masked_all_dimr      s<    |yy38899T%%teT2WMMr   c                  v    t        |       dk(  r!t        |      dk(  rt        | d   |d         S t        | i |S )Nr   r   r   r   )lenr   r   )argskwargss     r   _masked_allr      s=    
4yA~#f+*tAwVF^<<D+F++r   c                     t        |t              rt        | |g|      S t        |d      D ]  }t	        j
                  | ||      }  | S )NT)reverser   )
isinstanceint_multidim_anysortedr   any)r   r   r   ds       r   r   r   !   sJ    #sTC5'22C& 7yy1g67Kr   c                 L    | dk(  rt         S t        t        j                  |       S )Nr   )r   getattrr   maskedfns    r   _get_masked_fnr(   )   s     	U{5<<$$r   c                       fd}|S )Nc                    t        	      }| j                         }| j                  r| j                         j	                         n| j                         }	dk(  r |||      }nJ	dv r| j                         r ||j	                         |      j                  t        j                        }| j                         s|j                         j                         n|j                         }|j                  d      |   }|j                         j                         t        j                  |j                         |j                        j!                  d      z  }t        j"                  ||z        }n7| j                  r! |t%        |j	                         |            }n
 || |      }t'        |t        j(                  |            S )Nr   r   >   argmaxargmin)dtyper   )devicer   )r(   get_data	is_sparseget_maskvaluesis_sparse_cootor   r   to_sparse_cooindicesunbindsizenumeltensorr.   cumprodsumr   r   r!   )
self	masked_fnr
   r   result_data
sparse_idxr6   idxstrider'   s
            r   
reduce_allz%_torch_reduce_all.<locals>.reduce_all0   sb   "2&	}}+/>>t}}%%'t}} ;#Dt4K''D,>,>,@"4;;=t<??eii?PJ ))+ ""$,,.\\^ 
 ..#J/CYY[&&(5<<		DKK,gajF  ))C&L1K ^^#M$++-$FGK $Dt4KUYYt_==r    )r'   rC   s   ` r   _torch_reduce_allrE   /   s    >@ r   c                      d fd	}|S )Nc                 h   | j                   r!d	 d}t        j                  |       t        S t	        |       st        d      t        	      }| j                         }| j                         }	dk(  r |||||      }n || |||| j                               }t        |t        |||            S )NzThe sparse version of aa   is not implemented in reductions.
If you would like this operator to be supported, please file an issue for a feature request at https://github.com/pytorch/maskedtensor/issues with a minimal reproducible code snippet.
In the case that the semantics for the operator are not trivial, it would be appreciated to also include a proposal for the semantics.z*Input to reduce_dim must be a MaskedTensorr   )r   r   r   )r   r   r-   r   )r0   warningswarnNotImplementedr   	TypeErrorr(   r/   r1   r   r   )
r=   r   r   r-   msgr>   r
   r   r?   r'   s
            r   
reduce_dimz%_torch_reduce_dim.<locals>.reduce_dimT   s    >>( -@ @  MM#!!%HII"2&	}}}};#Dc7NK##we$--/K  ]4g-NOOr   FNrD   )r'   rM   s   ` r   _torch_reduce_dimrO   S   s    P2 r   c                       fd}|S )Nc                      t        |       dk(  r"t        |      dk(  r t              | d         S  t              | i |S Nr   r   )r   rE   rO   )r   r   r'   s     r   	reduce_fnz _torch_reduce.<locals>.reduce_fnq   sH    t9>c&kQ.($R(a11$ $d5f55r   rD   )r'   rS   s   ` r   _torch_reducerT   p   s    6
 r   c                     | |||fS NrD   )inputr   r   r-   s       r   _reduce_dim_argsrX   y   s    #w%%r   c                       fd}|S )Nc                      t        |       dk(  r"t        |      dk(  r t              | d         S t        | i |\  }}}} t              ||||      S rR   )r   rE   rX   rO   )r   r   rW   r   r   r-   r'   s         r   grad_reducez'_torch_grad_reduce.<locals>.grad_reduce~   sa    t9>c&kQ.($R(a11%5t%Fv%F"sGU$ $UC%@@r   rD   )r'   r[   s   ` r   _torch_grad_reducer\   }   s    A r   )r<   meanaminamaxr,   r+   prodr   normvarstdc                 :    | t         v xs | t        v xs | t        v S rV   )NATIVE_REDUCE_MAPTORCH_REDUCE_MAPTENSOR_REDUCE_MAPr&   s    r   _is_reductionrh      s$    ""Wb,<&<WFW@WWr   c                     | t         v rt        |    |i |S | t        v rt        |    |i |S | t        v rt        |    |i |S t        S rV   )re   rf   rg   rJ   )r'   r   r   s      r   _apply_reductionrj      sb    	 $d5f55	#T4V44	 $d5f55r   rV   rN   )!rH   r   corer   creationr   r   __all__r   r   r   r   r(   rE   rO   rT   rX   r\   REDUCE_NAMESr$   opsatenre   rf   Tensorrg   listkeysNATIVE_REDUCE_FNSTORCH_REDUCE_FNSTENSOR_REDUCE_FNSrh   rj   )names   0r   <module>rx      s?     " 5 /N,%!H:& DP;?GEIINND!=#66  @L7;GE4,T22  GS>BGELL$!3D!99  *//12 (--/0 *//12 X's   +C?4D!D	