neuron_executor.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 (not execute_model_req.blocks_to_swap_in
  39. and not execute_model_req.blocks_to_swap_out
  40. and not 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(execute_model_req)
  45. return output
  46. def add_lora(self, lora_request: LoRARequest) -> bool:
  47. return self.driver_worker.add_lora(lora_request)
  48. def remove_lora(self, lora_id: int) -> bool:
  49. return self.driver_worker.remove_lora(lora_id)
  50. def list_loras(self) -> Set[int]:
  51. return self.driver_worker.list_loras()
  52. def pin_lora(self, lora_id: int) -> bool:
  53. return self.driver_worker.pin_lora(lora_id)
  54. def add_prompt_adapter(self, prompt_adapter_request) -> bool:
  55. raise NotImplementedError(
  56. "Soft prompt is currently not supported by the Neuron backend.")
  57. def remove_prompt_adapter(self, prompt_adapter_id: int) -> bool:
  58. raise NotImplementedError(
  59. "Soft prompt is currently not supported by the Neuron backend.")
  60. def pin_prompt_adapter(self, prompt_adapter_id: int) -> bool:
  61. raise NotImplementedError(
  62. "Soft prompt is currently not supported by the Neuron backend.")
  63. def list_prompt_adapters(self) -> Set[int]:
  64. raise NotImplementedError(
  65. "Soft prompt is currently not supported by the Neuron backend.")
  66. def check_health(self) -> None:
  67. # NeuronExecutor will always be healthy as long as
  68. # it's running.
  69. return
  70. class NeuronExecutorAsync(NeuronExecutor, ExecutorAsyncBase):
  71. async def execute_model_async(
  72. self,
  73. execute_model_req: ExecuteModelRequest,
  74. ) -> List[SamplerOutput]:
  75. output = await make_async(
  76. self.driver_worker.execute_model
  77. )(seq_group_metadata_list=execute_model_req.seq_group_metadata_list, )
  78. return output
  79. async def check_health_async(self) -> None:
  80. # NeuronExecutor will always be healthy as long as
  81. # it's running.
  82. return