Rewrite graph to combine similarly-shaped variables (faster startup).
545658
def autostack(self):
    """"""
    num_slices = 0
    for v in self.graph.all_variables:
      num_slices += self.mesh_to_impl[v.mesh].size
    if num_slices >= 2 ** 16:
      # Startup times are slow with lots of variable slices.
      # Perform more aggressive stacking
      max_combined_slice_size = 2 ** 27
    else:
      # Stacking hurts memory utilization - only stack small variables.
      max_combined_slice_size = 2 ** 16
    self.graph.rewrite_stack_variables(
        mesh_to_impl=self.mesh_to_impl,
        max_combined_slice_size=max_combined_slice_size)
493279
def maybe_with_graph(graph=None, create_if_none=True):
  """"""
  if tf.executing_eagerly():
    yield None
  else:
    if graph is None and create_if_none:
      graph = tf.Graph()

    if graph is None:
      yield None
    else:
      with graph.as_default():
        yield graph
659314
def _merge_cfgnodes(self, cfgnode_0, cfgnode_1):
        

        assert cfgnode_0.addr + cfgnode_0.size == cfgnode_1.addr
        addr0, addr1 = cfgnode_0.addr, cfgnode_1.addr
        new_node = cfgnode_0.merge(cfgnode_1)

        # Update the graph and the nodes dict accordingly
        if addr1 in self._nodes_by_addr:
            self._nodes_by_addr[addr1].remove(cfgnode_1)
            if not self._nodes_by_addr[addr1]:
                del self._nodes_by_addr[addr1]
        del self._nodes[cfgnode_1.block_id]

        self._nodes_by_addr[addr0].remove(cfgnode_0)
        if not self._nodes_by_addr[addr0]:
            del self._nodes_by_addr[addr0]
        del self._nodes[cfgnode_0.block_id]

        in_edges = list(self.graph.in_edges(cfgnode_0, data=True))
        out_edges = list(self.graph.out_edges(cfgnode_1, data=True))

        self.graph.remove_node(cfgnode_0)
        self.graph.remove_node(cfgnode_1)

        self.graph.add_node(new_node)
        for src, _, data in in_edges:
            self.graph.add_edge(src, new_node, **data)
        for _, dst, data in out_edges:
            self.graph.add_edge(new_node, dst, **data)

        # Put the new node into node dicts
        self._nodes[new_node.block_id] = new_node
        self._nodes_by_addr[addr0].append(new_node)
1100357
def new_graph(self, graph, typ):
        """"""
        graph = self.pack(graph)
        return self.sql('new_graph', graph, typ)
291980
def generate_np(self, x_val, **kwargs):
    
    _, feedable, _feedable_types, hash_key = self.construct_variables(kwargs)

    if hash_key not in self.graphs:
      with tf.variable_scope(None, 'attack_%d' % len(self.graphs)):
        # x is a special placeholder we always want to have
        with tf.device('/gpu:0'):
          x = tf.placeholder(tf.float32, shape=x_val.shape, name='x')

        inputs, outputs = self.generate(x, **kwargs)

        from runner import RunnerMultiGPU
        runner = RunnerMultiGPU(inputs, outputs, sess=self.sess)
        self.graphs[hash_key] = runner

    runner = self.graphs[hash_key]
    feed_dict = {'x': x_val}
    for name in feedable:
      feed_dict[name] = feedable[name]
    fvals = runner.run(feed_dict)
    while not runner.is_finished():
      fvals = runner.run()

    return fvals['adv_x']
1118291
def write_csv(graph, prefix):
    
    node_headers = list(set([a for n, attrs in graph.nodes(data=True)
                             for a in attrs.keys()]))
    edge_headers = list(set([a for s, t, attrs in graph.edges(data=True)
                             for a in attrs.keys()]))

    value = lambda attrs, h: _recast_value(attrs[h]) if h in attrs else ''
    with open(prefix + '_nodes.csv', 'w') as f:
        writer = csv.writer(f)
        writer.writerow(['node'] + node_headers)
        for n, attrs in graph.nodes(data=True):
            values = map(value, repeat(attrs, len(node_headers)), node_headers)
            writer.writerow([_recast_value(n)] + list(values))

    with open(prefix + '_edges.csv', 'w') as f:
        writer = csv.writer(f)
        writer.writerow(['source', 'target'] + edge_headers)
        for s, t, attrs in graph.edges(data=True):
            values = map(value, repeat(attrs, len(edge_headers)), edge_headers)
            writer.writerow([_recast_value(s), _recast_value(t)] + list(values))
78760
def flatten(self, condition=None, start=None):
        
        if start is None:
            start = self
        start = self.getRawIdent(start)
        return self.graph.iterdata(start=start, condition=condition)
284336
def reshape_like(a, b):
  """"""
  ret = tf.reshape(a, tf.concat([tf.shape(b)[:-1], tf.shape(a)[-1:]], 0))
  if not tf.executing_eagerly():
    ret.set_shape(b.get_shape().as_list()[:-1] + a.get_shape().as_list()[-1:])
  return ret
127260
private List<PathEdge> combine(final List<PathEdge> edges, final int x) {

        final int n = edges.size();
        final List<PathEdge> reduced = new ArrayList<PathEdge>(n);

        for (int i = 0; i < n; i++) {
            PathEdge e = edges.get(i);
            for (int j = i + 1; j < n; j++) {
                PathEdge f = edges.get(j);
                if (e.disjoint(f)) reduced.add(new ReducedEdge(e, f, x));
            }
        }

        return reduced;
    }
169318
def merge_vertices(self):
        """"""

        # groupby expects sorted data
        sorted_vertices = sorted(list(self.vertices.items()), key=lambda v: hash(v[1]))
        groups = []
        for k, g in groupby(sorted_vertices, lambda v: hash(v[1])):
            groups.append(list(g))
        for group in groups:
            if len(group) == 1:
                continue
            names = [v[0] for v in group]
            self.reduce_vertex(*names)
764038
def assemble_graph():
    """"""
    if request.method == 'OPTIONS':
        return {}
    response = request.body.read().decode('utf-8')
    body = json.loads(response)
    stmts_json = body.get('statements')
    stmts = stmts_from_json(stmts_json)
    ga = GraphAssembler(stmts)
    model_str = ga.make_model()
    res = {'model': model_str}
    return res
585244
def _combine(self, applied, shortcut=False):
        """"""
        applied_example, applied = peek_at(applied)
        coord, dim, positions = self._infer_concat_args(applied_example)
        if shortcut:
            combined = self._concat_shortcut(applied, dim, positions)
        else:
            combined = concat(applied, dim)
            combined = _maybe_reorder(combined, dim, positions)

        if isinstance(combined, type(self._obj)):
            # only restore dimension order for arrays
            combined = self._restore_dim_order(combined)
        if coord is not None:
            if shortcut:
                combined._coords[coord.name] = as_variable(coord)
            else:
                combined.coords[coord.name] = coord
        combined = self._maybe_restore_empty_groups(combined)
        combined = self._maybe_unstack(combined)
        return combined
178250
def import_graph(self, t_input=None, scope='import', forget_xy_shape=True):
    """"""
    graph = tf.get_default_graph()
    assert graph.unique_name(scope, False) == scope, (
        'Scope "%s" already exists. Provide explicit scope names when '
        'importing multiple instances of the model.') % scope
    t_input, t_prep_input = self.create_input(t_input, forget_xy_shape)
    tf.import_graph_def(
        self.graph_def, {self.input_name: t_prep_input}, name=scope)
    self.post_import(scope)
853461
def nt_yielder(self, graph, size):
        
        for grp in self.make_batch(size, graph):
            tmpg = Graph()
            # Add statements as list to tmp graph
            tmpg += grp
            yield (len(tmpg), tmpg.serialize(format='nt'))
409014
function combineStatsAndIndexes(done, results) {
  var indexes = results.getIndexes;
  var stats = results.getIndexStats;
  var sizes = results.getIndexSizes;
  _.each(indexes, function(idx, i) {
    _.assign(indexes[i], stats[idx.name]);
    _.assign(indexes[i], sizes[idx.name]);
  });
  done(null, indexes);
}
684839
def draw(graph, fname):
    """"""
    ag = networkx.nx_agraph.to_agraph(graph)
    ag.draw(fname, prog='dot')
238925
def dict_to_nx(graph, oriented=False):
    """"""
    nxg = nx.DiGraph() if oriented else nx.Graph()
    for node, succs in graph.items():
        for succ in succs:
            nxg.add_edge(node, succ)
    return nxg