
    )`i                         d Z ddlZddlmZmZmZmZ ddlZddlm	Z	 ddl
mZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZmZ  ej        e          Z G d 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.
    N)AnyListOptionalUnion   )compile_pipeline)
FusionRule)LegalizationErrorinfer_initial_typelegalize_processors)Op)LogitsProcessor)TaggedTensor
TensorType)CompileErrorValidityCheckc                   *   e Zd ZdZ	 	 	 	 ddee         dedee         deee	                  deee
                  f
d	Zd
efdZdeej        ef         ded
ej        fdZed
efd            Z	 	 ddeee	                  deee
                  d
dfdZdS )
LogitsPipeaR	  
    Provides a declarative way to build processing pipelines for LLM outputs.

    Parameters
    ----------
    processors : List[:class:`LogitsProcessor`]
        List of processors to apply in sequence.
    compile : bool, optional
        Whether to compile the pipeline with fusion optimizations. Default is True. LogitsPipe.compile() can be called to perform compilation after pipeline instantiation.
    input_type : Optional[TensorType], optional
        Expected input tensor type. It can be TensorType.LOGITS or TensorType.PROBS. It's required if the first processor can take both types. In other cases, it will be automatically inferred from the first processor.
        Default is None.
    custom_fusion_rules : Optional[List[FusionRule]], optional
        Additional fusion rules to apply during compilation. Default is None.
    custom_validity_checks : Optional[List[ValidityCheck]], optional
        Additional validity checks to apply during compilation. Default is None.

    Examples
    --------
    >>> import torch
    >>> from flashinfer.logits_processor import LogitsPipe, Temperature, Softmax, TopK, Sample
    >>> torch.manual_seed(42)
    >>>
    >>> # Basic pipeline with temperature, top-k, and sampling
    >>> pipe = LogitsPipe([
    ...     Temperature(),
    ...     Softmax(),
    ...     TopK(),
    ...     Sample(deterministic=True)
    ... ])
    >>>
    >>> # Execute the pipeline
    >>> logits = torch.randn(4, 32000, device="cuda")  # [batch_size, vocab_size]
    >>> token_ids = pipe(logits, temperature=0.9, top_k=40)
    >>> token_ids
    tensor([15806,  8154, 13923, 20311], device='cuda:0')
    >>>
    >>> # Pipeline starting from probabilities
    >>> from flashinfer.logits_processor import TensorType, TopK
    >>>
    >>> prob_pipe = LogitsPipe(
    ...     [TopK(), Sample()],
    ...     input_type=TensorType.PROBS
    ... )
    >>> probs = torch.softmax(logits, dim=-1)
    >>> token_ids = prob_pipe(probs, top_k=40)
    >>> token_ids
    tensor([  346, 14846,  1517,  9006], device='cuda:0')

    Notes
    -----
    - The pipeline automatically validates type compatibility between operations.
    - Operations are fused when possible
    - Runtime parameters (like temperature, top_k) are passed with pipe.call().
    - The output is always a plain torch.Tensor, not a :class:`~flashinfer.logits_processor.TaggedTensor`.
    TN
processorscompile
input_typecustom_fusion_rulescustom_validity_checksc                 `   |st          d          t          |          | _        	 |pt          | j                  | _        t          | j        | j                  | _        d| _        |r|                     ||           dS dS # t          t          f$ r}t          d|           |d}~ww xY w)z8
        Constructor for a :class:`LogitsPipe`.
        zPipeline cannot be emptyNzPipeline creation failed: )
ValueErrorlistr   r   _initial_typer   opscompiled_opsr   r
   r   )selfr   r   r   r   r   es          x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/flashinfer/logits_processor/pipeline.py__init__zLogitsPipe.__init__[   s      	97888z**	F!+!R/A$//R/RD +4?D<NOODH 59D J02HIIIIIJ J "<0 	F 	F 	F=!==>>AE	Fs   AB B-B((B-returnc                    d | j         D             }d | j        D             }| j        rd | j        D             ng }dd                    |           dd                    |           dd                    |           dS )	Nc                 &    g | ]}|j         j        S  	__class____name__).0procs     r"   
<listcomp>z'LogitsPipe.__repr__.<locals>.<listcomp>{   s    OOOt4>2OOO    c                 &    g | ]}|j         j        S r'   r(   r+   ops     r"   r-   z'LogitsPipe.__repr__.<locals>.<listcomp>|   s    ===bBL)===r.   c                 &    g | ]}|j         j        S r'   r(   r0   s     r"   r-   z'LogitsPipe.__repr__.<locals>.<listcomp>~   s    ???rR\"???r.   zLogitsPipe([z -> z], ops=[z], compiled_ops=[z]))r   r   r   join)r    processor_namesop_namescompiled_op_namess       r"   __repr__zLogitsPipe.__repr__z   s    OOtOOO==DH===  ??T->???? 	
 Ofkk/::  O  OFKKPXDYDY  O  Olrlwlw  yJ  mK  mK  O  O  O  	Or.   xkwargsc           
         | j         "t                              d           | j        }n| j         }t	          |t
                    r|}n>| j        t          j        k    rt          j	        |          }nt          j
        |          }t          |          }t          |          D ]D\  }}	  ||fi |}# t          $ r(}t          d| d|j        j         d|           |d }~ww xY w|j        S )Nz/Pipeline is not compiled, running discrete ops.zError executing operator z (z): )r   loggerwarningr   
isinstancer   r   r   PROBSprobslogitsdict	enumerate	Exceptionr   r)   r*   data)	r    r8   r9   r   tagged_tensorruntime_kwargsir1   r!   s	            r"   __call__zLogitsPipe.__call__   s    $NNLMMM(CC#Ca&& 	7MM!Z%555 , 21 5 5 , 3A 6 6fs^^ 	 	EAr "= C CN C C    RRRR\5JRRqRR 
 !!s   +	B55
C'?#C""C'c                     | j         S )z
        The initial input tensor type of the pipeline. It can be :attr:`TensorType.LOGITS` or :attr:`TensorType.PROBS`.
        )r   )r    s    r"   initial_typezLogitsPipe.initial_type   s    
 !!r.   c                     	 t          | j        ||          | _        dS # t          $ r}t	          d|           |d}~ww xY w)a  
        Compile the pipeline.

        Parameters
        ----------
        custom_fusion_rules : Optional[List[FusionRule]], optional
            Additional fusion rules to apply during compilation. Default is None.
        custom_validity_checks : Optional[List[ValidityCheck]], optional
            Additional validity checks to apply during compilation. Default is None.
        zCompilation failed: N)r   r   r   r   r   )r    r   r   r!   s       r"   r   zLogitsPipe.compile   si    	@ 0-/E! !D  	@ 	@ 	@7A7788a?	@s    
A<A)TNNN)NN)r*   
__module____qualname____doc__r   r   boolr   r   r	   r   r#   strr7   r   torchTensorr   r   rH   propertyrJ   r   r'   r.   r"   r   r   !   sz       7 7x +/:>@DF F)F F Z(	F
 &d:&67F !)m)< =F F F F>O# O O O O"u|\12">A"	" " " ": "j " " " X" ;?@D@ @%d:&67@ !)m)< =@ 
	@ @ @ @ @ @r.   r   )rN   loggingtypingr   r   r   r   rQ   compilerr   fusion_rulesr	   legalizationr
   r   r   r1   r   r   r   typesr   r   
validatorsr   r   	getLoggerr*   r;   r   r'   r.   r"   <module>r\      s5      - - - - - - - - - - - -  & & & & & & $ $ $ $ $ $ T T T T T T T T T T       ' ' ' ' ' ' + + + + + + + + 3 3 3 3 3 3 3 3		8	$	$[@ [@ [@ [@ [@ [@ [@ [@ [@ [@r.   