From 345b6dee8c65b0979812a9051864f9ae0e87d25c Mon Sep 17 00:00:00 2001 From: Ayanami Rei Date: Mon, 13 Mar 2023 19:59:57 +0300 Subject: [PATCH 01/17] refactor quant models loader and add support of OPT --- .../{quantized_LLaMA.py => quant_loader.py} | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) rename modules/{quantized_LLaMA.py => quant_loader.py} (61%) diff --git a/modules/quantized_LLaMA.py b/modules/quant_loader.py similarity index 61% rename from modules/quantized_LLaMA.py rename to modules/quant_loader.py index e9352f90..8bf505a6 100644 --- a/modules/quantized_LLaMA.py +++ b/modules/quant_loader.py @@ -7,28 +7,20 @@ import torch import modules.shared as shared sys.path.insert(0, str(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 +def load_quant(model_name, model_type): + if model_type == 'llama': + from llama import load_quant + elif model_type == 'opt': + from opt import load_quant else: - bits = shared.args.gptq_bits + print("Unknown pre-quantized model type specified. Only 'llama' and 'opt' are supported") + exit() 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' + pt_model = f'{model_name}-{shared.args.gptq_bits}bit.pt' # Try to find the .pt both in models/ and in the subfolder pt_path = None @@ -40,7 +32,7 @@ def load_quantized_LLaMA(model_name): print(f"Could not find {pt_model}, exiting...") exit() - model = load_quant(path_to_model, str(pt_path), bits) + model = load_quant(path_to_model, str(pt_path), shared.args.gptq_bits) # Multiple GPUs or GPU+CPU if shared.args.gpu_memory: From edbc61139ff5a0ccb2c41a3d8446b231fd31ac5e Mon Sep 17 00:00:00 2001 From: Ayanami Rei Date: Mon, 13 Mar 2023 20:00:38 +0300 Subject: [PATCH 02/17] use new quant loader --- modules/models.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/models.py b/modules/models.py index 7d094ed5..31696795 100644 --- a/modules/models.py +++ b/modules/models.py @@ -1,6 +1,5 @@ import json import os -import sys import time import zipfile from pathlib import Path @@ -35,6 +34,7 @@ if shared.args.deepspeed: ds_config = generate_ds_config(shared.args.bf16, 1 * world_size, shared.args.nvme_offload_dir) dschf = HfDeepSpeedConfig(ds_config) # Keep this object alive for the Transformers integration + def load_model(model_name): print(f"Loading {model_name}...") t0 = time.time() @@ -42,7 +42,7 @@ def load_model(model_name): shared.is_RWKV = model_name.lower().startswith('rwkv-') # Default settings - if not any([shared.args.cpu, shared.args.load_in_8bit, shared.args.load_in_4bit, shared.args.gptq_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 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]): 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) else: @@ -87,11 +87,11 @@ def load_model(model_name): return model, tokenizer - # 4-bit LLaMA - elif shared.args.gptq_bits > 0 or shared.args.load_in_4bit: - from modules.quantized_LLaMA import load_quantized_LLaMA + # Quantized model + elif shared.args.gptq_bits > 0: + from modules.quant_loader import load_quant - model = load_quantized_LLaMA(model_name) + model = load_quant(model_name, shared.args.gptq_model_type) # Custom else: From 1b99ed61bc834a76b1d436fe6e7ad411a46c8385 Mon Sep 17 00:00:00 2001 From: Ayanami Rei Date: Mon, 13 Mar 2023 20:01:34 +0300 Subject: [PATCH 03/17] add argument --gptq-model-type and remove duplicate arguments --- modules/shared.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/shared.py b/modules/shared.py index 5f6c01f3..c74117ab 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -68,8 +68,8 @@ parser.add_argument('--chat', action='store_true', help='Launch the web UI in ch 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.') parser.add_argument('--cpu', action='store_true', help='Use the CPU to generate text.') parser.add_argument('--load-in-8bit', action='store_true', help='Load the model with 8-bit precision.') -parser.add_argument('--load-in-4bit', action='store_true', help='Load the model with 4-bit precision. Currently only works with LLaMA.') -parser.add_argument('--gptq-bits', type=int, default=0, help='Load a pre-quantized model with specified precision. 2, 3, 4 and 8bit are supported. Currently only works with LLaMA.') +parser.add_argument('--gptq-bits', type=int, default=0, help='Load a pre-quantized model with specified precision. 2, 3, 4 and 8bit are supported. Currently only works with LLaMA and OPT.') +parser.add_argument('--gptq-model-type', type=str, default='llama', help='Model type of pre-quantized model. Currently only LLaMa and OPT are supported.') parser.add_argument('--bf16', action='store_true', help='Load the model with bfloat16 precision. Requires NVIDIA Ampere GPU.') parser.add_argument('--auto-devices', action='store_true', help='Automatically split the model across the available GPU(s) and CPU.') 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.') From 3c9afd5ca32d082eb29cab2eb708a0247244195e Mon Sep 17 00:00:00 2001 From: Ayanami Rei Date: Mon, 13 Mar 2023 20:14:40 +0300 Subject: [PATCH 04/17] rename method --- modules/models.py | 4 ++-- modules/quant_loader.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/models.py b/modules/models.py index 31696795..e4dce127 100644 --- a/modules/models.py +++ b/modules/models.py @@ -89,9 +89,9 @@ def load_model(model_name): # Quantized model elif shared.args.gptq_bits > 0: - from modules.quant_loader import load_quant + from modules.quant_loader import load_quantized - model = load_quant(model_name, shared.args.gptq_model_type) + model = load_quantized(model_name, shared.args.gptq_model_type) # Custom else: diff --git a/modules/quant_loader.py b/modules/quant_loader.py index 8bf505a6..a2b484b0 100644 --- a/modules/quant_loader.py +++ b/modules/quant_loader.py @@ -10,7 +10,7 @@ sys.path.insert(0, str(Path("repositories/GPTQ-for-LLaMa"))) # 4-bit LLaMA -def load_quant(model_name, model_type): +def load_quantized(model_name, model_type): if model_type == 'llama': from llama import load_quant elif model_type == 'opt': From b746250b2f3dd543f354c0b8b7db26e607be489a Mon Sep 17 00:00:00 2001 From: Ayanami Rei Date: Mon, 13 Mar 2023 20:18:56 +0300 Subject: [PATCH 05/17] Update README --- README.md | 66 +++++++++++++++++++++++++++---------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 89b567f2..16e07592 100644 --- a/README.md +++ b/README.md @@ -132,39 +132,39 @@ Then browse to Optionally, you can use the following command-line flags: -| Flag | Description | -|-------------|-------------| -| `-h`, `--help` | show this help message and exit | -| `--model MODEL` | Name of the model to load by default. | -| `--notebook` | Launch the web UI in notebook mode, where the output is written to the same text box as the input. | -| `--chat` | Launch the web UI in chat mode.| -| `--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. | -| `--cpu` | Use the CPU to generate text.| -| `--load-in-8bit` | Load the model with 8-bit precision.| -| `--load-in-4bit` | Load the model with 4-bit precision. Currently only works with LLaMA.| -| `--gptq-bits GPTQ_BITS` | Load a pre-quantized model with specified precision. 2, 3, 4 and 8 (bit) are supported. Currently only works with LLaMA. | -| `--bf16` | Load the model with bfloat16 precision. Requires NVIDIA Ampere GPU. | -| `--auto-devices` | Automatically split the model across the available GPU(s) and CPU.| -| `--disk` | If the model is too large for your GPU(s) and CPU combined, send the remaining layers to the disk. | -| `--disk-cache-dir DISK_CACHE_DIR` | Directory to save the disk cache to. Defaults to `cache/`. | -| `--gpu-memory GPU_MEMORY [GPU_MEMORY ...]` | Maxmimum GPU memory in GiB to be allocated per GPU. Example: `--gpu-memory 10` for a single GPU, `--gpu-memory 10 5` for two GPUs. | -| `--cpu-memory CPU_MEMORY` | Maximum CPU memory in GiB to allocate for offloaded weights. Must be an integer number. Defaults to 99.| -| `--flexgen` | Enable the use of FlexGen offloading. | -| `--percent PERCENT [PERCENT ...]` | FlexGen: allocation percentages. Must be 6 numbers separated by spaces (default: 0, 100, 100, 0, 100, 0). | -| `--compress-weight` | FlexGen: Whether to compress weight (default: False).| -| `--pin-weight [PIN_WEIGHT]` | FlexGen: whether to pin weights (setting this to False reduces CPU memory by 20%). | -| `--deepspeed` | Enable the use of DeepSpeed ZeRO-3 for inference via the Transformers integration. | -| `--nvme-offload-dir NVME_OFFLOAD_DIR` | DeepSpeed: Directory to use for ZeRO-3 NVME offloading. | -| `--local_rank LOCAL_RANK` | DeepSpeed: Optional argument for distributed setups. | -| `--rwkv-strategy RWKV_STRATEGY` | RWKV: The strategy to use while loading the model. Examples: "cpu fp32", "cuda fp16", "cuda fp16i8". | -| `--rwkv-cuda-on` | RWKV: Compile the CUDA kernel for better performance. | -| `--no-stream` | Don't stream the text output in real time. This improves the text generation performance.| -| `--settings SETTINGS_FILE` | Load the default interface settings from this json file. See `settings-template.json` for an example. If you create a file called `settings.json`, this file will be loaded by default without the need to use the `--settings` flag.| -| `--extensions EXTENSIONS [EXTENSIONS ...]` | The list of extensions to load. If you want to load more than one extension, write the names separated by spaces. | -| `--listen` | Make the web UI reachable from your local network.| -| `--listen-port LISTEN_PORT` | The listening port that the server will use. | -| `--share` | Create a public URL. This is useful for running the web UI on Google Colab or similar. | -| `--verbose` | Print the prompts to the terminal. | +| Flag | Description | +|--------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `-h`, `--help` | show this help message and exit | +| `--model MODEL` | Name of the model to load by default. | +| `--notebook` | Launch the web UI in notebook mode, where the output is written to the same text box as the input. | +| `--chat` | Launch the web UI in chat mode. | +| `--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. | +| `--cpu` | Use the CPU to generate text. | +| `--load-in-8bit` | Load the model with 8-bit precision. | +| `--gptq-bits GPTQ_BITS` | Load a pre-quantized model with specified precision. 2, 3, 4 and 8 (bit) are supported. Currently only works with LLaMA and OPT. | +| `--gptq-model-type MODEL_TYPE` | Model type of pre-quantized model. Currently only LLaMa and OPT are supported. | +| `--bf16` | Load the model with bfloat16 precision. Requires NVIDIA Ampere GPU. | +| `--auto-devices` | Automatically split the model across the available GPU(s) and CPU. | +| `--disk` | If the model is too large for your GPU(s) and CPU combined, send the remaining layers to the disk. | +| `--disk-cache-dir DISK_CACHE_DIR` | Directory to save the disk cache to. Defaults to `cache/`. | +| `--gpu-memory GPU_MEMORY [GPU_MEMORY ...]` | Maxmimum GPU memory in GiB to be allocated per GPU. Example: `--gpu-memory 10` for a single GPU, `--gpu-memory 10 5` for two GPUs. | +| `--cpu-memory CPU_MEMORY` | Maximum CPU memory in GiB to allocate for offloaded weights. Must be an integer number. Defaults to 99. | +| `--flexgen` | Enable the use of FlexGen offloading. | +| `--percent PERCENT [PERCENT ...]` | FlexGen: allocation percentages. Must be 6 numbers separated by spaces (default: 0, 100, 100, 0, 100, 0). | +| `--compress-weight` | FlexGen: Whether to compress weight (default: False). | +| `--pin-weight [PIN_WEIGHT]` | FlexGen: whether to pin weights (setting this to False reduces CPU memory by 20%). | +| `--deepspeed` | Enable the use of DeepSpeed ZeRO-3 for inference via the Transformers integration. | +| `--nvme-offload-dir NVME_OFFLOAD_DIR` | DeepSpeed: Directory to use for ZeRO-3 NVME offloading. | +| `--local_rank LOCAL_RANK` | DeepSpeed: Optional argument for distributed setups. | +| `--rwkv-strategy RWKV_STRATEGY` | RWKV: The strategy to use while loading the model. Examples: "cpu fp32", "cuda fp16", "cuda fp16i8". | +| `--rwkv-cuda-on` | RWKV: Compile the CUDA kernel for better performance. | +| `--no-stream` | Don't stream the text output in real time. This improves the text generation performance. | +| `--settings SETTINGS_FILE` | Load the default interface settings from this json file. See `settings-template.json` for an example. If you create a file called `settings.json`, this file will be loaded by default without the need to use the `--settings` flag. | +| `--extensions EXTENSIONS [EXTENSIONS ...]` | The list of extensions to load. If you want to load more than one extension, write the names separated by spaces. | +| `--listen` | Make the web UI reachable from your local network. | +| `--listen-port LISTEN_PORT` | The listening port that the server will use. | +| `--share` | Create a public URL. This is useful for running the web UI on Google Colab or similar. | +| `--verbose` | Print the prompts to the terminal. | Out of memory errors? [Check this guide](https://github.com/oobabooga/text-generation-webui/wiki/Low-VRAM-guide). From e1c952c41ce7ce93dde13dc77860b55d80d59be4 Mon Sep 17 00:00:00 2001 From: Ayanami Rei Date: Mon, 13 Mar 2023 20:22:38 +0300 Subject: [PATCH 06/17] make argument non case-sensitive --- modules/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/models.py b/modules/models.py index e4dce127..9bcaca9e 100644 --- a/modules/models.py +++ b/modules/models.py @@ -91,7 +91,7 @@ def load_model(model_name): elif shared.args.gptq_bits > 0: from modules.quant_loader import load_quantized - model = load_quantized(model_name, shared.args.gptq_model_type) + model = load_quantized(model_name, shared.args.gptq_model_type.lower()) # Custom else: From b6c5c57f2ec5e3aa13f51363091db4dcbbc685ef Mon Sep 17 00:00:00 2001 From: Ayanami Rei Date: Mon, 13 Mar 2023 22:11:08 +0300 Subject: [PATCH 07/17] remove default value from argument --- modules/shared.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/shared.py b/modules/shared.py index c74117ab..aeb82806 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -69,7 +69,7 @@ parser.add_argument('--cai-chat', action='store_true', help='Launch the web UI i parser.add_argument('--cpu', action='store_true', help='Use the CPU to generate text.') parser.add_argument('--load-in-8bit', action='store_true', help='Load the model with 8-bit precision.') parser.add_argument('--gptq-bits', type=int, default=0, help='Load a pre-quantized model with specified precision. 2, 3, 4 and 8bit are supported. Currently only works with LLaMA and OPT.') -parser.add_argument('--gptq-model-type', type=str, default='llama', help='Model type of pre-quantized model. Currently only LLaMa and OPT are supported.') +parser.add_argument('--gptq-model-type', type=str, help='Model type of pre-quantized model. Currently only LLaMa and OPT are supported.') parser.add_argument('--bf16', action='store_true', help='Load the model with bfloat16 precision. Requires NVIDIA Ampere GPU.') parser.add_argument('--auto-devices', action='store_true', help='Automatically split the model across the available GPU(s) and CPU.') 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.') From a6a6522b6a910c0bf6faa76191f89543db43eadf Mon Sep 17 00:00:00 2001 From: Ayanami Rei Date: Mon, 13 Mar 2023 22:11:32 +0300 Subject: [PATCH 08/17] determine model type from model name --- modules/quant_loader.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/modules/quant_loader.py b/modules/quant_loader.py index a2b484b0..7a5f8461 100644 --- a/modules/quant_loader.py +++ b/modules/quant_loader.py @@ -9,8 +9,17 @@ import modules.shared as shared sys.path.insert(0, str(Path("repositories/GPTQ-for-LLaMa"))) -# 4-bit LLaMA -def load_quantized(model_name, model_type): +def load_quantized(model_name): + if not shared.args.gptq_model_type: + # Try to determine model type from model name + model_type = model_name.split('-')[0].lower() + if model_type not in ('llama', 'opt'): + print("Can't determine model type from model name. Please specify it manually using --gptq-model-type " + "argument") + exit() + else: + model_type = shared.args.gptq_model_type.lower() + if model_type == 'llama': from llama import load_quant elif model_type == 'opt': @@ -20,7 +29,16 @@ def load_quantized(model_name, model_type): exit() path_to_model = Path(f'models/{model_name}') - pt_model = f'{model_name}-{shared.args.gptq_bits}bit.pt' + if path_to_model.name.lower().startswith('llama-7b'): + pt_model = f'llama-7b-{shared.args.gptq_bits}bit.pt' + elif path_to_model.name.lower().startswith('llama-13b'): + pt_model = f'llama-13b-{shared.args.gptq_bits}bit.pt' + elif path_to_model.name.lower().startswith('llama-30b'): + pt_model = f'llama-30b-{shared.args.gptq_bits}bit.pt' + elif path_to_model.name.lower().startswith('llama-65b'): + pt_model = f'llama-65b-{shared.args.gptq_bits}bit.pt' + else: + pt_model = f'{model_name}-{shared.args.gptq_bits}bit.pt' # Try to find the .pt both in models/ and in the subfolder pt_path = None From 8778b756e69144ff91180076068eeb9bcd915a60 Mon Sep 17 00:00:00 2001 From: Ayanami Rei Date: Mon, 13 Mar 2023 22:11:40 +0300 Subject: [PATCH 09/17] use updated load_quantized --- modules/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/models.py b/modules/models.py index 9bcaca9e..46cd77ff 100644 --- a/modules/models.py +++ b/modules/models.py @@ -91,7 +91,7 @@ def load_model(model_name): elif shared.args.gptq_bits > 0: from modules.quant_loader import load_quantized - model = load_quantized(model_name, shared.args.gptq_model_type.lower()) + model = load_quantized(model_name) # Custom else: From 518e5c4244b1d373d616ab32215b2f1c195deae8 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Mon, 13 Mar 2023 16:45:08 -0300 Subject: [PATCH 10/17] Some minor fixes to the GPTQ loader --- modules/quant_loader.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/modules/quant_loader.py b/modules/quant_loader.py index 7a5f8461..c2723490 100644 --- a/modules/quant_loader.py +++ b/modules/quant_loader.py @@ -7,6 +7,8 @@ import torch import modules.shared as shared sys.path.insert(0, str(Path("repositories/GPTQ-for-LLaMa"))) +import llama +import opt def load_quantized(model_name): @@ -21,9 +23,9 @@ def load_quantized(model_name): model_type = shared.args.gptq_model_type.lower() if model_type == 'llama': - from llama import load_quant + load_quant = llama.load_quant elif model_type == 'opt': - from opt import load_quant + load_quant = opt.load_quant else: print("Unknown pre-quantized model type specified. Only 'llama' and 'opt' are supported") exit() @@ -50,7 +52,7 @@ def load_quantized(model_name): print(f"Could not find {pt_model}, exiting...") exit() - model = load_quant(path_to_model, str(pt_path), shared.args.gptq_bits) + model = load_quant(str(path_to_model), str(pt_path), shared.args.gptq_bits) # Multiple GPUs or GPU+CPU if shared.args.gpu_memory: From 265ba384b7e5e928d97d2749b25771b7d3d93fde Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Tue, 14 Mar 2023 07:56:31 -0300 Subject: [PATCH 11/17] Rename a file, add deprecation warning for --load-in-4bit --- modules/{quant_loader.py => GPTQ_loader.py} | 0 modules/models.py | 2 +- modules/shared.py | 6 ++++++ 3 files changed, 7 insertions(+), 1 deletion(-) rename modules/{quant_loader.py => GPTQ_loader.py} (100%) diff --git a/modules/quant_loader.py b/modules/GPTQ_loader.py similarity index 100% rename from modules/quant_loader.py rename to modules/GPTQ_loader.py diff --git a/modules/models.py b/modules/models.py index 46cd77ff..f4bb11fd 100644 --- a/modules/models.py +++ b/modules/models.py @@ -89,7 +89,7 @@ def load_model(model_name): # Quantized model elif shared.args.gptq_bits > 0: - from modules.quant_loader import load_quantized + from modules.GPTQ_loader import load_quantized model = load_quantized(model_name) diff --git a/modules/shared.py b/modules/shared.py index 3abdc551..ea2eb50b 100644 --- a/modules/shared.py +++ b/modules/shared.py @@ -69,6 +69,7 @@ parser.add_argument('--chat', action='store_true', help='Launch the web UI in ch 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.') parser.add_argument('--cpu', action='store_true', help='Use the CPU to generate text.') parser.add_argument('--load-in-8bit', action='store_true', help='Load the model with 8-bit precision.') +parser.add_argument('--load-in-4bit', action='store_true', help='DEPRECATED: use --gptq-bits 4 instead.') parser.add_argument('--gptq-bits', type=int, default=0, help='Load a pre-quantized model with specified precision. 2, 3, 4 and 8bit are supported. Currently only works with LLaMA and OPT.') parser.add_argument('--gptq-model-type', type=str, help='Model type of pre-quantized model. Currently only LLaMa and OPT are supported.') parser.add_argument('--bf16', action='store_true', help='Load the model with bfloat16 precision. Requires NVIDIA Ampere GPU.') @@ -95,3 +96,8 @@ parser.add_argument('--share', action='store_true', help='Create a public URL. T parser.add_argument('--auto-launch', action='store_true', default=False, help='Open the web UI in the default browser upon launch.') parser.add_argument('--verbose', action='store_true', help='Print the prompts to the terminal.') args = parser.parse_args() + +# Provisional, this will be deleted later +if args.load_in_4bit: + print("Warning: --load-in-4bit is deprecated and will be removed. Use --gptq-bits 4 instead.\n") + args.gptq_bits = 4 From 87192e2813181ca4241c97bd258e19d52ff6f204 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Tue, 14 Mar 2023 08:02:21 -0300 Subject: [PATCH 12/17] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f66126d8..1c267739 100644 --- a/README.md +++ b/README.md @@ -140,7 +140,7 @@ Optionally, you can use the following command-line flags: | `--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. | | `--cpu` | Use the CPU to generate text.| | `--load-in-8bit` | Load the model with 8-bit precision.| -| `--load-in-4bit` | Load the model with 4-bit precision. Currently only works with LLaMA.| +| `--load-in-4bit` | DEPRECATED: use `--gptq-bits 4` instead. | | `--gptq-bits GPTQ_BITS` | Load a pre-quantized model with specified precision. 2, 3, 4 and 8 (bit) are supported. Currently only works with LLaMA and OPT. | | `--gptq-model-type MODEL_TYPE` | Model type of pre-quantized model. Currently only LLaMa and OPT are supported. | | `--bf16` | Load the model with bfloat16 precision. Requires NVIDIA Ampere GPU. | From afc5339510c71cb6a17a7e6ba9bc432545f03b83 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Tue, 14 Mar 2023 16:04:17 -0300 Subject: [PATCH 13/17] Remove "eval" statements from text generation functions --- modules/text_generation.py | 65 ++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/modules/text_generation.py b/modules/text_generation.py index d64481b2..70a51d91 100644 --- a/modules/text_generation.py +++ b/modules/text_generation.py @@ -122,7 +122,7 @@ def generate_reply(question, max_new_tokens, do_sample, temperature, top_p, typi input_ids = encode(question, max_new_tokens) original_input_ids = input_ids output = input_ids[0] - cuda = "" if (shared.args.cpu or shared.args.deepspeed or shared.args.flexgen) else ".cuda()" + cuda = not any((shared.args.cpu, shared.args.deepspeed, shared.args.flexgen)) eos_token_ids = [shared.tokenizer.eos_token_id] if shared.tokenizer.eos_token_id is not None else [] if eos_token is not None: eos_token_ids.append(int(encode(eos_token)[0][-1])) @@ -132,45 +132,48 @@ def generate_reply(question, max_new_tokens, do_sample, temperature, top_p, typi t = encode(stopping_string, 0, add_special_tokens=False) stopping_criteria_list.append(_SentinelTokenStoppingCriteria(sentinel_token_ids=t, starting_idx=len(input_ids[0]))) + generate_params = {} if not shared.args.flexgen: - generate_params = [ - f"max_new_tokens=max_new_tokens", - f"eos_token_id={eos_token_ids}", - f"stopping_criteria=stopping_criteria_list", - f"do_sample={do_sample}", - f"temperature={temperature}", - f"top_p={top_p}", - f"typical_p={typical_p}", - f"repetition_penalty={repetition_penalty}", - f"top_k={top_k}", - f"min_length={min_length if shared.args.no_stream else 0}", - f"no_repeat_ngram_size={no_repeat_ngram_size}", - f"num_beams={num_beams}", - f"penalty_alpha={penalty_alpha}", - f"length_penalty={length_penalty}", - f"early_stopping={early_stopping}", - ] + generate_params.update({ + "max_new_tokens": max_new_tokens, + "eos_token_id": eos_token_ids, + "stopping_criteria": stopping_criteria_list, + "do_sample": do_sample, + "temperature": temperature, + "top_p": top_p, + "typical_p": typical_p, + "repetition_penalty": repetition_penalty, + "top_k": top_k, + "min_length": min_length if shared.args.no_stream else 0, + "no_repeat_ngram_size": no_repeat_ngram_size, + "num_beams": num_beams, + "penalty_alpha": penalty_alpha, + "length_penalty": length_penalty, + "early_stopping": early_stopping, + }) else: - generate_params = [ - f"max_new_tokens={max_new_tokens if shared.args.no_stream else 8}", - f"do_sample={do_sample}", - f"temperature={temperature}", - f"stop={eos_token_ids[-1]}", - ] + generate_params.update({ + "max_new_tokens": max_new_tokens if shared.args.no_stream else 8, + "do_sample": do_sample, + "temperature": temperature, + "stop": eos_token_ids[-1], + }) if shared.args.deepspeed: - generate_params.append("synced_gpus=True") + generate_params.update({"synced_gpus": True}) if shared.soft_prompt: inputs_embeds, filler_input_ids = generate_softprompt_input_tensors(input_ids) - generate_params.insert(0, "inputs_embeds=inputs_embeds") - generate_params.insert(0, "inputs=filler_input_ids") + generate_params.update({"inputs_embeds": inputs_embeds}) + generate_params.update({"inputs": filler_input_ids}) else: - generate_params.insert(0, "inputs=input_ids") + generate_params.update({"inputs": input_ids}) try: # Generate the entire reply at once. if shared.args.no_stream: with torch.no_grad(): - output = eval(f"shared.model.generate({', '.join(generate_params)}){cuda}")[0] + output = shared.model.generate(**generate_params)[0] + if cuda: + output = output.cuda() if shared.soft_prompt: output = torch.cat((input_ids[0], output[filler_input_ids.shape[1]:])) @@ -194,7 +197,7 @@ def generate_reply(question, max_new_tokens, do_sample, temperature, top_p, typi return Iteratorize(generate_with_callback, kwargs, callback=None) yield formatted_outputs(original_question, shared.model_name) - with eval(f"generate_with_streaming({', '.join(generate_params)})") as generator: + with generate_with_streaming(**generate_params) as generator: for output in generator: if shared.soft_prompt: output = torch.cat((input_ids[0], output[filler_input_ids.shape[1]:])) @@ -214,7 +217,7 @@ def generate_reply(question, max_new_tokens, do_sample, temperature, top_p, typi for i in range(max_new_tokens//8+1): clear_torch_cache() with torch.no_grad(): - output = eval(f"shared.model.generate({', '.join(generate_params)})")[0] + output = shared.model.generate(**generate_params)[0] if shared.soft_prompt: output = torch.cat((input_ids[0], output[filler_input_ids.shape[1]:])) reply = decode(output) From 72d207c0980232db287f9ce89ec4dd3b032465e5 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Tue, 14 Mar 2023 16:31:27 -0300 Subject: [PATCH 14/17] Remove the chat API It is not implemented, has not been tested, and this is causing confusion. --- server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.py b/server.py index 08b1a478..a54e3b62 100644 --- a/server.py +++ b/server.py @@ -269,7 +269,7 @@ if shared.args.chat or shared.args.cai_chat: function_call = 'chat.cai_chatbot_wrapper' if shared.args.cai_chat else 'chat.chatbot_wrapper' - gen_events.append(shared.gradio['Generate'].click(eval(function_call), shared.input_params, shared.gradio['display'], show_progress=shared.args.no_stream, api_name='textgen')) + gen_events.append(shared.gradio['Generate'].click(eval(function_call), shared.input_params, shared.gradio['display'], show_progress=shared.args.no_stream)) gen_events.append(shared.gradio['textbox'].submit(eval(function_call), shared.input_params, shared.gradio['display'], show_progress=shared.args.no_stream)) gen_events.append(shared.gradio['Regenerate'].click(chat.regenerate_wrapper, shared.input_params, shared.gradio['display'], show_progress=shared.args.no_stream)) gen_events.append(shared.gradio['Impersonate'].click(chat.impersonate_wrapper, shared.input_params, shared.gradio['textbox'], show_progress=shared.args.no_stream)) From b419dffba3592792dd6292b7d6f4943850fd6195 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Tue, 14 Mar 2023 17:55:35 -0300 Subject: [PATCH 15/17] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1c267739..141d549d 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,9 @@ pip3 install torch torchvision torchaudio --extra-index-url https://download.pyt conda install pytorch torchvision torchaudio git -c pytorch ``` -See also: [Installation instructions for human beings](https://github.com/oobabooga/text-generation-webui/wiki/Installation-instructions-for-human-beings). +> **Note** +> 1. If you are on Windows, it may be easier to run the commands above in an WSL environment. The performance may also be better. +> 2. For more detailed, user-contributed guide, see: [Installation instructions for human beings](https://github.com/oobabooga/text-generation-webui/wiki/Installation-instructions-for-human-beings). ## Installation option 2: one-click installers From 1236c7f97153b1de6905ed46a4293d3e625f1894 Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Tue, 14 Mar 2023 17:56:15 -0300 Subject: [PATCH 16/17] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 141d549d..2bfbd2f8 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ conda install pytorch torchvision torchaudio git -c pytorch ``` > **Note** -> 1. If you are on Windows, it may be easier to run the commands above in an WSL environment. The performance may also be better. +> 1. If you are on Windows, it may be easier to run the commands above in a WSL environment. The performance may also be better. > 2. For more detailed, user-contributed guide, see: [Installation instructions for human beings](https://github.com/oobabooga/text-generation-webui/wiki/Installation-instructions-for-human-beings). ## Installation option 2: one-click installers From 128d18e2988c5f42f95476cc09c89aff9741e44a Mon Sep 17 00:00:00 2001 From: oobabooga <112222186+oobabooga@users.noreply.github.com> Date: Tue, 14 Mar 2023 17:57:25 -0300 Subject: [PATCH 17/17] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2bfbd2f8..c9834558 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ conda install pytorch torchvision torchaudio git -c pytorch > **Note** > 1. If you are on Windows, it may be easier to run the commands above in a WSL environment. The performance may also be better. -> 2. For more detailed, user-contributed guide, see: [Installation instructions for human beings](https://github.com/oobabooga/text-generation-webui/wiki/Installation-instructions-for-human-beings). +> 2. For a more detailed, user-contributed guide, see: [Installation instructions for human beings](https://github.com/oobabooga/text-generation-webui/wiki/Installation-instructions-for-human-beings). ## Installation option 2: one-click installers