-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_export_db_branches.py
More file actions
324 lines (267 loc) · 10.9 KB
/
Copy pathtest_export_db_branches.py
File metadata and controls
324 lines (267 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
###############################################################################
#
# MIT License
#
# Copyright (c) 2025 Advanced Micro Devices, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#
###############################################################################
"""Additional branch coverage for export_db."""
import argparse
import base64
import json
import os
import sqlite3
from types import SimpleNamespace
from unittest.mock import MagicMock
import pytest
import tuna.miopen.subcmd.export_db as export_db
from tuna.utils.db_utility import DB_Type
from tuna.miopen.utils.config_type import ConfigType
class _FakeColumn:
"""Lightweight column stub that records equality checks."""
def __init__(self, name):
self.name = name
def __eq__(self, other):
return SimpleNamespace(value=other)
def in_(self, _):
return self
class _FakeQuery:
"""Query stub that carries simple state."""
def __init__(self, entries=None, value=None):
self._entries = entries or []
self.value = value
def all(self):
return self._entries
def filter(self, cond):
val = getattr(cond, 'value', None)
return _FakeQuery(self._entries, val)
def distinct(self):
return self
def subquery(self):
return self
def order_by(self, *_, **__):
return self
def _fake_session_factory(num_cu_values):
"""Build a DbSession replacement that returns provided num_cu values."""
class _FakeSession:
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
return False
def query(self, *_):
return _FakeQuery(entries=[(val,) for val in num_cu_values])
def commit(self):
return None
return _FakeSession
def test_arg_export_db_requires_arch(monkeypatch):
args = argparse.Namespace(golden_v='1.0', arch=None)
logger = MagicMock()
export_db.arg_export_db(args, logger)
logger.error.assert_called_once()
def test_get_filename_variants(monkeypatch, tmp_path):
monkeypatch.chdir(tmp_path)
# filename provided
fname = export_db.get_filename('gfx900', None, 'custom', True,
DB_Type.FIND_DB)
assert fname.endswith('custom.OpenCL.fdb.txt')
# num_cu missing falls back to arch
fname = export_db.get_filename('gfx900', None, None, False, DB_Type.PERF_DB)
assert fname.endswith('gfx900.db.txt')
# num_cu over 64 encoded as hex
fname = export_db.get_filename('gfx900', 128, None, False, DB_Type.FIND_DB)
assert fname.endswith('gfx90080.HIP.fdb.txt')
# kernel db extension
fname = export_db.get_filename('gfx900', 32, None, False, DB_Type.KERN_DB)
assert fname.endswith('.kdb')
def test_fin_net_cfg_job_non_convolution(monkeypatch):
logger = MagicMock()
jobs = export_db.fin_net_cfg_job([], logger, ConfigType.batch_norm)
logger.error.assert_called_once()
assert jobs == []
def test_add_entry_to_solvers_duplicate():
solvers = {'k1': {'solver_a': 123}}
entry = SimpleNamespace(fdb_key='k1', solver='solver_a', update_ts=456)
added = export_db.add_entry_to_solvers(entry, solvers, MagicMock())
assert added is False
assert solvers['k1']['solver_a'] == 123
def test_build_miopen_fdb_trims(monkeypatch):
monkeypatch.setattr(export_db, 'require_id_solvers', lambda: None)
monkeypatch.setattr(export_db, 'ID_SOLVER_MAP', {
's0': 0,
's1': 1,
's2': 2,
's3': 3,
's4': 4
})
class _Entry(SimpleNamespace):
pass
entries = []
for idx in range(5):
entries.append((_Entry(fdb_key='key',
solver=f's{idx}',
kernel_time=idx,
workspace_sz=0,
update_ts=idx), None))
query = _FakeQuery(entries=entries)
result = export_db.build_miopen_fdb(query, MagicMock())
assert 'key' in result
assert len(result['key']) == 4
assert all(isinstance(x, _Entry) for x in result['key'])
def test_write_kdb_handles_extensions_and_dedup(monkeypatch, tmp_path):
monkeypatch.chdir(tmp_path)
logger = MagicMock()
blob = base64.b64encode(b'abc')
kern1 = SimpleNamespace(kernel_name='k',
kernel_args='arg',
kernel_blob=blob,
kernel_hash='h1',
uncompressed_size=1,
kernel_group=1)
kern2 = SimpleNamespace(kernel_name='k',
kernel_args='arg',
kernel_blob=blob,
kernel_hash='h1',
uncompressed_size=1,
kernel_group=1)
kern3 = SimpleNamespace(kernel_name='k.mlir',
kernel_args='arg3',
kernel_blob=blob,
kernel_hash='h3',
uncompressed_size=1,
kernel_group=2)
fname = export_db.write_kdb('gfx900',
64, [kern1, kern2, kern3],
logger,
filename='out')
conn = sqlite3.connect(fname)
cur = conn.cursor()
cur.execute("SELECT kernel_name, kernel_args FROM kern_db ORDER BY id")
rows = cur.fetchall()
cur.close()
conn.close()
# duplicate filtered, mlir keeps args as-is without -mcpu
assert len(rows) == 2
assert rows[0][0] == 'k.o'
assert '-mcpu=gfx900' in rows[0][1]
assert rows[1][0] == 'k.mlir.o'
assert '-mcpu=' not in rows[1][1]
def test_build_miopen_fdb_skews(monkeypatch):
# ensure deterministic solver map for sorting
monkeypatch.setattr(export_db, 'require_id_solvers', lambda: None)
monkeypatch.setattr(export_db, 'ID_SOLVER_MAP', {'s': 0})
args = SimpleNamespace()
args.src_table = SimpleNamespace(num_cu=_FakeColumn('num_cu'),
id=_FakeColumn('id'))
query = _FakeQuery(entries=[(SimpleNamespace(id=1), None)])
def fake_build(miopen_query, logger):
return {f"k_{miopen_query.value}": ['entry']}
fake_session = _fake_session_factory([64, 32])
monkeypatch.setattr(export_db, 'DbSession', fake_session)
monkeypatch.setattr(export_db, 'build_miopen_fdb', fake_build)
res = export_db.build_miopen_fdb_skews(args, query, MagicMock())
assert set(res.keys()) == {'k_64_cu64', 'k_32_cu32'}
def test_export_kdb_uses_skews(monkeypatch):
args = SimpleNamespace(arch='gfx', num_cu=None, filename=None, opencl=False)
dbt = SimpleNamespace()
logger = MagicMock()
monkeypatch.setattr(export_db, 'get_fdb_query', lambda *_: _FakeQuery())
monkeypatch.setattr(export_db, 'build_miopen_fdb_skews',
lambda *_, **__: {'k': ['v']})
monkeypatch.setattr(export_db, 'build_miopen_kdb', lambda *_: [1, 2, 3])
monkeypatch.setattr(export_db, 'write_kdb', lambda *_, **__: 'kdb.out')
result = export_db.export_kdb(dbt, args, logger, skew_fdbs=True)
assert result == 'kdb.out'
def test_build_miopen_pdb(monkeypatch):
monkeypatch.setattr(export_db, 'require_id_solvers', lambda: None)
monkeypatch.setattr(export_db, 'ID_SOLVER_MAP', {'s1': 1})
args = SimpleNamespace()
query_entries = [
(SimpleNamespace(fdb_key='key1',
solver='s1',
kernel_time=1,
workspace_sz=0,
update_ts=1,
params='p1'), SimpleNamespace(id=1)),
(SimpleNamespace(fdb_key='dup',
solver='s1',
kernel_time=2,
workspace_sz=0,
update_ts=2,
params='p1'), SimpleNamespace(id=1)),
(SimpleNamespace(fdb_key='key2',
solver='s1',
kernel_time=3,
workspace_sz=0,
update_ts=3,
params='p2'), SimpleNamespace(id=2)),
]
query = _FakeQuery(entries=query_entries)
monkeypatch.setattr(export_db, 'fin_db_key', lambda *_: {1: 'db1', 2: 'db2'})
res = export_db.build_miopen_pdb(query, MagicMock())
assert set(res.keys()) == {'db1', 'db2'}
# entries sharing a db_key are preserved even with different fdb_keys
assert len(res['db1']) == 2
def test_write_pdb_orders_by_solver(monkeypatch, tmp_path):
monkeypatch.chdir(tmp_path)
monkeypatch.setattr(export_db, 'require_id_solvers', lambda: None)
monkeypatch.setattr(export_db, 'ID_SOLVER_MAP', {'s1': 1, 's2': 2})
perf_db = {
'db': [
SimpleNamespace(solver='s2', params='two'),
SimpleNamespace(solver='s1', params='one')
]
}
path = export_db.write_pdb('gfx', 1, False, perf_db, filename='perf.txt')
with open(path, 'r', encoding='utf-8') as fp:
line = fp.read().strip()
assert line == 'db=1:one;2:two'
def test_export_pdb_txt(monkeypatch):
args = SimpleNamespace(arch='gfx', num_cu=1, opencl=False, filename=None)
dbt = SimpleNamespace()
logger = MagicMock()
monkeypatch.setattr(export_db, 'get_pdb_query', lambda *_: _FakeQuery())
monkeypatch.setattr(export_db, 'build_miopen_pdb',
lambda *_: {'db': ['entry']})
monkeypatch.setattr(export_db, 'write_pdb', lambda *_, **__: 'perf.out')
res = export_db.export_pdb_txt(dbt, args, logger)
assert res == 'perf.out'
def test_run_export_db_routes(monkeypatch, capsys):
class _DummyTables:
def __init__(self, session_id=None, **_):
self.session = SimpleNamespace(arch='gfxA', num_cu=64)
self.find_db_table = 'find'
self.golden_table = 'golden'
self.session_id = session_id
args = argparse.Namespace(session_id=1,
golden_v='1.0',
find_db=True,
kern_db=False,
perf_db=False,
arch=None,
num_cu=None,
opencl=False,
filename=None)
logger = MagicMock()
monkeypatch.setattr(export_db, 'MIOpenDBTables', _DummyTables)
monkeypatch.setattr(export_db, 'export_fdb', lambda *_: 'out.find')
export_db.run_export_db(args, logger)
captured = capsys.readouterr()
assert 'out.find' in captured.out