Coverage for / home / jenkins / .local / lib / python3.10 / site-packages / hyper_parallel / components / ops / spec.py: 100%
18 statements
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
« prev ^ index » next coverage.py v7.13.1, created at 2026-12-05 05:12 +0800
1# Copyright 2026 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""Typed contracts for selectable operator implementations."""
17from collections.abc import Callable
18from dataclasses import dataclass
19from typing import Generic, Literal, TypeVar
21DeviceType = Literal["cuda", "cpu", "npu"]
22Fn = TypeVar("Fn", bound=Callable[..., object])
25@dataclass(frozen=True, init=False)
26class OpSpec(Generic[Fn]):
27 """Identify one callable contract within a YAML-configurable family."""
29 config_key: str
30 variant: str = "standard"
32 def __init__(self, *, config_key: str, variant: str = "standard") -> None:
33 """Initialize the keyword-only contract without version-specific dataclass options."""
34 object.__setattr__(self, "config_key", config_key)
35 object.__setattr__(self, "variant", variant)
36 self.__post_init__()
38 def __post_init__(self) -> None:
39 if not isinstance(self.config_key, str) or not self.config_key.strip():
40 raise ValueError("Operator config_key must be a nonempty string")
41 if not isinstance(self.variant, str) or not self.variant.strip():
42 raise ValueError("Operator variant must be a nonempty string")