Download code/models/common/tests/test_legacy_sampling_capabilities.py from changh95/gemma4-31b-it-dflash: direct link, hf CLI and curl.
- Browser
- Download file 2.84 kB
-
https://huggingface.co/changh95/gemma4-31b-it-dflash/resolve/main/code/models/common/tests/test_legacy_sampling_capabilities.py
- Command line
-
hf download hf://changh95/gemma4-31b-it-dflash/code/models/common/tests/test_legacy_sampling_capabilities.py
-
curl -L -o test_legacy_sampling_capabilities.py https://huggingface.co/changh95/gemma4-31b-it-dflash/resolve/main/code/models/common/tests/test_legacy_sampling_capabilities.py
2.84 kB
| # SPDX-FileCopyrightText: © 2026 Tenstorrent AI ULC | |
| # SPDX-License-Identifier: Apache-2.0 | |
| """vLLM generators that sample on device must advertise the device sampler's top-k limit, | |
| and the TTTv1 generators with device grammar must keep their sampling capabilities. | |
| Host-only: reads the generator sources with ``ast``; nothing is imported or opened. | |
| These checks used to run from the in-tree TTTv2 sampling tests. tt-transformers skips | |
| them because the generators live here, so they stay in tt-metal. | |
| """ | |
| import ast | |
| from pathlib import Path | |
| import pytest | |
| REPOSITORY_ROOT = Path(__file__).resolve().parents[3] | |
| DEVICE_TOP_K = 32 | |
| def _advertised_device_sampling_capabilities(relative_path): | |
| source = (REPOSITORY_ROOT / relative_path).read_text(encoding="utf-8") | |
| tree = ast.parse(source, filename=relative_path) | |
| advertised = [] | |
| for node in tree.body: | |
| if not isinstance(node, ast.ClassDef): | |
| continue | |
| for statement in node.body: | |
| if not isinstance(statement, ast.Assign): | |
| continue | |
| if not any( | |
| isinstance(target, ast.Name) and target.id == "model_capabilities" for target in statement.targets | |
| ): | |
| continue | |
| capabilities = ast.literal_eval(statement.value) | |
| if capabilities.get("supports_sample_on_device"): | |
| advertised.append((node.name, capabilities)) | |
| return advertised | |
| def test_device_sampling_capabilities_declare_the_exact_limit(relative_path): | |
| advertised = _advertised_device_sampling_capabilities(relative_path) | |
| assert advertised | |
| assert all(capabilities.get("max_device_top_k") == DEVICE_TOP_K for _, capabilities in advertised) | |
| def test_device_grammar_generators_retain_sampling_capabilities(class_name): | |
| relative_path = "models/ttt_compat/tt/generator_vllm.py" | |
| source = (REPOSITORY_ROOT / relative_path).read_text(encoding="utf-8") | |
| tree = ast.parse(source, filename=relative_path) | |
| class_node = next(node for node in tree.body if isinstance(node, ast.ClassDef) and node.name == class_name) | |
| assignment = next( | |
| node | |
| for node in class_node.body | |
| if isinstance(node, ast.Assign) | |
| and any(isinstance(target, ast.Name) and target.id == "model_capabilities" for target in node.targets) | |
| ) | |
| capabilities = ast.literal_eval(assignment.value) | |
| assert capabilities["supports_sample_on_device"] is True | |
| assert capabilities["supports_device_grammar"] is True | |
| assert capabilities["max_device_top_k"] == DEVICE_TOP_K | |