Skip to content

PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度

update readme of calibration dataset and lm-head usage (#98)

wenhuach21f95b8c7d0cf7
diff --git a/examples/language-modeling/README.md b/examples/language-modeling/README.md
index 79d1cba4a..df370963f 100644
--- a/examples/language-modeling/README.md
+++ b/examples/language-modeling/README.md
@@ -43,10 +43,11 @@ The [NeelNanda/pile-10k](https://huggingface.co/datasets/NeelNanda/pile-10k) in
See more about loading [huggingface dataset](https://huggingface.co/docs/datasets/main/en/quickstart)
### Customized Dataset
-- Following the [code](./main_customized_data.py) to pass list of string or list of inputs to dataloader.
-
-- Register your dataset/dataloader following the [code](../../auto_round/calib_dataset.py) and pass the new dataset&split args to initialize AutoRound object.
+- Option 1: Pass a local json file path to dataset argument
+- Option 2: Register your dataset following the [code](../../auto_round/calib_dataset.py) and pass the new dataset&split args to initialize AutoRound object.
+- Option 3: Following the [code](./main_customized_data.py) to pass list of string or list of inputs to dataloader.
+Combination of different datasets has been supported, --dataset "./tmp.json,NeelNanda/pile-10k". Please note that samples with sequence length < args.seq will be dropped.
<br />
@@ -60,10 +61,18 @@ pip install -r requirements.txt
```bash
CUDA_VISIBLE_DEVICES=0 python3 main.py --model_name facebook/opt-125m --bits 4 --group_size -1
```
-- **Reduced GPU Memory Usage and Adjusted Training Batch Size:**
+- **Reduced GPU Memory Usage:**
```bash
CUDA_VISIBLE_DEVICES=0 python3 main.py --model_name facebook/opt-125m --bits 4 --group_size -1 --train_bs 1 --gradient_accumulate_steps 8
```
+
+- **Enable quantized lm-head:**
+
+--disable_low_gpu_mem_usage is strongly recommended if the whole model could be loaded to the device, otherwise it will be quite slow to cache the inputs of lm-head. Another way is reducing n_samples,e.g. 128, to alleviate the issue.
+```bash
+CUDA_VISIBLE_DEVICES=0 python3 main.py --model_name facebook/opt-125m --bits 4 --group_size -1 --quant_lm_head --disable_low_gpu_mem_usage
+```
+
- **Utilizing the AdamW Optimizer:**
Include the flag `--adam`. Note that AdamW is less effective than sign gradient descent in many scenarios we tested.
@@ -73,7 +82,6 @@ Include the flag `--adam`. Note that AdamW is less effective than sign gradient
CUDA_VISIBLE_DEVICES=0 python3 main.py --model_name facebook/opt-125m --bits 4 --group_size -1 --iters 400 --lr 0.0025 --disable_minmax_tuning --disable_quanted_input
```
-
- **Running on Intel Gaudi2**
```bash
bash run_autoround_on_gaudi.sh
diff --git a/examples/language-modeling/eval_legacy/evaluation.py b/examples/language-modeling/eval_legacy/evaluation.py
index bc58d4533..1bb2593e6 100644
--- a/examples/language-modeling/eval_legacy/evaluation.py
+++ b/examples/language-modeling/eval_legacy/evaluation.py
@@ -11,7 +11,7 @@ import transformers
if __name__ == "__main__":
import sys
sys.path.insert(0, './')
-from .parse_results import result_parser
+from eval_legacy.parse_results import result_parser
import time
EXT_TASKS = ['wikitext2', 'ptb', 'c4', 'ptb-new', 'c4-new']
fewshots_dict = {}
@@ -345,6 +345,9 @@ if __name__ == "__main__":
test_tasks = ['wikitext2', 'ptb-new', 'c4-new', 'lambada_openai', 'hellaswag', 'winogrande', 'piqa',
"hendrycksTest-*", "wikitext", "truthfulqa_mc", "openbookqa", "boolq", "rte", "arc_easy",
"arc_challenge"]
+ seen = set()
+ tmp_tasks = test_tasks
+ test_tasks = [x for x in tmp_tasks if not (x in seen or seen.add(x))]
model_name = args.model_name.rstrip('/')
excel_name = model_name.split('/')[-1] + ".xlsx"
eval_model(model_path=args.model_name,
diff --git a/examples/language-modeling/main.py b/examples/language-modeling/main.py
index c26aeb971..3859c65da 100644
--- a/examples/language-modeling/main.py
+++ b/examples/language-modeling/main.py
@@ -269,6 +269,9 @@ if __name__ == '__main__':
f"{n} will not be quantized due to its shape not being divisible by 32, resulting in an exporting issue to autogptq")
if args.quant_lm_head:
weight_config['lm_head'] = {"data_type": "int"}
+ if not args.disable_low_gpu_mem_usage:
+ print(f"warning, disable_low_gpu_mem_usage is strongly recommended if the whole model could be loaded to "
+ f"gpu")
autoround = round(model, tokenizer, args.bits, args.group_size, sym=args.sym, batch_size=args.train_bs,
dataset=args.dataset, seqlen=seqlen, n_blocks=args.n_blocks, iters=args.iters, lr=args.lr,
minmax_lr=args.minmax_lr, enable_quanted_input=not args.disable_quanted_input, device=device_str,