def test_conditional_with_length(): def f(a): return a, a with pf.Graph() as graph: x = pf.constant(4) y = pf.placeholder(name='y') condition = pf.placeholder(name='condition') z1, z2 = pf.conditional(condition, pf.func_op(f, x), pf.func_op(f, y), length=2) assert graph([z1, z2], condition=True) == (4, 4) assert graph([z1, z2], condition=False, y=5) == (5, 5)
def test_try(context, expected): finally_reached = [] with pf.Graph() as graph: a = pf.placeholder('a') b = pf.placeholder('b') c = pf.try_(a / b, [(ZeroDivisionError, 'zero-division')], pf.func_op(lambda: finally_reached.append('done'))) assert graph(c, context) == expected assert finally_reached
def workers(broker): with pf.Graph() as graph: x = pf.placeholder('x') y = pf.placeholder('y') sleep = pf.func_op(time.sleep, pf.func_op(random.uniform, 0, .1)) with pf.control_dependencies([sleep]): (x / y).set_name('z') # Can't pickle entire modules pf.constant(pf).set_name('not_pickleable') # Create multiple workers _workers = [] while len(_workers) < 10: worker = pfmq.Worker.from_graph(graph, broker.backend_address) worker.run_async() _workers.append(worker) yield _workers # Shut down all the workers for worker in _workers: worker.cancel()
def test_consistent_context(): with pf.Graph() as graph: uniform = pf.func_op(random.uniform, 0, 1) scaled = uniform * 4 _uniform, _scaled = graph([uniform, scaled]) assert _scaled == 4 * _uniform # ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~ # @slipform() def graph(): uniform = random.uniform(0, 1) scaled = uniform * 4 _uniform, _scaled = graph(['uniform', 'scaled']) assert _scaled == 4 * _uniform
def test_consistent_context(): with pf.Graph() as graph: uniform = pf.func_op(random.uniform, 0, 1) scaled = uniform * 4 _uniform, _scaled = graph([uniform, scaled]) assert _scaled == 4 * _uniform
def generate_comparisons(): comparisons = zip(ops, zip([left] + comparators, comparators)) for (con, (lft, rht)) in comparisons: yield pf.func_op(con, lft, rht)
def _(foo, lhs=None, graph=None): left = unwind(foo.left, lhs=lhs, graph=graph) right = unwind(foo.right, lhs=lhs, graph=graph) op = unwind(foo.op, lhs=lhs, graph=graph) return pf.func_op(op, left, right)
def _(call, lhs=None, graph=None): global FUNCTIONS fn = FUNCTIONS[resolve_name(call.func)] args = unwind(call.args, lhs=lhs, graph=graph) return pf.func_op(fn, *args)
def create_benchmark_graph(): with pf.Graph() as graph: pf.constant(None, name='fetch') seconds = pf.placeholder('seconds') pf.func_op(time.sleep, seconds, name='sleep') return graph