File size: 6,712 Bytes
e431807
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
## Model Details

This model is an int4 model with group_size 128 of  [THUDM/glm-4-9b-chat](https://huggingface.co/THUDM/glm-4-9b-chat) generated by [intel/auto-round](https://github.com/intel/auto-round). For GPTQ format, please load the model with revision `d1bb095`

## How To Use

### INT4 Inference on CPU/CUDA/HPU

```python
from auto_round import AutoRoundConfig ## must import for auto-round format
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

quantized_model_dir = "OPEA/glm-4-9b-chat-int4-inc"

model = AutoModelForCausalLM.from_pretrained(quantized_model_dir,
                                             device_map=backend.split(':')[0],                                                                                                torch_dtype=torch.float16,
                                             quantization_config=quantization_config,
                                             trust_remote_code=True
                                             #revision=d1bb095, ##auto-gptq format
                                            ).eval()

tokenizer = AutoTokenizer.from_pretrained(quantized_model_dir, trust_remote_code=True)
query = "请介绍一下智谱华章科技有限公司"
inputs = tokenizer.apply_chat_template([{"role": "user", "content": query}],
                                       add_generation_prompt=True,
                                       tokenize=True,
                                       return_tensors="pt",
                                       return_dict=True
                                       )
inputs = inputs.to(model.device)

gen_kwargs = {"max_length": 50, "do_sample": False, "top_k": 1}##change this to follow official usage
with torch.no_grad():
    outputs = model.generate(**inputs, **gen_kwargs)
    outputs = outputs[:, inputs['input_ids'].shape[1]:]
    print(tokenizer.decode(outputs[0], skip_special_tokens=True))
    
##请介绍一下智谱华章科技有限公司
#智谱华章科技有限公司是一家专注于人工智能、大数据、云计算等前沿技术领域的创新型企业。公司成立于2016年,总部位于中国北京,是一家集技术研发、产品开发、


##9.8大还是9.11大
##9.8比9.11小。在数值上,9.8小于9.11。


##Once upon a time,

#In a land where the sun kissed the horizon with a golden glow and the stars whispered secrets to the night, there was a village nestled among rolling hills and whispering forests. This was a place


##There is a girl who likes adventure,
##That's quite the intriguing starting point! If you're looking to create a story or a character, here's a brief introduction to a girl who likes adventure:

##---

##**Name:**
```



### Evaluate the model 

pip3 install lm-eval==0.4.5

pip3 install langdetect,immutabledict,antlr4-python3-runtime==4.11

```bash
auto-round --eval --eval_bs 16 --tasks leaderboard_ifeval,leaderboard_mmlu_pro,gsm8k,lambada_openai,hellaswag,piqa,winogrande,truthfulqa_mc1,openbookqa,boolq,arc_easy,arc_challenge,cmmlu,ceval-valid

```

| Metric                                     | BF16   | INT4(6.4G) | INT4-quanted-lm-head(5.5G) |
| ------------------------------------------ | ------ | ---------- | -------------------------- |
| Avg                                        | 0.6260 | 0.6230     | 0.6204                     |
| leaderboard_mmlu_pro 5shot                 | 0.3678 | 0.3616     | 0.3610                     |
| leaderboard_ifeval inst_level_strict_acc   | 0.5504 | 0.5600     | 0.5588                     |
| leaderboard_ifeval prompt_level_strict_acc | 0.4067 | 0.4233     | 0.4067                     |
| cmmlu                                      | 0.7213 | 0.7137     | 0.7086                     |
| ceval-valid                                | 0.7065 | 0.7058     | 0.6909                     |
| gsm8k 5shot strict match                   | 0.7794 | 0.7597     | 0.7589                     |
| lambada_openai                             | 0.6608 | 0.6493     | 0.6470                     |
| hellaswag                                  | 0.6195 | 0.6137     | 0.6134                     |
| winogrande                                 | 0.7561 | 0.7545     | 0.7522                     |
| piqa                                       | 0.8030 | 0.7976     | 0.8003                     |
| truthfulqa_mc1                             | 0.4223 | 0.4223     | 0.4284                     |
| openbookqa                                 | 0.3560 | 0.3640     | 0.3580                     |
| boolq                                      | 0.8691 | 0.8606     | 0.8578                     |
| arc_easy                                   | 0.8241 | 0.8249     | 0.8203                     |
| arc_challenge                              | 0.5469 | 0.5341     | 0.5444                     |



### Generate the model

Here is the sample command to generate the model.  

```bash
auto-round \
--model THUDM/glm-4-9b-chat \
--iter 1000 \
--nsamples 512 \
--disable_eval \
--format "auto_round,auto_gptq"
--output_dir "./tmp_autoround" 
```

copy all the *.py file to the quantized_model folder

For gptq format, need to add "block_name_to_quantize":"transformer.encoder.layers" to config.json, we only tested it on transformers==4.46.1

## Ethical Considerations and Limitations

The model can produce factually incorrect output, and should not be relied on to produce factually accurate information. Because of the limitations of the pretrained model and the finetuning datasets, it is possible that this model could generate lewd, biased or otherwise offensive outputs.

Therefore, before deploying any applications of the model, developers should perform safety testing.

## Caveats and Recommendations

Users (both direct and downstream) should be made aware of the risks, biases and limitations of the model.

Here are a couple of useful links to learn more about Intel's AI software:

* Intel Neural Compressor [link](https://github.com/intel/neural-compressor)
* Intel Extension for Transformers [link](https://github.com/intel/intel-extension-for-transformers)

## Disclaimer

The license on this model does not constitute legal advice. We are not responsible for the actions of third parties who use this model. Please consult an attorney before using this model for commercial purposes.



## Cite

@article{cheng2023optimize, title={Optimize weight rounding via signed gradient descent for the quantization of llms}, author={Cheng, Wenhua and Zhang, Weiwei and Shen, Haihao and Cai, Yiyang and He, Xin and Lv, Kaokao and Liu, Yi}, journal={arXiv preprint arXiv:2309.05516}, year={2023} }

[arxiv](https://arxiv.org/abs/2309.05516) [github](https://github.com/intel/auto-round)