Files
wehub-resource-sync 59a0a3844c
PR Test AMD / cancel-on-close (push) Has been skipped
PR Test NVIDIA ARM / scan (push) Has been skipped
PR Test NVIDIA / cancel-on-close (push) Has been skipped
PR Test AMD / scan (push) Has been skipped
PR Test NVIDIA ARM / cancel-on-close (push) Has been skipped
PR Test NVIDIA / scan (push) Has been skipped
Release Docker Images / build (cu129-torch-2.11.0) (push) Has been skipped
Release Docker Images / build (cu130-torch-2.11.0) (push) Has been skipped
Release PyPI / publish (push) Has been skipped
Scheduler Python Test / test (push) Successful in 27m19s
Docs / build (push) Successful in 28m8s
Scheduler C++ Test / test (push) Successful in 28m19s
Scheduler C++ Test / test-flat (push) Successful in 28m18s
Docs / deploy (push) Has been cancelled
PR Test AMD / finish (push) Has been cancelled
PR Test NVIDIA / finish (push) Has been cancelled
PR Test NVIDIA ARM / finish (push) Has been cancelled
PR Test NVIDIA ARM / ${{ matrix.name }} (${{ matrix.runner }}) (push) Has been cancelled
PR Test AMD / ${{ matrix.name }} (${{ matrix.runner }}) (push) Has been cancelled
PR Test NVIDIA / ${{ matrix.name }} (${{ matrix.runner }}) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:32:31 +08:00

139 lines
4.4 KiB
Python

# Copyright (c) 2026 LightSeek Foundation
#
# 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.
from __future__ import annotations
from typing import Any, Callable
import torch
from tokenspeed_kernel.signature import FormatSignature
__all__ = [
"InputGenerator",
"get_benchmark_shapes",
"get_input_generator",
"get_standard_shapes",
"set_benchmark_shapes",
"set_input_generator",
"set_standard_shapes",
]
InputGeneratorFactory = Callable[..., "InputGenerator"]
_INPUT_GENERATORS: dict[tuple[str, str], InputGeneratorFactory] = {}
_STANDARD_SHAPES: dict[tuple[str, str], list[dict[str, Any]]] = {}
_BENCHMARK_SHAPES: dict[tuple[str, str], list[dict[str, Any]]] = {}
class InputGenerator:
"""Generates test inputs for a given operator family/mode."""
def __init__(
self,
op_family: str,
op_mode: str,
dtype: torch.dtype,
traits: dict,
*,
format_signature: FormatSignature | None = None,
device: str | None = None,
seed: int = 42,
) -> None:
self.op_family = op_family
self.op_mode = op_mode
self.dtype = dtype
self.traits = traits
self.format_signature = format_signature
self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
rng_device = "cuda" if self.device.startswith("cuda") else "cpu"
self.rng = torch.Generator(device=rng_device).manual_seed(seed)
def generate(self, **kwargs: Any) -> dict[str, Any]:
raise NotImplementedError
def set_input_generator(
op_family: str,
op_mode: str,
generator_factory: InputGeneratorFactory,
) -> None:
_INPUT_GENERATORS[(op_family, op_mode)] = generator_factory
def set_standard_shapes(
op_family: str,
op_mode: str,
shapes: list[dict[str, Any]],
) -> None:
_STANDARD_SHAPES[(op_family, op_mode)] = [dict(shape) for shape in shapes]
def set_benchmark_shapes(
op_family: str,
op_mode: str,
shapes: list[dict[str, Any]],
) -> None:
_BENCHMARK_SHAPES[(op_family, op_mode)] = [dict(shape) for shape in shapes]
def get_input_generator(
op_family: str,
op_mode: str,
dtype: torch.dtype,
traits: dict,
*,
format_signature: FormatSignature | None = None,
device: str | None = None,
seed: int = 42,
) -> InputGenerator:
factory = _INPUT_GENERATORS.get((op_family, op_mode))
if factory is None:
known = ", ".join(f"{f}.{m}" for f, m in sorted(_INPUT_GENERATORS)) or "none"
raise KeyError(
f"No input generator registered for {op_family}.{op_mode}. Known: {known}"
)
return factory(
op_family,
op_mode,
dtype,
traits,
format_signature=format_signature,
device=device,
seed=seed,
)
def get_standard_shapes(op_family: str, op_mode: str) -> list[dict[str, Any]]:
shapes = _STANDARD_SHAPES.get((op_family, op_mode))
if shapes is None:
known = ", ".join(f"{f}.{m}" for f, m in sorted(_STANDARD_SHAPES)) or "none"
raise KeyError(
f"No standard shapes registered for {op_family}.{op_mode}. Known: {known}"
)
return [dict(shape) for shape in shapes]
def get_benchmark_shapes(op_family: str, op_mode: str) -> list[dict[str, Any]]:
shapes = _BENCHMARK_SHAPES.get((op_family, op_mode))
if shapes is not None:
return [dict(shape) for shape in shapes]
return get_standard_shapes(op_family, op_mode)