o
    Xi)                     @  sn   d dl mZ d dlmZmZmZ G dd dejZeg ej	ddd
 ej	dd	d
 ZeeZd
S )    )annotations)_fusion_utils	_ir_utilspatternc                      s<   e Zd ZdZd fddZdd	 ZdddZdd Z  ZS )BiasGeluFusionzFuses a Bias-Gelu pattern into a single BiasGelu operator.

    Attributes:
        contrib_op (bool): If True, matches the Gelu operator from the 'com.microsoft' domain.
            If False, matches the standard ONNX Gelu operator.
    namestr
contrib_opboolc                  s   t  | || _d S )N)super__init___contrib_op)selfr   r	   	__class__ ]/home/ubuntu/.local/lib/python3.10/site-packages/onnxscript/rewriter/ort_fusions/bias_gelu.pyr      s   
zBiasGeluFusion.__init__c                 C  s4   | ||}| jr|j|ddgdS |j|dgdS )Ncom.microsoftgelu)_domain_outputs)r   )Addr   Gelu)r   opinputbiasgelu_addr   r   r   r      s   zBiasGeluFusion.patternreturnpattern.MatchResultc                 K  sN   t  }| jd}|d ur|dkr|dS t|ds%|d|S |S )Napproximatetanhz@Gelu operator with 'approximate' set to 'tanh' is not supported.   zbias is not of shape 1D tensor)r   MatchResultproducer
attributes
get_stringfailr   has_rank)r   r   r   r   r   _check_resultr   r   r   r   check!   s   zBiasGeluFusion.checkc                 K  s   |j ||ddS )Nr   )r   )BiasGelu)r   r   r   r   r(   r   r   r   rewrite.   s   zBiasGeluFusion.rewrite)r   r   r	   r
   )r   r   )	__name__
__module____qualname____doc__r   r   r*   r,   __classcell__r   r   r   r   r      s    	
r   gelu_onnx_opF)r	   gelu_contrib_opTN)
__future__r   onnxscript.rewriterr   r   r   RewriteRuleClassBaser   RewriteRuleSetrulecommutebias_gelu_rulesapply_fusion_rulesfuse_bias_gelur   r   r   r   <module>   s   *