Skip to content

Commit 210c504

Browse files
authored
Add support for the latest GPTQ models with group-size (oobabooga#530)
**Warning: old 4-bit weights will not work anymore!** See here how to get up to date weights: https://github.com/oobabooga/text-generation-webui/wiki/LLaMA-model#step-2-get-the-pre-converted-weights
1 parent a6189ef commit 210c504

File tree

5 files changed

+63
-42
lines changed

5 files changed

+63
-42
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ Optionally, you can use the following command-line flags:
176176
| `--cai-chat` | Launch the web UI in chat mode with a style similar to Character.AI's. If the file `img_bot.png` or `img_bot.jpg` exists in the same folder as server.py, this image will be used as the bot's profile picture. Similarly, `img_me.png` or `img_me.jpg` will be used as your profile picture. |
177177
| `--cpu` | Use the CPU to generate text.|
178178
| `--load-in-8bit` | Load the model with 8-bit precision.|
179-
| `--load-in-4bit` | DEPRECATED: use `--gptq-bits 4` instead. |
180-
| `--gptq-bits GPTQ_BITS` | GPTQ: Load a pre-quantized model with specified precision. 2, 3, 4 and 8 (bit) are supported. Currently only works with LLaMA and OPT. |
181-
| `--gptq-model-type MODEL_TYPE` | GPTQ: Model type of pre-quantized model. Currently only LLaMa and OPT are supported. |
182-
| `--gptq-pre-layer GPTQ_PRE_LAYER` | GPTQ: The number of layers to preload. |
179+
| `--wbits WBITS` | GPTQ: Load a pre-quantized model with specified precision in bits. 2, 3, 4 and 8 are supported. |
180+
| `--model_type MODEL_TYPE` | GPTQ: Model type of pre-quantized model. Currently only LLaMA and OPT are supported. |
181+
| `--groupsize GROUPSIZE` | GPTQ: Group size. |
182+
| `--pre_layer PRE_LAYER` | GPTQ: The number of layers to preload. |
183183
| `--bf16` | Load the model with bfloat16 precision. Requires NVIDIA Ampere GPU. |
184184
| `--auto-devices` | Automatically split the model across the available GPU(s) and CPU.|
185185
| `--disk` | If the model is too large for your GPU(s) and CPU combined, send the remaining layers to the disk. |

modules/GPTQ_loader.py

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@
1414

1515

1616
def load_quantized(model_name):
17-
if not shared.args.gptq_model_type:
17+
if not shared.args.model_type:
1818
# Try to determine model type from model name
19-
model_type = model_name.split('-')[0].lower()
20-
if model_type not in ('llama', 'opt'):
21-
print("Can't determine model type from model name. Please specify it manually using --gptq-model-type "
19+
if model_name.lower().startswith(('llama', 'alpaca')):
20+
model_type = 'llama'
21+
elif model_name.lower().startswith(('opt', 'galactica')):
22+
model_type = 'opt'
23+
else:
24+
print("Can't determine model type from model name. Please specify it manually using --model_type "
2225
"argument")
2326
exit()
2427
else:
25-
model_type = shared.args.gptq_model_type.lower()
28+
model_type = shared.args.model_type.lower()
2629

2730
if model_type == 'llama':
28-
if not shared.args.gptq_pre_layer:
31+
if not shared.args.pre_layer:
2932
load_quant = llama.load_quant
3033
else:
3134
load_quant = llama_inference_offload.load_quant
@@ -35,35 +38,44 @@ def load_quantized(model_name):
3538
print("Unknown pre-quantized model type specified. Only 'llama' and 'opt' are supported")
3639
exit()
3740

41+
# Now we are going to try to locate the quantized model file.
3842
path_to_model = Path(f'models/{model_name}')
39-
if path_to_model.name.lower().startswith('llama-7b'):
40-
pt_model = f'llama-7b-{shared.args.gptq_bits}bit'
41-
elif path_to_model.name.lower().startswith('llama-13b'):
42-
pt_model = f'llama-13b-{shared.args.gptq_bits}bit'
43-
elif path_to_model.name.lower().startswith('llama-30b'):
44-
pt_model = f'llama-30b-{shared.args.gptq_bits}bit'
45-
elif path_to_model.name.lower().startswith('llama-65b'):
46-
pt_model = f'llama-65b-{shared.args.gptq_bits}bit'
43+
found_pts = list(path_to_model.glob("*.pt"))
44+
found_safetensors = list(path_to_model.glob("*.safetensors"))
45+
pt_path = None
46+
47+
if len(found_pts) == 1:
48+
pt_path = found_pts[0]
49+
elif len(found_safetensors) == 1:
50+
pt_path = found_safetensors[0]
4751
else:
48-
pt_model = f'{model_name}-{shared.args.gptq_bits}bit'
52+
if path_to_model.name.lower().startswith('llama-7b'):
53+
pt_model = f'llama-7b-{shared.args.wbits}bit'
54+
elif path_to_model.name.lower().startswith('llama-13b'):
55+
pt_model = f'llama-13b-{shared.args.wbits}bit'
56+
elif path_to_model.name.lower().startswith('llama-30b'):
57+
pt_model = f'llama-30b-{shared.args.wbits}bit'
58+
elif path_to_model.name.lower().startswith('llama-65b'):
59+
pt_model = f'llama-65b-{shared.args.wbits}bit'
60+
else:
61+
pt_model = f'{model_name}-{shared.args.wbits}bit'
4962

50-
# Try to find the .safetensors or .pt both in models/ and in the subfolder
51-
pt_path = None
52-
for path in [Path(p+ext) for ext in ['.safetensors', '.pt'] for p in [f"models/{pt_model}", f"{path_to_model}/{pt_model}"]]:
53-
if path.exists():
54-
print(f"Found {path}")
55-
pt_path = path
56-
break
63+
# Try to find the .safetensors or .pt both in models/ and in the subfolder
64+
for path in [Path(p+ext) for ext in ['.safetensors', '.pt'] for p in [f"models/{pt_model}", f"{path_to_model}/{pt_model}"]]:
65+
if path.exists():
66+
print(f"Found {path}")
67+
pt_path = path
68+
break
5769

5870
if not pt_path:
59-
print(f"Could not find {pt_model}, exiting...")
71+
print("Could not find the quantized model in .pt or .safetensors format, exiting...")
6072
exit()
6173

6274
# qwopqwop200's offload
63-
if shared.args.gptq_pre_layer:
64-
model = load_quant(str(path_to_model), str(pt_path), shared.args.gptq_bits, shared.args.gptq_pre_layer)
75+
if shared.args.pre_layer:
76+
model = load_quant(str(path_to_model), str(pt_path), shared.args.wbits, shared.args.groupsize, shared.args.pre_layer)
6577
else:
66-
model = load_quant(str(path_to_model), str(pt_path), shared.args.gptq_bits)
78+
model = load_quant(str(path_to_model), str(pt_path), shared.args.wbits, shared.args.groupsize)
6779

6880
# accelerate offload (doesn't work properly)
6981
if shared.args.gpu_memory:

modules/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def load_model(model_name):
4444
shared.is_RWKV = model_name.lower().startswith('rwkv-')
4545

4646
# Default settings
47-
if not any([shared.args.cpu, shared.args.load_in_8bit, shared.args.gptq_bits, 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]):
47+
if not any([shared.args.cpu, shared.args.load_in_8bit, shared.args.wbits, 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]):
4848
if any(size in shared.model_name.lower() for size in ('13b', '20b', '30b')):
4949
model = AutoModelForCausalLM.from_pretrained(Path(f"models/{shared.model_name}"), device_map='auto', load_in_8bit=True)
5050
else:
@@ -95,7 +95,7 @@ def load_model(model_name):
9595
return model, tokenizer
9696

9797
# Quantized model
98-
elif shared.args.gptq_bits > 0:
98+
elif shared.args.wbits > 0:
9999
from modules.GPTQ_loader import load_quantized
100100

101101
model = load_quantized(model_name)

modules/shared.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@
5252
'default': 'Common sense questions and answers\n\nQuestion: \nFactual answer:',
5353
'^(gpt4chan|gpt-4chan|4chan)': '-----\n--- 865467536\nInput text\n--- 865467537\n',
5454
'(rosey|chip|joi)_.*_instruct.*': 'User: \n',
55-
'oasst-*': '<|prompter|>Write a story about future of AI development<|endoftext|><|assistant|>'
55+
'oasst-*': '<|prompter|>Write a story about future of AI development<|endoftext|><|assistant|>',
56+
'alpaca-*': "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n### Instruction:\nWrite a poem about the transformers Python library. \nMention the word \"large language models\" in that poem.\n### Response:\n",
5657
},
5758
'lora_prompts': {
5859
'default': 'Common sense questions and answers\n\nQuestion: \nFactual answer:',
@@ -78,10 +79,15 @@ def str2bool(v):
7879
parser.add_argument('--cai-chat', action='store_true', help='Launch the web UI in chat mode with a style similar to Character.AI\'s. If the file img_bot.png or img_bot.jpg exists in the same folder as server.py, this image will be used as the bot\'s profile picture. Similarly, img_me.png or img_me.jpg will be used as your profile picture.')
7980
parser.add_argument('--cpu', action='store_true', help='Use the CPU to generate text.')
8081
parser.add_argument('--load-in-8bit', action='store_true', help='Load the model with 8-bit precision.')
81-
parser.add_argument('--load-in-4bit', action='store_true', help='DEPRECATED: use --gptq-bits 4 instead.')
82-
parser.add_argument('--gptq-bits', type=int, default=0, help='GPTQ: Load a pre-quantized model with specified precision. 2, 3, 4 and 8bit are supported. Currently only works with LLaMA and OPT.')
83-
parser.add_argument('--gptq-model-type', type=str, help='GPTQ: Model type of pre-quantized model. Currently only LLaMa and OPT are supported.')
84-
parser.add_argument('--gptq-pre-layer', type=int, default=0, help='GPTQ: The number of layers to preload.')
82+
83+
parser.add_argument('--gptq-bits', type=int, default=0, help='DEPRECATED: use --wbits instead.')
84+
parser.add_argument('--gptq-model-type', type=str, help='DEPRECATED: use --model_type instead.')
85+
parser.add_argument('--gptq-pre-layer', type=int, default=0, help='DEPRECATED: use --pre_layer instead.')
86+
parser.add_argument('--wbits', type=int, default=0, help='GPTQ: Load a pre-quantized model with specified precision in bits. 2, 3, 4 and 8 are supported.')
87+
parser.add_argument('--model_type', type=str, help='GPTQ: Model type of pre-quantized model. Currently only LLaMA and OPT are supported.')
88+
parser.add_argument('--groupsize', type=int, default=-1, help='GPTQ: Group size.')
89+
parser.add_argument('--pre_layer', type=int, default=0, help='GPTQ: The number of layers to preload.')
90+
8591
parser.add_argument('--bf16', action='store_true', help='Load the model with bfloat16 precision. Requires NVIDIA Ampere GPU.')
8692
parser.add_argument('--auto-devices', action='store_true', help='Automatically split the model across the available GPU(s) and CPU.')
8793
parser.add_argument('--disk', action='store_true', help='If the model is too large for your GPU(s) and CPU combined, send the remaining layers to the disk.')
@@ -109,6 +115,8 @@ def str2bool(v):
109115
args = parser.parse_args()
110116

111117
# Provisional, this will be deleted later
112-
if args.load_in_4bit:
113-
print("Warning: --load-in-4bit is deprecated and will be removed. Use --gptq-bits 4 instead.\n")
114-
args.gptq_bits = 4
118+
deprecated_dict = {'gptq_bits': ['wbits', 0], 'gptq_model_type': ['model_type', None], 'gptq_pre_layer': ['prelayer', 0]}
119+
for k in deprecated_dict:
120+
if eval(f"args.{k}") != deprecated_dict[k][1]:
121+
print(f"Warning: --{k} is deprecated and will be removed. Use --{deprecated_dict[k][0]} instead.")
122+
exec(f"args.{deprecated_dict[k][0]} = args.{k}")

server.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ def set_interface_arguments(interface_mode, extensions, cmd_active):
237237

238238
# Default UI settings
239239
default_preset = shared.settings['presets'][next((k for k in shared.settings['presets'] if re.match(k.lower(), shared.model_name.lower())), 'default')]
240-
default_text = shared.settings['lora_prompts'][next((k for k in shared.settings['lora_prompts'] if re.match(k.lower(), shared.lora_name.lower())), 'default')]
241-
if default_text == '':
240+
if shared.lora_name != "None":
241+
default_text = shared.settings['lora_prompts'][next((k for k in shared.settings['lora_prompts'] if re.match(k.lower(), shared.lora_name.lower())), 'default')]
242+
else:
242243
default_text = shared.settings['prompts'][next((k for k in shared.settings['prompts'] if re.match(k.lower(), shared.model_name.lower())), 'default')]
243244
title ='Text generation web UI'
244245
description = '\n\n# Text generation lab\nGenerate text using Large Language Models.\n'

0 commit comments

Comments
 (0)