Debian Codegraph @

tensilelite/Tensile/Tests/conftest.py

raw symbols SCIP
1 ################################################################################
2 #
3 # Copyright (C) 2022-2025 Advanced Micro Devices, Inc. All rights reserved.
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22 #
23 ################################################################################
24
25 import pytest
26 import os
27 import sys
28
29 try:
30 import xdist # noqa
31 except ImportError:
32 @pytest.fixture(scope="session")
33 def worker_id():
34 return None
35
36 testdir = os.path.dirname(__file__)
37 moddir = os.path.dirname(testdir)
38 rootdir = os.path.dirname(moddir)
39 sys.path.append(rootdir)
40
41 def pytest_addoption(parser):
42 parser.addoption("--tensile-options")
43 parser.addoption("--global-parameters")
44 parser.addoption("--prebuilt-client")
45 parser.addoption("--no-common-build", action="store_true")
46 parser.addoption("--builddir", "--client-dir")
47 parser.addoption("--timing-file", default=None)
48
49 @pytest.fixture(scope="session")
50 def timing_path(pytestconfig, tmpdir_factory):
51 userDir = pytestconfig.getoption("--timing-file")
52 if userDir is not None:
53 return userDir
54 return str(tmpdir_factory.mktemp("results") / "timing.csv")
55
56 @pytest.fixture(autouse=True)
57 def incremental_timer(request, worker_lock_instance, timing_path):
58 #import pdb
59 #pdb.set_trace()
60 import datetime
61 start = datetime.datetime.now()
62 yield
63 stop = datetime.datetime.now()
64
65 testtime = stop - start
66 with worker_lock_instance:
67 with open(timing_path, "a") as f:
68 f.write("{},{}\n".format(request.node.name, testtime.total_seconds()))
69
70 @pytest.fixture(scope="session")
71 def builddir(pytestconfig, tmpdir_factory):
72 userDir = pytestconfig.getoption("--builddir")
73 if userDir is not None:
74 return userDir
75 return str(tmpdir_factory.mktemp("0_Build"))
76
77 @pytest.fixture(scope="session")
78 def worker_lock_path(tmp_path_factory, worker_id):
79 if not worker_id:
80 return None
81
82 return tmp_path_factory.getbasetemp().parent / "client_execution.lock"
83
84 @pytest.fixture
85 def tensile_script_path():
86 return os.path.join(moddir, 'bin', 'Tensile')
87
88 @pytest.fixture
89 def worker_lock_instance(worker_lock_path):
90 if not worker_lock_path:
91 return open(os.devnull)
92
93 import filelock
94 return filelock.FileLock(str(worker_lock_path))
95
96 @pytest.fixture
97 def tensile_args(pytestconfig, builddir, worker_lock_path):
98 rv = []
99 if worker_lock_path:
100 rv += ["--client-lock", str(worker_lock_path)]
101
102 extraOptions = pytestconfig.getoption("--tensile-options")
103 if extraOptions is not None:
104 rv += extraOptions.split(",")
105 if pytestconfig.getoption("--global-parameters"):
106 rv += ["--global-parameters", pytestconfig.getoption("--global-parameters")]
107 if not pytestconfig.getoption("--no-common-build"):
108 if pytestconfig.getoption("--prebuilt-client"):
109 rv += ["--prebuilt-client", pytestconfig.getoption("--prebuilt-client")]
110
111 return rv
112
113 def pytest_collection_modifyitems(items):
114 """
115 Mainly for tests that aren't simple YAML files (including unit tests).
116 Adds a mark for the root directory name to each test.
117 """
118 for item in items:
119
120 relpath = item.fspath.relto(testdir)
121 components = relpath.split(os.path.sep)
122 # print(f"Items: {item}, Testdir: {testdir}, Components: {components}")
123 if len(components) > 0 and len(components[0]) > 0:
124 item.add_marker(getattr(pytest.mark, components[0]))
125
126 @pytest.fixture
127 def useGlobalParameters(tensile_args):
128 from Tensile import Common
129 from Tensile import Tensile
130 import argparse
131
132 class gpUpdater:
133 def __init__(self, **params):
134 self.additionalParams = params
135
136 def __enter__(self):
137 argParser = argparse.ArgumentParser()
138 Tensile.addCommonArguments(argParser)
139 args = argParser.parse_args(tensile_args)
140
141 Common.restoreDefaultGlobalParameters()
142 if args.CxxCompiler:
143 Common.globalParameters["CxxCompiler"] = args.CxxCompiler
144 isa = Common.detectGlobalCurrentISA(args.device)
145 Common.assignGlobalParameters({}, isa)
146
147 overrideParameters = Tensile.argUpdatedGlobalParameters(args)
148 for key, value in overrideParameters.items():
149 Common.globalParameters[key] = value
150
151 for key, value in self.additionalParams.items():
152 Common.globalParameters[key] = value
153
154 return Common.globalParameters
155
156 def __exit__(self, exc_type, exc_value, traceback):
157 Common.restoreDefaultGlobalParameters()
158
159 return gpUpdater
160