a
    d
                     @   sx   d dl 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
 d dlmZ d dlmZ d dlmZ G d	d
 d
eZdS )    N)AnyDictOptional)apply_to_collection)RequirementCache)_XLA_AVAILABLE)TorchCheckpointIO)get_filesystem)_PATHc                       sL   e Zd ZdZeedd fddZd	eeef ee	e ddddZ
  ZS )
XLACheckpointIOz[CheckpointIO that utilizes :func:`xm.save` to save checkpoints for TPU training strategies.N)argskwargsreturnc                    s&   t sttt t j|i | d S )N)r   ModuleNotFoundErrorstrsuper__init__)selfr   r   	__class__ h/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/lightning_fabric/plugins/io/xla.pyr      s    zXLACheckpointIO.__init__)
checkpointpathstorage_optionsr   c           	      C   s   |durt d| jj dt|}|jtj|dd tdrhddl	m
}m}m} t|||f|j}ddlm  m} |d	d
 | D | dS )a{  Save model/training states as a checkpoint file through state-dump and file-write.

        Args:
            checkpoint: dict containing model and trainer state
            path: write-target path
            storage_options: not used in ``XLACheckpointIO.save_checkpoint``

        Raises:
            TypeError:
                If ``storage_options`` arg is passed in
        Nze`Trainer.save_checkpoint(..., storage_options=...)` with `storage_options` arg is not supported for `za`. Please implement your custom `CheckpointIO` to define how you'd like to use `storage_options`.T)exist_ok	omegaconfr   )
DictConfig
ListConfig	OmegaConfc                 S   s   i | ]\}}|d kr||qS )	callbacksr   ).0kvr   r   r   
<dictcomp>=       z3XLACheckpointIO.save_checkpoint.<locals>.<dictcomp>)	TypeErrorr   __name__r	   makedirsosr   dirnamer   r   r   r   r   r   Zto_containerZtorch_xla.core.xla_modelcoreZ	xla_modelsaveitems)	r   r   r   r   fsr   r   r   Zxmr   r   r   save_checkpoint"   s    zXLACheckpointIO.save_checkpoint)N)r'   
__module____qualname____doc__r   r   r   r   r
   r   r/   __classcell__r   r   r   r   r      s   r   )r)   typingr   r   r   Z#lightning_utilities.core.apply_funcr   Z lightning_utilities.core.importsr   Z!lightning_fabric.accelerators.tpur   Z$lightning_fabric.plugins.io.torch_ior   Z#lightning_fabric.utilities.cloud_ior	   Z lightning_fabric.utilities.typesr
   r   r   r   r   r   <module>   s   