Esempio n. 1
0
def test_dataflow_timer_handlers_profiler():
    true_dataflow_time_per_ele = 0.1
    true_max_epochs = 1
    true_num_iters = 2

    def dummy_data_loader(data):
        while True:
            for d in data:
                time.sleep(true_dataflow_time_per_ele)
                yield d

    dummy_data = range(true_num_iters)

    profiler = HandlersTimeProfiler()
    dummy_trainer = Engine(_do_nothing_update_fn)
    profiler.attach(dummy_trainer)
    dummy_trainer.run(dummy_data_loader(dummy_data),
                      max_epochs=true_max_epochs,
                      epoch_length=true_num_iters)
    results = profiler.get_results()
    dataflow_results = results[-1]

    assert dataflow_results[0] == "Dataflow"
    # event name
    assert dataflow_results[1] == "None"
    # total
    assert dataflow_results[2] == approx(true_num_iters *
                                         true_dataflow_time_per_ele,
                                         abs=1e-1)
    # min
    assert dataflow_results[3][0] == approx(true_dataflow_time_per_ele,
                                            abs=1e-1)
    # max
    assert dataflow_results[4][0] == approx(true_dataflow_time_per_ele,
                                            abs=1e-1)
    # mean
    assert dataflow_results[5] == approx(true_dataflow_time_per_ele, abs=1e-1)
    # stddev
    assert dataflow_results[6] == approx(0.0, abs=1e-1)
Esempio n. 2
0
def test_event_handler_get_batch_completed_handlers_profiler():
    true_event_handler_time = 0.1
    true_max_epochs = 1
    true_num_iters = 2

    profiler = HandlersTimeProfiler()
    dummy_trainer = Engine(_do_nothing_update_fn)
    profiler.attach(dummy_trainer)

    @dummy_trainer.on(Events.GET_BATCH_COMPLETED)
    def delay_get_batch_completed(engine):
        time.sleep(true_event_handler_time)

    dummy_trainer.run(range(true_num_iters), max_epochs=true_max_epochs)
    results = profiler.get_results()
    event_results = results[0]
    assert "delay_get_batch_completed" in event_results[0]
    assert event_results[1] == "GET_BATCH_COMPLETED"

    assert event_results[2] == approx(true_max_epochs * true_num_iters * true_event_handler_time, abs=1e-1)  # total
    assert event_results[3][0] == approx(true_event_handler_time, abs=1e-1)  # min
    assert event_results[4][0] == approx(true_event_handler_time, abs=1e-1)  # max
    assert event_results[5] == approx(true_event_handler_time, abs=1e-1)  # mean
    assert event_results[6] == approx(0.0, abs=1e-1)  # stddev
Esempio n. 3
0
def test_profilers_wrong_inputs():
    profiler = BasicTimeProfiler()
    with pytest.raises(TypeError, match=r"Argument engine should be ignite.engine.Engine"):
        profiler.attach(None)

    with pytest.raises(RuntimeError, match=r"Need pandas to write results as files"):
        with patch.dict("sys.modules", {"pandas": None}):
            profiler.write_results("")

    profiler = HandlersTimeProfiler()
    with pytest.raises(TypeError, match=r"Argument engine should be ignite.engine.Engine"):
        profiler.attach(None)

    with pytest.raises(RuntimeError, match=r"Need pandas to write results as files"):
        with patch.dict("sys.modules", {"pandas": None}):
            profiler.write_results("")
Esempio n. 4
0
def test_print_results_handlers_profiler_handlers_profiler(capsys):

    true_max_epochs = 1
    true_num_iters = 5

    profiler = HandlersTimeProfiler()
    dummy_trainer, _, _ = get_prepared_engine_for_handlers_profiler(true_event_handler_time=0.0125)
    profiler.attach(dummy_trainer)

    dummy_trainer.run(range(true_num_iters), max_epochs=true_max_epochs)
    HandlersTimeProfiler.print_results(profiler.get_results())

    captured = capsys.readouterr()
    out = captured.out
    assert "HandlersTimeProfiler." not in out
    assert "Timer." not in out
Esempio n. 5
0
def test_write_results_handlers_profiler(dirname):
    true_event_handler_time = 0.125
    true_max_epochs = 3
    true_num_iters = 2

    profiler = HandlersTimeProfiler()
    dummy_trainer, _, _ = get_prepared_engine_for_handlers_profiler(true_event_handler_time)
    profiler.attach(dummy_trainer)

    dummy_trainer.run(range(true_num_iters), max_epochs=true_max_epochs)
    fp = dirname / "test_log.csv"
    profiler.write_results(fp)

    assert fp.is_file()

    file_length = 0
    with open(fp) as f:
        for _ in f:
            file_length += 1

    assert file_length == (true_max_epochs * true_num_iters) + 1