o
    zi
                     @   s   d dl mZ d dlZd dlmZ d dlZd dlmZm	Z	m
Z
mZmZ d dlmZ deddfd	d
Zded ddfddZdeddfddZdddeddfddZdS )    )UnionN)OptimizedModule)DDPStrategyDeepSpeedStrategyFSDPStrategySingleDeviceStrategyStrategy)_check_mixed_importsmodelreturnpl.LightningModulec                 C   s   t | tstdt| j d| j}t |tjs(t|  tdt|j dd| j	|j
|j|j|j|jd|_| 	|j
|_
| 	|j|_| 	|j|_| 	|j|_| 	|j|_|S )aZ  Returns an instance LightningModule from the output of ``torch.compile``.

    .. warning::  This is an :ref:`experimental <versioning:Experimental API>` feature.

    The ``torch.compile`` function returns a ``torch._dynamo.OptimizedModule``, which wraps the LightningModule
    passed in as an argument, but doesn't inherit from it. This means that the output of ``torch.compile`` behaves
    like a LightningModule, but it doesn't inherit from it (i.e. `isinstance` will fail).

    Use this method to obtain a LightningModule that still runs with all the optimizations from ``torch.compile``.

    z8`model` is required to be a `OptimizedModule`. Found a `z
` instead.z?`model` is expected to be a compiled LightningModule. Found a `z	` insteaddynamo)compiler
dynamo_ctxoriginal_forwardoriginal_training_steporiginal_validation_steporiginal_test_steporiginal_predict_step)
isinstancer   
ValueErrortype__name__	_orig_modplLightningModuler	   r   forwardtraining_stepvalidation_step	test_steppredict_step_compiler_ctx)r
   orig_module r#   W/home/ubuntu/.local/lib/python3.10/site-packages/pytorch_lightning/utilities/compile.pyfrom_compiled   s,   

r%   )r   ztorch._dynamo.OptimizedModulec                 C   s   t | tr| j}t |tjstdt| j nt | tjr+| jdu r(t	d| }nt	d|j}|durR|d |_
|d |_|d |_|d |_|d	 |_d|_|S )
a  Returns an instance of LightningModule without any compilation optimizations from a compiled model.

    .. warning::  This is an :ref:`experimental <versioning:Experimental API>` feature.

    This takes either a ``torch._dynamo.OptimizedModule`` returned by ``torch.compile()`` or a ``LightningModule``
    returned by ``from_compiled``.

    Note: this method will in-place modify the ``LightningModule`` that is passed in.

    zGUnexpected error, the wrapped model should be a LightningModule, found Nzc`model` is required to be a compiled LightningModule. Found a non-compiled LightningModule instead.zH`model` must either be an instance of OptimizedModule or LightningModuler   r   r   r   r   )r   r   r   r   r   	TypeErrorr   r   r!   r   r   r   r   r   r    )r
   originalctxr#   r#   r$   to_uncompiledA   s.   






r)   c                 C   s@   t | tr	t| S t | tjr| S t|  tdt| j d)NzM`model` must be a `LightningModule` or `torch._dynamo.OptimizedModule`, got ``)	r   r   r%   r   r   r	   r&   r   __qualname__)r
   r#   r#   r$   _maybe_unwrap_optimizedi   s   
r,   strategyc                 C   s`   | j d ur,tttf}t||rt|tr.ddd |D }tdt|j	 d| dd S d S )Nz, c                 s   s    | ]}|j V  qd S )N)r   ).0sr#   r#   r$   	<genexpr>x   s    z4_verify_strategy_supports_compile.<locals>.<genexpr>zCUsing a compiled model is incompatible with the current strategy: `z`. Only zj support compilation. Either switch to one of the supported strategies or avoid passing in compiled model.)
r!   r   r   r   r   r   joinRuntimeErrorr   r   )r
   r-   supported_strategiessupported_strategy_namesr#   r#   r$   !_verify_strategy_supports_compilet   s   

r5   )typingr   torchtorch._dynamor   pytorch_lightningr   pytorch_lightning.strategiesr   r   r   r   r   )pytorch_lightning.utilities.model_helpersr	   r%   r)   objectr,   r5   r#   r#   r#   r$   <module>   s   )(