U
    9%e$                  	   @   s|  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eee  eedddZee
ee
df f ed	d
dZeeeg ef edddZeeeeeeef dddZd%ee
ee
df f eeeeeedddZeeddddZeeddddZedddZd&eeeedd d!Zd'eeeeed"d#d$ZdS )(    N)AnyCallableListOptionalTupleUnion)Tensor)_broadcast_to_and_flattentree_flattentree_unflatten.)flat_in_dims	flat_argsreturnc                    sF   dd t | |D   r>t fdd D r>td  d d S )Nc                 S   s"   g | ]\}}|d k	r| |qS N)size.0in_dimarg r   T/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/_vmap_internals.py
<listcomp>   s   z0_validate_and_get_batch_size.<locals>.<listcomp>c                 3   s   | ]}| d  kV  qdS )r   Nr   )r   r   Zbatch_sizesr   r   	<genexpr>   s     z/_validate_and_get_batch_size.<locals>.<genexpr>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   r   r   _validate_and_get_batch_size   s    
r   )batched_outputsr   c                 C   s   t | trt| S dS )N   )
isinstancetuplelen)r   r   r   r   _num_outputs   s    
r#   )valuenum_elementserror_message_lambdar   c                 C   s.   t | ts| f| S t| |kr*t| | S r   )r    r!   r"   r   )r$   r%   r&   r   r   r   	_as_tuple&   s
    


r'   )in_dimsargs
vmap_levelfuncr   c                    s  t | ts8t | ts8tdt| d|  dt|  dt|dkrXtdt| dt|\}}t| |}|d krtdt| d|  dt| d  d	| d	t	||D ]\}}t |ts|d k	rtdt| d|  d
| dt |tr*t |t
s*tdt| d|  d
| dt| d	|d k	r|dk sH|| krtdt| d|  d
| d|  d|  dqt||}	 fd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                    s*   g | ]"\}}|d kr|nt || qS r   )torchZ_add_batch_dimr   r*   r   r   r   d   s   z*_create_batched_inputs.<locals>.<listcomp>)r    intr!   r   	_get_nametyper"   r
   r	   r   r   dimr   r   )r(   r)   r*   r+   r   Z	args_specr   r   r   
batch_sizebatched_inputsr   r/   r   _create_batched_inputs2   s@    
($.

r6   F)r   out_dimsr*   r4   r+   allow_none_pass_throughr   c                    s   t | tfdd}t| trB|d }t|  |S |rdt fddt| |D S t fddt| |D S d S )Nc                	      s&   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   )r+   num_outputsr7   r   r   <lambda>x       z!_unwrap_batched.<locals>.<lambda>r   c                 3   s.   | ]&\}}|d k	r"t | |nd V  qd S r   r.   _remove_batch_dimr   outout_dimr4   r*   r   r   r      s   z"_unwrap_batched.<locals>.<genexpr>c                 3   s"   | ]\}}t | |V  qd S r   r=   r?   rB   r   r   r      s   )r#   r'   r    r   r.   r>   r!   r   )r   r7   r*   r4   r+   r8   Zout_dims_as_tuplerA   r   )r4   r+   r:   r7   r*   r   _unwrap_batchedl   s     

	rC   )outputsr+   r   c                 C   s   t | trd S t | ts@tdt| dt| dt|  dt| D ]B\}}t |tr\qHtdt| dt| dt| d| d	qHd S )Nr,   z	, ...): `z%` must only return Tensors, got type z as the return.z for return r-   )r    r   r!   r   r1   r2   	enumerate)rD   r+   idxoutputr   r   r   _validate_outputs   s    

"
(rH   )r7   r+   r   c                 C   sH   t | trd S t | tr*tdd | D sDtdt| d|  dd S )Nc                 s   s   | ]}t |tV  qd S r   )r    r0   )r   rA   r   r   r   r      s    z6_check_out_dims_is_int_or_int_tuple.<locals>.<genexpr>r,   r9   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   )r7   r+   r   r   r   #_check_out_dims_is_int_or_int_tuple   s    
rJ   r+   c                 C   s   t | dr| jS t| S )N__name__)hasattrrL   reprrK   r   r   r   r1      s    
r1   )r+   r(   r7   r   c                 C   s   t jd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(   r7   r   r   r   vmap   s
    rT   )r+   r(   r7   r8   r   c                    s    t  fdd}|S )Nc                     sf   t  tj }z@t| |\}}| } s>t| t||| dW S tj  X d S )N)r8   )rJ   r.   Z_CZ_vmapmode_increment_nestingZ_vmapmode_decrement_nestingr6   rH   rC   )r)   r*   r5   r4   r   r8   r+   r(   r7   r   r   wrapped   s*    

   

	z_vmap.<locals>.wrapped)	functoolswraps)r+   r(   r7   r8   rV   r   rU   r   rS      s    
rS   )F)r   r   )r   r   F)rW   rQ   typingr   r   r   r   r   r   r.   r   Ztorch.utils._pytreer	   r
   r   r0   Z	in_dims_tZ
out_dims_tr   r#   strr'   r6   boolrC   rH   rJ   r1   rT   rS   r   r   r   r   <module>   s^    
  	  
   
@ *   