@@ -28,29 +28,35 @@ def test_quantization_status(self):
28
28
x = torch .randn ((4 , 5 ), dtype = torch .float32 ).to (device )
29
29
model = torch .nn .Linear (5 , 10 , bias = True ).float ().to (device )
30
30
31
+ model1 = copy .deepcopy (model )
32
+ x1 = x .clone ()
31
33
conf = ipex .AmpConf (torch .int8 )
32
34
with ipex .AutoMixPrecision (conf , running_mode = 'calibration' ):
33
- ref = model ( x )
35
+ ref = model1 ( x1 )
34
36
conf .save ('configure.json' )
35
37
conf = ipex .AmpConf (torch .int8 , 'configure.json' )
36
38
with ipex .AutoMixPrecision (conf , running_mode = 'inference' ):
37
- y = model ( x )
39
+ y = model1 ( x1 )
38
40
self .assertTrue (ipex .core .is_int8_dil_tensor (y ))
39
41
jsonFile = open ('configure.json' , 'r' )
40
42
data = json .load (jsonFile )
41
43
jsonFile .close ()
42
44
self .assertTrue (data [0 ]['quantized' ])
43
45
44
46
# check configure's change can works for calibration step,
45
- # need get fp32 tensor for quantized=False.
47
+ # we need use origin model, because after running inference
48
+ # step, the model has beem quantized, after change quantized
49
+ # to False, the output should be fp32, i.e. not be quantized.
46
50
data [0 ]['quantized' ] = False
47
51
jsonFile = open ('configure.json' , "w+" )
48
52
jsonFile .write (json .dumps (data ))
49
53
jsonFile .close ()
50
54
# use user's changed configure.
55
+ model2 = copy .deepcopy (model )
56
+ x2 = x .clone ()
51
57
conf = ipex .AmpConf (torch .int8 , 'configure.json' )
52
58
with ipex .AutoMixPrecision (conf , running_mode = 'calibration' ):
53
- ref = model ( x )
59
+ ref = model2 ( x2 )
54
60
conf .save ('configure.json' )
55
61
conf = ipex .AmpConf (torch .int8 , 'configure.json' )
56
62
jsonFile = open ('configure.json' , 'r' )
@@ -59,7 +65,7 @@ def test_quantization_status(self):
59
65
self .assertFalse (data [0 ]['quantized' ])
60
66
61
67
with ipex .AutoMixPrecision (conf , running_mode = 'inference' ):
62
- y = model ( x )
68
+ y = model2 ( x2 )
63
69
self .assertTrue (ipex .core .is_fp32_dil_tensor (y ))
64
70
os .remove ('configure.json' )
65
71
0 commit comments