interfaces.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 LLMEngine and should be seen as an extension
  16. of it. The logic is separated to simplify the LLMEngine class and allow
  17. separate implementations for single-step decoding (which supports beam
  18. search sequence forking) and multi-step decoding (which does not support
  19. beam search, but does support speculative decoding).
  20. """
  21. @staticmethod
  22. def create_output_processor(
  23. scheduler_config: SchedulerConfig,
  24. detokenizer: Detokenizer,
  25. scheduler: 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