o
    fôj`+  ã                   @   sV   d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlmZ G dd„ dejƒZ	dS )é    N)ÚPath)Únnc                   @   sê   e Zd ZdZg d¢Z	 g Z	 ddg g g fdededede	d	e	d
e	fdd„Z
edd„ ƒZedddœdededefdd„ƒZg g g fdd„Zed dd„ƒZ		d!dejeef dedefdd„Ze		d"dejeef dedefdd„ƒZdS )#Ú	BaseModelaù  This is a class that adds useful save/load functionality to a
    ``torch.nn.Module`` object. ``BaseModel`` objects can be saved
    as ``torch.package`` easily, making them super easy to port between
    machines without requiring a ton of dependencies. Files can also be
    saved as just weights, in the standard way.

    >>> class Model(ml.BaseModel):
    >>>     def __init__(self, arg1: float = 1.0):
    >>>         super().__init__()
    >>>         self.arg1 = arg1
    >>>         self.linear = nn.Linear(1, 1)
    >>>
    >>>     def forward(self, x):
    >>>         return self.linear(x)
    >>>
    >>> model1 = Model()
    >>>
    >>> with tempfile.NamedTemporaryFile(suffix=".pth") as f:
    >>>     model1.save(
    >>>         f.name,
    >>>     )
    >>>     model2 = Model.load(f.name)
    >>>     out2 = seed_and_run(model2, x)
    >>>     assert torch.allclose(out1, out2)
    >>>
    >>>     model1.save(f.name, package=True)
    >>>     model2 = Model.load(f.name)
    >>>     model2.save(f.name, package=False)
    >>>     model3 = Model.load(f.name)
    >>>     out3 = seed_and_run(model3, x)
    >>>
    >>> with tempfile.TemporaryDirectory() as d:
    >>>     model1.save_to_folder(d, {"data": 1.0})
    >>>     Model.load_from_folder(d)

    )zaudiotools.**ÚtqdmÚ__main__znumpy.**z	julius.**ztorchaudio.**zscipy.**ÚeinopsNTÚpathÚmetadataÚpackageÚinternÚexternÚmockc                 C   sÎ   t  | j¡}i }|j ¡ D ]\}	}
|
j}|t jjur|||	< qt| ƒD ]}||v r0t	| |ƒ||< q#|du r7i n|}||d< t
| dƒsEi | _| j |¡ |s\|  ¡ |dœ}t ||¡ |S | j||||d� |S )aÜ  Saves the model, either as a torch package, or just as
        weights, alongside some specified metadata.

        Parameters
        ----------
        path : str
            Path to save model to.
        metadata : dict, optional
            Any metadata to save alongside the model,
            by default None
        package : bool, optional
            Whether to use ``torch.package`` to save the model in
            a format that is portable, by default True
        intern : list, optional
            List of additional libraries that are internal
            to the model, used with torch.package, by default []
        extern : list, optional
            List of additional libraries that are external to
            the model, used with torch.package, by default []
        mock : list, optional
            List of libraries to mock, used with torch.package,
            by default []

        Returns
        -------
        str
            Path to saved model.
        NÚkwargsr	   )Ú
state_dictr	   )r   r   r   )ÚinspectÚ	signatureÚ	__class__Ú
parametersÚitemsÚdefaultÚ	ParameterÚemptyÚdirÚgetattrÚhasattrr	   Úupdater   ÚtorchÚsaveÚ_save_package)Úselfr   r	   r
   r   r   r   ÚsigÚargsÚkeyÚvalÚarg_valÚ	attributer   © r&   új/var/www/html/academy/tools/voices/clone/sv-venv/lib/python3.10/site-packages/audiotools/ml/layers/base.pyr   B   s,   %€€
þzBaseModel.savec                 C   s   t |  ¡ ƒd jS )z¥Gets the device the model is on by looking at the device of
        the first parameter. May not be valid if model is split across
        multiple devices.
        r   )Úlistr   Údevice)r   r&   r&   r'   r)   ƒ   s   zBaseModel.deviceF)Úpackage_nameÚstrictÚlocationr*   r+   c                O   s²   z
| j ||d�}W |S    t |d¡}|d }|d  |¡ t | ¡}	t|	j ¡ ƒ}
t|d  ¡ ƒD ]}||
vr@|d  	|¡ q3| |i |d ¤Ž}|j
|d |d� ||_Y |S )a»  Load model from a path. Tries first to load as a package, and if
        that fails, tries to load as weights. The arguments to the class are
        specified inside the model weights file.

        Parameters
        ----------
        location : str
            Path to file.
        package_name : str, optional
            Name of package, by default ``cls.__name__``.
        strict : bool, optional
            Ignore unmatched keys, by default False
        kwargs : dict
            Additional keyword arguments to the model instantiation, if
            not loading from package.

        Returns
        -------
        BaseModel
            A model that inherits from BaseModel.
        )r*   Úcpur	   r   r   ©r+   )Ú_load_packager   Úloadr   r   r   r(   r   ÚkeysÚpopÚload_state_dictr	   )Úclsr,   r*   r+   r!   r   ÚmodelZ
model_dictr	   r    Z
class_keysÚkr&   r&   r'   r0   ‹   s"   ñ
€zBaseModel.loadc           
   	   K   s  t | ƒj}t | ƒj› d�}t| dƒr| jtjjf|d< | `tjdd��V}tjj	|j
fi |¤Ž�4}	|	 | j| ¡ |	 |¡ |	 | j| ¡ |	 ||| ¡ t| dƒr\|	 ||› d�| j¡ W d   ƒ n1 sfw   Y  t |j
|¡ W d   ƒ n1 s|w   Y  d|v rŒ|d d | _|S )Nú.pthÚimporter)Úsuffixr	   ú	.metadatar   )ÚtypeÚ__name__r   r8   r   r
   Úsys_importerÚtempfileÚNamedTemporaryFileÚPackageExporterÚnamer   ÚINTERNr   r   ÚEXTERNÚsave_pickler	   ÚshutilÚcopyfile)
r   r   r   r   r   r   r*   Úresource_nameÚfÚexpr&   r&   r'   r   ¼   s,   



ÿ€ùôzBaseModel._save_packagec                 C   sb   |d u r| j n|}|› d�}tj |¡}| ||d¡}z| ||› d�¡|_W n   Y ||_|S )Nr7   r-   r:   )r<   r   r
   ÚPackageImporterÚload_pickler	   r8   )r4   r   r*   rG   Úimpr5   r&   r&   r'   r/   Þ   s   
zBaseModel._load_packageÚfolderÚ
extra_datac           
      C   s’   |du ri n|}t | ƒj ¡ }t|› d|› d�ƒ}|jddd� |r+|d }|  |¡ |d }| j|dd� | ¡ D ]\}}	t |	|| ¡ q:|S )	a³  Dumps a model into a folder, as both a package
        and as weights, as well as anything specified in
        ``extra_data``. ``extra_data`` is a dictionary of other
        pickleable files, with the keys being the paths
        to save them in. The model is saved under a subfolder
        specified by the name of the class (e.g. ``folder/generator/[package, weights].pth``
        if the model name was ``Generator``).

        >>> with tempfile.TemporaryDirectory() as d:
        >>>     extra_data = {
        >>>         "optimizer.pth": optimizer.state_dict()
        >>>     }
        >>>     model.save_to_folder(d, extra_data)
        >>>     Model.load_from_folder(d)

        Parameters
        ----------
        folder : typing.Union[str, Path]
            _description_
        extra_data : dict, optional
            _description_, by default None

        Returns
        -------
        str
            Path to folder
        Nú/T)Úexist_okÚparentsúpackage.pthúweights.pthF)r
   )r;   r<   Úlowerr   Úmkdirr   r   r   )
r   rM   rN   r
   Ú
model_nameZtarget_baseÚpackage_pathZweights_pathr   Úobjr&   r&   r'   Úsave_to_folderí   s   !
zBaseModel.save_to_folderc           
         s‚   t |ƒ| j ¡  }|rdnd}|| }| j||d�}i }ddg‰ ‡ fdd„| d¡D ƒ}|D ]}	tj|	fi |¤Ž||	j< q.||fS )a?  Loads the model from a folder generated by
        :py:func:`audiotools.ml.layers.base.BaseModel.save_to_folder`.
        Like that function, this one looks for a subfolder that has
        the name of the class (e.g. ``folder/generator/[package, weights].pth`` if the
        model name was ``Generator``).

        Parameters
        ----------
        folder : typing.Union[str, Path]
            _description_
        package : bool, optional
            Whether to use ``torch.package`` to load the model,
            loading the model from ``package.pth``.
        strict : bool, optional
            Ignore unmatched keys, by default False

        Returns
        -------
        tuple
            tuple of model and extra data as saved by
            :py:func:`audiotools.ml.layers.base.BaseModel.save_to_folder`.
        rR   rS   r.   c                    s"   g | ]}|  ¡ r|jˆ vr|‘qS r&   )Úis_filerA   )Ú.0Úx©Úexcludedr&   r'   Ú
<listcomp>D  s   " z.BaseModel.load_from_folder.<locals>.<listcomp>Ú*)r   r<   rT   r0   Úglobr   rA   )
r4   rM   r
   r+   r   Z	model_pthr5   rN   ÚfilesrH   r&   r]   r'   Úload_from_folder  s   zBaseModel.load_from_folder)N)NT)TF)r<   Ú
__module__Ú__qualname__Ú__doc__rC   rB   ÚstrÚdictÚboolr(   r   Úpropertyr)   Úclassmethodr0   r   r/   ÚtypingÚUnionr   rY   rc   r&   r&   r&   r'   r      st    %
ùþýüûú
ùA
ûþüû0"üþý
ü2üþýür   )
r   rE   r>   rl   Úpathlibr   r   r   ÚModuler   r&   r&   r&   r'   Ú<module>   s    