neuron_executor.py 4.2 KB

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