
    }YhWX                     2   d dl Z d dlZd dlmZmZ d dlmZmZmZ d dl	m
Z
mZmZmZmZ d dlmZ g dZdej        deej        geej                 f         fdZdej        d	eej        eee
f         f         d
edeej        geej                 f         fdZdej        deeej                          deej                 deee
f         d	eej        eee
f         f         f
dZdefdZ G d de          Zdej        dededeeej                          def
dZ G d de          Z G d de          Z dej        dedededef
dZ!dej        dededeeej                          def
dZ"dej        dee         defd Z#dej        dededefd!Z$ ed"          ddfdej        deded#ed$eeeej                                   d%eeeej                                   defd&Z%ej&        ej'        he%_(        ej)        he%_*        e j+        d'e
d(e
ded)         fd*            Z,dej        d+e
dej        fd,Z-dej        d'edej        fd-Z.	 d6dej        d/ed'edeej                 d0eej/                 d1ed2e
de0ej        ef         fd3Z1 G d4 d5          Z2dS )7    N)ABCabstractmethod)	GeneratorIterableSequence)AnyCallablecastOptionalUnion)always_wrap_policylambda_auto_wrap_policytransformer_auto_wrap_policysize_based_auto_wrap_policyenable_wrapwrapCustomPolicyModuleWrapPolicyroot_modulefnc                     | hdt           j        dt          dt          t           j                 ffd | dd           dS )aQ  
    This applies ``fn`` to every module in the module tree of ``root_module``
    following a post-order traversal. If ``fn`` returns an :class:`nn.Module`,
    then this replaces the original module with the newly returned one in the
    tree. Otherwise, ``fn`` should return ``None``, in which case the module is
    not changed.
    modulemodule_nameparent_modulec                    |                                  D ]+\  }}|vr"                    |            |||            , |           }|st          |t          j                  sJ d| d|              |sJ d|              t          |t          j                  sJ d|             t          |||           d S d S )Nz=Non-root modules should have their parent module set but got z for zTNon-root modules should have their module name set but got an empty module name for z.fn should return None or an nn.Module but got )named_childrenadd
isinstancennModulesetattr)	r   r   r   child_module_namechild_moduleoptional_module_post_order_apply_innerr   visited_moduless	         m/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torch/distributed/fsdp/wrap.pyr%   z2_post_order_apply.<locals>._post_order_apply_inner-   s?   
 06/D/D/F/F 	Q 	Q+|?22##L111''6GPPP"V**&mRY77  0 0 0'-0 0     5,25 5   ory99  RRR   M;@@@@@ '&     N)r   r    strr   )r   r   r%   r&   s    `@@r'   _post_order_applyr+      s     (3mOA	AA  	*A A A A A A A A0 KT22222r(   target_module_to_kwargsfsdp_fnreturnc                 b     dt           j        dt          t           j                 f fd}|S )z
    This constructs the "wrap" function to pass to :func:`_post_order_apply`
    based on ``target_module_to_kwargs``, which should be constructed from the
    wrapping policy.
    r   r.   c                 :    | v r| ur|          } | fi |S d S N )r   kwargsr-   r   r,   s     r'   r   z_construct_wrap_fn.<locals>.fnS   sB     ,,,{1J1J,V4F76,,V,,,tr(   )r   r    r   )r   r,   r-   r   s   ``` r'   _construct_wrap_fnr4   H   sK    29 ")!4         Ir(   module_classesignored_modulesroot_kwargsc                     t          t          |                    }|                                 D ]+}||v rt          ||          r||vr|||<   d ||         d<   ,|S )Nmixed_precision)tuplesetmodulesr   )r   r5   r6   r7   r,   module_classes_tupler   s          r'   $_run_mixed_precision_override_policyr>   ^   s     !^!4!455%%'' F F_$$ 455 	F4442='/AE#F+,=>""r(   c                      dS )z
    A simple recursive wrap policy that always returns ``True``. This means
    that every submodule is wrapped by the wrapper class in
    :func:`_recursive_wrap`.
    Tr2   )argsr3   s     r'   r   r   r   s	     4r(   c                       e Zd ZdZedej        deej                 dee	e
f         deej        ee	e
f         f         fd            ZdS )_Policyzk
    This defines an abstract base class that represents a policy for applying
    a module-level API.
    r   r6   r7   r.   c                     dS )z
        This should return a dict ``target_module_to_kwargs`` that maps from
        each target module to wrap to its kwargs.
        Nr2   )selfr   r6   r7   s       r'   _run_policyz_Policy._run_policy   s	     	r(   N)__name__
__module____qualname____doc__r   r   r    r;   dictr*   r   rE   r2   r(   r'   rB   rB   {   s         
 
Y
 RY
 #s(^	

 
bic3h'	(
 
 
 ^
 
 
r(   rB   r   recursenonwrapped_numelc                 D    |rdS t          | t          |                    S )a   
    This auto wrap policy wraps every module that is an instance of any type in
    ``module_classes`` as its own FSDP instance. The root module given by
    ``module`` is always wrapped as an FSDP instance regardless. Since the
    wrapping proceeds bottom up, each FSDP instance manages the parameters in
    its subtree excluding any already managed by a child FSDP instance.

    Args:
        module (nn.Module): Current module being considered.
        recurse (bool): If ``False``, then this function must decide whether
            ``module`` should be wrapped as an FSDP instance or not. If
            ``True``, then the function is still recursing down the module
            tree as a part of the DFS.
        nonwrapped_numel (int): Parameter numel not yet wrapped.
        module_classes (Set[Type[nn.Module]]): Set of module classes that are
            wrapped as FSDP instances.

    Returns:
        ``True`` if ``recurse=True``, and whether ``module`` should be wrapped
        if ``recurse=False``.
    Tr   r:   )r   rK   rL   r5   s       r'   _module_wrap_policyrO      s)    6  tfeN33444r(   c                        e Zd ZdZdeeej                          fdZdej        de	ej                 de
eef         de
ej        e
eef         f         fdZd	 Zdef fd
Z xZS )r   z{
    This policy applies to every module of the specified module classes,
    passing in the kwargs given to the root.
    r5   c                 Z    t          |          }|| _        t          |          | _        d S r1   )r;   _module_classesr*   _module_classes_str)rD   r5   module_classes_sets      r'   __init__zModuleWrapPolicy.__init__   s/     001#&'9#:#:   r(   r   r6   r7   r.   c                     t          | j                  }i }|                                D ].}||v rt          ||          rt	          j        |          ||<   /|S r1   )r:   rR   r<   r   copy)rD   r   r6   r7   r5   r,   r   s          r'   rE   zModuleWrapPolicy._run_policy   st     t344CE!))++ 	I 	IF((FN33 I26)K2H2H'/&&r(   c                 2    t          ||d| j                  S )N)rL   r5   )rO   rR   )rD   r   rK   r@   r3   s        r'   __call__zModuleWrapPolicy.__call__   s%    "GbAU
 
 
 	
r(   c                 \    t                                                      d| j         dz   S )N())super__repr__rS   )rD   	__class__s    r'   r_   zModuleWrapPolicy.__repr__   s-    ww!!$C(@$C$C$CCCr(   )rF   rG   rH   rI   r   typer   r    rU   r;   rJ   r*   r   rE   rZ   r_   __classcell__)r`   s   @r'   r   r      s         
;xRY'@ ; ; ; ;
'Y' RY' #s(^	'
 
bic3h'	(' ' ' ' 
 
 
D# D D D D D D D D D Dr(   r   c                       e Zd ZdZdeej        geee	e
ef         f         f         fdZdej        deej                 de	e
ef         de	ej        e	e
ef         f         fdZd	S )
r   a  
    This policy takes in a lambda function that maps a given ``nn.Module`` to
    either ``False``, ``True``, or a kwarg dictionary.
    - If the function returns ``False`` or an empty dictionary, then the module
      does not have the API applied.
    - If the function returns ``True``, then the module has the API applied
      with the root's kwargs.
    - If the function returns a non-empty dictionary, then the module has the
      API applied, and the dictionary overrides the root's kwargs.

    Example::

        >>> # xdoctest: +SKIP("undefined variables")
        >>> model = init_transformer_model(...)
        >>> def lambda_fn(module: nn.Module):
        >>>     if module is model.lm_head:
        >>>         return {"sharding_strategy": ShardingStrategy.SHARD_GRAD_OP}
        >>>     elif isinstance(module, TransformerBlock):
        >>>         return True
        >>>     return False
        >>> policy = CustomPolicy(lambda_fn)
        >>> fsdp_model = FSDP(model, auto_wrap_policy=policy)
    	lambda_fnc                     || _         d S r1   )
_lambda_fn)rD   rd   s     r'   rU   zCustomPolicy.__init__   s    #r(   r   r6   r7   r.   c                 T   i }|                                 D ]}||v r|                     |          }t          |t          t          f          st          d|           |sMt          j        |          }t          |t                    r|                    |           |||<   |S )Nz_The lambda_fn passed to CustomPolicy should return False/True or a kwarg dict, but it returned )r<   rf   r   rJ   bool
ValueErrorrW   update)rD   r   r6   r7   r,   r   resr3   s           r'   rE   zCustomPolicy._run_policy   s     DF!))++ 	5 	5F((//&))CcD$<00  ICFI I    Y{++F#t$$ # c""".4#F++&&r(   N)rF   rG   rH   rI   r	   r   r    r   rh   rJ   r*   r   rU   r;   rE   r2   r(   r'   r   r      s         0$(BI;dDcN>R8S+S"T $ $ $ $'Y' RY' #s(^	'
 
bic3h'	(' ' ' ' ' 'r(   r   rd   c                      |rdS  ||           S )aU  
    A convenient auto wrap policy to wrap submodules based on an arbitrary user
    function. If `lambda_fn(submodule) == True``, the submodule will be wrapped as
    a `wrapper_cls` unit.

    Return if a module should be wrapped during auto wrapping.

    The first three parameters are required by :func:`_recursive_wrap`.

    Args:
        module (nn.Module): Current module being considered.
        recurse (bool): If ``False``, then this function must decide whether
            ``module`` should be wrapped as an FSDP instance or not. If
            ``True``, then the function is still recursing down the module
            tree as a part of the DFS.
        nonwrapped_numel (int): Parameter numel not yet wrapped.

        lambda_fn (Callable[[nn.Module], bool]): If this returns ``True``, then
            this module will be wrapped.
    Tr2   )r   rK   rL   rd   s       r'   r   r     s!    .  t9Vr(   transformer_layer_clsc                 &    t          | |||          S )a-  
    See :func:`_module_wrap_policy`, where ``transformer_layer_cls`` is the
    same as ``module_classes``. Note that shared parameters must be wrapped in
    the same FSDP instance, so this auto wrap policy can help wrap shared
    embeddings into the same FSDP instance for transformer models.
    )rO   )r   rK   rL   rm   s       r'   r   r   '  s     vw0@BWXXXr(   c                 D    |rdS t          | t          |                    S )NTrN   )r   r5   rK   r@   r3   s        r'   _wrap_module_cls_individuallyrp   6  s+      9t &%"7"7888r(   c                 B     t           fd|D                       S )zv
    A policy that wraps ``module`` if any policy in the passed in iterable of
    ``policies`` returns ``True``.
    c              3   4   K   | ]} |           V  dS )r   rK   rL   Nr2   ).0policyr   rL   rK   s     r'   	<genexpr>z_or_policy.<locals>.<genexpr>L  sI         	fg@PQQQ     r(   )any)r   rK   rL   policiess   ``` r'   
_or_policyry   B  sI                r(   g    חAmin_num_paramsforce_leaf_modulesexclude_wrap_modulesc                     |t           j        n|}|t           j        n|}|}||k    }|r |ot          | t	          |                     S |ot          | t	          |                     S )a  
    A size-based auto wrap policy.

    Args:
        module (nn.Module): Current module being considered.
        recurse (bool): If ``False``, then this function must decide whether
            ``module`` should be wrapped as an FSDP instance or not. If
            ``True``, then the function is still recursing down the module
            tree as a part of the DFS.
        nonwrapped_numel (int): Parameter numel not yet wrapped.

        min_num_params (int): Customizable policy input that controls the size
            threshold over which a module is ready to be wrapped. This is in
            units of numel.
        force_leaf_modules (Optional[set[type[nn.Module]]]): Set of module types to keep
            as leaves, i.e. their children will never be wrapped.
        exclude_wrap_modules (Optional[set[type[nn.Module]]]): Set of module types to be
            excluded in wrapping.

    Returns:
        Whether ``module`` should be wrapped.
    )r   FORCE_LEAF_MODULESEXCLUDE_WRAP_MODULESr   r:   )r   rK   rL   rz   r{   r|   min_nonwrapped_numelis_larges           r'   r   r   R  s    B % 	$66   ' 	$88!  *#77H PM
659K3L3L M MMM O
659M3N3N O OOOr(   wrapper_clswrapper_kwargs)NNNc              +   h   K   d| i|}t          di |5  dV  ddd           dS # 1 swxY w Y   dS )a  
    Context manager to wrap modules using a wrapper.

    Useful for when you'd like to apply the same configuration arguments to all
    child modules that you wrap. A particularly important use case is wrapping
    large layers so that they get sharded (in-place) during initialization, to
    avoid running out of system memory. Large layers can indicate that they
    should be sharded via the ``wrap`` annotation and this context manager can
    provide the exact configuration for these nested instances.

    Usage::

        with enable_wrap(wrapper_cls, **params):
            # Wraps layer in FSDP by default if within context
            self.l1 = wrap(torch.nn.Linear(5, 5))

    Args:
        wrapper_cls:
            Class that `wrap` annotation will `wrap` modules with, such as
            `FullyShardedDataParallel`.
        **wrapper_kwargs:
            Configuration settings that will be passed to all ``wrap``
            instances inside the context
    r   Nr2   )_ConfigAutoWrap)r   r   r3   s      r'   r   r     s      : 	{
F 
	"	"6	"	"                   s   '++wrap_overridesc                     t           j        r6t           j        J i t           j        |}t	          | t           j        fi |S | S )a  
    Annotate that a module should be wrapped. Annotated modules will only be
    wrapped if inside of an :func:`enable_wrap` context manager. This allows
    a module to be initialized both with and without a wrapper without code
    change.

    The class that this function wraps the passed in ``nn.Module`` with is the
    passed in ``wrapper_cls`` argument into ``enable_wrap``. Both
    ``enable_wrap`` and ``wrap`` can take in kwargs specifying how to construct
    the ``wrapper_cls`` instance. In the case of duplicate kwargs in
    ``enable_wrap`` and ``wrap``, the argument passed into ``wrap`` will be
    respected.

    Usage::

        with enable_wrap(wrapper_cls=FSDP, **fsdp_config):
            # Wraps layer in FSDP by default if within context
            self.l1 = wrap(torch.nn.Linear(5, 5))

    Args:
        module (nn.Module): module to wrap (if in :func:`enable_wrap` context)
        **wrap_overrides: configuration overrides that will take priority over
            the values provided by the :func:`enable_wrap` context
    )r   in_autowrap_contextr   r3   _wrap)r   r   s     r'   r   r     sa    2 * 
*666EO2EnE'
 
 
 
 	

 Mr(   c                 d    |J t          | d          ri || j        } || fi |S  || fi |S )N_wrap_overrides)hasattrr   )r   r   r3   	overridess       r'   r   r     se    """v()) 0
 9v8!78	{6//Y///;v(((((r(   Fauto_wrap_policyignored_paramsonly_wrap_childrenr3   c           
      N   |
J d            |
J d            |                                  D ]@\  }}||v r
	 t          |t          t          |                    rJ 1# t          $ r Y =w xY wt          fd|                                 D                       }	|J  || d|	          rud}
|                                 D ]5\  }}||v r
t          d
||||d|\  }}t          | ||           |
|z  }
6|	|
z
  }|s || d	|          rt          | |fi ||	fS | |
fS | dfS )a  
    Wraps submodules of ``module`` for which ``auto_wrap_policy`` returns
    ``True`` with ``wrapper_cls``.

    Args:
        module (nn.Module): Module to recursively wrap.
        auto_wrap_policy (Callable): A callable representing a policy that
            determines which modules to recursively wrap with ``wrapper_cls``.
        ignored_modules (set[torch.nn.Module]): Modules to ignore when
            wrapping.
        ignored_params (set[torch.nn.Parameter]): Parameters to ignore when
            wrapping; these should be the parameters contained in the modules
            in ``ignored_modules``.
    Returns:
        (nn.Module, int):
            ``module`` after wrapping and the numel recursively wrapped.
    NzMust specify auto_wrap_policy.zMust specify wrapper_clsc              3   H   K   | ]}|v|                                 V  d S r1   )numel)rt   pr   s     r'   rv   z"_recursive_wrap.<locals>.<genexpr>
  s>        !>2I2I		2I2I2I2I r(   Trs   r   )r   r   r   r6   r   Fr2   )named_modulesr   r
   ra   	TypeErrorsum
parametersr   _recursive_wrapr!   r   )r   r   r   r6   r   r   r3   _childrL   total_wrapped_numelnamewrapped_childnum_wrapped_params	remainders       `          r'   r   r     s   4 '')I'''""$>"""((**  5O##	!%dK)@)@AAAAAA 	 	 	D	
     !,,..     '''vtFVWWW /!0022 	6 	6KD%''0? 1!1' /-1 1 1 1-M- FD-000#55 %'::	! 	/&6&659'
 '
 '
 	/ 77779III...19s   %A
A,+A,c                       e Zd ZU dZdZeed<   dZee	         ed<   i Z
eeef         ed<   deeef         fdZededdfd	            Zedd
            ZddZdedededdfdZdS )r   z
    Helper class to wrap modules based on default config args via a context manager.
    See :func:`enable_wrap` for more information.
    Fr   Nr   r3   c                     || _         d S r1   r3   )rD   r3   s     r'   rU   z_ConfigAutoWrap.__init__7  s    r(   r.   c                     t           j        rt          d          dt           _        d|                                 v s
J d            t	          t
          | d                   t           _        | d= | t           _        d S )Nz]You are already within an autowrap context and we currently do not supported nested autowrap.Tr   z9Expected to pass in wrapper_cls arg into _ConfigAutoWrap.)r   r   NotImplementedErrorkeysr
   r	   r   r3   r   s    r'   enable_autowrap_contextz'_ConfigAutoWrap.enable_autowrap_context:  s    . 	%o   /3+---G .-- '+8VM5J&K&K#=!!'r(   c                  N    dt           _        d t           _        i t           _        d S )NF)r   r   r   r3   r2   r(   r'   disable_autowrap_contextz(_ConfigAutoWrap.disable_autowrap_contextJ  s     .3+&*#!#r(   c                 :    |                      | j                   d S r1   )r   r3   )rD   s    r'   	__enter__z_ConfigAutoWrap.__enter__P  s    $$T[11111r(   exc_typeexc_valexc_tbc                 .    |                                   d S r1   )r   )rD   r   r   r   s       r'   __exit__z_ConfigAutoWrap.__exit__S  s    %%'''''r(   )r.   N)rF   rG   rH   rI   r   rh   __annotations__r   r   r	   r3   rJ   r*   r   rU   staticmethodr   r   r   r   r2   r(   r'   r   r   -  s         
 !&%%%&*K(#***FDcNc3h     ( ( ( ( ( \( $ $ $ \$
2 2 2 2( (s (C (D ( ( ( ( ( (r(   r   )F)3
contextlibrW   abcr   r   collections.abcr   r   r   typingr   r	   r
   r   r   torch.nnr   __all__r    r+   rJ   r*   r4   ra   r;   r>   rh   r   rB   intrO   r   r   r   r   rp   ry   r   
ModuleList
ModuleDictr   MultiheadAttentionr~   contextmanagerr   r   r   	Parameterr:   r   r   r2   r(   r'   <module>r      s        # # # # # # # # 9 9 9 9 9 9 9 9 9 9 7 7 7 7 7 7 7 7 7 7 7 7 7 7      	 	 	&3&3")hry112&3 &3 &3 &3R!")T#s(^";<  ryk8BI../	   ,##T")_-# ^# c3h	#
 "")T#s(^";<# # # #(4        c   (5I55 5 RY(	5
 
5 5 5 5@"D "D "D "D "Dw "D "D "DJ4' 4' 4' 4' 4'7 4' 4' 4'nI $8;HP	   8YIYY Y tBI/	Y
 
Y Y Y Y	9I	9'/~	9@D	9 	9 	9 	9I 
 
   * #c((9=;?3P 3PI3P3P 3P
 3P !T")_!563P #3tBI#783P 
3P 3P 3P 3Pn 57M2=3Q  0242G1H  .   +.         F" "c "bi " " " "J
)") 
)( 
) 
) 
) 
) 
)&  %G GIGG G ^	G
 %G G G 29c>G G G GT'( '( '( '( '( '( '( '( '( '(r(   