@@ -90,46 +90,13 @@ def __init__(self, framework_specific_info):
90
90
os .mkdir (DEFAULT_WORKSPACE )
91
91
self .tmp_dir = (DEFAULT_WORKSPACE + "tmp_model.keras" ) if self .keras3 else (DEFAULT_WORKSPACE + "tmp_model" )
92
92
93
- def _check_itex (self ):
94
- """Check if the Intel® Extension for TensorFlow has been installed."""
95
- try :
96
- import intel_extension_for_tensorflow
97
- except :
98
- raise ImportError (
99
- "The Intel® Extension for TensorFlow is not installed. "
100
- "Please install it to run models on ITEX backend"
101
- )
102
-
103
- def convert_bf16 (self ):
104
- """Execute the BF16 conversion."""
105
- tf .keras .mixed_precision .set_global_policy ("mixed_bfloat16" )
106
- model = self .pre_optimized_model
107
-
108
- for layer in model .layers :
109
- if layer .name in self .bf16_ops :
110
- layer .dtype = "mixed_bfloat16"
111
-
112
- model .save (self .tmp_dir )
113
- converted_model = tf .keras .models .load_model (self .tmp_dir )
114
- tf .keras .mixed_precision .set_global_policy ("float32" )
115
-
116
- return converted_model
117
-
118
- # (TODO) choose the properly quantize mode
119
- def _check_quantize_mode (self , model ):
120
- """Check what quantize mode to use."""
121
- for layer in model .layers :
122
- if "ReLU" in layer .__class__ .__name__ :
123
- return "MIN_FIRST"
124
- return "SCALED"
125
-
126
93
def _set_weights (self , qmodel , layer_weights ):
127
94
"""Set fp32 weights to qmodel."""
128
95
for qlayer in qmodel .layers :
129
96
if qlayer .get_weights ():
130
97
if qlayer .name in layer_weights :
131
98
qlayer .set_weights (layer_weights [qlayer .name ])
132
- else :
99
+ else : # pragma: no cover
133
100
hit_layer = False
134
101
for sub_layer in qlayer .submodules :
135
102
if sub_layer .name in layer_weights :
@@ -164,7 +131,7 @@ def _check_quantize_format(self, model):
164
131
self .conv_format [layer .name ] = "u8"
165
132
break
166
133
167
- def _fuse_bn_keras3 (self , fuse_conv_bn , fp32_layers ):
134
+ def _fuse_bn_keras3 (self , fuse_conv_bn , fp32_layers ): # pragma: no cover
168
135
fuse_layers = []
169
136
fused_bn_name = ""
170
137
for idx , layer in enumerate (fp32_layers ):
@@ -211,7 +178,7 @@ def _fuse_bn_keras3(self, fuse_conv_bn, fp32_layers):
211
178
212
179
return fuse_layers
213
180
214
- def _fuse_bn_keras2 (self , fuse_conv_bn , fp32_layers ):
181
+ def _fuse_bn_keras2 (self , fuse_conv_bn , fp32_layers ): # pragma: no cover
215
182
fuse_layers = []
216
183
for idx , layer in enumerate (fp32_layers ):
217
184
if hasattr (layer , "_inbound_nodes" ):
@@ -272,7 +239,7 @@ def _fuse_bn_keras2(self, fuse_conv_bn, fp32_layers):
272
239
273
240
return fuse_layers
274
241
275
- def _fuse_bn (self , model ):
242
+ def _fuse_bn (self , model ): # pragma: no cover
276
243
"""Fusing Batch Normalization."""
277
244
model .save (self .tmp_dir )
278
245
fuse_bn_model = tf .keras .models .load_model (self .tmp_dir )
@@ -362,14 +329,6 @@ def quantize(self, quant_config, model, dataloader, iteration, q_func=None):
362
329
tune_cfg = converter .parse_to_tune_cfg ()
363
330
self .tuning_cfg_to_fw (tune_cfg )
364
331
365
- # just convert the input model to mixed_bfloat16
366
- if self .bf16_ops and not self .quantize_config ["op_wise_config" ]:
367
- converted_model = self .convert_bf16 ()
368
- return converted_model
369
-
370
- # if self.backend == "itex":
371
- # self._check_itex()
372
-
373
332
logger .debug ("Dump quantization configurations:" )
374
333
logger .debug (self .quantize_config )
375
334
calib_sampling_size = tune_cfg .get ("calib_sampling_size" , 1 )
@@ -469,59 +428,6 @@ def _calibrate(self, model, dataloader, calib_interation):
469
428
470
429
return quantized_model
471
430
472
- @dump_elapsed_time (customized_msg = "Model inference" )
473
- def evaluate (
474
- self ,
475
- model ,
476
- dataloader ,
477
- postprocess = None ,
478
- metrics = None ,
479
- measurer = None ,
480
- iteration = - 1 ,
481
- fp32_baseline = False ,
482
- ):
483
- """The function is used to run evaluation on validation dataset.
484
-
485
- Args:
486
- model (object): The model to do calibration.
487
- dataloader (generator): generate the data and labels.
488
- postprocess (object, optional): process the result from the model
489
- metric (object, optional): Depends on model category. Defaults to None.
490
- measurer (object, optional): for precise benchmark measurement.
491
- iteration(int, optional): control steps of mini-batch
492
- fp32_baseline (boolean, optional): only for compare_label=False pipeline
493
- """
494
- # use keras object
495
- keras_model = model .model
496
- logger .info ("Start to evaluate the Keras model." )
497
- results = []
498
- for idx , (inputs , labels ) in enumerate (dataloader ):
499
- # use predict on batch
500
- if measurer is not None :
501
- measurer .start ()
502
- predictions = keras_model .predict_on_batch (inputs )
503
- measurer .end ()
504
- else :
505
- predictions = keras_model .predict_on_batch (inputs )
506
-
507
- if self .fp32_preds_as_label :
508
- self .fp32_results .append (predictions ) if fp32_baseline else results .append (predictions )
509
-
510
- if postprocess is not None :
511
- predictions , labels = postprocess ((predictions , labels ))
512
- if metrics :
513
- for metric in metrics :
514
- if not hasattr (metric , "compare_label" ) or (
515
- hasattr (metric , "compare_label" ) and metric .compare_label
516
- ):
517
- metric .update (predictions , labels )
518
- if idx + 1 == iteration :
519
- break
520
-
521
- acc = 0 if metrics is None else [metric .result () for metric in metrics ]
522
-
523
- return acc if not isinstance (acc , list ) or len (acc ) > 1 else acc [0 ]
524
-
525
431
def query_fw_capability (self , model ):
526
432
"""The function is used to return framework tuning capability.
527
433
@@ -621,7 +527,7 @@ def tuning_cfg_to_fw(self, tuning_cfg):
621
527
for each_op_info in tuning_cfg ["op" ]:
622
528
op_name = each_op_info [0 ]
623
529
624
- if tuning_cfg ["op" ][each_op_info ]["activation" ]["dtype" ] == "bf16" :
530
+ if tuning_cfg ["op" ][each_op_info ]["activation" ]["dtype" ] == "bf16" : # pragma: no cover
625
531
if each_op_info [1 ] in bf16_type :
626
532
bf16_ops .append (op_name )
627
533
continue
@@ -693,31 +599,6 @@ def _get_specified_version_cfg(self, data):
693
599
694
600
return default_config
695
601
696
- def get_version (self ):
697
- """Get the current backend version information.
698
-
699
- Returns:
700
- [string]: version string.
701
- """
702
- return self .cur_config ["version" ]["name" ]
703
-
704
- def get_precisions (self ):
705
- """Get supported precisions for current backend.
706
-
707
- Returns:
708
- [string list]: the precisions' name.
709
- """
710
- return self .cur_config ["precisions" ]["names" ]
711
-
712
- def get_op_types (self ):
713
- """Get the supported op types by all precisions.
714
-
715
- Returns:
716
- [dictionary list]: A list composed of dictionary which key is precision
717
- and value is the op types.
718
- """
719
- return self .cur_config ["ops" ]
720
-
721
602
def get_quantization_capability (self ):
722
603
"""Get the supported op types' quantization capability.
723
604
@@ -846,7 +727,7 @@ def _parse_inputs(self, BN_fused_layers=None, conv_names=None):
846
727
847
728
try :
848
729
model_input = self .model .input
849
- except ValueError :
730
+ except ValueError : # pragma: no cover
850
731
model_input = self .model .inputs [0 ]
851
732
852
733
return input_layer_dict , model_input
0 commit comments