xpu_executor.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from typing import List, Optional
  2. import torch
  3. from loguru import logger
  4. from aphrodite.common.config import (CacheConfig, DeviceConfig, LoadConfig,
  5. LoRAConfig, ModelConfig, ParallelConfig,
  6. SchedulerConfig, SpeculativeConfig,
  7. VisionLanguageConfig)
  8. from aphrodite.common.sequence import ExecuteModelRequest, SamplerOutput
  9. from aphrodite.common.utils import make_async
  10. from aphrodite.executor.executor_base import ExecutorAsyncBase
  11. from aphrodite.executor.gpu_executor import GPUExecutor
  12. from aphrodite.task_handler.worker_base import WorkerWrapperBase
  13. class XPUExecutor(GPUExecutor):
  14. def __init__(
  15. self,
  16. model_config: ModelConfig,
  17. cache_config: CacheConfig,
  18. parallel_config: ParallelConfig,
  19. scheduler_config: SchedulerConfig,
  20. device_config: DeviceConfig,
  21. load_config: LoadConfig,
  22. lora_config: Optional[LoRAConfig],
  23. vision_language_config: Optional[VisionLanguageConfig],
  24. speculative_config: Optional[SpeculativeConfig],
  25. ) -> None:
  26. assert device_config.device_type == "xpu"
  27. assert (not speculative_config
  28. ), "Speculative decoding not yet supported for XPU backend"
  29. model_config = _verify_and_get_model_config(model_config)
  30. self.model_config = model_config
  31. self.cache_config = cache_config
  32. self.load_config = load_config
  33. self.lora_config = lora_config
  34. self.parallel_config = parallel_config
  35. self.scheduler_config = scheduler_config
  36. self.device_config = device_config
  37. self.vision_language_config = vision_language_config
  38. self.speculative_config = None
  39. # Instantiate the worker and load the model to GPU.
  40. self._init_executor()
  41. def _create_worker(self,
  42. local_rank: int = 0,
  43. rank: int = 0,
  44. distributed_init_method: Optional[str] = None):
  45. if self.speculative_config is None:
  46. worker_module_name = "aphrodite.task_handler.xpu_worker"
  47. worker_class_name = "XPUWorker"
  48. else:
  49. raise NotImplementedError(
  50. "XPU does not support speculative decoding")
  51. wrapper = WorkerWrapperBase(
  52. worker_module_name=worker_module_name,
  53. worker_class_name=worker_class_name,
  54. )
  55. wrapper.init_worker(**self._get_worker_kwargs(local_rank, rank,
  56. distributed_init_method))
  57. return wrapper.worker
  58. def execute_model(
  59. self,
  60. execute_model_req: ExecuteModelRequest) -> List[SamplerOutput]:
  61. output = self.driver_worker.execute_model(execute_model_req)
  62. return output
  63. class XPUExecutorAsync(XPUExecutor, ExecutorAsyncBase):
  64. async def execute_model_async(
  65. self,
  66. execute_model_req: ExecuteModelRequest,
  67. ) -> List[SamplerOutput]:
  68. output = await make_async(self.driver_worker.execute_model
  69. )(execute_model_req=execute_model_req)
  70. return output
  71. def _verify_and_get_model_config(config: ModelConfig) -> ModelConfig:
  72. if config.dtype == torch.bfloat16:
  73. logger.warning(
  74. "bfloat16 is not fully supported on XPU, casting to float16.")
  75. config.dtype = torch.float16
  76. if not config.enforce_eager:
  77. logger.warning(
  78. "CUDA graph is not supported on XPU, fallback to the eager "
  79. "mode.")
  80. config.enforce_eager = True
  81. return config