1
0

interfaces.py 2.7 KB

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