a
    hp                     @   sN   d dl Z d dlmZ d dlmZ dgZe jjee j	df e jjdddZ
dS )    N)constant_fold)freezing_passeslower_pt2e_quantized_to_x86.)modelexample_inputsreturnc                 C   sL   dd }dd }t jj| |dd|  }|| t|| t| |S )a  Lower a PT2E-qantized model to x86 backend.

    Args:
    * `model` (torch.fx.GraphModule): a model quantized by PT2E quantization flow.
    * `example_inputs` (tuple[torch.Tensor, ...]): example inputs for the model.

    Return:
    A GraphModule lowered to x86 backend.
    c                  S   s4   t j } t|  D ]}t jj|s| |= q| S )N)torchexportZdefault_decompositionslistkeysZ_exportutilsZ
_is_cia_op)Zdecomp_tablek r   _/var/www/html/assistant/venv/lib/python3.9/site-packages/torch/ao/quantization/pt2e/lowering.py_post_autograd_decomp_table   s
    
z@lower_pt2e_quantized_to_x86.<locals>._post_autograd_decomp_tablec              	   S   s   t jj}| j}|jD ]x}|j|jjkr||L |j	d }ddg}|j
|jj||fd}|| || W d    q1 s0    Y  q|  |   d S )Nr      )args)r   opsatengraphnodestargettdefaultZinserting_beforer   Zcall_functionZpermuteZreplace_all_uses_withZ
erase_nodeZlintZ	recompile)mr   gnodexdimsZ	perm_noder   r   r   _node_replace$   s    


*z2lower_pt2e_quantized_to_x86.<locals>._node_replaceT)strict)r   r	   Zexport_for_trainingZrun_decompositionsmoduler   r   )r   r   r   r   Zlowered_modelr   r   r   r      s    
)r   Z torch._inductor.constant_foldingr   Z+torch._inductor.fx_passes.freezing_patternsr   __all__ZfxZGraphModuletupleZTensorr   r   r   r   r   <module>   s   