1
0

interfaces.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from abc import ABC, abstractmethod
  2. from typing import Callable, List
  3. from transformers import PreTrainedTokenizer
  4. from aphrodite.common.config import SchedulerConfig
  5. from aphrodite.common.sequence import (Sequence, SequenceGroup,
  6. SequenceGroupOutput)
  7. from aphrodite.common.utils import Counter
  8. from aphrodite.engine.output_processor.stop_checker import StopChecker
  9. from aphrodite.processing.scheduler import Scheduler
  10. from aphrodite.transformers_utils.detokenizer import Detokenizer
  11. class SequenceGroupOutputProcessor(ABC):
  12. """Interface for logic that processes new token ids in sequence groups,
  13. managing detokenization, stop checking, and freeing/forking sequences with
  14. the scheduler.
  15. This is highly coupled with the AphroditeEngine and should be seen as an
  16. extension of it. The logic is separated to simplify the AphroditeEngine
  17. class and allow separate implementations for single-step decoding
  18. (which supports beam search sequence forking) and multi-step decoding
  19. (which does not support beam search, but does support speculative decoding)
  20. """
  21. @staticmethod
  22. def create_output_processor(
  23. scheduler_config: SchedulerConfig,
  24. detokenizer: Detokenizer,
  25. scheduler: List[Scheduler],
  26. seq_counter: Counter,
  27. get_tokenizer_for_seq: Callable[[Sequence], PreTrainedTokenizer],
  28. stop_checker: "StopChecker",
  29. ):
  30. """Create an output processor.
  31. This returns a single-step output processor if num_lookahead_slots is
  32. zero, else returns a multi-step output processor.
  33. """
  34. if scheduler_config.num_lookahead_slots == 0:
  35. # Importing here to avoid cycle.
  36. from aphrodite.engine.output_processor.single_step import (
  37. SingleStepOutputProcessor)
  38. return SingleStepOutputProcessor(
  39. scheduler_config,
  40. detokenizer,
  41. scheduler,
  42. seq_counter,
  43. stop_checker,
  44. )
  45. else:
  46. # Importing here to avoid cycle.
  47. from aphrodite.engine.output_processor.multi_step import (
  48. MultiStepOutputProcessor)
  49. return MultiStepOutputProcessor(
  50. detokenizer,
  51. scheduler,
  52. seq_counter,
  53. get_tokenizer_for_seq,
  54. stop_checker,
  55. )
  56. @abstractmethod
  57. def process_outputs(self, sequence_group: SequenceGroup,
  58. outputs: List[SequenceGroupOutput]) -> None:
  59. """Process new token ids for the sequence group. Handles logic such as
  60. detokenization, stop checking, and freeing/forking sequences in the
  61. scheduler.
  62. """
  63. pass
  64. @abstractmethod
  65. def process_prompt_logprob(self, seq_group: SequenceGroup,
  66. outputs: List[SequenceGroupOutput]) -> None:
  67. """Update prompt logprobs received from outputs to seq_group."""
  68. pass