def local_add_mul_fusion(node):
 if ((not isinstance(node.op, Elemwise)) or (not isinstance(node.op.scalar_op, (scalar.Add, scalar.Mul)))):
  return False
 s_op = node.op.scalar_op.__class__
 new_inp = []
 fused = False
 for inp in node.inputs:
  if (inp.owner and isinstance(inp.owner.op, Elemwise) and isinstance(inp.owner.op.scalar_op, s_op) and (len(inp.clients) == 1)):
   new_inp.extend(inp.owner.inputs)
   fused = True
  else:
   new_inp.append(inp)
 if fused:
  output = node.op(*new_inp)
  copy_stack_trace(node.outputs[0], output)
  if output.owner:
   output2 = local_add_mul_fusion(output.owner)
   if output2:
    return output2
  return [output]

def local_add_mul_fusion ( node ) : if ( ( not isinstance ( node . op , Elemwise ) ) or ( not isinstance ( node . op . scalar_op , ( scalar . Add , scalar . Mul ) ) ) ) : return False s_op = node . op . scalar_op . __class__ new_inp = [ ] fused = False for inp in node . inputs : if ( inp . owner and isinstance ( inp . owner . op , Elemwise ) and isinstance ( inp . owner . op . scalar_op , s_op ) and ( len ( inp . clients ) == 1 ) ) : new_inp . extend ( inp . owner . inputs ) fused = True else : new_inp . append ( inp ) if fused : output = node . op ( * new_inp ) copy_stack_trace ( node . outputs [ 0 ] , output ) if output . owner : output2 = local_add_mul_fusion ( output . owner ) if output2 : return output2 return [ output ]
fuse consecutive add or mul in one such node with more inputs .