def test_conditional_callback(): with pf.Graph() as graph: a = pf.constant(1) b = pf.constant(2) c = pf.placeholder() d = pf.conditional(c, a, b + 1) # Check that we have "traced" the correct number of operation evaluations tracer = pf.Profiler() assert graph(d, {c: True}, callback=tracer) == 1 assert len(tracer.times) == 2 tracer = pf.Profiler() assert graph(d, {c: False}, callback=tracer) == 3 assert len(tracer.times) == 3
def test_try_callback(): with pf.Graph() as graph: a = pf.placeholder('a') b = pf.assert_((a > 0).set_name('condition'), value=a, name='b') c = pf.try_( b, [(AssertionError, (pf.constant(41, name='41') + 1).set_name('alternative'))]) tracer = pf.Profiler() graph(c, {a: 3}, callback=tracer) == 3 assert len(tracer.times) == 3 graph(c, {a: -2}, callback=tracer) == 42 assert len(tracer.times) == 5
def test_profiling(): with pf.Graph() as graph: duration = pf.placeholder('duration') # Use a conditional to make sure we also test the evaluation of conditionals with callbacks positive_duration = pf.conditional(duration < 0, 0, duration) sleep = pf.import_('time').sleep(positive_duration) callback = pf.Profiler() graph(sleep, callback=callback, duration=0.5) assert callback.times[sleep] > 0.5 # We never evaluate duration because it is already provided in the context assert duration not in callback.times # Check that the slowest operation comes first callback_str = str(callback) assert callback_str.startswith(str(sleep)) assert len(callback_str.split('\n')) == 5