neuron_executor.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from typing import Dict, List, Set
  2. from aphrodite.lora.request import LoRARequest
  3. from aphrodite.executor.executor_base import ExecutorBase
  4. from aphrodite.common.sequence import SamplerOutput, SequenceGroupMetadata
  5. class NeuronExecutor(ExecutorBase):
  6. def _init_executor(self) -> None:
  7. assert (self.lora_config is
  8. None), "LoRA is not supported for Neuron backend."
  9. assert (not self.speculative_config
  10. ), "Speculative decoding not yet supported for Neuron backend."
  11. # Instantiate the worker and load the model to the device.
  12. self._init_worker()
  13. def _init_worker(self):
  14. from aphrodite.task_handler.neuron_worker import NeuronWorker
  15. self.driver_worker = NeuronWorker(
  16. self.model_config,
  17. self.parallel_config,
  18. self.scheduler_config,
  19. self.device_config,
  20. self.cache_config,
  21. )
  22. self.driver_worker.init_device()
  23. self.driver_worker.load_model()
  24. def determine_num_available_blocks(self) -> tuple[int, int]:
  25. """Determine the number of available KV blocks by invoking the
  26. underlying worker.
  27. """
  28. return self.driver_worker.determine_num_available_blocks()
  29. def initialize_cache(self, num_gpu_blocks: int,
  30. num_cpu_blocks: int) -> None:
  31. """Initialize the KV cache by invoking the underlying worker.
  32. """
  33. self.driver_worker.initialize_cache(num_gpu_blocks, num_cpu_blocks)
  34. def execute_model(self,
  35. seq_group_metadata_list: List[SequenceGroupMetadata],
  36. blocks_to_swap_in: Dict[int, int],
  37. blocks_to_swap_out: Dict[int, int],
  38. blocks_to_copy: Dict[int, List[int]],
  39. num_lookahead_slots: int) -> List[SamplerOutput]:
  40. assert (blocks_to_swap_in == {} and blocks_to_swap_out == {}
  41. and blocks_to_copy == {}), (
  42. "Cache operations are not supported for Neuron backend.")
  43. assert num_lookahead_slots == 0, (
  44. "lookahead not supported for Neuron backend.")
  45. output = self.driver_worker.execute_model(
  46. seq_group_metadata_list=seq_group_metadata_list)
  47. return output
  48. def add_lora(self, lora_request: LoRARequest) -> bool:
  49. return self.driver_worker.add_lora(lora_request)
  50. def remove_lora(self, lora_id: int) -> bool:
  51. return self.driver_worker.remove_lora(lora_id)
  52. def list_loras(self) -> Set[int]:
  53. return self.driver_worker.list_loras()
  54. def check_health(self) -> None:
  55. # NeuronExecutor will always be healthy as long as
  56. # it's running.
  57. return