U
    ‰dß	  ã                	   @   s    d dl mZ d dlmZ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„ Zdd	„ Zee	e
jeeeeeef ed
œdd„Ze
jjeedœdd„ZdS )é    )ÚOrderedDict)ÚAnyÚCallableÚDictÚListÚSetÚTupleÚUnionN)Ú
_BatchNorm)ÚPackedSequencec                 C   s   t dd„ |  ¡ D ƒƒS )Nc                 s   s   | ]}t |tƒV  qd S ©N)Ú
isinstancer
   )Ú.0Úmod© r   úA/tmp/pip-unpacked-wheel-ua33x9lu/torch/distributed/fsdp/_utils.pyÚ	<genexpr>   s    z&_contains_batchnorm.<locals>.<genexpr>)ÚanyÚmodules)Úmoduler   r   r   Ú_contains_batchnorm   s    ÿr   c                 C   s&   |   ¡ D ]}t|tƒrdd i|_qd S )NZmixed_precision)r   r   r
   Z_wrap_overrides)r   r   r   r   r   Ú#_override_batchnorm_mixed_precision   s    
r   )ÚfnÚ	containerÚreturnc                    s2   t tjttttttf t	dœ‡ ‡fdd„‰ ˆ |ƒS )zFRecursively apply to all tensor in different kinds of container types.)Úxr   c                    s°   t  | ¡rˆ| ƒS t| tƒrF|  ¡ }|  ¡ D ]\}}ˆ |ƒ||< q,|S t| tƒr^ˆ | jƒ | S t| tƒr~‡ fdd„|  ¡ D ƒS t| t	t
tfƒr¨t| ƒ‡ fdd„| D ƒƒS | S d S )Nc                    s   i | ]\}}|ˆ |ƒ“qS r   r   )r   ÚkeyÚvalue©Úapplyr   r   Ú
<dictcomp>&   s      z4_apply_to_tensors.<locals>.apply.<locals>.<dictcomp>c                 3   s   | ]}ˆ |ƒV  qd S r   r   )r   Úelr   r   r   r   (   s     z3_apply_to_tensors.<locals>.apply.<locals>.<genexpr>)ÚtorchZ	is_tensorr   r   Ú	__class__Úitemsr   ÚdataÚdictÚlistÚtupleÚsetÚtype)r   Zodr   r   ©r   r   r   r   r      s    




z _apply_to_tensors.<locals>.apply)
r	   r"   ÚTensorr   r   r   r   r   r   r   )r   r   r   r+   r   Ú_apply_to_tensors   s    *r-   )Úroot_moduleÚ	module_fnÚ	return_fnc                    s6   t jjtdœ‡ ‡fdd„‰ ˆ | df|ž|Ž |||ŽS )ae  
    Performs a pre-order traversal of the modules in the hierarchy rooted at
    ``root_module``, applying ``module_fn`` at each module and finally
    returning a value using ``return_fn``. The traversal constructs the full
    module prefix name (e.g. "module.submodule." just like in model state dict)
    and makes that available to ``module_fn``.
    )r   Úprefixc                    sN   ˆ| |f|ž|Ž |   ¡ D ].\}}|d k	r|| d }ˆ ||f|ž|Ž qd S )NÚ.)Znamed_children)r   r1   ÚargsÚkwargsZsubmodule_nameÚ	submoduleÚ
new_prefix©Úfr/   r   r   r8   <   s
    z_apply_to_modules.<locals>.fÚ )r"   ÚnnÚModuleÚstr)r.   r/   r0   r3   r4   r   r7   r   Ú_apply_to_modules.   s    r=   )Úcollectionsr   Útypingr   r   r   r   r   r   r	   r"   Ztorch.nn.modules.batchnormr
   Ztorch.nn.utils.rnnr   r   r   r,   r-   r:   r;   r=   r   r   r   r   Ú<module>   s   $ þý