This is an automated email from the ASF dual-hosted git repository.

mehrdadh pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/main by this push:
     new 8d0da24f12 [Hexagon] moves conftest.py to tvm.contrib.hexagon so 
outside repos can access the testing fixtures (#11277)
8d0da24f12 is described below

commit 8d0da24f12bdccd8b7d0d953c1280142c8600b4d
Author: Farshid Salemi Parizi <[email protected]>
AuthorDate: Thu May 19 09:40:01 2022 -0700

    [Hexagon] moves conftest.py to tvm.contrib.hexagon so outside repos can 
access the testing fixtures (#11277)
    
    * adding pytest_plugin to python so other repos can access
    
    * import requires_hexagon_toolchain from tvm.contrib.hexagon.pytest_plugin
---
 .../tvm/contrib/hexagon/pytest_plugin.py           |  43 +++--
 tests/python/contrib/test_hexagon/conftest.py      | 212 +--------------------
 .../test_hexagon/test_2d_physical_buffers.py       |   2 +-
 tests/python/contrib/test_hexagon/test_usmp.py     |   2 +-
 4 files changed, 29 insertions(+), 230 deletions(-)

diff --git a/tests/python/contrib/test_hexagon/conftest.py 
b/python/tvm/contrib/hexagon/pytest_plugin.py
similarity index 86%
copy from tests/python/contrib/test_hexagon/conftest.py
copy to python/tvm/contrib/hexagon/pytest_plugin.py
index f76181e06d..2c62a0a0b5 100644
--- a/tests/python/contrib/test_hexagon/conftest.py
+++ b/python/tvm/contrib/hexagon/pytest_plugin.py
@@ -15,12 +15,12 @@
 # specific language governing permissions and limitations
 # under the License.
 
+# pylint: disable=invalid-name,redefined-outer-name
 """ Hexagon testing fixtures used to deduce testing argument
     values from testing parameters """
 
 import os
 import random
-import socket
 from typing import Optional, Union
 
 import pytest
@@ -46,17 +46,17 @@ def shape_nhwc(batch, in_channel, in_size):
 def _compose(args, decs):
     """Helper to apply multiple markers"""
     if len(args) > 0:
-        f = args[0]
-        for d in reversed(decs):
-            f = d(f)
-        return f
+        func = args[0]
+        for dec in reversed(decs):
+            func = dec(func)
+        return func
     return decs
 
 
 def requires_hexagon_toolchain(*args):
     _requires_hexagon_toolchain = [
         pytest.mark.skipif(
-            os.environ.get(HEXAGON_TOOLCHAIN) == None,
+            os.environ.get(HEXAGON_TOOLCHAIN) is None,
             reason=f"Missing environment variable {HEXAGON_TOOLCHAIN}.",
         ),
     ]
@@ -80,23 +80,23 @@ def android_serial_number() -> Optional[str]:
 # triggering TIME_WAIT state on the server socket. This prevents another
 # server to bind to the same port until the wait time elapses.
 
-listen_port_min = 2000  # Well above the privileged ports (1024 or lower)
-listen_port_max = 9000  # Below the search range end (port_end=9199) of RPC 
server
-previous_port = None
+LISTEN_PORT_MIN = 2000  # Well above the privileged ports (1024 or lower)
+LISTEN_PORT_MAX = 9000  # Below the search range end (port_end=9199) of RPC 
server
+PREVIOUS_PORT = None
 
 
 def get_free_port() -> int:
-
-    global previous_port
-    if previous_port is None:
-        port = random.randint(listen_port_min, listen_port_max)
+    """Return the next port that is available to listen on"""
+    global PREVIOUS_PORT
+    if PREVIOUS_PORT is None:
+        port = random.randint(LISTEN_PORT_MIN, LISTEN_PORT_MAX)
     else:
-        port = previous_port + 1
+        port = PREVIOUS_PORT + 1
 
     while tvm.contrib.hexagon.build._is_port_in_use(port):
-        port = port + 1 if port < listen_port_max else listen_port_min
+        port = port + 1 if port < LISTEN_PORT_MAX else LISTEN_PORT_MIN
 
-    previous_port = port
+    PREVIOUS_PORT = port
     return port
 
 
@@ -136,13 +136,13 @@ def _tracker_info() -> Union[str, int]:
 
 @pytest.fixture(scope="session")
 def tvm_tracker_host(_tracker_info) -> str:
-    host, port = _tracker_info
+    host, _ = _tracker_info
     return host
 
 
 @pytest.fixture(scope="session")
 def tvm_tracker_port(_tracker_info) -> int:
-    host, port = _tracker_info
+    _, port = _tracker_info
     return port
 
 
@@ -160,6 +160,7 @@ def adb_server_socket() -> str:
 def hexagon_launcher(
     request, android_serial_number, rpc_server_port, adb_server_socket
 ) -> HexagonLauncherRPC:
+    """Initials and returns hexagon launcher if ANDROID_SERIAL_NUMBER is 
defined"""
     if android_serial_number is None:
         yield None
     else:
@@ -193,7 +194,7 @@ def hexagon_session(hexagon_launcher) -> Session:
 
 
 # If the execution aborts while an RPC server is running, the python
-# code that is supposed to shut it dowm will never execute. This will
+# code that is supposed to shut it down will never execute. This will
 # keep pytest from terminating (indefinitely), so add a cleanup
 # fixture to terminate any still-running servers.
 @pytest.fixture(scope="session", autouse=True)
@@ -209,7 +210,9 @@ def terminate_rpc_servers():
 
 aot_host_target = tvm.testing.parameter(
     "c",
-    "llvm -keys=hexagon -link-params=0 
-mattr=+hvxv68,+hvx-length128b,+hvx-qfloat,-hvx-ieee-fp -mcpu=hexagonv68 
-mtriple=hexagon",
+    "llvm -keys=hexagon -link-params=0 "
+    "-mattr=+hvxv68,+hvx-length128b,+hvx-qfloat,-hvx-ieee-fp "
+    "-mcpu=hexagonv68 -mtriple=hexagon",
 )
 
 
diff --git a/tests/python/contrib/test_hexagon/conftest.py 
b/tests/python/contrib/test_hexagon/conftest.py
index f76181e06d..3b057384df 100644
--- a/tests/python/contrib/test_hexagon/conftest.py
+++ b/tests/python/contrib/test_hexagon/conftest.py
@@ -18,216 +18,12 @@
 """ Hexagon testing fixtures used to deduce testing argument
     values from testing parameters """
 
-import os
-import random
-import socket
-from typing import Optional, Union
 
 import pytest
 
 import tvm
-import tvm.rpc.tracker
-from tvm.contrib.hexagon.build import HexagonLauncher, HexagonLauncherRPC
-from tvm.contrib.hexagon.session import Session
+import tvm.testing
 
-HEXAGON_TOOLCHAIN = "HEXAGON_TOOLCHAIN"
-TVM_TRACKER_HOST = "TVM_TRACKER_HOST"
-TVM_TRACKER_PORT = "TVM_TRACKER_PORT"
-ANDROID_REMOTE_DIR = "ANDROID_REMOTE_DIR"
-ANDROID_SERIAL_NUMBER = "ANDROID_SERIAL_NUMBER"
-ADB_SERVER_SOCKET = "ADB_SERVER_SOCKET"
-
-
[email protected]
-def shape_nhwc(batch, in_channel, in_size):
-    return (batch, in_size, in_size, in_channel)
-
-
-def _compose(args, decs):
-    """Helper to apply multiple markers"""
-    if len(args) > 0:
-        f = args[0]
-        for d in reversed(decs):
-            f = d(f)
-        return f
-    return decs
-
-
-def requires_hexagon_toolchain(*args):
-    _requires_hexagon_toolchain = [
-        pytest.mark.skipif(
-            os.environ.get(HEXAGON_TOOLCHAIN) == None,
-            reason=f"Missing environment variable {HEXAGON_TOOLCHAIN}.",
-        ),
-    ]
-
-    return _compose(args, _requires_hexagon_toolchain)
-
-
[email protected]
-def android_serial_number() -> Optional[str]:
-    serial = os.getenv(ANDROID_SERIAL_NUMBER, default="")
-    # Setting ANDROID_SERIAL_NUMBER to an empty string should be
-    # equivalent to having it unset.
-    if not serial.strip():
-        serial = None
-    return serial
-
-
-# NOTE on server ports:
-# These tests use different port numbers for the RPC server (7070 + ...).
-# The reason is that an RPC session cannot be gracefully closed without
-# triggering TIME_WAIT state on the server socket. This prevents another
-# server to bind to the same port until the wait time elapses.
-
-listen_port_min = 2000  # Well above the privileged ports (1024 or lower)
-listen_port_max = 9000  # Below the search range end (port_end=9199) of RPC 
server
-previous_port = None
-
-
-def get_free_port() -> int:
-
-    global previous_port
-    if previous_port is None:
-        port = random.randint(listen_port_min, listen_port_max)
-    else:
-        port = previous_port + 1
-
-    while tvm.contrib.hexagon.build._is_port_in_use(port):
-        port = port + 1 if port < listen_port_max else listen_port_min
-
-    previous_port = port
-    return port
-
-
[email protected](scope="session")
-def _tracker_info() -> Union[str, int]:
-    env_tracker_host = os.getenv(TVM_TRACKER_HOST, default="")
-    env_tracker_port = os.getenv(TVM_TRACKER_PORT, default="")
-
-    if env_tracker_host or env_tracker_port:
-        # A tracker is already running, and we should connect to it
-        # when running tests.
-        assert env_tracker_host, "TVM_TRACKER_PORT is defined, but 
TVM_TRACKER_HOST is not"
-        assert env_tracker_port, "TVM_TRACKER_HOST is defined, but 
TVM_TRACKER_PORT is not"
-        env_tracker_port = int(env_tracker_port)
-
-        try:
-            tvm.rpc.connect_tracker(env_tracker_host, env_tracker_port)
-        except RuntimeError as exc:
-            message = (
-                "Could not connect to external tracker "
-                "specified by $TVM_TRACKER_HOST and $TVM_TRACKER_PORT "
-                f"({env_tracker_host}:{env_tracker_port})"
-            )
-            raise RuntimeError(message) from exc
-
-        yield (env_tracker_host, env_tracker_port)
-
-    else:
-        # No tracker is provided to the tests, so we should start one
-        # for the tests to use.
-        tracker = tvm.rpc.tracker.Tracker("127.0.0.1", get_free_port())
-        try:
-            yield (tracker.host, tracker.port)
-        finally:
-            tracker.terminate()
-
-
[email protected](scope="session")
-def tvm_tracker_host(_tracker_info) -> str:
-    host, port = _tracker_info
-    return host
-
-
[email protected](scope="session")
-def tvm_tracker_port(_tracker_info) -> int:
-    host, port = _tracker_info
-    return port
-
-
[email protected]
-def rpc_server_port() -> int:
-    return get_free_port()
-
-
[email protected]
-def adb_server_socket() -> str:
-    return os.getenv(ADB_SERVER_SOCKET, default="tcp:5037")
-
-
[email protected]
-def hexagon_launcher(
-    request, android_serial_number, rpc_server_port, adb_server_socket
-) -> HexagonLauncherRPC:
-    if android_serial_number is None:
-        yield None
-    else:
-        # Requesting these fixtures sets up a local tracker, if one
-        # hasn't been provided to us.  Delaying the evaluation of
-        # these fixtures avoids starting a tracker unless necessary.
-        tvm_tracker_host = request.getfixturevalue("tvm_tracker_host")
-        tvm_tracker_port = request.getfixturevalue("tvm_tracker_port")
-
-        rpc_info = {
-            "rpc_tracker_host": tvm_tracker_host,
-            "rpc_tracker_port": tvm_tracker_port,
-            "rpc_server_port": rpc_server_port,
-            "adb_server_socket": adb_server_socket,
-        }
-        launcher = HexagonLauncher(serial_number=android_serial_number, 
rpc_info=rpc_info)
-        launcher.start_server()
-        try:
-            yield launcher
-        finally:
-            launcher.stop_server()
-
-
[email protected]
-def hexagon_session(hexagon_launcher) -> Session:
-    if hexagon_launcher is None:
-        yield None
-    else:
-        with hexagon_launcher.start_session() as session:
-            yield session
-
-
-# If the execution aborts while an RPC server is running, the python
-# code that is supposed to shut it dowm will never execute. This will
-# keep pytest from terminating (indefinitely), so add a cleanup
-# fixture to terminate any still-running servers.
[email protected](scope="session", autouse=True)
-def terminate_rpc_servers():
-    # Since this is a fixture that runs regardless of whether the
-    # execution happens on simulator or on target, make sure the
-    # yield happens every time.
-    serial = os.environ.get(ANDROID_SERIAL_NUMBER)
-    yield []
-    if serial == "simulator":
-        os.system("ps ax | grep tvm_rpc_x86 | awk '{print $1}' | xargs kill")
-
-
-aot_host_target = tvm.testing.parameter(
-    "c",
-    "llvm -keys=hexagon -link-params=0 
-mattr=+hvxv68,+hvx-length128b,+hvx-qfloat,-hvx-ieee-fp -mcpu=hexagonv68 
-mtriple=hexagon",
-)
-
-
[email protected]
-def aot_target(aot_host_target):
-    if aot_host_target == "c":
-        yield tvm.target.hexagon("v68")
-    elif aot_host_target.startswith("llvm"):
-        yield aot_host_target
-    else:
-        assert False, "Incorrect AoT host target: {aot_host_target}. Options 
are [c, llvm]."
-
-
-def pytest_addoption(parser):
-    parser.addoption("--gtest_args", action="store", default="")
-
-
-def pytest_generate_tests(metafunc):
-    option_value = metafunc.config.option.gtest_args
-    if "gtest_args" in metafunc.fixturenames and option_value is not None:
-        metafunc.parametrize("gtest_args", [option_value])
+pytest_plugins = [
+    "tvm.contrib.hexagon.pytest_plugin",
+]
diff --git a/tests/python/contrib/test_hexagon/test_2d_physical_buffers.py 
b/tests/python/contrib/test_hexagon/test_2d_physical_buffers.py
index 78e1eb11ad..787d71fa17 100644
--- a/tests/python/contrib/test_hexagon/test_2d_physical_buffers.py
+++ b/tests/python/contrib/test_hexagon/test_2d_physical_buffers.py
@@ -29,7 +29,7 @@ from tvm import te
 from tvm.tir.stmt_functor import post_order_visit
 from tvm.contrib.hexagon.build import HexagonLauncher
 
-from .conftest import requires_hexagon_toolchain
+from tvm.contrib.hexagon.pytest_plugin import requires_hexagon_toolchain
 from .infrastructure import allocate_hexagon_array
 
 # Needed to register the link_shared packedfunc.
diff --git a/tests/python/contrib/test_hexagon/test_usmp.py 
b/tests/python/contrib/test_hexagon/test_usmp.py
index 116ecb4154..03badfb655 100644
--- a/tests/python/contrib/test_hexagon/test_usmp.py
+++ b/tests/python/contrib/test_hexagon/test_usmp.py
@@ -26,7 +26,7 @@ from tvm.relay.backend import Executor, Runtime
 from tvm.contrib.hexagon.session import Session
 from tvm.testing.usmp import is_tvm_backendallocworkspace_calls
 
-from .conftest import requires_hexagon_toolchain
+from tvm.contrib.hexagon.pytest_plugin import requires_hexagon_toolchain
 
 usmp_enabled = tvm.testing.parameter(False, True)
 

Reply via email to