Skip to content

Commit

Permalink
TensorFlow utility refine (intel#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
lvliang-intel authored Jul 11, 2022
1 parent 5032f34 commit 909f22e
Show file tree
Hide file tree
Showing 74 changed files with 206 additions and 117 deletions.
12 changes: 6 additions & 6 deletions neural_compressor/adaptor/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def evaluate(self, model, dataloader, postprocess=None,
logger.info("Rank {!s} dataloaders' data distribution balance check for evaluation have been finnished." \
.format(hvd.allgather_object(hvd.rank())))
if tensorboard:
from .tf_utils.graph_rewriter.graph_util import GraphAnalyzer
from .tf_utils.graph_util import GraphAnalyzer
from tensorflow.python.framework import tensor_util

output_postfix = "_fp32.output"
Expand Down Expand Up @@ -732,7 +732,7 @@ def _query_quantizable_ops(self, matched_nodes):
def filter_unquantizable_concat(self, matched_nodes):
target_concat_nodes = [i[0] for i in matched_nodes if i[-1][0] == 'ConcatV2']
from neural_compressor.adaptor.tf_utils.util import GraphAnalyzer
from .tf_utils.graph_rewriter.graph_util import GraphRewriterHelper
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper

g = GraphAnalyzer()
g.graph = self.pre_optimized_model.graph_def
Expand Down Expand Up @@ -821,7 +821,7 @@ def check_match(patterns, input_pattern):
return capability

def set_tensor(self, model, tensor_dict):
from .tf_utils.graph_rewriter.graph_util import GraphAnalyzer
from .tf_utils.graph_util import GraphAnalyzer
g = GraphAnalyzer()
g.graph = model.graph_def
graph_info = g.parse_graph()
Expand All @@ -845,7 +845,7 @@ def _get_fp32_op_name(model, tensor_name):

return is_weight, is_biasadd, current_node_name, last_node_name

from neural_compressor.adaptor.tf_utils.graph_rewriter.graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import tensor_util
from tensorflow.core.framework import attr_value_pb2
Expand Down Expand Up @@ -908,7 +908,7 @@ def _get_fp32_op_name(model, tensor_name):
tensor_content.transpose(2,3,1,0), dtypes.float32)
min_filter_node = graph_info[current_node.input[5]].node
per_channel = True if min_filter_node.attr['value'].tensor.tensor_shape else False
from .tf_utils.quantize_graph.quantize_graph_common import QuantizeGraphHelper
from .tf_utils.quantize_graph_common import QuantizeGraphHelper
original_fp32_op = current_node.op.split("With")[0].split("Quantized")[-1]
if original_fp32_op.find("Depthwise") != -1:
original_fp32_op = "DepthwiseConv2dNative"
Expand Down Expand Up @@ -1001,7 +1001,7 @@ def quantize_input(self, model):
quantize_node_outputs = [node for node in graph_def.node
if quantize_node.name in node.input]

from .tf_utils.graph_rewriter.graph_util import GraphRewriterHelper
from .tf_utils.graph_util import GraphRewriterHelper
if quantize_node_input.op == 'Pad':
pad_node_input = node_name_mapping[quantize_node_input.input[0]]
assert pad_node_input.op == 'Placeholder', \
Expand Down
6 changes: 3 additions & 3 deletions neural_compressor/adaptor/tf_utils/graph_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@
from .util import iterator_sess_run,version1_gt_version2,version1_eq_version2,version1_lt_version2
from .util import version1_gte_version2,version1_lte_version2
from .quantize_graph.quantize_graph_for_intel_cpu import QuantizeGraphForIntel
from .quantize_graph.quantize_graph_common import QuantizeGraphHelper
from .quantize_graph_common import QuantizeGraphHelper
from .quantize_graph.quantize_graph_conv import FuseNodeStartWithConv2d

from .graph_rewriter.graph_util import GraphAnalyzer
from .graph_util import GraphAnalyzer
from .graph_rewriter.generic.remove_training_nodes import RemoveTrainingNodesOptimizer
from .graph_rewriter.generic.strip_unused_nodes import StripUnusedNodesOptimizer
from .graph_rewriter.generic.fold_batch_norm import FoldBatchNormNodesOptimizer
Expand All @@ -62,7 +62,7 @@
from .graph_rewriter.int8.rnn_convert import QuantizedRNNConverter
from .graph_rewriter.itex.itex_convert import GenerateITEXModel
from neural_compressor.adaptor.tf_utils.graph_rewriter.generic.insert_print_node import InsertPrintMinMaxNode
from neural_compressor.adaptor.tf_utils.graph_rewriter.graph_util import GraphRewriterHelper as Helper
from .graph_util import GraphRewriterHelper as Helper


TF_SUPPORTED_MAX_VERSION = '2.9.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
from .transform_graph.rerange_quantized_concat import RerangeQuantizedConcat
from .transform_graph.bias_correction import BiasCorrection
from .quantize_graph.quantize_graph_for_intel_cpu import QuantizeGraphForIntel
from .quantize_graph.quantize_graph_common import QuantizeGraphHelper
from .quantize_graph_common import QuantizeGraphHelper

from .graph_rewriter.graph_util import GraphAnalyzer
from .graph_util import GraphAnalyzer
from .graph_rewriter.generic.remove_training_nodes import RemoveTrainingNodesOptimizer
from .graph_rewriter.generic.strip_unused_nodes import StripUnusedNodesOptimizer
from .graph_rewriter.generic.fold_batch_norm import FoldBatchNormNodesOptimizer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
from tensorflow.python.framework.kernels import get_registered_kernels_for_op

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper
from ..generic.graph_cse_optimizer import GraphCseOptimizer

DT_FLOAT32 = attr_value_pb2.AttrValue(type=dtypes.float32.as_datatype_enum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from neural_compressor.utils.utility import dump_elapsed_time

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper
from tensorflow.python.framework import tensor_util

class ConvertAddToBiasAddOptimizer(GraphRewriterBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from neural_compressor.utils.utility import dump_elapsed_time

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper


class ConvertLeakyReluOptimizer(GraphRewriterBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import numpy as np
from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import tensor_util
from tensorflow.core.framework import attr_value_pb2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from neural_compressor.utils.utility import dump_elapsed_time

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.util import version1_gt_version2


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@


from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper
from neural_compressor.utils.utility import dump_elapsed_time
from tensorflow.python.framework import dtypes
import numpy as np
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@


from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper
from neural_compressor.utils.utility import dump_elapsed_time
from tensorflow.python.framework import dtypes
import numpy as np
Expand Down Expand Up @@ -80,4 +80,4 @@ def do_transformation(self):
cur_graph.remove_node(shape_node.name)
cur_graph.remove_node(pack_node.name)

return cur_graph.dump_graph()
return cur_graph.dump_graph()
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

from neural_compressor.utils.utility import dump_elapsed_time
from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper

class FoldBatchNormNodesOptimizer(GraphRewriterBase):
INPUT_ORDER = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from neural_compressor.utils.utility import dump_elapsed_time

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer, GraphRewriterHelper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer, GraphRewriterHelper


class GraphFoldConstantOptimizer(GraphRewriterBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import tensorflow as tf
from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import tensor_util

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from neural_compressor.utils.utility import dump_elapsed_time

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper

class FuseColumnWiseMulOptimizer(GraphRewriterBase):
"""Fuse Mul op into Conv2D/DepthwiseConv2dNative/MatMul
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
from neural_compressor.utils.utility import dump_elapsed_time

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper


class FuseConvWithMathOptimizer(GraphRewriterBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import tensorflow as tf
from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper
from tensorflow.python.framework import dtypes


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from tensorflow.python.framework import tensor_util

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.util import version1_gt_version2

class FusePadWithConv2DOptimizer(GraphRewriterBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from tensorflow.python.framework import tensor_util

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.util import version1_gt_version2

class FusePadWithFP32Conv2DOptimizer(GraphRewriterBase):
Expand Down Expand Up @@ -84,4 +84,4 @@ def do_transformation(self):
Helper.set_attr_string(conv_node, 'padding', b'EXPLICIT')
Helper.set_attr_int_list(conv_node, "explicit_paddings", padding_tensor)

return cur_graph.dump_graph()
return cur_graph.dump_graph()
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
from neural_compressor.utils.utility import dump_elapsed_time

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper


class FuseTransposeReshapeOptimizer(GraphRewriterBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from neural_compressor.utils.utility import dump_elapsed_time

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper


class GraphCseOptimizer(GraphRewriterBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import tensor_util as tu
from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper


class InsertPrintMinMaxNode(GraphRewriterBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from neural_compressor.utils.utility import dump_elapsed_time

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer


class MoveSqueezeAfterReluOptimizer(GraphRewriterBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


import logging
from neural_compressor.adaptor.tf_utils.graph_rewriter.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.utils.utility import dump_elapsed_time
from .fuse_column_wise_mul import FuseColumnWiseMulOptimizer
from .remove_training_nodes import RemoveTrainingNodesOptimizer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from neural_compressor.utils.utility import dump_elapsed_time

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer


class RemoveTrainingNodesOptimizer(GraphRewriterBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

from neural_compressor.utils.utility import dump_elapsed_time
from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper

class RenameBatchNormOptimizer(GraphRewriterBase):
@dump_elapsed_time("Pass RenameBatchNormOptimizer")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from neural_compressor.utils.utility import dump_elapsed_time

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper


class SplitSharedInputOptimizer(GraphRewriterBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.utils.utility import dump_elapsed_time
from tensorflow.python.framework import tensor_util

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.utils.utility import dump_elapsed_time


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
from neural_compressor.utils.utility import dump_elapsed_time

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper
from tensorflow.python.framework import dtypes

class FreezeFakeQuantOpOptimizer(GraphRewriterBase):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from tensorflow.python.framework import dtypes

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper

import numpy as np
import re
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from tensorflow.python.framework import dtypes

from ..graph_base import GraphRewriterBase
from ..graph_util import GraphAnalyzer
from ..graph_util import GraphRewriterHelper as Helper
from neural_compressor.adaptor.tf_utils.graph_util import GraphAnalyzer
from neural_compressor.adaptor.tf_utils.graph_util import GraphRewriterHelper as Helper

class FreezeValueWithoutCalibTransformer(GraphRewriterBase):
def __init__(self, model, max_min_data, postfix, th=0.95, device='gpu'):
Expand Down
Loading

0 comments on commit 909f22e

Please sign in to comment.