13 Commits

Author SHA1 Message Date
Artidoro Pagnoni 7f4e95a68d Merge pull request #183 from AlpinDale/patch-2
Enhance GPU `bfloat16` support check
2023-07-24 02:11:34 -07:00
Artidoro Pagnoni eee4f88f29 Merge pull request #178 from ranchlai/main
Add trust_remote_code for AutoTokenizer
2023-07-24 02:07:58 -07:00
Artidoro Pagnoni 5486baa59a Merge branch 'main' into main 2023-07-24 02:07:28 -07:00
Artidoro Pagnoni 76d86645b8 Merge pull request #219 from abhilash1910/main
Enable Qlora scripts on Intel GPUs
2023-07-24 01:25:03 -07:00
Artidoro Pagnoni 82c730a48a Merge pull request #143 from bubundas17/patch-1
Fixed Local .jsonl dataset loading
2023-07-24 01:15:44 -07:00
Artidoro Pagnoni 965f8b3430 generage example 2023-07-23 15:05:52 -07:00
Artidoro Pagnoni 6c6fc4653a Merge pull request #220 from artidoro/llama2
Example for LLaMA2 Finetuning and Version Update
2023-07-19 06:21:46 -07:00
abhilash1910 a9644b4154 xpu support 2023-07-19 04:01:03 -07:00
abhilash1910 a17d93e6fd xpu support 2023-07-19 00:47:53 -07:00
AlpinDale 0fc40da9dc Enhances GPU bfloat16 support check 2023-06-22 16:06:06 +00:00
ranchlai af90b31d5a add trust_remote_code for AutoTokenizer 2023-06-19 00:44:14 +08:00
Bubun Das 6d6a2ed677 Merge branch 'artidoro:main' into patch-1 2023-06-09 22:02:16 +05:30
Bubun Das 9c6e450468 Fixed Local .jsonl dataset loading 2023-06-08 00:18:53 +05:30
3 changed files with 123 additions and 9 deletions
+1 -1
View File
@@ -60,7 +60,7 @@ In addition, here are Colab notebooks with examples for inference and finetuning
- [Inference notebook](https://colab.research.google.com/drive/1ge2F1QSK8Q7h0hn3YKuBCOAS0bK8E0wf?usp=sharing)
- [Finetuning notebook](https://colab.research.google.com/drive/1VoYNfYDKcKRQRor98Zbf2-9VQTtGJ24k?usp=sharing)
Other examples are found under the `examples/` folder.
Other examples are found under the `examples/` folder. We include a generation getting started example with guanaco at `examples/guanaco_generate.py`.
### Quantization
Quantization parameters are controlled from the `BitsandbytesConfig` ([see HF documenation](https://huggingface.co/docs/transformers/main_classes/quantization#transformers.BitsAndBytesConfig)) as follows:
+81
View File
@@ -0,0 +1,81 @@
import os
from os.path import exists, join, isdir
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, GenerationConfig
from peft import PeftModel
from peft.tuners.lora import LoraLayer
def get_last_checkpoint(checkpoint_dir):
if isdir(checkpoint_dir):
is_completed = exists(join(checkpoint_dir, 'completed'))
if is_completed: return None, True # already finished
max_step = 0
for filename in os.listdir(checkpoint_dir):
if isdir(join(checkpoint_dir, filename)) and filename.startswith('checkpoint'):
max_step = max(max_step, int(filename.replace('checkpoint-', '')))
if max_step == 0: return None, is_completed # training started, but no checkpoint
checkpoint_dir = join(checkpoint_dir, f'checkpoint-{max_step}')
print(f"Found a previous checkpoint at: {checkpoint_dir}")
return checkpoint_dir, is_completed # checkpoint found!
return None, False # first training
# TODO: Update variables
max_new_tokens = 64
top_p = 0.9
temperature=0.7
user_question = "What is Einstein's theory of relativity?"
# Base model
model_name_or_path = 'huggyllama/llama-7b'
# Adapter name on HF hub or local checkpoint path.
# adapter_path, _ = get_last_checkpoint('qlora/output/guanaco-7b')
adapter_path = 'timdettmers/guanaco-7b'
tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
# Fixing some of the early LLaMA HF conversion issues.
tokenizer.bos_token_id = 1
# Load the model (use bf16 for faster inference)
model = AutoModelForCausalLM.from_pretrained(
model_name_or_path,
torch_dtype=torch.bfloat16,
device_map={"": 0},
load_in_4bit=True,
quantization_config=BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type='nf4',
)
)
model = PeftModel.from_pretrained(model, adapter_path)
model.eval()
prompt = (
"A chat between a curious human and an artificial intelligence assistant. "
"The assistant gives helpful, detailed, and polite answers to the user's questions. "
"### Human: {user_question}"
"### Assistant: "
)
def generate(model, user_question, max_new_tokens=max_new_tokens, top_p=top_p, temperature=temperature):
inputs = tokenizer(prompt.format(user_question=user_question), return_tensors="pt").to('cuda')
outputs = model.generate(
**inputs,
generation_config=GenerationConfig(
do_sample=True,
max_new_tokens=max_new_tokens,
top_p=top_p,
temperature=temperature,
)
)
text = tokenizer.decode(outputs[0], skip_special_tokens=True)
print(text)
return text
generate(model, user_question)
import pdb; pdb.set_trace()
+41 -8
View File
@@ -14,6 +14,9 @@ from tqdm import tqdm
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
@@ -41,7 +44,31 @@ from peft.tuners.lora import LoraLayer
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__)
@@ -261,7 +288,11 @@ class SavePeftModelCallback(transformers.TrainerCallback):
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"
@@ -298,11 +329,14 @@ def get_accelerate_model(args, checkpoint_dir):
use_auth_token=args.use_auth_token
)
if compute_dtype == torch.float16 and args.bits == 4:
major, minor = torch.cuda.get_device_capability()
if major >= 8:
if torch.cuda.is_bf16_supported():
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 support float16 yet, so switching to bfloat16')
setattr(model, 'model_parallel', True)
setattr(model, 'is_parallelizable', True)
@@ -316,6 +350,7 @@ def get_accelerate_model(args, checkpoint_dir):
padding_side="right",
use_fast=False, # Fast tokenizer giving issues.
tokenizer_type='llama' if 'llama' in args.model_name_or_path else None, # Needed for HF name change
trust_remote_code=args.trust_remote_code,
use_auth_token=args.use_auth_token,
)
if tokenizer._pad_token is None:
@@ -500,10 +535,8 @@ def extract_alpaca_dataset(example):
return {'input': prompt_format.format(**example)}
def local_dataset(dataset_name):
if dataset_name.endswith('.json'):
if dataset_name.endswith('.json') or dataset_name.endswith('.jsonl'):
full_dataset = Dataset.from_json(path_or_paths=dataset_name)
elif dataset_name.endswith('.jsonl'):
full_dataset = Dataset.from_json(filename=dataset_name, format='jsonlines')
elif dataset_name.endswith('.csv'):
full_dataset = Dataset.from_pandas(pd.read_csv(dataset_name))
elif dataset_name.endswith('.tsv'):
@@ -663,7 +696,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!')