MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / dense_opt

Function dense_opt

tools/low_precision_optimize/low_precision_optimize.py:184–351  ·  view source on GitHub ↗
(session, graph_def, opt_config, data_type, calib_file)

Source from the content-addressed store, hash-verified

182
183
184def dense_opt(session, graph_def, opt_config, data_type, calib_file):
185 simple_graph = SimpleGraph(graph_def)
186 update_dict = dict()
187 calib_data = None
188 if calib_file:
189 calib_data = np.load(calib_file, allow_pickle=True, encoding='bytes')
190
191 def _calibrate(ts_name):
192 assert calib_data is not None, 'Calibration data needed for INT8 optimization.'
193 values = [session.run(ts_name, feed_dict=fd) for fd in calib_data]
194 values = np.concatenate([v.ravel() for v in values])
195 return non_linear_quant_params_search(values)
196
197 def _get_matmul_pattern(with_bias, with_relu):
198 pl = list()
199 output = ['0']
200 if with_relu:
201 tmp_input = ['bias_add'] if with_bias else ['matmul']
202 pl.append(SimpleNode('relu', 'Relu', tmp_input, ['1']))
203 output = ['relu']
204 if with_bias:
205 pl.append(SimpleNode('bias_add', 'BiasAdd', ['matmul', '2'], output))
206 output = ['bias_add']
207 pl.append(SimpleNode('matmul', 'MatMul', ['0', '1'], output))
208 pattern_nodes = {node.name: node for node in pl}
209 return pattern_nodes, pl[0].name
210
211 def _get_weight_data(node_name, input_index=1):
212 weight_name = util.get_input_target_op_name(
213 simple_graph, node_name, input_index, 'Const', {'Identity': [0]}
214 )
215 if weight_name:
216 data = util.get_const_value_by_name(graph_def, weight_name, simple_graph)
217 else:
218 node = util.get_node_by_name(graph_def, simple_graph, node_name)
219 try:
220 data = session.run(_ts(node.input[input_index]))
221 except Exception:
222 return None
223 return data
224
225 def _optimize(with_bias, with_relu):
226 pattern, first_key = _get_matmul_pattern(with_bias, with_relu)
227 ptm_list = util.get_matched_pattern(simple_graph, pattern, first_key)
228 ptm_list = [ptm for ptm in ptm_list if ptm['matmul'] not in update_dict]
229 if opt_config:
230 ptm_list = [ptm for ptm in ptm_list if ptm['matmul'] in opt_config]
231 for ptm in ptm_list:
232 if ptm['matmul'] in update_dict:
233 continue
234 w_data = _get_weight_data(ptm['matmul'])
235 if with_bias:
236 bias_data = _get_weight_data(ptm['bias_add'])
237 if w_data is None or (with_bias and bias_data is None):
238 continue
239 node = util.get_node_by_name(graph_def, simple_graph, ptm['matmul'])
240 opt_dtype = opt_config.get(node.name) if opt_config else data_type
241 print(f'Optimize dense op to {opt_dtype}: {node.name}')

Callers 1

optimizeFunction · 0.85

Calls 6

SimpleGraphClass · 0.90
_optimizeFunction · 0.85
remove_redundant_quantsFunction · 0.85
remove_redundant_castsFunction · 0.85
as_graph_defMethod · 0.80
loadMethod · 0.45

Tested by

no test coverage detected