registry.py 1.6 KB
Newer Older
cmx's avatar
cmx committed
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
"""
Vendor registry for Liger-Kernel multi-backend support.

This module defines VendorInfo and the registry for vendor registration.
Each vendor registers itself by calling register_vendor() in its __init__.py.
"""

from dataclasses import dataclass
from typing import Optional

# Dynamically get backends package path to avoid hardcoding
_BACKENDS_PACKAGE = __name__.rsplit(".", 1)[0]  # "liger_kernel.ops.backends"


@dataclass
class VendorInfo:
    """
    Information about a chip vendor and its supported device.

    Attributes:
        vendor: Vendor name (e.g., "ascend", "intel", "nvidia")
        device: Device type this vendor supports (e.g., "npu", "xpu")
    """

    vendor: str
    device: str

    @property
    def module_path(self) -> str:
        """Auto-generated module path based on vendor name."""
        return f"{_BACKENDS_PACKAGE}._{self.vendor}.ops"


# Registry mapping device types to their vendor info
# Vendors register themselves via register_vendor()
VENDOR_REGISTRY: dict[str, VendorInfo] = {}


def register_vendor(vendor_info: VendorInfo) -> None:
    """
    Register a vendor's info in the global registry.

    This should be called in each vendor's __init__.py to register itself.

    Args:
        vendor_info: VendorInfo instance to register
    """
    VENDOR_REGISTRY[vendor_info.device] = vendor_info


def get_vendor_for_device(device: str) -> Optional[VendorInfo]:
    """
    Get the VendorInfo for a given device type.

    Args:
        device: Device type (e.g., "npu", "xpu")

    Returns:
        VendorInfo if found, None otherwise
    """
    return VENDOR_REGISTRY.get(device)