Skip to content

Commit cdf6fc4

Browse files
authored
Revert "Enable TensorFlow INT8/BF16 mix precision for small ops (#814)" (#849)
This reverts commit 61c275d
1 parent 80aa908 commit cdf6fc4

File tree

4 files changed

+293
-303
lines changed

4 files changed

+293
-303
lines changed

neural_compressor/adaptor/tensorflow.py

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def _pre_hook_for_hvd(self):
103103
import horovod.tensorflow as hvd
104104
self.hvd = hvd
105105
self.hvd.init()
106-
106+
107107
@dump_elapsed_time(customized_msg="Model training")
108108
def train(self, model, dataloader, optimizer_tuple,
109109
criterion_tuple, hooks, postprocess, **kwargs):
@@ -142,7 +142,7 @@ def train(self, model, dataloader, optimizer_tuple,
142142
for i in range(len(list_len_dataloader)-1):
143143
if list_len_dataloader[i] != list_len_dataloader[i+1]:
144144
raise AttributeError("The traning dataloader's iteration is"
145-
"different between processes, please reset dataloader's batch_size.")
145+
"different between processes, please reset dataloader's batch_size.")
146146

147147
def training_step(x, y, first_batch):
148148
with tf.GradientTape() as tape:
@@ -158,7 +158,7 @@ def training_step(x, y, first_batch):
158158
self.hvd.broadcast_variables(input_model.variables, root_rank=0)
159159
self.hvd.broadcast_variables(optimizer.variables(), root_rank=0)
160160
return loss_value
161-
161+
162162
training_step = training_step if execution_mode=='eager' else tf.function(training_step)
163163
if start_epochs is not None and end_epochs is not None:
164164
epochs = end_epochs - start_epochs
@@ -599,18 +599,6 @@ def _dump_model_op_stats(self, model_graphdef):
599599
header='Mixed Precision Statistics',
600600
field_names=["Op Type", "Total", "INT8", "BF16", "FP32"]).print_stat()
601601

602-
def _query_bf16_ops(self, matched_nodes):
603-
self.bf16_op_details = OrderedDict()
604-
605-
valid_precision = self.query_handler.get_mixed_precision_combination()
606-
if ('bf16' in valid_precision and CpuInfo().bf16) or os.getenv('FORCE_BF16') == '1':
607-
for details in matched_nodes:
608-
node_op = details[-1][0]
609-
node_name = details[0]
610-
611-
self.bf16_op_details[(node_name, node_op)] = {'weight': {'dtype': ['bf16']}, \
612-
'activation': {'dtype': ['bf16']}}
613-
614602
def _query_quantizable_ops(self, matched_nodes):
615603
"""Collect the op-wise configuration for quantization.
616604
@@ -730,9 +718,7 @@ def query_fw_capability(self, model):
730718

731719
self.exclude_node_names = self.pre_optimizer_handle.get_excluded_node_names()
732720
patterns = self.query_handler.generate_internal_patterns()
733-
bf16_patterns = self.query_handler.get_bf16_patterns()
734721
matched_nodes = self.pre_optimizer_handle.get_matched_nodes(patterns)
735-
matched_bf16_nodes = self.pre_optimizer_handle.get_matched_nodes(bf16_patterns)
736722
original_graph_node_name = [i.name for i in model.graph_def.node]
737723
matched_nodes = sorted(matched_nodes, reverse=True, key=lambda i: (
738724
original_graph_node_name.index(i[0]), len(i[-1])))
@@ -756,21 +742,11 @@ def check_match(patterns, input_pattern):
756742

757743
del copied_matched_nodes
758744

759-
copied_matched_nodes = copy.deepcopy(matched_bf16_nodes)
760-
for i in copied_matched_nodes:
761-
for j in matched_nodes:
762-
if i[0] in j and i in matched_bf16_nodes:
763-
matched_bf16_nodes.remove(i)
764-
765-
del copied_matched_nodes
766-
767745
self._query_quantizable_ops(matched_nodes)
768-
self._query_bf16_ops(matched_bf16_nodes)
769746
capability = {
770747
'optypewise': self.get_optype_wise_ability(),
771748
}
772749
capability['opwise'] = copy.deepcopy(self.quantizable_op_details)
773-
capability['opwise'].update(self.bf16_op_details)
774750
logger.debug("Dump framework quantization capability:")
775751
logger.debug(capability)
776752

@@ -1013,9 +989,6 @@ def get_optype_wise_ability(self):
1013989
res[op[1]] = {'activation': self.quantizable_op_details[op]['activation']}
1014990
if 'weight' in self.quantizable_op_details[op]:
1015991
res[op[1]]['weight'] = self.quantizable_op_details[op]['weight']
1016-
for op in self.bf16_op_details:
1017-
if op[1] not in res:
1018-
res[op[1]] = {'activation': {'dtype': ['bf16']}, 'weight': {'dtype': ['bf16']}}
1019992
return res
1020993

1021994
def _pre_eval_hook(self, model):
@@ -1318,15 +1291,6 @@ def get_mixed_precision_combination(self):
13181291
def get_grappler_optimization_cfg(self):
13191292
return self.cur_config['grappler_optimization']
13201293

1321-
def get_bf16_patterns(self):
1322-
int8_op_types = self.get_op_types_by_precision('int8') + self.get_op_types_by_precision('uint8')
1323-
bf16_op_types = [i for i in self.get_op_types_by_precision('bf16') if i not in int8_op_types]
1324-
res = []
1325-
for i in bf16_op_types:
1326-
res.append([[i]])
1327-
1328-
return res
1329-
13301294
def get_eightbit_patterns(self):
13311295
"""Get eightbit op wise sequences information.
13321296

neural_compressor/adaptor/tensorflow.yaml

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,7 @@
2525
ops: &common_ops
2626
int8: ['Conv2D', 'MatMul', 'ConcatV2', 'MaxPool', 'AvgPool']
2727
uint8: ['Conv2D', 'DepthwiseConv2dNative', 'MatMul', 'ConcatV2', 'MaxPool', 'AvgPool']
28-
bf16: ['Conv2D', 'Conv2DBackpropFilter', 'Conv2DBackpropInput', 'Conv3D', 'Conv3DBackpropFilterV2',
29-
'Conv3DBackpropInputV2', 'DepthwiseConv2dNative', 'DepthwiseConv2dNativeBackpropFilter',
30-
'DepthwiseConv2dNativeBackpropInput', 'MatMul', 'BatchMatMul', 'BatchMatMulV2', # allow_list
31-
'Add', 'AddN', 'AddV2', 'AvgPool', 'AvgPool3D', 'AvgPool3DGrad', 'AvgPoolGrad', 'BiasAdd',
32-
'BiasAddGrad', 'BiasAddV1', 'FusedBatchNormV2', 'FusedBatchNormGradV2', 'FusedBatchNormV3',
33-
'FusedBatchNormGradV3', 'LeakyRelu', 'LeakyReluGrad', 'Mul', 'Sub', 'Elu', 'EluGrad', 'FloorDiv',
34-
'_FusedBatchNormEx', 'Log', 'Log1p', 'LogSoftmax', 'Prod', 'RealDiv', 'Reciprocal', 'Selu',
35-
'SeluGrad', 'Sigmoid', 'SigmoidGrad', 'Softmax', 'Softplus', 'SoftplusGrad', 'Softsign',
36-
'SoftsignGrad', 'Sqrt', 'Tanh', 'TanhGrad', #infer_list
37-
'Abs', 'ArgMax', 'ArgMin', 'BatchToSpace', 'BatchToSpaceND', 'BroadcastTo', 'Ceil', 'CheckNumerics',
38-
'ClipByValue', 'Concat', 'ConcatV2', 'DepthToSpace', 'DynamicPartition', 'DynamicStitch',
39-
'EnsureShape', 'Enter', #'Range',
40-
'Equal', 'Exit', 'ExpandDims', 'Fill', 'Floor',
41-
'Gather', 'GatherNd', 'GatherV2', 'Greater', 'GreaterEqual',
42-
'Identity', 'IsFinite', 'IsInf', 'IsNan', 'Less', 'LessEqual',
43-
'Max', 'Maximum', 'MaxPool', 'MaxPool3D', 'MaxPool3DGrad', 'MaxPoolGrad', 'MaxPoolGradGrad',
44-
'MaxPoolGradGradV2', 'MaxPoolGradV2', 'MaxPoolV2', 'Merge', 'Min', 'Minimum', 'MirrorPad', 'MirrorPadGrad',
45-
'Neg', 'NextIteration', 'NotEqual', 'OnesLike', 'Pack', 'Pad', 'PadV2', 'PreventGradient', 'Rank',
46-
'Relu', 'Relu6', 'Relu6Grad', 'ReluGrad', 'Reshape',
47-
'ResizeNearestNeighbor', 'ResizeNearestNeighborGrad',
48-
'Reverse', 'ReverseSequence', 'ReverseV2', 'Round', 'Select', 'SelectV2', 'Shape', 'ShapeN', 'Sign',
49-
'Slice', 'Snapshot', 'SpaceToBatch', 'SpaceToBatchND', 'SpaceToDepth', 'Split', 'SplitV', 'Squeeze',
50-
'StopGradient', 'StridedSlice', 'StridedSliceGrad', #'Switch',
51-
'Tile', 'TopK', 'TopKV2', 'Transpose', 'Where', 'Unpack', 'ZerosLike'] #clear_list
28+
bf16: ['Conv2D'] #TODO need to add more bf16 op types here
5229
fp32: ['*'] # '*' means all op types
5330

5431
capabilities: &common_capabilities

0 commit comments

Comments
 (0)