/
Chaizee
/
ZenithCode_Incident-LLM-analytics
Обзор
Документация
Войти
/
Chaizee
/
ZenithCode_Incident-LLM-analytics
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/memory_guard.py
46 строк
1 KB
Chaizee
feat: new realisation
11 июн 2026, 19:58
11 июн 2026, 19:58
697b236
Код
Авторство
О чём код?
from __future__ import annotations import gc from contextlib import contextmanager from typing import Iterator def available_ram_gb() -> float: try: with open("/proc/meminfo", encoding="utf-8") as fh: for line in fh: if line.startswith(("MemAvailable:", "MemFree:")): kb = int(line.split()[1]) return kb / 1024 / 1024 except OSError: pass return 0.0 def ensure_ram(min_gb: float, *, context: str = "") -> None: avail = available_ram_gb() if avail < min_gb: label = f" ({context})" if context else "" raise MemoryError( f"Недостаточно RAM{label}: доступно {avail:.1f} ГБ, " f"требуется минимум {min_gb:.1f} ГБ. " "Отключите LLM или увеличьте memory в .wslconfig." ) def free_memory(*, deep: bool = False) -> None: gc.collect() if deep: gc.collect() try: import torch if torch.cuda.is_available(): torch.cuda.empty_cache() torch.cuda.synchronize() except ImportError: pass @contextmanager def release_after() -> Iterator[None]: try: yield finally: free_memory(deep=True)