add intel gpu with vllm support (#2903)

This commit is contained in:
Lei Zhenyuan 2025-07-10 05:08:38 +08:00 committed by GitHub
commit d188d5b7ce
4 changed files with 32 additions and 15 deletions

View file

@ -78,7 +78,7 @@ HAS_CUDA_STREAM = False
# INTEL GPU specific logic
if DEVICE_TYPE == "xpu":
# TODO: Changed here after adding XPU BNB support
HAS_XPU_STREAM = False
HAS_XPU_STREAM = True
def get_ptr(x: Optional[torch.Tensor]):
raise RuntimeError("XPU BNB support is not implemented yet. This function should not be called.")
else:

View file

@ -142,6 +142,12 @@ warnings.filterwarnings(action = "ignore", category = RuntimeWarning, module = "
import logging
logging.getLogger("transformers.tokenization_utils_base").setLevel(logging.CRITICAL+1)
def get_device_num():
if DEVICE_TYPE == "xpu":
return torch.xpu.device_count()
else:
return torch.cuda.device_count()
# Ignore logging messages
class HideLoggingMessage(logging.Filter):
__slots__ = "text",
@ -740,7 +746,7 @@ def get_statistics():
pass
pass
try:
devices = torch.cuda.device_count()
devices = get_device_num()
_get_statistics(f"{devices if devices <= 8 else 9}")
except:
pass
@ -767,7 +773,7 @@ BitsAndBytesConfig__init__ = BitsAndBytesConfig__init__.replace(
)
exec(BitsAndBytesConfig__init__, globals())
if torch.cuda.device_count() == 1:
if get_device_num() == 1:
from accelerate.utils.dataclasses import DistributedType
def _prepare_backend(self, *args, **kwargs): return None, DistributedType.NO
import accelerate.state

View file

@ -85,6 +85,11 @@ from triton import __version__ as triton_version
HAS_XFORMERS = xformers is not None
BlockDiagonalCausalMask = xformers.attn_bias.BlockDiagonalCausalMask if HAS_XFORMERS else None
def clean_gpu_cache():
if DEVICE_TYPE == "xpu":
torch.xpu.empty_cache()
else:
torch.cuda.empty_cache()
def original_apply_qkv(self, X):
Q = self.q_proj(X)
@ -1752,10 +1757,11 @@ class FastLlamaModel:
if not is_vLLM_available():
print("Unsloth: vLLM is not installed! Will use Unsloth inference!")
fast_inference = False
major_version, minor_version = torch.cuda.get_device_capability()
if major_version < 7:
print("Unsloth: vLLM does not work on older GPUs - will switch to Unsloth inference!")
fast_inference = False
if DEVICE_TYPE == "cuda":
major_version, minor_version = torch.cuda.get_device_capability()
if major_version < 7:
print("Unsloth: vLLM does not work on older GPUs - will switch to Unsloth inference!")
fast_inference = False
if unsloth_vllm_standby and os.environ.get("UNSLOTH_VLLM_STANDBY", "0") == "0":
raise RuntimeError("Unsloth: `unsloth_vllm_standby` is True, but environment variable `UNSLOTH_VLLM_STANDBY` is not set to 1!")
pass
@ -1779,8 +1785,8 @@ class FastLlamaModel:
num_gpus = torch.xpu.device_count()
gpu_stats_snippet = f"Intel Toolkit: {gpu_version}."
# TODO: After adding vLLM support for XPU, changed this
vllm_version = ""
try: vllm_version = f" vLLM: {importlib_version('vllm')}."
except: vllm_version = ""
else:
raise ValueError(f"Unsloth: Unsupported device type: {DEVICE_TYPE}")
@ -2020,7 +2026,10 @@ class FastLlamaModel:
import gc
for _ in range(3):
gc.collect()
torch.cuda.empty_cache()"""
if DEVICE_TYPE == "xpu":
torch.xpu.empty_cache()
else:
torch.cuda.empty_cache()"""
debug_info = debug_info.split('\n')
debug_info = "\n".join([debug_info[0]] + [spaces + x[8:] for x in debug_info[1:]])
@ -2508,7 +2517,7 @@ class FastLlamaModel:
# Remove old items to save VRAM
for _ in range(3):
gc.collect()
torch.cuda.empty_cache()
clean_gpu_cache()
pass
if train_lm_head:
@ -2519,7 +2528,7 @@ class FastLlamaModel:
# Remove old items to save VRAM
for _ in range(3):
gc.collect()
torch.cuda.empty_cache()
clean_gpu_cache()
pass
pass
@ -2580,7 +2589,7 @@ class FastLlamaModel:
# Clear deleted GPU items
for _ in range(3):
gc.collect()
torch.cuda.empty_cache()
clean_gpu_cache()
pass
# Patch for fast inference
@ -2796,7 +2805,7 @@ class FastLlamaModel:
# Clear deleted GPU items
for _ in range(3):
gc.collect()
torch.cuda.empty_cache()
clean_gpu_cache()
pass
# Patch for fast inference

View file

@ -26,6 +26,8 @@ import torch
import inspect
from collections import defaultdict
from unsloth_zoo.rl_replacements import RL_REPLACEMENTS
from unsloth import DEVICE_TYPE
RL_EXTRA_ARGS = defaultdict(list)
RL_FUNCTIONS = defaultdict(list)
RL_PRE_ITEMS = defaultdict(list)
@ -258,7 +260,7 @@ def grpo_trainer__get_per_token_logps(function_name, function):
if os.environ.get('UNSLOTH_FORCE_FLOAT32', '0') == '1': self._autocast_dtype = torch.float16
os.environ["UNSLOTH_RETURN_HIDDEN_STATES"] = "1"
with torch.amp.autocast(device_type = 'cuda', dtype = self._autocast_dtype):
with torch.amp.autocast(device_type = DEVICE_TYPE, dtype = self._autocast_dtype):
# We add 1 to `logits_to_keep` because the last logits of the sequence is later excluded
logits = model(
input_ids = input_ids,