o
    پiG                     @   sv   d dl Zd dlZd dlm  mZ d dlm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ZeZdS )	    N)DllmAlgorithm)
DllmConfig)LogitsProcessorOutput)ForwardBatch)ModelRunnerc                	       sN   e Zd Zdef fddZdededeee	j
B e	j
dB ef fdd	Z  ZS )
JointThresholdconfigc                    sP   t  | |jdd| _|jdd| _|jdd| _|jdd| _d S )N	thresholdg      ?edit_thresholdr   max_post_edit_steps   penalty_lambda)super__init__algorithm_configgetr	   r
   r   r   )selfr   	__class__ ]/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/dllm/algorithm/joint_threshold.pyr      s   zJointThreshold.__init__model_runnerforward_batchreturnNc           "         s  |j }|jj}|j| jk}| s|j|d d}|jg |jfS g g }t|D ]&}|| j	 }	|	| j	 }
|j|	|
 }|| jk}|
| 
|   q'tj|tj|d}tj|tj|d}d}| j	| j }t|D ]}| rt n|j|d d}|j|j}}d}t|D ]}|| rq|| j	 }	|	| j	 }
|j|	|
 }|j|	|
 }|| }| jdkr|d d }|dd d d f jd|d| j dd tj|dd	}ttjtj|dd	dt|dd
d}|| jk}| }t|}|rt||tj }|| j k}| stj!|dd\}}d||< n||  d7  < || | jkr/d||< q| | @ }|| j"k||k@ |@ }||B } |  sNd||< q||  || < d}qql|ri|j|d d}|j|j}}t#|j|df  fddt|D }!||!|fS )N)pp_proxy_tensors)dtypedeviceFr      add)reduce)dim)r!   index)kTc                    s    g | ]} || d f qS )Nr   ).0inext_token_ids
start_listr   r   
<listcomp>   s    z&JointThreshold.run.<locals>.<listcomp>)$
batch_size	input_idsr   mask_idanyforwardlogits_outputcan_run_graphrange
block_sizeappendsumitemtorchzerosint32boolr   allfull_logitsr   scatter_	unsqueezeargmaxsqueezegatherFsoftmax
zeros_likewherenpinfr	   topkr
   reshape)"r   r   r   r*   r   
mask_indexoutprompt_masksr%   block_start	block_endblock_input_idsprompt_maskpost_edit_stepsfinishedany_changed_in_last_stepmax_iterations_r/   can_run_cuda_graphcurr_input_idscurr_logitscurr_prompt_maskprev_idsxphas_maskmask_transfer_index
confidenceselect_index	edit_maskedit_transfer_indextransfer_indexnext_token_ids_listr   r&   r   run   s   








	



;
zJointThreshold.run)__name__
__module____qualname__r   r   r   r   tupler   r6   Tensorr9   rd   __classcell__r   r   r   r   r      s    r   )numpyrE   r6   torch.nn.functionalnn
functionalrA   sglang.srt.dllm.algorithm.baser   sglang.srt.dllm.configr   "sglang.srt.layers.logits_processorr   ,sglang.srt.model_executor.forward_batch_infor   &sglang.srt.model_executor.model_runnerr   r   	Algorithmr   r   r   r   <module>   s    