tpu_executor.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from typing import List, Set, Tuple
  2. import torch
  3. from loguru import logger
  4. from aphrodite.common.sequence import ExecuteModelRequest, SamplerOutput
  5. from aphrodite.common.utils import (get_distributed_init_method, get_ip,
  6. get_open_port, make_async)
  7. from aphrodite.executor.executor_base import ExecutorAsyncBase, ExecutorBase
  8. from aphrodite.lora.request import LoRARequest
  9. class TPUExecutor(ExecutorBase):
  10. def _init_executor(self) -> None:
  11. assert not self.scheduler_config.chunked_prefill_enabled, (
  12. "Chunked prefill is not yet supported for TPU backend")
  13. assert not self.speculative_config, (
  14. "Speculative decoding is not yet supported for TPU backend")
  15. if self.model_config.dtype in (torch.float16, torch.float32):
  16. logger.warning(
  17. "The TPU backend currently does not support %s. "
  18. "Using bfloat16 instead.", self.model_config.dtype)
  19. self.model_config.dtype = torch.bfloat16
  20. # Instantiate the worker and load the model to the device.
  21. self._init_worker()
  22. def _init_worker(self):
  23. from aphrodite.task_handler.tpu_worker import TPUWorker
  24. assert self.parallel_config.world_size == 1, (
  25. "TPUExecutor currently only supports a single TPU chip.")
  26. distributed_init_method = get_distributed_init_method(
  27. get_ip(), get_open_port())
  28. self.driver_worker = TPUWorker(
  29. self.model_config,
  30. self.parallel_config,
  31. self.scheduler_config,
  32. self.device_config,
  33. self.cache_config,
  34. self.load_config,
  35. self.vision_language_config,
  36. local_rank=0,
  37. rank=0,
  38. distributed_init_method=distributed_init_method,
  39. )
  40. self.driver_worker.init_device()
  41. self.driver_worker.load_model()
  42. def initialize_cache(
  43. self,
  44. num_gpu_blocks: int,
  45. num_cpu_blocks: int,
  46. ) -> None:
  47. """Initialize the KV cache by invoking the underlying worker."""
  48. # NOTE: This is logged in the executor because there can be >1 worker
  49. # with other executors. We could log in the engine level, but work
  50. # remains to abstract away the device for non-GPU configurations.
  51. logger.info(f"# TPU blocks: {num_gpu_blocks}, "
  52. f"# CPU blocks: {num_cpu_blocks}")
  53. logger.info(
  54. f"Minimum concurrency: {num_gpu_blocks * self.cache_config.block_size / self.scheduler_config.max_model_len:.2f}x" # noqa: E501
  55. )
  56. self.driver_worker.initialize_cache(num_gpu_blocks, num_cpu_blocks)
  57. def determine_num_available_blocks(self) -> Tuple[int, int]:
  58. """Determine the number of available KV blocks by invoking the
  59. underlying worker.
  60. """
  61. return self.driver_worker.determine_num_available_blocks()
  62. def execute_model(
  63. self,
  64. execute_model_req: ExecuteModelRequest,
  65. ) -> List[SamplerOutput]:
  66. output = self.driver_worker.execute_model(execute_model_req)
  67. return output
  68. def add_lora(self, lora_request: LoRARequest) -> bool:
  69. raise NotImplementedError("LoRA is not implemented for TPU backend.")
  70. def remove_lora(self, lora_id: int) -> bool:
  71. raise NotImplementedError("LoRA is not implemented for TPU backend.")
  72. def list_loras(self) -> Set[int]:
  73. raise NotImplementedError("LoRA is not implemented for TPU backend.")
  74. def check_health(self) -> None:
  75. # TPUExecutor will always be healthy as long as it's running.
  76. return
  77. class TPUExecutorAsync(TPUExecutor, ExecutorAsyncBase):
  78. async def execute_model_async(
  79. self,
  80. sexecute_model_req: ExecuteModelRequest,
  81. ) -> SamplerOutput:
  82. output = await make_async(self.driver_worker.execute_model
  83. )(sexecute_model_req)
  84. return output