
    sgI                        d dl Z d dlZd dlZd dlZd dlZd dlm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mZmZ d dlmZmZmZmZmZ eeef   Zeeeedf   f   Zd 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ee	e   e	e   ef   fdZ#d	e	e   d
e	e   dedefdZ$d Z%deeeedf   f   dededededefdZ&d Z'dededdfdZ(defdZ)d a* ejV                         Z,da-d! Z.d" Z/d# Z0d$ Z1d% Z2d& Z3d' Z4d( Z5e jl                  d)        Z7d* Z8d+ Z9d, Z:d- Z;y).    N)partial)AnyCallableListOptionalTupleUnion)Tensor)_add_batch_dim_remove_batch_dim_vmap_decrement_nesting_vmap_increment_nestingis_batchedtensor)_broadcast_to_and_flattentree_flatten	tree_map_tree_unflattenTreeSpec.c                 J     dt        j                          fd       }|S )Nzvtorch.func.{grad, vjp, jacrev, hessian} don't yet support saved tensor hooks. Please open an issue with your use case.c                      t         j                  j                  j                        5   | i |cd d d        S # 1 sw Y   y xY wN)torchautogradgraphdisable_saved_tensors_hooks)argskwargsfmessages     H/var/www/html/venv/lib/python3.12/site-packages/torch/_functorch/vmap.pyfnz.doesnt_support_saved_tensors_hooks.<locals>.fn-   s=    ^^!!==gF 	&d%f%	& 	& 	&s	   =A)	functoolswraps)r   r!   r   s   ` @r    "doesnt_support_saved_tensors_hooksr$   '   s1    	3 
 __Q& & I    flat_in_dims	flat_argsreturnc                     t        | |      D cg c]  \  }}||j                  |       c}}t              dk(  rt        d      r#t	        fdD              rt        d d      d   S c c}}w )Nr   z/vmap: Expected at least one Tensor to vmap overc              3   .   K   | ]  }|d    k7    yw)r   N ).0sizebatch_sizess     r    	<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 dimension)zipr-   len
ValueErrorany)r&   r'   in_dimargr.   s       @r    _validate_and_get_batch_sizer6   6   s    
 |Y7FC 	K
 ;1JKKsJkJJ$$/=0IK
 	
 q>s   A5batched_outputsc                 :    t        | t              rt        |       S y)N   )
isinstancetupler1   )r7   s    r    _num_outputsr<   H   s    /5)?##r%   valuenum_elementserror_message_lambdac                 n    t        | t              s| f|z  S t        |       |k7  rt         |             | S r   )r:   r;   r1   r2   )r=   r>   r?   s      r    	_as_tuplerA   R   s;     eU#x,&&
5z\!-/00Lr%   in_dimsr   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        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	      |x||j                          k  s||j                         k\  rQt        dt	        |       d|  d
| d|j                          d|j                          d|j                          d      ||dk  s	||j                         z  ||<   ! 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 r9   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 -z <= in_dim < )r:   intr;   r2   	_get_nametyper1   r   r   	enumerater0   r
   dimr6   )	rB   r   rC   r'   	args_specr&   ir5   r4   s	            r    _process_batched_inputsrN   \   sa    gs#Jw,FIdO$Jwi 866:7m_AG
 	

 4yA~IdO$ %) *
 	
 (-Iy,Wi@LIdO$Jwi 8%%1'%:1%=$> ?&Kq*
 	
 &c)\&BC 1=C&#&6+=	$(
7) <$X &01 
 fc":c6+B	$(
7) <$X%L9+ ;<  6SWWYJ#6&CGGI:M	$(
7) <$X &%%(WWYK 0GGI;mCGGI;a9  &1*$swwy0LO-12 	%\9=	 r%   
vmap_levelc           	          t        | |      D cg c]  \  }}||nt        |||       }}}t        ||      S c c}}w r   )r0   r   r   )r&   r'   rO   rL   r4   r5   batched_inputss          r    _create_batched_inputsrR      sR     |Y7FC ~>#vz#JJN  .)44	s   :c           
         |9t        |t        j                        rt        |      rt	        d|  d|  d      |S t        |t        j                        st	        d|  d|  dt        |       d      t        ||||      S )NrE   z	, ...): `z5` can not return a BatchedTensor when out_dim is Nonez%` must only return Tensors, got type z3. Did you mean to set out_dims= to None for output?)r:   r   r
   r   r2   rI   r   )namebatched_outputrO   
batch_sizeout_dims        r    _maybe_remove_batch_dimrX      s    nell38H9
 vYtf -5 6   nell3D64& )!!%n!5 6 7@@
 	
 ^ZWMMr%   out_dimsrV   c                    t        |       \  }fd}t        | t        j                        rCt        t              rg}nDt        t
              rt              dk(  r}n#g}n |        nt              }| |        t        |      D 	cg c]  \  }}	t        t              ||||	       }
}}	t        |
      S c c}	}w )Nc                  ^    t        dt                d dt              d    d d	      )NrE   , ..., out_dims=z`)(<inputs>): out_dims is not compatible with the structure of `outputs`. out_dims has structure r9   z but outputs has structure rF   )r2   rH   r   )rC   rY   output_specs   r    incompatible_errorz+_unwrap_batched.<locals>.incompatible_error   sJ    IdO$$4XJ ?&&28&<Q&?%@ A(M,
 	
r%   r9   )r   r:   r   r
   rG   r;   r1   r   r0   rX   rH   r   )r7   rY   rO   rV   rC   flat_batched_outputsr^   flat_out_dimsrU   rW   flat_outputsr]   s    `  `      @r    _unwrap_batchedrb      s     )5_(E%+
 /5<<0 h$%JM%(S]a-?$M%JM 1(KH   (++?'O	 $NG 	 dO^ZW	
L  ,44s   "Cc                 `    t        | t              ry | y t        dt        |       d| d      )NrE   r\   z): `out_dims` must be an int, None or a python collection of ints representing where in the outputs the vmapped dimension should appear.)r:   rG   r2   rH   )xrC   rY   s      r    _check_int_or_nonere      sA    !Sy

	$  0
 ;+ 	, r%   c                 ^    t        | t              ry t        t        t        ||       |        y )N)rC   rY   )r:   rG   r   r   re   )rY   rC   s     r    $_check_out_dims_is_int_or_int_pytreerg      s#    (C g(thGRr%   c                 H    t        | d      r| j                  S t        |       S )N__name__)hasattrri   repr)rC   s    r    rH   rH      s"    tZ }}
 :r%   Fc                  f   t         ry t        5  t         r
	 d d d        y t        j                  j	                  dd      dk(  rnda 	 d d d        y t
        j                  j                  ddd      addl	m
 fd	}  | t
        j                  j                  j                  j                          | t
        j                  j                  j                  j                          | t
        j                  j                  j                   j                          | t
        j                  j                  j"                  j                          | t
        j                  j                  j$                  j                          | t
        j                  j                  j&                  j                          | t
        j                  j                  j(                  j                          | t
        j                  j                  j*                  j                         da d d d        y # 1 sw Y   y xY w)
NPYTORCH_JIT1TatenIMPLFuncTorchBatchedr   )decomposition_tablec                 \    | v rt         j                  | |           y t        d|        )Nz!could not find decomposition for )VMAP_DECOMPOSITIONS_LIBimplRuntimeError)decomprr   s    r    #_register_python_decomposition_vmapzElazy_load_decompositions.<locals>._register_python_decomposition_vmap!  s5    ,,',,V5H5PQ"%Fvh#OPPr%   )DECOMPOSITIONS_LOADEDDECOMPOSITIONS_LOCKosenvirongetr   libraryLibraryrt   torch._decomprr   opsro   mse_loss_backwarddefaultsmooth_l1_loss_backwardhuber_loss_backwardnll_loss_forwardnll_loss2d_forwardnll_loss_backwardnll_loss2d_backwardaddr)rx   rr   s    @r    lazy_load_decompositionsr   	  sw   	 $% $% $% 

}c2c9i$(!$% $% #(--"7"7F.#
 	6	Q 	,EIINN,L,L,T,TU+IINN22::	
 	,EIINN,N,N,V,VW+EIINN,K,K,S,ST+EIINN,M,M,U,UV+EIINN,L,L,T,TU+EIINN,N,N,V,VW+EIINN,?,?,G,GH $I$% $% $%s   H''H'GH''H0c           	          t                t        ||        t        |||       \  }}}	}
|t        |	|||      }t	        | |||
||fi |S t        | |||	|
||fi |S r   )r   rg   rN   _get_chunked_inputs_chunked_vmap
_flat_vmap)rC   rB   rY   
randomness
chunk_sizer   r   rV   r&   r'   rL   chunks_flat_argss               r    	vmap_implr   5  s    (485Lt62Ji .|Z
 
 
 	
 	 	 	r%   c                 V    | |z  x}}|g|z  }| |z  }|dk7  r|j                  |       |S )Nr   )append)total_elemsr   n_chunkschunk_sizes	remainders        r    get_chunk_sizesr   W  sB    %33Hx,)Kj(IA~9%r%   c                     |f|*t        ||      }t        t        j                  |            t        fdt	        | |      D              }t	        | }|S )Nc              3   l   K   | ]+  \  }}||j                  |      n|gt              z   - y wN)rK   )tensor_splitr1   )r,   tr4   
split_idxss      r    r/   z&_get_chunked_inputs.<locals>.<genexpr>g  sQ       Av  	
zv. 
 j/	s   14)r   r;   	itertools
accumulater0   )r'   r&   rV   r   r   flat_args_chunksr   r   s          @r    r   r   a  sf    J%j*=9//<=
  Y5  ,-r%   c                     g }d }| D ]&  }t        |      \  }}|j                  |       |%|}( t        t        |       }||fS r   )r   r   listr0   )chunks_output_flat_chunks_outputarg_specoutputflat_output	arg_specsflat_output_chunkss          r    _flatten_chunks_outputr   w  sd     H  !!-f!5Y!!+. H	! c#567x''r%   c                     t        | |      }t        |      t        |      k(  sJ g }t        |      D ]3  \  }}|j                  t	        j
                  ||   |             d ||<   5 |S r   )r   r1   rJ   r   r   cat)rY   r   r   r`   r   idxrW   s          r    _concat_chunked_outputsr     sv    -hAM}%7!8888K!-0 'W599%7%<'JK"&3'
 r%   c                 .   g }|dk(  rt        j                         nd }|D ]L  }	t        ||	      }
|
dk(  r|t        j                  |       |j	                  t        | |
||	|||fi |       N t        |      \  }}~t        |||      }t        ||      S )Nsamer   )	r   get_rng_stater6   set_rng_stater   r   r   r   r   )rC   r&   r   rL   rY   r   r   chunks_outputrsr'   rV   r   r   r   s                 r    r   r     s     M","6			DB% 
	1,	J
 ?>#	 		
%
> $:-#H  	 *(H>PQK +x00r%   c                 (    | dvrt        d|        y )N)error	differentr   zLOnly allowed values for randomness are 'error', 'different', or 'same'. Got )rv   )r   s    r    _check_randomness_argr     s)    77Z[eZfg
 	
 8r%   c              #   `   K   	 t        | |      }| t                y # t                w xY wwr   )r   r   )rV   r   rO   s      r    vmap_increment_nestingr     s*     ",ZD
!!s   . .+.c                     t        ||      5 }t        ||||      }	 | |	i |}
t        |
||||       cd d d        S # 1 sw Y   y xY wr   )r   rR   rb   )rC   rV   r&   r'   rL   rY   r   r   rO   rQ   r7   s              r    r   r     s^     
 
J	7 X:/)Z
 9&9*jRVWX X Xs	   %<Ac                       fd}|S )Nc                      t              5 }t        | |      } |i |}t        ||      cd d d        S # 1 sw Y   y xY wr   )r   wrap_batchedunwrap_batched)	r   r   rO   rQ   r7   rV   rC   rB   r   s	        r    innerzrestore_vmap.<locals>.inner  sK    #J
; 	?z)$DN"N=f=O!/:>	? 	? 	?s	   !9Ar+   )rC   rB   rV   r   r   s   ```` r    restore_vmapr     s    ? Lr%   c                 ^    t        |       \  }}t        ||      }|J t        ||||      }|S r   )r   r   rR   )r   bdimslevelr'   spec
flat_bdimsresults          r    r   r     s=    "4(OIt*5$7J!!!#J	5$GFMr%   c                 <   t        |       \  }}t        |      dk(  r| dfS |D cg c]J  }t        |t        j                        r*t        j
                  j                  j                  ||      n|d fL }}t        | \  }}t        ||      t        ||      fS c c}w )Nr   r+   )
r   r1   r:   r   r
   _C
_functorchrb   r0   r   )r   r   r'   r   r5   r   r   r   s           r    r   r   	  s    "4(OIt
9~Rx
 	  c5<<( 	++C74[	F  LMFE&$'t)DDDs   AB)<
contextlibr"   r   r{   	threadingr   typingr   r   r   r   r   r	   r   r
   torch._C._functorchr   r   r   r   r   torch.utils._pytreer   r   r   r   r   rG   	in_dims_t
out_dims_tr$   r6   r<   strrA   rN   rR   rX   rb   re   rg   rH   ry   Lockrz   rt   r   r   r   r   r   r   r   r   contextmanagerr   r   r   r   r   r+   r%   r    <module>r      sG      	   > >     #u*	3c3h'(
x}%26$%fck0B(B"C  !9A"c'9J
77#7+37
3S	49h./7|5s)5(,S	5?B5
5N.'565#556'5'5 '5 	'5
 '5 '5T	S: SX SRV SH   $inn&  
)%XD,("
.1d
 " "X>Er%   