
    Pi              
       \   d dl Z d dlmZ d dlmZmZmZmZ d dlZd dl	m
Z
 d dlmZmZ d dlmZ  e j        e          Ze                     e j                               ddde
j        dee
j        ge
j        f         d	eee
j        egef                  d
e
j        fdZdddde
j        d	eee
j        egef                  dee         d
e
j        fdZdeeef         dee         d
ee
j        egef         fdZde
j        dedee         d
efdZde
j        dedee         d
efdZdS )    N)partial)CallableListOptionalUnion)Float8LinearConfigFloat8LinearRecipeName)Float8Linearmodule_filter_fnmodulefrom_float_funcr   returnc                   t          | t          j                  r] | d          rOt          t	          |                                                     dk    rt          d|             |           S | }	 	 d	dt          j        dt          t                   dt          t          j                 ffd |           |S )
a  
    Generic function to swap linear layers in a module with a new type of linear layer.

    Note:
        If applied to a root-level nn.Linear, the module will not be modified in place
        and returned instead

    Args:
        module: Module to modify.
        from_float_func: Function that accepts a linear layer and returns a new type of linear layer.
        module_filter_fn: If specified, only the `torch.nn.Linear` subclasses that
            that pass the filter function will be swapped. The inputs to the
            filter function are the module instance, and the FQN.

    Returns:
     nn.Module: The modified module with swapped linear layers.
    N r   z1Does not support a root nn.Linear with children: r   cur_fqnparent_modulec                 b   |d}|                                  D ]"\  }}|dk    r|}n| d| } 
|||            #t          | t          j                  rV	 	| |          rJ|J d|               |           }|                    d          d         }t          |||           d S d S d S )Nr   .z(Linear root module should return early: )named_children
isinstancennLinearsplitsetattr)r   r   r   child_module_namechild_modulenew_fqnnew_linear_modulecur_module_namer   r   post_order_traversals           v/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchao/float8/float8_linear_utils.pyr"   z0swap_linear_layers.<locals>.post_order_traversal8   s   
 ?G/5/D/D/F/F 	@ 	@+|"}}+$::'8::  w????fbi(( 	G$(8(8(I(I$ ,,C6CC -,, !0 7 7%mmC004OM?4EFFFFF	G 	G$$    )NN)
r   r   r   lenlistchildrenAssertionErrorModuler   str)r   r   r   root_moduler"   s    `` @r#   swap_linear_layersr,      s   . &")$$ 	
 $4$4VR$@$@ tFOO%%&&''!++ LFLL   
 
 	
 K "&-1G G	G#G  	*G G G G G G G G4 %%%r$   )r   configr-   c                    t           j                            d           t                      fd}t	          | ||          S )a  
    Swaps `torch.nn.Linear` in `module` with `Float8Linear`.

    Args:
        module: Module to modify.
        module_filter_fn: If specified, only the `torch.nn.Linear` subclasses that
            that pass the filter function will be swapped. The inputs to the
            filter function are the module instance and the FQN.
        config (Float8LinearConfig): configuration for conversion to float8

    Returns:
     nn.Module: The modified module with swapped linear layers.
    z)torchao.float8.convert_to_float8_trainingNc                 0    t          j        |           S )N)r-   )r
   
from_float)mr-   s    r#   <lambda>z,convert_to_float8_training.<locals>.<lambda>m   s    <2	   r$   r   )torch_C_log_api_usage_oncer   r,   )r   r   r-   r0   s     ` r#   convert_to_float8_trainingr6   V   sd    & 
H  !LMMM~#%%   J
 )   r$   recipefilter_fqnsc                 J   t          | t                    rt          |           } | t          j        k    rt	          t
          |          S | t          j        k    rt	          t          |          S | t          j        k    rt          d|            t          d|            )a?  Returns function which automatically filters nn.Linear modules that meet at least one of the following criteria:

    1. Dims not divisible by 16 (hardware requirement for float8).
    2. Dim sizes below certain thresholds, which may result in worse performance.

    NOTE: the thresholds are simple heuristics based on performance testing, and may not be optimal
    for your model. For the best performance, we recommend defining your own module_filter_fn customized for
    your module, using the performance tables for the given float8 recipe here:
    https://github.com/pytorch/ao/tree/main/torchao/float8#performance). These benchmarks referenced for
    auto filtering layers were run on H100 GPUs, and may not be representative of other hardware.

    This is an experimental API, the design may change in the future.
    )r8   zUnsupported recipe: zInvalid recipe: )r   r*   r	   
TENSORWISEr   _auto_filter_for_tensorwiseROWWISE_auto_filter_for_rowwiseROWWISE_WITH_GW_HPNotImplementedError
ValueError)r7   r8   s     r#   _auto_filter_for_reciperA   y   s      &# 0'//'2222LLLL	)1	1	1/[IIII	)<	<	<!"A"A"ABBB4F44555r$   modfqnc                    t          | t          j                  sdS t          fd|D                       }|rdS | j        j        \  }}|dz  dk    o|dz  dk    }|sdS |dk    rdS |dk    rdS |dk    r|dk    rdS dS )	NFc              3       K   | ]}|v V  	d S N .0
filter_fqnrC   s     r#   	<genexpr>z+_auto_filter_for_rowwise.<locals>.<genexpr>   (      JJ
*+JJJJJJr$      r   i         Tr   r   r   anyweightshaperB   rC   r8   is_filtered_fqnNKdims_multiples_of_16s    `     r#   r=   r=      s    c29%% u JJJJkJJJJJO u :DAqr6Q;61r6Q; u 	Dyyu	
du	
dqDyyu4r$   c                     t          | t          j                  sdS t          fd|D                       }|rdS | j        j        \  }}|dz  dk    o|dz  dk    }|sdS |dk    r|dk    rdS dS )NFc              3       K   | ]}|v V  	d S rF   rG   rH   s     r#   rK   z._auto_filter_for_tensorwise.<locals>.<genexpr>   rL   r$   rM   r   rO   rN   TrP   rT   s    `     r#   r;   r;      s     c29%% u JJJJkJJJJJO u :DAqr6Q;61r6Q; u 	DyyQ$YYu4r$   )logging	functoolsr   typingr   r   r   r   r3   torch.nnr   torchao.float8.configr   r	   torchao.float8.float8_linearr
   	getLogger__name__log
addHandlerNullHandlerr)   r   r*   boolr,   r6   rA   r=   r;   rG   r$   r#   <module>rg      se          2 2 2 2 2 2 2 2 2 2 2 2        L L L L L L L L 5 5 5 5 5 5g!! "w"$$ % % % DH	? ? ?I?ryk2945? xC(8$(>?@	?
 Y? ? ? ?J DH+/	     I  xC(8$(>?@  '(	 
 Y       F6#--.6=A#Y6ry#$%6 6 6 68") # DI RV    8	+/9	     r$   