o
    iz                     @   s\   d Z ddlZddlmZmZmZm	Z
 dd Zdd Zdd	 Z	d
d Zdadadadd ZdS )z
This module contains pre-dispatch wrappers for functorch operations
that enable proper tracing in PT2 non-strict export/compile fx graph.
    N)_add_batch_dim_remove_batch_dim_vmap_decrement_nesting_vmap_increment_nestingc                 C   s<   ddl m} | }|rtjt| f| ||S t| ||}|S )zn
    Thin wrapper around torch._C._add_batch_dim that is used to proxy in
    PT2 export/compile fx graph
    r   +_maybe_find_pre_dispatch_tf_mode_for_export)torch._export.utilsr   torch	overrideshandle_torch_functionr   _add_batch_dim_impl)self	batch_dimlevelr   moderes r   R/home/ubuntu/vllm_env/lib/python3.10/site-packages/torch/_functorch/predispatch.pyr      s   r   c                 C   s@   ddl m} | }|rtjt| f| |||S t| |||}|S )zq
    Thin wrapper around torch._C._remove_batch_dim that is used to proxy in
    PT2 export/compile fx graph
    r   r   )r   r   r	   r
   r   r   _remove_batch_dim_impl)r   r   
batch_sizeout_dimr   r   r   r   r   r   r   )   s   r   c                 C   s8   ddl m} | }|rtjt| f| |S t| |}|S )p
    Thin wrapper around torch._C._vmap_increment_nesting that is used
    to proxy in export/compile graph
    r   r   )r   r   r	   r
   r   r   _vmap_increment_nesting_impl)r   
randomnessr   r   r   r   r   r   r   ;   s   

r   c                  C   s*   ddl m}  |  }|rtjtdS t S )r   r   r   r   )r   r   r	   r
   r   r   _vmap_decrement_nesting_impl)r   r   r   r   r   r   L   s   r   Fc                     sb  ddl m}  |  }|rtjtdS trdS tdu r"ddl}|	 at tr0	 W d   dS ddl
}|jdddkr>nda	 W d   dS tjdd	d
addlm   fd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W d   dS 1 sw   Y  dS )zH
    Lazy loading of vmap decompositions with pre-dispatch support.
    r   r   r   NPYTORCH_JIT1TatenIMPLFuncTorchBatcheddecomposition_tablec                    s*   |  v rt |  |   d S td|  )Nz!could not find decomposition for )VMAP_DECOMPOSITIONS_LIBimplRuntimeError)decompr    r   r   #_register_python_decomposition_vmap   s   zElazy_load_decompositions.<locals>._register_python_decomposition_vmap)r   r   r	   r
   r   lazy_load_decompositionsDECOMPOSITIONS_LOADEDDECOMPOSITIONS_LOCK	threadingLockosenvirongetlibraryLibraryr"   torch._decompr!   opsr   mse_loss_backwarddefaultsmooth_l1_loss_backwardhuber_loss_backwardnll_loss_forwardnll_loss2d_forwardnll_loss_backwardnll_loss2d_backwardaddr)r   r   r*   r,   r&   r   r    r   r'   c   sF   
"r'   )__doc__r	   torch._C._functorchr   r   r   r   r   r   r   r   r(   r)   r"   r'   r   r   r   r   <module>   s   