
    i$                        d dl Z d dlmZ d dl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z  Zeeedf   z  Zdeedz     d	ed
efdZdeeedf   z  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df   z  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)Callable)Any)
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     O/var/www/html/engine/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+   r,   r   	args_specr
   r   r   
batch_sizebatched_inputss              r   _create_batched_inputsr9   8   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_dimsr7   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 )Nr.   , ..., out_dims=z0): `out_dims` must have one dim per output (got z outputs) of r/   )r1   )r,   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'   r4   _remove_batch_dimr   outout_dimr7   r+   s      r   r   z"_unwrap_batched.<locals>.<genexpr>   s?      
 W ? ''ZWM
s   ),c              3   T   K   | ]  \  }}t        j                  ||       ! y wr'   rB   rD   s      r   r   z"_unwrap_batched.<locals>.<genexpr>   s.      
W ##CZI
s   %()r!   r(   r   r   r4   rC   r   r   )	r   r:   r+   r7   r,   r;   out_dims_as_tuplerF   r?   s	    ````   @r   _unwrap_batchedrI   u   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 )Nr.   z	, ...): `z%` must only return Tensors, got type z as the return.z for return r/   )r   r   r   r   r1   r2   	enumerate)rJ   r,   idxoutputs       r   _validate_outputsrO      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   r0   )r   rF   s     r   r   z6_check_out_dims_is_int_or_int_tuple.<locals>.<genexpr>   s      2%,
7C 2s   r.   r>   zu): `out_dims` must be an int or a tuple of int representing where in the outputs the vmapped dimension should appear.)r   r0   r   allr   r1   )r:   r,   s     r   #_check_out_dims_is_int_or_int_tuplerS      s^    (C h&c 2082 / IdO$$4XJ ?/ 0
 	
/r"   c                 H    t        | d      r| j                  S t        |       S )N__name__)hasattrrU   repr)r,   s    r   r1   r1      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)r,   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;   )rS   r4   _C_vmapmode_increment_nestingr9   rO   rI   _vmapmode_decrement_nesting)	r*   r+   r8   r7   r   r;   r,   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)r,   r)   r:   r;   ra   s   ```` r   rZ   rZ      s'     __T3 3* Nr"   )F)r   r   )r   r   F)rb   collections.abcr   typingr   typing_extensionsr   r4   r   torch.utils._pytreer   r   r	   r0   r   	in_dims_t
out_dims_tlistr   r!   strr(   r9   boolrI   rO   rS   r1   FutureWarningr[   rZ   r   r"   r   <module>rn      s    $  (   W W %K	5c?"
sTz" 	"&5+="= # 			 #2s7+	 		9A9A
9A 9A 	9A
 5#:9AF %*#
eFCK00#
#
 #
 	#

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