
    {Yh$                     "   d dl Z d dl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dS )&    N)AnyCallableOptionalUnion)
deprecated)Tensor)_broadcast_to_and_flattentree_flattentree_unflatten.flat_in_dims	flat_argsreturnc                     d t          | |          D             r.t          fdD                       rt          d d          d         S )Nc                 B    g | ]\  }}||                     |          S N)size).0in_dimargs      g/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torch/_vmap_internals.py
<listcomp>z0_validate_and_get_batch_size.<locals>.<listcomp>   s8       FC 	    c              3   0   K   | ]}|d          k    V  dS )r   N )r   r   batch_sizess     r   	<genexpr>z/_validate_and_get_batch_size.<locals>.<genexpr>   s,      JJd4;q>1JJJJJJr   zTvmap: Expected all tensors to have the same size in the mapped dimension, got sizes z for the mapped dimensionr   )zipany
ValueError)r   r   r   s     @r   _validate_and_get_batch_sizer       s     |Y77  K
  
sJJJJkJJJJJ 
K$/K K K
 
 	
 q>r   batched_outputsc                 N    t          | t                    rt          |           S dS )N   )
isinstancetuplelen)r!   s    r   _num_outputsr'   !   s'    /5)) $?###1r   valuenum_elementserror_message_lambdac                     t          | t                    s| f|z  S t          |           |k    rt           |                      | S r   )r$   r%   r&   r   )r(   r)   r*   s      r   	_as_tupler,   )   sQ    
 eU## 'x,&&
5zz\!!--//000Lr   in_dimsargs
vmap_levelfuncc                    t          | t                    sHt          | t                    s3t          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                    rKt          |t                    s6t          dt	          |           d|  d
| dt          |           d	          |n|dk     s||                                k    rPt          dt	          |           d|  d
| d|                                 d|                                 d          t          ||          }	fdt          ||          D             }
t          |
|          |	fS )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 < c                 J    g | ]\  }}||nt          j        ||           S r   )torch_add_batch_dim)r   r   r   r/   s      r   r   z*_create_batched_inputs.<locals>.<listcomp>l   sD       FC ~5#7VZ#P#P  r   )r$   intr%   r   	_get_nametyper&   r
   r	   r   r   dimr    r   )r-   r.   r/   r0   r   	args_specr   r   r   
batch_sizebatched_inputss     `        r   _create_batched_inputsr>   7   s4    gs## 
Jw,F,F 
GIdOO G Gw G G6:7mmG G G
 
 	

 4yyA~~*IdOO * * *
 
 	
 (--Iy,Wi@@L*IdOO * *w * *%1'%:%:1%=* * '* * *
 
 	
 9l33  V&#&& 	6+=1	$ 1 17 1 1$1 1 1  
 fc"" 	:c6+B+B 	<	$ < <7 < <$< <99< < <   6A::377991D1D.	$ . .7 . .$. .%(WWYY. . "%. . .   .lIFFJ   |Y77  N .)44j@@r   Fout_dimsr<   allow_none_pass_throughc                 j   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            
      V    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   )r8   )r0   num_outputsr?   s   r   <lambda>z!_unwrap_batched.<locals>.<lambda>   sW     U	$ U U U U(3U UBKD//U U U r   r   c              3   T   K   | ]"\  }}|t          j        ||          nd V  #d S r   r5   _remove_batch_dimr   outout_dimr<   r/   s      r   r   z"_unwrap_batched.<locals>.<genexpr>   s[       
 
 W ? 'ZWMMM	
 
 
 
 
 
r   c              3   L   K   | ]\  }}t          j        ||          V  d S r   rG   rI   s      r   r   z"_unwrap_batched.<locals>.<genexpr>   sL       
 
W #CZII
 
 
 
 
 
r   )r'   r,   r$   r   r5   rH   r%   r   )	r!   r?   r/   r<   r0   r@   out_dims_as_tuplerK   rD   s	    ````   @r   _unwrap_batchedrN   t   s#    //K!	U 	U 	U 	U 	U 	U  /6** Y#A&&
JPWXXX 
 
 
 
 
 
 !$O5F G G
 
 
 
 
 	
  
 
 
 
 
 #O5F G G
 
 
 
 
 	
r   outputsc                    t          | t                    rd S t          | t                    s@t          dt	          |           dt	          |           dt          |            d          t          |           D ]]\  }}t          |t                    rt          dt	          |           dt	          |           dt          |           d| d	          d S )Nr2   z	, ...): `z%` must only return Tensors, got type z as the return.z for return r3   )r$   r   r%   r   r8   r9   	enumerate)rO   r0   idxoutputs       r   _validate_outputsrT      s,   '6"" gu%% 
@IdOO @ @ioo @ @!%g@ @ @
 
 	
 !)) 
 
Vff%% 	BIdOO B Bioo B B!%fB B;>B B B
 
 	

 
r   c                     t          | t                    rd S t          | t                    rt          d | D                       s#t	          dt          |           d|  d          d S )Nc              3   @   K   | ]}t          |t                    V  d S r   )r$   r7   )r   rK   s     r   r   z6_check_out_dims_is_int_or_int_tuple.<locals>.<genexpr>   s=       2 2%,
7C  2 2 2 2 2 2r   r2   rC   zu): `out_dims` must be an int or a tuple of int representing where in the outputs the vmapped dimension should appear.)r$   r7   r%   allr   r8   )r?   r0   s     r   #_check_out_dims_is_int_or_int_tuplerX      s    (C   h&& 
c 2 2082 2 2 / / 
 0IdOO 0 0X 0 0 0
 
 	

 
r   c                 N    t          | d          r| j        S t          |           S )N__name__)hasattrrZ   repr)r0   s    r   r8   r8      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                 R     t          j                    fd            }|S )Nc                  f   t                     t          j                                        }	 t	          | |          \  }} | }st          |           t          |||          t          j                                         S # t          j                                         w xY w)N)r@   )rX   r5   _C_vmapmode_increment_nestingr>   rT   rN   _vmapmode_decrement_nesting)	r.   r/   r=   r<   r!   r@   r0   r-   r?   s	        r   wrappedz_vmap.<locals>.wrapped   s    +Hd;;;X99;;
	3)?z4* *&NJ #dN3O* 9!/4888"(?   H002222EH002222s   A B  B0)	functoolswraps)r0   r-   r?   r@   rf   s   ```` r   r_   r_      sL     _T3 3 3 3 3 3 3 3* Nr   )F)r   r   )r   r   F) rg   typingr   r   r   r   typing_extensionsr   r5   r   torch.utils._pytreer	   r
   r   r7   r%   	in_dims_t
out_dims_tlistr    r'   strr,   r>   boolrN   rT   rX   r8   FutureWarningr`   r_   r   r   r   <module>rr      sB       1 1 1 1 1 1 1 1 1 1 1 1 ( ( ( ( ( (        W W W W W W W W W W #u*	3c3h'(
x}% 	   "%fck0B(B"C     			 #2s7+	 		 	 	 	9A9A
9A 9A 	9A
 5#:9A 9A 9A 9AF %*#
 #
65#556#
#
 #
 	#

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