Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TESTS] Refactor tests to run on either the GPU or CPU #6331

Merged
merged 1 commit into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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