neuron_executor.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. uses_ray: bool = False
  8. def _init_executor(self) -> None:
  9. assert (self.lora_config is
  10. None), "LoRA is not supported for Neuron backend."
  11. assert (not self.speculative_config
  12. ), "Speculative decoding not yet supported for Neuron backend."
  13. # Instantiate the worker and load the model to the device.
  14. self._init_worker()
  15. def _init_worker(self):
  16. from aphrodite.task_handler.neuron_worker import NeuronWorker
  17. self.driver_worker = NeuronWorker(
  18. self.model_config,
  19. self.parallel_config,
  20. self.scheduler_config,
  21. self.device_config,
  22. self.cache_config,
  23. )
  24. self.driver_worker.init_device()
  25. self.driver_worker.load_model()
  26. def determine_num_available_blocks(self) -> Tuple[int, int]:
  27. """Determine the number of available KV blocks by invoking the
  28. underlying worker.
  29. """
  30. return self.driver_worker.determine_num_available_blocks()
  31. def initialize_cache(self, num_gpu_blocks: int,
  32. num_cpu_blocks: int) -> None:
  33. """Initialize the KV cache by invoking the underlying worker.
  34. """
  35. self.driver_worker.initialize_cache(num_gpu_blocks, num_cpu_blocks)
  36. def execute_model(
  37. self,
  38. execute_model_req: ExecuteModelRequest) -> List[SamplerOutput]:
  39. assert (not execute_model_req.blocks_to_swap_in
  40. and not execute_model_req.blocks_to_swap_out
  41. and not execute_model_req.blocks_to_copy), (
  42. "Cache operations are not supported for Neuron backend.")
  43. assert execute_model_req.num_lookahead_slots == 0, (
  44. "lookahead not supported for Neuron backend.")
  45. output = self.driver_worker.execute_model(execute_model_req)
  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 pin_lora(self, lora_id: int) -> bool:
  54. return self.driver_worker.pin_lora(lora_id)
  55. def add_prompt_adapter(self, prompt_adapter_request) -> bool:
  56. raise NotImplementedError(
  57. "Soft prompt is currently not supported by the Neuron backend.")
  58. def remove_prompt_adapter(self, prompt_adapter_id: int) -> bool:
  59. raise NotImplementedError(
  60. "Soft prompt is currently not supported by the Neuron backend.")
  61. def pin_prompt_adapter(self, prompt_adapter_id: int) -> bool:
  62. raise NotImplementedError(
  63. "Soft prompt is currently not supported by the Neuron backend.")
  64. def list_prompt_adapters(self) -> Set[int]:
  65. raise NotImplementedError(
  66. "Soft prompt is currently not supported by the Neuron backend.")
  67. def check_health(self) -> None:
  68. # NeuronExecutor will always be healthy as long as
  69. # it's running.
  70. return
  71. class NeuronExecutorAsync(NeuronExecutor, ExecutorAsyncBase):
  72. async def execute_model_async(
  73. self,
  74. execute_model_req: ExecuteModelRequest,
  75. ) -> List[SamplerOutput]:
  76. output = await make_async(self.driver_worker.execute_model
  77. )(execute_model_req=execute_model_req, )
  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