Testing fastai

Quick Guide

Most of this document is various notes explaining how to do all kinds of things with the test suite. But if you’re new to the fastai test suite, here is what you need to know to get started.

  • Step 1. Setup and check you can run the test suite:

    1. git clone https://github.com/fastai/fastai1
    2. cd fastai
    3. tools/run-after-git-clone # python toolsrun-after-git-clone on windows
    4. pip install -e ".[dev]"
    5. make test # or pytest
  • Step 2. Run a specific test module and a specific test of that module

    The following will run all tests inside tests/test_vision_transform.py:

    1. pytest -sv tests/test_vision_transform.py

    If you want to run just this test_points_data_aug of that test module:

    1. pytest -sv tests/test_vision_transform.py::test_points_data_aug
  • Step 3. Write a new test, or improve an existing one.

    fastai test modules are named mostly to be the same as the python modules they test, so for example test_vision_transform.py tests fastai/vision/transform.py (but not always).

    Locate an existing test that is similar to what you need, copy it, rename and modify it to test what you feel needs to be tested.

    Let’s assume you took test_points_data_aug and converted it into test_quality in the same module. Test that it works: ``` pytest -sv tests/test_vision_transform.py::test_quality

    1. If it reproduces a problem, i.e. assert fails, then add:

    @pytest.mark.skip(reason=”fix me: brief note describing the problem”) def test_quality(): … ```

    The best way to figure out how to test, is by looking at existing tests. And the rest of this document explains how to do all kinds of things that you might want to do in your tests.

  • Step 4. Submit a PR with your new test(s)

    You won’t be able to PR from this plain checkout, so you need to switch to a forked version of fastai and create a new branch there. Follow the easy instructions here to accomplish that.

    Note that this guide helps you to write tests with a plain git checkout, without needing to fork and branch, so that you can get results faster and easier. But once you’re ready, then switch to your own fork and branch as explained in the guide above. You can just copy the files over to the new branch. Of course, feel free, to start with making a PR branch first - whatever is the easiest for you.

Handy things

Here is a bunch of useful pytest extensions to install (most are discussed somewhere in this document):

  1. pip install pytest-xdist pytest-sugar pytest-repeat pytest-picked pytest-forked pytest-flakefinder pytest-cov nbsmoke

Only pytest-sugar will automatically change pytest’s behavior (in a nice way), so remove it from the list if you don’t like it. All the other extensions need to be explicitly enabled via pytest flag to have an impact, so are safe to install.

Automated tests

At the moment there are only a few automated tests, so we need to start expanding it! It’s not easy to properly automatically test ML code, but there’s lots of opportunities for unit tests.

We use pytest. Here is a complete pytest API reference.

The tests have been configured to automatically run against the fastai directory inside the fastai git repository and not pre-installed fastai. i.e. tests/test_* work with ../fastai.

Running Tests

Choosing which tests to run

Full documentation.

For nuances of configuring pytest’s repo-wide behavior see collection.

Here are some most useful ways of running tests.

Run all

  1. pytest

or:

  1. make test

or:

  1. python setup.py test

Run specific test module

To run an individual test module:

  1. pytest tests/test_core.py

Run specific tests

Run tests by keyword expressions:

  1. pytest -k "list and not listify" tests/test_core.py

For example, if we have the following tests:

  1. def test_whatever():
  2. def test_listify(p, q, expected):
  3. def test_listy():

it will first select test_listify and test_listy, and then deselect test_listify, resulting in only the sub-test test_listy being run.

A more superior way, which avoids unintentional multiple matches is to use the test node approach:

  1. pytest tests/test_basic_train.py::test_save_load tests/test_basic_data.py::test_DataBunch_oneitem

It’s really just the test module followed by the specific test name, joined by ::.

Run only modified tests

Run the tests related to the unstaged files or the current branch (according to Git).

pytest-picked

  1. pip install pytest-picked
  1. pytest --picked

All tests will be run from files and folders which are modified, but not yet committed.

Automatically rerun failed tests on source modification

pytest-xdist provides a very useful feature of detecting all failed tests, and then waiting for you to modify files and continuously re-rerun those failing tests until they pass while you fix them. So that you don’t need to re start pytest after you made the fix. This is repeated until all tests pass after which again a full run is performed.

  1. pip install pytest-xdist

To enter the mode:

  1. pytest -f # or pytest --looponfail

File changes are detected by looking at looponfailingroots root directories and all of their contents (recursively). If the default for this value does not work for you you can change it in your project by setting a configuration option in setup.cfg:

  1. [tool:pytest]
  2. looponfailroots = fastai tests

or pytest.ini or tox.ini files:

  1. [pytest]
  2. looponfailroots = fastai tests

This would lead to only looking for file changes in the respective directories, specified relatively to the ini-file’s directory.

pytest-watch is an alternative implementation of this functionality.

Skip integration tests

To skip the integration tests in order to do quick testing while you work:

  1. pytest --skipint

Skip a test module

If you need to skip a certain test module temporarily you can either tell pytest which tests to run explicitly, so for example to skip any test modules that contain the string link, you could run:

  1. pytest `ls -1 tests/*py | grep -v link`

Clearing state

CI builds and when isolation is important (against speed), cache should be cleared:

  1. pytest --cache-clear tests

Running tests in parallel

This can speed up the total execution time of the test suite.

  1. pip install pytest-xdist
  1. $ time pytest
  2. real 0m51.069s
  3. $ time pytest -n 6
  4. real 0m26.940s

That’s twice the speed of the normal sequential execution!

XXX: We just need to fix the temp files creation to use a unique string (pid?), otherwise at times some tests collide in a race condition over the same temp file path.

Since the order of executed tests is different and unpredictable, if running the test suite with pytest-xdist produces failures (meaning we have some undetected coupled tests), use pytest-replay to replay the tests in the same order, which should help with then somehow reducing that failing sequence to a minimum. Currently there is bisect-like module that can reduce a long sequence of tests that leads to failure to the minimal one.

Test order and repetition

It’s good to repeat the tests several times, in sequence, randomly, or in sets, to detect any potential inter-dependency and state-related bugs (tear down). And the straightforward multiple repetition is just good to detect some problems that get uncovered by randomness of DL.

Plugins:

  • Repeat tests:

    1. pip install pytest-repeat

    Now 2 new options becomes available:

    1. --count=COUNT Number of times to repeat each test
    2. --repeat-scope={function,class,module,session} Scope for repeating tests

    e.g.:

    1. pytest --count=10 tests/test_fastai.py
    1. pytest --count=10 --repeat-scope=function tests

    Here is another similar module pytest-flakefinder:

    1. pip install pytest-flakefinder

    And then run every test multiple times (50 by default):

    1. pytest --flake-finder --flake-runs=5
  • Run tests in a random order:

    1. pip install pytest-random-order

    Important: Presence of pytest-random-order will automatically randomize tests, no configuration change or command line options is required.

    XXX: need to find a package or write our own pytest extension to be able to randomize at will, since the two available modules that do that once installed force the randomization by default.

    As explained earlier this allows detection of coupled tests - where one test’s state affects the state of another. When pytest-random-order is installed it will print the random seed it used for that session, e.g:

    1. pytest tests
    2. [...]
    3. Using --random-order-bucket=module
    4. Using --random-order-seed=573663
    5. [...]

    So that if the given particular sequence fails, you can reproduce it by adding that exact seed, e.g.:

    1. pytest --random-order-seed=573663
    2. [...]
    3. Using --random-order-bucket=module
    4. Using --random-order-seed=573663

    It will only reproduce the exact order if you use the exact same list of tests (or no list at all (==all)). Once you start to manually narrowing down the list you can no longer rely on the seed, but have to list them manually in the exact order they failed and tell pytest to not randomize them instead using --random-order-bucket=none, e.g.:

    1. pytest --random-order-bucket=none tests/test_a.py tests/test_c.py tests/test_b.py

    To disable the shuffling for all tests:

    1. pytest --random-order-bucket=none

    By default --random-order-bucket=module is implied, which will shuffle the files on the module levels. It can also shuffle on class, package, global and none levels. For the complete details please see its documentation.

Randomization alternatives:

  • pytest-randomly

    This module has a very similar functionality/interface, but it doesn’t have the bucket modes available in pytest-random-order. It has the same problem of imposing itself once installed.

Look and feel variations

pytest-sugar

pytest-sugar is a plugin that improves the look-n-feel, adds a progressbar, and show tests that fail and the assert instantly. It gets activated automatically upon installation.

  1. pip install pytest-sugar

To run tests without it, run:

  1. pytest -p no:sugar

or uninstall it.

instantly shows failed tests

pytest-instafail shows failures and errors instantly instead of waiting until the end of test session.

  1. pip install pytest-instafail
  1. pytest --instafail

To GPU or not to GPU

On a GPU-enabled setup, to test in CPU-only mode add CUDA_VISIBLE_DEVICES="":

  1. CUDA_VISIBLE_DEVICES="" pytest tests/test_vision.py

To do the same inside the code of the test:

  1. fastai.torch_core.default_device = torch.device('cpu')

To switch back to cuda:

  1. fastai.torch_core.default_device = torch.device('cuda')

Make sure you don’t hard-code any specific device ids in the test, since different users may have a different GPU setup. So avoid code like:

  1. fastai.torch_core.default_device = torch.device('cuda:1')

which tells torch to use the 2nd GPU. Instead, if you’d like to run a test locally on a different GPU, use the CUDA_VISIBLE_DEVICES environment variable:

  1. CUDA_VISIBLE_DEVICES="1" pytest tests/test_vision.py

Report each sub-test name and its progress

For a single or a group of tests via pytest (after pip install pytest-pspec):

  1. pytest --pspec tests/test_fastai.py
  2. pytest --pspec tests

For all tests via setup.py:

  1. python setup.py test --addopts="--pspec"

This also means that meaningful names for each sub-test are important.

Output capture

During test execution any output sent to stdout and stderr is captured. If a test or a setup method fails, its according captured output will usually be shown along with the failure traceback.

To disable output capturing and to get the stdout and stderr normally, use -s or --capture=no:

  1. pytest -s tests/test_core.py

To send test results to JUnit format output:

  1. py.test tests --junitxml=result.xml

Color control

To have no color (e.g. yellow on white bg is not readable):

  1. pytest --color=no tests/test_core.py

Sending test report to online pastebin service

Creating a URL for each test failure:

  1. pytest --pastebin=failed tests/test_core.py

This will submit test run information to a remote Paste service and provide a URL for each failure. You may select tests as usual or add for example -x if you only want to send one particular failure.

Creating a URL for a whole test session log:

  1. pytest --pastebin=all tests/test_core.py

Writing tests

When writing tests:

  • Avoid mocks; instead, think about how to create a test of the real functionality that runs quickly
  • Use module scope fixtures to run init code that can be shared amongst tests. When using fixtures, make sure the test doesn’t modify the global object it received, otherwise other tests will be impacted. If a given test modifies the global fixture object, it should either clone it or not use the fixture and create a fresh object instead.
  • Avoid pretrained models, since they have to be downloaded from the internet to run the test
  • Create some minimal data for your test, or use data already in repo’s data/ directory

Important: currently, in the test suite we can only use modules that are already in the required dependencies of fastai (i.e. conda dependencies). No other modules are allowed, unless the test is skipped if some new dependency is used.

Test Registry

fastai has a neat feature where users while reading the API documentation can also discover which tests exercise the function they are interested to use. This provides extra insights at how the API can be used, and also provides an incentive to users to write tests which are missing or improving the existing ones. Therefore, every new test should include a single call of this_tests.

The following is an actual test, that tests this_tests, so you can quickly see how it should be used:

  1. from fastai.gen_doc.doctest import this_tests
  2. def test_this_tests():
  3. # function by reference (and self test)
  4. this_tests(this_tests)
  5. # multiple entries: same function twice on purpose, should result in just one entry,
  6. # but also testing multiple entries - and this test tests only a single function.
  7. this_tests(this_tests, this_tests)
  8. import fastai
  9. # explicit fully qualified function (requires all the sub-modules to be loaded)
  10. this_tests(fastai.gen_doc.doctest.this_tests)
  11. # explicit fully qualified function as a string
  12. this_tests('fastai.gen_doc.doctest.this_tests')
  13. # special case for cases where a test doesn't test fastai API
  14. this_tests('na')
  15. # not a real function
  16. func = 'foo bar'
  17. try: this_tests(func)
  18. except Exception as e: assert f"'{func}' is not a function" in str(e)
  19. else: assert False, f'this_tests({func}) should have failed'
  20. # not a function as a string that looks like fastai function, but it is not
  21. func = 'fastai.gen_doc.doctest.doesntexistreally'
  22. try: this_tests(func)
  23. except Exception as e: assert f"'{func}' is not a function" in str(e)
  24. else: assert False, f'this_tests({func}) should have failed'
  25. # not a fastai function
  26. import numpy as np
  27. func = np.any
  28. try: this_tests(func)
  29. except Exception as e: assert f"'{func}' is not in the fastai API" in str(e)
  30. else: assert False, f'this_tests({func}) should have failed'

When you use this function ideally try to use live objects obj.method and not class.method approach, because if the API changes and classes get renamed behind the scenes the test will still work without requiring any modification. Therefore, instead of doing this:

  1. def test_get_preds():
  2. learn = fake_learner()
  3. this_tests(Learner.get_preds)

it’s better to write it as:

  1. def test_get_preds():
  2. learn = fake_learner()
  3. this_tests(learn.get_preds)

You can make the call this_tests anywhere in the test, so if the object becomes available at line 10 of the test, add this_tests after it.

And there is a special case for situations where a test doesn’t test fastai API or it’s a non-callable attribute, e.g. learn.loss_func, in which case use na (not applicable):

  1. def test_non_fastai_func():
  2. this_tests('na')

But we still want the call to be there, since we run a check to make sure we don’t miss out on any tests, hence each test needs to have this call.

The test registry is located at fastai/test_registry.json and it gets auto-generated or updated when pytest is run.

Expensive object reuse

Reusing objects, especially those that take a lot of time to create, helps to keep the test suite fast. If the test suite is slow, it’ll not be run and developers will tend to commit code without testing it first. Therefore, it’s OK to prototype things in a non-efficient way. But once the test is working, please spend extra effort to optimize its speed. Having hundreds of tests, a few extra seconds of unnecessary slowness per test quickly adds up to minutes. And chances are, you won’t want to wait for 20min before you can commit a shiny new code you have just written.

Currently we mostly use module scoped fixtures (global variables scoped to the test module). For example:

  1. @pytest.fixture(scope="module")
  2. def learn():
  3. learn = ... create a learn object ...
  4. return learn

Now we can use it, in multiple tests of that module, by passing the fixture’s function name as an argument to the test function:

  1. def test_opt_params(learn):
  2. learn.freeze()
  3. assert n_params(learn) == 2
  4. def test_val_loss(learn):
  5. assert learn.validate()[1] > 0.3

You can have multiple fixtures and combine them too. For example, in the following code we create 2 fixtures: path and learn, and the learn fixture receives the path argument that is fixture itself, just like a test function will do. And then the example shows how you can pass one or more fixtures to a test function.

  1. @pytest.fixture(scope="module")
  2. def path():
  3. path = untar_data(URLs.MNIST_TINY)
  4. return path
  5. @pytest.fixture(scope="module")
  6. def learn(path):
  7. data = ImageDataBunch.from_folder(path, ds_tfms=([], []), bs=2)
  8. learn = cnn_learner(data, models.resnet18, metrics=accuracy)
  9. return learn
  10. def test_val_loss(learn):
  11. assert learn.validate()[1] > 0.3
  12. def test_path(path):
  13. assert path
  14. def test_something(learn, path):
  15. assert learn.validate()[1] > 0.3
  16. assert path

If we want test-suite global objects, e.g. learn_vision, learn_text, we can pre-create them from conftest.py:

  1. from fastai.vision import *
  2. @pytest.fixture(scope="session", autouse=True)
  3. def learn_vision():
  4. path = untar_data(URLs.MNIST_TINY)
  5. data = ImageDataBunch.from_folder(path, ds_tfms=(rand_pad(2, 28), []), num_workers=2)
  6. data.normalize()
  7. learn = Learner(data, simple_cnn((3,16,16,16,2), bn=True), metrics=[accuracy, error_rate])
  8. learn.fit_one_cycle(3)
  9. return learn

Now, inside, for example, tests/test_vision_train.py we can access the global session-wide fixture in the same way the module-scoped one:

  1. def test_accuracy(learn_vision):
  2. assert accuracy(*learn_vision.get_preds()) > 0.9

If we use:

  1. @pytest.fixture(scope="session", autouse=True)

all global objects will be pre-created no matter whether the running tests need them or not, so we probably don’t want autouse=True. Without this setting these fixture objects will be created on demand.

There is a cosmetic issue with having learn_vision, learn_text, since now we either have to spell out:

  1. def test_accuracy(learn_vision):
  2. assert accuracy(*learn_vision.get_preds()) > 0.9

or rename:

  1. def test_accuracy(learn_vision):
  2. learn = learn_vision
  3. assert accuracy(*learn.get_preds()) > 0.9

both aren’t very great…

We want to be able to copy-n-paste quickly and ideally it should always be learn.foo, especially since there are many calls usually.

Another important nuance related to fixtures is that those global objects shouldn’t get modified by tests. If they do this can impact other tests that rely on a freshly created object. If that’s the case, let the test create its own object and do anything it wants with it. For example, our most commonly used learn object is almost guaranteed to be modified by any method that calls it. If, however, you’re reusing global variables that don’t get modified, as in this example:

  1. @pytest.fixture(scope="module")
  2. def path():
  3. path = untar_data(URLs.MNIST_TINY)
  4. return path

then there is nothing to worry about.

Skipping tests

This is useful when a bug is found and a new test is written, yet the bug is not fixed yet. In order to be able to commit it to the main repository we need make sure it’s skipped during make test.

Methods:

  • A skip means that you expect your test to pass only if some conditions are met, otherwise pytest should skip running the test altogether. Common examples are skipping windows-only tests on non-windows platforms, or skipping tests that depend on an external resource which is not available at the moment (for example a database).

  • A xfail means that you expect a test to fail for some reason. A common example is a test for a feature not yet implemented, or a bug not yet fixed. When a test passes despite being expected to fail (marked with pytest.mark.xfail), it’s an xpass and will be reported in the test summary.

One of the important differences between the two is that skip doesn’t run the test, and xfail does. So if the code that’s buggy causes some bad state that will affect other tests, do not use xfail.

Implementation:

  • The whole test unconditionally:

    1. @pytest.mark.skip(reason="this bug needs to be fixed")
    2. def test_feature_x():

    or the xfail way:

    1. @pytest.mark.xfail
    2. def test_feature_x():
  • Based on some internal check inside the test:

    1. def test_feature_x():
    2. if not has_something(): pytest.skip("unsupported configuration")

    or the whole module:

    1. import pytest
    2. if not pytest.config.getoption("--custom-flag"):
    3. pytest.skip("--custom-flag is missing, skipping tests", allow_module_level=True)

    or the xfail way:

    1. def test_feature_x():
    2. pytest.xfail("expected to fail until bug XYZ is fixed")
  • Skip all tests in a module if some import is missing:

    1. docutils = pytest.importorskip("docutils", minversion="0.3")
  • Skip if

    1. import sys
    2. @pytest.mark.skipif(sys.version_info < (3,6),
    3. reason="requires python3.6 or higher")
    4. def test_feature_x():

    or the whole module:

    1. @pytest.mark.skipif(sys.platform == 'win32',
    2. reason="does not run on windows")
    3. class TestPosixCalls(object):
    4. def test_feature_x(self):

More details, example and ways are here.

Custom markers

Normally, you should be able to declare a test as:

  1. import pytest
  2. @pytest.mark.mymarker
  3. def test_mytest(): ...

You can then restrict a test run to only run tests marked with mymarker:

  1. pytest -v -m mymarker

Running all tests except the mymarker ones:

  1. $ pytest -v -m "not mymarker"

Custom markers should be registered in setup.cfg, for example:

  1. [tool:pytest]
  2. # force all used markers to be registered here with an explanation
  3. addopts = --strict
  4. markers =
  5. marker1: description of its purpose
  6. marker2: description of its purpose

fastai custom markers

These are defined in tests/conftest.py.

The following markers override normal marker functionality, so they won’t work with:

  1. pytest -m marker

and may have their own command line option to be used instead, which are defined in tests/conftest.py, and can also be seen in the output of pytest -h in the “custom options” section:

  1. custom options:
  2. --runslow run slow tests
  3. --runcpp run cuda cpp extension tests
  4. --skipint skip integration tests
  • slow - skip tests that can be quite slow (especially on CPU):

    1. @pytest.mark.slow
    2. def test_some_slow_test(): ...

    To force this kind of tests to run, use:

    1. pytest --runslow
  • integration - used for tests that are relatively slow but OK to be run on CPU and useful when one needs to finish the tests suite asap (also remember to use parallel testing if that’s the case xdist). These are usually declared on the test module level, by adding at the top of the file:

    1. pytestmark = pytest.mark.integration

    And to skip those use:

    1. pytest --skipint
  • cuda - mark tests as requiring a CUDA device to run (skipped if no such device is present). These tests check CUDA-specific code, e.g., compiling and running kernels or GPU version of function’s forward/backward methods. Example:

    1. @pytest.mark.cuda
    2. def test_cuda_something(): pass

After test cleanup

To ensure some cleanup code is always run at the end of the test module, add to the desired test module the following code:

  1. @pytest.fixture(scope="module", autouse=True)
  2. def cleanup(request):
  3. """Cleanup the tmp file once we are finished."""
  4. def remove_tmp_file():
  5. file = "foobar.tmp"
  6. if os.path.exists(file): os.remove(file)
  7. request.addfinalizer(remove_tmp_file)

The autouse=True tells pytest to run this fixture automatically (without being called anywhere else).

Use scope="session" to run the teardown code not at the end of this test module, but after all test modules were run, i.e. just before pytest exits.

Another way to accomplish the global teardown is to put in tests/conftest.py:

  1. def pytest_sessionfinish(session, exitstatus):
  2. # global tear down code goes here

To run something before and after each test, add to the test module:

  1. @pytest.fixture(autouse=True)
  2. def run_around_tests():
  3. # Code that will run before your test, for example:
  4. some_setup()
  5. # A test function will be run at this point
  6. yield
  7. # Code that will run after your test, for example:
  8. some_teardown()

autouse=True makes this function run for each test defined in the same module automatically.

For creation/teardown of temporary resources for the scope of a test, do the same as above, except get yield to return that resource.

  1. @pytest.fixture(scope="module")
  2. def learner_obj():
  3. # Code that will run before your test, for example:
  4. learn = Learner(...)
  5. # A test function will be run at this point
  6. yield learn
  7. # Code that will run after your test, for example:
  8. del learn

You can now use that function as an argument to a test function:

  1. def test_foo(learner_obj):
  2. learner_obj.fit(...)

Testing the stdout/stderr output

In order to test functions that write to stdout and/or stderr, the test can access those streams using the pytest’s capsys system. Here is how this is accomplished:

  1. import sys
  2. def print_to_stdout(s): print(s)
  3. def print_to_stderr(s): sys.stderr.write(s)
  4. def test_result_and_stdout(capsys):
  5. msg = "Hello"
  6. print_to_stdout(msg)
  7. print_to_stderr(msg)
  8. out, err = capsys.readouterr() # consume the captured output streams
  9. # optional: if you want to replay the consumed streams:
  10. sys.stdout.write(out)
  11. sys.stderr.write(err)
  12. # test:
  13. assert msg in out
  14. assert msg in err

And, of course, most of the time, stderr will come as a part of an exception, so try/except has to be used in such a case:

  1. def raise_exception(msg): raise ValueError(msg)
  2. def test_something_exception():
  3. msg = "Not a good value"
  4. error = ''
  5. try: raise_exception(msg)
  6. except Exception as e:
  7. error = str(e)
  8. assert msg in error, f"{msg} is in the exception:n{error}"

Another approach to capturing stdout, is via contextlib.redirect_stdout:

  1. from io import StringIO
  2. from contextlib import redirect_stdout
  3. def print_to_stdout(s): print(s)
  4. def test_result_and_stdout():
  5. msg = "Hello"
  6. buffer = StringIO()
  7. with redirect_stdout(buffer): print_to_stdout(msg)
  8. out = buffer.getvalue()
  9. # optional: if you want to replay the consumed streams:
  10. sys.stdout.write(out)
  11. # test:
  12. assert msg in out

An important potential issue with capturing stdout is that it may contain r characters that in normal print reset everything that has been printed so far. There is no problem with pytest, but with pytest -s these characters get included in the buffer, so to be able to have the test run w/ and w/o -s, you have to make an extra cleanup to the captured output, using re.sub(r'^.*r', '', buf, 0, re.M). You can use a test helper function for that:

  1. from utils.text import *
  2. output = apply_print_resets(output)

But, then we have a helper context manager wrapper to automatically take care of it all, regardless of whether it has some rs in it or not, so it’s a simple:

  1. from utils.text import *
  2. with CaptureStdout() as cs: function_that_writes_to_stdout()
  3. print(cs.out)

Here is a full test example:

  1. from utils.text import *
  2. msg = "Secret messager"
  3. final = "Hello World"
  4. with CaptureStdout() as cs: print(msg + final)
  5. assert cs.out == final+"n", f"captured: {cs.out}, expecting {final}"

If you’d like to capture stderr use the CaptureStderr class instead:

  1. from utils.text import *
  2. with CaptureStderr() as cs: function_that_writes_to_stderr()
  3. print(cs.err)

If you need to capture both streams at once, use the parent CaptureStd class:

  1. from utils.text import *
  2. with CaptureStd() as cs: function_that_writes_to_stdout_and_stderr()
  3. print(cs.err, cs.out)

Testing memory leaks

This section is currently focused on GPU RAM since it’s the scarce resource, but we should test general RAM too.

Utils

  • Memory measuring helper utils are found in tests/utils/mem.py:

    1. from utils.mem import *
  • Test whether we can use GPU:

    1. use_gpu = torch.cuda.is_available()

    torch.cuda.is_available() checks if we can use NVIDIA GPU. It automatically handles the case when CUDA_VISIBLE_DEVICES=”” env var is set, so even if CUDA is available it will return False, thus we can emulate non-CUDA environment.

  • Force pytorch to preload cuDNN and its kernels to claim unreclaimable memory (~0.5GB) if it hasn’t done so already, so that we get correct measurements. This must run before any tests that measure GPU RAM. If you don’t run it you will get erratic behavior and wrong measurements.

    1. torch_preload_mem()
  • Consume some GPU RAM:

    1. gpu_mem_consume_some(n)

    n is the size of the matrix of torch.ones. When n=2**14 it consumes about 1GB, but that’s too much for the test suite, so use small numbers, e.g.: n=2000 consumes about 16MB.

  • alias for torch.cuda.empty_cache()

    1. gpu_cache_clear()

    It’s absolutely essential to run this one, if you’re trying to measure real used memory. If cache doesn’t get cleared the reported used/free memory can be quite inconsistent.

  • This is a combination of gc.collect() and torch.cuda.empty_cache()

    1. gpu_mem_reclaim()

    Again, this one is crucial for measuring the memory usage correctly. While normal objects get destroyed and their memory becomes available/cached right away, objects with circular references only get freed up when python invokes gc.collect, which happens periodically. So if you want to make sure your test doesn’t get caught in the inconsistency of getting gc.collect to be called during that test or not, call it yourself. But, remember, that if you have to call gc.collect() there could be a problem that you will be masking by calling it. So before using it understand what it is doing.

    After gc.collect() is called this functions clears the cache that potentially grew due to the released by gc objects, and we want to make sure we get the real used/free memory at all times.

  • This is a wrapper for getting the used memory for the currently selected device.

    1. gpu_mem_get_used()

Concepts

  • Taking into account cached memory and unpredictable gc.collect calls. See above.

  • Memory fluctuations. When measuring either general or GPU RAM there is often a small fluctuation in reported numbers, so when writing tests use functions that approximate equality, but do think deep about the margin you allow, so that the test is useful and yet it doesn’t fail at random times.

    Also remember that rounding happens when Bs are converted to MBs.

    Here is an example:

    1. from math import isclose
    2. used_before = gpu_mem_get_used()
    3. ... some gpu consuming code here ...
    4. used_after = gpu_mem_get_used()
    5. assert isclose(used_before, used_after, abs_tol=6), "testing absolute tolerance"
    6. assert isclose(used_before, used_after, rel_tol=0.02), "testing relative tolerance"

    This example compares used memory size (in MBs). The first assert compares whether the absolute difference between the two numbers is no more than 6. The second assert does the same but uses a relative tolerance in percents – 0.02 in the example means 2%. So the accepted difference between the two numbers is no more than 2%. Often absolute numbers provide a better test, because a percent-based approach could result in quite a large gap if the numbers are big.

Getting reproducible results

In some situations you may want to remove randomness for your tests. To get identical reproducable results set, you’ll need to set num_workers=1 (or 0) in your DataLoader/DataBunch, and depending on whether you are using torch’s random functions, or python’s (numpy) or both:

  1. seed = 42
  2. # python RNG
  3. import random
  4. random.seed(seed)
  5. # pytorch RNGs
  6. import torch
  7. torch.manual_seed(seed)
  8. torch.backends.cudnn.deterministic = True
  9. if torch.cuda.is_available(): torch.cuda.manual_seed_all(seed)
  10. # numpy RNG
  11. import numpy as np
  12. np.random.seed(seed)

Debugging tests

To start a debugger at the point of the warning, do this:

  1. pytest tests/test_vision_data_block.py -W error::UserWarning --pdb

Tests requiring jupyter notebook environment

If pytest-ipynb pytest extension is installed it’s possible to add .ipynb files to the normal test suite.

Basically, you just write a normal notebook with asserts, and pytest just runs it, along with normal .py tests, reporting any assert failures normally.

We currently don’t have such tests, and if we add any, we will first need to make a conda package for it on the fastai channel, and then add this dependency to fastai. (note: I haven’t researched deeply, perhaps there are other alternatives)

Here is one example of such test.

Coverage

When you run:

  1. make coverage

it will run the test suite directly via pytest and on completion open a browser to show you the coverage report, which will give you an indication of which parts of the code base haven’t been exercised by tests yet. So if you are not sure which new tests to write this output can be of great insight.

Remember, that coverage only indicated which parts of the code tests have exercised. It can’t tell anything about the quality of the tests. As such, you may have a 100% coverage and a very poorly performing code.

Notebook integration tests

The two places you should check for notebooks to test your code with are:

In each case, look for notebooks that have names starting with the application you’re working on - e.g. ‘text’ or ‘vision’.

docs_src/*ipynb

The docs_src notebooks can be executed as a test suite: You need to have at least 8GB available on your GPU to run all of the tests. So make sure you shutdown any unnecessary jupyter kernels, so that the output of your nvidia-smi shows that you have at least 8GB free.

  1. cd docs_src
  2. ./run_tests.sh

To run a subset:

  1. ./run_tests.sh callback*

There are a lot more details on this subject matter in this document.

examples/*ipynb

You can run each of these interactively in jupyter, or as CLI:

  1. jupyter nbconvert --execute --ExecutePreprocessor.timeout=600 --to notebook examples/tabular.ipynb

This set is examples and there is no pass/fail other than visual observation.


Company logo

©2021 fast.ai. All rights reserved.
Site last generated: Jan 5, 2021