o
    ¢ôjÂ
  ã                   @   s¤   d dl 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 d dlmZ d dlmZ G dd„ de	ƒZd	ejjd
eddfdd„Zd	ejjd
eddfdd„ZdS )é    N)ÚAnyÚCallableÚDictÚOptionalÚSequence)ÚTorchFunctionMode)Úoverride)Ú_TORCH_GREATER_EQUAL_2_1)Ú_DEVICEc                       s\   e Zd ZdZddeddf‡ fdd„Ze		dd	ed
edee	 de
e de	f
dd„ƒZ‡  ZS )Ú
_EmptyInitzÑInitialize `nn.Module` with empty tensors, i.e., uninitialized memory.

    Example::

        with _EmptyInit():
            model = BigModel()
        model.load_state_dict(torch.load("checkpoint.pt"))

    TÚenabledÚreturnNc                    s   t ƒ  ¡  || _d S ©N)ÚsuperÚ__init__r   )Úselfr   ©Ú	__class__© úr/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/lightning/fabric/utilities/init.pyr   %   s   

z_EmptyInit.__init__r   ÚfuncÚtypesÚargsÚkwargsc                 C   sR   |pi }| j s||i |¤ŽS t|dd ƒdkr"d|v r|d S |d S ||i |¤ŽS )NÚ
__module__ztorch.nn.initÚtensorr   )r   Úgetattr)r   r   r   r   r   r   r   r   Ú__torch_function__)   s   z_EmptyInit.__torch_function__)T)r   N)Ú__name__r   Ú__qualname__Ú__doc__Úboolr   r   r   r   r   r   r   r   Ú__classcell__r   r   r   r   r      s"    
ûþýüûúr   ÚmoduleÚdevicer   c                 C   sF   t stdƒ‚| j|dd� t| dƒstdt| ƒj› d�ƒ‚|  ¡  dS )zMaterialize a module.z recurse=False requires torch 2.1F)r$   ÚrecurseÚreset_parametersz#Materialization requires that the `z~.reset_parameters` method is implemented. This method is used to initialize any children parameters or buffers in this module.N)r	   ÚRuntimeErrorÚto_emptyÚhasattrÚ	TypeErrorÚtyper   r&   ©r#   r$   r   r   r   Ú_materialize;   s   
ÿr-   c              	   C   sF   |   ¡ D ]} tdd„ t | jdd�| jdd�¡D ƒƒr t| |ƒ qdS )z*Materialize all tensors in a given module.c                 s   s   � | ]}|j V  qd S r   )Úis_meta)Ú.0Útr   r   r   Ú	<genexpr>K   s   € z,_materialize_meta_tensors.<locals>.<genexpr>F)r%   N)ÚmodulesÚanyÚ	itertoolsÚchainÚ
parametersÚbuffersr-   r,   r   r   r   Ú_materialize_meta_tensorsH   s
   *
€þr8   )r4   Útypingr   r   r   r   r   ÚtorchÚtorch.overridesr   Útyping_extensionsr   Ú"lightning.fabric.utilities.importsr	   Ú lightning.fabric.utilities.typesr
   r   ÚnnÚModuler-   r8   r   r   r   r   Ú<module>   s   !