File size: 4,508 Bytes
49d95b0
 
5ee71b6
 
49d95b0
 
 
 
 
 
 
 
 
 
80be5fa
 
49d95b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bf2205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
license: apache-2.0
tags:
- moe
train: false
inference: false
pipeline_tag: text-generation
---
## Mixtral-8x7B-Instruct-v0.1-hf-attn-4bit-moe-2bit-HQQ
This is a version of the Mixtral-8x7B-Instruct-v0.1 model (https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1) quantized with a mix of 4-bit and 2-bit via Half-Quadratic Quantization (HQQ).

More specifically, the attention layers are quantized to 4-bit and the experts are quantized to 2-bit. 
This model should perform a lot better compared to the all 2-bit model for a slight increase in model size (18.2GB vs. 18GB).

This idea was suggest by Artem Eliseev (@lavawolfiee) and Denis Mazur (@dvmazur) [in this Github discussion](https://github.com/mobiusml/hqq/issues/2).

### Basic Usage
To run the model, install the HQQ library from https://github.com/mobiusml/hqq and use it as follows:
``` Python
model_id = 'mobiuslabsgmbh/Mixtral-8x7B-Instruct-v0.1-hf-attn-4bit-moe-2bit-HQQ'
#Load the model
from hqq.engine.hf import HQQModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(model_id)
model     = HQQModelForCausalLM.from_quantized(model_id)
#Optional
from hqq.core.quantize import *
HQQLinear.set_backend(HQQBackend.PYTORCH_COMPILE) 
#Text Generation
prompt = "<s> [INST] How do I build a car? [/INST] "
inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
outputs = model.generate(**(inputs.to('cuda')), max_new_tokens=1000)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
```
<b>Output</b>:
<p>

 1. Design the Car:
   * Determine the type of car you want to build (e.g., sedan, SUV, sports car) and its specifications (e.g., size, weight, horsepower, fuel efficiency).
   * Create detailed sketches and 3D models of the car's exterior and interior.
   * Design the car's technical components, including the engine, transmission, brakes, and suspension system.

2. Acquire Necessary Materials and Parts:
   * Purchase or manufacture the necessary materials, such as steel, aluminum, and plastics.
   * Obtain or manufacture the required parts, such as the engine, transmission, brakes, suspension system, and electrical components.

3. Set Up a Production Facility:
   * Establish a manufacturing facility with the necessary equipment, such as assembly lines, paint booths, and welding machines.
   * Hire a skilled workforce to oversee production and ensure quality control.

4. Manufacture the Car:
   * Follow the design specifications to assemble the car's components.
   * Perform rigorous testing to ensure the car meets safety and performance standards.

5. Market and Sell the Car:
   * Develop a marketing strategy to promote the car to potential buyers.
   * Establish a distribution network to sell the car through dealerships or online platforms.

6. Provide After-Sales Support:
   * Offer maintenance and repair services to ensure customer satisfaction and loyalty.
   * Continuously improve the car's design and performance based on customer feedback and market trends.

Please note, building a car requires significant expertise, resources, and adherence to strict safety and regulatory standards. It is not a project that can be undertaken without extensive knowledge and experience in automotive engineering, manufacturing, and business management.

----------------------------------------------------------------------------------------------------------------------------------
</p>

### Quantization
You can reproduce the model using the following quant configs:

``` Python
from hqq.engine.hf import HQQModelForCausalLM, AutoTokenizer
model_id  = "mistralai/Mixtral-8x7B-Instruct-v0.1"
model     = HQQModelForCausalLM.from_pretrained(model_id, use_auth_token=hf_auth, cache_dir=cache_path)

#Quantize params
from hqq.core.quantize import *
attn_prams     = BaseQuantizeConfig(nbits=4, group_size=64, quant_zero=True, quant_scale=True) 
attn_prams['scale_quant_params']['group_size'] = 256
experts_params = BaseQuantizeConfig(nbits=2, group_size=16, quant_zero=True, quant_scale=True) 

quant_config = {}
#Attention
quant_config['self_attn.q_proj'] = attn_prams
quant_config['self_attn.k_proj'] = attn_prams
quant_config['self_attn.v_proj'] = attn_prams
quant_config['self_attn.o_proj'] = attn_prams
#Experts
quant_config['block_sparse_moe.experts.w1'] = experts_params
quant_config['block_sparse_moe.experts.w2'] = experts_params
quant_config['block_sparse_moe.experts.w3'] = experts_params

#Quantize
model.quantize_model(quant_config=quant_config)
```