Move LLaMA 4-bit into a separate file
This commit is contained in:
parent
28fd4fc970
commit
fed3617f07
2 changed files with 64 additions and 50 deletions
|
@ -42,7 +42,7 @@ def load_model(model_name):
|
||||||
shared.is_RWKV = model_name.lower().startswith('rwkv-')
|
shared.is_RWKV = model_name.lower().startswith('rwkv-')
|
||||||
|
|
||||||
# Default settings
|
# Default settings
|
||||||
if not (shared.args.cpu or shared.args.load_in_8bit or shared.args.llama_bits>0 or shared.args.load_in_4bit or shared.args.auto_devices or shared.args.disk or shared.args.gpu_memory is not None or shared.args.cpu_memory is not None or shared.args.deepspeed or shared.args.flexgen or shared.is_RWKV):
|
if not any([shared.args.cpu, shared.args.load_in_8bit, shared.args.load_in_4bit, shared.args.llama_bits > 0, shared.args.auto_devices, shared.args.disk, shared.args.gpu_memory is not None, shared.args.cpu_memory is not None, shared.args.deepspeed, shared.args.flexgen, shared.is_RWKV]):
|
||||||
if any(size in shared.model_name.lower() for size in ('13b', '20b', '30b')):
|
if any(size in shared.model_name.lower() for size in ('13b', '20b', '30b')):
|
||||||
model = AutoModelForCausalLM.from_pretrained(Path(f"models/{shared.model_name}"), device_map='auto', load_in_8bit=True)
|
model = AutoModelForCausalLM.from_pretrained(Path(f"models/{shared.model_name}"), device_map='auto', load_in_8bit=True)
|
||||||
else:
|
else:
|
||||||
|
@ -88,56 +88,10 @@ def load_model(model_name):
|
||||||
return model, tokenizer
|
return model, tokenizer
|
||||||
|
|
||||||
# 4-bit LLaMA
|
# 4-bit LLaMA
|
||||||
elif shared.args.llama_bits>0 or shared.args.load_in_4bit:
|
elif shared.args.llama_bits > 0 or shared.args.load_in_4bit:
|
||||||
sys.path.insert(0, os.path.abspath(Path("repositories/GPTQ-for-LLaMa")))
|
from modules.quantized_LLaMA import load_quantized_LLaMA
|
||||||
if shared.args.load_in_4bit:
|
|
||||||
bits = 4
|
|
||||||
else:
|
|
||||||
bits = shared.args.llama_bits
|
|
||||||
|
|
||||||
|
|
||||||
from llama import load_quant
|
model = load_quantized_LLaMA(model_name)
|
||||||
|
|
||||||
path_to_model = Path(f'models/{model_name}')
|
|
||||||
pt_model = ''
|
|
||||||
if path_to_model.name.lower().startswith('llama-7b'):
|
|
||||||
pt_model = f'llama-7b-{bits}bit.pt'
|
|
||||||
elif path_to_model.name.lower().startswith('llama-13b'):
|
|
||||||
pt_model = f'llama-13b-{bits}bit.pt'
|
|
||||||
elif path_to_model.name.lower().startswith('llama-30b'):
|
|
||||||
pt_model = f'llama-30b-{bits}bit.pt'
|
|
||||||
elif path_to_model.name.lower().startswith('llama-65b'):
|
|
||||||
pt_model = f'llama-65b-{bits}bit.pt'
|
|
||||||
else:
|
|
||||||
pt_model = f'{model_name}-{bits}bit.pt'
|
|
||||||
|
|
||||||
# Try to find the .pt both in models/ and in the subfolder
|
|
||||||
pt_path = None
|
|
||||||
for path in [Path(p) for p in [f"models/{pt_model}", f"{path_to_model}/{pt_model}"]]:
|
|
||||||
if path.exists():
|
|
||||||
pt_path = path
|
|
||||||
|
|
||||||
if not pt_path:
|
|
||||||
print(f"Could not find {pt_model}, exiting...")
|
|
||||||
exit()
|
|
||||||
|
|
||||||
model = load_quant(path_to_model, pt_path, bits)
|
|
||||||
|
|
||||||
# Multi-GPU setup
|
|
||||||
if shared.args.gpu_memory:
|
|
||||||
import accelerate
|
|
||||||
|
|
||||||
max_memory = {}
|
|
||||||
for i in range(len(shared.args.gpu_memory)):
|
|
||||||
max_memory[i] = f"{shared.args.gpu_memory[i]}GiB"
|
|
||||||
max_memory['cpu'] = f"{shared.args.cpu_memory or '99'}GiB"
|
|
||||||
|
|
||||||
device_map = accelerate.infer_auto_device_map(model, max_memory=max_memory, no_split_module_classes=["LLaMADecoderLayer"])
|
|
||||||
model = accelerate.dispatch_model(model, device_map=device_map)
|
|
||||||
|
|
||||||
# Single GPU
|
|
||||||
else:
|
|
||||||
model = model.to(torch.device('cuda:0'))
|
|
||||||
|
|
||||||
# Custom
|
# Custom
|
||||||
else:
|
else:
|
||||||
|
|
60
modules/quantized_LLaMA.py
Normal file
60
modules/quantized_LLaMA.py
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import accelerate
|
||||||
|
import torch
|
||||||
|
|
||||||
|
import modules.shared as shared
|
||||||
|
|
||||||
|
sys.path.insert(0, os.path.abspath(Path("repositories/GPTQ-for-LLaMa")))
|
||||||
|
from llama import load_quant
|
||||||
|
|
||||||
|
|
||||||
|
# 4-bit LLaMA
|
||||||
|
def load_quantized_LLaMA(model_name):
|
||||||
|
if shared.args.load_in_4bit:
|
||||||
|
bits = 4
|
||||||
|
else:
|
||||||
|
bits = shared.args.llama_bits
|
||||||
|
|
||||||
|
path_to_model = Path(f'models/{model_name}')
|
||||||
|
pt_model = ''
|
||||||
|
if path_to_model.name.lower().startswith('llama-7b'):
|
||||||
|
pt_model = f'llama-7b-{bits}bit.pt'
|
||||||
|
elif path_to_model.name.lower().startswith('llama-13b'):
|
||||||
|
pt_model = f'llama-13b-{bits}bit.pt'
|
||||||
|
elif path_to_model.name.lower().startswith('llama-30b'):
|
||||||
|
pt_model = f'llama-30b-{bits}bit.pt'
|
||||||
|
elif path_to_model.name.lower().startswith('llama-65b'):
|
||||||
|
pt_model = f'llama-65b-{bits}bit.pt'
|
||||||
|
else:
|
||||||
|
pt_model = f'{model_name}-{bits}bit.pt'
|
||||||
|
|
||||||
|
# Try to find the .pt both in models/ and in the subfolder
|
||||||
|
pt_path = None
|
||||||
|
for path in [Path(p) for p in [f"models/{pt_model}", f"{path_to_model}/{pt_model}"]]:
|
||||||
|
if path.exists():
|
||||||
|
pt_path = path
|
||||||
|
|
||||||
|
if not pt_path:
|
||||||
|
print(f"Could not find {pt_model}, exiting...")
|
||||||
|
exit()
|
||||||
|
|
||||||
|
model = load_quant(path_to_model, pt_path, bits)
|
||||||
|
|
||||||
|
# Multi-GPU setup
|
||||||
|
if shared.args.gpu_memory:
|
||||||
|
max_memory = {}
|
||||||
|
for i in range(len(shared.args.gpu_memory)):
|
||||||
|
max_memory[i] = f"{shared.args.gpu_memory[i]}GiB"
|
||||||
|
max_memory['cpu'] = f"{shared.args.cpu_memory or '99'}GiB"
|
||||||
|
|
||||||
|
device_map = accelerate.infer_auto_device_map(model, max_memory=max_memory, no_split_module_classes=["LLaMADecoderLayer"])
|
||||||
|
model = accelerate.dispatch_model(model, device_map=device_map)
|
||||||
|
|
||||||
|
# Single GPU
|
||||||
|
else:
|
||||||
|
model = model.to(torch.device('cuda:0'))
|
||||||
|
|
||||||
|
return model
|
Loading…
Add table
Add a link
Reference in a new issue