
    )`i                         d Z ddlmZ ddlmZ ddlmZ ddlmZ  G d de	          Z
ej        fd	ee         d
edee         fdZd	ee         defdZdedee         fdZd	ee         dd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   )Op)LogitsProcessor)
TensorTypec                       e Zd ZdS )LegalizationErrorN)__name__
__module____qualname__     |/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/flashinfer/logits_processor/legalization.pyr	   r	      s        Dr   r	   
processorsinitial_typereturnc           
      r   | st          d          g }|}t          |           D ]\  }}	 |                    |          }|st          d|j        j         d          |                    |           |d         j        }]# t          $ r(}t          d| d|j        j         d|           |d}~ww xY w|S )	a  
    Transform high-level LogitsProcessors into low-level Ops.

    This is the legalization stage that converts:
    Logits -> [TopK(), Sampling()]
    into:
    Logits -> [TopKLogitsOp(), SampleLogitsOp()]

    Args:
        processors: List of high-level processors
        initial_type: The initial input tensor type (LOGITS or PROBS)

    Returns:
        List of low-level Ops
    z$Cannot legalize empty processor list
Processor z produced no opszFailed to legalize processor z (z): N)r	   	enumeratelegalize	__class__r
   extendOUT	Exception)r   r   opscurrent_typei	processorlegalized_opses           r   legalize_processorsr"      s   $  H FGGG
CL!*--  9	%..|<<M  'O!4!=OOO   JJ}%%%(,0LL 	 	 	#YYYY5H5QYYVWYY 	
 Js   AB
B4#B//B4c                    | st           j        S | d         }t          |          }t          |          dk    rt	          d|j        j         d          t          |          dk    r|d         S t	          d|j        j         d          )Nr   r   zCannot infer input type: ze can accept both LOGITS and PROBS. Please specify input_type explicitly when creating the LogitsPipe.r   z9 cannot accept standard pipeline inputs (LOGITS or PROBS))r   LOGITS_get_supported_typeslenr	   r   r
   )r   first_processorvalid_typess      r   infer_initial_typer)   I   s     !   mO&77K
;!R(A(J R R R
 
 	

 ;11~
	_.7 	 	 	  r   r   c                     g }t           j        t           j        fD ]D}	 |                     |           |                    |           .# t
          t          f$ r Y Aw xY w|S )N)r   r$   PROBSr   append
ValueErrorr	   )r   r(   tensor_types      r   r%   r%   _   s     K"):+;<  	{+++{++++-. 	 	 	H	 s   *A		AANc                     | st          d          t          |           }	 t          | |           d S # t           $ r  t          $ r}t          d|           |d }~ww xY w)NzProcessor chain cannot be emptyz#Processor chain validation failed: )r	   r)   r"   r   )r   r   r!   s      r   validate_processor_chainr0   m   s     C ABBB%j11LRJ55555    R R R Ia I IJJPQQRs   4 A AA )__doc__typingr   opr   r   r   typesr   r   r	   r$   r"   r)   r%   r0   r   r   r   <module>r5      sQ                 ' ' ' ' ' '      	 	 	 	 		 	 	 	
 CMBS* *_%*5?*	"X* * * *Z4#8 Z    ,	*   Ro)> R4 R R R R R Rr   