
    Pg$                        d dl Z d dlmZmZmZmZmZmZ d dlm	Z	 d dl
Z
d dl
mZ d dlmZmZmZ eeef   Zeeeedf   f   Zdeee      ded	efd
Zdeeeedf   f   d	efdZdededeg ef   d	efdZdedededed	eeef   f
dZ	 d"deeeedf   f   dededededed	efdZdeded	dfdZdeded	dfdZdefdZ e	de      d#dededed	efd        Z	 	 	 d$dedededed	ef
d!Zy)%    N)AnyCallableListOptionalTupleUnion)
deprecated)Tensor)_broadcast_to_and_flattentree_flattentree_unflatten.flat_in_dims	flat_argsreturnc                     t        | |      D cg c]  \  }}||j                  |       c}}r#t        fdD              rt        d d      d   S c c}}w )Nc              3   .   K   | ]  }|d    k7    yw)r   N ).0sizebatch_sizess     \/var/www/html/suriana-translation/venv/lib/python3.12/site-packages/torch/_vmap_internals.py	<genexpr>z/_validate_and_get_batch_size.<locals>.<genexpr>   s     Jd4;q>1Js   zTvmap: Expected all tensors to have the same size in the mapped dimension, got sizes z for the mapped dimensionr   )zipr   any
ValueError)r   r   in_dimargr   s       @r   _validate_and_get_batch_sizer      sz     |Y7FC 	K
 sJkJJ$$/=0IK
 	
 q>s   Abatched_outputsc                 :    t        | t              rt        |       S y)N   )
isinstancetuplelen)r   s    r   _num_outputsr%   !   s    /5)?##    valuenum_elementserror_message_lambdac                 n    t        | t              s| f|z  S t        |       |k7  rt         |             | S N)r"   r#   r$   r   )r'   r(   r)   s      r   	_as_tupler,   )   s;    
 eU#x,&&
5z\!-/00Lr&   in_dimsargs
vmap_levelfuncc                    t        | t              s7t        | t              s't        dt	        |       d|  dt        |        d      t        |      dk(  rt        dt	        |       d      t        |      \  }}t        | |      }|-t        dt	        |       d|  dt        |       d    d	| d	      t        ||      D ]  \  }}t        |t              s |t        dt	        |       d|  d
| d      t        |t              r:t        |t              s*t        dt	        |       d|  d
| dt        |       d	      ||dk  s||j                         k\  st        dt	        |       d|  d
| d|j                          d|j                          d       t        ||      }	t        ||      D cg c]   \  }}||nt        j                  |||      " }
}}t        |
|      |	fS c c}}w )Nvmap(z
, in_dims=zv, ...)(<inputs>): expected `in_dims` to be int or a (potentially nested) tuple matching the structure of inputs, got: .r   z)(<inputs>): got no inputs. Maybe you forgot to add inputs, or you are trying to vmap over a function with no inputs. The latter is unsupported.zb, ...)(<inputs>): in_dims is not compatible with the structure of `inputs`. in_dims has structure r!   z but inputs has structure z, ...)(<inputs>): Got in_dim=zE for an input but in_dim must be either an integer dimension or None.z' for an input but the input is of type zT. We cannot vmap over non-Tensor arguments, please use None as the respective in_dimz> for some input, but that input is a Tensor of dimensionality z- so expected in_dim to satisfy 0 <= in_dim < )r"   intr#   r   	_get_nametyper$   r   r   r   r
   dimr   torch_add_batch_dimr   )r-   r.   r/   r0   r   	args_specr   r   r   
batch_sizebatched_inputss              r   _create_batched_inputsr=   7   sj    gs#Jw,FIdO$Jwi 866:7m_AG
 	

 4yA~IdO$ %) *
 	
 (-Iy,Wi@LIdO$Jwi 8%%1'%:1%=$> ?&Kq*
 	
 9l3 V&#&6+=	$(
7) <$X &01 
 fc":c6+B	$(
7) <$X%L9+ ;<  6A:37791D	$(
7) <$X &%%(WWYK 0!!$1. , .lIFJ |Y7FC ~5#7#7VZ#PPN  .)4j@@	s   ;%G1out_dimsr;   allow_none_pass_throughc                     t        |       t        fd      }t        | t              r|d   }t	        j
                  | |      S |rt        fdt        | |      D              S t        fdt        | |      D              S )Nc            
      F    dt                d d dt                d	S )Nr2   , ..., out_dims=z0): `out_dims` must have one dim per output (got z outputs) of r3   )r5   )r0   num_outputsr>   s   r   <lambda>z!_unwrap_batched.<locals>.<lambda>   s6    %	$((8
 C((3}M)D/ARRSU r&   r   c              3   \   K   | ]#  \  }}|t        j                  ||      nd  % y wr+   r8   _remove_batch_dimr   outout_dimr;   r/   s      r   r   z"_unwrap_batched.<locals>.<genexpr>   s?      
 W ? ''ZWM
s   ),c              3   T   K   | ]  \  }}t        j                  ||       ! y wr+   rF   rH   s      r   r   z"_unwrap_batched.<locals>.<genexpr>   s.      
W ##CZI
s   %()r%   r,   r"   r
   r8   rG   r#   r   )	r   r>   r/   r;   r0   r?   out_dims_as_tuplerJ   rC   s	    ````   @r   _unwrap_batchedrM   t   s     /K!	U /6*#A&&&
JPWXX 
 !$O5F G
 
 	
  
 #O5F G
 
 	
r&   outputsc                 R   t        | t              ry t        | t              s0t        dt	        |       dt	        |       dt        |        d      t        |       D ]H  \  }}t        |t              rt        dt	        |       dt	        |       dt        |       d| d	       y )Nr2   z	, ...): `z%` must only return Tensors, got type z as the return.z for return r3   )r"   r
   r#   r   r5   r6   	enumerate)rN   r0   idxoutputs       r   _validate_outputsrS      s    '6"gu%IdO$Iio-> ?!!%g@
 	
 !) 
Vff%IdO$Iio-> ?!!%fl3%qB
 	

r&   c                     t        | t              ry t        | t              rt        d | D              st	        dt        |       d|  d      y )Nc              3   <   K   | ]  }t        |t                y wr+   )r"   r4   )r   rJ   s     r   r   z6_check_out_dims_is_int_or_int_tuple.<locals>.<genexpr>   s      2%,
7C 2s   r2   rB   zu): `out_dims` must be an int or a tuple of int representing where in the outputs the vmapped dimension should appear.)r"   r4   r#   allr   r5   )r>   r0   s     r   #_check_out_dims_is_int_or_int_tuplerW      s^    (C h&c 2082 / IdO$$4XJ ?/ 0
 	
/r&   c                 H    t        | d      r| j                  S t        |       S )N__name__)hasattrrY   repr)r0   s    r   r5   r5      s"    tZ }}
 :r&   z@Please use `torch.vmap` instead of `torch._vmap_internals.vmap`.)categoryc                     t        | ||      S )z4
    Please use torch.vmap instead of this API.
    )_vmap)r0   r-   r>   s      r   vmapr_      s     w))r&   c                 N     t        j                          fd       }|S )Nc                  F   t               t        j                  j                         }	 t	        | |      \  }} | }st        |       t        |||      t        j                  j                          S # t        j                  j                          w xY w)N)r?   )rW   r8   _C_vmapmode_increment_nestingr=   rS   rM   _vmapmode_decrement_nesting)	r.   r/   r<   r;   r   r?   r0   r-   r>   s	        r   wrappedz_vmap.<locals>.wrapped   s    +Hd;XX99;
	3)?z4*&NJ #N3O*!/48"(? HH002EHH002s   4B    B )	functoolswraps)r0   r-   r>   r?   re   s   ```` r   r^   r^      s'     __T3 3* Nr&   )F)r   r   )r   r   F) rf   typingr   r   r   r   r   r   typing_extensionsr	   r8   r
   torch.utils._pytreer   r   r   r4   	in_dims_t
out_dims_tr   r%   strr,   r=   boolrM   rS   rW   r5   FutureWarningr_   r^   r   r&   r   <module>rp      s!    > > (   W W #u*	3c3h'(
x}% 	"%fck0B(B"C  			 #2s7+	 		9A9A
9A 9A 	9A
 5#:9AF %*#
65#556#
#
 #
 	#

 #
 "#
 #
T
s 
( 
t 
"

* 

H 

QU 

H  F*x *) *: *h *	* $)	 
     "	 
  r&   