Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Qlora scripts on Intel GPUs #219

Merged
merged 2 commits into from
Jul 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions qlora.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import logging
import bitsandbytes as bnb
import pandas as pd
import importlib
from packaging import version
from packaging.version import parse

import torch
import transformers
Expand Down Expand Up @@ -41,7 +44,31 @@
from transformers.trainer_utils import PREFIX_CHECKPOINT_DIR


torch.backends.cuda.matmul.allow_tf32 = True
def is_ipex_available():
def get_major_and_minor_from_version(full_version):
return str(version.parse(full_version).major) + "." + str(version.parse(full_version).minor)

_torch_version = importlib.metadata.version("torch")
if importlib.util.find_spec("intel_extension_for_pytorch") is None:
return False
_ipex_version = "N/A"
try:
_ipex_version = importlib.metadata.version("intel_extension_for_pytorch")
except importlib.metadata.PackageNotFoundError:
return False
torch_major_and_minor = get_major_and_minor_from_version(_torch_version)
ipex_major_and_minor = get_major_and_minor_from_version(_ipex_version)
if torch_major_and_minor != ipex_major_and_minor:
warnings.warn(
f"Intel Extension for PyTorch {ipex_major_and_minor} needs to work with PyTorch {ipex_major_and_minor}.*,"
f" but PyTorch {_torch_version} is found. Please switch to the matching version and run again."
)
return False
return True


if torch.cuda.is_available():
torch.backends.cuda.matmul.allow_tf32 = True

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -261,7 +288,11 @@ def touch(fname, times=None):

def get_accelerate_model(args, checkpoint_dir):

n_gpus = torch.cuda.device_count()
if torch.cuda.is_available():
n_gpus = torch.cuda.device_count()
if is_ipex_available() and torch.xpu.is_available():
n_gpus = torch.xpu.device_count()

max_memory = f'{args.max_memory_MB}MB'
max_memory = {i: max_memory for i in range(n_gpus)}
device_map = "auto"
Expand Down Expand Up @@ -303,6 +334,10 @@ def get_accelerate_model(args, checkpoint_dir):
print('='*80)
print('Your GPU supports bfloat16, you can accelerate training with the argument --bf16')
print('='*80)

if compute_dtype == torch.float16 and (is_ipex_available() and torch.xpu.is_available()):
compute_dtype = torch.bfloat16
print('Intel XPU does not supports float16 yet, you can accelerate training with the argument --bf16')
abhilash1910 marked this conversation as resolved.
Show resolved Hide resolved

setattr(model, 'model_parallel', True)
setattr(model, 'is_parallelizable', True)
Expand Down Expand Up @@ -651,7 +686,7 @@ def train():
**vars(model_args), **vars(data_args), **vars(training_args)
)
print(args)

checkpoint_dir, completed_training = get_last_checkpoint(args.output_dir)
if completed_training:
print('Detected that training was already completed!')
Expand Down