
    )`i'              
           d Z ddlmZmZ ddlmZmZ ddlmZ ddl	m
Z
 ddlmZmZmZ  G d d	          Z	 	 ddee         deee                  deee                  dee         fdZd
S )a3  
Copyright (c) 2025 by FlashInfer team.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    )ListOptional   )
FusionRuleget_default_fusion_rules)Op)
TensorType)CompileErrorValidityCheckget_default_validity_checksc                       e Zd ZddZdeddfdZdeddfdZdee	         dee	         fd	Z
dee	         ddfd
Zdee	         ddfdZdee	         dee	         fdZdee	         dedefdZddZdS )CompilerreturnNc                 J    g | _         g | _        |                                  d S N)fusion_rulesvalidity_checks_install_defaults)selfs    x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/flashinfer/logits_processor/compiler.py__init__zCompiler.__init__   s*    .046         rulec                 r    | j                             |           | j                             d            d S )Nc                     | j          S r   )prio)rs    r   <lambda>z/Compiler.register_fusion_rule.<locals>.<lambda>!   s
    afW r   )key)r   appendsort)r   r   s     r   register_fusion_rulezCompiler.register_fusion_rule   s=      &&&#4#455555r   checkc                 :    | j                             |           d S r   )r   r    )r   r#   s     r   register_validity_checkz Compiler.register_validity_check#   s    ##E*****r   opsc                     |st          d          t          |          }|                     |           |                     |           |                     |          }|S )Nz"Cannot compile empty operator list)r
   list_type_check_run_validity_checks	_fuse_all)r   r&   compiled_opss      r   compilezCompiler.compile&   sd     	ECDDDCyy&&&!!,///~~l33r   c                 H   |d         }|j         }|t          j        t          j        fvr$t	          d|j        j         d|j                    t          |          D ]D\  }}||j         k    r-t	          d| d|j        j         d| d|j          d| 
          |j        }Ed S )	Nr   zFirst operator (zZ) cannot accept standard pipeline inputs. Expected LOGITS or PROBS, but operator accepts: zType mismatch at operator z (z). Expected input type: z, but operator accepts: z. Previous operator output: )	INr	   LOGITSPROBSr
   	__class____name__	enumerateOUT)r   r&   first_opcurrent_typeiops         r   r)   zCompiler._type_check4   s   q6{
 1:3CDDDQ8#5#> Q QCK;Q Q  
 s^^ 	" 	"EArru$$"@ @ @bl6K @ @,8@ @RTRW@ @1=@ @   6LL	" 	"r   c                 0    | j         D ]} ||           d S r   )r   )r   r&   r#   s      r   r*   zCompiler._run_validity_checksI   s-    ) 	 	EE#JJJJ	 	r   c                    d}|t          |          k     rd}| j        D ]}t          |j                  }||z   t          |          k    r-||||z            }|                     ||j                  rL|                    |          r7|                    |          }|g||||z   <   t          |dz
  d          }d} n|s|dz  }|t          |          k     |S )Nr   Fr   T)lenr   pattern_pattern_matchesguardbuildmax)r   r&   r8   fusion_appliedr   spanwindowfused_ops           r   r+   zCompiler._fuse_allM   s    #c((ll"N)  4<((t8c#hh&&QT\*((>> 4::fCUCU #zz&11H)1
CAH%AE1A%)NE! Q) #c((ll, 
r   rD   r=   c                     t                    t                    k    rdS t          fdt          t                              D                       S )NFc              3   P   K   | ] }t          |         |                   V  !d S r   )
isinstance).0r8   r=   rD   s     r   	<genexpr>z,Compiler._pattern_matches.<locals>.<genexpr>k   s5      RR:fQi44RRRRRRr   )r<   allrange)r   rD   r=   s    ``r   r>   zCompiler._pattern_matchesg   sT    v;;#g,,&&5RRRRReCLL>Q>QRRRRRRr   c                     t                      D ]}| j                            |           t                      D ]}|                     |           d S r   )r   r   r    r   r"   )r   r#   r   s      r   r   zCompiler._install_defaultsm   sg    022 	/ 	/E ''....,.. 	, 	,D%%d++++	, 	,r   )r   N)r3   
__module____qualname__r   r   r"   r   r%   r   r   r-   r)   r*   r+   tupleboolr>   r    r   r   r   r      sT       ! ! ! !
6 6 6 6 6 6+] +t + + + +48 R    "tBx "D " " " "*R T    T"X $r(    4StBx S% SD S S S S, , , , , ,r   r   Nr&   custom_fusion_rulescustom_validity_checksr   c                     t                      }|r|D ]}|                    |           |r|D ]}|                    |           |                    |           S )a  
    Compile a pipeline of operators into a list of compiled operators.

    Parameters
    ----------
    ops : List[Op]
        List of operators to compile
    custom_fusion_rules : Optional[List[FusionRule]]
        List of custom fusion rules to use
    custom_validity_checks : Optional[List[ValidityCheck]]
        List of custom validity checks to use

    Returns
    -------
    List[Op]
        List of compiled operators
    )r   r"   r%   r-   )r&   rS   rT   compilerr   r#   s         r   compile_pipelinerW   u   s}    , zzH 0' 	0 	0D))$//// 4+ 	4 	4E,,U3333C   r   )NN)__doc__typingr   r   r   r   r   r9   r   typesr	   
validatorsr
   r   r   r   rW   rR   r   r   <module>r\      s     " ! ! ! ! ! ! ! > > > > > > > >             P P P P P P P P P PY, Y, Y, Y, Y, Y, Y, Y,| 7;<@ !  !	b !!$z"23 ! %T-%89 ! 
"X	 !  !  !  !  !  !r   