diff --git a/.github/scripts/component_ci.py b/.github/scripts/component_ci.py index 0b5d16822f9d..a0a005071f79 100644 --- a/.github/scripts/component_ci.py +++ b/.github/scripts/component_ci.py @@ -7,6 +7,7 @@ - geko=true/false - miopen=true/false - tensilelite_coverage=true/false + - hipblaslt_library_uniqueness=true/false Each component defines a set of path patterns. If any changed file matches, that component is marked as triggered. @@ -44,6 +45,13 @@ "projects/hipblaslt/tensilelite/**", ".github/workflows/component-ci-tensilelite-coverage.yml", ], + "hipblaslt_library_uniqueness": [ + "projects/hipblaslt/library/src/amd_detail/rocblaslt/src/Tensile/Logic/**", + "projects/hipblaslt/tensilelite/Tensile/Tests/unit/" + "test_solution_uid_uniqueness.py", + "projects/hipblaslt/tensilelite/tox.ini", + ".github/workflows/component-ci-library-uniqueness.yml", + ], } # Paths that affect every component's CI run: the workflow, this script, diff --git a/.github/workflows/component-ci-library-uniqueness.yml b/.github/workflows/component-ci-library-uniqueness.yml new file mode 100644 index 000000000000..744e08a09a44 --- /dev/null +++ b/.github/workflows/component-ci-library-uniqueness.yml @@ -0,0 +1,37 @@ +# Copyright (c) Advanced Micro Devices, Inc., or its affiliates. +# SPDX-License-Identifier: MIT + +name: "Component CI: Library Uniqueness" + +on: + workflow_call: + inputs: + docker_image: + type: string + required: true + linux_runner: + type: string + required: true + +permissions: + contents: read + +jobs: + linux: + name: Linux + runs-on: ${{ inputs.linux_runner }} + container: + image: ${{ inputs.docker_image }} + defaults: + run: + shell: bash + working-directory: projects/hipblaslt/tensilelite + steps: + - name: Checkout repository + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + + - name: Install tox + run: python -m pip install tox + + - name: Run Library Uniqueness gate + run: python -m tox -e library-uniqueness diff --git a/.github/workflows/component-ci.yml b/.github/workflows/component-ci.yml index 333edf6b70bc..33b76fd9b491 100644 --- a/.github/workflows/component-ci.yml +++ b/.github/workflows/component-ci.yml @@ -25,6 +25,7 @@ jobs: geko: ${{ steps.configure.outputs.geko }} miopen: ${{ steps.configure.outputs.miopen }} tensilelite-coverage: ${{ steps.configure.outputs.tensilelite_coverage }} + library-uniqueness: ${{ steps.configure.outputs.hipblaslt_library_uniqueness }} docker-image: ${{ steps.env.outputs.docker-image }} linux-runner: ${{ steps.env.outputs.linux-runner }} windows-runner: ${{ steps.env.outputs.windows-runner }} @@ -88,6 +89,15 @@ jobs: docker_image: ${{ needs.changes.outputs.docker-image }} linux_runner: ${{ needs.changes.outputs.linux-runner }} + library-uniqueness: + name: "hipBLASLt library uniqueness" + needs: changes + if: needs.changes.outputs.library-uniqueness == 'true' + uses: ./.github/workflows/component-ci-library-uniqueness.yml + with: + docker_image: ${{ needs.changes.outputs.docker-image }} + linux_runner: ${{ needs.changes.outputs.linux-runner }} + component-ci-summary: name: "Component CI Summary" if: always() @@ -97,6 +107,7 @@ jobs: - geko - miopen - tensilelite-coverage + - library-uniqueness runs-on: ubuntu-24.04 steps: - name: Checkout TheRock repository diff --git a/projects/hipblaslt/docs/solution-uid.md b/projects/hipblaslt/docs/solution-uid.md new file mode 100644 index 000000000000..67d8a5bbf011 --- /dev/null +++ b/projects/hipblaslt/docs/solution-uid.md @@ -0,0 +1,50 @@ +# Solution UID foundation + +`SolutionUID` is a stable 64-bit identifier for a hipBLASLt/TensileLite +solution. This first migration step provides the identifier format and +maintenance tooling, but does not assign or consume UIDs in library generation +or at runtime. + +## Identifier format + +```text +| 40 bits: milliseconds since 2026-01-01 UTC | 24 bits: OS randomness | +``` + +UIDs are generated by +`tensilelite/Tensile/Common/SolutionIdGen.py`. Internally, a UID remains an +unsigned 64-bit integer. Human-readable files and command-line output use an +unpadded base62 representation: + +- the frozen alphabet is `0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`; +- writers use the `0u` prefix; readers also accept `0U`; +- leading zero digits are forbidden, except that zero is written as `0u0`; +- values larger than `2^64 - 1` are rejected. + +For example, decimal UID `200` is represented as `0u3E`. A UID is stored as +the optional, unquoted `SolutionUID` field immediately after the local +`SolutionIndex` in dict-format logic YAML: + +```yaml +- SolutionIndex: 0 + SolutionUID: 0u3E +``` + +YAML readers decode the text immediately and all identity, comparison, and +mapping operations use the unsigned 64-bit value. + +## Regenerate a UID + +To replace one solution's UID after a duplicate is reported: + +```bash +cd projects/hipblaslt/tensilelite +python3 -m Tensile.TensileGenerateUID path/to/logic.yaml --index 5 --inplace +``` + +`--index` is the solution's local `SolutionIndex` in that YAML file. Without +`--inplace`, the command prints the new canonical `0u` UID without modifying +the file. + +Repository-wide assignment, runtime selection, and mandatory uniqueness are +introduced by later migration steps. diff --git a/projects/hipblaslt/tensilelite/Tensile/Common/SolutionIdGen.py b/projects/hipblaslt/tensilelite/Tensile/Common/SolutionIdGen.py new file mode 100644 index 000000000000..06751908a073 --- /dev/null +++ b/projects/hipblaslt/tensilelite/Tensile/Common/SolutionIdGen.py @@ -0,0 +1,192 @@ +# Copyright Advanced Micro Devices, Inc., or its affiliates. +# SPDX-License-Identifier: MIT + +"""Generate stable 64-bit solution IDs for hipBLASLt.""" + +from __future__ import annotations + +import secrets +import time + +""" +Solution ID is a 64-bit integer that is used to uniquely identify a solution. +It is a combination of the current time in milliseconds since the SolutionUID +epoch and an OS-backed random 24-bit integer. + +| 40 bits | 24 bits | +|---------|---------| +| Time | Random | +""" + +EPOCH_MS = 1_767_225_600_000 # 2026-01-01 00:00:00 UTC +MS_MASK = (1 << 40) - 1 +RANDOM_BITS = 24 +RANDOM_MASK = (1 << RANDOM_BITS) - 1 +RANDOM_SHIFT = RANDOM_BITS +BASE62_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +BASE62_LOOKUP = {character: index for index, character in enumerate(BASE62_ALPHABET)} +BASE62_PREFIX = "0u" +UINT64_MAX = (1 << 64) - 1 + + +def encode_solution_uid(solution_uid: int) -> str: + """Encode a 64-bit solution UID in canonical ``0u`` base62 form. + + Args: + solution_uid: Unsigned 64-bit solution identifier. + + Returns: + Canonical unpadded base62 representation prefixed with ``0u``. + + Raises: + ValueError: If ``solution_uid`` is outside the unsigned 64-bit range. + """ + if solution_uid < 0 or solution_uid > UINT64_MAX: + raise ValueError( + f"SolutionUID ({solution_uid}) is out of range for uint64 " + f"(0..{UINT64_MAX})" + ) + + if solution_uid == 0: + return f"{BASE62_PREFIX}0" + + digits = [] + value = solution_uid + while value: + value, remainder = divmod(value, len(BASE62_ALPHABET)) + digits.append(BASE62_ALPHABET[remainder]) + return BASE62_PREFIX + "".join(reversed(digits)) + + +def decode_solution_uid(encoded_uid: str) -> int: + """Decode a canonical ``0u`` or ``0U`` base62 solution UID. + + Args: + encoded_uid: Canonical prefixed, unpadded base62 representation. + + Returns: + Unsigned 64-bit solution identifier. + + Raises: + TypeError: If ``encoded_uid`` is not a string. + ValueError: If the representation is malformed, non-canonical, or + outside the unsigned 64-bit range. + """ + if not isinstance(encoded_uid, str): + raise TypeError("SolutionUID representation must be a string") + if len(encoded_uid) < 3 or encoded_uid[:2] not in {"0u", "0U"}: + raise ValueError("SolutionUID must start with 0u or 0U") + + payload = encoded_uid[2:] + if len(payload) > 1 and payload[0] == "0": + raise ValueError("SolutionUID base62 payload must not contain leading zeros") + + value = 0 + for character in payload: + digit = BASE62_LOOKUP.get(character) + if digit is None: + raise ValueError(f"Invalid SolutionUID base62 character: {character!r}") + if value > (UINT64_MAX - digit) // len(BASE62_ALPHABET): + raise ValueError("SolutionUID is out of range for uint64") + value = value * len(BASE62_ALPHABET) + digit + return value + + +def _ms_since_epoch(now_ms: int) -> int: + """Convert Unix milliseconds to ms since :data:`EPOCH_MS`. + + Args: + now_ms: Unix timestamp in milliseconds. + + Returns: + Milliseconds since the configured epoch. + + Raises: + ValueError: If the value does not fit in 40 bits. + """ + ms = now_ms - EPOCH_MS + if ms < 0 or ms > MS_MASK: + raise ValueError( + f"ms since epoch ({ms}) is out of range for 40 bits (0..{MS_MASK})" + ) + return ms + + +def generate_solution_id(*, now_ms: int | None = None) -> int: + """Generate a 64-bit solution ID. + + Layout: ``(ms_since_epoch << 24) | random24`` where ``ms_since_epoch`` uses + 40 bits and ``random24`` uses ``secrets.randbits(24)``. + + Args: + now_ms: Optional fixed Unix timestamp in milliseconds for testing. + When omitted, ``time.time()`` is used. + + Returns: + A 64-bit unsigned solution identifier. + + Raises: + ValueError: If the ms-since-epoch value does not fit in 40 bits. + """ + if now_ms is None: + now_ms = int(time.time() * 1000) + + ms = _ms_since_epoch(now_ms) + random24 = secrets.randbits(RANDOM_BITS) + return (ms << RANDOM_SHIFT) | random24 + + +def decode_solution_id(solution_id: int) -> tuple[int, int]: + """Split a solution ID into its timestamp and random components. + + Args: + solution_id: 64-bit solution identifier from :func:`generate_solution_id`. + + Returns: + Tuple of ``(ms_since_epoch, random24)``. + """ + return solution_id >> RANDOM_SHIFT, solution_id & RANDOM_MASK + + +def read_solution_uid(solution: dict) -> int: + """Read a solution UID from YAML state, generating one if absent. + + Args: + solution: Solution dictionary from logic YAML. + + Returns: + 64-bit solution UID. + """ + if "SolutionUID" in solution: + return decode_solution_uid(solution["SolutionUID"]) + return generate_solution_id() + + +def ensure_solution_uid(solution: dict) -> None: + """Assign ``SolutionUID`` when the solution has no UID field yet. + + Args: + solution: Solution dictionary to update in place. + + Raises: + ValueError: Propagated from :func:`generate_solution_id` on time overflow. + """ + if "SolutionUID" not in solution: + solution["SolutionUID"] = encode_solution_uid(generate_solution_id()) + + +def regenerate_solution_uid(solution: dict) -> int: + """Replace ``SolutionUID`` with a newly generated value. + + Args: + solution: Solution dictionary to update in place. + + Returns: + The newly assigned 64-bit solution UID. + + Raises: + ValueError: Propagated from :func:`generate_solution_id` on time overflow. + """ + uid = generate_solution_id() + solution["SolutionUID"] = encode_solution_uid(uid) + return uid diff --git a/projects/hipblaslt/tensilelite/Tensile/LibraryIO.py b/projects/hipblaslt/tensilelite/Tensile/LibraryIO.py index 5dac0f88c85e..e3b6dd4e6572 100644 --- a/projects/hipblaslt/tensilelite/Tensile/LibraryIO.py +++ b/projects/hipblaslt/tensilelite/Tensile/LibraryIO.py @@ -606,9 +606,11 @@ def prepareLibraryLogicDict(data: dict[str, Any]) -> None: def reorderSolutionsParams(data: Dict[str, Any]) -> None: """Reorder solution dict keys after list-to-dict conversion. - Moves ``SolutionIndex``, ``KernelNameMin``, and ``SolutionNameMin`` to the - top of each entry in ``data["Solutions"]``. Used when migrating legacy - list-format logic to dict format and after :func:`reorderSolutionDictForDictMerge`. + Moves ``SolutionIndex``, ``SolutionUID``, ``KernelNameMin``, and + ``SolutionNameMin`` to the top of each entry in ``data["Solutions"]``. + ``SolutionUID`` is dropped from that lead list when the solution has no + such field. Used when migrating legacy list-format logic to dict format + and after :func:`reorderSolutionDictForDictMerge`. Args: data: Dict-format library logic data (mutated in place). Must contain @@ -620,16 +622,19 @@ def reorderSolutionsParams(data: Dict[str, Any]) -> None: Raises: None. """ - keys = ["SolutionIndex", "KernelNameMin", "SolutionNameMin"] sols = data.get("Solutions") if not sols: return for solIdx in range(len(sols)): + sol = sols[solIdx] + keys = ["SolutionIndex", "SolutionUID", "KernelNameMin", "SolutionNameMin"] + if "SolutionUID" not in sol: + keys.remove("SolutionUID") vals: Dict[str, Any] = {} for key in keys: - if key in sols[solIdx]: - vals[key] = sols[solIdx].pop(key) - sols[solIdx] = {**vals, **sols[solIdx]} + if key in sol: + vals[key] = sol.pop(key) + sols[solIdx] = {**vals, **sol} def reorderSolutionDictForDictMerge(state: Dict[str, Any]) -> Dict[str, Any]: @@ -977,7 +982,9 @@ def createLibraryLogic( Emits the root mapping written for YAML (``ProblemType``, ``Solutions``, ``ExactLogic``, etc.) including sorted ``ProblemType`` keys and merge-aligned - solution key layout. ``DefaultSolution`` is a sorted snapshot of + solution key layout (``SolutionIndex``, ``SolutionUID``, + ``KernelNameMin``, ``SolutionNameMin``, then remaining keys). + ``DefaultSolution`` is a sorted snapshot of ``defaultSolution`` at write time; any solution field equal to that snapshot is omitted from per-solution dicts so defaults live only under ``DefaultSolution``. diff --git a/projects/hipblaslt/tensilelite/Tensile/TensileGenerateUID.py b/projects/hipblaslt/tensilelite/Tensile/TensileGenerateUID.py new file mode 100644 index 000000000000..9e725cdfa726 --- /dev/null +++ b/projects/hipblaslt/tensilelite/Tensile/TensileGenerateUID.py @@ -0,0 +1,144 @@ +# Copyright Advanced Micro Devices, Inc., or its affiliates. +# SPDX-License-Identifier: MIT + +"""CLI to regenerate ``SolutionUID`` for one solution in a logic YAML file.""" + +from __future__ import annotations + +import argparse +import sys +from pathlib import Path +from typing import Any + +from . import LibraryIO +from .Common.SolutionIdGen import encode_solution_uid, regenerate_solution_uid + + +def find_solution_by_index( + data: dict[str, Any], + solution_index: int, +) -> dict[str, Any]: + """Return the solution dict with the given local ``SolutionIndex``. + + Args: + data: Parsed library logic YAML data. + solution_index: Local ``SolutionIndex`` value to locate. + + Returns: + Matching solution dictionary from ``data["Solutions"]``. + + Raises: + ValueError: If ``Solutions`` is missing or no entry matches ``solution_index``. + """ + solutions = data.get("Solutions") + if not isinstance(solutions, list): + raise ValueError("Logic YAML does not contain a Solutions list") + + for solution in solutions: + if not isinstance(solution, dict): + continue + if int(solution.get("SolutionIndex", -1)) == solution_index: + return solution + + raise ValueError( + f"No solution with SolutionIndex={solution_index} found in logic YAML" + ) + + +def regenerate_uid_for_solution( + yaml_path: Path, + solution_index: int, + *, + inplace: bool = False, +) -> int: + """Regenerate ``SolutionUID`` for one solution in a logic YAML file. + + Args: + yaml_path: Path to the logic YAML file. + solution_index: Local ``SolutionIndex`` of the target solution. + inplace: When True, write the updated YAML back to ``yaml_path``. + + Returns: + Newly generated 64-bit ``SolutionUID``. + + Raises: + FileNotFoundError: If ``yaml_path`` does not exist. + ValueError: If the target solution cannot be found. + """ + yaml_path = yaml_path.resolve() + if not yaml_path.is_file(): + raise FileNotFoundError(f"Logic YAML not found: {yaml_path}") + + data = LibraryIO.readYAML(str(yaml_path)) + if not isinstance(data, dict): + raise ValueError(f"Expected dict-format logic YAML: {yaml_path}") + + solution = find_solution_by_index(data, solution_index) + new_uid = regenerate_solution_uid(solution) + LibraryIO.reorderSolutionsParams(data) + + if inplace: + LibraryIO.writeYAML(str(yaml_path), data) + + return new_uid + + +def _build_parser() -> argparse.ArgumentParser: + """Build the CLI argument parser. + + Returns: + Configured :class:`argparse.ArgumentParser`. + """ + parser = argparse.ArgumentParser( + description=( + "Regenerate SolutionUID for one solution in a logic YAML file. " + "Uses a fresh timestamp + random draw; no full-repo scan is required." + ), + ) + parser.add_argument( + "yaml_file", + help="Path to a logic YAML file (dict format with Solutions list)", + ) + parser.add_argument( + "--index", + type=int, + required=True, + dest="solution_index", + help="Local SolutionIndex of the solution to update", + ) + parser.add_argument( + "--inplace", + action="store_true", + help="Write the new SolutionUID back to the YAML file", + ) + return parser + + +def main(argv: list[str] | None = None) -> int: + """Entry point for ``TensileGenerateUID.py``. + + Args: + argv: Optional argument list; defaults to ``sys.argv[1:]``. + + Returns: + Process exit code (0 on success, 1 on error). + """ + parser = _build_parser() + args = parser.parse_args(argv) + + try: + new_uid = regenerate_uid_for_solution( + Path(args.yaml_file), + args.solution_index, + inplace=args.inplace, + ) + except (FileNotFoundError, ValueError) as exc: + print(f"Error: {exc}", file=sys.stderr) + return 1 + + print(encode_solution_uid(new_uid)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_LibraryIO.py b/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_LibraryIO.py index a776bacdf013..1d1cfb100284 100644 --- a/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_LibraryIO.py +++ b/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_LibraryIO.py @@ -283,6 +283,38 @@ def test_reorder_solutions_params_puts_naming_keys_first() -> None: assert keys[3:] == ["WorkGroupMapping", "NumThreads"] +def test_reorder_solutions_params_puts_solution_uid_after_index() -> None: + """Verify that ``SolutionUID`` follows ``SolutionIndex``. + + Returns: + None. + + Raises: + AssertionError: If solution fields are not in canonical order. + """ + data = { + "Solutions": [ + { + "WorkGroupMapping": 8, + "SolutionUID": "0u34Z1KlVFjgl", + "SolutionIndex": 3, + "KernelNameMin": "kern_min", + "SolutionNameMin": "sol_min", + "NumThreads": 256, + } + ] + } + LibraryIO.reorderSolutionsParams(data) + keys = list(data["Solutions"][0].keys()) + assert keys[:4] == [ + "SolutionIndex", + "SolutionUID", + "KernelNameMin", + "SolutionNameMin", + ] + assert keys[4:] == ["WorkGroupMapping", "NumThreads"] + + def test_reorder_solutions_params_no_solutions_is_noop() -> None: """``reorderSolutionsParams`` returns immediately when ``Solutions`` is absent.""" data: dict[str, Any] = {} diff --git a/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_TensileMergeLibrary.py b/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_TensileMergeLibrary.py index 6b14cfbd9724..afa046d34908 100644 --- a/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_TensileMergeLibrary.py +++ b/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_TensileMergeLibrary.py @@ -226,6 +226,30 @@ def test_convert_sorts_solution_params_regardless_of_input_order( assert keys[:3] == ["SolutionIndex", "KernelNameMin", "SolutionNameMin"] assert keys[3:] == sorted(keys[3:]) + def test_convert_places_solution_uid_after_index(self, list_logic: list[Any]) -> None: + """Verify that conversion preserves canonical ``SolutionUID`` placement. + + Args: + list_logic: Legacy list-format logic fixture. + + Returns: + None. + + Raises: + AssertionError: If conversion drops or misorders ``SolutionUID``. + """ + data = deepcopy(list_logic) + data[5][0]["SolutionUID"] = "0u34Z1KlVFjgl" + out = convertToDict(data, "fixture.yaml") + keys = list(out["Solutions"][0].keys()) + assert keys[:4] == [ + "SolutionIndex", + "SolutionUID", + "KernelNameMin", + "SolutionNameMin", + ] + assert keys[4:] == sorted(keys[4:]) + class TestNormalizeDictLibraryLayout: """Tests for ``normalizeDictLibraryLayout`` (always runs on dict input).""" diff --git a/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_solution_uid_base62.py b/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_solution_uid_base62.py new file mode 100644 index 000000000000..0ba7ea8a8c72 --- /dev/null +++ b/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_solution_uid_base62.py @@ -0,0 +1,146 @@ +# Copyright Advanced Micro Devices, Inc., or its affiliates. +# SPDX-License-Identifier: MIT + +"""Tests for the canonical SolutionUID base62 representation.""" + +from __future__ import annotations + +import pytest + +from Tensile.Common.SolutionIdGen import ( + UINT64_MAX, + decode_solution_uid, + encode_solution_uid, +) + +pytestmark = pytest.mark.unit + + +@pytest.mark.parametrize( + ("solution_uid", "encoded_uid"), + [ + (0, "0u0"), + (1, "0u1"), + (9, "0u9"), + (10, "0uA"), + (35, "0uZ"), + (36, "0ua"), + (61, "0uz"), + (62, "0u10"), + (UINT64_MAX, "0uLygHa16AHYF"), + ], +) +def test_solution_uid_base62_round_trip( + solution_uid: int, + encoded_uid: str, +) -> None: + """Encode and decode representative unsigned 64-bit values. + + Args: + solution_uid: Numeric UID under test. + encoded_uid: Expected canonical representation. + + Returns: + None. + + Raises: + AssertionError: If either codec direction produces an unexpected value. + """ + assert encode_solution_uid(solution_uid) == encoded_uid + assert decode_solution_uid(encoded_uid) == solution_uid + + +def test_decode_solution_uid_accepts_uppercase_prefix() -> None: + """Accept ``0U`` as a read-only alias for the canonical ``0u`` prefix. + + Returns: + None. + + Raises: + AssertionError: If the uppercase prefix is rejected or changes the value. + """ + assert decode_solution_uid("0UABC") == decode_solution_uid("0uABC") + assert encode_solution_uid(decode_solution_uid("0UABC")) == "0uABC" + + +@pytest.mark.parametrize("encoded_uid", ["", "0", "0u", "u1", "0x1", "1"]) +def test_decode_solution_uid_rejects_missing_or_invalid_prefix( + encoded_uid: str, +) -> None: + """Reject representations without a complete supported prefix. + + Args: + encoded_uid: Invalid representation under test. + + Returns: + None. + + Raises: + AssertionError: If an invalid representation is accepted. + """ + with pytest.raises(ValueError, match="0u or 0U"): + decode_solution_uid(encoded_uid) + + +@pytest.mark.parametrize("encoded_uid", ["0u00", "0u01", "0u0ABC"]) +def test_decode_solution_uid_rejects_leading_zeros(encoded_uid: str) -> None: + """Reject non-canonical payloads with leading zero digits. + + Args: + encoded_uid: Non-canonical representation under test. + + Returns: + None. + + Raises: + AssertionError: If a leading-zero representation is accepted. + """ + with pytest.raises(ValueError, match="leading zeros"): + decode_solution_uid(encoded_uid) + + +@pytest.mark.parametrize("encoded_uid", ["0u+", "0u_", "0u/", "0uA-B"]) +def test_decode_solution_uid_rejects_invalid_characters(encoded_uid: str) -> None: + """Reject characters outside the frozen base62 alphabet. + + Args: + encoded_uid: Invalid representation under test. + + Returns: + None. + + Raises: + AssertionError: If a non-base62 character is accepted. + """ + with pytest.raises(ValueError, match="base62 character"): + decode_solution_uid(encoded_uid) + + +def test_solution_uid_codec_rejects_uint64_overflow() -> None: + """Reject integer and text values larger than ``uint64``. + + Returns: + None. + + Raises: + AssertionError: If either codec accepts an overflowing value. + """ + with pytest.raises(ValueError, match="out of range"): + encode_solution_uid(UINT64_MAX + 1) + with pytest.raises(ValueError, match="out of range"): + decode_solution_uid("0uLygHa16AHYG") + + +def test_solution_uid_codec_rejects_negative_and_non_string_inputs() -> None: + """Reject negative integers and non-string encoded representations. + + Returns: + None. + + Raises: + AssertionError: If either invalid input is accepted. + """ + with pytest.raises(ValueError, match="out of range"): + encode_solution_uid(-1) + with pytest.raises(TypeError, match="must be a string"): + decode_solution_uid(1) # type: ignore[arg-type] diff --git a/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_solution_uid_concurrency.py b/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_solution_uid_concurrency.py new file mode 100644 index 000000000000..26536c04591e --- /dev/null +++ b/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_solution_uid_concurrency.py @@ -0,0 +1,239 @@ +# Copyright Advanced Micro Devices, Inc., or its affiliates. +# SPDX-License-Identifier: MIT + +"""Concurrent uniqueness tests for :mod:`Tensile.Common.SolutionIdGen`.""" + +from __future__ import annotations + +import multiprocessing as mp +from typing import List + +import pytest + +from Tensile.Common.SolutionIdGen import ( + EPOCH_MS, + MS_MASK, + RANDOM_MASK, + RANDOM_SHIFT, + _ms_since_epoch, + decode_solution_id, + decode_solution_uid, + encode_solution_uid, + ensure_solution_uid, + generate_solution_id, + read_solution_uid, + regenerate_solution_uid, +) + +pytestmark = pytest.mark.unit + +PROCESS_COUNT = 8 +IDS_PER_PROCESS = 4 + + +def _generate_after_barrier( + barrier: mp.synchronize.Barrier, + result_queue: mp.Queue, +) -> None: + """Generate unique UIDs once all workers have reached the barrier. + + A repeated draw in this process is discarded and drawn again. The set stays + in this worker and is not passed to the generator. + + Args: + barrier: Shared process barrier sized to the worker count. + result_queue: Queue used to return generated UIDs to the parent process. + + Raises: + RuntimeError: If unique IDs cannot be collected after repeated draws. + """ + barrier.wait() + seen: set[int] = set() + attempts = 0 + max_attempts = IDS_PER_PROCESS * 100 + while len(seen) < IDS_PER_PROCESS: + if attempts >= max_attempts: + raise RuntimeError( + f"Could not draw {IDS_PER_PROCESS} unique solution IDs " + f"after {max_attempts} attempts" + ) + attempts += 1 + seen.add(generate_solution_id()) + result_queue.put(list(seen)) + + +def _run_concurrent_generation( + process_count: int, + ids_per_process: int, +) -> List[int]: + """Run concurrent SolutionUID generation across multiple processes. + + Args: + process_count: Number of worker processes to spawn. + ids_per_process: UIDs each worker generates after synchronizing. + + Returns: + Flat list of all generated SolutionUID values. + """ + ctx = mp.get_context("spawn") + barrier = ctx.Barrier(process_count) + result_queue: mp.Queue = ctx.Queue() + processes = [ + ctx.Process( + target=_generate_after_barrier, + args=(barrier, result_queue), + ) + for _ in range(process_count) + ] + for process in processes: + process.start() + for process in processes: + process.join() + assert process.exitcode == 0, f"worker exited with code {process.exitcode}" + + batches: List[List[int]] = [] + for _ in range(process_count): + batches.append(result_queue.get(timeout=30)) + return [solution_uid for batch in batches for solution_uid in batch] + + +def test_8_processes_generate_unique_solution_uids() -> None: + """8 processes generating UIDs concurrently must not collide.""" + solution_uids = _run_concurrent_generation(PROCESS_COUNT, IDS_PER_PROCESS) + expected_count = PROCESS_COUNT * IDS_PER_PROCESS + assert len(solution_uids) == expected_count + + unique_uids = set(solution_uids) + duplicates = len(solution_uids) - len(unique_uids) + assert duplicates == 0, ( + f"Expected all {expected_count} SolutionUID values to be unique, " + f"but found {duplicates} collision(s)" + ) + + +def test_decode_solution_id_splits_40_plus_24_layout() -> None: + """``decode_solution_id`` extracts 40-bit ms and 24-bit random fields.""" + uid = generate_solution_id(now_ms=EPOCH_MS + 12345) + ms_part, random_part = decode_solution_id(uid) + assert ms_part == 12345 + assert 0 <= random_part <= RANDOM_MASK + assert uid == (ms_part << RANDOM_SHIFT) | random_part + + +def test_read_solution_uid_reads_solution_uid_field() -> None: + """SolutionUID in YAML state should be returned as-is.""" + assert read_solution_uid({"SolutionUID": "0u5"}) == 5 + + +def test_read_solution_uid_generates_when_missing() -> None: + """Missing SolutionUID should trigger ephemeral UID generation.""" + generated = read_solution_uid({}) + assert generated > 0 + + +def test_ensure_solution_uid_assigns_solution_uid() -> None: + """Missing UID fields should be assigned as SolutionUID.""" + solution: dict[str, str] = {} + ensure_solution_uid(solution) + assert "SolutionUID" in solution + assert solution["SolutionUID"].startswith("0u") + assert decode_solution_uid(solution["SolutionUID"]) > 0 + + +def test_multi_winner_batch_assigns_unique_solution_uids() -> None: + """Multi-winner LibraryLogic scale (10) should stay unique without retry.""" + import time + + now_ms = int(time.time() * 1000) + winner_count = 10 + solutions = [{} for _ in range(winner_count)] + for solution in solutions: + ensure_solution_uid(solution) + + uids = [decode_solution_uid(solution["SolutionUID"]) for solution in solutions] + assert len(set(uids)) == winner_count + expected_ms = now_ms - EPOCH_MS + assert all(decode_solution_id(uid)[0] >= expected_ms for uid in uids) + + +def test_ms_since_epoch_rejects_time_before_epoch() -> None: + """Timestamps before the SolutionUID epoch are out of range. + + Args: + None. + + Returns: + None. + + Raises: + AssertionError: If a pre-epoch timestamp is accepted. + """ + with pytest.raises(ValueError, match="out of range"): + _ms_since_epoch(EPOCH_MS - 1) + + +def test_ms_since_epoch_rejects_time_after_40_bit_limit() -> None: + """Timestamps that overflow the 40-bit millisecond field are rejected. + + Args: + None. + + Returns: + None. + + Raises: + AssertionError: If a post-mask timestamp is accepted. + """ + with pytest.raises(ValueError, match="out of range"): + generate_solution_id(now_ms=EPOCH_MS + MS_MASK + 1) + + +def test_ms_since_epoch_accepts_zero_and_max_offset() -> None: + """Epoch and the last representable millisecond both encode. + + Args: + None. + + Returns: + None. + + Raises: + AssertionError: If either bound is rejected or mis-encoded. + """ + assert _ms_since_epoch(EPOCH_MS) == 0 + assert _ms_since_epoch(EPOCH_MS + MS_MASK) == MS_MASK + + +def test_ensure_solution_uid_preserves_existing_uid() -> None: + """``ensure_solution_uid`` is a no-op when ``SolutionUID`` is already set. + + Args: + None. + + Returns: + None. + + Raises: + AssertionError: If an existing UID is overwritten. + """ + solution = {"SolutionUID": "0ug"} + ensure_solution_uid(solution) + assert solution["SolutionUID"] == "0ug" + + +def test_regenerate_solution_uid_replaces_existing_value() -> None: + """``regenerate_solution_uid`` always writes a new UID. + + Args: + None. + + Returns: + None. + + Raises: + AssertionError: If the previous UID is kept. + """ + solution = {"SolutionUID": "0ug"} + new_uid = regenerate_solution_uid(solution) + assert new_uid != 42 + assert solution["SolutionUID"] == encode_solution_uid(new_uid) diff --git a/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_solution_uid_uniqueness.py b/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_solution_uid_uniqueness.py new file mode 100644 index 000000000000..ce8ca3b5233e --- /dev/null +++ b/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_solution_uid_uniqueness.py @@ -0,0 +1,258 @@ +# Copyright Advanced Micro Devices, Inc., or its affiliates. +# SPDX-License-Identifier: MIT + +"""Verify SolutionUID uniqueness across shipped logic YAML files.""" + +from __future__ import annotations + +import importlib.util +import multiprocessing as mp +import os +from pathlib import Path +from types import ModuleType +from typing import Dict, List, Tuple + +import pytest + + +def _load_solution_id_gen() -> ModuleType: + """Load SolutionIdGen without importing ``Tensile.Common``. + + The ``library-uniqueness`` tox env skips installing Tensile and rocisa. + ``from Tensile.Common.SolutionIdGen import ...`` still executes + ``Tensile.Common.__init__``, which imports rocisa. + + Args: + None. + + Returns: + The loaded ``SolutionIdGen`` module. + + Raises: + FileNotFoundError: If ``SolutionIdGen.py`` is missing. + ImportError: If the module spec or loader cannot be created. + """ + module_path = Path(__file__).resolve().parents[2] / "Common" / "SolutionIdGen.py" + if not module_path.is_file(): + raise FileNotFoundError(f"SolutionIdGen.py not found: {module_path}") + spec = importlib.util.spec_from_file_location("tensile_solution_id_gen", module_path) + if spec is None or spec.loader is None: + raise ImportError(f"Could not load SolutionIdGen from {module_path}") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + +_solution_id_gen = _load_solution_id_gen() +decode_solution_uid = _solution_id_gen.decode_solution_uid +encode_solution_uid = _solution_id_gen.encode_solution_uid + +pytestmark = pytest.mark.unit + +DEFAULT_LOGIC_ROOT = ( + Path(__file__).resolve().parents[4] + / "library" + / "src" + / "amd_detail" + / "rocblaslt" + / "src" + / "Tensile" + / "Logic" +) + +# The shipped logic corpus lives in the hipBLASLt product tree, not in +# tensilelite, so it is absent whenever this file is run from installed +# tensilelite test artifacts. The enforcement point is the library-uniqueness +# component job, which checks out the source tree; this copy is a +# convenience/local-dev signal otherwise -- hence ``skipif`` (an unmet +# precondition), not ``xfail`` (an expected failure). +_LOGIC_ROOT = Path(os.environ.get("HIPBLASLT_LOGIC_ROOT") or DEFAULT_LOGIC_ROOT) + +_needs_logic_dir = pytest.mark.skipif( + not _LOGIC_ROOT.is_dir(), + reason="Logic files not found: https://github.com/ROCm/rocm-libraries/issues/7481", +) + +STRICT_ENV = "HIPBLASLT_REQUIRE_SOLUTION_UID" + + +def _collect_yaml_files(logic_root: Path) -> List[Path]: + """Return logic YAML files that contain solution definitions. + + Args: + logic_root: Root directory containing arch/datatype logic trees. + + Returns: + Sorted list of YAML file paths. + """ + asm_full = logic_root / "asm_full" + if asm_full.is_dir(): + return sorted(asm_full.rglob("*.yaml")) + return sorted(logic_root.rglob("*.yaml")) + + +def test_collect_yaml_files_includes_all_yaml_names(tmp_path: Path) -> None: + """Verify discovery does not require the ``_UserArgs.yaml`` suffix. + + Args: + tmp_path: Pytest temporary-directory fixture. + + Returns: + None. + + Raises: + AssertionError: If any YAML file under ``asm_full`` is omitted. + """ + asm_full = tmp_path / "asm_full" + asm_full.mkdir() + user_args = asm_full / "logic_UserArgs.yaml" + conventional = asm_full / "logic.yaml" + ignored = asm_full / "README.txt" + user_args.touch() + conventional.touch() + ignored.touch() + + assert _collect_yaml_files(tmp_path) == [conventional, user_args] + + +def _scan_yaml_file(yaml_path: Path) -> Tuple[List[Tuple[int, str, int]], int]: + """Extract SolutionUID entries from one logic YAML. + + Args: + yaml_path: Path to a logic YAML file. + + Returns: + Tuple of (entries, missing_count) where each entry is + ``(SolutionUID, yaml_path, SolutionIndex)`` and + ``missing_count`` counts solutions without the field. + + Raises: + TypeError: If a UID value is not a string. + ValueError: If a UID is malformed, zero, out of range, or an index + value is not an integer. + """ + entries: List[Tuple[int, str, int]] = [] + missing = 0 + current_index: int | None = None + current_uid: int | None = None + + def flush_solution() -> None: + """Record the current solution before scanning the next one. + + Args: + None. + + Returns: + None. + + Raises: + None. + """ + nonlocal missing, current_index, current_uid + if current_index is None: + return + if current_uid is None: + missing += 1 + else: + entries.append((current_uid, str(yaml_path), current_index)) + current_index = None + current_uid = None + + with yaml_path.open(encoding="utf-8", errors="replace") as handle: + for line in handle: + stripped = line.strip() + if stripped.startswith("- SolutionIndex:") or stripped.startswith("SolutionIndex:"): + flush_solution() + current_index = int(stripped.split(":", 1)[1].strip()) + elif current_index is not None and stripped.startswith("SolutionUID:"): + encoded_uid = stripped.split(":", 1)[1].strip() + current_uid = decode_solution_uid(encoded_uid) + if current_uid == 0: + raise ValueError( + f"Stored SolutionUID must not be zero: {yaml_path} " + f"(SolutionIndex={current_index})" + ) + + flush_solution() + return entries, missing + + +def collect_solution_uids( + logic_root: Path, + *, + process_count: int, +) -> Tuple[List[Tuple[int, str, int]], int]: + """Collect SolutionUID values using multiple processes. + + Args: + logic_root: Root directory containing logic YAML files. + process_count: Number of worker processes to use. + + Returns: + Tuple of all ``(uid, yaml_path, local_index)`` entries and total missing count. + """ + yaml_files = _collect_yaml_files(logic_root) + if not yaml_files: + return [], 0 + + process_count = max(1, min(process_count, len(yaml_files))) + ctx = mp.get_context("spawn") + all_entries: List[Tuple[int, str, int]] = [] + total_missing = 0 + with ctx.Pool(process_count) as pool: + for entries, missing in pool.imap(_scan_yaml_file, yaml_files): + all_entries.extend(entries) + total_missing += missing + return all_entries, total_missing + + +def find_duplicate_uids( + entries: List[Tuple[int, str, int]], +) -> Dict[int, List[Tuple[str, int]]]: + """Find duplicate SolutionUID values. + + Args: + entries: Output from :func:`collect_solution_uids`. + + Returns: + Mapping from duplicate UID to list of ``(yaml_path, local SolutionIndex)``. + """ + seen: Dict[int, List[Tuple[str, int]]] = {} + for uid, yaml_path, local_index in entries: + seen.setdefault(uid, []).append((yaml_path, local_index)) + return {uid: locations for uid, locations in seen.items() if len(locations) > 1} + + +@pytest.fixture(name="logic_root") +def fixture_logic_root() -> Path: + """Resolve the logic YAML root, overridable via env var. + + Returns: + Path to ``Logic/`` under the hipBLASLt library tree. + """ + return _LOGIC_ROOT + + +@_needs_logic_dir +def test_solution_uid_unique_across_logic_files(logic_root: Path) -> None: + """All present SolutionUID values must be unique repo-wide.""" + process_count = int(os.environ.get("HIPBLASLT_UID_TEST_PROCESSES", "8")) + entries, missing = collect_solution_uids(logic_root, process_count=process_count) + if not entries and missing == 0: + pytest.fail(f"No solution definitions found under {logic_root}") + + duplicates = find_duplicate_uids(entries) + if duplicates: + lines = ["Duplicate SolutionUID values detected:"] + for uid, locations in sorted(duplicates.items()): + lines.append(f" uid {encode_solution_uid(uid)}:") + for yaml_path, local_index in locations: + lines.append(f" {yaml_path} (SolutionIndex={local_index})") + pytest.fail("\n".join(lines)) + + strict = os.environ.get(STRICT_ENV, "").lower() in {"1", "true", "yes"} + if strict and missing > 0: + pytest.fail( + f"{missing} solution(s) under {logic_root} are missing SolutionUID " + f"(set {STRICT_ENV}=0 to warn only during migration)" + ) diff --git a/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_tensile_generate_uid.py b/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_tensile_generate_uid.py new file mode 100644 index 000000000000..a4d247192d47 --- /dev/null +++ b/projects/hipblaslt/tensilelite/Tensile/Tests/unit/test_tensile_generate_uid.py @@ -0,0 +1,272 @@ +# Copyright Advanced Micro Devices, Inc., or its affiliates. +# SPDX-License-Identifier: MIT + +"""Unit tests for :mod:`Tensile.TensileGenerateUID`.""" + +from __future__ import annotations + +import runpy +import sys +from pathlib import Path + +import pytest + +from Tensile import LibraryIO +from Tensile.Common.SolutionIdGen import ( + RANDOM_MASK, + RANDOM_SHIFT, + decode_solution_id, + decode_solution_uid, + encode_solution_uid, +) +from Tensile.TensileGenerateUID import ( + find_solution_by_index, + main, + regenerate_uid_for_solution, +) + +pytestmark = pytest.mark.unit + + +def _sample_logic_data() -> dict: + """Build minimal dict-format logic YAML content for tests. + + Returns: + Dict-format library logic data with two solutions. + """ + return { + "MinimumRequiredVersion": "0.0.0", + "ProblemType": {"OperationType": "GEMM"}, + "Solutions": [ + { + "SolutionIndex": 0, + "SolutionUID": "0u1c", + "SolutionNameMin": "sol0", + "KernelNameMin": "kern0", + "NumThreads": 256, + }, + { + "SolutionIndex": 1, + "SolutionUID": "0u3E", + "SolutionNameMin": "sol1", + "KernelNameMin": "kern1", + "NumThreads": 128, + }, + ], + } + + +def test_find_solution_by_index_returns_matching_solution() -> None: + """``find_solution_by_index`` locates the requested solution.""" + data = _sample_logic_data() + solution = find_solution_by_index(data, 1) + assert solution["SolutionUID"] == "0u3E" + + +def test_find_solution_by_index_raises_when_missing() -> None: + """Missing ``SolutionIndex`` values raise ``ValueError``.""" + with pytest.raises(ValueError, match="SolutionIndex=99"): + find_solution_by_index(_sample_logic_data(), 99) + + +def test_find_solution_by_index_raises_when_solutions_missing() -> None: + """Logic without a ``Solutions`` list raises ``ValueError``. + + Args: + None. + + Returns: + None. + + Raises: + AssertionError: If a non-list ``Solutions`` value is accepted. + """ + with pytest.raises(ValueError, match="Solutions list"): + find_solution_by_index({"ProblemType": {}}, 0) + with pytest.raises(ValueError, match="Solutions list"): + find_solution_by_index({"Solutions": {"0": {}}}, 0) + + +def test_find_solution_by_index_skips_non_dict_and_missing_index() -> None: + """Non-dict entries and missing ``SolutionIndex`` keys are skipped. + + Args: + None. + + Returns: + None. + + Raises: + AssertionError: If a later matching dict entry is not returned. + """ + data = { + "Solutions": [ + "not-a-solution", + {"SolutionNameMin": "no-index"}, + {"SolutionIndex": 3, "SolutionUID": "0u4q"}, + ] + } + solution = find_solution_by_index(data, 3) + assert solution["SolutionUID"] == "0u4q" + + +def test_regenerate_uid_for_solution_replaces_uid(tmp_path: Path) -> None: + """Regeneration updates only the targeted solution UID.""" + yaml_path = tmp_path / "logic.yaml" + data = _sample_logic_data() + LibraryIO.writeYAML(str(yaml_path), data) + + new_uid = regenerate_uid_for_solution(yaml_path, 1, inplace=True) + assert new_uid != 200 + + updated = LibraryIO.readYAML(str(yaml_path)) + assert updated["Solutions"][0]["SolutionUID"] == "0u1c" + assert updated["Solutions"][1]["SolutionUID"] == encode_solution_uid(new_uid) + + +def test_regenerate_uid_for_solution_without_inplace_does_not_write( + tmp_path: Path, +) -> None: + """Without ``--inplace`` the YAML file on disk is unchanged.""" + yaml_path = tmp_path / "logic.yaml" + data = _sample_logic_data() + LibraryIO.writeYAML(str(yaml_path), data) + + new_uid = regenerate_uid_for_solution(yaml_path, 0, inplace=False) + assert new_uid != 100 + + unchanged = LibraryIO.readYAML(str(yaml_path)) + assert unchanged["Solutions"][0]["SolutionUID"] == "0u1c" + + +def test_regenerate_uid_uses_40_plus_24_layout(tmp_path: Path) -> None: + """New UIDs follow the 40+24 bit layout.""" + yaml_path = tmp_path / "logic.yaml" + LibraryIO.writeYAML(str(yaml_path), _sample_logic_data()) + + new_uid = regenerate_uid_for_solution(yaml_path, 0, inplace=False) + ms_part, random_part = decode_solution_id(new_uid) + assert ms_part >= 0 + assert 0 <= random_part <= RANDOM_MASK + assert new_uid == (ms_part << RANDOM_SHIFT) | random_part + + +def test_main_prints_uid_and_returns_zero(capsys, tmp_path: Path) -> None: + """CLI main prints the new UID and exits successfully.""" + yaml_path = tmp_path / "logic.yaml" + LibraryIO.writeYAML(str(yaml_path), _sample_logic_data()) + + rc = main([str(yaml_path), "--index", "0"]) + captured = capsys.readouterr() + + assert rc == 0 + assert captured.out.strip().startswith("0u") + assert decode_solution_uid(captured.out.strip()) != 100 + + +def test_main_inplace_updates_file(tmp_path: Path) -> None: + """CLI ``--inplace`` persists the regenerated UID.""" + yaml_path = tmp_path / "logic.yaml" + LibraryIO.writeYAML(str(yaml_path), _sample_logic_data()) + + rc = main([str(yaml_path), "--index", "1", "--inplace"]) + assert rc == 0 + + updated = LibraryIO.readYAML(str(yaml_path)) + assert updated["Solutions"][1]["SolutionUID"] != "0u3E" + + +def test_main_returns_error_for_missing_index(tmp_path: Path) -> None: + """CLI returns exit code 1 when the solution index is not found.""" + yaml_path = tmp_path / "logic.yaml" + LibraryIO.writeYAML(str(yaml_path), _sample_logic_data()) + + rc = main([str(yaml_path), "--index", "42"]) + assert rc == 1 + + +def test_regenerate_uid_for_solution_raises_for_missing_file(tmp_path: Path) -> None: + """A missing YAML path raises ``FileNotFoundError``. + + Args: + tmp_path: Pytest temporary-directory fixture. + + Returns: + None. + + Raises: + AssertionError: If a missing file is treated as success. + """ + missing = tmp_path / "does-not-exist.yaml" + with pytest.raises(FileNotFoundError, match="Logic YAML not found"): + regenerate_uid_for_solution(missing, 0) + + +def test_regenerate_uid_for_solution_rejects_non_dict_yaml(tmp_path: Path) -> None: + """List-format YAML is rejected as not dict-format logic. + + Args: + tmp_path: Pytest temporary-directory fixture. + + Returns: + None. + + Raises: + AssertionError: If non-dict YAML is accepted. + """ + yaml_path = tmp_path / "list.yaml" + yaml_path.write_text("- not\n- a\n- dict\n", encoding="utf-8") + with pytest.raises(ValueError, match="dict-format logic YAML"): + regenerate_uid_for_solution(yaml_path, 0) + + +def test_main_returns_error_for_missing_file( + tmp_path: Path, + capsys: pytest.CaptureFixture[str], +) -> None: + """CLI reports ``FileNotFoundError`` on stderr and returns 1. + + Args: + tmp_path: Pytest temporary-directory fixture. + capsys: Pytest stdout/stderr capture fixture. + + Returns: + None. + + Raises: + AssertionError: If the CLI does not fail for a missing path. + """ + missing = tmp_path / "missing.yaml" + rc = main([str(missing), "--index", "0"]) + captured = capsys.readouterr() + assert rc == 1 + assert "Error:" in captured.err + assert "Logic YAML not found" in captured.err + + +def test_module_main_guard_exits_zero( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + """``python -m Tensile.TensileGenerateUID`` runs ``main`` via ``__main__``. + + Args: + tmp_path: Pytest temporary-directory fixture. + monkeypatch: Pytest monkeypatch fixture. + + Returns: + None. + + Raises: + AssertionError: If the module entry point does not exit 0. + """ + yaml_path = tmp_path / "logic.yaml" + LibraryIO.writeYAML(str(yaml_path), _sample_logic_data()) + monkeypatch.setattr( + sys, + "argv", + ["TensileGenerateUID.py", str(yaml_path), "--index", "0"], + ) + with pytest.raises(SystemExit) as excinfo: + runpy.run_module("Tensile.TensileGenerateUID", run_name="__main__") + assert excinfo.value.code == 0 diff --git a/projects/hipblaslt/tensilelite/tox.ini b/projects/hipblaslt/tensilelite/tox.ini index 9ffd5205b9dc..bd17a19ed747 100644 --- a/projects/hipblaslt/tensilelite/tox.ini +++ b/projects/hipblaslt/tensilelite/tox.ini @@ -85,6 +85,16 @@ commands = pip install {toxinidir}/rocisa/ pytest -v --basetemp={envtmpdir} {posargs} +[testenv:library-uniqueness] +description = "Checks SolutionUID uniqueness across shipped logic YAML files." +basepython = python3 +skip_install = true +deps = + pytest +commands = + pytest -v --noconftest \ + {toxinidir}/Tensile/Tests/unit/test_solution_uid_uniqueness.py + [testenv:comm] description = "Runs the cross-device (comm) YAML tests. Builds the client with the SDMA transport enabled and runs pytest without xdist; the tests skip themselves unless two devices are visible." basepython = python3