neural_compressor.adaptor.tf_utils.graph_util

Tensorflow Graph Utils Helper Classes.

Module Contents

Classes

GraphAnalyzer

Tensorflow Graph Analyzer class which implemented under singleton mode.

GraphRewriterHelper

Encapsulates the graph operation into one class.

class neural_compressor.adaptor.tf_utils.graph_util.GraphAnalyzer(extend_engine=None)

Tensorflow Graph Analyzer class which implemented under singleton mode.

This class provides the following API: * Analyze the graph * Analyze the input/output node names of the specified graph

property graph

Getter of the _graph object.

Returns:

current graphdef object

Return type:

graph

has_positive_input(node_name)

Check the specified node has positive input data or not.

Parameters:

node_name (string) – node name

Returns:

retrun True if the node has the positive input data,

return False if the node has the negative input data.

Return type:

bool

get_graph_input_output()

Get the graphdef input/output node names.

Sometimes, the configuration doesn’t specifies the input/output names of the graph, but tensorflow need to know them clearly to run the graph.We implement this function has the similar feature like summarize_graph.py which writtern by Google.

Returns:

(inputs’ name list, outputs’name list)

Return type:

tuple

query_fusion_pattern_nodes(patterns=None)

Public interface for query the nodes aggregation status.

Parameters:

patterns (string list) – Please check the _search_patterns definition.

Returns:

The matched node names which saved as the string list.

Return type:

[string list]

remove_node_with_single_input_output(node_name)

Remove node with one input and rebuild internal graph data structure.

Parameters:

node_name (string) – node name

Returns:

True if remove the node without exception,

False if failed to remove it.

Return type:

[bool]

remove_node(node_name)

Remove the user specified node by its name.

Parameters:

node_name (string) – node name string.

Returns:

True if remove the node without exception.

False if failed to remove it.

Return type:

[bool]

replace_const_node(new_const_node, target_node, old_constant_node_name, replace_all=True)

Replace the specified const node with another one.

Parameters:
  • new_const_node (NodeDef) – node name string.

  • target_node (list) – the string list that contains name of node that need to be replaced const node.

  • old_constant_node_name (string) – the outdated const node name.

  • replace_all (bool) – replace the specified node name once or not.

replace_constant_graph_with_constant_node(new_node, old_end_node_name)

Remove sub-graph with a const node.

Parameters:
  • new_node (nodedef) – the constant node

  • old_end_node_name (string) – the sub-graph end node which will be updated by new node

Returns:

True if remove the node without exception.

False if failed to remove it.

Return type:

[bool]

replace_single_node(new_node, old_output_node_names, old_output_name, old_input_node_names, old_input_name)

Insert one node into the graph.

Parameters:
  • new_node (nodedef) – new nodedef object

  • old_output_node_names (string list) – the node names that would be the top node of new node.

  • old_output_name (string list) – the names that need to be updated with new node name

  • old_input_node_names (string list) – the node names that would be the bottom node of new node.

  • old_input_name (string list) – the names that need to be updated with new node name

replace_node(new_node, old_node_name, output_nodes_name)

Replace the node into the internal data structure node_name_details.

Parameters:
  • new_node (nodedef) – the nodedef object.

  • old_node_name (string) – the parent node of input node.

  • output_nodes_name (string list) – output node names list

add_node(new_node, start_node_name, end_node_names)

Add the node into the internal data structure node_name_details.

Parameters:
  • new_node (nodedef) – the nodedef object.

  • start_node_name (string) – the parent node of input node.

  • end_node_names (string list) – output node names list

dump_graph()

Dump the current model’s graphdef.

Returns:

A graphdef object

Return type:

[graphdef]

get_frame_info()

Get the frame info of the model.

Returns:

OrderedDict frame info of the graph nodes.

Return type:

[parent_frame_details]

parse_graph(input_graph_def=None)

Analyze the input graphdef and return the list contains each node’s input/outputnode names.

Parameters:

input_graph_def ([graphdef]) – graphdef object

Returns:

A list contains each node’s inputs/outputs info.

Return type:

[list]

class neural_compressor.adaptor.tf_utils.graph_util.GraphRewriterHelper

Encapsulates the graph operation into one class.

static compare_node_attr(node_a, node_b)

Compare two node has identical attributes or not.

Parameters:
  • node_a (nodedef) – Input node.

  • node_b (nodedef) – Another node to be compared.

Returns:

True if two node have the identical attributes.

Return type:

[bool]

static create_node(op, name, inputs)

Create a nodedef object.

Parameters:
  • op (string) – op type

  • name (string) – op name

  • inputs (string list) – op’s inputs name

Returns:

the created nodedef object

Return type:

nodedef

static create_constant_node(name, value, dtype, shape=None, device='cpu')

Create constant node.

Parameters:
  • name (string) – op name

  • value (np.array) – input data

  • dtype (datatype) – data type of the input value

  • shape (int list, optional) – the value’s shape. Defaults to None.

  • device (str, optional) – the device type, it may be the ‘cpu’ or ‘gpu’. Defaults to ‘cpu’.

Returns:

[description]

Return type:

[type]

static set_attr_dtype(node, key, value)

Set the attribute data type.

static set_attr_tensor(node, key, value, dtype, shape=None)

Set the tensor value to specified attribute field.

Parameters:
  • node (nodedef) – the target nodedef object

  • key (string) – attribute name

  • value (np.array) – the content

  • dtype (dtypes) – data type

  • shape (int list, optional) – the input tensor’s shape. Defaults to None.

static set_attr_type_list(node, key, value)

Set the node’s attr which data type is int list.

static set_attr_string_list(node, key, value)

Set the node’s attr which data type is int list.

static set_attr_string(node, key, value)

Set the node’s attr which data type is string.

static set_attr_int_list(node, key, value)

Set the node’s attr which data type is int list.

static set_attr_int(node, key, value)

Set the node’s attr which data type is int.

static set_attr_float(node, key, value)

Set the node’s attr which data type is float.

static set_attr_bool(node, key, value)

Set the node’s attr which data type is bool.

static node_name_from_input(node_name)

Static method that get the valid node name from input name.

Parameters:

node_name (string) – node name defined in the input field.

Returns:

node’s name

Return type:

string

static values_from_const(node_def)

Extracts the values from a const NodeDef as a numpy ndarray.

Parameters:

node_def – Const NodeDef that has the values we want to access.

Returns:

Numpy ndarray containing the values.

Raises:

ValueError – If the node isn’t a Const.

static generate_int32_bias_for_conv(bias_tensor, channel_size, max_input, min_input, max_filter_tensor, min_filter_tensor, activation_range, weights_range=127.0)

Static method that generate int32 bias for conv op.

Parameters:
  • bias_tensor – bias node tensor.

  • channel_size – channel size.

  • max_input – max activation input value.

  • min_input – min activation input value.

  • max_filter_tensor – max weight input tensor.

  • min_filter_tensor – min weight input tensor.

  • activation_range – activation range value.

  • weights_range – weight range value.

Returns:

int32 bias

Return type:

int32_bias

static generate_int32_bias_for_matmul(bias_tensor, weights_tensor, input_range, max_input, min_input, max_filter_value, min_filter_value)

Static method that generate int32 bias for matmul op.

Parameters:
  • bias_tensor – bias node tensor.

  • weights_tensor – weights tensor.

  • input_range – activation range value.

  • max_input – max activation input value.

  • min_input – min activation input value.

  • max_filter_tensor – max weight input tensor.

  • min_filter_tensor – min weight input tensor.

Returns:

int32 bias

Return type:

int32_bias

static gen_valid_sampling_log(log_path)

Generate the valid sampling log.

Parameters:

log_path – the valid sampling log file path.

Returns:

the sampling min max value.

static analysis_rnn_model(graph_def, bf16_ops=[], fp32_ops=[])

Match the RNN and dynamic RNN patterns.