+
     °³jÝ  ã                   óÌ   € R t ^ RIHt ^ RIHtHtHt ^ RIt^RIH	t	H
t
 ]'       d   ]	! 4       '       d   ^ RIHt R R ltR R	 ltR
 R lt]
R R l4       t]
RR R ll4       tR# )z,
Needed utilities for torchao FP8 training.
)Úpartial)ÚTYPE_CHECKINGÚCallableÚOptionalN)Úis_torchao_availableÚtorchao_required)ÚFloat8LinearConfigc                óL   € V ^8„  d   QhR\         P                  P                  /# ©é   Úmodel©ÚtorchÚnnÚModule)Úformats   "ÚT/home/luca/screen-checking/.venv/lib/python3.14/site-packages/accelerate/utils/ao.pyÚ__annotate__r       s   € ÷ %ñ %¬¯©¯©ñ %ó    c                ó¨   € RRr!V P                  4        F9  w  r4\        V\        P                  P                  4      '       g   K1  Vf   TpTpK;  	  W3# )zà
Finds the first and last linear layer names in a model.

This is needed during FP8 to avoid issues with instability by keeping the first and last layers unquantized.

Ref: https://x.com/xariusrke/status/1826669142604141052
N)Únamed_modulesÚ
isinstancer   r   ÚLinear)r   Úfirst_linearÚlast_linearÚnameÚmodules   &    r   Úfind_first_last_linear_layersr       sP   € ð !% d�+Ø×+Ñ+Ö-‰ˆÜ�fœeŸh™hŸo™o×.Ô.ØÒ#Ø#�ØŠKñ	 .ð
 Ð$Ð$r   c                óR   € V ^8„  d   QhR\         R\        \         ,          R\        /# )r   ÚfqnÚlayers_to_filterÚreturn)ÚstrÚlistÚbool)r   s   "r   r   r   1   s%   € ÷ ñ ¤cð ¼TÄ#½Yð Ì4ñ r   c                óÎ   € \        V \        P                  P                  4      '       d3   V P                  ^,          ^ 8w  g   V P
                  ^,          ^ 8w  d   R# W9   d   R# R# )an  
A function which will check if `module` is:
- a `torch.nn.Linear` layer
- has in_features and out_features divisible by 16
- is not part of `layers_to_filter`

Args:
    module (`torch.nn.Module`):
        The module to check.
    fqn (`str`):
        The fully qualified name of the layer.
    layers_to_filter (`List[str]`):
        The list of layers to filter.
FT)r   r   r   r   Úin_featuresÚout_features)r   r   r    s   &&&r   Úfilter_linear_layersr(   1   sM   € ô �&œ%Ÿ(™(Ÿ/™/×*Ò*Ø×Ñ Õ" aÔ'¨6×+>Ñ+>ÀÕ+CÀqÔ+HÙØ
ÔÙÙr   c                ó0   € V ^8„  d   QhR\         R\        /# )r   r   r!   )r"   r$   )r   s   "r   r   r   H   s   € ÷ [ñ [´Sð [¼Tñ [r   c                ó8   € \        V 4      w  r#\        WW#.R7      # )av  
A filter function which will filter out all linear layers except the first and last.

<Tip>

    For stability reasons, we skip the first and last linear layers Otherwise can lead to the model not training or
    converging properly

</Tip>

Args:
    module (`torch.nn.Module`):
        The module to check.
    fqn (`str`):
        The fully qualified name of the layer.
©r    )r   r(   )r   r   r   r   s   &&  r   Ú#filter_first_and_last_linear_layersr,   H   s!   € ô" !>¸fÓ EÑ€LÜ ¸|Ð>YÔZÐZr   c                óL   € V ^8„  d   QhR\         P                  P                  /# r
   r   )r   s   "r   r   r   ^   s   € ÷ ñ œŸ™Ÿ™ñ r   c                 ól   € ^ RI Hp V P                  4        F  w  r#\        W14      '       g   K   R# 	  R# )é    )ÚFloat8LinearTF)Útorchao.float8.float8_linearr0   r   r   )r   r0   r   r   s   &   r   Úhas_ao_layersr2   ]   s-   € å9à×+Ñ+Ö-‰ˆÜ�f×+Ô+Úñ .ñ r   c                óˆ   € V ^8„  d   QhR\         P                  P                  R\        R,          R\        \        ,          /# )r   r   Úconfigr   Úmodule_filter_func)r   r   r   r   r   )r   s   "r   r   r   h   s>   € ÷ 'Zñ 'ZÜ�8‰8�?‰?ð'ZäÐ)Õ*ð'Zô !¤Õ*ñ'Zr   c                ól   € ^ RI Hp \        V 4      w  rEVf   \        \        WE.R7      pV! WVR7       R# )a  
Converts all `nn.Linear` layers in the model (except the first and last) to torchao's `Float8Linear` layer inplace.

Args:
    model (`torch.nn.Module`):
        The model to convert.
    config (`torchao.float8.Float8LinearConfig`, *optional*):
        The configuration for the FP8 training. Recommended to utilize
        `torchao.float8.recipe_name_to_linear_config` to generate this. In general, the default config should be
        sufficient (what is passed when set to `None`).
    module_filter_func (`Callable`, *optional*, defaults to `filter_linear_layers`):
        Optional function that must take in a module and layer name, and returns a boolean indicating whether the
        module should be converted to FP8. Defaults to `filter_linear_layers`. See it for an example.

Example:

```python
from accelerate.utils.ao import convert_model_to_fp8_ao
from accelerate import Accelerator

accelerator = Accelerator(

model = MyModel()
model.to(accelerator.device)
convert_to_float8_training(model)

model.train()
```
)Úconvert_to_float8_trainingNr+   )Úmodule_filter_fnr4   )Útorchao.float8r7   r   r   r(   )r   r4   r5   r7   r   r   s   &&&   r   Úconvert_model_to_fp8_aor:   g   s7   € õF :ä =¸eÓ DÑ€LØÒ!Ü$Ô%9È\ÐLgÔhÐÙ˜uÐRX×Yr   )NN)Ú__doc__Ú	functoolsr   Útypingr   r   r   r   Úimportsr   r   r1   r   r   r(   r,   r2   r:   © r   r   Ú<module>r@      sf   ðñõ ß 4Ñ 4ã ç ;÷ Ù×ÒÝCõ%õ"õ.[ð* ôó ðð ö'Zó ò'Zr   