
    Vhi,                    f   d dl mZ d dlZd dlZd dlZd dlmZmZmZm	Z	m
Z
 d dlZd dlZe	rd dlmZ d dlmZ d dlmZ g dZ ej*                  e      Z	 d	 	 	 	 	 ddZ	 	 dddd		 	 	 	 	 	 	 dd
Z	 dddddd	 	 	 	 	 	 	 	 	 	 	 	 	 ddZdddZ	 ddd	 	 	 	 	 	 	 	 	 ddZ	 d	 	 	 	 	 ddZddZd Zy)    )annotationsN)AnyIOOptionalTYPE_CHECKINGUnion)	InputTypeExportedProgram)FileLike)compilelist_mode_optionslist_optionscudagraph_mark_step_beginc                $    ddl m }  || ||      S )au  
    Compile a given FX graph with TorchInductor.  This allows compiling
    FX graphs captured without using TorchDynamo.

    Args:
        gm: The FX graph to compile.
        example_inputs:  List of tensor inputs.
        options:  Optional dict of config options.  See `torch._inductor.config`.

    Returns:
        Callable with same behavior as gm but faster.
       
compile_fxconfig_patchesr   )gmexample_inputsoptionsr   s       H/home/dcms/DCMS/lib/python3.12/site-packages/torch/_inductor/__init__.pyr   r      s    " 'b.AA    package_pathinductor_configsc               6   ddl m} ddlm} t	        | |      st        d      | j                  t        d      ||t        j                  d       |t	        |t        j                  t        f      r |j                         r|j                         sNt	        |t        t         j"                  f      r$t!        j$                  |      j'                  d      s
J d	|        |xs i }d
|d<   |j)                  d      rt        d       |t*        | ||      S )a;  
    Compiles the exported program with AOTInductor, and packages it into a .pt2
    artifact specified by the input package_path. To load the package, you can
    call ``torch._inductor.aoti_load_package(package_path)``.

    An example usage is as follows:

    .. code-block:: python

        ep = torch.export.export(M(), ...)
        aoti_file = torch._inductor.aoti_compile_and_package(
            ep, package_path="my_package.pt2"
        )
        compiled_model = torch._inductor.aoti_load_package("my_package.pt2")

    To compile and save multiple models into a single ``.pt2`` artifact, you can do
    the following:

    .. code-block:: python

        ep1 = torch.export.export(M1(), ...)
        aoti_file1 = torch._inductor.aot_compile(
            ep1, ..., options={"aot_inductor.package": True}
        )
        ep2 = torch.export.export(M2(), ...)
        aoti_file2 = torch._inductor.aot_compile(
            ep2, ..., options={"aot_inductor.package": True}
        )

        from torch._inductor.package import package_aoti, load_package

        package_aoti("my_package.pt2", {"model1": aoti_file1, "model2": aoti_file2})

        compiled_model1 = load_package("my_package.pt2", "model1")
        compiled_model2 = load_package("my_package.pt2", "model2")

    Args:
        exported_program: An exported program created through a call from torch.export
        package_path: Optional specified path to the generated .pt2 artifact.
        inductor_configs: Optional dictionary of configs to control inductor.

    Returns:
        Path to the generated artifact
    r   r
   r   )aot_inductor_minifier_wrapperz!Only ExportedProgram is supportedz[exported_program.example_inputs is required to be set in order for AOTInductor compilation.zYou no longer need to specify args/kwargs to aoti_compile_and_package as we can get this information from exported_program.example_inputs..pt2zVExpect package path to be a file ending in .pt2, is None, or is a buffer. Instead got Tzaot_inductor.packagezaot_inductor.output_pathzoPlease pass in a package path to aot_inductor_compile() instead of setting the aot_inductor.output_path config.r   )torch.exportr   debugr    
isinstance
ValueErrorr   RuntimeErrorlogwarningioIOBaser   writableseekablestrosPathLikefspathendswithget_aoti_compile_and_package_inner)exported_program_deprecated_unused_args_deprecated_unused_kwargsr   r   r   r    s          r   aoti_compile_and_packager7   3   s1   h -4&8<==&&.+
 	

 *.G.SS	
 	|bii_5%%'%%' |c2;;%78		,'008 aam`no		
 (-2/3+,67>
 	
 )'!)	 r   F)load_and_runcheck_accuracyr   r   c                  |r|t        |      dk(  sJ d       ddlm} t        | t        j
                  j                        sJ |xs i }t        | |||      }t        |t              sJ ||D 	cg c]I  }	t        j                  j                  |	      d   dk(  r"t        j                  j                  |	      d   K }
}	t        |
      dk(  rT|D 	cg c]I  }	t        j                  j                  |	      d   dk(  r"t        j                  j                  |	      d   K }
}	|
d   dz   } |||      }||k(  sJ |s|r:t        |      }|r%dd	lm}m} |d
k(  } || ||d||      s |d      |S  ||i | |S c c}	w c c}	w )a:  
    See docstring for aoti_compile_and_package.

    If `load_and_run` is True, this function will load the compiled model and run it.
    This is for the minifier to check the correctness of the compiled model.

    If `check_accuracy` is set, this function will check the accuracy of the compiled
    model against gm. kwargs must be None if check_accuracy is set.
    "strict_accuracy" means "we will minify any time we see anything that
     diverges", whereas "accuracy" is more conservative, and will only minify if there
     is a meaningful fp64 divergence
    r   zRwhen checking for accuracy, the inputs must have been flattened and kwargs is Noner   )package_aotir   z.soz.cppr!   )AccuracyErrorsame_two_modelsaccuracyT)only_fwdrequire_fp64ignore_non_fpzBad accuracy detected)lenpackager;   r$   torchfxGraphModuleaot_compilelistr.   pathsplitextaoti_load_packagetorch._dynamo.debug_utilsr=   r>   )r   argskwargsr8   r9   r   r   r;   
aoti_filesfilerJ   rescompiled_modelr=   r>   not_strict_accuracys                   r   r3   r3      s   0 ~V!1 	
`	
1 &b%((..///\rFRv7GHJj$''' #
ww%a(E1 GGT"1%
 

 t9> '77##D)!,6   &q)D 
 Aw'
|Z
0C,~*<8P #1J">"01 $$;<<  D+F+M
s   3AE=AFc                "    ddl m}  || |      S )a  
    Loads the model from the PT2 package.

    If multiple models were packaged into the PT2, this will load the default
    model. To load a specific model, you can directly call the load API

    .. code-block:: python

        from torch._inductor.package import load_package

        compiled_model1 = load_package("my_package.pt2", "model1")
        compiled_model2 = load_package("my_package.pt2", "model2")

    Args:
        path: Path to the .pt2 package
        run_single_threaded (bool): Whether the model should be run without
            thread synchronization logic. This is useful to avoid conflicts with
            CUDAGraphs.
    r   )load_package)run_single_threaded)torch._inductor.packagerV   )rJ   rW   rV   s      r   rL   rL      s    ( 52EFFr   r<   c               F    ddl m}m}  || |||      \  }} || ||      S )a  
    Ahead-of-time compile a given FX graph with TorchInductor into a shared library.

    Args:
        gm: The FX graph to compile.
        args:  Example arguments
        kwargs: Example keyword arguments
        options:  Optional dict of config options.  See `torch._inductor.config`.

    Returns:
        Path to the generated shared library, or a list of files generated by
        AOTI if aot_inductor.package=True.
        TODO: make it return a list by default
    r   )_aoti_flatten_inputscompile_fx_aotr<   r   )r   rZ   r[   )r   rN   rO   r   rZ   r[   flat_example_inputss          r   rH   rH     s9    * A#7
D&'$  
 r   c           
         i ddidddddddd}	 | r||    S |S # t         $ r4}t        d|  ddj                  |j                                      |d	}~ww xY w)
a  Returns a dictionary describing the optimizations that each of the available
    modes passed to `torch.compile()` performs.

    Args:
        mode (str, optional): The mode to return the optimizations for.
        If None, returns optimizations for all modes
        dynamic (bool, optional): Whether dynamic shape is enabled.

    Example::
        >>> torch._inductor.list_mode_options()
    triton.cudagraphsT)max_autotunecoordinate_descent_tuning)r_   r^   r`   )defaultzreduce-overheadzmax-autotune-no-cudagraphszmax-autotunezUnrecognized mode=z, should be one of: z, N)KeyErrorr&   joinkeys)modedynamicmode_optionses       r   r   r   &  s        

 !)-'
 !!%)-
0L&%)|D!;|;  &:499\EVEVEX;Y:Z[
	s     	A/AAc                 `    ddl m}  | j                         }t        |j	                               S )zReturns a dictionary describing the optimizations and debug configurations
    that are available to `torch.compile()`.

    The options are documented in `torch._inductor.config`.

    Example::

        >>> torch._inductor.list_options()
    r   )config)torch._inductorrj   get_config_copyrI   rd   )rj   current_configs     r   r   r   P  s)     '%+%;%;%=N##%&&r   c                     ddl m}   |         y)zJIndicates that a new iteration of inference or training is about to begin.r   mark_step_beginN)cudagraph_treesrp   ro   s    r   r   r   b  s    0r   )N)r   torch.fx.GraphModuler   zlist[InputType]r   Optional[dict[str, Any]])NN)r4   r   r   zOptional[FileLike]r   rs   returnr-   )r   ztorch.nn.ModulerN   
tuple[Any]rO   rs   r8   boolr9   Optional[str]r   z Optional[Union[str, io.BytesIO]]r   rs   )F)rJ   r   rW   rv   rt   r   )
r   rr   rN   ru   rO   rs   r   rs   rt   zUnion[str, list[str]])re   rw   rf   zOptional[bool]rt   zdict[str, Any])rt   z	list[str]) 
__future__r   r)   loggingr.   typingr   r   r   r   r   torch._inductor.configrE   torch.fxtorch._inductor.utilsr	   r"   r   torch.typesr   __all__	getLogger__name__r'   r   r7   r3   rL   rH   r   r   r    r   r   <module>r      s   " 	  	 : :   /,$ g! )-BB#B &B0 !"e
 (,15e%e
 %e /e 	eX (,	M $(5915MM M %	M M "M 3M /M`G8 (,
 )-
 %
 & F ;?'
')7''T'$r   