ó
    pyüi®  ã                   ó  • S SK Jr  S SK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  S
SKJrJrJr  S
SKJrJrJrJrJr  \ " S S5      5       r " S S5      r " S S5      r " S S5      rg)é    )Únullcontext)Ú	dataclass)Úpartial)ÚAnyN)ÚPretrainedConfigé   )Úget_available_devices)Útracedé   )ÚPagedAttentionCache)Ú%ContinuousBatchingLogitsProcessorList)ÚTMP_TOKEN_IDÚFutureRequestStateÚlogger)ÚCudaGraphBufferÚaligned_divideÚattn_mask_is_neededÚbuild_attention_maskÚpad_to_pow2c                   óú  • \ rS rSr% Sr\R                  \S'   \R                  \\	\R                  4   -  S-  \S'   \R                  \S'   \R                  \S'   \R                  \\	\R                  4   -  \S'   \
\S	'   \
\\	\
4   -  \S
'   \\R                     \S'   \\R                     \S'   \R                  \S'   \\S'   \R                  S-  \S'   \R                  \S'   Sr\\S'   S\\	\4   4S jrSrg)ÚPagedAttentionArgsé   a¢  Dataclass containing the keyword arguments for a forward pass using paged attention.

Attributes:
    input_ids: Input token IDs tensor of shape `(1, total_query_tokens)`.
    attention_mask: Attention mask tensor or dictionary mapping layer types to masks. Can be `None` if the
        attention implementation doesn't require explicit masks.
    position_ids: Position IDs tensor of shape `(1, total_query_tokens)`.
    cu_seq_lens_q: Cumulative sequence lengths for queries, used for variable-length batching.
    cu_seq_lens_k: Cumulative sequence lengths for keys/values. Can be a tensor or dictionary mapping layer
        types (e.g., "full_attention", "sliding_attention") to tensors for hybrid models.
    max_seqlen_q: Maximum query sequence length in the batch.
    max_seqlen_k: Maximum key/value sequence length. Can be an int or dictionary for hybrid models.
    write_index: List of tensors indicating where to write new KV states in the cache, one per attention group.
    read_index: List of tensors indicating which cache positions to read from, one per attention group.
    logits_indices: Tensor indicating which positions in the output should be used for next-token prediction.
    cache: The [`PagedAttentionCache`] instance managing the KV cache.
    block_table: Block table for paged KV cache. If provided, uses `flash_attn_with_kvcache` for fused attention +
        cache update. More information in src/transformers/integrations/flash_paged.py
    logits_processor_args: List of tensors containing the arguments for the logits processors, one per request.
    use_cache: Whether to use caching (always `False` in continuous batching as the cache is managed externally).
Ú	input_idsNÚattention_maskÚposition_idsÚcu_seq_lens_qÚcu_seq_lens_kÚmax_seqlen_qÚmax_seqlen_kÚwrite_indexÚ
read_indexÚlogits_indicesÚcacheÚblock_tableÚlogits_processor_argsFÚ	use_cacheÚreturnc                 ó<  • U R                   U R                  U R                  U R                  U R                  U R
                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  U R                  S.$ )N©r   r   r   r   r   r   r   r    r!   r"   r#   r$   r%   r&   r)   ©Úselfs    Úv/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/generation/continuous_batching/input_outputs.pyÚasdictÚPagedAttentionArgs.asdictF   s‚   € àŸ™Ø"×1Ñ1Ø ×-Ñ-Ø!×/Ñ/Ø!×/Ñ/Ø ×-Ñ-Ø ×-Ñ-Ø×+Ñ+ØŸ/™/Ø"×1Ñ1Ø—Z‘ZØ×+Ñ+Ø%)×%?Ñ%?ØŸ™ñ
ð 	
ó    © )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__ÚtorchÚTensorÚ__annotations__ÚdictÚstrÚintÚlistr   r&   Úboolr   r-   Ú__static_attributes__r0   r/   r,   r   r      sä   ‡ ñð, �|‰|ÓØ—L‘L 4¨¨U¯\©\Ð(9Ñ#:Ñ:¸TÑAÓAØ—,‘,ÓØ—<‘<ÓØ—<‘< $ s¨E¯L©LÐ'8Ñ"9Ñ9Ó9ØÓØ˜˜S #˜X™Ñ&Ó&Ø�e—l‘lÑ#Ó#Ø�U—\‘\Ñ"Ó"Ø—L‘LÓ ØÓØ—‘ Ñ$Ó$Ø Ÿ<™<Ó'Ø€IˆtÓð
˜˜S #˜X™÷ 
r/   r   c                   óT  • \ rS rSr% SrSr\\S'    S0S\S\	S\
R                  S\
R                  S	\S
\S\S\SS4S jjr\" SS9S\SS4S j5       r S0SS S\
R$                  R&                  S\SS4S jjr\\
R*                  " 5       S0S\SS4S jj5       5       rS1S jrS\\
R2                  \\\
R2                  4   4   4S jrS\
R2                  S\
R2                  S\
R2                  SS4S jrS1S jrS\\\    \\   \\!   S-  4   4S  jr"\S!\\    S"\S#\S$\S%\SS4S& j5       r#S0S'\S\\\$4   4S( jjr%S\\
R2                  \
R2                  \
R2                  4   4S) jr&S\\S*4   4S+ jr'S\
R$                  RP                  S-  4S, jr)S-\
R$                  RP                  SS4S. jr*S/r+g)2ÚContinuousBatchingIOséY   zñA class to hold inputs and outputs for a continuous batching forward pass, using static tensors as storage. The
class is meant to be self-contained, so once a set of inputs have been created, the class can be used to update the
batch alone.
é   Ústatic_inputsr#   ÚconfigÚdeviceÚmodel_dtypeÚ
max_graphsÚreturn_logprobsÚlogit_processorÚuse_cuda_graph_varlenr'   Nc	                 ó¨  • Xl         X0l        X l        X@l        X€l        [        USS5      c  SOUR                  U l        X`l        SU l        SU l	        SU l
        [        UR                  5       V	s/ s H  n	SPM     sn	U l        [        UR                  5       V	s/ s H  n	SPM     sn	U l        SU l        / U l        0 U l        [%        U5      U l        UR(                  U l        U R-                  US9  U R/                  SS9  UR0                  S	:X  a-  [2        R4                  R7                  U R                  S
9U l        gSU l        gs  sn	f s  sn	f )a²  Initialize the continuous batching I/O manager. Args:
- cache: The [`PagedAttentionCache`] instance managing the KV cache. Meant to be unique.
- config: The model's pretrained configuration.
- device: The device to allocate tensors on. If the device is CPU, then the memory is pinned.
- model_dtype: The data type for model computations.
- max_graphs: Maximum number of CUDA graphs to cache. Uses LRU eviction when full.
- return_logprobs: Whether to return log probabilities along with the token IDs.
- logit_processor: The [`ContinuousBatchingLogitsProcessorList`] object used to process the logits.
- use_cuda_graph_varlen: Whether CUDA graphs are enabled for the varlen (prefill) path.
Úsliding_windowNr   r   F)rI   T©Ú
full_resetÚcuda©rE   )r#   rE   rD   rF   rJ   ÚgetattrrL   rH   Únum_q_tokensÚmax_kv_readÚtrue_batch_sizeÚrangeÚ
num_groupsÚtrue_read_sizesÚtrue_write_sizesÚuse_block_tableÚrequests_in_batchÚreq_id_to_new_token_positionr   ÚgraphsÚtrash_indexÚ_trash_indexÚ_setup_static_tensorsÚ_reset_static_tensorsÚtyper6   rO   ÚStreamÚcompute_stream©
r+   r#   rD   rE   rF   rG   rH   rI   rJ   Ú_s
             r,   Ú__init__ÚContinuousBatchingIOs.__init__a   s4  € ð, Œ
ØŒØŒØ&ÔØ%:Ô"Ü#*¨6Ð3CÀTÓ#JÑ#R™aÐX^×XmÑXmˆÔØ.ÔàˆÔØˆÔØ ˆÔÜ+0°×1AÑ1AÔ+BÓCÒ+B a£Ñ+BÑCˆÔÜ,1°%×2BÑ2BÔ,CÓ DÒ,C q£Ñ,CÑ DˆÔØ$ˆÔà;=ˆÔØ<>ˆÔ)Ü'6°zÓ'BˆŒØ!×-Ñ-ˆÔà×"Ñ"°?Ð"ÑCØ×"Ñ"¨dÐ"Ñ3ØGMÇ{Á{ÐV\ÓG\œeŸj™j×/Ñ/°t·{±{Ð/ÐCˆÕÐbfˆÕùò  DùÚ Ds   Á2E
ÂET)Ú
standalonec                 óœ  • U R                   R                  nU R                   R                  nU R                   R                  U R                   R                  -  nU R
                  R                  S:H  =(       a    [        [        5       5      S:„  nU R                  UR                  -   n[        US-   SS5      n[        R                  " Xg4[        R                  U R
                  US9U l        [        R                  " UR                  S4[        R                  U R
                  S9U l        UR#                  U R                   5        U R                  SSU24   U l        U R                  SSU24   U l        U R                  SSUS-   24   U l        U R                  S	SU24   U l        U R                  S
SUS-   24   nU R                  SSUS-   24   n	U R                  SSU24   U l        0 U l        U R                   R0                  (       a  X€R.                  S'   U R                   R2                  (       a  X�R.                  S'   U R4                  (       a  SOSn
[        R                  " X£S-   4[        R                  U R
                  US9U l        U R6                  R9                  5         SU l        SU l        [>        RA                  U R.                  RC                  5       S5      U l"        [G        U RH                  5      (       ag  0 U l%        U R.                  RC                  5        HA  n[        R                  " SSX4U-   4U RL                  U R
                  US9U RJ                  U'   MC     OSU l%        U R                   RN                  S:”  a  UOSn[        R                  " XÃU R                   RN                  4[        R                  U R
                  US9U l(        [        R                  " X#4[        RR                  U R
                  US9U l*        [        R                  " X$U-   4[        RR                  U R
                  US9U l+        g)a­  Allocates static tensors for generation inputs and outputs. This is called only once at init time, to avoid
repeated allocations and enable CUDA graphs. All tensors are allocated with maximum possible sizes.
The allocated tensors are:

- `_bulk_input_tensor`: Storage for all the small inputs: `input_ids`, `position_ids`, `cumulative_seqlens_q`,
  `logits_indices`, `cumulative_seqlens_k`, `carry_over_ids`.
- `attention_mask`: Optional attention masks (only for eager/SDPA implementations)
- `write_index` and `read_index` storage: Cache indexing tensors for each attention group
- `output_ids`: Storage for generated token IDs and maybe log probabilities if return_logprobs is True
Úcpur   é    )ÚdtyperE   Ú
pin_memory©rl   rE   r   Né   r   é   é   é   Úfull_attentionÚsliding_attention)Úsizerl   rE   rm   ),r#   rV   Úmax_batch_tokensÚ
num_blocksÚ
block_sizerE   ra   Úlenr	   rC   Útensors_requiredr   r6   ÚemptyÚint32Ú_bulk_input_tensorÚlogits_processors_defaultsÚfill_defaultsr   r   Úcumulative_seqlens_qr"   Úcarry_over_idsÚcumulative_seqlens_kÚnum_full_attention_groupsÚnum_sliding_attention_groupsrH   Ú
output_idsÚzero_Útotal_seqlen_qr   r9   ÚfromkeysÚkeysr   r   rD   r   rF   Úmax_blocks_per_requestr$   Úint64Úwrite_index_storageÚread_index_storage)r+   rI   rV   rv   Ú	num_pagesrm   Ú
bulk_linesÚbulk_columnsÚ#full_attention_cumulative_seqlens_kÚ&sliding_attention_cumulative_seqlens_kÚnum_output_rowsÚ
layer_typeÚns                r,   r_   Ú+ContinuousBatchingIOs._setup_static_tensors�   sÆ  € ð —Z‘Z×*Ñ*ˆ
ØŸ:™:×6Ñ6ÐØ—J‘J×)Ñ)¨D¯J©J×,AÑ,AÑAˆ	à—[‘[×%Ñ%¨Ñ.×S´3Ô7LÓ7NÓ3OÐRSÑ3Sˆ
ð ×'Ñ'¨/×*JÑ*JÑJˆ
Ü%Ð&6¸Ñ&:¸A¸rÓBˆÜ"'§+¢+ØÐ&¬e¯k©kÀ$Ç+Á+ÐZdñ#
ˆÔô +0¯+ª+Ø×-Ñ-¨qÐ1¼¿¹ÈTÏ[É[ñ+
ˆÔ'ð 	×%Ñ% d×&EÑ&EÔFà×0Ñ0°Ð4EÐ5EÐ4EÐ1EÑFˆŒØ ×3Ñ3°AÐ7HÐ8HÐ7HÐ4HÑIˆÔØ$(×$;Ñ$;¸AÐ?UÐAQÐTUÑAUÐ?UÐ<UÑ$VˆÔ!Ø"×5Ñ5°aÐ9JÐ:JÐ9JÐ6JÑKˆÔØ.2×.EÑ.EÀaÐI_ÐK[Ð^_ÑK_ÐI_ÐF_Ñ.`Ð+Ø15×1HÑ1HÈÐLbÐN^ÐabÑNbÐLbÐIbÑ1cÐ.Ø"×5Ñ5°aÐ9JÐ:JÐ9JÐ6JÑKˆÔð >@ˆÔ!Ø�:‰:×/×/Ø:]×%Ñ%Ð&6Ñ7Ø�:‰:×2×2Ø=c×%Ñ%Ð&9Ñ:ð  $×3×3™!¸ˆÜŸ+š+Ø°Ñ2Ð3¼5¿;¹;ÈtÏ{É{Ðgqñ
ˆŒð 	�‰×ÑÔØˆÔØˆÔÜ ŸM™M¨$×*CÑ*C×*HÑ*HÓ*JÈAÓNˆÔô ˜tŸ{™{×+Ñ+Ø"$ˆDÔØ"×7Ñ7×<Ñ<Ö>�
Ü27·+²+Ø˜QÐ 0Ð>NÑ2NÐOØ×*Ñ*ØŸ;™;Ø)ñ	3�×#Ñ# JÓ/ò ?ð #'ˆDÔð Ÿ*™*×;Ñ;¸aÓ?‰JÀQˆÜ Ÿ;š;Ø $§*¡*×"CÑ"CÐDÜ—+‘+Ø—;‘;Ø!ñ	
ˆÔô $)§;¢;ØÐ*´%·+±+ÀdÇkÁkÐ^hñ$
ˆÔ ô #(§+¢+ØÐ%5Ñ5Ð6¼e¿k¹kÐRV×R]ÑR]Ðjtñ#
ˆÕr/   ÚotherÚstreamÚnon_blockingc                 ó.  • U R                   Ul         U R                  Ul        U R                  Ul        U R                  S S  Ul        U R                  S S  Ul        U R
                  Ul        U R                  Ul        U R                  Ul        [        U R                  R                  5       5      Ul	        Ub  [        R                  R                  U5      O	[        5       nU   UR                  R!                  U R                  US9  U R
                  (       a%  UR"                  R!                  U R"                  US9  OXUR$                  R!                  U R$                  US9  U R                  S:”  a$  UR&                  R!                  U R&                  US9  U R(                  bX  UR(                  bK  U R(                  R+                  5        H-  nUR(                  U   R!                  U R(                  U   US9  M/     S S S 5        g ! , (       d  f       g = f)N©r™   r   )rR   rS   rT   rW   rX   rY   r‡   r   r9   r   Úitemsr6   rO   r˜   r   r}   Úcopy_r$   rŒ   r�   r   r‰   )r+   r—   r˜   r™   Úmaybe_streamr”   s         r,   Ú_transfer_inputsÚ&ContinuousBatchingIOs._transfer_inputsç   s¶  € ð "×.Ñ.ˆÔØ ×,Ñ,ˆÔØ $× 4Ñ 4ˆÔØ $× 4Ñ 4±QÐ 7ˆÔØ!%×!6Ñ!6±qÐ!9ˆÔØ $× 4Ñ 4ˆÔà#×2Ñ2ˆÔØ!×.Ñ.ˆÔÜ! $×"3Ñ"3×"9Ñ"9Ó";Ó<ˆÔà4:Ñ4F”u—z‘z×(Ñ(¨Ô0ÌKËMˆÚØ×$Ñ$×*Ñ*¨4×+BÑ+BÐQ]Ð*Ñ^à×#×#Ø×!Ñ!×'Ñ'¨×(8Ñ(8À|Ð'ÒTð ×)Ñ)×/Ñ/°×0HÑ0HÐWcÐ/ÑdØ×#Ñ# aÓ'Ø×,Ñ,×2Ñ2°4×3JÑ3JÐYeÐ2Ñfà×"Ñ"Ñ.°5×3GÑ3GÑ3SØ"&×"5Ñ"5×":Ñ":Ö"<�JØ×(Ñ(¨Ñ4×:Ñ:¸4×;NÑ;NÈzÑ;ZÐiuÐ:Óvñ #=÷ �\Ž\ús   Ã%DHÈ
HrN   c                 óJ  • U(       a  U R                   R                  S5      OU R                  nU(       a  U R                  R                  S5      OU R                  nU R
                  SU R                  2SUS-   24   R                  5         U(       a#  U R                  U R
                  U R                  S& SU l	        U R                  SU R                  5         U R                  SS2SU24   R                  5         U R                   Hx  nSU R                  U'   U R                  c  M!  U R                  U   SS2SS2SU2SX#-   24   R                  [         R"                  " U R$                  5      R&                  5        Mz     U(       aƒ  U R(                  SS2SU24   R                  S5        U R                   SS2SU24   R                  U R*                  5        U R                  SS2SX#-   24   R                  U R*                  5        gU R,                  (       a%  U R(                  SS2SU24   R                  S5        gU R                   SS2SU24   R                  U R*                  5        U R                  SS2SX#-   24   R                  U R*                  5        g)a  Reset static tensors for the next batch. For efficiency, this only resets the portions of tensors that were
actually used in the previous batch, using the attributes num_q_tokens and max_kv_read. If a (full_reset)
is requested, the entire tensor storage is reset.
éÿÿÿÿNr   r   )rŒ   ru   rR   r�   rS   r}   rC   r†   r~   r   r"   r…   r‚   r   r   Úfill_r6   ÚfinforF   Úminr$   r^   rY   )r+   rN   Úq_lenÚkv_lenr”   s        r,   r`   Ú+ContinuousBatchingIOs._reset_static_tensors  sJ  € ö 6@�×(Ñ(×-Ñ-¨bÔ1ÀT×EVÑEVˆÞ5?�×(Ñ(×-Ñ-¨bÔ1ÀT×EUÑEUˆð 	×ÑÐ 4 $×"4Ñ"4Ð 4°k¸À¹	°kÐ AÑB×HÑHÔJÞØ<@×<[Ñ<[ˆD×#Ñ# D×$6Ñ$6Ð$8Ð9ØˆÔð 	×Ñ˜F˜UÐ#×)Ñ)Ô+Ø�‰š˜6˜E˜6˜	Ñ"×(Ñ(Ô*ð ×3Ô3ˆJØ,-ˆD×Ñ˜jÑ)Ø×"Ñ"Ó.Ø×#Ñ# JÑ/²²1°f°u°fÐ>NÀÁÐ>NÐ0NÑO×UÑUÜ—K’K × 0Ñ 0Ó1×5Ñ5öñ 4ö Ø×ÑšQ   ˜YÑ'×-Ñ-¨bÔ1Ø×$Ñ$¢Q¨¨¨ YÑ/×5Ñ5°d×6GÑ6GÔHØ×#Ñ#¢AÐ'7¨©Ð'7Ð$7Ñ8×>Ñ>¸t×?PÑ?PÕQà×!×!Ø×ÑšQ   ˜YÑ'×-Ñ-¨bÕ1ð ×$Ñ$¢Q¨¨¨ YÑ/×5Ñ5°d×6GÑ6GÔHØ×#Ñ#¢AÐ'7¨©Ð'7Ð$7Ñ8×>Ñ>¸t×?PÑ?PÕQr/   c                 óŽ   • U R                  SS9  / U l        0 U l        U R                  b  U R                  R	                  5         gg)z4Reset all relevant states for a new generation loop.TrM   N)r`   rZ   r[   rc   Úsynchronizer*   s    r,   ÚresetÚContinuousBatchingIOs.reset0  sF   € à×"Ñ"¨dÐ"Ñ3Ø!#ˆÔØ,.ˆÔ)Ø×ÑÑ*Ø×Ñ×+Ñ+Õ-ð +r/   c                 ó2   • U R                   U R                  4$ )z:Get the cumulative sequence lengths for the current batch.)r€   r‚   r*   s    r,   Úget_cumulative_seqlensÚ,ContinuousBatchingIOs.get_cumulative_seqlens9  s   € à×(Ñ(¨$×*CÑ*CÐCÐCr/   r   r�   Úprev_output_idsc                 ó   • g ©Nr0   )r+   r   r�   r°   s       r,   Úcarry_over_tokensÚ'ContinuousBatchingIOs.carry_over_tokens=  s   € ð 	r/   c                 óT   • U R                   b  U R                   R                  5         g g r²   )rc   rª   r*   s    r,   Úretrieve_device_outputsÚ-ContinuousBatchingIOs.retrieve_device_outputsB  s%   € Ø×ÑÑ*Ø×Ñ×+Ñ+Õ-ð +r/   c                 óP  • U R                   nU R                  SS [        U R                   5      24   R                  5       nU R                  (       aP  U R                  SS [        U R                   5      24   R                  [        R                  S9R                  5       nOS nXU4$ )Nr   r   ©rl   )rZ   r…   ry   ÚtolistrH   Úviewr6   Úfloat32)r+   rZ   Ú
new_tokensÚlogprobss       r,   Úprepare_batch_updateÚ*ContinuousBatchingIOs.prepare_batch_updateF  s˜   € Ø ×2Ñ2ÐØ—_‘_ QÐ(E¬#¨d×.DÑ.DÓ*EÐ(EÐ%EÑF×MÑMÓOˆ
à××Ø—‘ qÐ*G¬C°×0FÑ0FÓ,GÐ*GÐ'GÑH×MÑMÔTY×TaÑTaÐMÐb×iÑiÓk‰Hð ˆHØ ¨hÐ6Ð6r/   rZ   Úlogits_processorsÚuse_decode_fast_pathrR   rS   c           	      óò  • U(       d  [        S5      eU=(       a    U R                  R                  5       S:„  U l        X@l        U R                  (       a  SOUU l        [        U5      U l        U R                  5         [        U R                  R                  5       Vs/ s H  nSPM     snU l        [        U R                  R                  5       Vs/ s H  nSPM     snU l        / U l        0 U l        / n/ nS/n	/ n
U R                   R#                  5        Vs0 s H  o»S/_M     nn[        U R                  R                  5       Vs/ s H  n/ PM     nnU R
                  S:X  a  SO/[        U R                  R                  5       Vs/ s H  n/ PM     snn[%        U5       GH   u  nnUR&                  nUR(                  nUR*                  nU R                  R-                  UU5      nU=R(                  U-  sl        UR/                  UR0                  5        UR/                  [        UUU-   5      5        U	R3                  U	S   U-   5        [5        U R6                  U5      U l        UR9                  5        HG  u  nnXË   R3                  XË   S   U-   5        [5        U R:                  U   U5      U R:                  U'   MI     U R                  (       a:  U R                  R=                  UR>                  UUU R                  SS2U4   5        O(U R                  RA                  UR>                  UUXí5        URB                  (       a?  U
R3                  U	S   S-
  5        [D        /Ul        U
S   U R                  UR>                  '   U R                  R3                  U5        GM     URG                  UU RH                  U RJ                  S S9  [M        [N        RP                  [N        RR                  U RT                  S9nU" U5      U RV                  S[        U5      & U" U5      U RX                  S[        U5      & U" U	5      U RZ                  S[        U	5      & U" U
5      U R\                  S[        U
5      & U	S   U l/        UR9                  5        Ha  u  nnU" U5      U R                   U   S[        U5      & U R`                  c  M6  [c        U R`                  U   U	UUS:X  a  U Rd                  OSS	9  Mc     U R                  (       dÔ  [M        [N        RP                  [N        Rf                  U RT                  S9n[%        U5       H@  u  nnU" U5      U Rh                  US[        U5      24'   [        U5      U R                  U'   MB     UbP  [%        U5       H@  u  nnU" U5      U Rj                  US[        U5      24'   [        U5      U R                  U'   MB     gggs  snf s  snf s  snf s  snf s  snf )
at  Prepare tensors and metadata for the next model forward pass, using the given requests as data. This method:

1. Resets the static tensors from the previous batch
2. Iterates through requests to accumulate input_ids, position_ids, and sequence lengths
3. Extends read/write indices for cache management
4. Builds attention masks if needed (for eager/SDPA implementations)
5. Converts accumulated lists to tensors and copies them to static storage

This method also modifies the `position_offset` attribute of each request to track progress and adds a
temporary token at the end of the requests for which there will a new token.
zNo requests in batchr   Nr¢   r   )rZ   Úarg_storagern   rt   )r   r€   r‚   rL   )6Ú
ValueErrorr$   ÚnumelrY   rR   rS   ry   rT   r`   rU   r#   rV   rW   rX   rZ   r[   r‚   r‰   Ú	enumerateÚstateÚposition_offsetÚquery_lengthÚget_seqlens_kÚextendÚtokens_to_processÚappendÚmaxr   rœ   r   Úfill_block_tableÚ
request_idÚextend_read_and_write_indicesÚhas_new_tokenr   Úprepare_tensor_argsr}   rC   r   r6   Útensorr|   rE   r   r   r€   r"   r‡   r   r   rL   r‹   rŒ   r�   )r+   rZ   rÁ   rÂ   rR   rS   re   r   r   r€   r"   r”   r‚   r    r!   ÚiÚfuture_staterÈ   Úpast_lengthrÊ   Ú	seqlens_kÚlayer_type_seqlen_kÚ	to_tensorÚlayer_type_seqlens_kÚto_index_tensorÚgroup_write_indicesÚgroup_read_indicess                              r,   Úprepare_batch_tensorsÚ+ContinuousBatchingIOs.prepare_batch_tensorsQ  s}  € ö* !ÜÐ3Ó4Ð4ð  4×T¸×8HÑ8H×8NÑ8NÓ8PÐSTÑ8TˆÔà(ÔØ $× 4× 4™1¸+ˆÔÜ"Ð#4Ó5ˆÔà×"Ñ"Ô$ô ,1°·±×1FÑ1FÔ+GÓHÒ+G a£Ñ+GÑHˆÔÜ,1°$·*±*×2GÑ2GÔ,HÓ IÒ,H q£Ñ,HÑ IˆÔØ!#ˆÔØ,.ˆÔ)ð ˆ	ØˆØ !˜sÐØˆØBF×B[ÑB[×B`ÑB`ÔBbÓcÒBb°J¨Q¨C¢ÑBbÐÐcÜ#(¨¯©×)>Ñ)>Ô#?Ó@Ò#?˜a“rÑ#?ˆÐ@Ø!×-Ñ-°Ó2‘TÄUÈ4Ï:É:×K`ÑK`ÔEaÓ8bÒEaÀ»ÑEaÑ8bˆ
ô  )Ð):×;‰OˆAˆ|à ×&Ñ&ˆEØ×/Ñ/ˆKØ'×4Ñ4ˆLØŸ
™
×0Ñ0°¸lÓKˆIð ×!Ò! \Ñ1Õ!ð ×Ñ˜U×4Ñ4Ô5Ø×Ñ¤ k°;ÀÑ3MÓ NÔOØ ×'Ñ'Ð(<¸RÑ(@À<Ñ(OÔPÜ # D×$5Ñ$5°|Ó DˆDÔð 4=·?±?Ö3DÑ/�
Ð/Ø$Ñ0×7Ñ7Ð8LÑ8XÐY[Ñ8\Ð_rÑ8rÔsÜ03°D×4EÑ4EÀjÑ4QÐSfÓ0g�×!Ñ! *Ó-ñ 4Eð
 ×#×#Ø—
‘
×+Ñ+¨E×,<Ñ,<¸kÈ<ÐY]×YiÑYiÒjkÐmnÐjnÑYoÕpà—
‘
×8Ñ8Ø×$Ñ$ k°<Àôð
 ×)×)Ø×%Ñ%Ð&:¸2Ñ&>ÀÑ&BÔCÜ+7¨.�Ô'ØFTÐUWÑFX�×1Ñ1°%×2BÑ2BÑCà×"Ñ"×)Ñ)¨,×7ñG  <ðL 	×-Ñ-Ø/Ø×/Ñ/°×0BÑ0BÐ0DÐEð 	.ñ 	
ô œEŸL™L´·±ÀDÇKÁKÑPˆ	ñ ,5°YÓ+?ˆ�‰Ð'œ˜Y›Ð(Ù1:¸<Ó1Hˆ×ÑÐ-œC Ó-Ð.ÙAJÐK_ÓA`ˆ×!Ñ!Ð"=¤CÐ(<Ó$=Ð>Ù5>¸~Ó5Nˆ×ÑÐ1œc .Ó1Ð2Ø2°2Ñ6ˆÔð 1E×0JÑ0JÖ0LÑ,ˆJÐ,ÙQZÐ[oÓQpˆD×%Ñ% jÑ1Ð2M´CÐ8LÓ4MÐNØ×"Ñ"Ó.Ü$Ø#'×#6Ñ#6°zÑ#BØ)=Ø)=Ø:DÐH[Ó:[ 4×#6Ò#6Ðabô	ñ 1Mð ×#×#Ü%¤e§l¡l¼%¿+¹+ÈdÏkÉkÑZˆOÜ*3°KÖ*@Ñ&�Ð&ÙJYÐZmÓJn�×(Ñ(¨Ð,F¬cÐ2EÓ.FÐ,FÐ)FÑGÜ+.Ð/BÓ+C�×%Ñ% aÓ(ñ +Að Ñ%Ü-6°zÖ-BÑ)�AÐ)ÙL[Ð\nÓLo�D×+Ñ+¨AÐ/H´Ð5GÓ1HÐ/HÐ,HÑIÜ.1Ð2DÓ.E�D×(Ñ(¨Ó+ò .Cð &ð $ùòe  IùÚ Iùò  dùÚ@ùÚ8bs   ÂW ÃW%ÄW*ÅW/ÆW4Úuse_paddingc                 ón  • U R                   nU R                  U R                   -   nU(       a  U R                   OU R                  n[        U R                  SU R                  S5      U R                  SU R                  S5      U R                  SUS-    U R                  U R                  SU U R                  U R                  S2SU24   0 0 U R                  c  SO0 / / U R                  U R                  (       a  U R                  SS2SU24   OSSS9nU(       a«  U R                   UR"                  U R                  S-   S& U R                  (       dt  U R$                  (       ac  X l        U R&                  R)                  5        Vs0 s H2  nU[+        U R&                  U   U R                  R,                  S5      _M4     snU l        U R                  (       a  SOU R                  Ul        [/        U R                  R0                  5       Hž  nU(       a  UOU R2                  U   nUR4                  R7                  U R8                  USU24   5        U R                  S:X  a  Sn	OU(       a  UOU R:                  U   n	UR<                  R7                  U R>                  USU	24   5        M      U R@                  RC                  5        H½  u  pjU
SUS-    URD                  U'   U(       a+  X R                     URD                  U   U R                  S-   S& U R                  (       a  SOU R&                  U   UR&                  U'   U R                  c  MŠ  U(       a  UOX¤   nU R                  U   SSU2SU24   UR                  U'   M¿     [G        U R@                  R)                  5       5      S:X  as  URD                  RI                  5       S   Ul"        UR&                  RI                  5       S   Ul        U R                  b"  UR                  RI                  5       S   Ul        URK                  5       $ s  snf )zÕGet model keyword arguments for the current batch, eventually padding the query dimension and KV dimensions
if use_padding is True. The padding is only useful if we want static shapes, like when using cuda graphs.Nr   r   F)r   r   r   r   r"   r%   r   r   r   r!   r    r#   r$   r&   i   .)&rR   rS   rT   r   r   Ú	unsqueezer   r€   r   r"   r}   rC   r   r#   rY   r$   r‡   r   rJ   r   r‰   r   rŽ   rU   rV   rX   r    rÎ   rŒ   rW   r!   r�   r‚   rœ   r   ry   Úpopitemr-   )r+   râ   Úq_sizeÚkv_sizeÚ
batch_sizeÚkwargsr”   rÖ   Úwrite_index_sizeÚread_index_sizerÙ   Úk_lens               r,   Úget_model_kwargsÚ&ContinuousBatchingIOs.get_model_kwargsÏ  sß  € ð ×"Ñ"ˆØ×"Ñ" T×%6Ñ%6Ñ6ˆÞ*5�T×&Ò&¸4×;OÑ;Oˆ
ô $Ø—n‘n W fÐ-×7Ñ7¸Ó:Ø×*Ñ*¨7¨FÐ3×=Ñ=¸aÓ@Ø×3Ñ3Ð4D°jÀ1±nÐEØ×*Ñ*Ø×.Ñ.¨w°Ð7Ø"&×"9Ñ"9¸$×:LÑ:LÑ:NÐPWÐQWÐPWÐ:WÑ"XØØØ#'×#6Ñ#6Ñ#>™4ÀBØØØ—*‘*Ø<@×<P×<P˜×(Ñ(ª¨K¨Z¨K¨Ò8ÐVZØñ
ˆö$ Ø?C×?RÑ?RˆF× Ñ  ×!5Ñ!5¸Ñ!9Ð!;Ð<ð ×'×'¨D×,F×,FØ$*Ô!ð '+×&7Ñ&7×&<Ñ&<Ô&>ó%â&>˜
ð ¤¨D×,=Ñ,=¸jÑ,IÈ4Ï:É:×K_ÑK_ÐaeÓ fÒfÙ&>ñ%�Ô!ð $(×#7×#7™a¸T×=NÑ=NˆÔô �t—z‘z×,Ñ,Ö-ˆAÞ)4™v¸$×:OÑ:OÐPQÑ:RÐØ×Ñ×%Ñ% d×&>Ñ&>¸qÐBSÐCSÐBSÐ?SÑ&TÔUà×Ñ 1Ó$Ø"#‘æ-8¡'¸d×>RÑ>RÐSTÑ>U�Ø×Ñ×$Ñ$ T×%<Ñ%<¸QÐ@PÀÐ@PÐ=PÑ%QÖRñ .ð &*×%>Ñ%>×%DÑ%DÖ%FÑ!ˆJØ/8Ð9I¸:È¹>Ð/JˆF× Ñ  Ñ,ÞØOX×YmÑYmÑOn�×$Ñ$ ZÑ0°×1EÑ1EÈÑ1IÐ1KÐLØ37×3G×3G©aÈT×M^ÑM^Ð_iÑMjˆF×Ñ 
Ñ+Ø×"Ñ"Ó.Þ#.™°IÑ4I�Ø48×4GÑ4GÈ
Ñ4SÐTWÐY`ÐZ`ÐY`ÐbhÐchÐbhÐThÑ4i�×%Ñ% jÓ1ñ &Gô ˆt×(Ñ(×-Ñ-Ó/Ó0°AÓ5Ø#)×#7Ñ#7×#?Ñ#?Ó#AÀ!Ñ#DˆFÔ Ø"(×"5Ñ"5×"=Ñ"=Ó"?ÀÑ"BˆFÔØ×"Ñ"Ñ.Ø(.×(=Ñ(=×(EÑ(EÓ(GÈÑ(J�Ô%à�}‰}‹ÐùòK%s   Æ9P2c                 óH   • U R                   U R                  U R                  4$ )a  Returns the tensors used inside the generation step that are not inputs to the model forward pass. In
synchronous batching, there is no carry over, so the only tensor that will be used is output_ids, but we still
return 3 tensors to have the same interface as when using async batching.)r�   r…   r*   s    r,   Úget_cb_kwargsÚ#ContinuousBatchingIOs.get_cb_kwargs  s   € ð ×"Ñ" D§O¡O°T·_±_ÐDÐDr/   .c                 ó¤   • U R                   (       a  U R                  4$ U R                  U R                  /U R                  R	                  5       Q7$ r²   )rY   rR   rS   r   Úvaluesr*   s    r,   Ú_get_graph_keyÚ$ContinuousBatchingIOs._get_graph_key  sE   € à××Ø×%Ñ%Ð'Ð'à×!Ñ! 4×#3Ñ#3ÐQ°d×6GÑ6G×6NÑ6NÓ6PÑQÐQr/   c                 óÊ   • U R                  5       nU R                  R                  U5      nUc4  U R                  R                  5         [        R
                  " SU< 35        U$ )NzCreating graph for key = )rô   r\   Ú	get_graphÚplan_for_new_graphr   Úinfo)r+   ÚkeyÚgraphs      r,   r÷   ÚContinuousBatchingIOs.get_graph#  sR   € Ø×!Ñ!Ó#ˆØ—‘×%Ñ% cÓ*ˆà‰=Ø�K‰K×*Ñ*Ô,Ü�KŠKÐ4¨c©XÐ6Ô7Øˆr/   rû   c                 óŽ   • U R                  5       nU R                  R                  X!5        [        R                  " SU< 35        g )NzSetting graph for key = )rô   r\   Ú	set_graphr   rù   )r+   rû   rú   s      r,   rþ   ÚContinuousBatchingIOs.set_graph,  s6   € Ø×!Ñ!Ó#ˆØ�‰×Ñ˜cÔ)Ü�ŠÐ/¨©Ð1Õ2r/   )"r}   r^   r   r$   r#   r�   rc   rD   r‚   r€   rE   r\   r   r"   r~   rS   r   r   rF   rR   r…   r   r�   r[   rZ   rH   rL   r‡   rT   rW   rX   rY   rJ   rŒ   ©F©r'   N),r1   r2   r3   r4   r5   rC   r;   r8   r   r   r6   rE   rl   r=   r   rf   r
   r_   rO   rb   rŸ   Úno_gradr`   r«   Útupler7   r9   r:   r®   r³   r¶   r<   r   Úfloatr¿   rà   r   rí   rð   rô   Ú	CUDAGraphr÷   rþ   r>   r0   r/   r,   r@   r@   Y   sµ  ‡ ñð
 €M�3Óð ',ñ,gà"ð,gð !ð,gð —‘ð	,gð
 —[‘[ð,gð ð,gð ð,gð ?ð,gð  $ð,gð 
õ,gñ\ �tÑðT
Ð5Zð T
Ð_có T
ó ðT
ðp _dñwØ,ðwØ6;·j±j×6GÑ6GðwØW[ðwà	õwð> Ø
‡]‚]ƒ_ñ&R°ð &RÀô &Ró ó ð&RôP.ðD¨¨e¯l©l¸DÀÀeÇlÁlÐARÑ<SÐ.SÑ(Tô DðØŸ™ðØ7<·|±|ðØV[×VbÑVbðà	ôô
.ð	7 e¨DÐ1CÑ,DÀdÈ3ÁiÐQUÐV[ÑQ\Ð_cÑQcÐ,cÑ&dô 	7ð ð{FàÐ 2Ñ3ð{Fð Að{Fð #ð	{Fð
 ð{Fð ð{Fð 
ó{Fó ð{FñzE¨Dð E¸TÀ#ÀsÀ(¹^õ EðNE˜u U§\¡\°5·<±<ÀÇÁÐ%MÑNô EðR  c¨3 h¡ô Rð˜5Ÿ:™:×/Ñ/°$Ñ6ô ð3˜uŸz™z×3Ñ3ð 3¸÷ 3r/   r@   c                   óô   • \ rS rSr SS\S\S\R                  S\R                  S\	S\
S\S	\
S
S4S jjrSS jrS\R                  R                  S
S4S jrS\R                  R                  S-  S
S4S jrSrg)ÚHostDeviceIOPairi2  r#   rD   rE   rF   rG   rH   rI   rJ   r'   Nc	           
      óD  • [        UU[        R                  " S5      UUUUU5      U l        [        UUUUUUUU5      U l        [        R
                  R                  5       (       a  [        R
                  R                  5       OS U l        [        R
                  R                  5       (       a  [        R
                  R                  5       OS U l	        [        R
                  R                  5       (       a$  [        R
                  R                  5       U l
        g S U l
        g )Nrj   )r@   r6   rE   Úhost_ioÚ	device_iorO   Úis_availableÚEventÚh2d_overÚcompute_overÚd2h_over)	r+   r#   rD   rE   rF   rG   rH   rI   rJ   s	            r,   rf   ÚHostDeviceIOPair.__init__3  s×   € ô -ØØÜ�LŠL˜ÓØØØØØ!ó	
ˆŒô /ØØØØØØØØ!ó	
ˆŒô /4¯j©j×.EÑ.E×.GÑ.GœŸ
™
×(Ñ(Ô*ÈTˆŒÜ27·*±*×2IÑ2I×2KÑ2KœEŸJ™J×,Ñ,Ô.ÐQUˆÔÜ.3¯j©j×.EÑ.E×.GÑ.GœŸ
™
×(Ñ(Ó*ˆ�ÈTˆ�r/   c                 óê   • U R                   R                  5         U R                  R                  5         U R                  U R                  U R
                  4 H  nUc  M  UR                  5         M     g r²   )r	  r«   r
  r  r  r  rª   )r+   Úevents     r,   r«   ÚHostDeviceIOPair.resetX  sT   € Ø�‰×ÑÔØ�‰×ÑÔØ—m‘m T×%6Ñ%6¸¿¹ÓFˆEØÓ Ø×!Ñ!Ö#ò Gr/   r˜   c                 óN   • U R                   R                  U R                  USS9  g )NT)r˜   r™   )r	  rŸ   r
  )r+   r˜   s     r,   Útransfer_inputs_h2dÚ$HostDeviceIOPair.transfer_inputs_h2d_  s    € Ø�‰×%Ñ% d§n¡n¸VÐRVÐ%ÒWr/   c                 ó  • Ub  [         R                  R                  U5      O	[        5       nU   U R                  R
                  R                  U R                  R
                  SS9  S S S 5        g ! , (       d  f       g = f)NTr›   )r6   rO   r˜   r   r	  r…   r�   r
  )r+   r˜   rž   s      r,   Útransfer_outputs_d2hÚ%HostDeviceIOPair.transfer_outputs_d2hb  sT   € Ø4:Ñ4F”u—z‘z×(Ñ(¨Ô0ÌKËMˆÚØ�L‰L×#Ñ#×)Ñ)¨$¯.©.×*CÑ*CÐRVÐ)ÑW÷ �\Ž\ús   ¯9A1Á1
A?)r  r  r
  r  r	  r   r  )r1   r2   r3   r4   r   r   r6   rE   rl   r;   r=   r   rf   r«   rO   rb   r  r  r>   r0   r/   r,   r  r  2  sÁ   † ð ',ñ#Rà"ð#Rð !ð#Rð —‘ð	#Rð
 —[‘[ð#Rð ð#Rð ð#Rð ?ð#Rð  $ð#Rð 
õ#RôJ$ðX¨%¯*©*×*;Ñ*;ð XÀô XðX¨5¯:©:×+<Ñ+<¸tÑ+Cð XÈ÷ Xr/   r  c                   óÔ  • \ rS rSrSr S&S\S\S\R                  S\R                  S\
S\S	\S
\SS4S jjrS\\R                  \\\R                  4   4   4S jrS\\   S\S\S\
S\
SS4S jrS\R                  4S jrS&S\S\\\4   4S jjrS\\R                  \R                  \R                  4   4S jrS\R                  S\R                  S\R                  SS4S jr\S\R                  4S j5       rS\R:                  R<                  S-  4S jrS\R:                  R<                  SS4S  jr \S\4S! j5       r!S'S" jr"S\\\   \\
   \\#   S-  4   4S# jr$S'S$ jr%S%r&g)(ÚContinuousBatchingAsyncIOsih  uh  A class to handle the inputs and outputs for the asynchronous API. It uses two IO pairs to avoid race conditions
between the two batches, which means twice as more VRAM is used for static input tensors and CUDA graph. If your GPU
is large enough or you want to generate long sequences, this is a good trade-off to make.

Asynchronous batching works by creating two pairs of host - device inputs and ouputs:

                                inputs
                  â”Œâ”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”� â”€â”€â”€â”€â”€â”€â”€â”€â–º â”Œâ”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”�
IO pair object:   â”‚ Host IOs â”‚           â”‚ Device IOs â”‚       (for a CUDA sytem, Host = CPU and Device = GPU)
                  â””â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”˜ â—„â”€â”€â”€â”€â”€â”€â”€â”€ â””â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”˜
                                outputs

Each pair is separate from the other. This means that each pairs has its own CUDA graphs set, because CUDA graphs
need to have static adresses for input tensors. To have a unique set of CUDA graph, we would need to copy the input
tensors to a third device-side buffer. This could limit the memory cost of CUDA graphs but would slow down the
forward pass.
But the CUDA streams orchestrating the transfer from host to device (H2D) and device to host (D2H) are the same for
both pairs. Same for the compute stream.
The order of steps in async batching looks like this (for 3 batches of compute):

     â”‚ â”Œâ”€â”€â”€â”€â”¬â”€â”€â”€â”€â”�                  â”Œâ”€â”€â”€â”€â”¬â”€â”€â”€â”€â”�     â”Œâ”€â”€â”€â”€â”¬â”€â”€â”€â”€â”�       â”Œâ”€â”€â”€â”€â”�          â”Œâ”€â”€â”€â”€â”�
CPU  â”‚ â”‚PR 0â”‚PR 1â”‚                  â”‚UP 0â”‚PR 2â”‚     â”‚UP 1â”‚PR 3â”‚       â”‚UP 2â”‚          â”‚UP 3â”‚
     â”‚ â””â”€â”€â”€â”€â”¼â”€â”€â”€â”¬â”´â”€â”€â”�               â””â”€â”€â”€â”€â”´â”€â”€â”€â”€â”¼â”€â”€â”€â”� â””â”€â”€â”€â”€â”´â”€â”€â”€â”€â”¼â”€â”€â”€â”�   â””â”€â”€â”€â”€â”˜          â””â”€â”€â”€â”€â”˜
H2D  â”‚      â”‚0->â”‚1->â”‚               Â¦         â”‚2->â”‚ Â¦         â”‚3->â”‚   Â¦               Â¦
     â”‚      â””â”€â”€â”€â”¼â”€â”€â”€â”´â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”¬â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”´â”€â”¬â”€â”¼â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”´â”€â”€â”€â”¼â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”�   Â¦
GPU  â”‚          â”‚   COMPUTE 0   â”‚   COMPUTE 1   â”‚â–ˆâ”‚   COMPUTE 2   â”‚   COMPUTE 3   â”‚   Â¦
     â”‚          â””â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”¼â”€â”€â”€â”¬â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”¼â”€â”´â”€â”¬â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”¼â”€â”€â”€â”¬â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”€â”¼â”€â”€â”€â”¤
D2H  â”‚                          â”‚0<-â”‚           â”‚1<-â”‚             â”‚2<-â”‚           â”‚3<-â”‚
     â”‚                          â””â”€â”€â”€â”˜           â””â”€â”€â”€â”˜             â””â”€â”€â”€â”˜           â””â”€â”€â”€â”˜

with: - CPU: actions happening on the CPU (host-side)
      - GPU: actions happening on the GPU (device-side)
      - H2D: host to device transfer
      - D2H: device to host transfer
and:
      - PR N: preparation of batch N
      - ->N: host to device transfer of batch N
      - COMPUTE N: compute step for batch N
      - <-N: device to host transfer of batch N
      - UP N: update of batch N

You can see that the GPU is almost always busy, except where the â–ˆ is.
Proper ordering of steps is ensured through the use of CUDA events and streams.
r#   rD   rE   rF   rG   rH   rI   rJ   r'   Nc	                 óø  • [         R                  R                  5       (       d+  [        S[         R                  R                  5       < 35      eSU l        [        S5       V	s/ s H  n	[        UUUUUUUU5      PM     sn	U l        [         R                  R                  US9U l	        [         R                  R                  US9U l
        [         R                  R                  US9U l        S U R                  S   R                  l        S U R                  S   R                  l        S U R                  S   R                  l        S U R                  S   R                  l        UR                  U l        g s  sn	f )Nz>Async batching requires CUDA, but torch.cuda.is_available() = r   ro   rP   r   )r6   rO   r  ÚRuntimeErrorÚcurrent_pairrU   r  Úio_pairsrb   Ú
h2d_streamÚ
d2h_streamrc   r	  r
  rv   rd   s
             r,   rf   Ú#ContinuousBatchingAsyncIOs.__init__–  sC  € ô �z‰z×&Ñ&×(Ñ(ÜÐ!`ÄEÇJÁJ×D[ÑD[ÓD]ÑCaÐbÓcÐcàˆÔô ˜1”Xó
ò �ô ØØØØØØØØ%ö	ñ ñ
ˆŒô  Ÿ*™*×+Ñ+°6Ð+Ð:ˆŒÜŸ*™*×+Ñ+°6Ð+Ð:ˆŒÜ#Ÿj™j×/Ñ/°vÐ/Ð>ˆÔà26ˆ�‰�aÑ× Ñ Ô/Ø48ˆ�‰�aÑ×"Ñ"Ô1Ø26ˆ�‰�aÑ× Ñ Ô/Ø48ˆ�‰�aÑ×"Ñ"Ô1à %× 6Ñ 6ˆÕùò/
s   Á#E7c                 ód   • U R                   U R                     R                  R                  5       $ r²   )r  r  r	  r®   r*   s    r,   r®   Ú1ContinuousBatchingAsyncIOs.get_cumulative_seqlensÀ  s&   € Ø�}‰}˜T×.Ñ.Ñ/×7Ñ7×NÑNÓPÐPr/   rZ   rÁ   rÂ   rR   rS   c                 óÖ   • U R                   U R                     nUR                  R                  XX4U5        UR                  R                  R                  U R                  5       5        g r²   )r  r  r	  rà   r�   r�   Úinfer_carry_over_ids)r+   rZ   rÁ   rÂ   rR   rS   Úio_pairs          r,   rà   Ú0ContinuousBatchingAsyncIOs.prepare_batch_tensorsÄ  sV   € ð —-‘- × 1Ñ 1Ñ2ˆØ�‰×-Ñ-ØÐ2FÐVaô	
ð 	�‰×&Ñ&×,Ñ,¨T×-FÑ-FÓ-HÕIr/   c                 óÎ  • U R                   U R                     R                  R                  nU R                   SU R                  -
     R                  R                  n[	        U R
                  5       Vs/ s H  nSPM     nn[        UR                  5       5       H  u  pVUR                  U5      nUc  M  XTU'   M!     [        R                  " U[        R                  S9$ s  snf )a„  Infers the ids of the tokens to carry over from batch N to batch N+1. In asynchronous batching mode, we can
schedule a request for batch N+1 without knowing the token predicted for that request in batch N. For that
reason, we might need to carry over tokens just predicted in batch N before launching the forwar pass of batch
N+1. This method computes the ids of the tokens to carry over.r   r¢   r¹   )r  r  r	  r[   rU   rv   rÇ   r‰   Úgetr6   rÕ   r|   )r+   Ú!next_req_id_to_new_token_positionÚ!prev_req_id_to_new_token_positionre   r�   rÖ   Úreq_idÚnew_token_positions           r,   r&  Ú/ContinuousBatchingAsyncIOs.infer_carry_over_idsÒ  sÇ   € ð
 -1¯M©M¸$×:KÑ:KÑ,L×,TÑ,T×,qÑ,qÐ)Ø,0¯M©M¸!¸d×>OÑ>OÑ:OÑ,P×,XÑ,X×,uÑ,uÐ)Ü&+¨D×,AÑ,AÔ&BÓCÒ&B ›"Ñ&BˆÐCô #Ð#D×#IÑ#IÓ#KÖL‰IˆAØ!B×!FÑ!FÀvÓ!NÐØ!Ó-Ø56Ð1Ó2ñ Mô �|Š|˜N´%·+±+Ñ>Ð>ùò Ds   Á5C"râ   c                 ó0  • U R                   U R                     nUR                  U R                  5        U R                  R	                  UR
                  5        U R                  R                  UR
                  5        UR                  R                  US9$ )N)râ   )
r  r  r  r   Úrecord_eventr  rc   Ú
wait_eventr
  rí   )r+   râ   r'  s      r,   rí   Ú+ContinuousBatchingAsyncIOs.get_model_kwargsæ  st   € Ø—-‘- × 1Ñ 1Ñ2ˆØ×#Ñ# D§O¡OÔ4Ø�‰×$Ñ$ W×%5Ñ%5Ô6Ø×Ñ×&Ñ& w×'7Ñ'7Ô8Ø× Ñ ×1Ñ1¸kÐ1ÐJÐJr/   c                 óî   • U R                   U R                     nU R                   SU R                  -
     nUR                  R                  UR                  R                  UR                  R                  4$ )aì  Returns the tensors used inside the generation step that are not inputs to the model forward pass. Those
tensors could be retrieved using this object, but it would trigger a recompile if using torch.compile. They are:
- output_ids: the output ids of the current batch
- prev_output_ids: the output ids of the previous batch, required to carry over outputs tokens of the previous
    batch to the input tokens of the next batch.
- carry_over_ids: a mask representing how to carry over tokens.
r   )r  r  r
  r�   r…   )r+   r  Úprevious_pairs      r,   rð   Ú(ContinuousBatchingAsyncIOs.get_cb_kwargsí  sh   € ð —}‘} T×%6Ñ%6Ñ7ˆØŸ™ a¨$×*;Ñ*;Ñ&;Ñ<ˆà×"Ñ"×1Ñ1Ø×#Ñ#×.Ñ.Ø×"Ñ"×-Ñ-ð
ð 	
r/   r   r�   r°   c                 ó®   • USU4   nUS:g  R                  5       nUSUR                  S5       nUSUR                  S5       nXE-  US   SU-
  -  -   US'   g)zõAs explained in the infer_carry_over_ids method, we might need to carry over tokens just predicted in batch N
before launching the forwar pass of batch N+1. This method performs the carry over, and is recorded in CUDA
graphs if they are enabled.r   r¢   Nr   )r;   ru   )r+   r   r�   r°   Úcarried_over_idsÚcarried_over_masks         r,   r³   Ú,ContinuousBatchingAsyncIOs.carry_over_tokensý  sv   € ð +¨1¨nÐ+<Ñ=ÐØ+¨rÑ1×6Ñ6Ó8Ðà+Ð,?¨i¯n©n¸QÓ.?Ð@ÐØ-Ð.A°	·±¸qÓ0AÐBÐà'Ñ;¸iÈ¹lÈaÐRcÑNcÑ>dÑdˆ	�!Šr/   c                 ó\   • U R                   U R                     R                  R                  $ r²   )r  r  r
  r…   r*   s    r,   r…   Ú%ContinuousBatchingAsyncIOs.output_ids  s%   € ð �}‰}˜T×.Ñ.Ñ/×9Ñ9×DÑDÐDr/   c                 ód   • U R                   U R                     R                  R                  5       $ r²   )r  r  r
  r÷   r*   s    r,   r÷   Ú$ContinuousBatchingAsyncIOs.get_graph  s&   € Ø�}‰}˜T×.Ñ.Ñ/×9Ñ9×CÑCÓEÐEr/   rû   c                 óh   • U R                   U R                     R                  R                  U5        g r²   )r  r  r
  rþ   )r+   rû   s     r,   rþ   Ú$ContinuousBatchingAsyncIOs.set_graph  s%   € Ø�‰�d×'Ñ'Ñ(×2Ñ2×<Ñ<¸UÕCr/   c                 ó\   • U R                   U R                     R                  R                  $ r²   )r  r  r	  rY   r*   s    r,   rY   Ú*ContinuousBatchingAsyncIOs.use_block_table  s#   € à�}‰}˜T×.Ñ.Ñ/×7Ñ7×GÑGÐGr/   c                 ór  • U R                   U R                     nU R                  R                  UR                  5        U R
                  R                  UR                  5        UR                  U R
                  5        U R
                  R                  UR                  5        SU R                  -
  U l        g )Nr   )	r  r  rc   r1  r  r!  r2  r  r  ©r+   r'  s     r,   r¶   Ú2ContinuousBatchingAsyncIOs.retrieve_device_outputs  s…   € Ø—-‘- × 1Ñ 1Ñ2ˆà×Ñ×(Ñ(¨×)=Ñ)=Ô>Ø�‰×"Ñ" 7×#7Ñ#7Ô8à×$Ñ$ T§_¡_Ô5Ø�‰×$Ñ$ W×%5Ñ%5Ô6à × 1Ñ 1Ñ1ˆÕr/   c                 óœ   • U R                   U R                     nUR                  R                  5         UR                  R                  5       $ r²   )r  r  r  rª   r	  r¿   rD  s     r,   r¿   Ú/ContinuousBatchingAsyncIOs.prepare_batch_update)  s;   € Ø—-‘- × 1Ñ 1Ñ2ˆØ×Ñ×$Ñ$Ô&Ø�‰×3Ñ3Ó5Ð5r/   c                 óô   • SU l         U R                   H  nUR                  5         M     U R                  R	                  5         U R
                  R	                  5         U R                  R	                  5         g)zWReset all state for a new generation session. Used in persistent mode between sessions.r   N)r  r  r«   r   rª   r!  rc   rD  s     r,   r«   Ú ContinuousBatchingAsyncIOs.reset.  sS   € àˆÔØ—}”}ˆGØ�M‰MŽOñ %à�‰×#Ñ#Ô%Ø�‰×#Ñ#Ô%Ø×Ñ×'Ñ'Õ)r/   )rc   r  r!  r   r  rv   r   r  )'r1   r2   r3   r4   r5   r   r   r6   rE   rl   r;   r=   r   rf   r  r7   r9   r:   r®   r<   r   rà   r&  r   rí   rð   r³   Úpropertyr…   rO   r  r÷   rþ   rY   r¶   r  r¿   r«   r>   r0   r/   r,   r  r  h  s*  † ñ+ðl ',ñ'7à"ð'7ð !ð'7ð —‘ð	'7ð
 —[‘[ð'7ð ð'7ð ð'7ð ?ð'7ð  $ð'7ð 
õ'7ðTQ¨¨e¯l©l¸DÀÀeÇlÁlÐARÑ<SÐ.SÑ(Tô QðJàÐ 2Ñ3ðJð AðJð #ð	Jð
 ðJð ðJð 
ôJð? e§l¡lô ?ñ(K¨Dð K¸TÀ#ÀsÀ(¹^õ Kð
˜u U§\¡\°5·<±<ÀÇÁÐ%MÑNô 
ð eØŸ™ðeØ7<·|±|ðeØV[×VbÑVbðeà	ôeð  ðE˜EŸL™Ló Eó ðEðF˜5Ÿ:™:×/Ñ/°$Ñ6ô FðD˜uŸz™z×3Ñ3ð D¸ô Dð ðH ó Hó ðHô	2ð6 e¨DÐ1CÑ,DÀdÈ3ÁiÐQUÐV[ÑQ\Ð_cÑQcÐ,cÑ&dô 6÷
*r/   r  ) Ú
contextlibr   Údataclassesr   Ú	functoolsr   Útypingr   r6   Ú transformers.configuration_utilsr   Úutilsr	   Úutils.metricsr
   r#   r   Úcb_logits_processorsr   Úrequestsr   r   r   r   r   r   r   r   r   r@   r  r  r0   r/   r,   Ú<module>rT     sq   ðõ #Ý !Ý Ý ã å =å *Ý #Ý &Ý Gß >Ñ >ß jÕ jð ÷6
ð 6
ó ð6
÷rV3ñ V3÷r3Xñ 3X÷lM*ò M*r/   