utils.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import time
  2. from typing import List, Optional
  3. from typing import Sequence as GenericSequence
  4. from typing import Tuple
  5. from aphrodite import SamplingParams
  6. from aphrodite.common.sequence import Logprob, Sequence, SequenceGroup
  7. from aphrodite.lora.request import LoRARequest
  8. def create_dummy_prompt(
  9. request_id: str,
  10. prompt_length: int,
  11. block_size: Optional[int] = None,
  12. lora_request: Optional[LoRARequest] = None,
  13. use_beam_search: bool = False,
  14. best_of: int = 1,
  15. prompt_tokens: Optional[List[int]] = None,
  16. ) -> Tuple[Sequence, SequenceGroup]:
  17. if not block_size:
  18. block_size = prompt_length
  19. if prompt_tokens is None:
  20. # Create dummy prompt sequence with tokens 0...block_size-1
  21. # and prompt "0 ... block_size".
  22. prompt_tokens = list(range(prompt_length))
  23. prompt_str = " ".join([str(t) for t in prompt_tokens])
  24. prompt = Sequence(int(request_id),
  25. inputs={
  26. "prompt": prompt_str,
  27. "prompt_token_ids": prompt_tokens,
  28. },
  29. block_size=block_size)
  30. seq_group = SequenceGroup(request_id=request_id,
  31. seqs=[prompt],
  32. arrival_time=time.time(),
  33. sampling_params=SamplingParams(
  34. use_beam_search=use_beam_search,
  35. best_of=best_of),
  36. lora_request=lora_request)
  37. return prompt, seq_group
  38. def create_dummy_prompt_encoder_decoder(
  39. request_id: str,
  40. decoder_prompt_length: int,
  41. encoder_prompt_length: int,
  42. block_size: Optional[int] = None,
  43. lora_request: Optional[LoRARequest] = None,
  44. use_beam_search: bool = False,
  45. best_of: int = 1,
  46. ) -> Tuple[Sequence, Sequence, SequenceGroup]:
  47. if not block_size:
  48. block_size = decoder_prompt_length
  49. # Create dummy prompt sequence with tokens 0...block_size-1
  50. # and prompt "0 ... block_size". Note that the prompt string
  51. # doesn't actually match the tokens
  52. decoder_prompt_tokens = list(range(decoder_prompt_length))
  53. decoder_prompt_str = " ".join([str(t) for t in decoder_prompt_tokens])
  54. encoder_prompt_tokens = list(reversed(list(range(encoder_prompt_length))))
  55. encoder_prompt_str = " ".join([str(t) for t in encoder_prompt_tokens])
  56. inputs = {
  57. "prompt": decoder_prompt_str,
  58. "prompt_token_ids": decoder_prompt_tokens,
  59. "encoder_prompt": encoder_prompt_str,
  60. "encoder_prompt_token_ids": encoder_prompt_tokens,
  61. "multi_modal_data": None,
  62. }
  63. decoder_prompt = Sequence(int(request_id),
  64. inputs=inputs,
  65. block_size=block_size,
  66. from_decoder_prompt=True)
  67. encoder_prompt = Sequence(int(request_id),
  68. inputs=inputs,
  69. block_size=block_size,
  70. from_decoder_prompt=False)
  71. seq_group = SequenceGroup(request_id=request_id,
  72. seqs=[decoder_prompt],
  73. sampling_params=SamplingParams(
  74. use_beam_search=use_beam_search,
  75. best_of=best_of),
  76. arrival_time=time.time(),
  77. lora_request=lora_request,
  78. encoder_seq=encoder_prompt)
  79. return decoder_prompt, encoder_prompt, seq_group
  80. def create_seq_group(
  81. seq_prompt_len: int = 1024,
  82. seq_output_lens: GenericSequence[int] = (128, ),
  83. request_id: str = '0',
  84. seq_id_start: int = 0,
  85. sampling_params: Optional[SamplingParams] = None) -> SequenceGroup:
  86. assert len(seq_output_lens) > 0
  87. if sampling_params is None:
  88. sampling_params = SamplingParams()
  89. prompt_token_ids = [0] * seq_prompt_len
  90. seqs: List[Sequence] = []
  91. for seq_id_offset, output_len in enumerate(seq_output_lens):
  92. seq = Sequence(
  93. seq_id=seq_id_start + seq_id_offset,
  94. inputs={"prompt_token_ids": prompt_token_ids},
  95. block_size=16,
  96. )
  97. for i in range(output_len):
  98. seq.append_token_id(
  99. token_id=i,
  100. logprobs={i: Logprob(0.0)},
  101. )
  102. seqs.append(seq)
  103. seq_group = SequenceGroup(
  104. request_id=request_id,
  105. seqs=seqs,
  106. sampling_params=sampling_params,
  107. arrival_time=time.time(),
  108. )
  109. return seq_group
  110. def create_seq_group_encoder_decoder(
  111. seq_prompt_len: int = 1024,
  112. seq_output_lens: GenericSequence[int] = (128, ),
  113. request_id: str = '0',
  114. seq_id_start: int = 0,
  115. sampling_params: Optional[SamplingParams] = None) -> SequenceGroup:
  116. assert len(seq_output_lens) > 0
  117. if sampling_params is None:
  118. sampling_params = SamplingParams()
  119. prompt_token_ids = [0] * seq_prompt_len
  120. inputs = {
  121. "prompt": "",
  122. "prompt_token_ids": prompt_token_ids,
  123. "encoder_prompt": "",
  124. "encoder_prompt_token_ids": prompt_token_ids,
  125. "multi_modal_data": None,
  126. }
  127. seqs = []
  128. for seq_id_offset, output_len in enumerate(seq_output_lens):
  129. # Construct decoder input sequences
  130. seq = Sequence(seq_id=seq_id_start + seq_id_offset,
  131. inputs=inputs,
  132. block_size=16,
  133. from_decoder_prompt=True)
  134. for i in range(output_len):
  135. seq.append_token_id(
  136. token_id=i,
  137. logprobs={i: Logprob(0.0)},
  138. )
  139. seqs.append(seq)
  140. # Encoder input sequence
  141. encoder_seq = Sequence(seq_id=seq_id_start + len(seq_output_lens),
  142. inputs=inputs,
  143. block_size=16,
  144. from_decoder_prompt=False)
  145. return SequenceGroup(request_id=request_id,
  146. seqs=seqs,
  147. sampling_params=sampling_params,
  148. arrival_time=time.time(),
  149. encoder_seq=encoder_seq)
  150. def round_up_to_next_block(seq_len: int, block_size: int) -> int:
  151. return (seq_len + block_size - 1) // block_size
  152. # Helper functions for scheduler tests
  153. def get_sequence_groups(scheduler_output):
  154. return [s.seq_group for s in scheduler_output.scheduled_seq_groups]
  155. def append_new_token(out, token_id: int):
  156. seq_groups = get_sequence_groups(out)
  157. for seq_group in seq_groups:
  158. for seq in seq_group.get_seqs():
  159. seq.append_token_id(token_id, {token_id: Logprob(token_id)})
  160. def schedule_and_update_computed_tokens(scheduler):
  161. metas, out, _ = scheduler.schedule()
  162. for s, meta in zip(out.scheduled_seq_groups, metas):
  163. s.seq_group.update_num_computed_tokens(meta.token_chunk_size)
  164. return metas, out
  165. def append_new_token_seq_group(token_chunk_size, seq_group, token_id: int):
  166. seq_group.update_num_computed_tokens(token_chunk_size)
  167. for seq in seq_group.get_seqs():
  168. seq.append_token_id(token_id, {token_id: Logprob(token_id)})