§
    [�’j˜ ã                   ó(  — d dl Z d dlZd dlZd dlZd dlm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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mZ d
dlmZ d
dlmZ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( ddl)m*Z*m+Z+ ddl,m-Z-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4m5Z5m6Z6m7Z7 ddl8m9Z9m:Z:m;Z; ddl<m=Z=m>Z> 	  G d„ dej?        ¦  «        Z@ G d„ d ¦  «        ZA G d!„ d"¦  «        ZB G d#„ d$¦  «        ZC G d%„ d&¦  «        ZD G d'„ d(¦  «        ZEdS ))é    N)Úabstractmethod)ÚCallableÚ	Generator)ÚcontextmanagerÚnullcontext)Úperf_counter)ÚAny)Únn)Útqdm)Úlogging_redirect_tqdmé   )ÚPretrainedConfig)ÚContinuousBatchingConfigÚGenerationConfig)Úis_flash_attention_requested)Úis_flash_attn_2_availableÚis_flash_attn_3_available)Úloggingé   )ÚLogitsProcessorListé   )ÚPagedAttentionCache)Ú%ContinuousBatchingLogitsProcessorList)ÚDistributedHelper)Ú"resolve_continuous_batching_configÚ%update_cb_config_after_cache_creation)ÚContinuousBatchingAsyncIOsÚContinuousBatchingIOs)ÚModelRunner)ÚOffloadingManager)ÚGenerationOutputÚRequestStateÚRequestStatusÚlogger)ÚSCHEDULER_MAPPINGÚFIFOSchedulerÚ	Scheduler)ÚWorkloadHintsÚdrain_queuec                   ó‚   — e Zd ZU eed<   ej        ed<   ej        ed<   ede	ddfd„¦   «         Z
ededefd	„¦   «         ZdS )
ÚProtoPretrainedModelÚconfigÚdtypeÚdeviceÚattn_implementationÚreturnNc                 ó   — d S ©N© )Úselfr/   s     úhC:\Projects\DSCons\.venv\Lib\site-packages\transformers\generation\continuous_batching\continuous_api.pyÚset_attn_implementationz,ProtoPretrainedModel.set_attn_implementationK   ó   € àˆó    Úgeneration_configc                 ó   — d S r2   r3   )r4   r9   s     r5   Ú_get_logits_processorz*ProtoPretrainedModel._get_logits_processorO   r7   r8   )Ú__name__Ú
__module__Ú__qualname__r   Ú__annotations__Útorchr-   r.   r   Ústrr6   r   r   r;   r3   r8   r5   r+   r+   F   sš   € € € € € € ØÐÐÑØŒ;ÐÐÑØŒLÐÐÑàð¸3ð À4ð ð ð ñ „^ðð ðÐ7Gð ÐL_ð ð ð ñ „^ðð ð r8   r+   c                   óF   — e Zd ZdZd	d„Zdeddfd„Zdee         ddfd„ZdS )
ÚOutputRoutera  Dedicated object for routing generation outputs to the right destination.

    When an async handler is registered for a request, the output is forwarded
    to that handler via ``call_soon_threadsafe``. Otherwise the output is placed
    on the shared ``output_queue``.
    r0   Nc                 ót   — t          j        ¦   «         | _        i | _        t	          j        ¦   «         | _        d S r2   )ÚqueueÚQueueÚoutput_queueÚresult_handlersÚ	threadingÚLockÚ_lock©r4   s    r5   Ú__init__zOutputRouter.__init__\   s+   € Ý!œK™MœMˆÔØVXˆÔÝ”^Ñ%Ô%ˆŒ
ˆ
ˆ
r8   Úoutputc                 óô   — | j         5  | j                             |j        ¦  «        }ddd¦  «         n# 1 swxY w Y   |�|\  }}|                     ||¦  «         dS | j                             |¦  «         dS )zDRoute a single output to its registered handler or the output_queue.N)rK   rH   ÚgetÚ
request_idÚcall_soon_threadsaferG   Úput)r4   rN   ÚentryÚcallbackÚloops        r5   ÚdeliverzOutputRouter.delivera   sÑ   € àŒZð 	@ð 	@ØÔ(×,Ò,¨VÔ->Ñ?Ô?ˆEð	@ð 	@ð 	@ñ 	@ô 	@ð 	@ð 	@ð 	@ð 	@ð 	@ð 	@øøøð 	@ð 	@ð 	@ð 	@àÐØ"‰NˆH�dØ×%Ò% h°Ñ7Ô7Ð7Ð7Ð7àÔ×!Ò! &Ñ)Ô)Ð)Ð)Ð)s   ˆ 4´8»8Úoutputsc                 óL  — g }d}| j         5  |D ]Z}| j                             |j        ¦  «        }|�|\  }}|                     ||f¦  «         Œ@| j                             |¦  «         Œ[	 ddd¦  «         n# 1 swxY w Y   |r|�|fd„}|                     |¦  «         dS dS dS )zÅRoute a batch of outputs, using a single ``call_soon_threadsafe`` to minimize cross-thread overhead.

        Outputs without a registered handler fall back to the shared ``output_queue``.
        Nc                 ó,   — | D ]\  }} ||¦  «         Œd S r2   r3   )ÚbatchÚcbÚouts      r5   Ú
_run_batchz.OutputRouter.deliver_batch.<locals>._run_batch|   s.   € Ø$ð ð ‘G�B˜Ø�B�s‘G”G�G�Gðð r8   )rK   rH   rP   rQ   ÚappendrG   rS   rR   )r4   rX   Ú	callbacksrV   rN   rT   rU   r^   s           r5   Údeliver_batchzOutputRouter.deliver_batchk   s0  € ð
 >@ˆ	ØˆØŒZð 	2ð 	2Ø!ð 2ð 2�ØÔ,×0Ò0°Ô1BÑCÔC�ØÐ$Ø%*‘N�H˜dØ×$Ò$ h°Ð%7Ñ8Ô8Ð8Ð8àÔ%×)Ò)¨&Ñ1Ô1Ð1Ð1ð2ð	2ð 	2ð 	2ñ 	2ô 	2ð 	2ð 	2ð 	2ð 	2ð 	2ð 	2øøøð 	2ð 	2ð 	2ð 	2ð ð 	2˜Ð)à!*ð ð ð ð ð ×%Ò% jÑ1Ô1Ð1Ð1Ð1ð	2ð 	2Ð)Ð)s   ŒAA7Á7A;Á>A;©r0   N)	r<   r=   r>   Ú__doc__rM   r!   rW   Úlistra   r3   r8   r5   rC   rC   T   s}   € € € € € ðð ð&ð &ð &ð &ð
*Ð.ð *°4ð *ð *ð *ð *ð2 TÐ*:Ô%;ð 2Àð 2ð 2ð 2ð 2ð 2ð 2r8   rC   c                   ó–   — e Zd ZdZdZdZdZdZdd„Zdd	„Z	d
e
de
ddfd„Zdd„Zde
ddfd„Zede
fd„¦   «         Zede
fd„¦   «         ZdS )ÚBackgroundThreadStatusz¦Tracks the status of the background thread locally and in its TP group. The status is an int that can only
    increase, representing how soon the thread should stop.r   r   r   r   r0   Nc                 óf   — t          j        ¦   «         | _        | j        | _        | j        | _        d S r2   )rI   rJ   Ú_local_status_lockÚ	DONT_STOPÚ_local_statusÚ
_tp_statusrL   s    r5   rM   zBackgroundThreadStatus.__init__Œ   s)   € Ý"+¤.Ñ"2Ô"2ˆÔØ!œ^ˆÔØœ.ˆŒˆˆr8   c                 óv   — | j         | _        | j        5  | j         | _        ddd¦  «         dS # 1 swxY w Y   dS )z‹Clear the local and TP statuses. This method should ONLY be called by the main thread itself BEFORE starting
        the background thread.N)ri   rk   rh   rj   rL   s    r5   ÚclearzBackgroundThreadStatus.clear‘   s„   € ð œ.ˆŒØÔ$ð 	0ð 	0Ø!%¤ˆDÔð	0ð 	0ð 	0ñ 	0ô 	0ð 	0ð 	0ð 	0ð 	0ð 	0ð 	0ð 	0øøøð 	0ð 	0ð 	0ð 	0ð 	0ð 	0s   ”.®2µ2ÚstatusÚglobal_rankc                 ó  — || j         | j        fvrt          d|› d|› �¦  «        ‚| j        5  t	          || j        | j        ¦  «        | _        ddd¦  «         n# 1 swxY w Y   t          j        d|› d|›d| j        ›�¦  «         dS )zRequest the background thread to stop. This does not take effect immediately, only after the TP group has
        communicated.zInvalid stop status z from rank NzRank z3 requested background thread to stop with status = z. Now self._local_status = )	ÚFLUSH_AND_STOPÚ	HARD_STOPÚ
ValueErrorrh   Úmaxrj   rk   r$   Úinfo)r4   rn   ro   s      r5   Úrequest_stopz#BackgroundThreadStatus.request_stop˜   sý   € ð ˜$Ô-¨t¬~Ð>Ð>Ð>ÝÐT°FÐTÐTÀ{ÐTÐTÑUÔUÐUØÔ$ð 	Rð 	RÝ!$ V¨TÔ-?ÀÄÑ!QÔ!QˆDÔð	Rð 	Rð 	Rñ 	Rô 	Rð 	Rð 	Rð 	Rð 	Rð 	Rð 	Røøøð 	Rð 	Rð 	Rð 	RåŒØs�KÐsÐsÈ6ÐsÐsÐ\`Ô\nÐsÐsñ	
ô 	
ð 	
ð 	
ð 	
s   ­!AÁAÁ!Ac                 ó^   — | j         5  | j        | _        ddd¦  «         dS # 1 swxY w Y   dS )zzMark the background thread as stopped. This should be called by the main thread when the generation loop
        finishes.N)rh   ÚSTOPPEDrj   rL   s    r5   Úmark_as_stoppedz&BackgroundThreadStatus.mark_as_stopped£   s{   € ð Ô$ð 	.ð 	.Ø!%¤ˆDÔð	.ð 	.ð 	.ñ 	.ô 	.ð 	.ð 	.ð 	.ð 	.ð 	.ð 	.ð 	.øøøð 	.ð 	.ð 	.ð 	.ð 	.ð 	.s   ˆ"¢&©&Ú	tp_statusc                 óÒ   — || j         k     rt          d|›d| j         ›�¦  «        ‚|| _         | j        5  t          | j        |¦  «        | _        ddd¦  «         dS # 1 swxY w Y   dS )z8Update the local and TP statuses with the new TP status.z1TP communicated a lower stop status: tp_status = z, self._tp_status = N)rk   rs   rh   rt   rj   )r4   rz   s     r5   Úupdate_with_tp_statusz,BackgroundThreadStatus.update_with_tp_status©   sÏ   € à�t”Ò&Ð&ÝÐiÀYÐiÐiÐUYÔUdÐiÐiÑjÔjÐjØ#ˆŒàÔ$ð 	Dð 	DÝ!$ TÔ%7¸Ñ!CÔ!CˆDÔð	Dð 	Dð 	Dñ 	Dô 	Dð 	Dð 	Dð 	Dð 	Dð 	Dð 	Dð 	Døøøð 	Dð 	Dð 	Dð 	Dð 	Dð 	Ds   ´AÁA Á#A c                 ó   — | j         S )zVThe locally requested status, possibly ahead of the value agreed upon by the TP group.)rj   rL   s    r5   Úlocal_statusz#BackgroundThreadStatus.local_status²   s   € ð Ô!Ð!r8   c                 ó   — | j         S )zKThe status last agreed upon by the TP group through a MAX-reduce operation.)rk   rL   s    r5   rz   z BackgroundThreadStatus.tp_status·   s   € ð ŒÐr8   rb   )r<   r=   r>   rc   ri   rq   rr   rx   rM   rm   Úintrv   ry   r|   Úpropertyr~   rz   r3   r8   r5   rf   rf   ƒ   s  € € € € € ð?ð ?ð €IØ€NØ€IØ€Gð)ð )ð )ð )ð
0ð 0ð 0ð 0ð	
 3ð 	
°Sð 	
¸Tð 	
ð 	
ð 	
ð 	
ð.ð .ð .ð .ðD¨sð D°tð Dð Dð Dð Dð ð"˜cð "ð "ð "ñ „Xð"ð ð˜3ð ð ð ñ „Xðð ð r8   rf   c                   ó˜  — e Zd ZU eez  ed<   eed<   dedede	de
dedej        d	z  d
ej        d	z  dededej        dej        dededd	fd„Zdefd„Zd!d„Zd!d„Zdefd„Zdededd	fd„Zdefd„Zd!d„Zdefd„Z d„ Z!dedd	fd„Z" ej#        ¦   «         de$j%        dd	fd„¦   «         Z& ej#        ¦   «         de$j%        dd	fd „¦   «         Z'd	S )"ÚContinuousBatchProcessorÚinputs_and_outputsÚ	schedulerÚcacher,   r9   Úcontinuous_batching_configÚlogit_processorÚinput_queueNÚcancel_queueÚoutput_routerÚbackground_thread_statusÚmodel_deviceÚmodel_dtypeÚdistributed_helperr0   c                 óð  — || _         || _        || _        || _        || _        || _        || _        |	| _        |
| _        || _	        || _
        || _        t          |dd¦  «        | _        |j        | _        | j                             |j        |
¦  «         t          |dd¦  «        €dn|j        | _        |j        | _        ||||
|| j        dœ}| j        j        | _        | j        rt)          d	i |¤Ž| _        nt-          d	i |¤Ž| _        t/          |||j        |j        | j        j        | j        ¬¦  «        | _        t9          | j        | j        | j         | j        | j        | j        ¬¦  «        | _        dS )
a)  Initialize the continuous batch processor.

        Args:
            cache: A [`PagedAttentionCache`] object
            config: The model configuration
            generation_config: The generation configuration
            continuous_batching_config: The continuous batching configuration
            logit_processor: The [`ContinuousBatchingLogitsProcessorList`] object used to process the logits.
            input_queue: Queue for incoming requests. Is None if this process is not a TP driver.
            cancel_queue: Queue for cancellation request_ids. Is None if this process is not a TP driver.
            output_router: An [`OutputRouter`] object that routes outputs to handlers or the output queue.
            background_thread_status: A [`BackgroundThreadStatus`] object to track the background thread status.
            model_device: Device for model inputs/outputs
            model_dtype: Data type for model inputs/outputs
            scheduler: The [`Scheduler`] to use
            distributed_helper: The [`DistributedHelper`] to use
        Ú	do_sampleTÚsliding_windowNr   )r†   r,   r‡   r.   rŽ   rˆ   )r†   r…   Úcpu_offload_space_gibÚsafety_thresholdÚcompute_streamr�   )rˆ   Ú	cb_configr†   r„   r‘   Úreturn_logprobsr3   )r†   r,   r–   rˆ   r‰   rŠ   r‹   rŒ   r�   rŽ   r…   r�   Úgetattrr‘   r—   Úset_tp_seedÚseedr’   Úmax_batch_tokensÚuse_async_batchingr   r„   r   r    Úcpu_offload_spaceÚ"cpu_offload_space_safety_thresholdr•   Úoffloading_managerr   Úmodel_runner)r4   r†   r,   r9   r‡   rˆ   r‰   rŠ   r‹   rŒ   r�   rŽ   r…   r�   Ú	io_kwargss                  r5   rM   z!ContinuousBatchProcessor.__init__Â   s·  € ðB ˆŒ
ØˆŒØ3ˆŒØ.ˆÔØ&ˆÔØ(ˆÔØ*ˆÔØ(@ˆÔ%Ø(ˆÔØ&ˆÔØ"ˆŒØ"4ˆÔõ !Ð!2°KÀÑFÔFˆŒØ9ÔIˆÔð 	Ô×+Ò+Ð,FÔ,KÈ\ÑZÔZÐZõ $+¨6Ð3CÀTÑ#JÔ#JÐ#R˜a˜aÐX^ÔXmˆÔà %Ô 6ˆÔð ØØ*DØ"Ø&Ø#Ô3ð
ð 
ˆ	ð #'¤.Ô"CˆÔàÔ"ð 	IÝ&@Ð&MÐ&MÀ9Ð&MÐ&MˆDÔ#Ð#å&;Ð&HÐ&H¸iÐ&HÐ&HˆDÔ#õ #4ØØØ"<Ô"NØ7ÔZØÔ2ÔAØ#Ô6ð#
ñ #
ô #
ˆÔõ (Ø Ô0Ø”nØ”*Ø#Ô6Ø”nØ Ô0ð
ñ 
ô 
ˆÔÐÐr8   c                 ó¤   — d| j         › d| j        j        › d| j        j        › d�| j                             ¦   «                              ¦   «         z   S )Nz%ContinuousBatchProcessor(input_queue=z, active_requests=z, waiting_requests=ú))r‰   r…   Úactive_requestsÚwaiting_requestsr„   Úget_model_kwargsÚ__repr__rL   s    r5   r§   z!ContinuousBatchProcessor.__repr__   sq   € ðu°DÔ4Dð uð uØ#œ~Ô=ðuð uØRVÔR`ÔRqðuð uð uàÔ%×6Ò6Ñ8Ô8×AÒAÑCÔCñDð	
r8   c                 ó¶   — d | _         t          j        ¦   «          t          j                             ¦   «         r t          j                             ¦   «          d S d S r2   )r„   ÚgcÚcollectr@   ÚcudaÚis_availableÚempty_cacherL   s    r5   Ú__del__z ContinuousBatchProcessor.__del__'  sO   € Ø"&ˆÔÝ
Œ
‰ŒˆÝŒ:×"Ò"Ñ$Ô$ð 	%ÝŒJ×"Ò"Ñ$Ô$Ð$Ð$Ð$ð	%ð 	%r8   c                 óÎ   — | j                              ¦   «          | j                             ¦   «          | j                             ¦   «          | j                             ¦   «          dS )z4Reset the batch processor for a new generation loop.N)rŸ   Úresetr…   r„   r†   Úfree_all_requestsrL   s    r5   r°   zContinuousBatchProcessor.reset-  sZ   € àÔ×%Ò%Ñ'Ô'Ð'ØŒ×ÒÑÔÐØÔ×%Ò%Ñ'Ô'Ð'ØŒ
×$Ò$Ñ&Ô&Ð&Ð&Ð&r8   c                 ó$  — | j         �0| j        �)t          | j         ¦  «        t          | j        ¦  «        f}ng g f}t          |d         ¦  «        t          |d         ¦  «        z   }| j        j        }| j                             ||¦  «        \  }}| j                             |¦  «         | j        j	        t          j        k    rdS |dk    rdS | j                             |¦  «        \  }}|D ]‚}	 | j                             |j        ¦  «         | j                             |¦  «         Œ=# t$          $ r9}t'          j        d|› �d¬¦  «         |                      ||¦  «         Y d}~Œ{d}~ww xY w|D ]}	| j                             |	¦  «         ŒdS )zïCommunicates with the TP group to get A. the new requests and cancellations from the TP driver, and B. an
        eventual stop signal from any process in the TP group. Returns True if the TP group is hard-stopping, False
        otherwiseNr   r   TFzError processing new request: ©Úexc_info)r‰   rŠ   r)   ÚlenrŒ   r~   r�   Útp_all_reduce_stater|   rz   rf   rr   Útp_broadcast_object_from_rank_0rˆ   Úcheck_kwargsÚlogit_processor_kwargsr…   Úadd_waiting_requestÚ	Exceptionr$   ÚerrorÚ_handle_request_errorÚset_request_cancellation)
r4   ÚpayloadÚpayload_sizeÚlocal_requested_statusrz   Ú
new_statesÚcancellationsÚstateÚerQ   s
             r5   Ú_update_tp_group_statez/ContinuousBatchProcessor._update_tp_group_state4  sÅ  € ð
 ÔÐ'¨DÔ,=Ð,IÝ" 4Ô#3Ñ4Ô4µkÀ$ÔBSÑ6TÔ6TÐUˆGˆGà˜2�hˆGå˜7 1œ:‘”­¨W°Q¬Z©¬Ñ8ˆð "&Ô!>Ô!KÐØ"&Ô"9×"MÒ"MÈlÐ\rÑ"sÔ"sÑˆ�iàÔ%×;Ò;¸IÑFÔFÐFð Ô(Ô2Õ6LÔ6VÒVÐVØ�4à˜1ÒÐØ�5à$(Ô$;×$[Ò$[Ð\cÑ$dÔ$dÑ!ˆ
�Mð  ð 	5ð 	5ˆEð5ØÔ$×1Ò1°%Ô2NÑOÔOÐOØ”×2Ò2°5Ñ9Ô9Ð9Ð9øÝð 5ð 5ð 5Ý”ÐA¸aÐAÐAÈDÐQÑQÔQÐQØ×*Ò*¨1¨eÑ4Ô4Ð4Ð4Ð4Ð4Ð4Ð4øøøøð5øøøð (ð 	@ð 	@ˆJØŒN×3Ò3°JÑ?Ô?Ð?Ð?Øˆus   Ã19D+Ä+
E.Ä5/E)Å)E.r¼   rÄ   c                 ó4  — t           j        |_        t          |¦  «        |_        t          |j        t          ¦  «        r%| j                             |j        ¦  «        |_	        ng |_	        | j
                             |                     ¦   «         ¦  «         dS )z(Handle general request processing error.N)r#   ÚFAILEDrn   rA   r¼   Ú
isinstancerQ   r…   Ú!get_active_request_static_outputsÚgenerated_tokensr‹   rW   Úto_generation_output)r4   r¼   rÄ   s      r5   r½   z.ContinuousBatchProcessor._handle_request_error[  s€   € å$Ô+ˆŒÝ˜%‘j”jˆŒõ �eÔ&­Ñ,Ô,ð 	(Ø%)¤^×%UÒ%UÐV[ÔVfÑ%gÔ%gˆEÔ"Ð"à%'ˆEÔ"àÔ×"Ò" 5×#=Ò#=Ñ#?Ô#?Ñ@Ô@Ð@Ð@Ð@r8   c                 óF  — |                       ¦   «         }|rdS | j                             ¦   «         }|D ]}| j                             |¦  «         Œ| j                             ¦   «         sdS | j                             | j        | j        j	        ¦  «        \  }}}}|€]| j         
                    ¦   «         dk    rt          d¦  «        ‚| j                             | j        | j        j	        ¦  «        \  }}}}|®]|sdS | j        j        r| j         
                    ¦   «          | j                             |¦  «         t          j        t           j        ¦  «        rxt          j        dt'          |¦  «        › dt'          | j        j        ¦  «        › dt'          | j        j        ¦  «        › d|› d	|› d
| j                             ¦   «         › �¦  «         | j                             |||¦  «        \  }}| j                             || j        |||| j        j        ¬¦  «         dS )z…Prepare tensors and metadata for the next model forward pass. Returns True if there are requests to process,
        False otherwise.FNr   z>No requests can be scheduled and no requests can be offloaded.zScheduled: z, Waiting: z
, Active: z	. cum Q: z
. cum KV: z, free blocks: )Úrequests_in_batchÚlogits_processorsÚuse_decode_fast_pathÚnum_q_tokensÚmax_kv_readÚuse_paddingT)rÆ   r…   Úclear_cancelled_requestsrŸ   Úfree_request_cpu_cacheÚhas_pending_requestsÚschedule_batchr›   r†   Ú	num_pagesÚoffload_requestsÚRuntimeErrorÚstarved_requestsÚrestore_scheduled_requestsr$   ÚisEnabledForr   ÚDEBUGÚdebugrµ   r¥   r¤   Úget_num_free_blocksr    Úmaybe_pad_inputsr„   Úprepare_batch_tensorsrˆ   Ú
pad_inputs)r4   Úhard_stoppingÚcancelled_statesrÄ   rÎ   rÐ   rÑ   rÒ   s           r5   Úprepare_next_batchz+ContinuousBatchProcessor.prepare_next_batchh  s†  € ð
 ×3Ò3Ñ5Ô5ˆØð 	Ø�5àœ>×BÒBÑDÔDÐà%ð 	Bð 	BˆEØÔ#×:Ò:¸5ÑAÔAÐAÐAØŒ~×2Ò2Ñ4Ô4ð 	Ø�5ð NRÌ^×MjÒMjØÔ! 4¤:Ô#7ñN
ô N
ÑJÐÐ/°¸{ð  Ð'àÔ&×7Ò7Ñ9Ô9¸QÒ>Ð>Ý"Ð#cÑdÔdÐdàQUÔQ_×QnÒQnØÔ% t¤zÔ';ñRô RÑNÐÐ3°\À;ð  Ð'ð !ð 	Ø�5àŒ>Ô*ð 	7ØÔ#×4Ò4Ñ6Ô6Ð6ð 	Ô×:Ò:Ð;LÑMÔMÐMõ Ô�wœ}Ñ-Ô-ð 	ÝŒLðZ�cÐ"3Ñ4Ô4ð Zð ZÅÀTÄ^ÔEdÑAeÔAeð Zð ZÝ˜tœ~Ô=Ñ>Ô>ðZð ZØIUðZð Zà&ðZð Zà7;´z×7UÒ7UÑ7WÔ7WðZð Zñô ð ð %)Ô$5×$FÒ$FÀ|ÐU`ÐbvÑ$wÔ$wÑ!ˆ�kàÔ×5Ò5Ø/Ø"Ô2Ø!5Ø%Ø#ØÔ)Ô4ð 	6ñ 	
ô 	
ð 	
ð ˆtr8   c                 ó^  ‡— | j                              ¦   «         \  }}}d}g }|D �]ˆ}|j        }|j        t          j        t          j        fv r9| j        r|j        r|dz  }Œ=t          d|j        j
        › d|j        › d�¦  «        ‚|j        rê|                     ¦   «         dk    rt          j        |_        ||         }|�||         nd}	|dz  }|                     ||	¦  «        }
| j                             ||j        ¦  «         |
r+| j                             |j        ¦  «         d| j        _        |j        s|j        t          j        k    r'|                     |                     ¦   «         ¦  «         �ŒS|j        t          j        k    r | j                             ||j        ¦  «         �ŒŠ|r| j                             |¦  «         g g }}| j        j        �r\| j        j                             ¦   «         Š‰j        }d‰_        ˆfd„t=          |¦  «        D ¦   «         }t?          || j                              ‰j        ¦  «        ¦  «        }||z
  }t=          |¦  «        D ]K}|                     ¦   «         }‰ !                    ¦   «         }||_        | j         "                    |¦  «         ŒL|dk    rŒè|D ]$}‰ #                    |¦  «        | j        j$        |<   Œ%| j         %                    ‰j        |¦  «        \  }}| &                    |¦  «         | &                    |¦  «         | j        j        �°\|rs| j         j'        }|�tP          j)         *                    |¦  «        ntW          ¦   «         }|5  | j         ,                    ||¦  «         ddd¦  «         dS # 1 swxY w Y   dS dS )	z0Update request states based on generated tokens.r   r   zTried to update z	 request z in sync mode.NFc                 ó(   •— g | ]}‰j         › d |› �‘ŒS )z__child#)rQ   )Ú.0ÚiÚstate_to_forks     €r5   ú
<listcomp>z9ContinuousBatchProcessor.update_batch.<locals>.<listcomp>Ø  s*   ø€ ÐeÐeÐeÈA -Ô":ÐGÐGÀAÐGÐGÐeÐeÐer8   )-r„   Úprepare_batch_updaterÄ   rn   r#   ÚFINISHEDÚPENDINGrœ   Úhas_new_tokenrÚ   ÚnamerQ   Úgenerated_lenÚDECODINGÚupdate_and_check_completionr†   Ú!mark_shareable_blocks_as_completeÚcomplete_blocksr…   Úfinish_requestÚblock_new_requestsÚ	streamingr_   rÌ   Ú
PREFILLINGr‹   ra   Ú_requests_to_forkÚpopÚnum_childrenÚrangeÚminÚcompute_max_num_forksÚ!create_equivalent_initial_requestrº   Úforkr¤   Úfork_requestÚextendr•   r@   r«   Ústreamr   Ú
copy_cache)r4   rÎ   Ú
new_tokensÚlogprobsÚcurrent_logits_indexÚpending_outputsÚfuture_staterÄ   ÚtokenÚlogprobÚis_finishedÚcopy_sourceÚcopy_destinationrý   Únew_request_idsÚnum_to_forkÚnum_to_scheduleÚ_Únew_request_idÚchild_stateÚcopy_srcÚcopy_dstr•   Úmaybe_streamrë   s                           @r5   Úupdate_batchz%ContinuousBatchProcessor.update_batch¨  sH  ø€ à26Ô2I×2^Ò2^Ñ2`Ô2`Ñ/Ð˜: xØ ÐØˆØ-ð 	bñ 	bˆLØ Ô&ˆEàŒ|¥Ô 6½Ô8MÐNÐNÐNØÔ*ð à#Ô1ð 2Ø,°Ñ1Ð,ØÝ"Ð#r°e´lÔ6GÐ#rÐ#rÐRWÔRbÐ#rÐ#rÐ#rÑsÔsÐsàÔ)ð bà×&Ò&Ñ(Ô(¨AÒ-Ð-Ý#0Ô#9�E”Là"Ð#7Ô8�Ø<DÐ<P˜(Ð#7Ô8Ð8ÐVZ�Ø$¨Ñ)Ð$ð $×?Ò?ÀÀwÑOÔO�à”
×<Ò<¸UÀLÔD`ÑaÔaÐaØð >Ø”N×1Ò1°%Ô2BÑCÔCÐCØ8=�D”NÔ5Ø”?ð I e¤lµmÔ6LÒ&LÐ&LØ#×*Ò*¨5×+EÒ+EÑ+GÔ+GÑHÔHÐHùà”¥Ô!9Ò9Ð9Ø”
×<Ò<¸UÀLÔD`ÑaÔaÐaùàð 	>ØÔ×,Ò,¨_Ñ=Ô=Ð=ð )+¨BÐ%ˆØŒnÔ.ñ 	.à œNÔ<×@Ò@ÑBÔBˆMØ(Ô5ˆLØ)*ˆMÔ&ØeÐeÐeÐeÕQVÐWcÑQdÔQdÐeÑeÔeˆOå˜l¨D¬J×,LÒ,LÈ]ÔMeÑ,fÔ,fÑgÔgˆKØ*¨[Ñ8ˆOÝ˜?Ñ+Ô+ð @ð @�Ø!0×!4Ò!4Ñ!6Ô!6�Ø+×MÒMÑOÔO�Ø)7�Ô&Ø”×2Ò2°;Ñ?Ô?Ð?Ð?à˜aÒÐØà"1ð dð d�ØAN×ASÒASÐTbÑAcÔAc�”Ô.¨~Ñ>Ð>à!%¤×!8Ò!8¸Ô9QÐSbÑ!cÔ!cÑˆH�hØ×Ò˜xÑ(Ô(Ð(Ø×#Ò# HÑ-Ô-Ð-ð/ ŒnÔ.ñ 	.ð4 ð 	Eð "Ô4ÔCˆNØ@NÐ@Z�5œ:×,Ò,¨^Ñ<Ô<Ð<Õ`kÑ`mÔ`mˆLØð Eð EØ”
×%Ò% kÐ3CÑDÔDÐDðEð Eð Eñ Eô Eð Eð Eð Eð Eð Eð Eð Eøøøð Eð Eð Eð Eð Eð Eð	Eð 	Es   Í7N Î N$Î'N$c                 ó4   — | j                              ¦   «         S )z2Check if there are any active or waiting requests.)r…   rÖ   rL   s    r5   rÖ   z-ContinuousBatchProcessor.has_pending_requestsõ  s   € àŒ~×2Ò2Ñ4Ô4Ð4r8   c                 óÌ   — | j                              ¦   «         d         }|D ]A}|                      ||j        ¦  «         | j                             |j        j        ¦  «         ŒBdS )z&Handle errors during batch processing.r   N)r„   rí   r½   rÄ   r…   r÷   rQ   )r4   r¼   Úfailed_future_statesr  s       r5   Úhandle_batch_errorz+ContinuousBatchProcessor.handle_batch_errorù  sq   € à#Ô6×KÒKÑMÔMÈaÔPÐØ0ð 	Ið 	IˆLØ×&Ò& u¨lÔ.@ÑAÔAÐAØŒN×)Ò)¨,Ô*<Ô*GÑHÔHÐHÐHð	Ið 	Ir8   c                 ó  — t          | j        j                             ¦   «         ¦  «        }|D ]7}|                      ||¦  «         | j                             |j        ¦  «         Œ8| j                             ¦   «          t          | j        j	         
                    ¦   «         ¦  «        D ]7}| j        j	                             |¦  «        }|                      ||¦  «         Œ8| j        j                             ¦   «          dS )z.Fail all active requests with the given error.N)rd   r…   r¤   Úvaluesr½   r÷   rQ   rŸ   Úfree_all_waiting_cpu_cachesr¥   Úkeysrü   Úwaiting_requests_orderrm   )r4   r¼   ÚrequestsrÄ   Úreq_ids        r5   Úfail_all_requestsz*ContinuousBatchProcessor.fail_all_requests   sò   € õ ˜œÔ6×=Ò=Ñ?Ô?Ñ@Ô@ˆØð 	<ð 	<ˆEØ×&Ò& u¨eÑ4Ô4Ð4ØŒN×)Ò)¨%Ô*:Ñ;Ô;Ð;Ð;ð 	Ô×;Ò;Ñ=Ô=Ð=Ý˜4œ>Ô:×?Ò?ÑAÔAÑBÔBð 	5ð 	5ˆFØ”NÔ3×7Ò7¸Ñ?Ô?ˆEØ×&Ò& u¨eÑ4Ô4Ð4Ð4ð 	ŒÔ-×3Ò3Ñ5Ô5Ð5Ð5Ð5r8   Úmodelc                 ó¸   — | j                              | j        j        ¬¦  «        }| j                             ||¦  «         | j                              ¦   «          dS )z!Perform a single generation step.)rÓ   N)r„   r¦   r    rã   Úcompute_batchÚretrieve_device_outputs)r4   r'  Ú
batch_datas      r5   Ú_generation_stepz)ContinuousBatchProcessor._generation_step  s[   € ð
 Ô,×=Ò=È$ÔJ[ÔJfÐ=ÑgÔgˆ
ð 	Ô×'Ò'¨¨zÑ:Ô:Ð:ð 	Ô×7Ò7Ñ9Ô9Ð9Ð9Ð9r8   c                 ó:   — | j                              |¦  «         dS )aG  Pre-capture CUDA graphs (or trigger compile warmup) for varlen and decode paths. In async mode, both IO
        pairs are warmed up since each has its own graph buffer and static tensors. The varlen path is warmed up at
        the largest possible `(q, kv)` sizes so subsequent captures fit inside it without growing the pool.N)r    Úwarmup)r4   r'  s     r5   r.  zContinuousBatchProcessor.warmup   s!   € ð
 	Ô× Ò  Ñ'Ô'Ð'Ð'Ð'r8   rb   )(r<   r=   r>   r   r   r?   r'   r   r   r   r   r   rE   rF   rC   rf   r@   r.   r-   r   rM   rA   r§   r®   r°   ÚboolrÆ   r»   r"   r½   ræ   r  rÖ   r  r&  Úno_gradr
   ÚModuler,  r.  r3   r8   r5   rƒ   rƒ   ¾   s~  € € € € € € Ø-Ð0JÑJÐJÐJÑJØÐÐÑð\
à"ð\
ð !ð\
ð ,ð	\
ð
 %=ð\
ð ?ð\
ð ”[ 4Ñ'ð\
ð ”k DÑ(ð\
ð $ð\
ð #9ð\
ð ”lð\
ð ”[ð\
ð ð\
ð .ð\
ð 
ð\
ð \
ð \
ð \
ð|
˜#ð 
ð 
ð 
ð 
ð%ð %ð %ð %ð'ð 'ð 'ð 'ð%¨ð %ð %ð %ð %ðNA¨9ð A¸\ð AÈdð Að Að Að Að> Dð >ð >ð >ð >ð@KEð KEð KEð KEðZ5 dð 5ð 5ð 5ð 5ðIð Ið Ið6 yð 6°Tð 6ð 6ð 6ð 6ð" €U„]�_„_ð: b¤ið :°Dð :ð :ð :ñ „_ð:ð €U„]�_„_ð(˜BœIð (¨$ð (ð (ð (ñ „_ð(ð (ð (r8   rƒ   c                   óX  — e Zd ZdZ	 d.dededededz  ddf
d„Zdeddfd	„Z	d/d
„Z
defd„Zd/d„Z	 	 	 	 d0dededz  dededdf
d„Zd.dededz  ddfd„Zd/d„Z	 	 	 	 	 d1dee         dedz  dedz  dededeee         z  dz  dededz  fd„Z	 	 	 d2deee                  dedz  dedededee         fd „Zdeddfd!„Zd3dedz  dedz  dedz  fd"„Zd#„ Zdedee         fd$„Zded%eddfd&„Z e j!        ¦   «         d/d'„¦   «         Z"d/d(„Z#de$fd)„Z%d*e&d+e$dz  ddfd,„Z'd*e&d+e$dz  ddfd-„Z(dS )4ÚContinuousBatchingManagera¼  Manager for handling continuous batching of generation requests. It provides a user interface for submitting
    generation requests, retrieving results, and managing the background generation thread. This class should not be
    created directly, but through one of the following entry points (all methods of the `ContinuousMixin` mixin):
    - `init_continuous_batching`
    - `continuous_batching_context_manager`
    - `generate_batch`
    Nr'  r9   r‡   Úworkload_hintsr0   c                 óÂ  — t          j        |j        ¬¦  «        | _        t          j        ¦   «         | _        d| _        t          j        ¦   «         | _        t          j	        ¦   «         | _
        t          ¦   «         | _        t          ¦   «         | _        d| _        d| _        d| _        d| _        d| _        |                      |¦  «         |                     ¦   «         | _        || _        t1          |dd¦  «        }|�|nd| _        t5          t1          | j        dd¦  «        |j        ¬¦  «        | _        | j        j        | _        |j        r| j                             ¦   «          tA          | j         !                    |¦  «        |j"        |j#        ¬	¦  «        | _$        tK          | j        j&        ||| j$        j'        ¬
¦  «        | _(        | j(        j)        | _*        dS )ay  Initialize the continuous batching manager.

        Args:
            model: The language model for generation
            generation_config: Configuration for generation parameters
            continuous_batching_config: Configuration for continuous batching parameters
            workload_hints: Workload hints for the continuous batching initialization (optional)
        )Úmaxsizer   NFÚnum_return_sequencesr   Ú_device_mesh)Údevice_meshÚcpu_group_timeout)Úlogits_processorÚper_request_processorsÚdrop_unsupported_processors)r,   r–   r4  Úhas_logit_processors)+rE   rF   Úmax_queue_sizer‰   rŠ   Ú_request_counterrI   rJ   Ú_request_lockÚEventÚ_has_new_requestsrf   rŒ   rC   r‹   Úbatch_processorÚ_generation_threadÚfatal_errorÚ	warmed_upÚ_original_attn_implÚswitch_to_cb_friendly_attnÚevalr'  r9   r˜   r7  r   r:  r�   Úis_tp_driverÚdisable_nccl_graph_mixingÚmaybe_warn_nccl_graph_mixingr   r;   r<  r=  rˆ   r   r,   Údo_processingr‡   Úallow_block_sharingÚ_use_prefix_sharing)r4   r'  r9   r‡   r4  r7  s         r5   rM   z"ContinuousBatchingManager.__init__2  sÒ  € õ  !œ;Ð/IÔ/XÐYÑYÔYˆÔÝ.3¬k©m¬mˆÔØ !ˆÔÝ&œ^Ñ-Ô-ˆÔÝ!*¤Ñ!2Ô!2ˆÔõ )?Ñ(@Ô(@ˆÔ%Ý)™^œ^ˆÔØ@DˆÔØ"&ˆÔð .2ˆÔØˆŒð $(ˆÔ Ø×'Ò'¨Ñ.Ô.Ð.Ø—Z’Z‘\”\ˆŒ
ð "3ˆÔÝ&Ð'8Ð:PÐRVÑWÔWÐØ<PÐ<\Ð$8Ð$8ÐbcˆÔ!õ #4Ý ¤
¨N¸DÑAÔAØ8ÔJð#
ñ #
ô #
ˆÔð !Ô3Ô@ˆÔà%Ô?ð 	CØÔ#×@Ò@ÑBÔBÐBõ  EØ!œZ×=Ò=Ð>OÑPÔPØ#=Ô#TØ(BÔ(^ð 
ñ  
ô  
ˆÔõ +MØ”:Ô$Ø0Ø)Ø!%Ô!5Ô!Cð	+
ñ +
ô +
ˆÔ'ð $(Ô#BÔ#VˆÔ Ð Ð r8   c                 óv  — |j         j        }|}t          |¬¦  «        }d|v }|so|sm|j        rft	          d¬¦  «        rd}nt          d¬¦  «        rd}nd}d}|�#d	|› �}t          j        |› d
|› d|› d�¦  «         nt          j        |› d�¦  «         d|vr!|                     d|› �¦  «         || _	        dS dS )z°Switch the attn implementation to one that is CB friendly: try to find a flash implementation if flash is
        requested and, in any cases, switch to a paged implementation.)Ú"requested_attention_implementationzpaged|T)Úkernels_fallback_okr   r   Nz>Continuous batching is much better when using flash attention.Úflash_attention_z Switching from z to z_. If you need to use eager or sdpa, use paged|eager or paged|sdpa as the `attn_implementation`.zE Consider using a flash `attn_implementation` when loading the model.)
r,   Ú_attn_implementationr   Ú_supports_flash_attnr   r   r$   Úwarningr6   rH  )r4   r'  Úoriginal_attn_implÚtarget_implemÚis_flashÚis_pagedÚversionÚmsgs           r5   rI  z4ContinuousBatchingManager.switch_to_cb_friendly_attnw  sD  € ð #œ\Ô>ÐØ*ˆõ 0ÐS`ÐaÑaÔaˆØ˜}Ð,ˆØð 	n ð 	n¨UÔ-Gð 	nå(¸TÐBÑBÔBð Ø��Ý*¸tÐDÑDÔDð Ø��à�àRˆCØÐ"Ø <°7Ð <Ð <�Ý”Øð tð tÐ,>ð tð tÀMð tð tð tñô ð ð õ
 ” #ÐlÐlÐlÑmÔmÐmð ˜=Ð(Ð(Ø×)Ò)Ð*B°=Ð*BÐ*BÑCÔCÐCØ'9ˆDÔ$Ð$Ð$ð )Ð(r8   c                 ó’   — | j         €|                      ¦   «         | _         | j                              | j        ¦  «         d| _        dS )zŠPre-capture CUDA graphs for varlen and decode paths by running dummy batches. Initializes the batch
        processor if not already done.NT)rD  Ú_create_batch_processorr.  r'  rG  rL   s    r5   r.  z ContinuousBatchingManager.warmup™  sE   € ð ÔÐ'Ø#'×#?Ò#?Ñ#AÔ#AˆDÔ ØÔ×#Ò# D¤JÑ/Ô/Ð/ØˆŒˆˆr8   c                 óF   — | j         duo| j                              ¦   «         S )zUReturns True if the background generation thread has been started and is still alive.N)rE  Úis_aliverL   s    r5   Ú
is_runningz$ContinuousBatchingManager.is_running£  s$   € àÔ&¨dÐ2ÐY°tÔ7N×7WÒ7WÑ7YÔ7YÐYr8   c                 ó
  — |                       ¦   «         rt          j        d¦  «         dS | j                             ¦   «          d| _        t          j        | j        ¬¦  «        | _	        | j	         
                    ¦   «          dS )z'Start the background generation thread.z"Manager thread is already running.N)Útarget)rb  r$   rW  rŒ   rm   rF  rI   ÚThreadÚ_run_generation_looprE  ÚstartrL   s    r5   rg  zContinuousBatchingManager.start§  s{   € à�?Š?ÑÔð 	ÝŒNÐ?Ñ@Ô@Ð@Ø�4ØÔ%×+Ò+Ñ-Ô-Ð-ØˆÔÝ"+Ô"2¸$Ô:SÐ"TÑ"TÔ"TˆÔØÔ×%Ò%Ñ'Ô'Ð'Ð'Ð'r8   TFÚblockÚtimeoutÚkeep_for_next_sessionÚ	hard_stopc                 ó¢  — | j         €t          j        d¦  «         | j        €d}|r|dz  }t          j        |¦  «         dS t	          ¦   «         }|rt
          j        nt
          j        }| j         	                    || j
        j        ¦  «         |r|                      ||¦  «         |sd| _         n t          j        d¦  «         | | j        _        | j        �&| j                             | j        ¦  «         d| _        t%          j        ¦   «          t(          j                             ¦   «         r t(          j                             ¦   «          dS dS )a¿  Stop the background generation thread. If the `block` flag is set to True, then this method waits for the
        thread to stop for a maximum time of `timeout` seconds (None means no timeout). If the `keep_for_next_session`
        flag is set to True, then the manager is cached on the model for future use. If the `hard_stop` flag is set,
        the background generation thread will be stopped immediately and pending requests will be failed.Nz%
Batch processor was not initialized.zManager not started.z? Hence the unstarted manager will not be kept for next session.z:Continuous batching manager will be kept for next session.)rD  r$   rW  rE  r   rf   rr   rq   rŒ   rv   r�   ro   Újoinru   r'  Ú#_cached_continuous_batching_managerrH  r6   r©   rª   r@   r«   r¬   r­   )r4   rh  ri  rj  rk  r]  Ústop_trigger_timeÚstop_statuss           r5   ÚstopzContinuousBatchingManager.stop±  s\  € ð ÔÐ'ÝŒNÐCÑDÔDÐDð Ô"Ð*Ø(ˆCØ$ð YØÐXÑX�ÝŒN˜3ÑÔÐØ�4õ )™NœNÐØ:CÐnÕ,Ô6Ð6ÕI_ÔInˆØÔ%×2Ò2°;ÀÔ@WÔ@cÑdÔdÐdàð 	2Ø�IŠIÐ'¨Ñ1Ô1Ð1ð %ð 	BØ#'ˆDÔ Ð õ ŒKÐTÑUÔUÐUØ=AˆDŒJÔ:ð Ô#Ð/ØŒJ×.Ò.¨tÔ/GÑHÔHÐHØ'+ˆDÔ$õ 	Œ
‰ŒˆÝŒ:×"Ò"Ñ$Ô$ð 	%ÝŒJ×"Ò"Ñ$Ô$Ð$Ð$Ð$ð	%ð 	%r8   ro  c                 ó  — | j         €dS | j                              |¬¦  «         | j                              ¦   «         rt          j        d|› d�¦  «         dS t          ¦   «         }t          j        d||z
  d›d�¦  «         d| _         dS )zeWait for the background thread to finish. Wait can be capped using the timeout argument (in seconds).N©ri  z3Generation thread did not exit after join timeout (z).z+Background generation thread stopped after ú.2fús.)rE  rm  ra  r$   rW  r   ru   )r4   ro  ri  Úends       r5   rm  zContinuousBatchingManager.joinã  s¤   € ð Ô"Ð*Ø�4ð 	Ô×$Ò$¨WÐ$Ñ5Ô5Ð5ØÔ"×+Ò+Ñ-Ô-ð 	+ÝŒNÐ\ÐQXÐ\Ð\Ð\Ñ]Ô]Ð]Ð]Ð]å‘.”.ˆCÝŒKÐeÀcÐL]ÑF]ÐeÐeÐeÐeÑfÔfÐfØ&*ˆDÔ#Ð#Ð#r8   c                 óŽ   — |                       ¦   «         r|                      dd¬¦  «         | j                             ¦   «          dS )z’Terminate the manager and release distributed resources. Safe to call multiple times. After calling this,
        the manager cannot be restarted.TF)rh  rj  N)rb  rq  r�   Údestroy_cpu_comm_grouprL   s    r5   Údestroyz!ContinuousBatchingManager.destroyò  sG   € ð �?Š?ÑÔð 	?Ø�IŠI˜D¸ˆIÑ>Ô>Ð>ØÔ×6Ò6Ñ8Ô8Ð8Ð8Ð8r8   Ú	input_idsrQ   Úmax_new_tokensrù   Úrecord_timestampsÚeos_token_idr¹   c           
      óN  — | j         sdS | j        j        t          j        k    rD|dd…         › dd…         dz   |dd…         › dd…         z   }t          j        d|› d�¦  «         dS |€9| j        5  d	| j        › �}| xj        dz  c_        ddd¦  «         n# 1 swxY w Y   |€| j	        j
        n|}|€| j	        j        n|}t          |t          |¦  «        | j        dz
  |||||¬
¦  «        }	| j                             |	dd¬¦  «         | j                             ¦   «          |S )aÇ  Add a new generation request to the queue. If the process is not a TP driver, this is a no-op.

        Args:
            input_ids: Input token IDs to use as prompt
            request_id: Optional custom request ID (auto-generated if None)
            max_new_tokens: Maximum number of new tokens to generate
            streaming: Whether to stream tokens as they're generated
            record_timestamps: Whether to record timestamps for each generated token
            eos_token_id: End-of-sequence token ID(s)
            logit_processor_kwargs: Keyword arguments for the logits processor.

        Returns:
            str | None: The request ID if the process is a TP driver, None otherwise.
        Nr   éÿÿÿÿz, ..., éýÿÿÿr   z0Background thread is stopping. Request with ids z will be dropped.Úreq_)rQ   Úinitial_tokensrý   r|  r{  r}  rù   r¹   Té
   ©rh  ri  )rK  rŒ   r~   rf   rq   r$   rW  rA  r@  r9   r{  r}  r"   rd   r7  r‰   rS   rC  Úset)
r4   rz  rQ   r{  rù   r|  r}  r¹   ÚpreviewrÄ   s
             r5   Úadd_requestz%ContinuousBatchingManager.add_requestû  s³  € ð2 Ô ð 	Ø�4àÔ(Ô5Õ9OÔ9^Ò^Ð^Ø" 2 A 2œÐ(¨¨"¨Ô-°	Ñ9¸yÈÈÈ¼~Ð<OÐPQÐPRÐPRÔ<SÑSˆGÝŒNÐhÈgÐhÐhÐhÑiÔiÐiØ�4àÐØÔ#ð +ð +Ø; DÔ$9Ð;Ð;�
ØÐ%Ô%¨Ñ*Ð%Ô%ð+ð +ð +ñ +ô +ð +ð +ð +ð +ð +ð +øøøð +ð +ð +ð +ð CQÐBX˜Ô/Ô>Ð>Ð^lˆØ>JÐ>R�tÔ-Ô:Ð:ÐXdˆõ Ø!Ý 	™?œ?ØÔ2°QÑ6Ø/Ø)Ø%ØØ#9ð	
ñ 	
ô 	
ˆð 	Ô×Ò˜U¨$¸ÐÑ;Ô;Ð;ØÔ×"Ò"Ñ$Ô$Ð$ØÐs   Á1BÂBÂBÚinputsc                 óÄ  — t          |¦  «        }| j        5  d„ t          | j        | j        |z   ¦  «        D ¦   «         }| xj        |z  c_        ddd¦  «         n# 1 swxY w Y   t	          t          ||¦  «        ¦  «        }| j        rt          |d„ d¬¦  «        }| j        j	        }	|	€| j
        j        j	        n|	}	|	€dn|	}	|D ]\  }
} | j        d||
||||	dœ|¤Ž Œ|S )	zgUtility function to batch `add_request` and return their IDs. Check its documentation for more details.c                 ó   — g | ]}d |› �‘ŒS )r�  r3   )ré   rê   s     r5   rì   z:ContinuousBatchingManager.add_requests.<locals>.<listcomp>@  s   € ÐrÐrÐr¨!˜: !˜:˜:ÐrÐrÐrr8   Nc                 ó   — | d         S )Nr   r3   )Úxs    r5   ú<lambda>z8ContinuousBatchingManager.add_requests.<locals>.<lambda>E  s
   € À!ÀAÄ$€ r8   T)ÚkeyÚreverser  )rz  rQ   r{  rù   r|  r}  r3   )rµ   rA  rþ   r@  rd   ÚziprP  Úsortedr9   r}  r'  r,   r‡  )r4   rˆ  r{  rù   r|  r¹   Únum_requestsÚrequest_idsÚids_and_inputsr}  rQ   rz  s               r5   Úadd_requestsz&ContinuousBatchingManager.add_requests4  su  € õ ˜6‘{”{ˆØÔð 	2ð 	2ØrÐr­u°TÔ5JÈDÔLaÐdpÑLpÑ/qÔ/qÐrÑrÔrˆKØÐ!Ô! \Ñ1Ð!Ô!ð	2ð 	2ð 	2ñ 	2ô 	2ð 	2ð 	2ð 	2ð 	2ð 	2ð 	2øøøð 	2ð 	2ð 	2ð 	2õ �c +¨vÑ6Ô6Ñ7Ô7ˆØÔ#ð 	VÝ# N¸¸ÐPTÐUÑUÔUˆNàÔ-Ô:ˆØ9EÐ9M�t”zÔ(Ô5Ð5ÐS_ˆØ)Ð1�r�r°|ˆà%3ð 		ð 		Ñ!ˆJ˜	ØˆDÔð Ø#Ø%Ø-Ø#Ø"3Ø)ðð ð )ðð ð ð ð Ðs   —8AÁAÁ"Ac                 ó~   — | j         r5| j                             |¦  «         | j                             ¦   «          dS dS )zœCancel a request by its ID. If this called from a process that is not a TP driver, it's a no-op: only TP
        driver processes interact with the manager.N)rK  rŠ   rS   rC  r…  )r4   rQ   s     r5   Úcancel_requestz(ContinuousBatchingManager.cancel_requestW  sK   € ð Ôð 	)ØÔ×!Ò! *Ñ-Ô-Ð-ØÔ"×&Ò&Ñ(Ô(Ð(Ð(Ð(ð	)ð 	)r8   c                 ó   — | j         € | j        j                             ¦   «         rdS 	 | j        j                             d|¬¦  «        }|�,|j        |k    r!| j        j                             |¦  «         dS |S # t          j        $ r Y dS w xY w)z³Retrieve one result from the output queue. If an ID is provided, returns the first matching request. If a
        timeout is provided, returns None after the timeout (in seconds).NTr„  )	rE  r‹   rG   ÚemptyrP   rQ   rS   rE   ÚEmpty)r4   rQ   ri  Úresults       r5   Ú
get_resultz$ContinuousBatchingManager.get_result`  s§   € ð Ô"Ð*¨tÔ/AÔ/N×/TÒ/TÑ/VÔ/VÐ*Ø�4ð	ØÔ'Ô4×8Ò8¸tÈWÐ8ÑUÔUˆFØÐ%¨&Ô*;¸zÒ*IÐ*IØÔ"Ô/×3Ò3°FÑ;Ô;Ð;Ø�tØˆMøÝŒ{ð 	ð 	ð 	Ø�4�4ð	øøøs   ©AA: Á8A: Á:BÂBc              #   óÎ   K  — | j         �W| j                              ¦   «         r@|                      d¬¦  «        }|�|V — | j         �| j                              ¦   «         °<dS dS dS dS )z.Iterate over results as they become available.Nçš™™™™™¹?rs  )rE  ra  rœ  )r4   r›  s     r5   Ú__iter__z"ContinuousBatchingManager.__iter__n  s|   è è € àÔ%Ð1°dÔ6M×6VÒ6VÑ6XÔ6XÐ1Ø—_’_¨S�_Ñ1Ô1ˆFØÐ!Ø���ð Ô%Ð1°dÔ6M×6VÒ6VÑ6XÔ6XÐ1Ð1Ð1Ð1Ð1Ð1Ð1Ð1Ð1r8   c              #   óü   K  — | j         �n| j                              ¦   «         rW|                      |d¬¦  «        }|�|V — |                     ¦   «         rdS | j         �| j                              ¦   «         °SdS dS dS dS )zÏIterate over results matching a specific request id (blocking).

        Uses the shared output queue with requeue. For high-concurrency serving,
        use :meth:`register_result_handler` instead.
        Nrž  )rQ   ri  )rE  ra  rœ  r  )r4   rQ   r›  s      r5   Úrequest_id_iterz)ContinuousBatchingManager.request_id_iteru  s™   è è € ð Ô%Ð1°dÔ6M×6VÒ6VÑ6XÔ6XÐ1Ø—_’_°
ÀC�_ÑHÔHˆFØÐ!Ø���Ø×%Ò%Ñ'Ô'ð Ø�Fð Ô%Ð1°dÔ6M×6VÒ6VÑ6XÔ6XÐ1Ð1Ð1Ð1Ð1Ð1Ð1Ð1Ð1r8   rU   c                 ó¬   ‡ ‡‡— t          j        ¦   «         }ˆˆˆ fd„}‰ j        j        5  ||f‰ j        j        ‰<   ddd¦  «         dS # 1 swxY w Y   dS )a+  Register a callback for result delivery (streaming or non-streaming).

        The callback is invoked on the event loop via ``call_soon_threadsafe`` each time a result is produced for this
        request. For streaming requests, this happens on every token; for non-streaming, only on completion. The handler
        is automatically cleaned up when the request finishes.

        Args:
            request_id (`str`): The request ID to receive outputs for.
            callback (`callable`): Called with a ``GenerationOutput`` for each result.
        c                 óÔ   •—  ‰| ¦  «         |                       ¦   «         rG‰j        j        5  ‰j        j                             ‰d ¦  «         d d d ¦  «         d S # 1 swxY w Y   d S d S r2   )r  r‹   rK   rH   rü   )r›  rU   rQ   r4   s    €€€r5   Ú_auto_cleanupzHContinuousBatchingManager.register_result_handler.<locals>._auto_cleanup�  sÔ   ø€ ØˆH�VÑÔÐØ×!Ò!Ñ#Ô#ð MØÔ'Ô-ð Mð MØÔ&Ô6×:Ò:¸:ÀtÑLÔLÐLðMð Mð Mñ Mô Mð Mð Mð Mð Mð Mð Mð Møøøð Mð Mð Mð Mð Mð MðMð Ms   ­!AÁAÁ"AN)ÚasyncioÚget_running_loopr‹   rK   rH   )r4   rQ   rU   rV   r¤  s   ```  r5   Úregister_result_handlerz1ContinuousBatchingManager.register_result_handler‚  sÚ   øøø€ õ Ô'Ñ)Ô)ˆð	Mð 	Mð 	Mð 	Mð 	Mð 	Mð 	Mð ÔÔ%ð 	Sð 	SØ>KÈTÐ=RˆDÔÔ.¨zÑ:ð	Sð 	Sð 	Sñ 	Sô 	Sð 	Sð 	Sð 	Sð 	Sð 	Sð 	Sð 	Søøøð 	Sð 	Sð 	Sð 	Sð 	Sð 	Ss   ªA	Á	AÁAc                 óÈ  — d}	 |                       ¦   «         }|| _        d| _        |j        rG|                     ¦   «         st          d¦  «        ‚|                      ¦   «          | xj        dz  c_        	 |                     ¦   «         }|r9|                      ¦   «          |                     ¦   «          | xj        dz  c_        n~| j        j	        t          j        k    rnd| j        j	        t          j        k    r|                     ¦   «         sn5| j                             d¬¦  «         | j                             ¦   «          ŒÎt#          |j        t&          ¦  «        r-d|j        j        z
  |j        _        |                     ¦   «          t          d| j        j	        ›�¦  «        }|                      ||¦  «         nF# t,          $ r9}t/          j        d	|› �d¬
¦  «         |                      ||¦  «         Y d}~nd}~ww xY w| j                             ¦   «          t/          j        d¦  «         dS # | j                             ¦   «          t/          j        d¦  «         w xY w)z6Main processing loop running in the background thread.Nr   z$Failed to bootstrap the first batch.r   Trž  rs  zdGeneration loop finished before this request completed w/ self.background_thread_status.tp_status = zError in generation loop: r³   zCGeneration loop finished and background thread exited successfully.)r_  rD  Úcurrent_batchrœ   ræ   rÚ   r,  r  rŒ   rz   rf   rr   rq   rÖ   rC  Úwaitrm   rÉ   r„   r   Úcurrent_pairÚ_fail_all_remaining_requestsr»   r$   r¼   Ú_handle_critical_errorry   ru   )r4   rD  Úrequests_availabler¼   rÅ   s        r5   rf  z.ContinuousBatchingManager._run_generation_loopš  s¢  € ð ˆð8	_à"×:Ò:Ñ<Ô<ˆOØ#2ˆDÔ Ø!"ˆDÔð Ô1ð (Ø&×9Ò9Ñ;Ô;ð OÝ&Ð'MÑNÔNÐNØ×%Ò%Ñ'Ô'Ð'ØÐ"Ô" aÑ'Ð"Ô"ð3Ø%4×%GÒ%GÑ%IÔ%IÐ"ð &ð 3Ø×)Ò)Ñ+Ô+Ð+Ø#×0Ò0Ñ2Ô2Ð2ØÐ&Ô&¨!Ñ+Ð&Ô&Ð&ð Ô2Ô<Õ@VÔ@`Ò`Ð`Øð Ô1Ô;Õ?UÔ?dÒdÐdØ+×@Ò@ÑBÔBð eð ð Ô*×/Ò/¸Ð/Ñ<Ô<Ð<ØÔ*×0Ò0Ñ2Ô2Ð2ð-3õ4 ˜/Ô<Õ>XÑYÔYð /ØBCÀoÔFhÔFuÑBu�Ô2Ô?Ø×,Ò,Ñ.Ô.Ð.õ !ØyÈTÔMjÔMtÐyÐyñô ˆEð ×-Ò-¨e°_ÑEÔEÐEÐEøõ ð 	<ð 	<ð 	<ÝŒLÐ9°aÐ9Ð9ÀDÐIÑIÔIÐIØ×'Ò'¨¨?Ñ;Ô;Ð;Ð;Ð;Ð;Ð;Ð;øøøøð	<øøøð Ô)×9Ò9Ñ;Ô;Ð;ÝŒKÐ]Ñ^Ô^Ð^Ð^Ð^øð Ô)×9Ò9Ñ;Ô;Ð;ÝŒKÐ]Ñ^Ô^Ð^Ð^øøøs0   „F8F= Æ<H2 Æ=
H Ç/G;Ç6H2 Ç;H È H2 È2/I!c                 óp   — | j         €t          d¦  «        ‚| j                              | j        ¦  «         dS )z=Perform a single generation step. This is mostly cuda graphedNzNTried to perform a generation step before the batch processor was initialized.)rD  rÚ   r,  r'  rL   s    r5   r,  z*ContinuousBatchingManager._generation_stepÚ  s9   € àÔÐ'ÝÐoÑpÔpÐpØÔ×-Ò-¨d¬jÑ9Ô9Ð9Ð9Ð9r8   c                 ód  — t          | dd¦  «        }t          |t          ¦  «        r|                     ¦   «          |S t	          | j        j        | j        | j        j        | j	        t          | j        di ¦  «        | j        j
        ¬¦  «        }|j        | _        t          | j        |j        |j        | j        ¬¦  «         |j        dk    rd| j        _        | j        j        }t'          j        |d¦  «        }|€t+          j        d|› d�¦  «         t.          } ||| j        j        | j        j        ¬	¦  «        }t          || j        j        | j        | j        | j        | j        r| j        nd| j        r| j        nd| j        | j         | j        j        | j        j
        || j	        ¬
¦  «        }|S )ziCreate a new batch processor. If an already initialized batch processor exists, it is reset and returned.rD  NÚtp_plan)r,   r‡   r.   r�   r±  r-   )r–   Ú
num_blocksr›   Úuse_prefix_sharingr   zScheduler 'z ' not found. Defaulting to FIFO.)r†   Úsafety_marginÚmax_requests_per_batch)r†   r,   r9   r‡   rˆ   r‰   rŠ   r‹   rŒ   r�   rŽ   r…   r�   )!r˜   rÉ   rƒ   r°   r   r'  r,   r‡   r.   r�   r-   r³  rP  r   r²  r›   Únum_sliding_attention_groupsÚmax_blocks_per_requestÚscheduler_typer%   rP   r$   rW  r&   r´  rµ  r9   rˆ   rK  r‰   rŠ   r‹   rŒ   )r4   rD  Úpaged_attention_cacher¸  Úscheduler_clsr…   s         r5   r_  z1ContinuousBatchingManager._create_batch_processorà  sÛ  € õ " $Ð(9¸4Ñ@Ô@ˆÝ�oÕ'?Ñ@Ô@ð 	#Ø×!Ò!Ñ#Ô#Ð#Ø"Ð"õ !4Ø”:Ô$Ø'+Ô'FØ”:Ô$Ø#Ô6Ý˜DœJ¨	°2Ñ6Ô6Ø”*Ô"ð!
ñ !
ô !
Ðð $9Ô#KˆÔ å-ØÔ5Ø,Ô7Ø2ÔCØ#Ô7ð		
ñ 	
ô 	
ð 	
ð !Ô=ÀÒAÐAØEFˆDÔ+ÔBð Ô8ÔGˆÝ)Ô-¨n¸dÑCÔCˆØÐ ÝŒNÐY¨ÐYÐYÐYÑZÔZÐZÝ)ˆMà!�MØ'ØÔ9ÔGØ#'Ô#BÔ#Yð
ñ 
ô 
ˆ	õ 3Ø'Ø”:Ô$Ø"Ô4Ø'+Ô'FØ Ô0Ø,0Ô,=ÐG˜Ô(Ð(À4Ø.2Ô.?ÐI˜Ô*Ð*ÀTØÔ,Ø%)Ô%BØœÔ*Øœ
Ô(ØØ#Ô6ð
ñ 
ô 
ˆð Ðr8   r¼   rD  c                 óê   — || _         | j                             t          j        | j        j        ¬¦  «         | j                             dt          j        ¦  «         |                      ||¦  «         dS )z:Handle critical errors that terminate the generation loop.)rn   ro   r   N)	rF  rŒ   rv   rf   rr   r�   ro   r¶   r¬  )r4   r¼   rD  s      r5   r­  z0ContinuousBatchingManager._handle_critical_error  su   € ð !ˆÔàÔ%×2Ò2Ý)Ô3ÀÔAXÔAdð 	3ñ 	
ô 	
ð 	
ð
 	Ô×3Ò3°AÕ7MÔ7WÑXÔXÐXà×)Ò)¨%°ÑAÔAÐAÐAÐAr8   c                 óÊ   — 	 	 | j                              ¦   «         }|�|                     ||¦  «         Œ2# t          j        $ r Y nw xY w|�|                     |¦  «         dS dS )zCFail all remaining requests in the input queue and active requests.TN)r‰   Ú
get_nowaitr½   rE   rš  r&  )r4   r¼   rD  Úreq_datas       r5   r¬  z6ContinuousBatchingManager._fail_all_remaining_requests,  s‘   € ð	ðKØÔ+×6Ò6Ñ8Ô8�Ø"Ð.Ø#×9Ò9¸%ÀÑJÔJÐJðKøõ Œ{ð 	ð 	ð 	ØˆDð	øøøð Ð&Ø×-Ò-¨eÑ4Ô4Ð4Ð4Ð4ð 'Ð&s   ‚35 µAÁAr2   rb   )TNFF)NNFFN)NFF)NN))r<   r=   r>   rc   r+   r   r   r(   rM   rI  r.  r/  rb  rg  Úfloatrq  rm  ry  rd   r€   rA   r	   r‡  r•  r—  r!   rœ  rŸ  r   r¡  r   r§  r@   r0  rf  r,  rƒ   r_  r»   r­  r¬  r3   r8   r5   r3  r3  )  s&  € € € € € ðð ð 04ðCWð CWà#ðCWð ,ðCWð %=ð	CWð
 &¨Ñ,ðCWð 
ðCWð CWð CWð CWðJ :Ð0Dð  :Èð  :ð  :ð  :ð  :ðDð ð ð ðZ˜Dð Zð Zð Zð Zð(ð (ð (ð (ð Ø $Ø&+Øð0%ð 0%àð0%ð ˜‘ð0%ð  $ð	0%ð
 ð0%ð 
ð0%ð 0%ð 0%ð 0%ðd+ð + eð +°e¸d±lð +Èdð +ð +ð +ð +ð9ð 9ð 9ð 9ð "&Ø%)ØØ"'Ø/3ð7ð 7à˜”9ð7ð ˜$‘Jð7ð ˜d™
ð	7ð
 ð7ð  ð7ð ˜D œI‘o¨Ñ,ð7ð #&ð7ð 
ˆt‰ð7ð 7ð 7ð 7ðx &*ØØ"'ð!ð !à�T˜#”Y”ð!ð ˜d™
ð!ð ð	!ð
  ð!ð #&ð!ð 
ˆcŒð!ð !ð !ð !ðF)¨ð )°ð )ð )ð )ð )ðð  S¨4¡Zð ÀÈÁð ÐYiÐlpÑYpð ð ð ð ðð ð ð¨#ð °)Ð<LÔ2Mð ð ð ð ðS°#ð SÀð SÈdð Sð Sð Sð Sð0 €U„]�_„_ð=_ð =_ð =_ñ „_ð=_ð~:ð :ð :ð :ð<Ð)Að <ð <ð <ð <ð|B¨Ið BÐH`ÐcgÑHgð BÐlpð Bð Bð Bð Bð5°)ð 5ÐNfÐimÑNmð 5Ðrvð 5ð 5ð 5ð 5ð 5ð 5r8   r3  c                   ó¬  — e Zd ZU dZeed<    ej        ¦   «         	 	 	 ddedz  dedz  de	dz  de
fd„¦   «         Zdd„Ze ej        ¦   «         	 	 	 	 	 	 	 ddedz  dededz  dedz  dedede	dz  dee
         fd„¦   «         ¦   «         Z ej        ¦   «         	 	 	 	 	 	 ddeee                  dedz  dedz  dededededeeef         fd„¦   «         ZdS )ÚContinuousMixina  Mixin class for models to add continuous batching capabilities. Continuous batching has three entry points:
    - `init_continuous_batching`, which is the actual entry point for continuous batching
    - `continuous_batching_context_manager`, which itself is a wrapper around `init_continuous_batching`
    - `generate_batch`, which is really a wrapper around `continuous_batching_context_manager`

    They are defined in this order. Any change made to any of those three entry points should be reflected in the other
    two.
    r9   Nr‡   r4  r0   c                 ód  — t          | d¦  «        r t          | d¦  «        rt          | d¦  «        st          d¦  «        ‚t          | dd¦  «        }t          |t          ¦  «        r+t          j        d¦  «         |                     | ¦  «         |S |�|n| j        }|€t          d¦  «        ‚|j
        €t          j        d	¦  «         d
|_
        |€Nt          t          |dd¦  «        t          ¦  «        rt          j        d¦  «         |j        }nt          ¦   «         }t	          | |||¬¦  «        S )a  Initialize a manager for continuous batching inference.

        Args:
            generation_config: An optional generation configuration, which may contain a CompileConfig object
            continuous_batching_config: An optional continuous batching configuration
            workload_hints: Optional WorkloadHints to help the continuous batching manager make better decisions for
                default values
        Returns:
            `ContinuousBatchingManager`: The manager instance to add requests and retrieve results.
        r,   r.   r-   z;Model must have 'config', 'device', and 'dtype' attributes.rn  NzÄCached continuous batching manager found: it will be re-used instead of creating a new one. If you want to create a new manager, you should call `destroy_cached_continuous_batching_manager` first.z8A GenerationConfig must be provided or set in the model.zE`eos_token_id` not set in GenerationConfig. Setting to -1 (disabled).r  r‡   zŽPassing ContinuousBatchingConfig through GenerationConfig is deprecated. Please pass it separately using the continuous_batching_config kwarg.)r'  r9   r‡   r4  )ÚhasattrÚAttributeErrorr˜   rÉ   r3  r$   ru   rI  r9   rs   r}  rW  r   r‡   )r4   r9   r‡   r4  Úcached_managerÚ
gen_configs         r5   Úinit_continuous_batchingz(ContinuousMixin.init_continuous_batchingG  sw  € õ$ �t˜XÑ&Ô&ð 	`­g°d¸HÑ.EÔ.Eð 	`ÍWÐUYÐ[bÑMcÔMcð 	`Ý Ð!^Ñ_Ô_Ð_õ ! Ð'LÈdÑSÔSˆÝ�nÕ&?Ñ@Ô@ð 	"ÝŒKðuñô ð ð ×5Ò5°dÑ;Ô;Ð;Ø!Ð!ð +<Ð*GÐ&Ð&ÈTÔMcˆ
ØÐÝÐWÑXÔXÐXàÔ"Ð*ÝŒNÐbÑcÔcÐcØ&(ˆJÔ#ð &Ð-Ý�' *Ð.JÈDÑQÔQÕSkÑlÔlð HÝ”ðCñô ð ð .8Ô-RÐ*Ð*å-EÑ-GÔ-GÐ*õ )ØØ(Ø'AØ)ð	
ñ 
ô 
ð 	
r8   c                 óž   — t          | dd¦  «        }t          |t          ¦  «        r&|                     ¦   «          t	          | d¦  «         dS dS )zFDestroy the cached continuous batching manager and free GPU resources.rn  N)r˜   rÉ   r3  ry  Údelattr)r4   rÅ  s     r5   Ú*destroy_cached_continuous_batching_managerz:ContinuousMixin.destroy_cached_continuous_batching_manager‚  s]   € å  Ð'LÈdÑSÔSˆÝ�nÕ&?Ñ@Ô@ð 	AØ×"Ò"Ñ$Ô$Ð$Ý�DÐ?Ñ@Ô@Ð@Ð@Ð@ð	Að 	Ar8   TFrh  ri  Úpersistent_managerr.  c              #   óR  K  — |                       |||¬¦  «        }|re|j        s^t          j        d¦  «         t	          ¦   «         }	|                     ¦   «          t          j        dt	          ¦   «         |	z
  d›d�¦  «         |                     ¦   «          	 |V — t          j        d¦  «         |                     |||¬¦  «         |s| 	                    ¦   «          dS dS # t          j        d¦  «         |                     |||¬¦  «         |s| 	                    ¦   «          w w xY w)	aÌ  A context manager to safely use the continuous batching manager. Arguments are similar to the ones of
        `init_continuous_batching`, except for:
            - block: whether to block the thread when stopping the manager. Default is True.
            - timeout: maximum time to wait for the thread to stop. Default is None (no timeout).
            - warmup: whether to pre-capture CUDA graphs at the largest sizes before running. Default is True.
        )r9   r‡   r4  z%Warming up for continuous batching...zWarming up completed in rt  ru  z!Continuous batching loop finished)rh  ri  rj  N)
rÇ  rG  r$   rW  r   r.  rg  rß   rq  ry  )
r4   r9   rh  ri  r‡   rË  r.  r4  Úmanagerrg  s
             r5   Ú#continuous_batching_context_managerz3ContinuousMixin.continuous_batching_context_manager‰  sR  è è € ð$ ×/Ò/Ø/Ø'AØ)ð 0ñ 
ô 
ˆð
 ð 	V˜'Ô+ð 	VåŒNÐBÑCÔCÐCÝ ‘N”NˆEØ�NŠNÑÔÐÝŒNÐTµl±n´nÀuÑ6LÐTÐTÐTÐTÑUÔUÐUØ�Š‰Œˆð	"ØˆMˆMˆMõ ŒLÐ<Ñ=Ô=Ð=Ø�LŠL˜u¨gÐM_ˆLÑ`Ô`Ð`Ø%ð "Ø—’Ñ!Ô!Ð!Ð!Ð!ð"ð "øõ ŒLÐ<Ñ=Ô=Ð=Ø�LŠL˜u¨gÐM_ˆLÑ`Ô`Ð`Ø%ð "Ø—’Ñ!Ô!Ð!Ð!ð"øøøs   ÂC! Ã!AD&rˆ  r|  Úprogress_barc           	      ó@  — |si S t          j        ¦   «         t          j        k    rt          j        d¦  «         d}|€| j        n|}	|	j        �|	j        nd}
t          |¦  «        |
z  }|                     dd¦  «        }|€|	j	        n|}t          t          d„ |D ¦   «         ¦  «        |�|nd|¬¦  «        }|rt          j        d	¦  «         |                      ||d
d|||¬¦  «        }t          t           g¦  «        }t          || d|› d�d¬¦  «        }i }d}|5 }|5  |5 }	 |                     |||¬¦  «        }||k     r‘|                     d¬¦  «        }|r;|j        }|                     ¦   «         r|||<   |dz  }|                     d¦  «         n8|                     ¦   «         s$t          j        d¦  «         t-          d¦  «         n||k     °‘n0# t.          $ r#}t          j        d|› �d
¬¦  «         Y d}~nd}~ww xY wddd¦  «         n# 1 swxY w Y   ddd¦  «         n# 1 swxY w Y   ddd¦  «         n# 1 swxY w Y   i }g }|D ]4}|                     |¦  «        }|�|||<   Œ|                     |¦  «         Œ5|rt          j        d|› d�¦  «         |S )a-  Generate sequences for a batch of prompts using continuous batching.

        Args:
            inputs: List of input token sequences (prompts)
            generation_config: Optional generation configuration
            continuous_batching_config: Optional continuous batching configuration
            record_timestamps: If set to true, the requests will have a timestamp for each token generated
            progress_bar: If set to true, a progress bar will be displayed
            persistent_manager: whether to persist the manager after the generation is finished. Default is False.
            warmup: whether to pre-capture CUDA graphs before processing requests. Default is True.
        Returns:
            `dict[str, GenerationOutput]`: a dictionary of request ids to GenerationOutput objects
        z=Progress bar is disabled when logger level is less than DEBUGFNr   r{  c              3   ó4   K  — | ]}t          |¦  «        V — Œd S r2   )rµ   )ré   rz  s     r5   ú	<genexpr>z1ContinuousMixin.generate_batch.<locals>.<genexpr>Þ  s(   è è € Ð!IÐ!I°Y¥# i¡.¤.Ð!IÐ!IÐ!IÐ!IÐ!IÐ!Ir8   r   )Úmax_prompt_lengthÚmax_generated_lengthr’  a,  Since you passed `persistent_manager=True`, the manager will be kept alive after the generation is finished. However, it was sized specifically for the requests passed in `generate_batch`. If you plan to reuse the manager for a very different workload, you might want to create a new manager instead.Té   )r9   r‡   rh  ri  rË  r.  r4  zSolving z	 requestsÚrequest)ÚtotalÚdisableÚdescÚunit)rˆ  r{  r|  rs  z*Generation thread terminated unexpectedly.zCReturning results of generate_batch despite unexpected termination.zError during batch generation: r³   z	Requests z not found in results.)r$   ÚgetEffectiveLevelr   rÞ   rW  r9   r7  rµ   rü   r{  r(   rt   rÎ  r   r   r•  rœ  rQ   r  Úupdaterb  r¼   Úprintr»   rP   r_   )r4   rˆ  r9   r‡   r|  rÏ  rË  r.  ÚkwargsÚgen_cfgr7  r’  r{  r4  Ú
manager_cmÚ
logging_cmÚpbar_cmÚresultsÚfinished_countrÍ  Úpbarr“  r›  r%  rÅ   Úreordered_resultsÚmissing_keyss                              r5   Úgenerate_batchzContinuousMixin.generate_batch±  sj  € ð4 ð 	ØˆIõ Ô#Ñ%Ô%­¬Ò6Ð6ÝŒNÐZÑ[Ô[Ð[Ø ˆLð ->Ð,E�$Ô(Ð(ÐK\ˆØ?FÔ?[Ð?g˜wÔ;Ð;ÐmnÐÝ˜6‘{”{Ð%9Ñ9ˆð  ŸšÐ$4°dÑ;Ô;ˆØ3AÐ3I˜Ô/Ð/È~ˆõ 'Ý!Ð!IÐ!IÀ&Ð!IÑ!IÔ!IÑIÔIØ3AÐ3M  ÐSTØ%ð
ñ 
ô 
ˆð
 ð 	ÝŒNðvñô ð ð ×=Ò=Ø/Ø'AØØØ1ØØ)ð >ñ 
ô 
ˆ
õ +­F¨8Ñ4Ô4ˆ
ÝØØ%Ð%Ø3˜LÐ3Ð3Ð3Øð	
ñ 
ô 
ˆð ˆØˆØð 	S˜7 Jð 	Sð 	S°ð 	S¸4ðSØ%×2Ò2Ø!°.ÐTeð 3ñ ô �ð % |Ò3Ð3Ø$×/Ò/¸Ð/Ñ:Ô:�FØð 
Ø!'Ô!2˜Ø!×-Ò-Ñ/Ô/ð +Ø.4˜G F™OØ*¨aÑ/˜NØ ŸKšK¨™NœN˜NøØ$×/Ò/Ñ1Ô1ð ÝœÐ%QÑRÔRÐRåÐcÑdÔdÐdØð % |Ò3Ð3øøõ ð Sð Sð SÝ”ÐB¸qÐBÐBÈTÐRÑRÔRÐRÐRÐRÐRÐRÐRøøøøðSøøøð'	Sð 	Sð 	Sñ 	Sô 	Sð 	Sð 	Sð 	Sð 	Sð 	Sð 	Søøøð 	Sð 	Sð 	Sð 	Sð 	Sð 	Sð 	Sñ 	Sô 	Sð 	Sð 	Sð 	Sð 	Sð 	Sð 	Søøøð 	Sð 	Sð 	Sð 	Sð 	Sð 	Sð 	Sñ 	Sô 	Sð 	Sð 	Sð 	Sð 	Sð 	Sð 	Søøøð 	Sð 	Sð 	Sð 	Sð. ÐØˆØ!ð 	,ð 	,ˆFØ—[’[ Ñ(Ô(ˆFØÐ!Ø,2Ð! &Ñ)Ð)à×#Ò# FÑ+Ô+Ð+Ð+Øð 	KÝŒLÐI \ÐIÐIÐIÑJÔJÐJØ Ð s…   ÄH>ÄH'Ä"HÄ$B/GÇHÇ
H	ÇG<	Ç7HÇ<H	ÈHÈH'ÈHÈH'ÈHÈH'ÈH>È'H+	È+H>È.H+	È/H>È>IÉI)NNNrb   )NTNNFTN)NNFTFT)r<   r=   r>   rc   r   r?   r@   r0  r   r(   r3  rÇ  rÊ  r   r/  r¿  r   rÎ  rd   r€   ÚdictrA   r!   rè  r3   r8   r5   rÁ  rÁ  ;  s#  € € € € € € ðð ð (Ð'Ð'Ñ'à€U„]�_„_ð 6:ØFJØ/3ð	8
ð 8
à+¨dÑ2ð8
ð %=¸tÑ$Cð8
ð &¨Ñ,ð	8
ð
 
#ð8
ð 8
ð 8
ñ „_ð8
ðtAð Að Að Að Ø€U„]�_„_ð 6:ØØ $ØFJØ#(ØØ/3ð#"ð #"à+¨dÑ2ð#"ð ð#"ð ˜‘ð	#"ð
 %=¸tÑ$Cð#"ð !ð#"ð ð#"ð &¨Ñ,ð#"ð 
Ð,Ô	-ð#"ð #"ð #"ñ „_ñ „^ð#"ðL €U„]�_„_ð 6:ØFJØ"'Ø!Ø#(Øðm!ð m!à�T˜#”Y”ðm!ð ,¨dÑ2ðm!ð %=¸tÑ$Cð	m!ð
  ðm!ð ðm!ð !ðm!ð ðm!ð 
ˆcÐ#Ð#Ô	$ðm!ð m!ð m!ñ „_ðm!ð m!ð m!r8   rÁ  )Fr¥  r©   rE   rI   Úabcr   Úcollections.abcr   r   Ú
contextlibr   r   Útimer   Útypingr	   r@   r
   r   Útqdm.contrib.loggingr   Úconfiguration_utilsr   Úgeneration.configuration_utilsr   r   Úutils.genericr   Úutils.import_utilsr   r   Úutils.loggingr   Úlogits_processr   r†   r   Úcb_logits_processorsr   Údistributedr   Úinitializationr   r   Úinput_outputsr   r   r    r   rŸ   r    r$  r!   r"   r#   r$   r…   r%   r&   r'   Úutilsr(   r)   r1  r+   rC   rf   rƒ   r3  rÁ  r3   r8   r5   ú<module>rû     sP  ðð €€€Ø 	€	€	€	Ø €€€Ø Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø /Ð /Ð /Ð /Ð /Ð /Ð /Ð /Ø 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ð 2Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6à 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø XÐ XÐ XÐ XÐ XÐ XÐ XÐ XØ 9Ð 9Ð 9Ð 9Ð 9Ð 9Ø VÐ VÐ VÐ VÐ VÐ VÐ VÐ VØ $Ð $Ð $Ð $Ð $Ð $Ø 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø &Ð &Ð &Ð &Ð &Ð &Ø GÐ GÐ GÐ GÐ GÐ GØ *Ð *Ð *Ð *Ð *Ð *Ø eÐ eÐ eÐ eÐ eÐ eÐ eÐ eØ LÐ LÐ LÐ LÐ LÐ LÐ LÐ LØ %Ð %Ð %Ð %Ð %Ð %Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ø KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KÐ KØ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BÐ BØ -Ð -Ð -Ð -Ð -Ð -Ð -Ð -ðð,ð ð ð ð ˜2œ9ñ ô ð ð,2ð ,2ð ,2ð ,2ð ,2ñ ,2ô ,2ð ,2ð^7ð 7ð 7ð 7ð 7ñ 7ô 7ð 7ðvg(ð g(ð g(ð g(ð g(ñ g(ô g(ð g(ðVO5ð O5ð O5ð O5ð O5ñ O5ô O5ð O5ðdd!ð d!ð d!ð d!ð d!ñ d!ô d!ð d!ð d!ð d!r8   