File size: 9,535 Bytes
a721dfa
 
 
9734b71
4106e0f
a721dfa
 
4106e0f
 
a721dfa
 
 
 
4106e0f
 
 
 
 
 
a721dfa
 
 
 
 
 
 
4106e0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9734b71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a721dfa
 
 
 
 
 
b9be111
 
 
 
 
 
 
 
 
 
a721dfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad4554b
 
 
 
a721dfa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from __future__ import annotations

import os
import socket
import sys
from copy import deepcopy
from dataclasses import dataclass
from pathlib import Path
from typing import Any, List, Mapping, MutableMapping, Protocol

import numpy as np
import pandas as pd

DEFAULT_TORCH_NUM_THREADS = 2
DEFAULT_TORCH_NUM_INTEROP_THREADS = 1
DEFAULT_OMP_NUM_THREADS = 2
DEFAULT_MKL_NUM_THREADS = 2
ALLOW_NON_VENV_PYTHON_ENV_KEY = "AIFORECAST_ALLOW_NON_VENV_PYTHON"


@dataclass(frozen=True)
class RuntimePaths:
    current_dir: str
    project_root: str


@dataclass(frozen=True)
class TorchThreadSettings:
    torch_num_threads: int
    torch_num_interop_threads: int
    omp_num_threads: int
    mkl_num_threads: int


class LoggerLike(Protocol):
    def info(self, msg: str, *args: Any, **kwargs: Any) -> None: ...

    def warning(self, msg: str, *args: Any, **kwargs: Any) -> None: ...


def _read_positive_int(
    raw_value: object,
    *,
    default: int,
) -> int:
    try:
        value = int(str(raw_value).strip())
    except (TypeError, ValueError, AttributeError):
        return default
    return max(1, value)


def _read_bool(raw_value: object, *, default: bool) -> bool:
    normalized = str(raw_value or "").strip().lower()
    if not normalized:
        return default
    if normalized in {"1", "true", "yes", "on"}:
        return True
    if normalized in {"0", "false", "no", "off"}:
        return False
    return default


def read_torch_thread_settings(
    env: Mapping[str, str] | None = None,
) -> TorchThreadSettings:
    runtime_env = os.environ if env is None else env
    return TorchThreadSettings(
        torch_num_threads=_read_positive_int(
            runtime_env.get("TORCH_NUM_THREADS"),
            default=DEFAULT_TORCH_NUM_THREADS,
        ),
        torch_num_interop_threads=_read_positive_int(
            runtime_env.get("TORCH_NUM_INTEROP_THREADS"),
            default=DEFAULT_TORCH_NUM_INTEROP_THREADS,
        ),
        omp_num_threads=_read_positive_int(
            runtime_env.get("OMP_NUM_THREADS"),
            default=DEFAULT_OMP_NUM_THREADS,
        ),
        mkl_num_threads=_read_positive_int(
            runtime_env.get("MKL_NUM_THREADS"),
            default=DEFAULT_MKL_NUM_THREADS,
        ),
    )


def _expected_project_venv_python(project_root: Path) -> Path:
    if os.name == "nt":
        return project_root / "venv" / "Scripts" / "python.exe"
    return project_root / "venv" / "bin" / "python"


def _same_path(left: Path, right: Path) -> bool:
    try:
        if left.exists() and right.exists():
            return left.samefile(right)
    except OSError:
        pass
    return str(left.resolve()).lower() == str(right.resolve()).lower()


def describe_runtime_environment(
    project_root: str | Path,
    *,
    env: Mapping[str, str] | None = None,
    current_executable: str | None = None,
) -> dict[str, Any]:
    runtime_env = os.environ if env is None else env
    root_path = Path(project_root).resolve()
    current_python = Path(current_executable or sys.executable).resolve()
    expected_python = _expected_project_venv_python(root_path)
    allow_non_venv_python = _read_bool(
        runtime_env.get(ALLOW_NON_VENV_PYTHON_ENV_KEY),
        default=False,
    )
    expected_exists = expected_python.exists()
    is_project_venv_python = not expected_exists or _same_path(current_python, expected_python)
    settings = read_torch_thread_settings(runtime_env)
    return {
        "current_executable": str(current_python),
        "expected_venv_python": str(expected_python),
        "expected_venv_exists": expected_exists,
        "is_project_venv_python": is_project_venv_python,
        "allow_non_venv_python": allow_non_venv_python,
        "thread_caps": {
            "torch_num_threads": settings.torch_num_threads,
            "torch_num_interop_threads": settings.torch_num_interop_threads,
            "omp_num_threads": settings.omp_num_threads,
            "mkl_num_threads": settings.mkl_num_threads,
        },
    }


def prepare_runtime_environment(
    project_root: str | Path,
    *,
    env: MutableMapping[str, str] | None = None,
    current_executable: str | None = None,
    logger: LoggerLike | None = None,
) -> TorchThreadSettings:
    runtime_env = os.environ if env is None else env
    settings = read_torch_thread_settings(runtime_env)
    runtime_env.setdefault("TORCH_NUM_THREADS", str(settings.torch_num_threads))
    runtime_env.setdefault("TORCH_NUM_INTEROP_THREADS", str(settings.torch_num_interop_threads))
    runtime_env.setdefault("OMP_NUM_THREADS", str(settings.omp_num_threads))
    runtime_env.setdefault("MKL_NUM_THREADS", str(settings.mkl_num_threads))

    snapshot = describe_runtime_environment(
        project_root,
        env=runtime_env,
        current_executable=current_executable,
    )
    if (
        snapshot["expected_venv_exists"]
        and not snapshot["is_project_venv_python"]
        and not snapshot["allow_non_venv_python"]
    ):
        raise RuntimeError(
            "Current Python interpreter is outside the project venv. "
            f"Expected {snapshot['expected_venv_python']} but got {snapshot['current_executable']}. "
            f"Set {ALLOW_NON_VENV_PYTHON_ENV_KEY}=true to override."
        )

    if logger is not None:
        logger.info(
            "Prepared runtime thread caps: torch=%s interop=%s omp=%s mkl=%s",
            settings.torch_num_threads,
            settings.torch_num_interop_threads,
            settings.omp_num_threads,
            settings.mkl_num_threads,
        )
    return settings


def apply_torch_thread_settings(
    torch_module: Any | None,
    env: Mapping[str, str] | None = None,
    *,
    logger: LoggerLike | None = None,
) -> dict[str, Any]:
    settings = read_torch_thread_settings(env)
    applied = False

    if torch_module is not None:
        set_num_threads = getattr(torch_module, "set_num_threads", None)
        if callable(set_num_threads):
            set_num_threads(settings.torch_num_threads)
            applied = True

        set_num_interop_threads = getattr(torch_module, "set_num_interop_threads", None)
        if callable(set_num_interop_threads):
            try:
                set_num_interop_threads(settings.torch_num_interop_threads)
            except RuntimeError as exc:
                if logger is not None:
                    logger.warning("Torch interop thread cap could not be updated: %s", exc)
            else:
                applied = True

    if logger is not None:
        logger.info(
            "Applied torch thread caps: torch=%s interop=%s omp=%s mkl=%s applied=%s",
            settings.torch_num_threads,
            settings.torch_num_interop_threads,
            settings.omp_num_threads,
            settings.mkl_num_threads,
            applied,
        )

    return {
        "torch_num_threads": settings.torch_num_threads,
        "torch_num_interop_threads": settings.torch_num_interop_threads,
        "omp_num_threads": settings.omp_num_threads,
        "mkl_num_threads": settings.mkl_num_threads,
        "applied": applied,
    }


def can_bind_tcp_port(host: str, port: int) -> bool:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
        try:
            server_socket.bind((host, port))
        except OSError:
            return False
    return True


def find_free_tcp_port(host: str = "127.0.0.1") -> int:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
        server_socket.bind((host, 0))
        server_socket.listen(1)
        return int(server_socket.getsockname()[1])


def parse_cors_origins(raw_origins: str) -> List[str]:
    origins = [origin.strip() for origin in raw_origins.split(",") if origin.strip()]
    return origins or ["*"]


def clone_cache_payload(payload: Any) -> Any:
    if payload is None or isinstance(payload, (str, int, float, bool)):
        return payload
    if isinstance(payload, list):
        return [clone_cache_payload(item) for item in payload]
    if isinstance(payload, tuple):
        return tuple(clone_cache_payload(item) for item in payload)
    if isinstance(payload, dict):
        return {clone_cache_payload(key): clone_cache_payload(value) for key, value in payload.items()}
    if isinstance(payload, set):
        return {clone_cache_payload(item) for item in payload}
    try:
        return deepcopy(payload)
    except Exception:
        return payload


def make_json_compatible(value: Any) -> Any:
    if isinstance(value, dict):
        return {str(key): make_json_compatible(item) for key, item in value.items()}
    if isinstance(value, (list, tuple, set)):
        return [make_json_compatible(item) for item in value]
    if isinstance(value, np.ndarray):
        return [make_json_compatible(item) for item in value.tolist()]
    if isinstance(value, np.generic):
        return make_json_compatible(value.item())
    if isinstance(value, pd.Timestamp):
        return value.isoformat()
    if isinstance(value, float):
        if not np.isfinite(value):
            return None
        return value
    return value


def resolve_runtime_paths(
    module_file: str,
    is_frozen: bool,
    bundle_dir: str,
) -> RuntimePaths:
    current_dir = os.path.dirname(os.path.abspath(module_file))
    if is_frozen:
        return RuntimePaths(current_dir=bundle_dir, project_root=bundle_dir)
    return RuntimePaths(
        current_dir=current_dir,
        project_root=os.path.dirname(current_dir),
    )