o
    8wi                     @   s4  d dl Z d dlZd dlmZmZmZ d dlmZmZ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 d dl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 d dlm Z m!Z!m"Z"m#Z#m$Z$ d dl%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/m0Z0 d dl1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7 d dl8m9Z9m:Z: d dl;m<Z<m=Z= d dl>m?Z? d dl@mAZAmBZB d dlCmDZDmEZEmFZFmGZG d dlHmIZI d dlJmKZKmLZL d dlMmNZN d dlOmPZPmQZQ d dlRmSZS d dlTmUZU d dlVmWZW d dlXmYZY d dlZm[Z[m\Z\m]Z]m^Z^m_Z_m`Z` ded dfd!d"ZaG d#d$ d$ZbdS )%    N)	GeneratorMappingSequence)AbstractContextManagercontextmanagernullcontext)partial)Path)AnyCallableOptionalUnioncastoverload)apply_to_collection)is_overridden)Tensor)	Optimizer)BatchSampler
DataLoaderDistributedSamplerRandomSamplerSequentialSampler)Accelerator)_PLUGIN_INPUT_PRECISION_INPUT
_Connector_is_using_cli)Logger)	Precision)DataParallelStrategyDeepSpeedStrategyFSDPStrategySingleDeviceStrategyStrategyXLAStrategy)_MultiProcessingLauncher_XLALauncher)
TBroadcast_Sharded)move_data_to_device)convert_tensors_to_scalarsconvert_to_tensors)_auto_add_worker_init_fn_replace_dunder_methods_update_dataloaderhas_iterable_dataset)_update_properties)DistributedSamplerWrapper_InfiniteBarrier&_has_meta_device_parameters_or_buffers)rank_zero_deprecationrank_zero_warn)_load_external_callbacksseed_everything)ReduceOp)PossibleUserWarning)_FabricDataLoader_FabricModule_FabricOptimizer_to_compiled_unwrap_compiled_unwrap_objects_returnc                  G      d S N )rC   rG   rG   T/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/lightning_fabric/fabric.py_do_nothingN      rI   c                   @   s  e Zd ZdZddddddddddeeef deeef deee	 ee	f d	e	d
e
e de
eeee f  de
eee ef  de
eeee f  ddfddZedefddZedefddZedejfddZede	fddZede	fddZede	fddZede	fddZedefdd Zedee fd!d"Zedefd#d$Zd%ed&edefd'd(Zd)d)d*d+ejd,e d-ed.edef
d/d0Z!	)dd+ejd-ed.ede"fd1d2Z#d,e dee$e%e$d3f f fd4d5Z&d)d)d6d7e'd8ed-edee'ee' f fd9d:Z(	)dd;e'd8ed-ede'fd<d=Z)dd>d?e*d%ed@e
e" d&eddf
dAdBZ+			C	)dd+eejje"f dDee e$f dEe
ee,e	f  dFe
ee,e	f  dGee,e	f dHede
ej* fdIdJZ-de.fdKdLZ/e0dMejdejfdNdOZ1e0dMe*de*fdPdOZ1e0dMedefdQdOZ1dMeeje*ef deeje*ef fdRdOZ1d%ed&eddfdSdTZ2ddUe
e ddfdVdWZ3ddMe4dYe	de4fdZd[Z5	\dd]ee*e6ee%f d^e
e d_edee*e6ee%f fd`daZ7		bdd]ee*e6ee%f d^e
e dce
ee8ef  dee*e6ee%f fdddeZ9e:ddfede;fdgdhZ<dd+e"diede.fdjdkZ=de.fdldmZ>de.fdndoZ?ddpe
e de.fdqdrZ@	ddseeeAf dte6eeeje ef f due
e6eeBeegef f  ddfdvdwZC		)ddseeeAf dte
e6eeeje ef f  dxede6eef fdydzZDddseeeAf dMeeje f dxeddfd{d|ZEeFfd}eBd gef d%ed&edefd~dZGded%ed&eddfddZHddUedede
e	 ddfddZIddeJeef de
e	 ddfddZKeLdde
e	 de
e dede	fddZMdeBd%ed&edefddZNdeBd%ed&edefddZOd@ejd,ee  dejfddZPd;e'defddZQeLd;e'd&edeRfddZSdddZTdddZUd+ejd,eVe  ddfddZWd+ejddfddZXd,eVe  ddfddZYd7eVe' ddfddZZeLde
eee ef  dee fddZ[dS )Fabrica:  Fabric accelerates your PyTorch training or inference code with minimal changes required.

    - Automatic placement of models and data onto the device.
    - Automatic support for mixed and double precision (smaller memory footprint).
    - Seamless switching between hardware (CPU, GPU, TPU) and distributed training strategies
      (data-parallel training, sharded training, etc.).
    - Automated spawning of processes, no launch utilities required.
    - Multi-node support.

    Args:
        accelerator: The hardware to run on. Possible choices are:
            ``"cpu"``, ``"cuda"``, ``"mps"``, ``"gpu"``, ``"tpu"``, ``"auto"``.
        strategy: Strategy for how to run across multiple devices. Possible choices are:
            ``"dp"``, ``"ddp"``, ``"ddp_spawn"``, ``"deepspeed"``, ``"fsdp"``.
        devices: Number of devices to train on (``int``), which GPUs to train on (``list`` or ``str``), or ``"auto"``.
            The value applies per node.
        num_nodes: Number of GPU nodes for distributed training.
        precision: Double precision (``"64"``), full precision (``"32"``), half precision AMP (``"16-mixed"``),
            or bfloat16 precision AMP (``"bf16-mixed"``).
        plugins: One or several custom plugins
        callbacks: A single callback or a list of callbacks. A callback can contain any arbitrary methods that
            can be invoked through :meth:`~lightning_fabric.fabric.Fabric.call` by the user.
        loggers: A single logger or a list of loggers. See :meth:`~lightning_fabric.fabric.Fabric.log` for more
            information.

    auto   N)acceleratorstrategydevices	num_nodes	precisionplugins	callbacksloggersrN   rO   rP   rQ   rR   rS   rT   rU   rD   c          	      C   s   t ||||||d| _| jj| _| jj| _| jj| _| || _	|d ur&|ng }t
|tr/|n|g| _d| _d| _|   t rJ| j  d| _d S d S )N)rN   rO   rP   rQ   rR   rS   r   FT)r   
_connectorrO   	_strategyrN   _acceleratorrR   
_precision_configure_callbacks
_callbacks
isinstancelist_loggers_models_setup	_launched_prepare_run_methodr   setup_environment)	selfrN   rO   rP   rQ   rR   rS   rT   rU   rG   rG   rH   __init__n   s*   




zFabric.__init__c                 C      | j S rF   )rX   rc   rG   rG   rH   rN         zFabric.acceleratorc                 C   re   rF   )rW   rf   rG   rG   rH   rO      rg   zFabric.strategyc                 C      | j jS )zxThe current device this process runs on.

        Use this to create tensors directly on the device if needed.

        )rW   root_devicerf   rG   rG   rH   device   s   zFabric.devicec                 C      t | jddS )zEThe global index of the current process across all devices and nodes.global_rankr   getattrrW   rf   rG   rG   rH   rl         zFabric.global_rankc                 C   rk   )zOThe index of the current process among the processes running on the local node.
local_rankr   rm   rf   rG   rG   rH   rp      ro   zFabric.local_rankc                 C   rk   )zThe index of the current node.	node_rankr   rm   rf   rG   rG   rH   rq      ro   zFabric.node_rankc                 C   rk   )zCThe total number of processes running across all devices and nodes.
world_sizerM   rm   rf   rG   rG   rH   rr      ro   zFabric.world_sizec                 C   rh   )zWhether this rank is rank zero.)rW   is_global_zerorf   rG   rG   rH   rs      s   zFabric.is_global_zeroc                 C   re   )z%Returns all loggers passed to Fabric.r^   rf   rG   rG   rH   rU      s   zFabric.loggersc                 C   s
   | j d S )z[Returns the first logger in the list passed to Fabric, which is considered the main logger.r   rt   rf   rG   rG   rH   logger      
zFabric.loggerargskwargsc                 O   s   dS )zAll the code inside this run method gets accelerated by Fabric.

        You can pass arbitrary arguments to this function when overriding it.

        NrG   rc   rw   rx   rG   rG   rH   run   s    z
Fabric.runT)move_to_device_reapply_compilemodule
optimizersr{   r|   c                   s,    || |rt|n|df\}}|} j|}|r% j|t|d}|r3 j|t|\}}n j|}|durBt	||}t
| j|d}t||rQ jn
t| tdjd  fdd|D }  jd7  _t|d	r |_||_| jvr j|  jd
 |d |r|g|R S |S )a  Set up a model and its optimizers for accelerated training.

        Args:
            module: A :class:`torch.nn.Module` to set up
            *optimizers: The optimizer(s) to set up (no optimizers is also possible)
            move_to_device: If set ``True`` (default), moves the model to the correct device. Set this to ``False``
                and alternatively use :meth:`to_device` manually.
            _reapply_compile: If ``True`` (default), and the model was ``torch.compile``d before, the
                corresponding :class:`~torch._dynamo.OptimizedModule` wrapper will be removed and reapplied with the
                same settings after the model was set up by the strategy (e.g., after the model was wrapped by DDP,
                FSDP etc.). Set it to ``False`` if compiling DDP/FSDP is causing issues.

        Returns:
            The tuple containing wrapped module and the optimizers, in the same order they were passed in.

        Nmodelr~   original_moduler   rj   c                    s   g | ]
}t | j jqS rG   r?   rW   r[   .0	optimizerrf   rG   rH   
<listcomp>   s    z Fabric.setup.<locals>.<listcomp>rM   _fabricon_after_setup)fabricr}   )_validate_setuprA   rY   convert_module_move_model_to_devicer]   rW   setup_module_and_optimizerssetup_moduler@   r>   r1   rj   next
parameterstorchtensorr_   hasattrr   _fabric_optimizersr[   appendcall)rc   r}   r{   r|   r~   compile_kwargsr   rG   rf   rH   setup   s8   

 

zFabric.setupc                 C   s   |  | |rt|n|df\}}|}| j|}|r"| j|g d}| j|}|dur1t||}t|| j|d}t	||r@| j
n
t| tdj
d t|dr`| |_|| jvr`| j| |  jd7  _|S )a'  Set up a model for accelerated training or inference.

        This is the same as calling ``.setup(model)`` with no optimizers. It is useful for inference or for certain
        strategies like `FSDP` that require setting up the module before the optimizer can be created and set up.
        See also :meth:`setup_optimizers`.

        Args:
            module: A :class:`torch.nn.Module` to set up
            move_to_device: If set ``True`` (default), moves the model to the correct device. Set this to ``False``
                and alternatively use :meth:`to_device` manually.
            _reapply_compile: If ``True`` (default), and the model was ``torch.compile``d before, the
                corresponding :class:`~torch._dynamo.OptimizedModule` wrapper will be removed and reapplied with the
                same settings after the model was set up by the strategy (e.g., after the model was wrapped by DDP,
                FSDP etc.). Set it to ``False`` if compiling DDP/FSDP is causing issues.
        Returns:
            The wrapped model.

        Nr   r   r   r   r   rM   )_validate_setup_modulerA   rY   r   r   rW   r   r@   r>   r1   rj   r   r   r   r   r   r   r[   r   r_   )rc   r}   r{   r|   r   r   rG   rG   rH   r     s&   

 

zFabric.setup_module.c                    sJ     |  fdd|D } fdd|D }t|dkr!|d S t|S )a{  Set up one or more optimizers for accelerated training.

        Some strategies do not allow setting up model and optimizer independently. For them, you should call
        ``.setup(model, optimizer, ...)`` instead to jointly set them up.

        Args:
            *optimizers: One or more optimizers to set up.

        Returns:
            The wrapped optimizer(s).

        c                    s   g | ]} j |qS rG   )rW   setup_optimizerr   rf   rG   rH   r   Q      z+Fabric.setup_optimizers.<locals>.<listcomp>c                    s   g | ]}t | j jd qS ))r   rO   rT   r   r   rf   rG   rH   r   R  s    rM   r   )_validate_setup_optimizerslentuplerc   r~   rG   rf   rH   setup_optimizersC  s   

zFabric.setup_optimizersuse_distributed_samplerr{   dataloadersr   c                   s8    |  fdd|D }t|dkr|d S |S )an  Set up one or multiple dataloaders for accelerated training. If you need different settings for each
        dataloader, call this method individually for each one.

        Args:
            *dataloaders: A single dataloader or a sequence of dataloaders.
            use_distributed_sampler: If set ``True`` (default), automatically wraps or replaces the sampler on the
                dataloader(s) for distributed training. If you have a custom sampler defined, set this argument
                to ``False``.
            move_to_device: If set ``True`` (default), moves the data returned by the dataloader(s) automatically to
                the correct device. Set this to ``False`` and alternatively use :meth:`to_device` manually on the
                returned data.

        Returns:
            The wrapped dataloaders, in the same order they were passed in.

        c                    s   g | ]
}j | d qS )r   )_setup_dataloader)r   
dataloaderr{   rc   r   rG   rH   r   l  s    z,Fabric.setup_dataloaders.<locals>.<listcomp>rM   r   )_validate_setup_dataloadersr   )rc   r   r{   r   rG   r   rH   setup_dataloadersX  s
   
zFabric.setup_dataloadersr   c                 C   sz   |r|  |r| j|fi | jj}t||}t|| j | j|}|r.t| jt	s.| j
nd}t||d}tt|}|S )a  Set up a single dataloader for accelerated training.

        Args:
            dataloader: The dataloader to accelerate.
            use_distributed_sampler: If set ``True`` (default), automatically wraps or replaces the sampler on the
                dataloader for distributed training. If you have a custom sampler defined, set this argument to
                ``False``.
            move_to_device: If set ``True`` (default), moves the data returned by the dataloader automatically to
                the correct device. Set this to ``False`` and alternatively use :meth:`to_device` manually on the
                returned data.

        Returns:
            The wrapped dataloader.

        N)r   rj   )_requires_distributed_sampler_get_distributed_samplerrW   distributed_sampler_kwargsr/   r-   rl   process_dataloaderr\   r%   rj   r=   r   r   )rc   r   r   r{   samplerrj   fabric_dataloaderrG   rG   rH   r   t  s   

zFabric._setup_dataloader)r   r   r   c                O   s   |dur|j n|}t|\}}t| jtr4|du r0| jdkr"td| jdkr+td| jj}n|| j_	dt
j_z| jj||g|R i | W dt
j_dS dt
j_w )a  Replaces ``loss.backward()`` in your training loop. Handles precision and automatically for you.

        Args:
            tensor: The tensor (loss) to back-propagate gradients from.
            *args: Optional positional arguments passed to the underlying backward function.
            model: Optional model instance for plugins that require the model for backward().
            **kwargs: Optional named keyword arguments passed to the underlying backward function.

        Note:
            When using ``strategy="deepspeed"`` and multiple models were set up, it is required to pass in the
            model as argument here.

        Nr   zLNo models were set up for backward. Did you forget to call `fabric.setup()`?rM   zWhen using multiple models + deepspeed, please provide the model used to perform the optimization: `self.backward(loss, model=model)`TF)_forward_modulerA   r\   rW   r!   r_   RuntimeError
ValueErrorr   _deepspeed_enginelightning_fabricwrappers_in_fabric_backwardbackward)rc   r   r   rw   rx   r}   rC   rG   rG   rH   r     s    


zFabric.backward       @r   clip_valmax_norm	norm_typeerror_if_nonfinitec                 C   sl   |dur|durt d|dur| jjt|t||d dS |dur2| jjt|t||||dS t d)ay  Clip the gradients of the model to a given max value or max norm.

        Args:
            module: The module whose parameters should be clipped.
            optimizer: The optimizer referencing the parameters to be clipped.
            clip_val: If passed, gradients will be clipped to this value.
            max_norm: If passed, clips the gradients in such a way that the p-norm of the resulting parameters is
                no larger than the given value.
            norm_type: The type of norm if `max_norm` was passed. Can be ``'inf'`` for infinity norm.
                Default is the 2-norm.
            error_if_nonfinite: An error is raised if the total norm of the gradients is NaN or infinite.

        Return:
            The total norm of the gradients (before clipping was applied) as a scalar tensor if ``max_norm`` was
            passed, otherwise ``None``.

        NzdOnly one of `clip_val` or `max_norm` can be set as this specifies the underlying clipping algorithm!)r   )r   r   r   zLYou have to specify either `clip_val` or `max_norm` to do gradient clipping!)r   rO   clip_gradients_valuerB   clip_gradients_norm)rc   r}   r   r   r   r   r   rG   rG   rH   clip_gradients  s    zFabric.clip_gradientsc                 C   
   | j  S )zA context manager to automatically convert operations for the chosen precision.

        Use this only if the `forward` method of your model does not cover all operations you wish to run with the
        chosen precision setting.

        )rY   forward_contextrf   rG   rG   rH   autocast  s   
zFabric.autocastobjc                 C   rE   rF   rG   rc   r   rG   rG   rH   	to_device  rJ   zFabric.to_devicec                 C   rE   rF   rG   r   rG   rG   rH   r     rJ   c                 C   rE   rF   rG   r   rG   rG   rH   r     rJ   c                 C   s8   t |tjr| j| j | j| |S t|| jdS )a  Move a :class:`torch.nn.Module` or a collection of tensors to the current device, if it is not already on
        that device.

        Args:
            obj: An object to move to the device. Can be an instance of :class:`torch.nn.Module`, a tensor, or a
                 (nested) collection of tensors (e.g., a dictionary).

        Returns:
            A reference to the object that was moved to the new device.

        r   )	r\   nnModulerX   setup_devicerj   rW   module_to_devicer*   r   rG   rG   rH   r     s
   c                 O   s    | j dkrt|i | dS dS )zPrint something only on the first process. If running on multiple machines, it will print from the first
        process in each machine.

        Arguments passed to this method are forwarded to the Python built-in :func:`print` function.

        r   N)rp   printry   rG   rG   rH   r   	  s   
zFabric.printnamec                 C   s   |    | jj|d dS )aP  Wait for all processes to enter this call.

        Use this to synchronize all parallel processes, but only if necessary, otherwise the overhead of synchronization
        will cause your program to slow down. This method needs to be called on all processes. Failing to do so will
        cause your program to stall forever.

        )r   N)_validate_launchedrW   barrier)rc   r   rG   rG   rH   r     s   zFabric.barrierr   srcc                 C   s   |    | jj||dS )a%  Send a tensor from one process to all others.

        This method needs to be called on all processes. Failing to do so will cause your program to stall forever.

        Args:
            obj: The object to broadcast to all other members. Any serializable object is supported, but it is
                most efficient with the object being a :class:`~torch.Tensor`.
            src: The (global) rank of the process that should send the data to all others.

        Return:
            The transferred data, the same value on every rank.

        )r   )r   rW   	broadcast)rc   r   r   rG   rG   rH   r     s   zFabric.broadcastFdatagroup
sync_gradsc                 C   B   |    |dur
|ntjjj}t|| jd}t|t| j	j
||dS )aZ  Gather tensors or collections of tensors from multiple processes.

        This method needs to be called on all processes and the tensors need to have the same shape across all
        processes, otherwise your program will stall forever.

        Args:
            data: int, float, tensor of shape (batch, ...), or a (possibly nested) collection thereof.
            group: the process group to gather results from. Defaults to all processes (world).
            sync_grads: flag that allows users to synchronize gradients for the ``all_gather`` operation

        Return:
            A tensor of shape (world_size, batch, ...), or if the input was a collection
            the output will also be a collection with tensors of this shape. For the special case where
            world_size is 1, no additional dimension is added to the tensor(s).

        Nr   )r   r   )r   r   distributedr   WORLDr,   rj   r   r   rW   
all_gather)rc   r   r   r   rG   rG   rH   r   /  s   zFabric.all_gathermean	reduce_opc                 C   r   )a  Reduce tensors or collections of tensors from multiple processes.

        The reduction on tensors is applied in-place, meaning the result will be placed back into the input tensor.
        This method needs to be called on all processes and the tensors need to have the same shape across all
        processes, otherwise your program will stall forever.

        Args:
            data: int, float, tensor of shape (batch, ...), or a (possibly nested) collection thereof. Tensor will be
                modified in-place.
            group: the process group to reduce results across. Defaults to all processes (world).
            reduce_op: the reduction operation. Defaults to 'mean'. Can also be a string 'sum' or ReduceOp.
                Some strategies may limit the choices here.

        Return:
            A tensor of the same shape as the input with values reduced pointwise across processes. The same is
            applied to tensors in a collection if a collection is given as input.

        Nr   )r   r   )r   r   r   r   r   r,   rj   r   r   rW   
all_reduce)rc   r   r   r   rG   rG   rH   r   G  s   zFabric.all_reducelocalc                 c   sp    |r| j n| j}t "}|dkr|  dV  |dkr&|  W d   dS W d   dS 1 s1w   Y  dS )a  The code block under this context manager gets executed first on the main process (rank 0) and only when
        completed, the other processes get to run the code in parallel.

        Args:
            local: Set this to ``True`` if the **local** rank should be the one going first. Useful if you are
                downloading data and the filesystem isn't shared between the nodes.

        Example::

            with fabric.rank_zero_first():
                dataset = MNIST("datasets/", download=True)

        r   N)rp   rl   r3   )rc   r   rankr   rG   rG   rH   rank_zero_firstd  s   "zFabric.rank_zero_firstenabledc                 C   s~   t |\}}t|tstdt| jttfrt S | jjdu r0t	d| jj
j dtd t S t |j\}}| jj||S )a  Skip gradient synchronization during backward to avoid redundant communication overhead.

        Use this context manager when performing gradient accumulation to speed up training with multiple devices.

        Example::

            # Accumulate gradient 8 batches at a time
            with fabric.no_backward_sync(model, enabled=(batch_idx % 8 != 0)):
                output = model(input)
                loss = ...
                fabric.backward(loss)
                ...

        For those strategies that don't support it, a warning is emitted. For single-device strategies, it is a no-op.
        Both the model's ``.forward()`` and the ``fabric.backward()`` call need to run under this context.

        Args:
            module: The module for which to control the gradient synchronization.
            enabled: Whether the context manager is enabled or not. ``True`` means skip the sync, ``False`` means do not
                skip.

        zvYou need to set up the model first before you can call `fabric.no_backward_sync()`: `model = fabric.setup(model, ...)`NThe `z` does not support skipping the gradient synchronization. Remove `.no_backward_sync()` from your code or choose a different strategy.category)rA   r\   r>   	TypeErrorrW   r#   r%   r   _backward_sync_controlr7   	__class____name__r<   r   no_backward_sync)rc   r}   r   rC   forward_modulerG   rG   rH   r   {  s   
zFabric.no_backward_syncc                 C   s,   t d |   t| jtr| j S t S )zInstantiate a model under this context manager to prepare it for model-parallel sharding.

        .. deprecated:: This context manager is deprecated in favor of :meth:`init_module`, use it instead.

        zJ`Fabric.sharded_model()` is deprecated in favor of `Fabric.init_module()`.)r6   r   r\   rO   r)   module_sharded_contextr   rf   rG   rG   rH   sharded_model  s
   
zFabric.sharded_modelc                 C   r   )zTensors that you instantiate under this context manager will be created on the device right away and have
        the right data type depending on the precision setting in Fabric.)rW   tensor_init_contextrf   rG   rG   rH   init_tensor  rv   zFabric.init_tensor
empty_initc                 C   s   |    | jj|dS )a1  Instantiate the model and its parameters under this context manager to reduce peak memory usage.

        The parameters get created on the device and with the right data type right away without wasting memory being
        allocated unnecessarily.

        Args:
            empty_init: Whether to initialize the model with empty weights (uninitialized memory).
                If ``None``, the strategy will decide. Some strategies may not support all options.
                Set this to ``True`` if you are loading a checkpoint into a large model.

        )r   )r   rW   module_init_context)rc   r   rG   rG   rH   init_module  s   zFabric.init_modulepathstatefilterc                 C   s   |dur?t |tstd|t||s(td| |  dt| d| D ]\}}t|s>td|d|q,| j	j
|t||d |   dS )	av  Save checkpoint contents to a file.

        How and which processes save gets determined by the `strategy`. For example, the `ddp` strategy
        saves checkpoints only on process 0, while the `fsdp` strategy saves files from every rank.
        This method must be called on all processes!

        Args:
            path: A path to where the file(s) should be saved
            state: A dictionary with contents to be saved. If the dict contains modules or optimizers, their
                state-dict will be retrieved and converted automatically.
            filter: An optional dictionary containing filter callables that return a boolean indicating whether the
                given item should be saved (``True``) or filtered out (``False``). Each filter key should match a
                state key, where its filter will be applied to the ``state_dict`` generated.

        Nz%Filter should be a dictionary, given zThe filter keys z# are not present in the state keys .z+Expected `fabric.save(filter=...)` for key z to be a callable, given )r   r   r   )r\   dictr   setissubsetr   keysitemscallablerW   save_checkpointrB   r   )rc   r   r   r   kvrG   rG   rH   save  s   
zFabric.savestrictc           	      C   sn   t |}| jj|||d}|   |dur5t| D ]}t|| \}}t|tt	t
fr.q|| ||< q|S )a  Load a checkpoint from a file and restore the state of objects (modules, optimizers, etc.)

        How and which processes load gets determined by the `strategy`.
        This method must be called on all processes!

        Args:
            path: A path to where the file is located
            state: A dictionary of objects whose state will be restored in-place from the checkpoint path.
                If no state is given, then the checkpoint will be returned in full.
            strict: Whether to enforce that the keys in `state` match the keys in the checkpoint.

        Returns:
            The remaining items that were not restored into the given state dictionary. If no state dictionary is
            given, the full checkpoint will be returned.

        r   r   r   N)rB   rW   load_checkpointr   r]   r   rA   r\   r>   r?   r=   )	rc   r   r   r   unwrapped_state	remainderr   r   rC   rG   rG   rH   load  s   zFabric.loadc                 C   s   t |}| jj|||d dS )an  Load the state of a module or optimizer from a single state-dict file.

        Use this for loading a raw PyTorch model checkpoint created without Fabric.
        This is conceptually equivalent to ``obj.load_state_dict(torch.load(path))``, but is agnostic to the strategy
        being used.

        Args:
            path: A path to where the file is located
            obj: A :class:`~torch.nn.Module` or :class:`~torch.optim.Optimizer` instance.
            strict: Whether to enforce that the keys in the module's state-dict match the keys in the checkpoint.
                Does not apply to optimizers.

        r   N)rB   rW   r   )rc   r   r   r   rG   rG   rH   load_raw
  s   zFabric.load_rawfunctionc                 O   s   t  rtd|tur&t|std| dt|js%td| dnt| j	j
ttfr;tdt| j	j d| j|| g|R i |S )a  Launch and initialize all the processes needed for distributed execution.

        Args:
            function: Optional function to launch when using a spawn/fork-based strategy, for example, when using the
                XLA strategy (``accelerator="tpu"``). The function must accept at least one argument, to which
                the Fabric object itself will be passed.
            *args: Optional positional arguments to be passed to the function.
            **kwargs: Optional keyword arguments to be passed to the function.

        Returns:
            Returns the output of the function that ran in worker process with rank 0.

        The ``launch()`` method should only be used if you intend to specify accelerator, devices, and so on in
        the code (programmatically). If you are launching with the Lightning CLI, ``fabric run ...``, remove
        ``launch()`` from your code.

        The ``launch()`` is a no-op when called multiple times and no function is passed in.

        z}This script was launched through the CLI, and processes have already been created. Calling  `.launch()` again is not allowed.z5`Fabric.launch(...)` needs to be a callable, but got z=. HINT: do `.launch(your_fn)` instead of `.launch(your_fn())`z`Fabric.launch(function=z|)` needs to take at least one argument. The launcher will pass in the `Fabric` object so you can use it inside the function.zTo spawn processes with the `zi` strategy, `.launch()` needs to be called with a function that contains the code to launch in processes.)r   r   rI   r   r   inspect	signaturer   r\   rO   launcherr&   r'   typer   _wrap_and_launch)rc   r  rw   rx   rG   rG   rH   launch  s&   

zFabric.launch	hook_namec                 O   sZ   | j D ]'}t||d}|du rqt|s#tdt|j d| d q||i | qdS )a  Trigger the callback methods with the given name and arguments.

        Not all objects registered via ``Fabric(callbacks=...)`` must implement a method with the given name. The ones
        that have a matching method name will get called.

        Args:
            hook_name: The name of the callback method.
            *args: Optional positional arguments that get passed down to the callback method.
            **kwargs: Optional keyword arguments that get passed down to the callback method.

        Example::

            class MyCallback:
                def on_train_epoch_end(self, results):
                    ...

            fabric = Fabric(callbacks=[MyCallback()])
            fabric.call("on_train_epoch_end", results={...})

        NzSkipping the callback `r   z` because it is not callable.)r[   rn   r   r7   r  r   )rc   r  rw   rx   callbackmethodrG   rG   rH   r   F  s   
zFabric.callvaluestepc                 C   s   | j ||i|d dS )a  Log a scalar to all loggers that were added to Fabric.

        Args:
            name: The name of the metric to log.
            value: The metric value to collect. If the value is a :class:`torch.Tensor`, it gets detached from the
                graph automatically.
            step: Optional step number. Most Logger implementations auto-increment the step value by one with every
                log call. You can specify your own value here.

        metricsr  N)log_dict)rc   r   r  r  rG   rG   rH   logm  s   z
Fabric.logr  c                 C   s&   t |}| jD ]	}|j||d qdS )a  Log multiple scalars at once to all loggers that were added to Fabric.

        Args:
            metrics: A dictionary where the key is the name of the metric and the value the scalar to be logged.
                Any :class:`torch.Tensor` in the dictionary get detached from the graph automatically.
            step: Optional step number. Most Logger implementations auto-increment this value by one with every
                log call. You can specify your own value here.

        r  N)r+   r^   log_metrics)rc   r  r  ru   rG   rG   rH   r  z  s   

zFabric.log_dictseedworkersverbosec                 C   s   |du rd}t | ||dS )zHelper function to seed everything without explicitly importing Lightning.

        See :func:`~lightning_fabric.utilities.seed.seed_everything` for more details.

        NTr  r  r  r9   r  rG   rG   rH   r:     s   zFabric.seed_everythingto_runc                 O   sH   d| _ t| j|}| jj }d ur|j|g|R i |S ||i |S NT)r`   r   _wrap_with_setuprW   r  r
  )rc   r  rw   rx   r  rG   rG   rH   r	    s
   zFabric._wrap_and_launchc              	   O   s~   | j   ttd+ tt ||i |W  d    W  d    S 1 s(w   Y  W d    d S 1 s8w   Y  d S )Ndataset)rW   rb   r.   r   r   )rc   r  rw   rx   rG   rG   rH   r    s   
RzFabric._wrap_with_setupc                    s*  z
t | \}}W n	 ty   Y n?w |j}d}d\}}| D ]\}	}
|
j|kr7|d7 }|d u r7|	}|
j}q!|dkrRtd| d|d| d|d| dtd	 t| jtrt	| }| 
|}t	| fd
d| D  |D ]}|jD ]} fdd|d D |d< q{qv|S | 
|}|S )Nr   )NNrM   z)The model passed to `Fabric.setup()` has z. parameters on different devices (for example z on z and z). Since `move_to_device=True`, all parameters will be moved to the new device. If this is not desired, set `Fabric.setup(..., move_to_device=False)`.r   c                    s   i | ]	\}}| | qS rG   rG   )r   r   param)params_on_devicerG   rH   
<dictcomp>  s    z0Fabric._move_model_to_device.<locals>.<dictcomp>c                    s   g | ]}  ||qS rG   )get)r   p)mappingrG   rH   r     r   z0Fabric._move_model_to_device.<locals>.<listcomp>params)r   named_parametersStopIterationrj   r7   r<   r\   rW   r%   r   r   r   param_groups)rc   r   r~   initial_nameinitial_paraminitial_devicecount
first_namefirst_devicer   r  params_before_mover   param_grouprG   )r"  r  rH   r     sP   



zFabric._move_model_to_devicec                 C   s*   t | jdd d uot|jt ot| S )Nr   )rn   rO   r\   r   r   r0   )rc   r   rG   rG   rH   r     s
   z$Fabric._requires_distributed_samplerc                 K   s`   | dt| jt | dttdd t| jttfr't| j	fi |S t
| jfi |S )Nshuffler  PL_GLOBAL_SEEDr   )
setdefaultr\   r   r   intosgetenvr   r   r  r2   )r   rx   rG   rG   rH   r     s
   zFabric._get_distributed_samplerc                 C   s4   t d| trt rtdt| dt| j| j d S )Nrz   zOverriding `Fabric.run()` and launching from the CLI is not allowed. Run the script normally, or change your code to directly call `fabric = Fabric(...); fabric.setup(...)` etc.)r   rK   r   r   setattrr   r	  rz   rf   rG   rG   rH   ra     s
   zFabric._prepare_run_methodc                 C   s&   | j st| jttfstdd S d S )NzgTo use Fabric with more than one device, you must call `.launch()` or use the CLI: `fabric run --help`.)r`   r\   rW   r#   r    r   rf   rG   rG   rH   r     s
   zFabric._validate_launchedc                 C   sb   |    t|trtdtdd |D rtdt| jtr-tdd |D r/tdd S d S )Nz9A model should be passed only once to the `setup` method.c                 s       | ]}t |tV  qd S rF   r\   r?   r   optrG   rG   rH   	<genexpr>      z)Fabric._validate_setup.<locals>.<genexpr>z>An optimizer should be passed only once to the `setup` method.c                 s       | ]}t |V  qd S rF   r4   r   rG   rG   rH   r:    s    
a[  The optimizer has references to the model's meta-device parameters. Materializing them is is currently not supported unless you to set up the model and optimizer(s) separately. Create and set up the model first through `model = fabric.setup_module(model)`. Then create the optimizer and set it up: `optimizer = fabric.setup_optimizers(optimizer)`.)r   r\   r>   r   anyrW   r"   r   )rc   r}   r~   rG   rG   rH   r     s   
zFabric._validate_setupc                 C   s   |    t|trtdd S )Nz@A model should be passed only once to the `setup_module` method.)r   r\   r>   r   )rc   r}   rG   rG   rH   r     s   
zFabric._validate_setup_modulec                 C   st   |    t| jttfrtdt| jj d|stdt	dd |D r+tdt	dd |D r8tdd S )	Nr   zc` requires the model and optimizer(s) to be set up jointly through `.setup(model, optimizer, ...)`.z<`setup_optimizers` requires at least one optimizer as input.c                 s   r6  rF   r7  r8  rG   rG   rH   r:    r;  z4Fabric._validate_setup_optimizers.<locals>.<genexpr>zIAn optimizer should be passed only once to the `setup_optimizers` method.c                 s   r<  rF   r4   r   rG   rG   rH   r:    s    zThe optimizer has references to the model's meta-device parameters. Materializing them is is currently not supported. Create the optimizer after setting up the model, then call `fabric.setup_optimizers(optimizer)`.)
r   r\   rW   r!   r%   r   r  r   r   r=  r   rG   rG   rH   r      s   z!Fabric._validate_setup_optimizersc                 C   sL   |    |s
tdtdd |D rtdtdd |D r$tdd S )Nz>`setup_dataloaders` requires at least one dataloader as input.c                 s   r6  rF   )r\   r=   r   dlrG   rG   rH   r:    r;  z5Fabric._validate_setup_dataloaders.<locals>.<genexpr>zJA dataloader should be passed only once to the `setup_dataloaders` method.c                 s   s    | ]	}t |t V  qd S rF   )r\   r   r>  rG   rG   rH   r:    s    zGOnly PyTorch DataLoader are currently supported in `setup_dataloaders`.)r   r   r=  r   )rc   r   rG   rG   rH   r     s   z"Fabric._validate_setup_dataloadersc                 C   s6   | d ur| ng } t | tr| n| g} | td | S )Nz"lightning_fabric.callbacks_factory)r\   r]   extendr8   )rT   rG   rG   rH   rZ      s   zFabric._configure_callbacks)TT)NNr   TrF   )r   )NF)Nr   )F)Tr  )NNT)rD   N)\r   
__module____qualname____doc__r   strr   r$   r]   r2  r   r   r   r
   r   rd   propertyrN   rO   r   rj   rl   rp   rq   rr   boolrs   rU   ru   rz   r   r   r   r   r>   r   r?   r   r   r   r   r   r   r   floatr   r   r   r   r   r   r   r(   r   r   r   r;   r   r   r   r   r   r   r   r   r	   r   r   r  r  rI   r
  r   r  r   r  staticmethodr:   r	  r  r   r   r   r   ra   r   r   r   r   r   r   rZ   rG   rG   rG   rH   rK   R   s   

	

$
D
"3

(!(


,	*


*

%


.#(+ '$&)

	,rK   )cr  r3  collections.abcr   r   r   
contextlibr   r   r   	functoolsr   pathlibr	   typingr
   r   r   r   r   r   r   torch.nnr   #lightning_utilities.core.apply_funcr   "lightning_utilities.core.overridesr   r   torch.optimr   torch.utils.datar   r   r   r   r   r   )lightning_fabric.accelerators.acceleratorr   lightning_fabric.connectorr   r   r   r   lightning_fabric.loggersr   lightning_fabric.pluginsr   lightning_fabric.strategiesr    r!   r"   r#   r$   r%   %lightning_fabric.strategies.launchersr&   r'   $lightning_fabric.strategies.strategyr(   r)   lightning_fabric.utilitiesr*   %lightning_fabric.utilities.apply_funcr+   r,   lightning_fabric.utilities.datar-   r.   r/   r0   -lightning_fabric.utilities.device_dtype_mixinr1   &lightning_fabric.utilities.distributedr2   r3   lightning_fabric.utilities.initr5   $lightning_fabric.utilities.rank_zeror6   r7   #lightning_fabric.utilities.registryr8   lightning_fabric.utilities.seedr:    lightning_fabric.utilities.typesr;   #lightning_fabric.utilities.warningsr<   lightning_fabric.wrappersr=   r>   r?   r@   rA   rB   rI   rK   rG   rG   rG   rH   <module>   sH    	  
