
      i
                         d dl mZ d dlZd dlmZ d dlm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	Zded
   ddfdZdeddfdZdddeddfdZy)    )UnionN)OptimizedModule)DDPStrategyDeepSpeedStrategyFSDPStrategySingleDeviceStrategyStrategy)_check_mixed_importsmodelreturnpl.LightningModulec                    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                  |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_modules     x/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/lightning/pytorch/utilities/compile.pyfrom_compiledr&      sD    e_-STXY^T_ThThSiistuu//Kk2#5#56U#MdS^N_NhNhMiirs
 	

 &&'//"-";";$/$?$?)33!,!9!9!K  **;+>+>?K % 0 01J1J KK"'"2"2;3N3N"OK!,,[-B-BCK$//0H0HIK    )r   ztorch._dynamo.OptimizedModulec                    t        | t              rG| j                  }t        |t        j                        s`t        dt        |       j                         t        | t        j                        r| j                  t        d      | }nt        d      |j                  }|9|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ctxs      r%   to_uncompiledr,   A   s     %)??(B$6$67YZ^_dZeZnZnYop  
E2--	.&u   cdd

 
 C
12!$%=!>#&'A#B  !56 #$; <!%Or'   c                     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   s    r%   _maybe_unwrap_optimizedr0   i   s\    %)U##%++,

WX\]bXcXpXpWqqrs r'   strategyc                     | j                   kt        t        t        f}t	        ||      rt	        |t
              r=dj                  d |D              }t        dt        |      j                   d| d      y y )Nz, c              3   4   K   | ]  }|j                     y w)N)r   ).0ss     r%   	<genexpr>z4_verify_strategy_supports_compile.<locals>.<genexpr>x   s     0ZEYEYs   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   r1   supported_strategiessupported_strategy_namess       r%   !_verify_strategy_supports_compiler;   t   s    & 4k<P($89ZRc=d'+yy0ZEY0Z'Z$UVZ[cVdVmVmUn o12 3BB  >e 'r'   )typingr   torchtorch._dynamor   lightning.pytorchpytorchr   lightning.pytorch.strategiesr   r   r   r   r	   )lightning.pytorch.utilities.model_helpersr
   r&   r,   objectr0   r;    r'   r%   <module>rE      s|      )  u u J& &-A &R%TU %Zn %P6 .B 	-A 	X 	Z^ 	r'   