
    '}h$                        d dl Z d dlZd dlmZmZmZmZmZ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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)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     T/var/www/html/test/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_inputsr<   2   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 )Nr1   , ..., out_dims=z0): `out_dims` must have one dim per output (got z outputs) of r2   )r4   )r/   num_outputsr=   s   r   <lambda>z!_unwrap_batched.<locals>.<lambda>x   s6    %	$((8
 C((3}M)D/ARRSU r%   r   c              3   \   K   | ]#  \  }}|t        j                  ||      nd  % y wr*   r7   _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*   rE   rG   s      r   r   z"_unwrap_batched.<locals>.<genexpr>   s.      
W ##CZI
s   %()r$   r+   r!   r	   r7   rF   r"   r   )	r   r=   r.   r:   r/   r>   out_dims_as_tuplerI   rB   s	    ````   @r   _unwrap_batchedrL   l   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 )Nr1   z	, ...): `z%` must only return Tensors, got type z as the return.z for return r2   )r!   r	   r"   r   r4   r5   	enumerate)rM   r/   idxoutputs       r   _validate_outputsrR      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!   r3   )r   rI   s     r   r   z6_check_out_dims_is_int_or_int_tuple.<locals>.<genexpr>   s      2%,
7C 2s   r1   rA   zu): `out_dims` must be an int or a tuple of int representing where in the outputs the vmapped dimension should appear.)r!   r3   r"   allr   r4   )r=   r/   s     r   #_check_out_dims_is_int_or_int_tuplerV      s^    (C h&c 2082 / IdO$$4XJ ?/ 0
 	
/r%   c                 H    t        | d      r| j                  S t        |       S )N__name__)hasattrrX   repr)r/   s    r   r4   r4      s"    tZ }}
 :r%   c                 J    t        j                  dd       t        | ||      S )z4
    Please use torch.vmap instead of this API.
    z=Please use torch.vmap instead of torch._vmap_internals.vmap.    )
stacklevel)warningswarn_vmap)r/   r,   r=   s      r   vmapra      s'     MMG 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>   )rV   r7   _C_vmapmode_increment_nestingr<   rR   rL   _vmapmode_decrement_nesting)	r-   r.   r;   r:   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>   rg   s   ```` r   r`   r`      s'     __T3 3* Nr%   )F)r   r   )r   r   F)rh   r^   typingr   r   r   r   r   r   r7   r	   torch.utils._pytreer
   r   r   r3   	in_dims_t
out_dims_tr   r$   strr+   r<   boolrL   rR   rV   r4   ra   r`   r   r%   r   <module>rp      s     > >   W W#u*	3c3h'(
x}%26 %fck0B(B"C  !9A"c'9J
6A6A#6A146A<D6A
5#:6A@ %*#
65#556#
#
 #
 	#

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