def replace_pattern(graph: Graph, match: [str, Node]): consumers = [ n for n in match if n not in ['mul_add', 'pow_d'] and not check_node_usages_out_of_match(match, n) ] if consumers: log.warning( 'Power(mul_add,pow) pattern was detected. Non pattern consumers of nodes: "{}" were found.' ' Won\'t replace'.format(', '.join( [match[n].id for n in consumers]))) return mul_add = match['mul_add'] pow = match['pow'] new_power = Power( graph, { 'name': mul_add.name + '/fused_power', 'shift': mul_add.shift, 'scale': mul_add.scale, 'power': pow.power }).create_node() source = mul_add.in_port(0).get_connection().get_source() mul_add.in_port(0).disconnect() new_power.in_port(0).connect(source) pow.out_port(0).get_connection().set_source(new_power.out_port(0)) log.debug( 'Power nodes {} and {} were fused to single Power node {}'.format( mul_add.name, pow.name, new_power.name))
def replace_sub_graph(self, graph: Graph, match: dict): consumers = [n for n in match if n not in ['mul', 'op', 'add'] and not check_node_usages_out_of_match(match, n)] if consumers: log.warning('PReLU pattern was detected. Non pattern consumers of nodes: "{}" were found. Won\'t replace' ''.format(', '.join([match[n].id for n in consumers]))) return gamma = match['mul'].in_node(0) if match['mul'].in_node(1).id == match['neg_1'].id else match['mul'].in_node(1) prelu_node = PReLU(graph, {'name': '{}/PReLU'.format(match['add'].id)}).create_node([match['op'], gamma]) match['add'].replace_node(prelu_node) log.debug('PReLU pattern starting from "{}" was collapsed to "{}"'.format(match['op'].id, prelu_node.id))