o
    پi                     @  sp   d dl mZ d dlZd dlZd dlmZmZmZmZ d dl	Z	er&d dl
mZ G dd dZG dd dejZdS )	    )annotationsN)TYPE_CHECKINGOptionalSetType)ScheduleBatchc                   @  s   e Zd Zd(ddZed)ddZejd*ddZdd Zd+ddZd,ddZ	d-ddZ
d.ddZd/d d!Zd.d"d#Zd0d%d&Zd'S )1BatchedPenalizerOrchestrator
vocab_sizeintbatchr   
penalizersSet[Type['_BatchedPenalizer']]c                   s\   | _ t| _|j _ fdd|D  _d} j D ]
}| }||O }q| _d S )Nc                   s   i | ]}|| qS  r   ).0	Penalizerselfr   _/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/sampling/penaltylib/orchestrator.py
<dictcomp>   s    z9BatchedPenalizerOrchestrator.__init__.<locals>.<dictcomp>F)	r	   weakrefref
_batch_refdevicer   valuesprepare_if_requiredis_required)r   r	   r   r   r   	penalizerpen_is_requiredr   r   r   __init__   s   

z%BatchedPenalizerOrchestrator.__init__returnScheduleBatch | Nonec                 C     |   S N)r   r   r   r   r   r      s   z"BatchedPenalizerOrchestrator.batchvalueOptional[ScheduleBatch]c                 C  s&   |d u rdd | _ d S t|| _ d S )Nc                   S  s   d S r"   r   r   r   r   r   <lambda>&   s    z4BatchedPenalizerOrchestrator.batch.<locals>.<lambda>)r   r   r   )r   r#   r   r   r   r   #   s   c                 C  s   | j jS r"   )r   reqsr   r   r   r   r&   *      z!BatchedPenalizerOrchestrator.reqs
output_idstorch.Tensorc                 C  s    | j  D ]}|j|d qdS )z
        Feed the output tokens to the penalizers.

        Args:
            output_ids (torch.Tensor): The output tokens.
        r(   N)r   r   cumulate_output_tokens)r   r(   r   r   r   r   r+   -   s   z3BatchedPenalizerOrchestrator.cumulate_output_tokenslogitsc                 C  s   | j  D ]}|| qdS )a  
        Apply the penalizers to the logits.
        Note that it may apply the penalizers in-place.

        Args:
            logits (torch.Tensor): The logits to apply the penalizers to.

        Returns:
            torch.Tensor: The logits after applying the penalizers.
        N)r   r   apply)r   r,   r   r   r   r   r-   7   s   z"BatchedPenalizerOrchestrator.applykeep_indicesc                 C  sj   | j sdS t|dkr|   dS d}| j D ]}|  }||O }|r+|j|d q|  q|| _ dS )z
        Filter the penalizers based on the indices to keep in the batch.

        Args:
            keep_indices (torch.Tensor): Tensor of indices to keep in the batch.
        Nr   Fr.   )r   lenreleaser   r   filterteardown)r   r.   r   r   tmp_is_requiredr   r   r   r2   E   s   

z#BatchedPenalizerOrchestrator.filterNonec                 C  s2   | j  D ]}|  q| j   d| _d| _dS )zGRelease all penalizers and break references so GC can reclaim promptly.NF)r   r   r3   clearr   r   )r   r   r   r   r   r1   _   s
   


z$BatchedPenalizerOrchestrator.release'BatchedPenalizerOrchestrator'c                 C  s   | S r"   r   r   r   r   r   	__enter__i   s   z&BatchedPenalizerOrchestrator.__enter__c                 C  s   |    d S r"   )r1   )r   exc_typeexctbr   r   r   __exit__l   s   z%BatchedPenalizerOrchestrator.__exit__theirc                 C  s>   | j s|j sdS d| _ |j D ]\}}| j| | qdS )a  
        Merge the penalizers of another orchestrator into this one.

        Note that this function **must** be called _before_ self.batch.reqs is updated (filtered).
        Each unprepared penalizers would have to be prepared (creating tensors, etc.) first before merging.
        This step requires the original batch.reqs, before it gets merged with other batch.reqs.

        Args:
            their (BatchedPenalizerOrchestrator): The orchestrator to merge into this one.
        NT)r   r   itemsmerge)r   r=   r   their_penalizerr   r   r   r?   o   s   z"BatchedPenalizerOrchestrator.mergeN)r	   r
   r   r   r   r   )r   r    )r#   r$   r(   r)   r,   r)   r   r)   r.   r)   )r   r5   )r   r7   )r=   r7   )__name__
__module____qualname__r   propertyr   setterr&   r+   r-   r2   r1   r8   r<   r?   r   r   r   r   r      s    








r   c                   @  s   e Zd ZdZd1ddZed2ddZd3d
dZd3ddZdd Z	dd Z
dd Zd4ddZd5ddZd6ddZd7d d!Zejd3d"d#Zejd$d% Zejd4d&d'Zejd5d(d)Zejd6d*d+Zejd7d,d-Zejd.d/ Zd0S )8_BatchedPenalizerz4
    An abstract class for a batched penalizer.
    orchestratorr   c                 C  s   t || _d| _d S NF)r   r   _orchestrator_ref_is_prepared)r   rJ   r   r   r   r      s   
z_BatchedPenalizer.__init__r   c                 C  s   |   }|d u rtd|S )Nz7BatchedPenalizerOrchestrator has been garbage-collected)rL   RuntimeError)r   orchr   r   r   rJ      s   z_BatchedPenalizer.orchestratorboolc                 C  s   | j S r"   )rM   r   r   r   r   is_prepared   s   z_BatchedPenalizer.is_preparedc                 C  r!   r"   )_is_requiredr   r   r   r   r      r'   z_BatchedPenalizer.is_requiredc                 C  s   | j s|   d| _ d S d S )NT)rM   _preparer   r   r   r   prepare   s   
z_BatchedPenalizer.preparec                 C  s   |   r
|   dS dS )NTF)rR   rT   r   r   r   r   r      s   z%_BatchedPenalizer.prepare_if_requiredc                 C  s   |    d| _d S rK   )	_teardownrM   r   r   r   r   r3      s   
z_BatchedPenalizer.teardownr(   r)   c                 C     | j sd S | j|d d S )Nr*   )rM   _cumulate_output_tokensr   r(   r   r   r   r+         z(_BatchedPenalizer.cumulate_output_tokensr,   c                 C  rV   )N)r,   )rM   _applyr   r,   r   r   r   r-      rY   z_BatchedPenalizer.applyr.   c                 C  rV   )Nr/   )rM   _filterr   r.   r   r   r   r2      rY   z_BatchedPenalizer.filterr=   '_BatchedPenalizer'c                 C  s.   | j s|j sd S |   |  | | d S r"   )rM   rT   _merger   r=   r   r   r   r?      s
   z_BatchedPenalizer.mergec                 C     dS )zD
        Check if the penalizer is required to be prepared.
        Nr   r   r   r   r   rR         z_BatchedPenalizer._is_requiredc                 C  ra   )zn
        Prepare the penalizer.
        Usually, this is where the penalizer initializes its tensors.
        Nr   r   r   r   r   rS         z_BatchedPenalizer._preparec                 C  ra   )z
        Cumulate the output tokens.
        Orchestrator will call this function to feed the output tokens to the penalizer.
        Nr   rX   r   r   r   rW      rc   z)_BatchedPenalizer._cumulate_output_tokensc                 C  ra   )zq
        Apply the penalizer to the logits.
        Penalizers can modify the logits in-place if needed.
        Nr   r[   r   r   r   rZ      rc   z_BatchedPenalizer._applyc                 C  ra   )zn
        Filter the penalizer (tensors or underlying data) based on the indices to keep in the batch.
        Nr   r]   r   r   r   r\      rb   z_BatchedPenalizer._filterc                 C  ra   )z=
        Merge the penalizer with another penalizer.
        Nr   r`   r   r   r   r_      rb   z_BatchedPenalizer._mergec                 C  ra   )z)
        Teardown the penalizer.
        Nr   r   r   r   r   rU      rb   z_BatchedPenalizer._teardownN)rJ   r   )r   r   )r   rP   rA   rB   rC   )r=   r^   )rD   rE   rF   __doc__r   rG   rJ   rQ   r   rT   r   r3   r+   r-   r2   r?   abcabstractmethodrR   rS   rW   rZ   r\   r_   rU   r   r   r   r   rI      s8    

	





rI   )
__future__r   re   r   typingr   r   r   r   torch"sglang.srt.managers.schedule_batchr   r   ABCrI   r   r   r   r   <module>   s    u