o
    ¢ôjYZ  ã                   @   sÎ  d dl Z d dl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	m
Z
mZmZmZmZmZmZmZm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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%de&fdd„Z'de%dee( fdd„Z)de%dee fdd„Z*dedeeef defdd„Z+dedeeef deee	 ee,e	f f fdd„Z-dedeeef dee,e	f fdd„Z.de%de(ddfdd „Z/dd!œd"e	d#e	d$ee d%e	de	f
d&d'„Z0dEd(e
d)ee, de
fd*d+„Z1d,e
d-e$de
fd.d/„Z2edEd0ed)ee, ded1 fd2d3„ƒZ3d4e,d5e	d#ee	d6f d%ee,e	f d7ee,e	f d8ee,d6f dee&ee	d6f ee,e	f f fd9d:„Z4de%d;e(ddfd<d=„Z5d>e(de(fd?d@„Z6de(fdAdB„Z7G dCdD„ dDeƒZ8dS )Fé    N)ÚOrderedDict)Úcontextmanager)Úpartial)
ÚAnyÚCallableÚDictÚ	GeneratorÚIterableÚOptionalÚSizedÚTupleÚTypeÚUnion)Úget_all_subclasses)ÚBatchSamplerÚ
DataLoaderÚIterableDatasetÚSampler)Ú	TypeGuard)ÚLightningEnum)ÚMisconfigurationException)Úrank_zero_warn)Úpl_worker_init_functionc                   @   s&   e Zd ZdZdZdeddfdd„ZdS )Ú_WrapAttrTagÚsetÚdelÚargsÚreturnNc                 G   s   | | j krtnt}||Ž S ©N)ÚSETÚsetattrÚdelattr)Úselfr   Úfn© r$   úr/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/lightning/fabric/utilities/data.pyÚ__call__%   s   z_WrapAttrTag.__call__)Ú__name__Ú
__module__Ú__qualname__r   ÚDELr   r&   r$   r$   r$   r%   r   !   s    r   Ú
dataloaderr   c                 C   s   t | dƒo
t| jtƒS )NÚdataset)ÚhasattrÚ
isinstancer,   r   )r+   r$   r$   r%   Úhas_iterable_dataset+   s   r/   c              	   C   s,   zt | ƒ}W |S  ttfy   d}Y |S w )z>Try to get the length of an object, return ``None`` otherwise.N)ÚlenÚ	TypeErrorÚNotImplementedError©r+   Úlengthr$   r$   r%   Ú	sized_len/   s   
þþr5   c                 C   sD   t | ƒ}|dkrtd| jj› d�ƒ |durt| ƒrtdƒ |duS )z<Checks if a given object has ``__len__`` method implemented.r   ú`z>` returned 0 length. Please make sure this was your intention.NzáYour `IterableDataset` has `__len__` defined. In combination with multi-process data loading (when num_workers > 1), `__len__` could be inaccurate if each worker is not configured independently to avoid having duplicate data.)r5   r   Ú	__class__r'   r/   r3   r$   r$   r%   Úhas_len9   s   ÿÿr8   Úsamplerc                 C   s$   t | |ƒ\}}t| g|¢R i |¤ŽS r   )Ú$_get_dataloader_init_args_and_kwargsÚ_reinstantiate_wrapped_cls)r+   r9   Údl_argsÚ	dl_kwargsr$   r$   r%   Ú_update_dataloaderJ   s   r>   c              
      s  t | tƒstd| › d�ƒ‚t| dƒ}|r!| j}| j‰| j‰ | j}ndd„ t| ƒ 	¡ D ƒ‰d }| j
ˆd< d‰ tt | j¡jƒ}tdd	„ | ¡ D ƒƒ}|ro|r_| d
d„ t tj¡j 	¡ D ƒ¡ n| t tj¡j¡ | dd ¡ |sŽ‡fdd„| 	¡ D ƒ‰ˆ d¡ ‡fdd„ˆ 	¡ D ƒ‰d}ˆ d|¡}t |tƒr¢d ˆd< d ˆd< nˆ t| |ƒ¡ ‡ ‡fdd„| ¡ D ƒ}|rÛt|ƒ}	| jj}
d dd	„ |	D ƒ¡}td|
› d|	› d|
› d|› d�	ƒ‚|�stˆƒtˆ ƒB | ¡  }|�rt|ƒ}| jj}
td|
› d|› d|
› d�ƒ‚|ˆfS )NzThe dataloader z0 needs to subclass `torch.utils.data.DataLoader`Ú__pl_saved_argsc                 S   s    i | ]\}}|  d ¡s||“qS )Ú_)Ú
startswith©Ú.0ÚkÚvr$   r$   r%   Ú
<dictcomp>^   s     z8_get_dataloader_init_args_and_kwargs.<locals>.<dictcomp>Úmultiprocessing_contextr$   c                 s   s   � | ]	}|j |ju V  qd S r   )ÚkindÚVAR_KEYWORD©rC   Úpr$   r$   r%   Ú	<genexpr>h   ó   € z7_get_dataloader_init_args_and_kwargs.<locals>.<genexpr>c                 S   s"   i | ]\}}|j |jur||“qS r$   )ÚdefaultÚemptyrB   r$   r$   r%   rF   o   s    r"   c                    s*   h | ]\}}|ˆ v r|j ˆ | ur|’qS r$   )rN   )rC   ÚnamerK   )Úattrsr$   r%   Ú	<setcomp>x   s   * z7_get_dataloader_init_args_and_kwargs.<locals>.<setcomp>r,   c                    s   i | ]\}}|ˆ v r||“qS r$   r$   rB   )Únon_defaultsr$   r%   rF   }   s    Úbatch_samplerr9   c                    sD   h | ]}|j |j|jfv r |j|ju r |jˆvr |jˆ vr|j’qS r$   )rH   ÚPOSITIONAL_ONLYÚPOSITIONAL_OR_KEYWORDrN   rO   rP   rJ   )Ú	arg_namesr=   r$   r%   rR   ‡   s    

ûz, c                 s   s   � | ]	}d |› d�V  qdS )z`self.r6   Nr$   )rC   Úarg_namer$   r$   r%   rL   “   rM   z,Trying to inject custom `Sampler` into the `z…` instance. This would fail as some of the `__init__` arguments are not available as instance attributes. The missing attributes are z. If you instantiate your `zZ` inside a `*_dataloader` hook of your module, we will do this for you. Otherwise, define z inside your `__init__`.z&Trying to inject parameters into the `z{` instance. This would fail as it doesn't expose all its attributes in the `__init__` signature. The missing arguments are z. HINT: If you wrote the `zA` class, add the `__init__` arguments or allow passing `**kwargs`) r.   r   Ú
ValueErrorr-   r?   Ú__pl_saved_kwargsÚ__pl_saved_arg_namesZ	__datasetÚvarsÚitemsrG   ÚdictÚinspectÚ	signatureÚ__init__Ú
parametersÚanyÚvaluesÚupdateÚpopÚaddÚgetr   Ú'_dataloader_init_kwargs_resolve_samplerÚsortedr7   r'   Újoinr   r   Úkeysr1   )r+   r9   Zwas_wrappedr<   Zoriginal_datasetÚparamsZhas_variadic_kwargsr,   Zrequired_argsZsorted_required_argsZdataloader_cls_nameZmissing_args_messageZmissing_kwargsZsorted_missing_kwargsr$   )rW   rQ   r=   rS   r%   r:   O   sx   




ÿ


þ	þþüÿþþÿr:   c              
   C   s  t | dƒ}|dur‰t|ƒtur‰t|ƒ}t|dƒrG|j}|j}|j}|j}td|||||ƒ\}}}|s;t	d|j
› d�ƒ‚t|g|¢R i |¤Ž}n:t|dƒr}t|dƒr}z|||j|jd	�}W n$ t	y| }	 zd
dl}
|
 dt|	ƒ¡}|ss‚ t	dƒ|	‚d}	~	ww t	dƒ‚dd|dddœS |dddœS )z‚This function is used to handle the sampler, batch_sampler arguments associated within a DataLoader for its re-
    instantiation.rT   Nr?   r9   zYTrying to inject a modified sampler into the batch sampler; however, it seems the class `z‹` does not have an argument called `sampler.` To mitigate this, expose an argument `sampler` in the `__init__` method of your custom class.Ú
batch_sizeÚ	drop_last)rn   ro   r   z:.*__init__\(\) (got multiple values)|(missing \d required)ak   Lightning can't inject a (distributed) sampler into your batch sampler, because it doesn't subclass PyTorch's `BatchSampler`. To mitigate this, either follow the API of `BatchSampler` or set`.setup_dataloaders(..., use_distributed_sampler=False)`. If you choose the latter, you will be responsible for handling the distributed sampling within your batch sampler.Fé   )r9   ÚshufflerT   rn   ro   )r9   rq   rT   )ÚgetattrÚtyper   r-   r?   rZ   Ú__pl_saved_default_kwargsr[   Ú_replace_value_in_saved_argsr1   r)   r;   rn   ro   ÚreÚmatchÚstr)r+   r9   rT   Zbatch_sampler_clsr   ÚkwargsÚdefault_kwargsrW   ÚsuccessÚexrv   rw   r$   r$   r%   ri   ¬   s^   


ÿÿÿ
ýÿû€öÿûri   Úrankc                 C   sD   t | dƒsd S ttj dd¡ƒr| jd u r tt|d�| _d S d S d S )NÚworker_init_fnZPL_SEED_WORKERSr   )r}   )r-   ÚintÚosÚenvironrh   r~   r   r   )r+   r}   r$   r$   r%   Ú_auto_add_worker_init_fnõ   s
   
ÿr‚   )Úexplicit_clsÚorig_objectr   rƒ   ry   c                O   sÂ   |d u rt | ƒn|}z	||i |¤Ž}W n6 tyI } z*dd l}| dt|ƒ¡}|s*‚ | ¡ d }	d|j› d|	› d|	› d|	› d�	}
t|
ƒ|‚d }~ww t| dg ƒ}|D ]\}}||g|¢R Ž  qR|S )	Nr   z-.*__init__\(\) got multiple values .* '(\w+)'zThe zd implementation has an error where more than one `__init__` argument can be passed to its parent's `zr=...` `__init__` argument. This is likely caused by allowing passing both a custom argument that will map to the `zc` argument as well as `**kwargs`. `kwargs` should be filtered to make sure they don't contain the `zR` key. This argument was automatically passed to your object by PyTorch Lightning.Ú__pl_attrs_record)	rs   r1   rv   rw   rx   Úgroupsr'   r   rr   )r„   rƒ   r   ry   ÚconstructorÚresultr|   rv   rw   ÚargumentÚmessageÚattrs_recordr#   r$   r$   r%   r;   ü   s0   
ÿþýÿ
€ïr;   ÚinitÚstore_explicit_argc              	      s.   t  ˆ ¡dtdtdtddf‡ ‡fdd„ƒ}|S )zÄWraps the ``__init__`` method of classes (currently :class:`~torch.utils.data.DataLoader` and
    :class:`~torch.utils.data.BatchSampler`) in order to enable re-instantiation of custom subclasses.Úobjr   ry   r   Nc                    s"  t | ddƒ}t | dd¡ t ˆ¡j}tdd„ | ¡ D ƒƒ}t|ƒd t	|ƒ… ‰‡ ‡fdd„| 
¡ D ƒ}t| dƒsUt | d|¡ t | d	ˆ ¡ t | d
ˆ¡ t | d|¡ ˆd ur}ˆˆv rmt | dˆ› �|ˆ ˆ¡ ¡ nˆˆ v r}t | dˆ› �ˆ ˆ ¡ ˆ| g|¢R i ˆ ¤Ž t | d|¡ d S )NÚ__pl_inside_initFTc                 s   s8   � | ]}|j d kr|j|j|jfvr|j |jfV  qdS )r"   N)rP   rH   ÚVAR_POSITIONALrI   rN   )rC   Úparamr$   r$   r%   rL   '  s   € 
þýz5_wrap_init_method.<locals>.wrapper.<locals>.<genexpr>c                    s2   i | ]\}}|ˆ vr|ˆvr|t jjkr||“qS r$   )r_   Ú	ParameterrO   )rC   rP   Úvalue©ry   Úparam_namesr$   r%   rF   /  s
    þz6_wrap_init_method.<locals>.wrapper.<locals>.<dictcomp>r?   rZ   r[   rt   Ú__)rr   ÚobjectÚ__setattr__r_   r`   rb   r   rd   Útupler0   r]   r-   Úindex)rŽ   r   ry   Zold_inside_initrm   Zparameters_defaultsrz   ©rŒ   r�   r”   r%   Úwrapper  s,   þþ
 z"_wrap_init_method.<locals>.wrapper©Ú	functoolsÚwrapsr   )rŒ   r�   rœ   r$   r›   r%   Ú_wrap_init_method  s   "'r    ÚmethodÚtagc                    s*   t  ˆ ¡dtdtddf‡ ‡fdd„ƒ}|S )zÛWraps the ``__setattr__`` or ``__delattr__`` method of classes (currently :class:`~torch.utils.data.DataLoader`
    and :class:`~torch.utils.data.BatchSampler`) in order to enable re- instantiation of custom subclasses.rŽ   r   r   Nc                    sš   |^}}t | ddƒ\}}||ko|ˆk }t | d|ˆf¡ ˆ | g|¢R Ž  |rBt | ddƒsBt | dg ƒ}| |ˆf¡ t | d|¡ t | d||f¡ d S )NZ__pl_current_call)Nr¡   r�   Tr…   )rr   r—   r˜   Úappend)rŽ   r   rP   r@   Zprev_call_nameZprev_call_methodÚ
first_callr‹   ©r¡   r¢   r$   r%   rœ   N  s   z"_wrap_attr_method.<locals>.wrapperr�   )r¡   r¢   rœ   r$   r¥   r%   Ú_wrap_attr_methodJ  s   r¦   Úbase_cls)NNNc              	   c   sô   � t | ƒ| hB }|D ]D}d|jv r|j|_t|j|ƒ|_dtjfdtjffD ]'\}}||jv s3|| u rMd|› �}t||t	||ƒƒ t||t
t	||ƒ|ƒƒ q&q
dV  |D ]#}dD ]}d|› �|jv rvt||t	|d|› �ƒƒ t|d|› �ƒ qXqTdS )z»This context manager is used to add support for re-instantiation of custom (subclasses) of `base_cls`.

    It patches the ``__init__``, ``__setattr__`` and ``__delattr__`` methods.

    ra   r˜   Ú__delattr__Z__oldN)r˜   r¨   ra   )r   Ú__dict__ra   Z__old__init__r    r   r   r*   r    rr   r¦   r!   )r§   r�   ÚclassesÚclsZpatch_fn_namer¢   Z
saved_nameZpatched_namer$   r$   r%   Ú_replace_dunder_methodse  s,   €

€ü€ûÿr¬   Úreplace_keyÚreplace_value.rz   rW   c                 C   sj   | |v r|  | ¡}|d|… |f ||d d…  }d||fS | |v s'| |v r0||| < d||fS d||fS )z¨Tries to replace an argument value in a saved list of args and kwargs.

    Returns a tuple indicating success of the operation and modified saved args and kwargs

    Nrp   TF)rš   )r­   r®   r   ry   rz   rW   Úreplace_indexr$   r$   r%   ru   …  s   
"


ru   Úepochc                 C   s†   i }t | ddƒ }dur||t|ƒ< t | ddƒ }dur,t |ddƒ }dur,||t|ƒ< | ¡ D ]}t |ddƒ}t|ƒr@||ƒ q0dS )a�  Calls the ``set_epoch`` method on either the sampler of the given dataloader.

    Every PyTorch dataloader has either a sampler or a batch sampler. If the sampler is wrapped by a
    :class:`~torch.utils.data.distributed.DistributedSampler`, ``set_epoch`` must be called at the beginning
    of every epoch to ensure shuffling applies a new ordering. This has no effect if shuffling is off.

    r9   NrT   Ú	set_epoch)rr   Úidrd   Úcallable)r+   r°   Úobjectsr9   rT   rŽ   r±   r$   r$   r%   Ú_set_sampler_epochž  s   	þ€ýrµ   Úlocal_world_sizec                 C   s0   | dk rt d| › d�ƒ‚tƒ }td||  d ƒS )aœ  Suggests an upper bound of ``num_workers`` to use in a PyTorch :class:`~torch.utils.data.DataLoader` based on
    the number of CPU cores available on the system and the number of distributed processes in the current machine.

    Args:
        local_world_size: The number of distributed processes running on the current machine. Set this to the number
            of devices configured in Fabric/Trainer.

    rp   z'`local_world_size` should be >= 1, got Ú.)rY   Ú_num_cpus_availableÚmax)r¶   Ú	cpu_countr$   r$   r%   Úsuggested_max_num_workers¶  s   	r»   c                  C   s0   t tdƒrtt d¡ƒS t ¡ } | d u rdS | S )NÚsched_getaffinityr   rp   )r-   r€   r0   r¼   rº   )rº   r$   r$   r%   r¸   Å  s   
r¸   c                   @   sX   e Zd ZdZdedefdd„Zdededdfdd	„Zd
eddfdd„Zdefdd„Z	dS )ÚAttributeDicta  A container to store state variables of your program.

    This is a drop-in replacement for a Python dictionary, with the additional functionality to access and modify keys
    through attribute lookup for convenience.

    Use this to define the state of your program, then pass it to
    :meth:`~lightning.fabric.fabric.Fabric.save` and :meth:`~lightning.fabric.fabric.Fabric.load`.

    Example:
        >>> import torch
        >>> model = torch.nn.Linear(2, 2)
        >>> state = AttributeDict(model=model, iter_num=0)
        >>> state.model
        Linear(in_features=2, out_features=2, bias=True)
        >>> state.iter_num += 1
        >>> state.iter_num
        1
        >>> state
        "iter_num": 1
        "model":    Linear(in_features=2, out_features=2, bias=True)

    Úkeyr   c              
   C   sB   z| | W S  t y  } ztdt| ƒj› d|› d�ƒ|‚d }~ww )Nú'z' object has no attribute ')ÚKeyErrorÚAttributeErrorrs   r'   )r"   r¾   Úer$   r$   r%   Ú__getattr__å  s   
€ÿzAttributeDict.__getattr__ÚvalNc                 C   s   || |< d S r   r$   )r"   r¾   rÄ   r$   r$   r%   r˜   ë  s   zAttributeDict.__setattr__Úitemc                 C   s   || vrt |ƒ‚| |= d S r   )rÀ   )r"   rÅ   r$   r$   r%   r¨   î  s   
zAttributeDict.__delattr__c                    sX   t ˆ ƒsdS tdd„ ˆ D ƒƒ}dt|d ƒ d ‰‡ ‡fdd„tˆ  ¡ ƒD ƒ}d	 |¡S )
NÚ c                 s   s   � | ]	}t t|ƒƒV  qd S r   )r0   rx   )rC   rD   r$   r$   r%   rL   ö  rM   z)AttributeDict.__repr__.<locals>.<genexpr>z{:é   zs} {}c                    s$   g | ]}ˆ  d |› d�ˆ | ¡‘qS )ú"z":)Úformat)rC   Ún©r"   Útmp_namer$   r%   Ú
<listcomp>ø  s   $ z*AttributeDict.__repr__.<locals>.<listcomp>Ú
)r0   r¹   rx   rj   rl   rk   )r"   Zmax_key_lengthÚrowsr$   rË   r%   Ú__repr__ó  s   
zAttributeDict.__repr__)
r'   r(   r)   Ú__doc__rx   r   rÃ   r˜   r¨   rÐ   r$   r$   r$   r%   r½   Í  s    r½   r   )9rž   r_   r€   Úcollectionsr   Ú
contextlibr   r   Útypingr   r   r   r   r	   r
   r   r   r   r   Z$lightning_utilities.core.inheritancer   Útorch.utils.datar   r   r   r   Útyping_extensionsr   Ú lightning.fabric.utilities.enumsr   Ú%lightning.fabric.utilities.exceptionsr   Ú$lightning.fabric.utilities.rank_zeror   Úlightning.fabric.utilities.seedr   r   r—   Úboolr/   r   r5   r8   r>   rx   r:   ri   r‚   r;   r    r¦   r¬   ru   rµ   r»   r¸   r½   r$   r$   r$   r%   Ú<module>   st   0

ÿ
þ
ý]ÿ
þ

ýI(/"ÿþ
ý
ü
û
ú
ù