Skip to content

Commit

Permalink
[TESTS] Refactor tests to run on either the GPU or CPU.
Browse files Browse the repository at this point in the history
Much of the time spent in testing is duplicated work between CPU and GPU
test nodes. The main reason is that there is no way to control which
TVM devices are enabled at runtime, so tests that use LLVM will run on
both GPU and CPU nodes.

This patch adds an environment variable, TVM_TEST_DEVICES, which
controls which TVM devices should be used by tests. Devices not in
TVM_TEST_DEVICES can still be used, so tests must be careful to check
that the desired device is enabled with `tvm.testing.device_enabled` or
by enumerating all devices with `tvm.testing.enabled_devices`. All
tests have been retrofitted with these checks.

This patch also provides the decorator `@tvm.testing.gpu` to mark a test
as possibly using the gpu. Tests that require the gpu can use
`@tvm.testing.requires_gpu`. Tests without these flags will not be run
on GPU nodes.
  • Loading branch information
tkonolige authored and Tristan Konolige committed Sep 2, 2020
1 parent f56bb71 commit 574a7dd
Show file tree
Hide file tree
Showing 154 changed files with 2,214 additions and 1,598 deletions.
5 changes: 3 additions & 2 deletions apps/extension/tests/test_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import tvm_ext
import tvm
import tvm._ffi.registry
import tvm.testing
from tvm import te
import numpy as np

Expand All @@ -32,7 +33,7 @@ def test_ext_dev():
B = te.compute((n,), lambda *i: A(*i) + 1.0, name='B')
s = te.create_schedule(B.op)
def check_llvm():
if not tvm.runtime.enabled("llvm"):
if not tvm.testing.device_enabled("llvm"):
return
f = tvm.build(s, [A, B], "ext_dev", "llvm")
ctx = tvm.ext_dev(0)
Expand Down Expand Up @@ -77,7 +78,7 @@ def test_extern_call():
s = te.create_schedule(B.op)

def check_llvm():
if not tvm.runtime.enabled("llvm"):
if not tvm.testing.device_enabled("llvm"):
return
f = tvm.build(s, [A, B], "llvm")
ctx = tvm.cpu(0)
Expand Down
29 changes: 29 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import tvm.testing
from pytest import ExitCode

def pytest_configure(config):
print("enabled targets:", "; ".join(map(lambda x: x[0], tvm.testing.enabled_targets())))
print("pytest marker:", config.option.markexpr)

def pytest_sessionfinish(session, exitstatus):
# Don't exit with an error if we select a subset of tests that doesn't
# include anything
if session.config.option.markexpr != '':
if exitstatus == ExitCode.NO_TESTS_COLLECTED:
session.exitstatus = ExitCode.OK
14 changes: 14 additions & 0 deletions docs/contribute/code_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ Python Code Styles
- Stick to language features as in ``python 3.5``


Writing Python Tests
--------------------
We use `pytest <https://docs.pytest.org/en/stable/>`_ for all python testing. ``tests/python`` contains all the tests.

If you want your test to run over a variety of targets, use the :py:func:`tvm.testing.parametrize_targets` decorator. For example:

.. code:: python
@tvm.testing.parametrize_targets
def test_mytest(target, ctx):
...
will run `test_mytest` with `target="llvm"`, `target="cuda"`, and few others. This also ensures that your test is run on the correct hardware by the CI. If you only want to test against a couple targets use `@tvm.testing.parametrize_targets("target_1", "target_2")`. If you want to test on a single target, use the associated decorator from :py:func:`tvm.testing`. For example, CUDA tests use the `@tvm.testing.requires_cuda` decorator.

Handle Integer Constant Expression
----------------------------------
We often need to handle constant integer expressions in TVM. Before we do so, the first question we want to ask is that is it really necessary to get a constant integer. If symbolic expression also works and let the logic flow, we should use symbolic expression as much as possible. So the generated code works for shapes that are not known ahead of time.
Expand Down
25 changes: 10 additions & 15 deletions python/tvm/relay/testing/config.py → pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,13 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""Configuration about tests"""
from __future__ import absolute_import as _abs

import os
import tvm


def ctx_list():
"""Get context list for testcases"""
device_list = os.environ.get("RELAY_TEST_TARGETS", "")
device_list = (device_list.split(",") if device_list
else ["llvm", "cuda"])
device_list = set(device_list)
res = [(device, tvm.context(device, 0)) for device in device_list]
return [x for x in res if x[1].exist]
[pytest]
markers =
gpu: mark a test as requiring a gpu
tensorcore: mark a test as requiring a tensorcore
cuda: mark a test as requiring cuda
opencl: mark a test as requiring opencl
rocm: mark a test as requiring rocm
vulkan: mark a test as requiring vulkan
metal: mark a test as requiring metal
llvm: mark a test as requiring llvm
4 changes: 2 additions & 2 deletions python/tvm/relay/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import tvm.relay as relay
import tvm.relay.op as op
from tvm.relay import Prelude
from tvm.testing import enabled_targets

from . import mlp
from . import resnet
Expand All @@ -41,7 +42,6 @@
from . import temp_op_attr
from . import synthetic

from .config import ctx_list
from .init import create_workload
from .nat import add_nat_definitions, count, make_nat_value, make_nat_expr
from .py_converter import to_python, run_as_python
Expand Down Expand Up @@ -125,7 +125,7 @@ def check_grad(func,
if test_inputs is None:
test_inputs = inputs

for target, ctx in ctx_list():
for target, ctx in enabled_targets():
intrp = relay.create_executor(ctx=ctx, target=target)

# Get analytic gradients.
Expand Down
Loading

0 comments on commit 574a7dd

Please sign in to comment.