"""Platform information utilities for pypto_pro.runtime.
Provides runtime access to NPU hardware information (SOC version, core count).
Uses pypto_impl binding for arch detection and core count query.
Usage::
from pypto_pro.runtime.platform import get_platform_info
info = get_platform_info()
print(info.soc_version) # e.g. "DAV_2201"
print(info.core_num) # e.g. 20
# Use in kernel launch:
kernel[None, info.core_num](q, k, v, o)
"""
from __future__ import annotations
from dataclasses import dataclass
import logging
from typing import Optional
logger = logging.getLogger(__name__)
_ARCH_MAP = {
"DAV_1001": "a3",
"DAV_2201": "a3",
"DAV_3510": "a5",
}
@dataclass
class PlatformInfo:
"""NPU platform hardware information."""
soc_version: str = ""
core_num: int = 0
cube_core_num: int = 0
vector_core_num: int = 0
@property
def arch(self) -> str:
"""Infer compilation arch from SOC version string.
Returns:
"a5" for DAV_3510 (950 series), "a3" for DAV_2201/DAV_1001, "" if unknown.
"""
if not self.soc_version:
return ""
return _ARCH_MAP.get(self.soc_version, "a3")
_cached_info: Optional[PlatformInfo] = None
def _get_npu_arch() -> str:
"""Get NPU architecture string via pypto_impl binding.
Returns:
NPUArch string like "DAV_2201", "DAV_3510", or "" if unavailable.
"""
try:
from pypto import pypto_impl
return pypto_impl.GetNPUArch()
except (ImportError, AttributeError, RuntimeError) as e:
logger.debug("pypto_impl.GetNPUArch() not available: %s", e)
return ""
def _get_ai_core_num() -> int:
"""Get AI Core count via pypto_impl binding.
Returns:
Number of AI Cores, or 0 if unavailable.
"""
try:
from pypto import pypto_impl
return pypto_impl.GetAICoreNum()
except (ImportError, AttributeError, RuntimeError) as e:
logger.debug("pypto_impl.GetAICoreNum() not available: %s", e)
return 0
def _get_aic_core_num() -> int:
"""Get cube (AIC) core count via pypto_impl binding.
Returns:
Number of cube cores, or 0 if unavailable.
"""
try:
from pypto import pypto_impl
return pypto_impl.GetAICCoreNum()
except (ImportError, AttributeError, RuntimeError) as e:
logger.debug("pypto_impl.GetAICCoreNum() not available: %s", e)
return 0
def _get_aiv_core_num() -> int:
"""Get vector (AIV) core count via pypto_impl binding.
Returns:
Number of vector cores, or 0 if unavailable.
"""
try:
from pypto import pypto_impl
return pypto_impl.GetAIVCoreNum()
except (ImportError, AttributeError, RuntimeError) as e:
logger.debug("pypto_impl.GetAIVCoreNum() not available: %s", e)
return 0
def get_platform_info(force_refresh: bool = False) -> PlatformInfo:
"""Get NPU platform information.
Uses pypto_impl binding for both arch detection and core count query.
Results are cached after the first call.
Args:
force_refresh: If True, re-query the hardware (ignore cache).
Returns:
PlatformInfo dataclass with hardware details.
"""
global _cached_info
if _cached_info is not None and not force_refresh:
return _cached_info
info = PlatformInfo()
info.soc_version = _get_npu_arch()
info.core_num = _get_ai_core_num()
info.cube_core_num = _get_aic_core_num() or info.core_num
info.vector_core_num = _get_aiv_core_num() or info.core_num
_cached_info = info
if info.soc_version:
logger.info(
"Platform: %s (arch=%s), core_num=%d, cube_core_num=%d, vector_core_num=%d",
info.soc_version,
info.arch,
info.core_num,
info.cube_core_num,
info.vector_core_num,
)
else:
logger.debug("Platform info not available (pypto_impl not loaded)")
return info