def gpu_job(i, op, o): """ Convert a cpu job to a gpu job inputs: i - iterable of input variables op - a cpu op o - iterable of output variables outputs: gi - iterable of input variables gop - a gpu op go - iterable of output variables """ node = op.make_node(*i) for v,nv in zip(o, node.outputs): nv.name = v.name gii, goo = cpu_to_gpu_graph(node.inputs, node.outputs) if len(list_of_nodes(gii, goo)) != 1: raise ValueError("We have assumed that translations of single cpu-ops " "would result in single gpu-ops. This computation " "has invalidated that assumption") op = goo[0].owner.op go = map(lambda x: x.clone(), goo) gi = map(lambda x: x.clone(), gii) for v, gv in zip(i+o, gi+go): gv.name = gpu_name(v.name) return gi, op, go
def gpu_job(i, op, o): """ Convert a cpu job to a gpu job inputs: i - iterable of input variables op - a cpu op o - iterable of output variables outputs: gi - iterable of input variables gop - a gpu op go - iterable of output variables """ node = op.make_node(*i) for v, nv in zip(o, node.outputs): nv.name = v.name gii, goo = cpu_to_gpu_graph(node.inputs, node.outputs) if len(list_of_nodes(gii, goo)) != 1: raise ValueError("We have assumed that translations of single cpu-ops " "would result in single gpu-ops. This computation " "has invalidated that assumption") op = goo[0].owner.op go = map(lambda x: x.clone(), goo) gi = map(lambda x: x.clone(), gii) for v, gv in zip(i + o, gi + go): gv.name = gpu_name(v.name) return gi, op, go
def test_list_of_nodes(): r1, r2, r3 = MyVariable(1), MyVariable(2), MyVariable(3) o1 = MyOp(r1, r2) o1.name = "o1" o2 = MyOp(r3, o1) o2.name = "o2" res = list_of_nodes([r1, r2], [o2]) assert res == [o2.owner, o1.owner]
def sort_apply_nodes(inputs, outputs, cmps): """ Order a graph of apply nodes according to a list of comparators The following example sorts first by dependence of nodes (this is a topological sort) and then by lexicographical ordering (nodes that start with 'E' come before nodes that start with 'I' if there is no dependence. >>> from theano.gof.graph import sort_apply_nodes, dependence >>> from theano.tensor import matrix, dot >>> x = matrix('x') >>> y = dot(x*2, x+1) >>> str_cmp = lambda a, b: cmp(str(a), str(b)) # lexicographical sort >>> sort_apply_nodes([x], [y], cmps=[dependence, str_cmp]) [Elemwise{add,no_inplace}(x, InplaceDimShuffle{x,x}.0), InplaceDimShuffle{x,x}(TensorConstant{2}), Elemwise{mul,no_inplace}(x, InplaceDimShuffle{x,x}.0), InplaceDimShuffle{x,x}(TensorConstant{1}), dot(Elemwise{mul,no_inplace}.0, Elemwise{add,no_inplace}.0)] """ return posort(list_of_nodes(inputs, outputs), *cmps)