
    Χg                        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d eD             Zd eD             Zd eD             Z ee                                          Z ee                                          Z ee                                          Zd Zd ZdS )    N   )is_masked_tensor)as_masked_tensormasked_tensorc                     ||                                  S |                     | d                                           S )NT)allmasked_fill)datamasks     `/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/masked/maskedtensor/reductions.py_masked_all_allr      s9    |xxzzTE4((,,...    Fc                     |t          j        | ||          S t          j        |                     | d          ||          S )NdimkeepdimT)torchr   r	   )r
   r   r   r   s       r   _masked_all_dimr      sG    |y388889T%%teT22WMMMMr   c                      t          |           dk    r0t          |          dk    rt          | d         |d                   S t          | i |S )Nr   r   r   r   )lenr   r   )argskwargss     r   _masked_allr      sO    
4yyA~~#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   !   sa    #s 3TC5'222C&&& 7 7y1g666Kr   c                 P    | 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                 8   t          	          }|                                 }| j        r&|                                                                 n|                                 }	dk    r |||          }n	dv r:|                                 r% ||                                |                              t          j                  }|                                 s&|	                                
                                n|
                                }|                    d          |         }|                                                                t          j        |                                |j                                      d          z  }t          j        ||z            }n@| j        r, |t%          |                                |                    }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   s   "2&&	}}+/>Nt}}%%'''t}} ;;#)Dt444KK'''D,>,>,@,@'"4;;==t<<<??ei?PPJ ))++$""$$,,...\\^^ 
 ..##J/CYY[[&&((5<		DK, , ,gajjF  )C&L11KK ^ 	5#)M$++--$F$FGGKK $)Dt444KUYt__===r    )r'   rC   s   ` r   _torch_reduce_allrE   /   s%    > > > > >@ r   c                      d fd	}|S )NFc                    | j         r!d	 d}t          j        |           t          S t	          |           st          d          t          	          }|                                 }|                                 }	dk    r |||||          }n" || ||||                                           }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    > 		"@ @ @ @  M#!!%% 	JHIII"2&&	}}}};;#)Dc7NNNKK#)#we$--//  K  ]4g-N-NOOOr   FNrD   )r'   rM   s   ` r   _torch_reduce_dimrO   S   s/    P P P P P P2 r   c                       fd}|S )Nc                      t          |           dk    r1t          |          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   s]    t99>>c&kkQ..($R((a111$ $$d5f555r   rD   )r'   rS   s   ` r   _torch_reducerT   p   s$    6 6 6 6 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    r1t          |          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~   sy    t99>>c&kkQ..($R((a111%5t%Fv%F%F"sGU$ $$UC%@@@r   rD   )r'   r[   s   ` r   _torch_grad_reducer\   }   s)    A A A A A r   )r<   meanaminamaxr,   r+   prodr   normvarstdc                 h    i | ]/}t          t          j        j        |          t	          |          0S rD   )r$   r   opsatenrT   .0names     r   
<dictcomp>rj      s=       ;?GEIND!!=#6#6  r   c                 T    i | ]%}t          t          |          t          |          &S rD   )r$   r   r\   rg   s     r   rj   rj      s:       7;GE4,T22  r   c                 ^    i | ]*}t          t          j        |          t          |          +S rD   )r$   r   Tensorr\   rg   s     r   rj   rj      s<       >BGEL$!3D!9!9  r   c                 8    | t           v p| t          v p| t          v S rV   )NATIVE_REDUCE_MAPTORCH_REDUCE_MAPTENSOR_REDUCE_MAPr&   s    r   _is_reductionrr      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   )ro   rp   rq   rJ   )r'   r   r   s      r   _apply_reductionrt      st    	 $d5f555	#T4V444	 $d5f555r   rV   rN   )rH   r   corer   creationr   r   __all__r   r   r   r   r(   rE   rO   rT   rX   r\   REDUCE_NAMESro   rp   rq   listkeysNATIVE_REDUCE_FNSTORCH_REDUCE_FNSTENSOR_REDUCE_FNSrr   rt   rD   r   r   <module>r~      s     " " " " " " 5 5 5 5 5 5 5 5 / / / /N N N N, , ,  % % %! ! !H  :  & & & &     CO    ?K    FR    D*//1122 4(--//00 D*//1122 X X X    r   