File size: 2,004 Bytes
290fe6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Resolve missing ZeroGPU cgroup mounts before importing ``spaces``."""

import logging
import os
from pathlib import Path


def configure_zerogpu_cgroup() -> None:
    """Use procfs only if it exposes the host's actual device cgroup."""
    if os.environ.get("SPACES_ZERO_GPU", "").lower() not in {"1", "t", "true"}:
        return

    configured = Path(os.environ.get("ZEROGPU_PROC_SELF_CGROUP_PATH", "/proc/self/cgroup"))
    if configured.exists():
        return

    fallback = Path("/proc/self/cgroup")
    try:
        lines = fallback.read_text().strip().splitlines()
    except OSError as exc:
        raise RuntimeError(
            f"ZeroGPU cgroup file {configured} is missing and {fallback} is unreadable. "
            "Restart the Hugging Face Space to restore its runtime mounts."
        ) from exc

    # Match spaces.zero.utils.self_cgroup_device_path: prefer the v1 devices
    # controller, otherwise use the unified v2 hierarchy.
    cgroup = None
    for line in lines:
        fields = line.split(":", 2)
        if len(fields) == 3 and fields[1] == "devices":
            cgroup = fields[2]
            break
    if cgroup is None and len(lines) == 1 and lines[0].startswith("0::"):
        cgroup = lines[0][3:]

    # A private cgroup namespace exposes '/' rather than the host container
    # path. It cannot identify this process to the ZeroGPU scheduler.
    if not cgroup or not cgroup.startswith("/") or cgroup == "/" or ".." in cgroup.split("/"):
        raise RuntimeError(
            f"ZeroGPU cgroup file {configured} is missing and {fallback} does not "
            "expose a usable host device cgroup. Restart the Hugging Face Space "
            "to restore its runtime mounts; if this persists, contact Hugging Face support."
        )

    os.environ["ZEROGPU_PROC_SELF_CGROUP_PATH"] = str(fallback)
    logging.getLogger(__name__).warning(
        "ZeroGPU cgroup file %s is missing; using %s for GPU scheduling",
        configured,
        fallback,
    )