Repository navigation
Expand file tree
/
Copy pathpreflight.py
More file actions
82 lines (59 loc) · 2.05 KB
/
Copy pathpreflight.py
File metadata and controls
82 lines (59 loc) · 2.05 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
"""Image self-check run once, right after pull, before any export step.
Installer plan (docs/design/openprocessor_internal/one_line_installer_plan.md
section 3.2): a stale ``:latest`` API image once shipped without
``perception_models`` importable. This script is invoked as
docker compose run --rm --no-deps -T yolo-api python -m export.preflight
and exits non-zero with a specific message per failing check, instead of
failing deep inside the first export step with a confusing traceback.
"""
from __future__ import annotations
import sys
from pathlib import Path
MODEL_REPO_SEED = Path('/opt/openprocessor/model_repo_seed')
def check_core_import() -> str | None:
try:
import core
except ImportError as exc:
return f'import core (perception_models) failed: {exc}'
return None
def check_tensorrt_import() -> str | None:
try:
import tensorrt
except ImportError as exc:
return f'import tensorrt failed: {exc}'
return None
def check_cuda_available() -> str | None:
try:
import torch
except ImportError as exc:
return f'import torch failed: {exc}'
if not torch.cuda.is_available():
return 'torch.cuda.is_available() is False'
return None
def check_model_repo_seed() -> str | None:
if not MODEL_REPO_SEED.is_dir():
return f'{MODEL_REPO_SEED} does not exist'
return None
CHECKS = (
('core import', check_core_import),
('tensorrt import', check_tensorrt_import),
('cuda available', check_cuda_available),
('model repo seed', check_model_repo_seed),
)
def main() -> int:
failures: list[str] = []
for label, check in CHECKS:
error = check()
if error:
failures.append(f'[{label}] {error}')
else:
print(f'OK: {label}')
if failures:
print('PREFLIGHT FAILED:', file=sys.stderr)
for f in failures:
print(f' - {f}', file=sys.stderr)
return 1
print('PREFLIGHT OK: image is ready for model export')
return 0
if __name__ == '__main__':
sys.exit(main())