neuron_executor.py 3.1 KB

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