-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathtest_intX_quantization.py
More file actions
419 lines (344 loc) · 16.5 KB
/
test_intX_quantization.py
File metadata and controls
419 lines (344 loc) · 16.5 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0
# DeepSpeed Team
import numpy as np
import torch
import torch.nn as nn
from unit.common import DistributedTest
from deepspeed.accelerator import get_accelerator
from deepspeed.inference.quantization.quantization import _init_group_wise_weight_quantization
from deepspeed.inference.quantization.utils import Quantizer, DeQuantizer
from deepspeed.inference.quantization.layers import QuantizedLinear
from deepspeed.utils.torch import required_torch_version
from transformers.models.opt.modeling_opt import OPTDecoderLayer
from transformers import AutoConfig, OPTConfig, AutoModel
import pytest
from collections import OrderedDict
from typing import Dict
from deepspeed.ops.aio import AsyncIOBuilder
device = get_accelerator().device_name() if get_accelerator().is_available() else 'cpu'
if not required_torch_version(min_version=1.11):
pytest.skip("torch.Tensor.bitwise_left_shift in INT4 quantizer needs torch 1.11 or above.",
allow_module_level=True)
def reset_random(seed=1234):
np.random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
get_accelerator().manual_seed_all(seed)
def quantization_test_helper(pre_quant_type: torch.dtype, num_bits: int):
reset_random()
num_group = 1024 * 32
group_size = 64
quantization_config = {'num_bits': num_bits, 'group_size': group_size, 'group_dim': 1, 'symmetric': False}
quantizer = Quantizer(config=quantization_config)
dequantizer = DeQuantizer(config=quantization_config, dtype=pre_quant_type)
data = torch.randn(num_group, group_size, dtype=pre_quant_type, device=device)
quantized_data, scale_buf, min_vals = quantizer.quantize(data)
dequantized_data = dequantizer.dequantize(quantized_data, scale_buf, min_vals)
max_diff = torch.max(torch.abs(data - dequantized_data))
mean_diff = torch.mean(torch.abs(data - dequantized_data))
# This threshold value is emperically selected.
assert mean_diff < 0.15 and max_diff < 0.5, f'Numeric error exceed threshold, mean diff {mean_diff} (threshold 0.15), max diff {max_diff} (threshold 0.5)'
def zero3_post_init_quantization_test_helper(cpu_offload: bool, nvme_offload: bool, bits: int, nvme_path=None):
import deepspeed
from transformers.integrations.deepspeed import HfDeepSpeedConfig
if nvme_offload and not deepspeed.ops.__compatible_ops__[AsyncIOBuilder.NAME]:
pytest.skip('Skip tests since async-io is not compatible')
def get_zero3_ds_config(hf_config: OPTConfig, cpu_offload: bool, nvme_offload: bool, bits: int) -> Dict:
GB = 1 << 30
ds_config = {
"fp16": {
"enabled": True,
},
"zero_optimization": {
"stage": 3,
"stage3_prefetch_bucket_size": 2 * hf_config.hidden_size * hf_config.hidden_size,
"stage3_param_persistence_threshold": hf_config.hidden_size,
"stage3_max_live_parameters": 2 * hf_config.hidden_size * hf_config.hidden_size
},
"steps_per_print": 2000,
"train_micro_batch_size_per_gpu": 1,
"wall_clock_breakdown": False,
'weight_quantization': {
'post_init_quant': {
'fc': {
'num_bits': bits,
'group_size': 32,
'group_dim': 1,
'symmetric': False
},
'self_attn.q_proj': {
'num_bits': bits,
'group_size': 32,
'group_dim': 1,
'symmetric': False
},
'self_attn.k_proj': {
'num_bits': bits,
'group_size': 32,
'group_dim': 1,
'symmetric': False
},
'self_attn.v_proj': {
'num_bits': bits,
'group_size': 32,
'group_dim': 1,
'symmetric': False
},
'self_attn.out_proj': {
'num_bits': bits,
'group_size': 32,
'group_dim': 1,
'symmetric': False
},
'lm_head': {
'num_bits': bits,
'group_size': 32,
'group_dim': 1,
'symmetric': False
},
'embed_tokens': {
'num_bits': bits,
'group_size': 32,
'group_dim': 1,
'symmetric': False
},
}
}
}
if cpu_offload:
ds_config["zero_optimization"]["offload_param"] = dict(device="cpu", pin_memory=1)
if nvme_offload:
ds_config["zero_optimization"]["offload_param"] = dict(
device="nvme",
pin_memory=True,
nvme_path=nvme_path or '~/tmp_offload_dir',
buffer_count=5,
buffer_size=1 * GB,
)
ds_config["aio"] = {
"block_size": 1048576,
"queue_depth": 8,
"thread_count": 1,
"single_submit": False,
"overlap_events": True,
}
return ds_config
hf_config = AutoConfig.from_pretrained('facebook/opt-125m')
ds_config = get_zero3_ds_config(hf_config=hf_config, cpu_offload=cpu_offload, nvme_offload=nvme_offload, bits=bits)
input_ids = torch.ones(1, 16, dtype=torch.int32, device=device)
attention_mask = torch.ones(1, 16, dtype=torch.float32, device=device)
with torch.no_grad():
ref_model = AutoModel.from_pretrained('facebook/opt-125m', torch_dtype=torch.float16).to(device)
ref_model.eval()
ref_output = ref_model(input_ids=input_ids, attention_mask=attention_mask)
with torch.no_grad():
dschf = HfDeepSpeedConfig(ds_config)
model = AutoModel.from_pretrained('facebook/opt-125m', torch_dtype=torch.float16)
model = model.eval()
model = _init_group_wise_weight_quantization(model, ds_config)
ds_engine = deepspeed.initialize(model=model, config_params=ds_config)[0]
ds_engine.module.eval()
model = ds_engine.module
output = model(input_ids=input_ids, attention_mask=attention_mask)
mean_diff = torch.mean(torch.abs(output.last_hidden_state - ref_output.last_hidden_state))
# This threshold value is emperically selected.
assert mean_diff < 0.4, f'Numeric error exceed threshold, relative error {mean_diff} (threshold 0.4)'
def zero3_quantized_initialization_test_helper(cpu_offload: bool, nvme_offload: bool, bits: int, nvme_path=None):
import deepspeed
from transformers.integrations.deepspeed import HfDeepSpeedConfig
if nvme_offload and not deepspeed.ops.__compatible_ops__[AsyncIOBuilder.NAME]:
pytest.skip('Skip tests since async-io is not compatible')
def get_zero3_ds_config(hf_config: OPTConfig, cpu_offload: bool, nvme_offload: bool, bits: int) -> Dict:
GB = 1 << 30
ds_config = {
"fp16": {
"enabled": True,
},
"zero_optimization": {
"stage": 3,
"stage3_prefetch_bucket_size": 2 * hf_config.hidden_size * hf_config.hidden_size,
"stage3_param_persistence_threshold": hf_config.hidden_size,
"stage3_max_live_parameters": 2 * hf_config.hidden_size * hf_config.hidden_size
},
"steps_per_print": 2000,
"train_micro_batch_size_per_gpu": 1,
"wall_clock_breakdown": False,
'weight_quantization': {
'quantized_initialization': {
'num_bits': bits,
'group_size': 32,
'group_dim': 1,
'symmetric': False
},
}
}
if cpu_offload:
ds_config["zero_optimization"]["offload_param"] = dict(device="cpu", pin_memory=1)
if nvme_offload:
ds_config["zero_optimization"]["offload_param"] = dict(
device="nvme",
pin_memory=True,
nvme_path=nvme_path or '~/tmp_offload_dir',
buffer_count=5,
buffer_size=1 * GB,
)
ds_config["aio"] = {
"block_size": 1048576,
"queue_depth": 8,
"thread_count": 1,
"single_submit": False,
"overlap_events": True,
}
return ds_config
hf_config = AutoConfig.from_pretrained('facebook/opt-125m')
ds_config = get_zero3_ds_config(hf_config=hf_config, cpu_offload=cpu_offload, nvme_offload=nvme_offload, bits=bits)
input_ids = torch.ones(1, 16, dtype=torch.int32, device=device)
attention_mask = torch.ones(1, 16, dtype=torch.float32, device=device)
with torch.no_grad():
ref_model = AutoModel.from_pretrained('facebook/opt-125m', torch_dtype=torch.float16).to(device)
ref_model.eval()
ref_output = ref_model(input_ids=input_ids, attention_mask=attention_mask)
with torch.no_grad():
dschf = HfDeepSpeedConfig(ds_config)
model = AutoModel.from_pretrained('facebook/opt-125m', torch_dtype=torch.float16)
model = model.eval()
ds_engine = deepspeed.initialize(model=model, config_params=ds_config)[0]
ds_engine.module.eval()
model = ds_engine.module
output = model(input_ids=input_ids, attention_mask=attention_mask)
mean_diff = torch.mean(torch.abs(output.last_hidden_state - ref_output.last_hidden_state))
# This threshold value is emperically selected.
assert mean_diff < 0.4, f'Numeric error exceed threshold, relative error {mean_diff} (threshold 0.4)'
@pytest.fixture(params=[4, 8], ids=["4bits", "8bits"])
def quantization_bits(request):
return request.param
@pytest.fixture(params=[0, 1], ids=["0", "1"])
def group_dim(request):
return request.param
class TestQuantizedInt(DistributedTest):
def test_model_quantization(self, quantization_bits):
reset_random()
config = AutoConfig.from_pretrained('facebook/opt-125m')
with torch.no_grad():
model = OPTDecoderLayer(config).half().to(device)
bits = quantization_bits
ds_config = {
'weight_quantization': {
'post_init_quant': {
'fc': {
'num_bits': bits,
'group_size': 64,
'group_dim': 0,
'symmetric': False
},
'self_attn.q_proj': {
'num_bits': bits,
'group_size': 64,
'group_dim': 0,
'symmetric': False
},
'self_attn.k_proj': {
'num_bits': bits,
'group_size': 64,
'group_dim': 0,
'symmetric': False
},
'self_attn.v_proj': {
'num_bits': bits,
'group_size': 64,
'group_dim': 0,
'symmetric': False
},
'self_attn.out_proj': {
'num_bits': bits,
'group_size': 64,
'group_dim': 0,
'symmetric': False
}
}
}
}
model = _init_group_wise_weight_quantization(model, ds_config)
assert type(model.fc1) is QuantizedLinear
assert type(model.fc2) is QuantizedLinear
assert type(model.self_attn.q_proj) is QuantizedLinear
assert type(model.self_attn.k_proj) is QuantizedLinear
assert type(model.self_attn.v_proj) is QuantizedLinear
assert type(model.self_attn.out_proj) is QuantizedLinear
@pytest.mark.skipif(device == 'cpu', reason='CPU does support FP16 GEMM')
def test_quantized_linear(self, quantization_bits, group_dim):
reset_random()
layers = []
for idx in range(5):
layers.append(
(f'layer_{idx}', nn.Linear(in_features=128, out_features=128, dtype=torch.float16, device=device)))
input_tensor = torch.randn(32, 128, dtype=torch.float16, device=device)
with torch.no_grad():
model = nn.Sequential(OrderedDict(layers))
ref_output = model(input_tensor)
ds_config = {
'weight_quantization': {
'post_init_quant': {
'layer': {
'num_bits': quantization_bits,
'group_size': 64,
'group_dim': group_dim,
'symmetric': False
}
}
}
}
model = _init_group_wise_weight_quantization(model, ds_config)
assert type(model.layer_0) is QuantizedLinear
assert type(model.layer_1) is QuantizedLinear
assert type(model.layer_2) is QuantizedLinear
assert type(model.layer_3) is QuantizedLinear
assert type(model.layer_4) is QuantizedLinear
output = model(input_tensor)
mean_diff = torch.mean(torch.abs(ref_output - output))
# This threshold value is emperically selected.
assert mean_diff < 0.15, f'Numeric error exceed threshold, mean diff {mean_diff}'
def test_float_int4_quantization(self):
reset_random()
quantization_test_helper(torch.float32, 4)
@pytest.mark.skipif(device == 'cpu', reason='CPU does support FP16 GEMM')
def test_half_int4_quantization(self):
reset_random()
quantization_test_helper(torch.float16, 4)
def test_float_int8_quantization(self):
reset_random()
quantization_test_helper(torch.float32, 8)
def test_half_int8_quantization(self):
reset_random()
quantization_test_helper(torch.float16, 8)
@pytest.mark.skipif(device == 'cpu', reason='CPU does support FP16 GEMM')
def test_zero3_int4_post_init_quant(self, quantization_bits):
reset_random()
zero3_post_init_quantization_test_helper(cpu_offload=False, nvme_offload=False, bits=quantization_bits)
@pytest.mark.skipif(device == 'cpu', reason='CPU does support FP16 GEMM')
def test_zero3_int4_post_init_quant_cpu_offload(self, quantization_bits):
reset_random()
zero3_post_init_quantization_test_helper(cpu_offload=True, nvme_offload=False, bits=quantization_bits)
@pytest.mark.skipif(device == 'cpu', reason='CPU does support FP16 GEMM')
def test_zero3_int4_post_init_quant_nvme_offload(self, tmpdir):
reset_random()
zero3_post_init_quantization_test_helper(cpu_offload=False,
nvme_offload=True,
bits=4,
nvme_path=str(tmpdir.join("nvme_offload")))
@pytest.mark.skipif(device == 'cpu', reason='CPU does support FP16 GEMM')
def test_zero3_int4_quantized_initialization(self, quantization_bits):
reset_random()
zero3_quantized_initialization_test_helper(cpu_offload=False, nvme_offload=False, bits=quantization_bits)
@pytest.mark.skipif(device == 'cpu', reason='CPU does support FP16 GEMM')
def test_zero3_int4_quantized_initialization_cpu_offload(self, quantization_bits):
reset_random()
zero3_quantized_initialization_test_helper(cpu_offload=True, nvme_offload=False, bits=quantization_bits)
@pytest.mark.skipif(device == 'cpu', reason='CPU does support FP16 GEMM')
def test_zero3_int4_quantized_initialization_nvme_offload(self, tmpdir):
reset_random()
zero3_quantized_initialization_test_helper(cpu_offload=False,
nvme_offload=True,
bits=4,
nvme_path=str(tmpdir.join("nvme_offload")))