o
    XiQ                     @  s   d Z ddlmZ ddlZddlZddlmZ ddl	m
Z
 ddlmZ ddlmZ G dd	 d	ejZG d
d deZG dd dejZG dd deZdddZdS )zlDoes the following transformation:
- Div(Clip(Add(x))) -> HardSigmoid
- Mul(HardSigmoid(x), x) -> HardSwish
    )annotationsN)pattern)MatchResult)is_singleton_value)RewriteRuleSetc                   @  s   e Zd ZdZdd
dZdS )_HardSigmoidFusionBasez=HardSwish requires constant values so we check in base class.xir.Valueclip_minclip_maxbiasdivisorreturnr   c                 C  sj   t  }t|ddds|dS t|ddds|dS t|ddds'|dS t|ddds3|d	S |S )
Ng        g-C6?)rtolz&Swish requires min value of 0 for clipg      @z&Swish requires max value of 6 for clipg      @zSwish requires bias value of 3z!Swish requires divisor value of 6)r   r   fail)selfopr   r
   r   r   r   check_result r   d/home/ubuntu/.local/lib/python3.10/site-packages/onnxscript/rewriter/rules/common/_fuse_hardswish.pycheck   s   	



z_HardSigmoidFusionBase.checkN)r   r	   r
   r	   r   r	   r   r	   r   r	   r   r   )__name__
__module____qualname____doc__r   r   r   r   r   r      s    r   c                   @  $   e Zd ZdZdd	d
ZdddZdS )HardSwishFusionzFuse Add(_, 3) + Clip<0, 6>(_) + Mul + Div(_, 6) into HardSwish

    In this case we can't make HardSigmoid fusion first. The Mul
    is placed before Div while HardSigmoid requires Add+Clip+Div.
    r   r	   r
   r   r   r   r   c                 C  s"   | || ||| }|| }|S NClipr   r   r   r
   r   r   r   outr   r   r   r   3   s   	zHardSwishFusion.patternc                 C  
   | |S r   	HardSwishr   r   r   r
   r   r   r   r   r   r   rewrite@   s   
	zHardSwishFusion.rewriteNr   r	   r
   r	   r   r	   r   r	   r   r	   r   r	   r   r   r   r   r   r&   r   r   r   r   r   ,   s    
r   c                   @  s.   e Zd ZdZdddZdd	d
ZdddZdS )HardSwishFusionFromHardSigmoidz:Fuse HardSigmoid<alpha=1/6, beta=0.5> + Mul into HardSwishr   r	   r   c                 C  s   |j |ddgd}|| }|S )NThardsigmoid_out)_allow_other_attributes_outputsHardSigmoid)r   r   r   r!   r   r   r   r   O   s   z&HardSwishFusionFromHardSigmoid.patternr*   r   c                 C  sZ   t  }| }|jdd}|jdd}t|ds |dS t|ds+|dS |S )NalphabetaUUUUUU?z9HardSigmoid alpha must be 1/6 to get fused into HardSwish      ?z8HardSigmoid beta must be 0.5 to get fused into HardSwish)r   producer
attributes	get_floatnpiscloser   )r   r   r   r*   r   hardsigmoidr/   r1   r   r   r   r   U   s   z$HardSwishFusionFromHardSigmoid.checkc                 C  r"   r   r#   )r   r   r   r*   r   r   r   r&   e   s   
z&HardSwishFusionFromHardSigmoid.rewriteN)r   r	   r   r	   )r   r	   r*   r	   r   r   )r   r	   r*   r	   r   r	   )r   r   r   r   r   r   r&   r   r   r   r   r)   L   s
    

r)   c                   @  r   )HardSigmoidFusionzIFuse HardSigmoid only for HardSwish hyper-parameters: alpha=1/6, beta=0.5r   r	   r
   r   r   r   r   c                 C  s   | || ||}|| }|S r   r   r    r   r   r   r   l   s   	zHardSigmoidFusion.patternc                 C  s   |j |dddS )Nr2   r3   )r/   r1   r-   r%   r   r   r   r&   y   s   	zHardSigmoidFusion.rewriteNr'   r(   r   r   r   r   r:   i   s    
r:   r   r   c                   C  s$   t t  t  t  gddS )z?Returns the rewrite rules for fusing HardSwish and HardSigmoid.T)commute)r   r   ruler:   r)   r   r   r   r   fuse_hardswish_rules   s   r=   )r   r   )r   
__future__r   numpyr7   onnx_irironnxscript.rewriterr   onnxscript.rewriter._basicsr   onnxscript.rewriter._ir_utilsr   !onnxscript.rewriter._rewrite_ruler   RewriteRuleClassBaser   r   r)   r:   r=   r   r   r   r   <module>   s    