neuron_executor.py 4.2 KB

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