
    Vh                        d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZ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 d dlmZ d dlmZmZmZmZ d dlmZ d dlZd dlZd dlmc mZ d dlm Z  d dl!m"Z" d d	l#m$Z$ d d
l%m&Z& d dl'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2 d dlm3Z4 d dl5m6Z6 d dl7m8Z8 d dl9m:Z:m;Z; ddl<m=Z= ddlm>Z>  ej~                  e@      ZAej                   G d d             ZCed        ZD	 d&dddddddedeEe   deeFeGef      deeFeGef      deeFeGef      deHdeHd eHd!eeIeG   eGf   fd"ZJd#eGd$eGd!efd%ZKy)'    N)OrderedDict)contextmanager)	lru_cache)AnyCallableOptionalUnion)patch)enable_python_dispatcher)compile_context)log_export_usage)reorder_kwargs)ArgumentSpecConstantArgumentExportGraphSignature	InputKind	InputSpec
OutputKind
OutputSpecSymIntArgumentSymBoolArgumentSymFloatArgumentTensorArgument)	traceback)compatibility)make_fx)_PyTreeCodeGen_PyTreeInfo   )_wrap_submodules)_materialize_cpp_cia_opsc                        e Zd ZU dZdZeed<   y)ExportDynamoConfigz:
    Manage Export-specific configurations of Dynamo.
    T	allow_rnnN)__name__
__module____qualname____doc__r$   bool__annotations__     F/home/dcms/DCMS/lib/python3.12/site-packages/torch/_export/__init__.pyr#   r#   5   s     Itr,   r#   c                      ddl m}  t        j                  d       t        j                  d       t        j                  d       t        j                  d       y )Nr   configz+============================+z|     !!!   WARNING   !!!    |ztorch._export.aot_compile()/torch._export.aot_load() is being deprecated, please switch to directly calling torch._inductor.aoti_compile_and_package(torch.export.export())/torch._inductor.aoti_load_package() instead.)torch._inductorr0   logwarningr/   s    r-   aot_compile_warningr4   ?   s=    &KK01KK01KK01KK	78r,   FT)dynamic_shapesoptionsremove_runtime_assertionsdisable_constraint_solversame_signaturefargskwargsr5   r6   r7   r8   r9   returnc          	         ddl m} ddlm}	 ddlm}
 t                |
j                  r=t        j                  j                  j                  | |||d      j                         }n || |||||d      }t        j                         5  t        j                  j                  ||||	      }d
d
d
       |S # 1 sw Y   S xY w)av  
    Note: this function is not stable yet

    Traces either an nn.Module's forward function or just a callable with PyTorch
    operations inside, generates executable cpp code from the program, and returns
    the path to the generated shared library

    Args:
        f: the `nn.Module` or callable to trace.

        args: example positional inputs.

        kwargs: optional example keyword inputs.

        dynamic_shapes: Should either be:
            1) a dict from argument names of ``f`` to their dynamic shape specifications,
            2) a tuple that specifies dynamic shape specifications for each input in original order.
            If you are specifying dynamism on keyword args, you will need to pass them in the order that
            is defined in the original function signature.

            The dynamic shape of a tensor argument can be specified as either
            (1) a dict from dynamic dimension indices to :func:`Dim` types, where it is
            not required to include static dimension indices in this dict, but when they are,
            they should be mapped to None; or (2) a tuple / list of :func:`Dim` types or None,
            where the :func:`Dim` types correspond to dynamic dimensions, and static dimensions
            are denoted by None. Arguments that are dicts or tuples / lists of tensors are
            recursively specified by using mappings or sequences of contained specifications.

        options: A dictionary of options to control inductor

        disable_constraint_solver: Whether the dim constraint solver must be disabled.

    Returns:
        Path to the generated shared library
    r   )_export_to_torch_ir)select_decomp_tabler/   T)pre_dispatchF)r8   r9   restore_fqn)r6   N)torch.export._tracer?   torch._inductor.decompositionr@   r1   r0   r4   is_predispatchtorchexport_trace_exportmoduleno_grad	_inductoraot_compile)r:   r;   r<   r5   r6   r7   r8   r9   r?   r@   r0   gmso_paths                r-   rM   rM   L   s    \ 8A&\\  ((D&.W[(\cce !&?) 

 
 Q//--b$-PQ NQ Ns   	$B77CrO   devicec                    t                |dk(  r+t        j                  j                  j	                  | d      n|dk(  s|j                  d      r,t        j                  j                  j                  | d|      nP|dk(  s|j                  d      r,t        j                  j                  j                  | d|      nt        d|z         fd}|S )	z
    Loads a shared library generated by aot_compile and returns a callable

    Args:
        so_path: Path to the shared library

    Returns:
        A callable
    cpur   cudazcuda:xpuzxpu:zUnsupported device c                  x   j                         }t        j                  |d         }t        j                  |d         }t        j                  | t	        ||      f      d   }|D cg c]  }t        |t        j                        s|! }}j                  |      }t        j                  ||      S c c}w )Nr   r   )
get_call_specpytreetreespec_loadstree_flattenr   
isinstancerF   Tensorruntree_unflatten)	r;   r<   	call_specin_specout_specflat_inputsxflat_outputsrunners	           r-   	optimizedzaot_load.<locals>.optimized   s    ((*	''	!5((16))41P*QRSTU"-MQAu||1LqMMzz+.$$\8<< Ns   *B7
B7)	r4   rF   _C_aotiAOTIModelContainerRunnerCpu
startswithAOTIModelContainerRunnerCudaAOTIModelContainerRunnerXpuRuntimeError)rO   rP   re   rd   s      @r-   aot_loadrm      s     ;;GQG	6	V..w7<<WaP	5F--f5;;GQO 069::= r,   )N)Lcopydataclasses	functoolsiojsonloggingosresystypeswarningsweakrefzipfilecollectionsr   
contextlibr   r   typingr   r   r   r	   unittest.mockr
   rF   torch.fxtorch.utils._pytreeutils_pytreerW   torch._dispatch.pythonr   torch._guardsr   torch._utils_internalr   torch.export._tree_utilsr   torch.export.graph_signaturer   r   r   r   r   r   r   r   r   r   r   r   fx_tracebacktorch.fx._compatibilityr   "torch.fx.experimental.proxy_tensorr   torch.fx.graphr   r   wrappersr    r!   	getLoggerr%   r2   	dataclassr#   r4   tupledictstrr)   listrM   rm   r+   r,   r-   <module>r      s      	   	 	 
     # %  1 1    $ $ ; ) 2 3    / 1 6 6 & +g!   	8 	8 (,H
 04(,&+&+HH
*H T#s(^$H
 T#s(^,H d38n%H  $H  $H H 49c>HTc 3 8 r,   