forked from maranasgroup/CatPred
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackends.py
More file actions
344 lines (296 loc) · 12.8 KB
/
Copy pathbackends.py
File metadata and controls
344 lines (296 loc) · 12.8 KB
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
from __future__ import annotations
from dataclasses import dataclass, field, replace
from pathlib import Path
import csv
import json
import os
from typing import Any
from urllib import error, request as urllib_request
import pandas as pd
from .types import PredictionRequest
class InferenceBackendError(RuntimeError):
"""Raised when a backend cannot satisfy an inference request."""
@dataclass(frozen=True)
class BackendPredictionResult:
backend_name: str
output_file: str
metadata: dict[str, Any] = field(default_factory=dict)
class InferenceBackend:
name = "base"
def readiness(self) -> dict[str, Any]:
raise NotImplementedError
def predict(self, request_obj: PredictionRequest, results_dir: str) -> BackendPredictionResult:
raise NotImplementedError
class LocalInferenceBackend(InferenceBackend):
name = "local"
def __init__(
self,
repo_root: str | None = None,
use_subprocess: bool | None = None,
) -> None:
self._repo_root = repo_root
self._use_subprocess = (
_env_flag("CATPRED_LOCAL_SUBPROCESS", default=False)
if use_subprocess is None
else use_subprocess
)
def readiness(self) -> dict[str, Any]:
root = Path(self._repo_root) if self._repo_root else Path.cwd()
root = root.resolve()
required = [
root / "predict.py",
root / "scripts" / "create_pdbrecords.py",
]
missing = [str(path) for path in required if not path.exists()]
return {
"configured": True,
"ready": len(missing) == 0,
"missing_files": missing,
"repo_root": str(root),
"mode": "subprocess" if self._use_subprocess else "in_process",
}
def predict(self, request_obj: PredictionRequest, results_dir: str) -> BackendPredictionResult:
from .service import run_inprocess_prediction_pipeline, run_prediction_pipeline
effective_request = request_obj
if not request_obj.repo_root and self._repo_root:
effective_request = replace(request_obj, repo_root=self._repo_root)
if self._use_subprocess:
output_file = run_prediction_pipeline(effective_request, results_dir=results_dir)
mode = "subprocess"
else:
output_file = run_inprocess_prediction_pipeline(effective_request, results_dir=results_dir)
mode = "in_process"
return BackendPredictionResult(
backend_name=self.name,
output_file=output_file,
metadata={"mode": mode},
)
class ModalHTTPInferenceBackend(InferenceBackend):
name = "modal"
def __init__(
self,
endpoint: str | None,
token: str | None = None,
timeout_seconds: int = 900,
repo_root: str | None = None,
) -> None:
self._endpoint = endpoint
self._token = token
self._timeout_seconds = timeout_seconds
self._repo_root = repo_root
def readiness(self) -> dict[str, Any]:
configured = bool(self._endpoint)
return {
"configured": configured,
"ready": configured,
"endpoint": self._endpoint,
"timeout_seconds": self._timeout_seconds,
}
def _resolve_input_file(self, request_obj: PredictionRequest) -> Path:
input_path = Path(request_obj.input_file)
if not input_path.is_absolute():
root = Path(request_obj.repo_root or self._repo_root or Path.cwd()).resolve()
input_path = (root / input_path).resolve()
if not input_path.exists():
raise FileNotFoundError(f'Input CSV not found for modal backend: "{input_path}"')
return input_path
def _resolve_results_dir(self, results_dir: str, request_obj: PredictionRequest) -> Path:
out_dir = Path(results_dir)
if not out_dir.is_absolute():
root = Path(request_obj.repo_root or self._repo_root or Path.cwd()).resolve()
out_dir = (root / out_dir).resolve()
out_dir.mkdir(parents=True, exist_ok=True)
return out_dir
@staticmethod
def _load_rows(csv_path: Path) -> list[dict[str, Any]]:
with csv_path.open(newline="", encoding="utf-8") as handle:
return list(csv.DictReader(handle))
def _post_json(self, payload: dict[str, Any]) -> dict[str, Any]:
if not self._endpoint:
raise InferenceBackendError(
"Modal backend is not configured. Set CATPRED_MODAL_ENDPOINT."
)
encoded_payload = json.dumps(payload).encode("utf-8")
headers = {"Content-Type": "application/json"}
if self._token:
headers["Authorization"] = f"Bearer {self._token}"
req = urllib_request.Request(
url=self._endpoint,
method="POST",
data=encoded_payload,
headers=headers,
)
try:
with urllib_request.urlopen(req, timeout=self._timeout_seconds) as resp:
raw = resp.read()
except error.HTTPError as exc:
body = exc.read().decode("utf-8", errors="replace")
raise InferenceBackendError(
f"Modal backend request failed with HTTP {exc.code}: {body}"
) from exc
except error.URLError as exc:
raise InferenceBackendError(
f"Modal backend request failed: {exc.reason}"
) from exc
try:
decoded = json.loads(raw.decode("utf-8"))
except json.JSONDecodeError as exc:
raise InferenceBackendError("Modal backend returned non-JSON output.") from exc
if not isinstance(decoded, dict):
raise InferenceBackendError(
"Modal backend response must be a JSON object."
)
return decoded
def _materialize_output(
self,
response: dict[str, Any],
input_path: Path,
results_dir: str,
request_obj: PredictionRequest,
) -> BackendPredictionResult:
output_file = response.get("output_file")
if isinstance(output_file, str) and output_file:
resolved = Path(output_file).resolve()
if resolved.exists():
return BackendPredictionResult(
backend_name=self.name,
output_file=str(resolved),
metadata={"endpoint": self._endpoint, "mode": "output_file"},
)
output_rows = response.get("output_rows")
if isinstance(output_rows, list):
out_dir = self._resolve_results_dir(results_dir, request_obj)
out_name = response.get("output_filename")
if not isinstance(out_name, str) or not out_name:
out_name = f"{input_path.stem}_modal_output.csv"
if not out_name.endswith(".csv"):
out_name = f"{out_name}.csv"
final_output = out_dir / out_name
pd.DataFrame(output_rows).to_csv(final_output, index=False)
return BackendPredictionResult(
backend_name=self.name,
output_file=str(final_output),
metadata={"endpoint": self._endpoint, "mode": "output_rows"},
)
output_csv_text = response.get("output_csv_text")
if isinstance(output_csv_text, str) and output_csv_text:
out_dir = self._resolve_results_dir(results_dir, request_obj)
out_name = response.get("output_filename")
if not isinstance(out_name, str) or not out_name:
out_name = f"{input_path.stem}_modal_output.csv"
if not out_name.endswith(".csv"):
out_name = f"{out_name}.csv"
final_output = out_dir / out_name
final_output.write_text(output_csv_text, encoding="utf-8")
return BackendPredictionResult(
backend_name=self.name,
output_file=str(final_output),
metadata={"endpoint": self._endpoint, "mode": "output_csv_text"},
)
raise InferenceBackendError(
"Modal backend response must include one of: output_file, output_rows, output_csv_text."
)
def predict(self, request_obj: PredictionRequest, results_dir: str) -> BackendPredictionResult:
input_path = self._resolve_input_file(request_obj)
payload = {
"parameter": request_obj.parameter,
"checkpoint_dir": request_obj.checkpoint_dir,
"use_gpu": request_obj.use_gpu,
"input_rows": self._load_rows(input_path),
"input_filename": input_path.name,
}
response = self._post_json(payload)
return self._materialize_output(
response=response,
input_path=input_path,
results_dir=results_dir,
request_obj=request_obj,
)
def _env_flag(name: str, default: bool = False) -> bool:
value = os.environ.get(name)
if value is None:
return default
return value.strip().lower() in {"1", "true", "yes", "y", "on"}
@dataclass(frozen=True)
class BackendRouterSettings:
default_backend: str = "local"
modal_endpoint: str | None = None
modal_token: str | None = None
modal_timeout_seconds: int = 900
repo_root: str | None = None
@classmethod
def from_env(cls) -> "BackendRouterSettings":
timeout = int(os.environ.get("CATPRED_MODAL_TIMEOUT_SECONDS", "900"))
return cls(
default_backend=os.environ.get("CATPRED_DEFAULT_BACKEND", "local").lower(),
modal_endpoint=os.environ.get("CATPRED_MODAL_ENDPOINT"),
modal_token=os.environ.get("CATPRED_MODAL_TOKEN"),
modal_timeout_seconds=timeout,
repo_root=os.environ.get("CATPRED_REPO_ROOT"),
)
class InferenceBackendRouter:
def __init__(self, settings: BackendRouterSettings | None = None) -> None:
self.settings = settings or BackendRouterSettings.from_env()
self._backends: dict[str, InferenceBackend] = {
"local": LocalInferenceBackend(repo_root=self.settings.repo_root),
"modal": ModalHTTPInferenceBackend(
endpoint=self.settings.modal_endpoint,
token=self.settings.modal_token,
timeout_seconds=self.settings.modal_timeout_seconds,
repo_root=self.settings.repo_root,
),
}
if self.settings.default_backend not in self._backends:
raise ValueError(
f"Unsupported CATPRED_DEFAULT_BACKEND '{self.settings.default_backend}'. "
"Use one of: local, modal."
)
def available_backends(self) -> list[str]:
return sorted(self._backends.keys())
def resolve_backend(self, backend_name: str | None = None) -> InferenceBackend:
selected = (backend_name or self.settings.default_backend).lower()
if selected not in self._backends:
raise ValueError(
f"Unsupported backend '{selected}'. Use one of: {', '.join(self.available_backends())}."
)
backend = self._backends[selected]
state = backend.readiness()
if not state.get("configured", False):
raise InferenceBackendError(
f"Backend '{selected}' is not configured. Readiness: {state}"
)
return backend
def predict(
self,
request_obj: PredictionRequest,
results_dir: str,
backend_name: str | None = None,
fallback_to_local: bool = False,
) -> BackendPredictionResult:
selected_name = (backend_name or self.settings.default_backend).lower()
backend: InferenceBackend | None = None
try:
backend = self.resolve_backend(selected_name)
return backend.predict(request_obj=request_obj, results_dir=results_dir)
except Exception as exc:
if fallback_to_local and selected_name != "local":
local_backend = self._backends["local"]
local_result = local_backend.predict(request_obj=request_obj, results_dir=results_dir)
metadata = dict(local_result.metadata)
metadata["fallback_from"] = selected_name
metadata["fallback_reason"] = str(exc)
return BackendPredictionResult(
backend_name=local_result.backend_name,
output_file=local_result.output_file,
metadata=metadata,
)
raise
def readiness(self) -> dict[str, Any]:
backends = {name: backend.readiness() for name, backend in self._backends.items()}
default_state = backends[self.settings.default_backend]
return {
"default_backend": self.settings.default_backend,
"ready": bool(default_state.get("ready", False)),
"backends": backends,
"fallback_to_local_enabled": _env_flag("CATPRED_MODAL_FALLBACK_TO_LOCAL", default=False),
}