infixspeed-cli 0.0.16__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- infix_cli/__init__.py +5 -0
- infix_cli/__main__.py +6 -0
- infix_cli/agent-guide.txt +71 -0
- infix_cli/cli_framework.py +96 -0
- infix_cli/command_contract.py +157 -0
- infix_cli/commands/__init__.py +1 -0
- infix_cli/commands/_artifacts.py +142 -0
- infix_cli/commands/_comparison.py +443 -0
- infix_cli/commands/_confirm.py +26 -0
- infix_cli/commands/_control.py +290 -0
- infix_cli/commands/_create.py +351 -0
- infix_cli/commands/_create_output.py +139 -0
- infix_cli/commands/_delete.py +288 -0
- infix_cli/commands/_download.py +459 -0
- infix_cli/commands/_fusion_common.py +191 -0
- infix_cli/commands/_fusion_config.py +394 -0
- infix_cli/commands/_fusion_feedback.py +319 -0
- infix_cli/commands/_fusion_tasks.py +507 -0
- infix_cli/commands/_fusion_tensorboard.py +132 -0
- infix_cli/commands/_jobs.py +312 -0
- infix_cli/commands/_logs.py +210 -0
- infix_cli/commands/_publish.py +123 -0
- infix_cli/commands/_register.py +600 -0
- infix_cli/commands/_register_output.py +231 -0
- infix_cli/commands/_storage_tree.py +62 -0
- infix_cli/commands/admin.py +668 -0
- infix_cli/commands/asset.py +2012 -0
- infix_cli/commands/auth.py +246 -0
- infix_cli/commands/deploy.py +850 -0
- infix_cli/commands/evaluation.py +360 -0
- infix_cli/commands/examples.py +138 -0
- infix_cli/commands/fusion.py +1842 -0
- infix_cli/commands/merge.py +709 -0
- infix_cli/commands/resource.py +127 -0
- infix_cli/commands/spark.py +347 -0
- infix_cli/commands/storage.py +329 -0
- infix_cli/commands/training.py +748 -0
- infix_cli/config.py +311 -0
- infix_cli/config_input.py +217 -0
- infix_cli/context.py +84 -0
- infix_cli/discovery.py +459 -0
- infix_cli/domain/__init__.py +1 -0
- infix_cli/domain/dto/__init__.py +6 -0
- infix_cli/domain/dto/admin.py +413 -0
- infix_cli/domain/dto/asset.py +390 -0
- infix_cli/domain/dto/common.py +136 -0
- infix_cli/domain/dto/evaluation.py +296 -0
- infix_cli/domain/dto/logs.py +98 -0
- infix_cli/domain/dto/resource.py +108 -0
- infix_cli/domain/dto/serving.py +156 -0
- infix_cli/domain/dto/spark.py +241 -0
- infix_cli/domain/dto/storage.py +75 -0
- infix_cli/domain/dto/training.py +272 -0
- infix_cli/domain/enums.py +359 -0
- infix_cli/domain/hyperparams.py +190 -0
- infix_cli/domain/refs.py +108 -0
- infix_cli/domain/resolve.py +619 -0
- infix_cli/errors.py +158 -0
- infix_cli/example_data/__init__.py +1 -0
- infix_cli/example_data/fusion/README.md +128 -0
- infix_cli/example_data/fusion/fusion.json +149 -0
- infix_cli/example_data/fusion/prompts/code.jsonl +16 -0
- infix_cli/example_data/fusion/prompts/math.jsonl +16 -0
- infix_cli/example_data/fusion.zip +0 -0
- infix_cli/example_data/merge/README.md +89 -0
- infix_cli/example_data/merge/merge.json +26 -0
- infix_cli/example_data/merge.zip +0 -0
- infix_cli/http/__init__.py +1 -0
- infix_cli/http/browser_auth.py +160 -0
- infix_cli/http/client.py +575 -0
- infix_cli/http/pagination.py +84 -0
- infix_cli/http/upload.py +254 -0
- infix_cli/main.py +432 -0
- infix_cli/pipeline_config_schema.py +122 -0
- infix_cli/render/__init__.py +79 -0
- infix_cli/render/json_out.py +123 -0
- infix_cli/render/progress.py +62 -0
- infix_cli/render/text.py +233 -0
- infix_cli/update_check.py +177 -0
- infix_cli/waiter.py +93 -0
- infix_cli/workspace.py +158 -0
- infixspeed_cli-0.0.16.dist-info/METADATA +58 -0
- infixspeed_cli-0.0.16.dist-info/RECORD +85 -0
- infixspeed_cli-0.0.16.dist-info/WHEEL +4 -0
- infixspeed_cli-0.0.16.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
"""name -> id resolution.
|
|
2
|
+
|
|
3
|
+
Every command addresses resources by name; the backend addresses them by id.
|
|
4
|
+
This module is the only place that bridges the two, and it caches per process —
|
|
5
|
+
one ``training create`` can otherwise resolve the same base model twice.
|
|
6
|
+
|
|
7
|
+
**Shared layer.** Both the CLI and the agent-side tooling import this module,
|
|
8
|
+
so it must stay free of either caller's concepts. It talks to the backend
|
|
9
|
+
through a small structural protocol rather than a concrete client class:
|
|
10
|
+
|
|
11
|
+
* ``client.get(path, params=...) -> parsed JSON``
|
|
12
|
+
* ``client.base_url`` and ``client.workspace_id`` — cache keys only
|
|
13
|
+
|
|
14
|
+
Anything satisfying those three works, which is how a caller with its own HTTP
|
|
15
|
+
client (different auth headers, different error handling) reuses the resolution
|
|
16
|
+
logic without adopting the CLI's client.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
from __future__ import annotations
|
|
20
|
+
|
|
21
|
+
from dataclasses import dataclass
|
|
22
|
+
from typing import Any, Iterable
|
|
23
|
+
|
|
24
|
+
from infix_cli.domain import enums
|
|
25
|
+
from infix_cli.domain.refs import AssetRef
|
|
26
|
+
from infix_cli.errors import NotFoundError, UsageError
|
|
27
|
+
from infix_cli.http.pagination import iter_items
|
|
28
|
+
|
|
29
|
+
# Asset kind -> path segment. Note "code" is not pluralised by the backend.
|
|
30
|
+
_ASSET_PATH = {"model": "models", "dataset": "datasets", "code": "code"}
|
|
31
|
+
|
|
32
|
+
_JOB_PATH = {
|
|
33
|
+
"training": "/training/jobs",
|
|
34
|
+
"evaluation": "/evaluation/jobs",
|
|
35
|
+
"spark": "/data-processing/",
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
_cache: dict[tuple, Any] = {}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def clear_cache() -> None:
|
|
42
|
+
"""Drop the process-local cache (tests, and long-running waits)."""
|
|
43
|
+
_cache.clear()
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@dataclass
|
|
47
|
+
class ResolvedAsset:
|
|
48
|
+
asset_id: str
|
|
49
|
+
version_id: str | None
|
|
50
|
+
version_number: str | None
|
|
51
|
+
name: str
|
|
52
|
+
raw: dict
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def asset_path(kind: str) -> str:
|
|
56
|
+
try:
|
|
57
|
+
return _ASSET_PATH[kind]
|
|
58
|
+
except KeyError: # pragma: no cover - guarded by refs.parse_ref
|
|
59
|
+
raise ValueError(f"unknown asset kind: {kind}") from None
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def find_asset(client: Any, kind: str, name: str, scope: str) -> dict:
|
|
63
|
+
"""Find one asset by exact name within a scope.
|
|
64
|
+
|
|
65
|
+
The backend's ``search`` is a fuzzy match, so the exact-name filter has to
|
|
66
|
+
be reapplied locally; and scope must always be sent, since official and
|
|
67
|
+
workspace assets are separate namespaces that may hold the same name.
|
|
68
|
+
"""
|
|
69
|
+
key = ("asset", client.base_url, client.workspace_id, kind, scope, name)
|
|
70
|
+
if key in _cache:
|
|
71
|
+
return _cache[key]
|
|
72
|
+
|
|
73
|
+
path = f"/assets/{asset_path(kind)}"
|
|
74
|
+
candidates = [
|
|
75
|
+
item
|
|
76
|
+
for item in iter_items(client, path, params={"scope": scope, "search": name})
|
|
77
|
+
if str(item.get("name", "")) == name
|
|
78
|
+
]
|
|
79
|
+
if not candidates:
|
|
80
|
+
# The name as typed, and the namespace in words (PRD 3.3.6): echoing a
|
|
81
|
+
# "workspace/" prefix the caller never wrote reads like a different name.
|
|
82
|
+
where = "in official assets" if scope == "official" else "in this workspace"
|
|
83
|
+
raise NotFoundError(
|
|
84
|
+
f'{kind.capitalize()} "{name}" not found {where}.',
|
|
85
|
+
f'Run "infix asset {kind} list --scope '
|
|
86
|
+
f'{"official" if scope == "official" else "workspace"}" to see what exists.',
|
|
87
|
+
)
|
|
88
|
+
if len(candidates) > 1: # pragma: no cover - backend enforces unique names
|
|
89
|
+
raise UsageError(
|
|
90
|
+
f'{kind.capitalize()} name "{name}" is ambiguous ({len(candidates)} matches).',
|
|
91
|
+
)
|
|
92
|
+
_cache[key] = candidates[0]
|
|
93
|
+
return candidates[0]
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def asset_scope_by_id(client: Any, kind: str, asset_id: str) -> str:
|
|
97
|
+
"""Which namespace an asset id lives in: ``official`` or ``workspace``.
|
|
98
|
+
|
|
99
|
+
Job details name their dataset and code but do not say where either came
|
|
100
|
+
from — ``base_model_source`` is the only source field the backend returns.
|
|
101
|
+
Without asking, a cloned config prefixes every reference ``workspace/``,
|
|
102
|
+
and one that was actually official fails to resolve when the config is
|
|
103
|
+
submitted back. The asset's own record carries ``source``, so one lookup
|
|
104
|
+
per slot settles it; ``clone`` is infrequent enough to afford it.
|
|
105
|
+
|
|
106
|
+
Falls back to ``workspace`` when the asset cannot be read: a clone that
|
|
107
|
+
still prints is more useful than one that aborts on a stale id.
|
|
108
|
+
"""
|
|
109
|
+
if not asset_id:
|
|
110
|
+
return "workspace"
|
|
111
|
+
key = ("asset-scope", client.base_url, client.workspace_id, kind, asset_id)
|
|
112
|
+
if key in _cache:
|
|
113
|
+
return _cache[key]
|
|
114
|
+
try:
|
|
115
|
+
raw = client.get(f"/assets/{asset_path(kind)}/{asset_id}")
|
|
116
|
+
except Exception:
|
|
117
|
+
return "workspace"
|
|
118
|
+
if not isinstance(raw, dict):
|
|
119
|
+
return "workspace"
|
|
120
|
+
raw = raw.get("data") if isinstance(raw.get("data"), dict) else raw
|
|
121
|
+
scope = "official" if str(raw.get("source") or "") == "Official" else "workspace"
|
|
122
|
+
_cache[key] = scope
|
|
123
|
+
return scope
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def list_versions(client: Any, kind: str, asset_id: str) -> list[dict]:
|
|
127
|
+
key = ("versions", client.base_url, kind, asset_id)
|
|
128
|
+
if key not in _cache:
|
|
129
|
+
payload = client.get(f"/assets/{asset_path(kind)}/{asset_id}/versions")
|
|
130
|
+
_cache[key] = list(payload or []) if isinstance(payload, list) else list(
|
|
131
|
+
(payload or {}).get("items") or []
|
|
132
|
+
)
|
|
133
|
+
return _cache[key]
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def resolve_asset(client: Any, ref: AssetRef) -> ResolvedAsset:
|
|
137
|
+
"""Resolve an ``AssetRef`` to concrete asset and version ids."""
|
|
138
|
+
asset = find_asset(client, ref.kind, ref.name, ref.scope)
|
|
139
|
+
asset_id = str(asset.get("id"))
|
|
140
|
+
versions = list_versions(client, ref.kind, asset_id)
|
|
141
|
+
|
|
142
|
+
if ref.version:
|
|
143
|
+
match = next(
|
|
144
|
+
(v for v in versions if str(v.get("version_number")) == ref.version), None,
|
|
145
|
+
)
|
|
146
|
+
if match is None:
|
|
147
|
+
available = ", ".join(str(v.get("version_number")) for v in versions) or "none"
|
|
148
|
+
raise NotFoundError(
|
|
149
|
+
f'Version "{ref.version}" not found for {ref.kind} "{ref.name}".',
|
|
150
|
+
f"Available versions: {available}.",
|
|
151
|
+
)
|
|
152
|
+
else:
|
|
153
|
+
match = _latest_active(versions)
|
|
154
|
+
if match is None:
|
|
155
|
+
raise NotFoundError(
|
|
156
|
+
f'{ref.kind.capitalize()} "{ref.name}" has no available version yet.',
|
|
157
|
+
f'Run "infix asset {ref.kind} versions {ref.display()}" to check registration status.',
|
|
158
|
+
)
|
|
159
|
+
|
|
160
|
+
status = enums.ASSET_STATUS.to_cli(match.get("status"))
|
|
161
|
+
if status in enums.ASSET_VERSION_UNUSABLE:
|
|
162
|
+
raise UsageError(
|
|
163
|
+
f'{ref.kind.capitalize()} "{ref.display()}" has status {status} and cannot be used.',
|
|
164
|
+
f'Run "infix asset {ref.kind} versions {ref.display()}" to select an available version.',
|
|
165
|
+
details={"reference": ref.display(), "version_status": status},
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
return ResolvedAsset(
|
|
169
|
+
asset_id=asset_id,
|
|
170
|
+
version_id=str(match.get("id")),
|
|
171
|
+
version_number=str(match.get("version_number")),
|
|
172
|
+
name=str(asset.get("name")),
|
|
173
|
+
raw=asset,
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _latest_active(versions: list[dict]) -> dict | None:
|
|
178
|
+
"""Newest usable version. The backend returns versions newest-first."""
|
|
179
|
+
for version in versions:
|
|
180
|
+
if enums.ASSET_STATUS.to_cli(version.get("status")) == "available":
|
|
181
|
+
return version
|
|
182
|
+
return None
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
def find_asset_by_version_id(
|
|
186
|
+
client: Any,
|
|
187
|
+
kind: str,
|
|
188
|
+
version_id: str,
|
|
189
|
+
*,
|
|
190
|
+
scopes: tuple[str, ...] = ("my", "official"),
|
|
191
|
+
) -> dict | None:
|
|
192
|
+
"""Reverse lookup: which asset owns ``version_id``.
|
|
193
|
+
|
|
194
|
+
Expensive fallback, and deliberately so. The API has no version -> asset
|
|
195
|
+
endpoint, so answering means listing every visible asset of the kind and
|
|
196
|
+
reading each one's versions: one request per asset, plus one listing per
|
|
197
|
+
scope. Callers almost always know the asset id already — every listing that
|
|
198
|
+
yields a version id yields its asset id alongside — and should pass it
|
|
199
|
+
instead of calling this.
|
|
200
|
+
|
|
201
|
+
Returns the asset dict, or None when no visible asset owns that version.
|
|
202
|
+
"""
|
|
203
|
+
key = ("asset_by_version", client.base_url, client.workspace_id, kind, version_id)
|
|
204
|
+
if key in _cache:
|
|
205
|
+
return _cache[key]
|
|
206
|
+
|
|
207
|
+
path = f"/assets/{asset_path(kind)}"
|
|
208
|
+
for scope in scopes:
|
|
209
|
+
for asset in iter_items(client, path, params={"scope": scope}):
|
|
210
|
+
asset_id = str(asset.get("id") or "")
|
|
211
|
+
if not asset_id:
|
|
212
|
+
continue
|
|
213
|
+
for version in list_versions(client, kind, asset_id):
|
|
214
|
+
if str(version.get("id")) == version_id:
|
|
215
|
+
_cache[key] = asset
|
|
216
|
+
return asset
|
|
217
|
+
_cache[key] = None
|
|
218
|
+
return None
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def find_job(client: Any, module: str, name: str) -> dict:
|
|
222
|
+
"""Locate a job by name, falling back to treating the value as an id.
|
|
223
|
+
|
|
224
|
+
Users address jobs by name, but ids appear in scripts and in other
|
|
225
|
+
commands' output; accepting both costs one extra request only when the
|
|
226
|
+
name lookup misses.
|
|
227
|
+
"""
|
|
228
|
+
path = _JOB_PATH[module]
|
|
229
|
+
matches = [
|
|
230
|
+
item
|
|
231
|
+
for item in iter_items(client, path, params={"search": name})
|
|
232
|
+
if str(item.get("name", "")) == name
|
|
233
|
+
]
|
|
234
|
+
if len(matches) == 1:
|
|
235
|
+
return matches[0]
|
|
236
|
+
if len(matches) > 1:
|
|
237
|
+
newest = sorted(matches, key=_created_key, reverse=True)[0]
|
|
238
|
+
return newest
|
|
239
|
+
detail = _try_get_by_id(client, module, name)
|
|
240
|
+
if detail is not None:
|
|
241
|
+
return detail
|
|
242
|
+
raise NotFoundError(
|
|
243
|
+
f'{module.capitalize()} job "{name}" not found.',
|
|
244
|
+
f'Run "infix {module} list" to see jobs in this workspace.',
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def _created_key(item: dict) -> str:
|
|
249
|
+
return str(item.get("created_at") or item.get("created_time") or "")
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _try_get_by_id(client: Any, module: str, value: str) -> dict | None:
|
|
253
|
+
from infix_cli.errors import CliError
|
|
254
|
+
|
|
255
|
+
path = _JOB_PATH[module]
|
|
256
|
+
url = f"{path.rstrip('/')}/{value}" if module != "spark" else f"/data-processing/{value}"
|
|
257
|
+
try:
|
|
258
|
+
payload = client.get(url)
|
|
259
|
+
except CliError:
|
|
260
|
+
return None
|
|
261
|
+
return payload if isinstance(payload, dict) else None
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
# ── computing specs ─────────────────────────────────────────────────────────
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def spec_workers(spec: dict) -> int:
|
|
268
|
+
"""The worker count baked into a spec; 1 when the field is absent."""
|
|
269
|
+
try:
|
|
270
|
+
return max(int(spec.get("workers") or 1), 1)
|
|
271
|
+
except (TypeError, ValueError):
|
|
272
|
+
return 1
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
def dedupe_by_cli_name(specs: Iterable[dict]) -> list[dict]:
|
|
276
|
+
"""Collapse specs that derive the same hardware name, fewest workers winning.
|
|
277
|
+
|
|
278
|
+
The GUI offers multi-worker presets ("16x H800 80GB (2 nodes)") built out of
|
|
279
|
+
a single-worker spec. The CLI names one worker's hardware and takes the count
|
|
280
|
+
from ``--workers``, so a preset derives the same name as the spec behind it:
|
|
281
|
+
listing both shows the same hardware twice, each row judged against a total
|
|
282
|
+
the user never asked for, and makes ``--hardware`` resolve to whichever row
|
|
283
|
+
the backend happened to return first.
|
|
284
|
+
|
|
285
|
+
Collapsing rather than dropping every multi-worker spec: a preset whose name
|
|
286
|
+
has no single-worker spec behind it (spark's ``cpu_1c_4gb`` is one) is the
|
|
287
|
+
only carrier of a name users can type, and dropping it would delete that name
|
|
288
|
+
from the vocabulary. Backend reads ``num_workers`` off the request, not off
|
|
289
|
+
the spec, so the kept row serves every worker count either way.
|
|
290
|
+
"""
|
|
291
|
+
best: dict[str, dict] = {}
|
|
292
|
+
order: list[str] = []
|
|
293
|
+
for spec in specs:
|
|
294
|
+
name = spec_cli_name(spec)
|
|
295
|
+
if name not in best:
|
|
296
|
+
best[name] = spec
|
|
297
|
+
order.append(name)
|
|
298
|
+
elif spec_workers(spec) < spec_workers(best[name]):
|
|
299
|
+
best[name] = spec
|
|
300
|
+
return [best[name] for name in order]
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def spec_cli_name(spec: dict) -> str:
|
|
304
|
+
"""Derive the CLI hardware name for one computing spec.
|
|
305
|
+
|
|
306
|
+
Prefers a backend-provided ``cli_name``: deriving it client-side means the
|
|
307
|
+
rule ships inside every installed CLI and drifts the moment the backend
|
|
308
|
+
changes it (tech-design §4.7 asks the backend to add the field). Until it
|
|
309
|
+
exists, derive locally from the fields that are already there.
|
|
310
|
+
"""
|
|
311
|
+
provided = str(spec.get("cli_name") or "").strip()
|
|
312
|
+
if provided:
|
|
313
|
+
return provided
|
|
314
|
+
|
|
315
|
+
gpu_count = int(spec.get("gpu_count") or 0)
|
|
316
|
+
if gpu_count > 0:
|
|
317
|
+
key = str(spec.get("gpu_key") or "").strip().lower()
|
|
318
|
+
if not key:
|
|
319
|
+
key = _gpu_key_from_type(str(spec.get("gpu_type") or ""))
|
|
320
|
+
vram = int(spec.get("gpu_memory_gb") or 0)
|
|
321
|
+
# Vendor is hardcoded: GpuModel has no vendor field yet (only
|
|
322
|
+
# resource_name="nvidia.com/gpu"). Ascend support will need it.
|
|
323
|
+
return f"{gpu_count}x_nvidia_{key or 'gpu'}_{vram}gb"
|
|
324
|
+
return f"cpu_{int(spec.get('cpu_cores') or 0)}c_{int(spec.get('memory_gb') or 0)}gb"
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
def _gpu_key_from_type(gpu_type: str) -> str:
|
|
328
|
+
"""``NVIDIA-H200-141GB`` -> ``h200`` (fallback when gpu_key is absent)."""
|
|
329
|
+
parts = [p for p in gpu_type.replace("_", "-").split("-") if p]
|
|
330
|
+
for part in parts:
|
|
331
|
+
upper = part.upper()
|
|
332
|
+
if upper in ("NVIDIA", "SXM", "SXM6", "PCIE", "AC"):
|
|
333
|
+
continue
|
|
334
|
+
if upper.endswith("GB") and upper[:-2].isdigit():
|
|
335
|
+
continue
|
|
336
|
+
return part.lower()
|
|
337
|
+
return gpu_type.strip().lower()
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
def list_specs(
|
|
341
|
+
client: Any, scope: str, *, workspace_id: str = "", workload: str = "",
|
|
342
|
+
) -> list[dict]:
|
|
343
|
+
"""Fetch computing specs for one workload scope.
|
|
344
|
+
|
|
345
|
+
Always scoped: derived names are only guaranteed unique within a scope.
|
|
346
|
+
|
|
347
|
+
``workload`` turns on the queue/capacity check that fills in ``selectable``
|
|
348
|
+
and ``reason``. Without it the endpoint ignores ``workspace_id`` too, and
|
|
349
|
+
``selectable`` reflects nothing beyond whether the cluster owns that GPU
|
|
350
|
+
model at all — so a caller that wants real availability must pass it.
|
|
351
|
+
"""
|
|
352
|
+
key = ("specs", client.base_url, scope, workspace_id, workload)
|
|
353
|
+
if key not in _cache:
|
|
354
|
+
params: dict[str, Any] = {"scope": scope}
|
|
355
|
+
if workload:
|
|
356
|
+
params["workload"] = workload
|
|
357
|
+
if workspace_id:
|
|
358
|
+
params["workspace_id"] = workspace_id
|
|
359
|
+
payload = client.get("/computing/specs", params=params)
|
|
360
|
+
items = list((payload or {}).get("items") or []) if isinstance(payload, dict) else []
|
|
361
|
+
_cache[key] = items
|
|
362
|
+
return _cache[key]
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def resolve_hardware(client: Any, hardware: str, *, task: str, workspace_id: str = "") -> dict:
|
|
366
|
+
"""按名称或 ID 选择当前作用域可见的规格;显式 ID 保留 CPU/内存差异。"""
|
|
367
|
+
scope = enums.RESOURCE_SCOPE.to_backend(task)
|
|
368
|
+
visible_specs = list_specs(client, scope, workspace_id=workspace_id)
|
|
369
|
+
wanted = hardware.strip().lower()
|
|
370
|
+
for spec in visible_specs:
|
|
371
|
+
if str(spec.get("id") or "").lower() == wanted and wanted:
|
|
372
|
+
return spec
|
|
373
|
+
specs = dedupe_by_cli_name(visible_specs)
|
|
374
|
+
for spec in specs:
|
|
375
|
+
if spec_cli_name(spec).lower() == wanted:
|
|
376
|
+
return spec
|
|
377
|
+
available = ", ".join(sorted(spec_cli_name(s) for s in specs)) or "none"
|
|
378
|
+
raise UsageError(
|
|
379
|
+
f'Unknown hardware "{hardware}" for {task}.',
|
|
380
|
+
f"Available: {available}.",
|
|
381
|
+
)
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
# ── tags ────────────────────────────────────────────────────────────────────
|
|
385
|
+
|
|
386
|
+
|
|
387
|
+
def tag_index(client: Any, category: str) -> list[dict]:
|
|
388
|
+
key = ("tags", client.base_url, category)
|
|
389
|
+
if key not in _cache:
|
|
390
|
+
payload = client.get("/tags", params={"category": category})
|
|
391
|
+
_cache[key] = list(payload or []) if isinstance(payload, list) else []
|
|
392
|
+
return _cache[key]
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
def tag_label(tag: dict) -> str:
|
|
396
|
+
"""Render a tag as the ``group:name`` form users type."""
|
|
397
|
+
group = str(tag.get("group") or "").strip()
|
|
398
|
+
name = str(tag.get("name") or "").strip()
|
|
399
|
+
return f"{group}:{name}".strip(":") if group else name
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
def resolve_tag_ids(client: Any, category: str, labels: list[str]) -> list[str]:
|
|
403
|
+
"""``["architecture:transformer"]`` -> tag ids, matched case-insensitively."""
|
|
404
|
+
if not labels:
|
|
405
|
+
return []
|
|
406
|
+
index = tag_index(client, category)
|
|
407
|
+
lookup = {tag_label(t).lower(): str(t.get("id")) for t in index}
|
|
408
|
+
lookup.update({str(t.get("name", "")).lower(): str(t.get("id")) for t in index})
|
|
409
|
+
resolved = []
|
|
410
|
+
for label in labels:
|
|
411
|
+
tag_id = lookup.get(label.strip().lower())
|
|
412
|
+
if tag_id is None:
|
|
413
|
+
available = ", ".join(sorted({tag_label(t) for t in index})) or "none"
|
|
414
|
+
raise UsageError(
|
|
415
|
+
f'Unknown {category} tag "{label}".',
|
|
416
|
+
f"Available tags: {available}.",
|
|
417
|
+
)
|
|
418
|
+
resolved.append(tag_id)
|
|
419
|
+
return resolved
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
class HardwareNamer:
|
|
423
|
+
"""Best-effort mapping of a job's stored resources back to a CLI hardware name.
|
|
424
|
+
|
|
425
|
+
Jobs store either a ``computing_spec_id`` (detail responses) or only a
|
|
426
|
+
human-readable ``resource_summary`` such as
|
|
427
|
+
``"8×NVIDIA-B300-SXM6-AC · 96 vCPU · 512 GB RAM"`` (list responses). Both
|
|
428
|
+
are matched against the spec table so the ``hardware`` field the CLI
|
|
429
|
+
publishes is the same word ``--hardware`` accepts.
|
|
430
|
+
|
|
431
|
+
Every failure degrades to ``None`` rather than raising: a listing must not
|
|
432
|
+
break because the spec catalogue moved on.
|
|
433
|
+
"""
|
|
434
|
+
|
|
435
|
+
def __init__(self, client: Any, task: str = "training") -> None:
|
|
436
|
+
self._client = client
|
|
437
|
+
self._task = task
|
|
438
|
+
self._specs: list[dict] | None = None
|
|
439
|
+
|
|
440
|
+
def _load(self) -> list[dict]:
|
|
441
|
+
if self._specs is None:
|
|
442
|
+
from infix_cli.errors import CliError
|
|
443
|
+
|
|
444
|
+
try:
|
|
445
|
+
self._specs = list_specs(
|
|
446
|
+
self._client, enums.RESOURCE_SCOPE.to_backend(self._task),
|
|
447
|
+
)
|
|
448
|
+
except CliError:
|
|
449
|
+
self._specs = []
|
|
450
|
+
return self._specs
|
|
451
|
+
|
|
452
|
+
def by_spec_id(self, spec_id: str | None) -> str | None:
|
|
453
|
+
if not spec_id:
|
|
454
|
+
return None
|
|
455
|
+
for spec in self._load():
|
|
456
|
+
if str(spec.get("id")) == str(spec_id):
|
|
457
|
+
return spec_cli_name(spec)
|
|
458
|
+
return None
|
|
459
|
+
|
|
460
|
+
def by_resources(
|
|
461
|
+
self, *, cpu_cores: int, memory_gb: int, gpu_count: int,
|
|
462
|
+
) -> str | None:
|
|
463
|
+
"""Match a spec by the flat numbers spark stores instead of a spec id."""
|
|
464
|
+
for spec in self._load():
|
|
465
|
+
if (
|
|
466
|
+
int(spec.get("cpu_cores") or 0) == cpu_cores
|
|
467
|
+
and int(spec.get("memory_gb") or 0) == memory_gb
|
|
468
|
+
and int(spec.get("gpu_count") or 0) == gpu_count
|
|
469
|
+
):
|
|
470
|
+
return spec_cli_name(spec)
|
|
471
|
+
return None
|
|
472
|
+
|
|
473
|
+
def by_summary(self, summary: str | None) -> str | None:
|
|
474
|
+
parsed = parse_resource_summary(summary)
|
|
475
|
+
if not parsed:
|
|
476
|
+
return None
|
|
477
|
+
for spec in self._load():
|
|
478
|
+
if _spec_matches(spec, parsed):
|
|
479
|
+
return spec_cli_name(spec)
|
|
480
|
+
return None
|
|
481
|
+
|
|
482
|
+
def resolve(self, *, spec_id: str | None = None, summary: str | None = None) -> str | None:
|
|
483
|
+
return self.by_spec_id(spec_id) or self.by_summary(summary)
|
|
484
|
+
|
|
485
|
+
|
|
486
|
+
def parse_resource_summary(summary: str | None) -> dict | None:
|
|
487
|
+
"""Parse ``"8×NVIDIA-B300-SXM6-AC · 96 vCPU · 512 GB RAM"`` into its parts."""
|
|
488
|
+
text = (summary or "").strip()
|
|
489
|
+
if not text:
|
|
490
|
+
return None
|
|
491
|
+
parsed: dict[str, Any] = {}
|
|
492
|
+
for chunk in text.split("·"):
|
|
493
|
+
piece = chunk.strip()
|
|
494
|
+
if not piece:
|
|
495
|
+
continue
|
|
496
|
+
lowered = piece.lower()
|
|
497
|
+
if lowered.endswith("vcpu"):
|
|
498
|
+
value = piece.split()[0]
|
|
499
|
+
if value.isdigit():
|
|
500
|
+
parsed["cpu_cores"] = int(value)
|
|
501
|
+
continue
|
|
502
|
+
if lowered.endswith("gb ram"):
|
|
503
|
+
value = piece.split()[0]
|
|
504
|
+
if value.isdigit():
|
|
505
|
+
parsed["memory_gb"] = int(value)
|
|
506
|
+
continue
|
|
507
|
+
head, sep, tail = piece.partition("×")
|
|
508
|
+
if not sep:
|
|
509
|
+
head, sep, tail = piece.partition("x")
|
|
510
|
+
if sep and head.strip().isdigit():
|
|
511
|
+
parsed["gpu_count"] = int(head.strip())
|
|
512
|
+
parsed["gpu_type"] = tail.strip()
|
|
513
|
+
return parsed or None
|
|
514
|
+
|
|
515
|
+
|
|
516
|
+
def _spec_matches(spec: dict, parsed: dict) -> bool:
|
|
517
|
+
if int(spec.get("gpu_count") or 0) != int(parsed.get("gpu_count") or 0):
|
|
518
|
+
return False
|
|
519
|
+
if parsed.get("gpu_type") and str(spec.get("gpu_type") or "") != parsed["gpu_type"]:
|
|
520
|
+
return False
|
|
521
|
+
for field in ("cpu_cores", "memory_gb"):
|
|
522
|
+
if field in parsed and int(spec.get(field) or 0) != parsed[field]:
|
|
523
|
+
return False
|
|
524
|
+
return True
|
|
525
|
+
|
|
526
|
+
|
|
527
|
+
def resolve_spark_resource(client: Any, hardware: str, workers: int) -> dict:
|
|
528
|
+
"""Map a hardware name to data-processing's flat resource fields.
|
|
529
|
+
|
|
530
|
+
Isolated on purpose. CreateDataProcessingRequest takes four bare numbers
|
|
531
|
+
(worker / cpu_per_worker / memory_per_worker / gpu_per_worker) where
|
|
532
|
+
training and serving take a computing_spec_id, so the CLI offers one
|
|
533
|
+
vocabulary and converts here. If the backend later accepts a spec id for
|
|
534
|
+
spark too, only this function changes.
|
|
535
|
+
|
|
536
|
+
Side effect worth knowing: because spark never sees the spec, it also never
|
|
537
|
+
goes through the spec availability check — a hardware name that is
|
|
538
|
+
unschedulable right now still submits, and fails later.
|
|
539
|
+
"""
|
|
540
|
+
spec = resolve_hardware(client, hardware, task="spark")
|
|
541
|
+
return {
|
|
542
|
+
"worker": workers,
|
|
543
|
+
"cpu_per_worker": int(spec.get("cpu_cores") or 0),
|
|
544
|
+
"memory_per_worker": int(spec.get("memory_gb") or 0),
|
|
545
|
+
"gpu_per_worker": int(spec.get("gpu_count") or 0),
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
def resolve_model_service(client: Any, value: str) -> dict:
|
|
550
|
+
"""Find a model service for evaluation's ``endpoint`` model source.
|
|
551
|
+
|
|
552
|
+
The PRD asks the user for the service's endpoint URL. The backend, however,
|
|
553
|
+
takes a service *id* and builds the URL itself after checking the service is
|
|
554
|
+
Ready (``evaluation_service.py`` -> ``serving_repo.get_by_id``), so a URL
|
|
555
|
+
sent straight through would 404. The mapping therefore happens here: match
|
|
556
|
+
the URL against each service's endpoint, and accept a bare service name too,
|
|
557
|
+
since that is what ``infix deploy list`` shows in its first column.
|
|
558
|
+
"""
|
|
559
|
+
text = (value or "").strip()
|
|
560
|
+
if not text:
|
|
561
|
+
raise UsageError("--model is empty.")
|
|
562
|
+
|
|
563
|
+
is_url = text.startswith(("http://", "https://"))
|
|
564
|
+
params = {"page_size": 100} if is_url else {"name": text, "page_size": 100}
|
|
565
|
+
payload = client.get("/serving/services", params=params)
|
|
566
|
+
items = list((payload or {}).get("items") or []) if isinstance(payload, dict) else []
|
|
567
|
+
|
|
568
|
+
if is_url:
|
|
569
|
+
wanted = text.rstrip("/")
|
|
570
|
+
matches = [
|
|
571
|
+
item for item in items
|
|
572
|
+
if wanted in {
|
|
573
|
+
str(item.get(field) or "").rstrip("/")
|
|
574
|
+
for field in ("endpoint", "external_endpoint")
|
|
575
|
+
}
|
|
576
|
+
]
|
|
577
|
+
if not matches:
|
|
578
|
+
raise NotFoundError(
|
|
579
|
+
f'No model service is serving "{text}".',
|
|
580
|
+
'Run "infix evaluation create --model <service-name>" instead, '
|
|
581
|
+
"or check the URL against the service detail page.",
|
|
582
|
+
)
|
|
583
|
+
return matches[0]
|
|
584
|
+
|
|
585
|
+
exact = [item for item in items if str(item.get("name") or "") == text]
|
|
586
|
+
if not exact:
|
|
587
|
+
raise NotFoundError(
|
|
588
|
+
f'Model service "{text}" not found.',
|
|
589
|
+
"Only a deployed, Ready service can be evaluated through --model-source endpoint.",
|
|
590
|
+
)
|
|
591
|
+
return exact[0]
|
|
592
|
+
|
|
593
|
+
|
|
594
|
+
def find_service(client: Any, name: str) -> dict:
|
|
595
|
+
"""Locate a model service by exact name, falling back to treating it as an id.
|
|
596
|
+
|
|
597
|
+
``?name=`` is a case-insensitive substring match, so the exact name is
|
|
598
|
+
re-checked here. Names are unique per workspace (a DB constraint).
|
|
599
|
+
"""
|
|
600
|
+
from infix_cli.errors import CliError
|
|
601
|
+
|
|
602
|
+
text = (name or "").strip()
|
|
603
|
+
matches = [
|
|
604
|
+
item
|
|
605
|
+
for item in iter_items(client, "/serving/services", params={"name": text})
|
|
606
|
+
if str(item.get("name", "")) == text
|
|
607
|
+
]
|
|
608
|
+
if matches:
|
|
609
|
+
return matches[0]
|
|
610
|
+
try:
|
|
611
|
+
detail = client.get(f"/serving/services/{text}")
|
|
612
|
+
except CliError:
|
|
613
|
+
detail = None
|
|
614
|
+
if isinstance(detail, dict) and detail.get("id"):
|
|
615
|
+
return detail
|
|
616
|
+
raise NotFoundError(
|
|
617
|
+
f'Service "{text}" not found.',
|
|
618
|
+
'Run "infix deploy list" to see model services in this workspace.',
|
|
619
|
+
)
|