ó
    ‡Ž¡j… ã                   ó  • S SK r S SKrS SKrS SKrS SKJr  S SKJrJr  S SK	J
r
Jr  S SKJr  S SKJr  S SKrS SKJr  S SKJr  S S	KJr  S
SKJr  S
SKJrJr  S
SKJr  S
SKJrJr  S
SKJ r   SSK!J"r"  SSK#J$r$  SSK%J&r&  SSK'J(r(  SSK)J*r*J+r+  SSK,J-r-J.r.  SSK/J0r0  SSK1J2r2  SSK3J4r4J5r5J6r6J7r7  SSK8J9r9J:r:J;r;  SSK<J=r=J>r>    " S S\R~                  5      r@ " S S 5      rA " S! S"5      rB " S# S$5      rC " S% S&5      rD " S' S(5      rEg))é    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                   ó”   • \ rS rSr% \\S'   \R                  \S'   \R                  \S'   \	S\
SS4S j5       r\	S	\S\4S
 j5       rSrg)ÚProtoPretrainedModeléF   ÚconfigÚdtypeÚdeviceÚattn_implementationÚreturnNc                 ó   • g ©N© )Úselfr0   s     ÚÈC:\Users\johnn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.13_qbz5n2kfra8p0\LocalCache\local-packages\Python313\site-packages\transformers/generation/continuous_batching/continuous_api.pyÚset_attn_implementationÚ,ProtoPretrainedModel.set_attn_implementationK   ó   € àó    Úgeneration_configc                 ó   • g r3   r4   )r5   r;   s     r6   Ú_get_logits_processorÚ*ProtoPretrainedModel._get_logits_processorO   r9   r:   r4   )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r   Ú__annotations__Útorchr.   r/   r   Ústrr7   r   r   r=   Ú__static_attributes__r4   r:   r6   r+   r+   F   s`   ‡ ØÓØ�;‰;ÓØ�L‰LÓàð¸3ð À4ó ó ðð ðÐ7Gð ÐL_ó ó ór:   r+   c                   ób   • \ rS rSrSrSS jrS\SS4S jrS\\   SS4S	 jr	S
\
S\SS4S jrSrg)ÚOutputRouteréT   a  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``.
r1   Nc                 óz   • [         R                  " 5       U l        0 U l        [        R
                  " 5       U l        g r3   )ÚqueueÚQueueÚoutput_queueÚresult_handlersÚ	threadingÚLockÚ_lock©r5   s    r6   Ú__init__ÚOutputRouter.__init__\   s&   € Ü!ŸKšK›MˆÔØVXˆÔÜ—^’^Ó%ˆ�
r:   Úoutputc                 ó  • U R                      U R                  R                  UR                  5      nSSS5        Wb  Uu  p4UR	                  X15        gU R
                  R                  U5        g! , (       d  f       NC= f)zDRoute a single output to its registered handler or the output_queue.N)rQ   rN   ÚgetÚ
request_idÚcall_soon_threadsaferM   Úput)r5   rU   ÚentryÚcallbackÚloops        r6   ÚdeliverÚOutputRouter.delivera   sa   € à�Z‹ZØ×(Ñ(×,Ñ,¨V×->Ñ->Ó?ˆE÷ àÑØ"‰NˆHØ×%Ñ% hÕ7à×Ñ×!Ñ! &Õ)÷ �Zús   �&A0Á0
A>Úoutputsc                 óf  • / nSnU R                      U H^  nU R                  R                  UR                  5      nUb  Uu  pcUR	                  Xd45        MC  U R
                  R                  U5        M`     SSS5        U(       a  Ub  U4S jnUR                  U5        ggg! , (       d  f       N2= f)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                 ó*   • U  H  u  pU" U5        M     g r3   r4   )ÚbatchÚcbÚouts      r6   Ú
_run_batchÚ.OutputRouter.deliver_batch.<locals>._run_batch|   s   € Û$‘G�BÙ�s–Gò  %r:   )rQ   rN   rW   rX   ÚappendrM   rZ   rY   )r5   r`   Ú	callbacksr]   rU   r[   r\   rf   s           r6   Údeliver_batchÚOutputRouter.deliver_batchk   s¤   € ð
 >@ˆ	ØˆØ�Z‹ZÛ!�Ø×,Ñ,×0Ñ0°×1BÑ1BÓC�ØÑ$Ø%*‘N�HØ×$Ñ$ hÐ%7Ö8à×%Ñ%×)Ñ)¨&Ö1ñ "÷ ö ˜Ñ)à!*ô ð ×%Ñ% jÕ1ð *ˆ9÷ �Zús   ‘A%B"Â"
B0ÚstateÚerrorc                 óŒ   • [         R                  Ul        [        U5      Ul        U R                  UR                  5       5        g)z«Fail the request and deliver the output to the output router. This is made from the OutputRouter so that it
can be called even when a batch processor could not be created.N)r#   ÚFAILEDÚstatusrE   rm   r^   Úto_generation_output)r5   rl   rm   s      r6   Úfail_and_deliverÚOutputRouter.fail_and_deliver‚   s1   € ô %×+Ñ+ˆŒÜ˜%“jˆŒØ�‰�U×/Ñ/Ó1Õ2r:   )rQ   rM   rN   ©r1   N)r?   r@   rA   rB   Ú__doc__rS   r!   r^   Úlistrj   r"   Ú	Exceptionrr   rF   r4   r:   r6   rH   rH   T   sS   † ñô&ð
*Ð.ð *°4ô *ð2 TÐ*:Ñ%;ð 2Àô 2ð.3 lð 3¸9ð 3È÷ 3r:   rH   c                   óÄ   • \ rS rSrSrSrSrSrSrSS	 jr	SS
 jr
S\S\SS4S jrS\SS4S jrSS jrS\SS4S jrS\S-  4S jr\S\4S j5       r\S\4S j5       rSrg)ÚBackgroundThreadStatuséŠ   z¢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   r1   Nc                 óŠ   • [         R                  " 5       U l        U R                  U l        U R                  U l        S U l        g r3   )rO   rP   Ú_local_status_lockÚ	DONT_STOPÚ_local_statusÚ
_tp_statusÚfatal_errorrR   s    r6   rS   ÚBackgroundThreadStatus.__init__“   s0   € Ü"+§.¢.Ó"2ˆÔØ!Ÿ^™^ˆÔØŸ.™.ˆŒØˆÕr:   c                 ó¢   • U R                   U l        U R                     U R                   U l        SSS5        SU l        g! , (       d  f       N= f)zƒClear the local and TP statuses. This method should ONLY be called by the main thread itself BEFORE starting
the background thread.N)r}   r   r|   r~   r€   rR   s    r6   ÚclearÚBackgroundThreadStatus.clear™   s:   € ð Ÿ.™.ˆŒØ×$Ó$Ø!%§¡ˆDÔ÷ %àˆÕ÷ %Õ$ús   žA Á 
Arp   Úglobal_rankc                 óH  • XR                   U R                  4;  a  [        SU SU 35      eU R                     [	        XR
                  U R                  5      U l        SSS5        [        R                  " SU SU< SU R
                  < 35        g! , (       d  f       N:= f)zwRequest 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Ú
ValueErrorr|   Úmaxr~   r   r$   Úinfo)r5   rp   r…   s      r6   Úrequest_stopÚ#BackgroundThreadStatus.request_stop¡   sŽ   € ð ×-Ñ-¨t¯~©~Ð>Ó>ÜÐ3°F°8¸;À{ÀmÐTÓUÐUØ×$Ó$Ü!$ V×-?Ñ-?ÀÇÁÓ!QˆDÔ÷ %ä�ŠØ�K�=Ð TÈ6É+ÐUqÐ\`×\nÑ\nÑ[rÐsõ	
÷ %Õ$ús   ¹&BÂ
B!rm   c                 ó^   • U R                   b  [        R                  " SU 35        gXl         g)z3Record a fatal error if none has been recorded yet.Nz:A fatal error was already recorded, ignoring later error: )r€   r$   rm   )r5   rm   s     r6   Úrecord_fatal_errorÚ)BackgroundThreadStatus.record_fatal_error¬   s*   € à×ÑÑ'Ü�LŠLÐUÐV[ÐU\Ð]Õ^à$Õr:   c                 ór   • U R                      U R                  U l        SSS5        g! , (       d  f       g= f)zrMark the background thread as stopped. This should be called by the main thread when the generation loop
finishes.N)r|   ÚSTOPPEDr~   rR   s    r6   Úmark_as_stoppedÚ&BackgroundThreadStatus.mark_as_stopped³   s%   € ð ×$Ó$Ø!%§¡ˆDÔ÷ %×$Ö$ús   �(¨
6Ú	tp_statusc                 óê   • XR                   :  a  [        SU< SU R                   < 35      eXl         U R                     [        U R                  U5      U l        SSS5        g! , (       d  f       g= f)z8Update the local and TP statuses with the new TP status.z1TP communicated a lower stop status: tp_status = z, self._tp_status = N)r   r‰   r|   rŠ   r~   )r5   r•   s     r6   Úupdate_with_tp_statusÚ,BackgroundThreadStatus.update_with_tp_status¹   s]   € à—‘Ó&ÜÐQÀYÁNÐRgÐUY×UdÑUdÑThÐiÓjÐjØ#Œà×$Ó$Ü!$ T×%7Ñ%7¸Ó!CˆDÔ÷ %×$Ö$ús   ¿A$Á$
A2c                 óV   • U R                   b  gU R                  U R                  :w  a  gg)zdIf the background thread cannot accept new requests, return the reason why. Otherwise, returns None.Nz.The background thread died with a fatal error.z"The background thread is stopping.)r€   Úlocal_statusr}   rR   s    r6   Úcan_accept_new_requestsÚ.BackgroundThreadStatus.can_accept_new_requestsÂ   s)   € à×ÑÑ'ØCØ×Ñ §¡Ó.Ø7Ør:   c                 ó   • U R                   $ )zVThe locally requested status, possibly ahead of the value agreed upon by the TP group.)r~   rR   s    r6   rš   Ú#BackgroundThreadStatus.local_statusÊ   s   € ð ×!Ñ!Ð!r:   c                 ó   • U R                   $ )zKThe status last agreed upon by the TP group through a MAX-reduce operation.)r   rR   s    r6   r•   Ú BackgroundThreadStatus.tp_statusÏ   s   € ð �‰Ðr:   )r~   r|   r   r€   rt   )r?   r@   rA   rB   ru   r}   r‡   rˆ   r’   rS   rƒ   ÚintrŒ   rw   r�   r“   r—   rE   r›   Úpropertyrš   r•   rF   r4   r:   r6   ry   ry   Š   sµ   † ñ?ð €IØ€NØ€IØ€Gô ô ð	
 3ð 	
°Sð 	
¸Tô 	
ð%¨	ð %°dô %ô.ðD¨sð D°tô Dð¨¨t©ô ð ð"˜có "ó ð"ð ð˜3ó ó ór:   ry   c                   óî  • \ rS rSr% \\-  \S'   \\S'   S\S\	S\
S\S\S	\R                  S
-  S\R                  S
-  S\S\S\R$                  S\R&                  S\S\SS
4S jrS\4S jrS#S jrS#S jrS\4S jrS\S\SS
4S jrS\4S jrS#S jr S\4S jr!S r"S\SS
4S jr#\RH                  " 5       S\%RL                  SS
4S  j5       r'\RH                  " 5       S\%RL                  SS
4S! j5       r(S"r)g
)$ÚContinuousBatchProcessoréÖ   Úinputs_and_outputsÚ	schedulerÚcacher-   r;   Úcontinuous_batching_configÚlogit_processorÚinput_queueNÚcancel_queueÚoutput_routerÚbackground_thread_statusÚmodel_deviceÚmodel_dtypeÚdistributed_helperr1   c           	      ór  • Xl         X l        X@l        XPl        X`l        Xpl        X€l        X�l        X l        X°l	        XÀl
        XÐl        [        USS5      U l        UR                  U l        U R                  R                  UR                   U
5        [        USS5      c  SOUR"                  U l        UR$                  U l        UUUU
UU R                  S.nU R                  R&                  U l        U R&                  (       a  [)        S	0 UD6U l        O[-        S	0 UD6U l        [/        UUUR0                  UR2                  U R*                  R4                  U R                  S9U l        [9        U R                  U R                  U R                   U R*                  U R                  U R                  S9U l        g)
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_logprobsr4   )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)r5   r¨   r-   r;   r©   rª   r«   r¬   r­   r®   r¯   r°   r§   r±   Ú	io_kwargss                  r6   rS   Ú!ContinuousBatchProcessor.__init__Ú   s•  € ðB Œ
ØŒØ3ŒØ.ÔØ&ÔØ(ÔØ*ÔØ(@Ô%Ø(ÔØ&ÔØ"ŒØ"4Ôô !Ð!2°KÀÓFˆŒØ9×IÑIˆÔð 	×Ñ×+Ñ+Ð,F×,KÑ,KÈ\ÔZô $+¨6Ð3CÀTÓ#JÑ#R™aÐX^×XmÑXmˆÔà %× 6Ñ 6ˆÔð ØØ*DØ"Ø&Ø#×3Ñ3ñ
ˆ	ð #'§.¡.×"CÑ"CˆÔà×"×"Ü&@Ñ&MÀ9Ñ&MˆDÕ#ä&;Ñ&H¸iÑ&HˆDÔ#ô #4ØØØ"<×"NÑ"NØ7×ZÑZØ×2Ñ2×AÑAØ#×6Ñ6ñ#
ˆÔô (Ø ×0Ñ0Ø—n‘nØ—*‘*Ø#×6Ñ6Ø—n‘nØ ×0Ñ0ñ
ˆÕr:   c                 óÐ   • SU R                    SU R                  R                   SU R                  R                   S3U R                  R                  5       R                  5       -   $ )Nz%ContinuousBatchProcessor(input_queue=z, active_requests=z, waiting_requests=Ú))r«   r§   Úactive_requestsÚwaiting_requestsr¦   Úget_model_kwargsÚ__repr__rR   s    r6   rÊ   Ú!ContinuousBatchProcessor.__repr__8  sj   € à3°D×4DÑ4DÐ3Eð FØ#Ÿ~™~×=Ñ=Ð>Ð>QÐRV×R`ÑR`×RqÑRqÐQrÐrsðuà×%Ñ%×6Ñ6Ó8×AÑAÓCñDð	
r:   c                 óÀ   • S U l         [        R                  " 5         [        R                  R                  5       (       a  [        R                  R                  5         g g r3   )r¦   ÚgcÚcollectrD   ÚcudaÚis_availableÚempty_cacherR   s    r6   Ú__del__Ú ContinuousBatchProcessor.__del__?  s;   € Ø"&ˆÔÜ
�
Š
ŒÜ�:‰:×"Ñ"×$Ñ$Ü�J‰J×"Ñ"Õ$ð %r:   c                 óÔ   • U R                   R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         g)z4Reset the batch processor for a new generation loop.N)rÁ   Úresetr§   r¦   r¨   Úfree_all_requestsrR   s    r6   rÕ   ÚContinuousBatchProcessor.resetE  sF   € à×Ñ×%Ñ%Ô'Ø�‰×ÑÔØ×Ñ×%Ñ%Ô'Ø�
‰
×$Ñ$Õ&r:   c                 ód  • U R                   b8  U R                  b+  [        U R                   5      [        U R                  5      4nO/ / 4n[        US   5      [        US   5      -   nU R                  R
                  nU R                  R                  X#5      u  p$U R                  R                  U5        U R                  R                  [        R                  :X  a  gUS:X  a  gU R                  R                  U5      u  pVU HD  n U R                  R                  UR                  5        U R                   R#                  U5        MF     U H  n	U R                   R-                  U	5        M      g! [$         a4  n[&        R(                  " SU 3SS9  U R+                  X‡5         SnAM¦  SnAff = f)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—   r•   ry   rˆ   Útp_broadcast_object_from_rank_0rª   Úcheck_kwargsÚlogit_processor_kwargsr§   Úadd_waiting_requestrw   r$   rm   Ú_handle_request_errorÚset_request_cancellation)
r5   ÚpayloadÚpayload_sizeÚlocal_requested_statusr•   Ú
new_statesÚcancellationsrl   ÚerX   s
             r6   Ú_update_tp_group_stateÚ/ContinuousBatchProcessor._update_tp_group_stateL  s{  € ð
 ×ÑÑ'¨D×,=Ñ,=Ñ,IÜ" 4×#3Ñ#3Ó4´kÀ$×BSÑBSÓ6TÐU‰Gà˜2�hˆGä˜7 1™:“¬¨W°Q©Z«Ñ8ˆð "&×!>Ñ!>×!KÑ!KÐØ"&×"9Ñ"9×"MÑ"MÈlÓ"sÑˆà×%Ñ%×;Ñ;¸IÔFð ×(Ñ(×2Ñ2Ô6L×6VÑ6VÓVØà˜1ÓØà$(×$;Ñ$;×$[Ñ$[Ð\cÓ$dÑ!ˆ
ó  ˆEð5Ø×$Ñ$×1Ñ1°%×2NÑ2NÔOØ—‘×2Ñ2°5Ö9ñ  ó (ˆJØ�N‰N×3Ñ3°JÖ?ñ (àøô ó 5Ü—’Ð=¸a¸SÐAÈDÒQØ×*Ñ*¨1×4Ò4ûð5ús   ÄA E1Å1
F/Å;)F*Æ*F/rm   rl   c                 óÜ   • [        UR                  [        5      (       a+  U R                  R	                  UR                  5      Ul        O/ Ul        U R                  R                  X!5        g)z(Handle general request processing error.N)Ú
isinstancerX   rE   r§   Ú!get_active_request_static_outputsÚgenerated_tokensr­   rr   )r5   rm   rl   s      r6   rá   Ú.ContinuousBatchProcessor._handle_request_errors  sQ   € ô �e×&Ñ&¬×,Ñ,Ø%)§^¡^×%UÑ%UÐV[×VfÑVfÓ%gˆEÕ"à%'ˆEÔ"à×Ñ×+Ñ+¨EÕ9r:   c                 ó  • U R                  5       nU(       a  gU R                  R                  5       nU H  nU R                  R	                  U5        M      U R                  R                  5       (       d  gU R                  R                  U R                  U R                  R                  5      u  pEpgUck  U R                  R                  5       S:X  a  [        S5      eU R                  R                  U R                  U R                  R                  5      u  pEpgUc  Mk  U(       d  gU R                  R                  (       a  U R                  R                  5         U R                  R                  U5        [        R                  " [         R"                  5      (       aƒ  [        R$                  " S['        U5       S['        U R                  R(                  5       S['        U R                  R*                  5       SU SU S	U R                  R-                  5        35        U R.                  R1                  XgU5      u  pgU R2                  R5                  UU R6                  UUUU R.                  R8                  S
9  g)z}Prepare tensors and metadata for the next model forward pass. Returns True if there are requests to process,
False otherwise.Fr   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)r5   Úhard_stoppingÚcancelled_statesrl   rñ   ró   rô   rõ   s           r6   Úprepare_next_batchÚ+ContinuousBatchProcessor.prepare_next_batch}  s#  € ð
 ×3Ñ3Ó5ˆÞØàŸ>™>×BÑBÓDÐã%ˆEØ×#Ñ#×:Ñ:¸5ÖAñ &à�~‰~×2Ñ2×4Ñ4Øð NRÏ^É^×MjÑMjØ×!Ñ! 4§:¡:×#7Ñ#7óN
ÑJÐ°ð  Ñ'à×&Ñ&×7Ñ7Ó9¸QÓ>Ü"Ð#cÓdÐdàQU×Q_ÑQ_×QnÑQnØ×%Ñ% t§z¡z×';Ñ';óRÑNÐ°\ð  Ó'ö !Øà�>‰>×*×*Ø×#Ñ#×4Ñ4Ô6ð 	×Ñ×:Ñ:Ð;LÔMô ×ÒœwŸ}™}×-Ñ-Ü�LŠLØœcÐ"3Ó4Ð5°[ÄÀTÇ^Á^×EdÑEdÓAeÐ@fð gÜ˜tŸ~™~×=Ñ=Ó>Ð?¸yÈÈð WØ&˜- °t·z±z×7UÑ7UÓ7WÐ6XðZôð %)×$5Ñ$5×$FÑ$FÀ|ÐbvÓ$wÑ!ˆà×Ñ×5Ñ5Ø/Ø"×2Ñ2Ø!5Ø%Ø#Ø×)Ñ)×4Ñ4ð 	6ñ 	
ð r:   c                 óØ  • U R                   R                  5       u  pnSn/ nU GHñ  nUR                  nUR                  [        R
                  [        R                  4;   aY  U R                  (       a  UR                  (       a  US-  nMg  [        SUR                  R                   SUR                   S35      eUR                  (       Ga  UR                  5       S:X  a  [        R                  Ul        X$   nUb  X4   OSn	US-  nUR                  X‰5      n
U R                  R!                  XvR"                  5        U
(       a6  U R$                  R'                  UR                  5        SU R$                  l        UR*                  (       d  UR                  [        R
                  :X  a"  UR-                  UR/                  5       5        GM¨  GM«  UR                  [        R0                  :X  d  GMÌ  U R                  R!                  XvR"                  5        GMô     U(       a  U R2                  R5                  U5        / / pËU R$                  R6                  (       GaŠ  U R$                  R6                  R9                  5       nUR:                  nSUl        [=        U5       Vs/ s H  oýR                   SU 3PM     nn[?        XàR                  RA                  UR                  5      5      nUU-
  n[=        U5       HE  nUR9                  5       nURC                  5       nUUl        U R$                  RE                  U5        MG     US:X  a  GM  U H+  nURG                  U5      U R$                  RH                  U'   M-     U R                  RK                  UR                  U5      u  nnURM                  U5        URM                  U5        U R$                  R6                  (       a  GMŠ  U(       ai  U R                   RN                  nUb  [P        RR                  RU                  U5      O	[W        5       nU   U R                  RY                  X¼5        SSS5        ggs  snf ! , (       d  f       g= f)	z0Update request states based on generated tokens.r   r   zTried to update z	 request z in sync mode.NFz__child#)-r¦   Úprepare_batch_updaterl   rp   r#   ÚFINISHEDÚPENDINGr¾   Úhas_new_tokenrý   ÚnamerX   Úgenerated_lenÚDECODINGÚupdate_and_check_completionr¨   Ú!mark_shareable_blocks_as_completeÚcomplete_blocksr§   Úfinish_requestÚblock_new_requestsÚ	streamingrh   rq   Ú
PREFILLINGr­   rj   Ú_requests_to_forkÚpopÚnum_childrenÚrangeÚminÚcompute_max_num_forksÚ!create_equivalent_initial_requestrà   ÚforkrÇ   Úfork_requestÚextendr·   rD   rÏ   Ústreamr   Ú
copy_cache)r5   rñ   Ú
new_tokensÚlogprobsÚcurrent_logits_indexÚpending_outputsÚfuture_staterl   ÚtokenÚlogprobÚis_finishedÚcopy_sourceÚcopy_destinationÚstate_to_forkr  ÚiÚnew_request_idsÚnum_to_forkÚnum_to_scheduleÚ_Únew_request_idÚchild_stateÚcopy_srcÚcopy_dstr·   Úmaybe_streams                             r6   Úupdate_batchÚ%ContinuousBatchProcessor.update_batch½  s�  € à26×2IÑ2I×2^Ñ2^Ó2`Ñ/Ð xØ ÐØˆÜ-ˆLØ ×&Ñ&ˆEà�|‰|¤× 6Ñ 6¼×8MÑ8MÐNÓNØ×*×*à#×1×1Ø,°Ñ1Ð,ÙÜ"Ð%5°e·l±l×6GÑ6GÐ5HÈ	ÐRW×RbÑRbÐQcÐcqÐ#rÓsÐsà×)×)Ð)à×&Ñ&Ó(¨AÓ-Ü#0×#9Ñ#9�E”Là"Ñ8�Ø<DÑ<P˜(Ò8ÐVZ�Ø$¨Ñ)Ð$ð $×?Ñ?ÀÓO�à—
‘
×<Ñ<¸U×D`ÑD`ÔaÞØ—N‘N×1Ñ1°%×2BÑ2BÔCØ8=�D—N‘NÔ5Ø—?—? e§l¡l´m×6LÑ6LÓ&LØ#×*Ñ*¨5×+EÑ+EÓ+G×Hò 'Mð —‘¤×!9Ñ!9Ö9Ø—
‘
×<Ñ<¸U×D`ÑD`×añ? .öB Ø×Ñ×,Ñ,¨_Ô=ð )+¨BÐ%Ø�n‰n×.×.Ð.à ŸN™N×<Ñ<×@Ñ@ÓBˆMØ(×5Ñ5ˆLØ)*ˆMÔ&ÜQVÐWcÔQdÓeÒQdÈA×":Ñ":Ð!;¸8ÀAÀ3ÓGÑQdˆOÐeä˜l¯J©J×,LÑ,LÈ]×MeÑMeÓ,fÓgˆKØ*¨[Ñ8ˆOÜ˜?Ö+�Ø!0×!4Ñ!4Ó!6�Ø+×MÑMÓO�Ø)7�Ô&Ø—‘×2Ñ2°;Ö?ñ	 ,ð ˜aÓÚã"1�ØAN×ASÑASÐTbÓAc�—‘×.Ñ.¨~Ó>ñ #2ð "&§¡×!8Ñ!8¸×9QÑ9QÐSbÓ!cÑˆH�hØ×Ñ˜xÔ(Ø×#Ñ# HÔ-ð/ �n‰n×.×.Ò.ö4 ð "×4Ñ4×CÑCˆNØ@NÑ@Zœ5Ÿ:™:×,Ñ,¨^Ô<Ô`kÓ`mˆLÚØ—
‘
×%Ñ% kÔD÷ �ð ùò+ f÷4 •ús   ÊQÐ0QÑ
Q)c                 ó6   • U R                   R                  5       $ )z2Check if there are any active or waiting requests.)r§   rù   rR   s    r6   rù   Ú-ContinuousBatchProcessor.has_pending_requests
  s   € à�~‰~×2Ñ2Ó4Ð4r:   c                 óä   • U R                   R                  5       S   nU HM  nU R                  XR                  5        U R                  R                  UR                  R                  5        MO     g)z&Handle errors during batch processing.r   N)r¦   r  rá   rl   r§   r  rX   )r5   rm   Úfailed_future_statesr*  s       r6   Úhandle_batch_errorÚ+ContinuousBatchProcessor.handle_batch_error  sX   € à#×6Ñ6×KÑKÓMÈaÑPÐÛ0ˆLØ×&Ñ& u×.@Ñ.@ÔAØ�N‰N×)Ñ)¨,×*<Ñ*<×*GÑ*GÖHò 1r:   c                 ó,  • [        U R                  R                  R                  5       5      nU H9  nU R	                  X5        U R                  R                  UR                  5        M;     U R                  R                  5         [        U R                  R                  R                  5       5       H9  nU R                  R                  R                  U5      nU R	                  X5        M;     U R                  R                  R                  5         g)z.Fail all active requests with the given error.N)rv   r§   rÇ   Úvaluesrá   r  rX   rÁ   Úfree_all_waiting_cpu_cachesrÈ   Úkeysr  Úwaiting_requests_orderrƒ   )r5   rm   Úrequestsrl   Úreq_ids        r6   Úfail_all_requestsÚ*ContinuousBatchProcessor.fail_all_requests  sÈ   € ô ˜Ÿ™×6Ñ6×=Ñ=Ó?Ó@ˆÛˆEØ×&Ñ& uÔ4Ø�N‰N×)Ñ)¨%×*:Ñ*:Ö;ñ ð
 	×Ñ×;Ñ;Ô=Ü˜4Ÿ>™>×:Ñ:×?Ñ?ÓAÖBˆFØ—N‘N×3Ñ3×7Ñ7¸Ó?ˆEØ×&Ñ& uÖ4ñ Cð
 	�‰×-Ñ-×3Ñ3Õ5r:   Úmodelc                 óÈ   • U R                   R                  U R                  R                  S9nU R                  R	                  X5        U R                   R                  5         g)z!Perform a single generation step.)rö   N)r¦   rÉ   rÂ   r  Úcompute_batchÚretrieve_device_outputs)r5   rL  Ú
batch_datas      r6   Ú_generation_stepÚ)ContinuousBatchProcessor._generation_step&  sS   € ð
 ×,Ñ,×=Ñ=È$×J[ÑJ[×JfÑJfÐ=Ðgˆ
ð 	×Ñ×'Ñ'¨Ô:ð 	×Ñ×7Ñ7Õ9r:   c                 ó:   • U R                   R                  U5        g)a7  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)r5   rL  s     r6   rT  ÚContinuousBatchProcessor.warmup5  s   € ð
 	×Ñ× Ñ  Õ'r:   )r®   r¨   r¬   r¸   r-   r±   r³   r«   r¦   rª   r½   r¯   r°   rÂ   rÁ   r­   r¹   r§   r´   r¾   rt   )*r?   r@   rA   rB   r   r   rC   r'   r   r   r   r   r   rK   rL   rH   ry   rD   r/   r.   r   rS   rE   rÊ   rÒ   rÕ   Úboolré   rw   r"   rá   r	  r;  rù   rA  rJ  Úno_gradr
   ÚModulerQ  rT  rF   r4   r:   r6   r¤   r¤   Ö   sœ  ‡ Ø-Ð0JÑJÓJØÓð\
à"ð\
ð !ð\
ð ,ð	\
ð
 %=ð\
ð ?ð\
ð —[‘[ 4Ñ'ð\
ð —k‘k DÑ(ð\
ð $ð\
ð #9ð\
ð —l‘lð\
ð —[‘[ð\
ð ð\
ð .ð\
ð 
ô\
ð|
˜#ô 
ô%ô'ð%¨ô %ðN:¨9ð :¸\ð :Èdô :ð> Dô >ô@KEðZ5 dô 5òIð6 yð 6°Tô 6ð" ‡]‚]ƒ_ð: b§i¡ið :°Dó :ó ð:ð ‡]‚]ƒ_ð(˜BŸI™Ið (¨$ó (ó ó(r:   r¤   c                   ó€  • \ rS rSrSr S0S\S\S\S\S-  SS4
S	 jjr	S\SS4S
 jr
S1S jrS\4S jrS1S jr    S2S\S\S-  S\S\SS4
S jjrS0S\S\S-  SS4S jjrS1S jr     S3S\\   S\S-  S\S-  S\S\S\\\   -  S-  S\S\S-  4S jjr   S4S\\\      S\S-  S\S\S\S\\   4S jjrS\SS4S  jrS5S\S-  S\S-  S\S-  4S! jjrS" rS\S\\   4S# jrS\S$\SS4S% jr S&\!S'\S\4S( jr"\#RH                  " 5       S1S) j5       r%S1S* jr&S\!4S+ jr'S,\(S&\!S-  SS4S- jr)S,\(S&\!S-  SS4S. jr*S/r+g)6ÚContinuousBatchingManageri>  a¤  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`
NrL  r;   r©   Úworkload_hintsr1   c                 ó  • [         R                  " UR                  S9U l        [         R                  " 5       U l        SU l        [        R                  " 5       U l        [        R                  " 5       U l
        [        5       U l        [        5       U l        SU l        SU l        SU l        SU l        U R'                  U5        UR)                  5       U l        X l        [/        USS5      nUb  UOSU l        [3        [/        U R*                  SS5      UR4                  S9U l        U R6                  R8                  U l        UR:                  (       a  U R6                  R=                  5         [?        U R*                  RA                  U5      URB                  URD                  S	9U l#        [I        U R*                  RJ                  RM                  5       UUU RF                  RN                  S
9U l(        U RP                  RR                  U l*        g)aI  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¸   r[  Úhas_logit_processors)+rK   rL   Úmax_queue_sizer«   r¬   Ú_request_counterrO   rP   Ú_request_lockÚEventÚ_has_new_requestsry   r®   rH   r­   Úbatch_processorÚ_generation_threadÚ	warmed_upÚ_original_attn_implÚswitch_to_cb_friendly_attnÚevalrL  r;   rº   r^  r   ra  r±   Úis_tp_driverÚdisable_nccl_graph_mixingÚmaybe_warn_nccl_graph_mixingr   r=   rc  rd  rª   r   r-   Úget_text_configÚdo_processingr©   Úallow_block_sharingÚ_use_prefix_sharing)r5   rL  r;   r©   r[  r^  s         r6   rS   Ú"ContinuousBatchingManager.__init__G  s¯  € ô  !Ÿ;š;Ð/I×/XÑ/XÑYˆÔÜ.3¯kªk«mˆÔØ !ˆÔÜ&Ÿ^š^Ó-ˆÔÜ!*§¢Ó!2ˆÔô )?Ó(@ˆÔ%Ü)›^ˆÔØ@DˆÔØ"&ˆÔð ˆŒð $(ˆÔ Ø×'Ñ'¨Ô.Ø—Z‘Z“\ˆŒ
ð "3ÔÜ&Ð'8Ð:PÐRVÓWÐØ<PÑ<\Ñ$8ÐbcˆÔ!ô #4Ü §
¡
¨N¸DÓAØ8×JÑJñ#
ˆÔð !×3Ñ3×@Ñ@ˆÔà%×?×?Ø×#Ñ#×@Ñ@ÔBô  EØ!ŸZ™Z×=Ñ=Ð>OÓPØ#=×#TÑ#TØ(B×(^Ñ(^ñ 
ˆÔô +MØ—:‘:×$Ñ$×4Ñ4Ó6Ø0Ø)Ø!%×!5Ñ!5×!CÑ!Cñ	+
ˆÔ'ð $(×#BÑ#B×#VÑ#VˆÕ r:   c                 óž  • UR                   R                  nUn[        US9nSU;   nU(       d  U(       dx  UR                  (       ag  [	        SS9(       a  SnO[        SS9(       a  SnOSnSnUb%  S	U 3n[        R                  " U S
U SU S35        O[        R                  " U S35        SU;  a  UR                  SU 35        X l
        gg)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$   Úwarningr‹   r7   rn  )r5   rL  Úoriginal_attn_implÚtarget_implemÚis_flashÚis_pagedÚversionÚmsgs           r6   ro  Ú4ContinuousBatchingManager.switch_to_cb_friendly_attn‹  sà   € ð #Ÿ\™\×>Ñ>ÐØ*ˆô 0ÐS`ÑaˆØ˜}Ñ,ˆÞ¦¨U×-G×-Gä(¸T×BØ‘Ü*¸t×DØ‘à�àRˆCØÑ"Ø"2°7°)Ð <�Ü—’Ø�eÐ+Ð,>Ð+?¸tÀMÀ?ð Stð tõô
 —’˜s˜eÐ#hÐiÔjð ˜=Ó(Ø×)Ñ)¨F°=°/Ð*BÔCØ'9Õ$ð )r:   c                 ó    • U R                   c  U R                  5       U l         U R                   R                  U R                  5        SU l        g)z‚Pre-capture CUDA graphs for varlen and decode paths by running dummy batches. Initializes the batch
processor if not already done.NT)rk  Ú_create_batch_processorrT  rL  rm  rR   s    r6   rT  Ú ContinuousBatchingManager.warmup­  s@   € ð ×ÑÑ'Ø#'×#?Ñ#?Ó#AˆDÔ Ø×Ñ×#Ñ# D§J¡JÔ/Øˆ�r:   c                 ó`   • U R                   SL=(       a    U R                   R                  5       $ )zUReturns True if the background generation thread has been started and is still alive.N)rl  Úis_aliverR   s    r6   Ú
is_runningÚ$ContinuousBatchingManager.is_running·  s'   € à×&Ñ&¨dÐ2×Y°t×7NÑ7N×7WÑ7WÓ7YÐYr:   c                 ó
  • U R                  5       (       a  [        R                  " S5        gU R                  R	                  5         [
        R                  " U R                  S9U l        U R                  R                  5         g)z'Start the background generation thread.z"Manager thread is already running.N)Útarget)
rŒ  r$   r  r®   rƒ   rO   ÚThreadÚ_run_generation_looprl  ÚstartrR   s    r6   r’  ÚContinuousBatchingManager.start»  s\   € à�?‰?×ÑÜ�NŠNÐ?Ô@ØØ×%Ñ%×+Ñ+Ô-Ü"+×"2Ò"2¸$×:SÑ:SÑ"TˆÔØ×Ñ×%Ñ%Õ'r:   ÚblockÚtimeoutÚkeep_for_next_sessionÚ	hard_stopc                 ó(  • U R                   c  [        R                  " S5        U R                  c%  SnU(       a  US-  n[        R                  " U5        g[	        5       nU(       a  [
        R                  O[
        R                  nU R                  R                  XpR                  R                  5        U(       a  U R                  Xb5        U(       d  SU l         O&[        R                  " S5        X R                  l        U R                   b,  U R                  R#                  U R                   5        SU l        [$        R&                  " 5         [(        R*                  R-                  5       (       a  [(        R*                  R/                  5         gg)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.)rk  r$   r  rl  r   ry   rˆ   r‡   r®   rŒ   r±   r…   Újoinr‹   rL  Ú#_cached_continuous_batching_managerrn  r7   rÍ   rÎ   rD   rÏ   rÐ   rÑ   )r5   r”  r•  r–  r—  r…  Ústop_trigger_timeÚstop_statuss           r6   ÚstopÚContinuousBatchingManager.stopÄ  s%  € ð ×ÑÑ'Ü�NŠNÐCÔDð ×"Ñ"Ñ*Ø(ˆCÞ$ØÐXÑX�Ü�NŠN˜3ÔØô )›NÐÞ:CÔ,×6Ò6ÔI_×InÑInˆØ×%Ñ%×2Ñ2°;×@WÑ@W×@cÑ@cÔdæØ�I‰IÐ'Ô1ö %Ø#'ˆDÕ ô �KŠKÐTÔUØ=A�J‰JÔ:ð ×#Ñ#Ñ/Ø�J‰J×.Ñ.¨t×/GÑ/GÔHØ'+ˆDÔ$ô 	�
Š
ŒÜ�:‰:×"Ñ"×$Ñ$Ü�J‰J×"Ñ"Õ$ð %r:   r›  c                 ó"  • U R                   c  gU R                   R                  US9  U R                   R                  5       (       a  [        R                  " SU S35        g[        5       n[        R                  " SX1-
  S S35        SU l         g)zeWait for the background thread to finish. Wait can be capped using the timeout argument (in seconds).N©r•  z3Generation thread did not exit after join timeout (z).z+Background generation thread stopped after ú.2fús.)rl  r™  r‹  r$   r  r   r‹   )r5   r›  r•  Úends       r6   r™  ÚContinuousBatchingManager.joinö  s‡   € ð ×"Ñ"Ñ*Øð 	×Ñ×$Ñ$¨WÐ$Ñ5Ø×"Ñ"×+Ñ+×-Ñ-Ü�NŠNÐPÐQXÐPYÐY[Ð\Õ]ä“.ˆCÜ�KŠKÐEÀcÑF]Ð^aÐEbÐbdÐeÔfØ&*ˆDÕ#r:   c                 ó‚   • U R                  5       (       a  U R                  SSS9  U R                  R                  5         g)zŠTerminate the manager and release distributed resources. Safe to call multiple times. After calling this,
the manager cannot be restarted.TF)r”  r–  N)rŒ  r�  r±   Údestroy_cpu_comm_grouprR   s    r6   ÚdestroyÚ!ContinuousBatchingManager.destroy  s3   € ð �?‰?×ÑØ�I‰I˜D¸ˆIÑ>Ø×Ñ×6Ñ6Õ8r:   Ú	input_idsrX   Úmax_new_tokensr  Úrecord_timestampsÚeos_token_idrß   c                 ó†  • U R                   (       d  gU R                  R                  5       nUb3  USS  SS S-   USS  SS -   n	[        R                  " U SU	 S35        gUc9  U R
                     S	U R                   3nU =R                  S-  sl        SSS5        Uc  U R                  R                  OUnUc  U R                  R                  OUn[        U[        U5      U R                  S-
  UUUUUS
9n
U R                  R                  U
SSS9  U R                  R!                  5         U$ ! , (       d  f       N¡= f)ao  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   z. Request with ids z will be dropped.Úreq_)rX   Úinitial_tokensr  r«  rª  r¬  r  rß   Té
   ©r”  r•  )rq  r®   r›   r$   r  rh  rg  r;   rª  r¬  r"   rv   r^  r«   rZ   rj  Úset)r5   r©  rX   rª  r  r«  r¬  rß   Ú
denial_msgÚpreviewrl   s              r6   Úadd_requestÚ%ContinuousBatchingManager.add_request  sW  € ð2 × × Øà×2Ñ2×JÑJÓLˆ
ØÑ!Ø" 2 A˜˜¨¨"Ð-°	Ñ9¸yÈÈ¸~Ð>NÐPQÐPRÐ<SÑSˆGÜ�NŠN˜j˜\Ð)<¸W¸IÐEVÐWÔXØàÑØ×#Ó#Ø# D×$9Ñ$9Ð#:Ð;�
Ø×%Ò%¨Ñ*Õ%÷ $ð CQÑBX˜×/Ñ/×>Ò>Ð^lˆØ>JÑ>R�t×-Ñ-×:Ò:ÐXdˆô Ø!Ü 	›?Ø×2Ñ2°QÑ6Ø/Ø)Ø%ØØ#9ñ	
ˆð 	×Ñ×Ñ˜U¨$¸ÐÑ;Ø×Ñ×"Ñ"Ô$ØÐ÷+ $Õ#ús   Á2%D2Ä2
E Úinputsc                 ó8  • [        U5      nU R                     [        U R                  U R                  U-   5       Vs/ s H  nSU 3PM
     nnU =R                  U-  sl        SSS5        [	        [        WU5      5      n	U R                  (       a  [        U	S SS9n	U R                  R                  n
U
c   U R                  R                  R                  OU
n
U
c  SOU
n
U	 H  u  p¼U R                  " SUUUUUU
S.UD6  M      U$ s  snf ! , (       d  f       N­= f)	zgUtility function to batch `add_request` and return their IDs. Check its documentation for more details.r°  Nc                 ó   • U S   $ )Nr   r4   )Úxs    r6   Ú<lambda>Ú8ContinuousBatchingManager.add_requests.<locals>.<lambda>Y  s   € À!ÀAÂ$r:   T)ÚkeyÚreverser®  )r©  rX   rª  r  r«  r¬  r4   )rÛ   rh  r  rg  rv   Úziprw  Úsortedr;   r¬  rL  r-   r·  )r5   r¹  rª  r  r«  rß   Únum_requestsr1  Úrequest_idsÚids_and_inputsr¬  rX   r©  s                r6   Úadd_requestsÚ&ContinuousBatchingManager.add_requestsH  s  € ô ˜6“{ˆØ×ÓÜ/4°T×5JÑ5JÈD×LaÑLaÐdpÑLpÔ/qÓrÒ/q¨!˜T ! ›:Ñ/qˆKÐrØ×!Ò! \Ñ1Õ!÷  ô œc +¨vÓ6Ó7ˆØ×#×#Ü# N¹ÐPTÑUˆNà×-Ñ-×:Ñ:ˆØ9EÑ9M�t—z‘z×(Ñ(×5Ò5ÐS_ˆØ)Ñ1‘r°|ˆã%3Ñ!ˆJØ×Òð Ø#Ø%Ø-Ø#Ø"3Ø)ñð )ôñ &4ð Ðùò+ s÷  Õús   ˜&D¾DÁDÄDÄ
Dc                 ó’   • U R                   (       a6  U R                  R                  U5        U R                  R	                  5         gg)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)rq  r¬   rZ   rj  r´  )r5   rX   s     r6   Úcancel_requestÚ(ContinuousBatchingManager.cancel_requestk  s8   € ð ××Ø×Ñ×!Ñ! *Ô-Ø×"Ñ"×&Ñ&Õ(ð r:   c                 ó’  • U R                   R                  R                  5       (       a%  U R                  b  U R                  R
                  b  g U R                   R                  R                  SUS9nUb6  UR                  U:w  a&  U R                   R                  R                  U5        gU$ ! [        R                   a     gf = f)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³  )r­   rM   Úemptyrl  r®   r€   rW   rX   rZ   rK   ÚEmpty)r5   rX   r•  Úresults       r6   Ú
get_resultÚ$ContinuousBatchingManager.get_resultt  s°   € ð ×Ñ×*Ñ*×0Ñ0×2Ñ2Ø×&Ñ&Ñ.°$×2OÑ2O×2[Ñ2[Ñ2gØð	Ø×'Ñ'×4Ñ4×8Ñ8¸tÈWÐ8ÐUˆFØÑ%¨&×*;Ñ*;¸zÓ*IØ×"Ñ"×/Ñ/×3Ñ3°FÔ;ØØˆMøÜ�{‰{ó 	Ùð	ús   ÁAB/ Â-B/ Â/CÃCc              #   óò   #   • U R                   bf  U R                   R                  5       (       aF  U R                  SS9nUb  Uv •  U R                   b"  U R                   R                  5       (       a  MD  gggg7f)z.Iterate over results as they become available.Nçš™™™™™¹?r   )rl  r‹  rÏ  )r5   rÎ  s     r6   Ú__iter__Ú"ContinuousBatchingManager.__iter__…  sk   é € à×%Ñ%Ñ1°d×6MÑ6M×6VÑ6V×6XÑ6XØ—_‘_¨S�_Ð1ˆFØÑ!Ø’ð ×%Ñ%Ñ1°d×6MÑ6M×6VÑ6V×6XÔ6XÐ1Ð6XÐ1ùs   ‚A/A7Á3A7c              #   ó   #   • U R                   b}  U R                   R                  5       (       a]  U R                  USS9nUb  Uv •  UR                  5       (       a  gU R                   b"  U R                   R                  5       (       a  M[  gggg7f)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Ò  )rX   r•  )rl  r‹  rÏ  r-  )r5   rX   rÎ  s      r6   Úrequest_id_iterÚ)ContinuousBatchingManager.request_id_iterŒ  s�   é € ð ×%Ñ%Ñ1°d×6MÑ6M×6VÑ6V×6XÑ6XØ—_‘_°
ÀC�_ÐHˆFØÑ!Ø’Ø×%Ñ%×'Ñ'Øð ×%Ñ%Ñ1°d×6MÑ6M×6VÑ6V×6XÔ6XÐ1Ð6XÐ1ùs   ‚BBÂ
Br\   c                 óØ   ^ ^^• [         R                  " 5       nUUU 4S jnT R                  R                     XC4T R                  R                  T'   SSS5        g! , (       d  f       g= f)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                 óî   >• T" U 5        U R                  5       (       aF  TR                  R                     TR                  R                  R	                  TS 5        S S S 5        g g ! , (       d  f       g = fr3   )r-  r­   rQ   rN   r  )rÎ  r\   rX   r5   s    €€€r6   Ú_auto_cleanupÚHContinuousBatchingManager.register_result_handler.<locals>._auto_cleanup¦  sY   ø€ Ù�VÔØ×!Ñ!×#Ñ#Ø×'Ñ'×-Ó-Ø×&Ñ&×6Ñ6×:Ñ:¸:ÀtÔL÷ .Ð-ð $ß-Õ-ús   µ'A&Á&
A4N)ÚasyncioÚget_running_loopr­   rQ   rN   )r5   rX   r\   r]   rÚ  s   ```  r6   Úregister_result_handlerÚ1ContinuousBatchingManager.register_result_handler™  sO   ú€ ô ×'Ò'Ó)ˆ÷	Mð ×Ñ×%Ó%Ø>KÐ=RˆD×Ñ×.Ñ.¨zÑ:÷ &×%Ö%ús   ·AÁ
A)rk  Úbootstrappingc                 óâ  • UR                  5       nU(       a>  U R                  5         U =R                  S-  sl        U(       a  gUR                  5         gU R                  R
                  [        R                  :X  a  gU R                  R
                  [        R                  :X  a  UR                  5       (       d  gU R                  R                  SS9  U R                  R                  5         g)a  Body of the generation loop. Returns True if the loop should continue, False otherwise. Behaves differently
if this is for bootstrapping an async run: in that case, there is no need to update the batch, and the first
step should exit the bootstrapping loop.r   FTrÒ  r   )r	  rQ  Úcurrent_batchr;  r®   r•   ry   rˆ   r‡   rù   rj  Úwaitrƒ   )r5   rk  rà  Úrequests_availables       r6   Ú_generation_loop_bodyÚ/ContinuousBatchingManager._generation_loop_body±  sÃ   € ð
 -×?Ñ?ÓAÐÞØ×!Ñ!Ô#Ø×Ò !Ñ#ÕÞØà×,Ñ,Ô.Øà×*Ñ*×4Ñ4Ô8N×8XÑ8XÓXØð ×)Ñ)×3Ñ3Ô7M×7\Ñ7\Ó\Ø#×8Ñ8×:Ñ:àð ×"Ñ"×'Ñ'°Ð'Ñ4Ø×"Ñ"×(Ñ(Ô*Ør:   c                 ój  • Sn U R                   R                  R                  S:X  aT  U R                   R                  R                  b3  [        R
                  R                  U R                   R                  5        U R                  5       nXl        SU l	        UR                  (       a-  U R                  USS9(       a   U R                  USS9(       a  M  U R                  USS9(       a   U R                  USS9(       a  M  [        UR                  [        5      (       a8  SUR                  R                  -
  UR                  l        UR!                  5         [#        SU R$                  R&                  < 35      nU R)                  X!5        U R$                  R3                  5         [,        R4                  " S5        g! [*         a3  n[,        R.                  " S	U 3SS
9  U R1                  X15         SnANiSnAff = f! U R$                  R3                  5         [,        R4                  " S5        f = f)z6Main processing loop running in the background thread.NrÏ   r   T)rà  Fr   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.)rL  r/   ÚtypeÚindexrD   rÏ   Ú
set_devicerˆ  rk  râ  r¾   rå  rì   r¦   r   Úcurrent_pairr;  rý   r®   r•   Ú_fail_all_remaining_requestsrw   r$   rm   Ú_handle_critical_errorr“   r‹   )r5   rk  rm   rè   s       r6   r‘  Ú.ContinuousBatchingManager._run_generation_loopÎ  sÑ  € ð ˆð%	_à�z‰z× Ñ ×%Ñ%¨Ó/°D·J±J×4EÑ4E×4KÑ4KÑ4WÜ—
‘
×%Ñ% d§j¡j×&7Ñ&7Ô8ð #×:Ñ:Ó<ˆOØ#2Ô Ø!"ˆDÔð ×1×1Ø×0Ñ0°ÐPTÐ0ÖUØð ×0Ñ0°ÐPTÐ0×UÐUð ×,Ñ,¨_ÈEÐ,ÖRØð ×,Ñ,¨_ÈEÐ,×RÐRô
 ˜/×<Ñ<Ô>X×YÑYØBCÀo×FhÑFh×FuÑFuÑBu�×2Ñ2Ô?Ø×,Ñ,Ô.ô !ØwÈT×MjÑMj×MtÑMtÑLxÐyóˆEð ×-Ñ-¨eÔEð ×)Ñ)×9Ñ9Ô;Ü�KŠKÐ]Õ^øô ó 	<Ü�LŠLÐ5°a°SÐ9ÀDÒIØ×'Ñ'¨×;Ñ;ûð	<ûð ×)Ñ)×9Ñ9Ô;Ü�KŠKÐ]Õ^ús7   „CG  Ã+G  ÄBG  Ç 
G=Ç
)G8Ç3H  Ç8G=Ç=H  È 2H2c                 ó~   • U R                   c  [        S5      eU R                   R                  U R                  5        g)z=Perform a single generation step. This is mostly cuda graphedNzNTried to perform a generation step before the batch processor was initialized.)rk  rý   rQ  rL  rR   s    r6   rQ  Ú*ContinuousBatchingManager._generation_stepû  s2   € à×ÑÑ'ÜÐoÓpÐpØ×Ñ×-Ñ-¨d¯j©jÕ9r:   c                 óæ  • [        U SS5      n[        U[        5      (       a  UR                  5         U$ [	        U R
                  R                  R                  5       U R                  U R
                  R                  U R                  [        U R
                  S0 5      U R
                  R                  S9nUR                  U l        [        U R                  UR                  UR                   U R                  S9  UR"                  S:”  a  SU R                  l        U R                  R&                  n[(        R*                  " US5      nUc   [,        R.                  " SU S35        [0        nU" UU R                  R2                  U R                  R4                  S	9n[        UU R
                  R                  R                  5       U R6                  U R                  U R8                  U R:                  (       a  U R<                  OSU R:                  (       a  U R>                  OSU R@                  U RB                  U R
                  R                  U R
                  R                  UU R                  S
9nU$ )ziCreate a new batch processor. If an already initialized batch processor exists, it is reset and returned.rk  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-   r;   r©   rª   r«   r¬   r­   r®   r¯   r°   r§   r±   )"rº   rì   r¤   rÕ   r   rL  r-   rt  r©   r/   r±   r.   rô  rw  r   ró  r½   Únum_sliding_attention_groupsÚmax_blocks_per_requestÚscheduler_typer%   rW   r$   r  r&   rõ  rö  r;   rª   rq  r«   r¬   r­   r®   )r5   rk  Úpaged_attention_cacherù  Úscheduler_clsr§   s         r6   rˆ  Ú1ContinuousBatchingManager._create_batch_processor  s  € ô " $Ð(9¸4Ó@ˆÜ�oÔ'?×@Ñ@Ø×!Ñ!Ô#Ø"Ð"ô !4Ø—:‘:×$Ñ$×4Ñ4Ó6Ø'+×'FÑ'FØ—:‘:×$Ñ$Ø#×6Ñ6Ü˜DŸJ™J¨	°2Ó6Ø—*‘*×"Ñ"ñ!
Ðð $9×#KÑ#KˆÔ ä-Ø×5Ñ5Ø,×7Ñ7Ø2×CÑCØ#×7Ñ7ò		
ð !×=Ñ=ÀÓAØEFˆD×+Ñ+ÔBð ×8Ñ8×GÑGˆÜ)×-Ò-¨n¸dÓCˆØÑ Ü�NŠN˜[¨Ð(8Ð8XÐYÔZÜ)ˆMá!Ø'Ø×9Ñ9×GÑGØ#'×#BÑ#B×#YÑ#Yñ
ˆ	ô 3Ø'Ø—:‘:×$Ñ$×4Ñ4Ó6Ø"×4Ñ4Ø'+×'FÑ'FØ ×0Ñ0Ø,0×,=×,=˜×(Ò(À4Ø.2×.?×.?˜×*Ò*ÀTØ×,Ñ,Ø%)×%BÑ%BØŸ™×*Ñ*ØŸ
™
×(Ñ(ØØ#×6Ñ6ñ
ˆð Ðr:   rm   c                 ó(  • U R                   R                  [        R                  U R                  R
                  S9  U R                  R                  S[        R                  5        U R                  X5        U R                   R                  U5        g)z:Handle critical errors that terminate the generation loop.)rp   r…   r   N)	r®   rŒ   ry   rˆ   r±   r…   rÜ   rì  r�   )r5   rm   rk  s      r6   rí  Ú0ContinuousBatchingManager._handle_critical_error?  su   € ð 	×%Ñ%×2Ñ2Ü)×3Ñ3À×AXÑAX×AdÑAdð 	3ñ 	
ð
 	×Ñ×3Ñ3°AÔ7M×7WÑ7WÔXà×)Ñ)¨%ÔAà×%Ñ%×8Ñ8¸Õ?r:   c                 óþ   •   U R                   R                  5       nUb  UR                  X5        OU R                  R	                  X15        ML  ! [
        R                   a     Of = fUb  UR                  U5        gg)zCFail all remaining requests in the input queue and active requests.N)r«   Ú
get_nowaitrá   r­   rr   rK   rÍ  rJ  )r5   rm   rk  Úreq_datas       r6   rì  Ú6ContinuousBatchingManager._fail_all_remaining_requestsM  s~   € ð	ØØ×+Ñ+×6Ñ6Ó8�Ø"Ñ.Ø#×9Ñ9¸%ÕJà×&Ñ&×7Ñ7¸ÔHñ øô �{‰{ó 	Ùð	úð Ñ&Ø×-Ñ-¨eÕ4ð 's   ‚AA ÁA&Á%A&)rl  rj  rn  rg  rh  rw  r®   rk  r¬   r©   râ  r±   r;   r«   rq  rª   rL  r^  r­   rm  r3   rt   )TNFF)NNFFN)NFF)NN),r?   r@   rA   rB   ru   r+   r   r   r(   rS   ro  rT  rV  rŒ  r’  Úfloatr�  r™  r§  rv   r¡   rE   r	   r·  rÆ  rÉ  r!   rÏ  rÓ  r   rÖ  r   rÞ  r¤   rå  rD   rW  r‘  rQ  rˆ  rw   rí  rì  rF   r4   r:   r6   rZ  rZ  >  sÕ  † ñð 04ñBWà#ðBWð ,ðBWð %=ð	BWð
 &¨Ñ,ðBWð 
õBWðH :Ð0Dð  :Èô  :ôDðZ˜Dô Zô(ð Ø $Ø&+Øñ0%àð0%ð ˜‘ð0%ð  $ð	0%ð
 ð0%ð 
õ0%ñd+ eð +°e¸d±lð +Èdõ +ô9ð "&Ø%)ØØ"'Ø/3ñ8à˜‘9ð8ð ˜$‘Jð8ð ˜d™
ð	8ð
 ð8ð  ð8ð ˜D ™I‘o¨Ñ,ð8ð #&ð8ð 
ˆt‰õ8ðz &*ØØ"'ñ!à�T˜#‘Y‘ð!ð ˜d™
ð!ð ð	!ð
  ð!ð #&ð!ð 
ˆc‰õ!ðF)¨ð )°ô )ñ S¨4¡Zð ÀÈÁð ÐYiÐlpÑYpõ ò"ð¨#ð °)Ð<LÑ2Mô ðS°#ð SÀð SÈdô Sð0Ð5Mð Ð^bð Ðgkô ð: ‡]‚]ƒ_ó*_ó ð*_ôX:ð<Ð)Aô <ð|@¨Ið @ÐH`ÐcgÑHgð @Ðlpô @ð5°)ð 5ÐNfÐimÑNmð 5Ðrv÷ 5r:   rZ  c                   óœ  • \ rS rSr% Sr\\S'   \R                  " 5          SS\S-  S\	S-  S\
S-  S\4S jj5       rSS	 jr\\R                  " 5              SS\S-  S
\S\S-  S\	S-  S\S\S\
S-  S\\   4S jj5       5       r\R                  " 5             SS\\\      S\S-  S\	S-  S\S\S\S\S\\\4   4S jj5       rSrg)ÚContinuousMixini^  aü  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.
r;   Nr©   r[  r1   c                 ó^  • [        U S5      (       a"  [        U S5      (       a  [        U S5      (       d  [        S5      e[        U SS5      n[        U[        5      (       a)  [
        R                  " S5        UR                  U 5        U$ Ub  UOU R                  nUc  [        S5      eUR                  c  [
        R                  " S	5        S
Ul
        UcM  [        [        USS5      [        5      (       a#  [
        R                  " S5        UR                  nO
[        5       n[	        U UUUS9$ )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.rš  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.)rL  r;   r©   r[  )ÚhasattrÚAttributeErrorrº   rì   rZ  r$   r‹   ro  r;   r‰   r¬  r  r   r©   )r5   r;   r©   r[  Úcached_managerÚ
gen_configs         r6   Úinit_continuous_batchingÚ(ContinuousMixin.init_continuous_batchingj  s*  € ô$ �t˜X×&Ñ&¬g°d¸H×.EÑ.EÌWÐUYÐ[b×McÑMcÜ Ð!^Ó_Ð_ô ! Ð'LÈdÓSˆÜ�nÔ&?×@Ñ@Ü�KŠKðuôð ×5Ñ5°dÔ;Ø!Ð!ð +<Ñ*GÑ&ÈT×McÑMcˆ
ØÑÜÐWÓXÐXà×"Ñ"Ñ*Ü�NŠNÐbÔcØ&(ˆJÔ#ð &Ñ-Üœ' *Ð.JÈDÓQÔSk×lÑlÜ—’ðCôð .8×-RÑ-RÑ*ä-EÓ-GÐ*ô )ØØ(Ø'AØ)ñ	
ð 	
r:   c                 ó‚   • [        U SS5      n[        U[        5      (       a  UR                  5         [	        U S5        gg)zFDestroy the cached continuous batching manager and free GPU resources.rš  N)rº   rì   rZ  r§  Údelattr)r5   r	  s     r6   Ú*destroy_cached_continuous_batching_managerÚ:ContinuousMixin.destroy_cached_continuous_batching_manager¥  s<   € ä  Ð'LÈdÓSˆÜ�nÔ&?×@Ñ@Ø×"Ñ"Ô$Ü�DÐ?Õ@ð Ar:   r”  r•  Úpersistent_managerrT  c              #   ó6  #   • U R                  UUUS9nU(       ag  UR                  (       dV  [        R                  " S5        [	        5       n	UR                  5         [        R                  " S[	        5       U	-
  S S35        UR                  5          Uv •  [        R                  " S5        UR                  X#US9  U(       d  UR                  5         gg! [        R                  " S5        UR                  X#US9  U(       d  UR                  5         f f = f7f)	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.
)r;   r©   r[  z%Warming up for continuous batching...zWarming up completed in r¡  r¢  z!Continuous batching loop finished)r”  r•  r–  N)
r  rm  r$   r‹   r   rT  r’  r  r�  r§  )
r5   r;   r”  r•  r©   r  rT  r[  Úmanagerr’  s
             r6   Ú#continuous_batching_context_managerÚ3ContinuousMixin.continuous_batching_context_manager¬  sè   é € ð$ ×/Ñ/Ø/Ø'AØ)ð 0ð 
ˆö
 ˜'×+×+ä�KŠKÐ?Ô@Ü “NˆEØ�N‰NÔÜ�KŠKÐ2´<³>ÀEÑ3IÈ#Ð2NÈbÐQÔRØ�‰Œð	"ØŠMô �LŠLÐ<Ô=Ø�L‰L˜uÐM_ˆLÑ`Þ%Ø—‘Õ!ð &øô �LŠLÐ<Ô=Ø�L‰L˜uÐM_ˆLÑ`Þ%Ø—‘Õ!ð &üs   ‚BDÂC Â?DÃA DÄDr¹  r«  Úprogress_barc           
      óÜ  • U(       d  0 $ [         R                  " 5       [        R                  ::  a  [         R                  " S5        SnUc  U R
                  OUn	U	R                  b  U	R                  OSn
[        U5      U
-  nUR                  SS5      nUc  U	R                  OUn[        [        S U 5       5      Ub  UOSUS9nU(       a  [         R                  " S	5        U R                  UUS
SUUUS9n[        [         /5      n[        UU(       + SU S3SS9n0 nSn/ nU nU   U n UR!                  XUS9nUU:  a’  UR#                  SS9nU(       a=  UR$                  nUR'                  5       (       a  UUU'   US-  nUR)                  S5        O7UR+                  5       (       d"  [         R,                  " S5        [/        S5        OUU:  a  M’  SSS5        SSS5        SSS5        0 n/ / nnU HO  nUR3                  U5      nUb'  UUU'   UR,                  b  UR5                  U5        M<  M>  UR5                  U5        MQ     U(       a  [         R,                  " SU S35        U(       a  [         R,                  " SU S35        U$ ! [0         a"  n[         R,                  " SU 3S
S9   SnANÞSnAff = f! , (       d  f       Nì= f! , (       d  f       Nõ= f! , (       d  f       Nþ= f)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   ó8   #   • U  H  n[        U5      v •  M     g 7fr3   )rÛ   )Ú.0r©  s     r6   Ú	<genexpr>Ú1ContinuousMixin.generate_batch.<locals>.<genexpr>  s   é € Ð!IÂ&°Y¤# i§. .Â&ùs   ‚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é   )r;   r©   r”  r•  r  rT  r[  zSolving z	 requestsÚrequest)ÚtotalÚdisableÚdescÚunit)r¹  rª  r«  r   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.z failed during generation.)r$   ÚgetEffectiveLevelr   r  r‹   r;   r^  rÛ   r  rª  r(   rŠ   r  r  r   r   rÆ  rÏ  rX   r-  ÚupdaterŒ  rm   Úprintrw   rW   rh   )r5   r¹  r;   r©   r«  r  r  rT  ÚkwargsÚgen_cfgr^  rÃ  rª  r[  Ú
manager_cmÚ
logging_cmÚpbar_cmÚresultsÚfinished_countrÄ  r  ÚpbarrÎ  rI  rè   Úreordered_resultsÚmissing_keysÚfailed_keyss                               r6   Úgenerate_batchÚContinuousMixin.generate_batchÔ  sá  € ö4 ØˆIô ×#Ò#Ó%¬¯©Ó6Ü�KŠKÐWÔXØ ˆLð ->Ñ,E�$×(Ò(ÐK\ˆØ?F×?[Ñ?[Ñ?g˜w×;Ò;ÐmnÐÜ˜6“{Ð%9Ñ9ˆð  Ÿ™Ð$4°dÓ;ˆØ3AÑ3I˜×/Ò/È~ˆô 'Ü!Ñ!IÁ&Ó!IÓIØ3AÑ3M¡ÐSTØ%ñ
ˆö
 Ü�NŠNðvôð ×=Ñ=Ø/Ø'AØØØ1ØØ)ð >ð 
ˆ
ô +¬F¨8Ó4ˆ
ÜØØ%Ô%Ø˜L˜>¨Ð3Øñ	
ˆð ˆØˆØˆÙ˜7¢J±¸4ðSØ%×2Ñ2Ø!ÐTeð 3ð �ð % |Ó3Ø$×/Ñ/¸Ð/Ð:�FÞØ!'×!2Ñ!2˜Ø!×-Ñ-×/Ñ/Ø.4˜G F™OØ*¨aÑ/˜NØ ŸK™K¨œNøØ$×/Ñ/×1Ñ1ÜŸšÐ%QÔRäÐcÔdØð % |Õ3÷ 18§J�Zð. ÐØ$&¨�kˆÛ!ˆFØ—[‘[ Ó(ˆFØÑ!Ø,2Ð! &Ñ)Ø—<‘<Ñ+Ø×&Ñ& vÖ.ñ ,ð ×#Ñ# FÖ+ñ "ö Ü�LŠL˜9 \ NÐ2HÐIÔJÞÜ�LŠL˜9 [ MÐ1KÐLÔMØ Ð øô' ó SÜ—’Ð>¸q¸cÐBÈT×RûðSú÷' 18µú§J¥Jú�Z�Zúsm   Ä%KÄ(KÄ+J;Ä-BJÇJ;ÇJÇKÇKÊ
J8	ÊJ3	Ê.J;Ê3J8	Ê8J;Ê;
K	ËKË
K	ËKË
K+r4   )NNNrt   )NTNNFTN)NNFFFT)r?   r@   rA   rB   ru   r   rC   rD   rW  r   r(   rZ  r  r  r   rV  r  r   r  rv   r¡   ÚdictrE   r!   r2  rF   r4   r:   r6   r  r  ^  s²  ‡ ñð (Ó'à
‡]‚]ƒ_ð 6:ØFJØ/3ñ	8
à+¨dÑ2ð8
ð %=¸tÑ$Cð8
ð &¨Ñ,ð	8
ð
 
#ô8
ó ð8
ôtAð Ø
‡]‚]ƒ_ð 6:ØØ $ØFJØ#(ØØ/3ñ#"à+¨dÑ2ð#"ð ð#"ð ˜‘ð	#"ð
 %=¸tÑ$Cð#"ð !ð#"ð ð#"ð &¨Ñ,ð#"ð 
Ð,Ñ	-ô#"ó ó ð#"ðL ‡]‚]ƒ_ð 6:ØFJØ"'Ø"Ø#(Øñs!à�T˜#‘Y‘ðs!ð ,¨dÑ2ðs!ð %=¸tÑ$Cð	s!ð
  ðs!ð ðs!ð !ðs!ð ðs!ð 
ˆcÐ#Ð#Ñ	$ôs!ó ós!r:   r  )FrÜ  rÍ   rK   rO   Úabcr   Úcollections.abcr   r   Ú
contextlibr   r   Útimer   Útypingr	   rD   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    rH  r!   r"   r#   r$   r§   r%   r&   r'   Úutilsr(   r)   rX  r+   rH   ry   r¤   rZ  r  r4   r:   r6   Ú<module>rF     s·   ðó Û 	Û Û Ý ß /ß 2Ý Ý ã Ý Ý Ý 6å 3ß XÝ 9ß VÝ $Ý 0Ý &Ý GÝ *ß eß LÝ %Ý 1ß KÓ Kß BÑ Bß -ðô,˜2Ÿ9™9ô ÷33ñ 33÷lHñ H÷Xd(ñ d(÷P]5ñ ]5÷@j!ò j!r:   