util.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import torch
  2. from typing import List, Tuple
  3. from contextlib import contextmanager
  4. from itertools import chain
  5. from aphrodite.common.sequence import SequenceGroupMetadata, SamplerOutput
  6. SeqId = int
  7. def get_all_seq_ids(
  8. seq_group_metadata_list: List[SequenceGroupMetadata]) -> List[SeqId]:
  9. """Given a list of SequenceGroupMetadata, create a list of all
  10. sequence ids.
  11. """
  12. return list(
  13. chain.from_iterable([
  14. seq_group_metadata.seq_data.keys()
  15. for seq_group_metadata in seq_group_metadata_list
  16. ]))
  17. def split_batch_by_proposal_len(
  18. seq_group_metadata_list: List[SequenceGroupMetadata],
  19. proposal_lens: List[int],
  20. select_proposal_len_zero: bool,
  21. ) -> Tuple[List[SequenceGroupMetadata], List[int]]:
  22. """Utility function that splits a batch based on whether the proposal len is
  23. zero or not. We should remove this once Aphrodite supports per-sequence
  24. proposal lens in a batch.
  25. """
  26. if select_proposal_len_zero:
  27. predicate = lambda proposal_len: proposal_len == 0
  28. else:
  29. predicate = lambda proposal_len: proposal_len != 0
  30. indices = [
  31. i for i, (_, proposal_len
  32. ) in enumerate(zip(seq_group_metadata_list, proposal_lens))
  33. if predicate(proposal_len)
  34. ]
  35. seq_groups = [
  36. seq_group for seq_group, proposal_len in zip(
  37. seq_group_metadata_list, proposal_lens) if predicate(proposal_len)
  38. ]
  39. return seq_groups, indices
  40. def sampler_output_to_torch(
  41. sampler_output_list: List[SamplerOutput],
  42. ) -> Tuple[torch.Tensor, torch.Tensor]:
  43. """Utility function which converts a list of SamplerOutput to tensors.
  44. Returns:
  45. sampled_token_ids: torch.Tensor
  46. shape: [batch_size, len(sampler_output_list)]
  47. sampled_token_probs: torch.Tensor
  48. shape: [batch_size, len(sampler_output_list), vocab_size]
  49. """
  50. # shape: [batch_size, num_sampler_output, vocab_size]
  51. sampled_token_probs = torch.stack(
  52. [
  53. sampler_output.sampled_token_probs
  54. for sampler_output in sampler_output_list
  55. ],
  56. dim=0,
  57. ).transpose(0, 1)
  58. # shape: [batch_size, num_sampler_output]
  59. sampled_token_ids = torch.stack(
  60. [
  61. sampler_output.sampled_token_ids.flatten()
  62. for sampler_output in sampler_output_list
  63. ],
  64. dim=0,
  65. ).transpose(0, 1)
  66. return sampled_token_ids, sampled_token_probs
  67. @contextmanager
  68. def nvtx_range(msg, *args, **kwargs):
  69. """
  70. Context manager / decorator that pushes an NVTX range at the beginning
  71. of its scope, and pops it at the end. If extra arguments are given,
  72. they are passed as arguments to msg.format().
  73. If running with cuda graphs, you must enable nsys cuda graph profiling.
  74. Arguments:
  75. msg (string): message to associate with the range
  76. """
  77. torch.cuda.nvtx.range_push(msg.format(*args, **kwargs))
  78. try:
  79. yield
  80. finally:
  81. torch.cuda.nvtx.range_pop()