@simplexai/localmodels-win32-x64 0.1.0-dev.59 → 0.1.0-dev.60
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.
- package/bootstrap.identity +1 -1
- package/bootstrap.sha256 +79 -73
- package/modules/localmodels/__init__.py +1 -1
- package/modules/localmodels/__main__.py +104 -43
- package/modules/localmodels/apple_sensors.py +431 -0
- package/modules/localmodels/bifrost_admin.py +30 -0
- package/modules/localmodels/bifrost_checks.py +2 -2
- package/modules/localmodels/bifrost_config.py +132 -9
- package/modules/localmodels/bifrost_models.py +1 -1
- package/modules/localmodels/catalog.json +146 -54
- package/modules/localmodels/catalog.py +20 -3
- package/modules/localmodels/catalog_add.py +42 -11
- package/modules/localmodels/custom_hub.py +983 -0
- package/modules/localmodels/custom_hub_http.py +579 -0
- package/modules/localmodels/custom_probe.py +451 -0
- package/modules/localmodels/deploy.py +27 -5
- package/modules/localmodels/deploy_steps.py +5 -0
- package/modules/localmodels/download-mirrors.json +8 -0
- package/modules/localmodels/engines-prism-b12178-03ec600.json +131 -0
- package/modules/localmodels/engines-strata-poly-0.2.20-poly.6.json +94 -0
- package/modules/localmodels/engines.py +38 -8
- package/modules/localmodels/estimator.py +60 -7
- package/modules/localmodels/gateway.py +48 -3
- package/modules/localmodels/gateway_app_bifrost.py +4 -4
- package/modules/localmodels/gateway_bifrost.py +98 -15
- package/modules/localmodels/gguf.py +6 -0
- package/modules/localmodels/host_sensors.py +704 -0
- package/modules/localmodels/library_cli.py +181 -3
- package/modules/localmodels/library_custom.py +831 -0
- package/modules/localmodels/library_elastic.py +35 -7
- package/modules/localmodels/library_gateway.py +94 -32
- package/modules/localmodels/library_jobs.py +76 -11
- package/modules/localmodels/library_kept.py +50 -6
- package/modules/localmodels/library_model.py +254 -2
- package/modules/localmodels/library_pricing.py +83 -4
- package/modules/localmodels/library_serve.py +112 -50
- package/modules/localmodels/library_smoke.py +315 -0
- package/modules/localmodels/library_state.py +77 -1
- package/modules/localmodels/library_status.py +126 -4
- package/modules/localmodels/nvml_ctypes.py +431 -0
- package/modules/localmodels/plan_envelope.py +14 -6
- package/modules/localmodels/records.py +1 -0
- package/modules/localmodels/remote_ops.py +53 -5
- package/modules/localmodels/remote_ops_download.py +168 -29
- package/modules/localmodels/remote_ops_ranges.py +2 -5
- package/modules/localmodels/remote_ops_service.py +2 -1
- package/modules/localmodels/remote_ops_verify.py +22 -5
- package/modules/localmodels/runtime-lock.json +19 -19
- package/modules/localmodels/service_control.py +32 -1
- package/modules/localmodels/service_wiring.py +2 -3
- package/modules/localmodels/service_worker.py +6 -2
- package/modules/localmodels/services_cli.py +35 -27
- package/modules/localmodels/source_byok.py +3 -5
- package/modules/localmodels/stack_lifecycle.py +2 -2
- package/modules/localmodels/stack_memory.py +1 -0
- package/modules/localmodels/stack_view.py +77 -53
- package/modules/localmodels/stack_view_demo.py +36 -5
- package/modules/localmodels/stack_view_events.py +147 -12
- package/modules/localmodels/stack_view_gpu.py +97 -17
- package/modules/localmodels/stack_view_llm.py +15 -0
- package/modules/localmodels/stack_view_measure.py +4 -1
- package/modules/localmodels/stack_view_schema.py +54 -8
- package/modules/localmodels/stack_view_serve.py +40 -3
- package/modules/localmodels/stack_view_slots.py +89 -10
- package/modules/localmodels/stack_view_telemetry.py +163 -0
- package/modules/localmodels/stack_view_text.py +5 -2
- package/modules/localmodels/stack_view_tick.py +658 -0
- package/modules/localmodels/stack_view_words.py +4 -0
- package/modules/localmodels/{strata-poly-serve-0.2.20-poly.5.txt → strata-poly-serve-0.2.20-poly.6.txt} +1 -1
- package/modules/localmodels/suite.py +3 -1
- package/modules/localmodels/telemetry_math.py +538 -0
- package/modules/localmodels/web/view/view.js +1 -1
- package/modules/{simplex_localmodels-0.1.0.dev59.dist-info → simplex_localmodels-0.1.0.dev60.dist-info}/METADATA +1 -1
- package/modules/{simplex_localmodels-0.1.0.dev59.dist-info → simplex_localmodels-0.1.0.dev60.dist-info}/RECORD +76 -70
- package/package.json +1 -1
- package/tool.json +1 -1
- package/modules/localmodels/bifrost_datasheet.py +0 -53
- package/modules/localmodels/bifrost_provider.py +0 -88
- package/modules/localmodels/bifrost_unit.py +0 -99
- package/modules/localmodels/engines-strata-poly-0.2.20-poly.5.json +0 -94
- package/modules/localmodels/gateway_switch.py +0 -51
- package/modules/localmodels/library_floor.py +0 -78
- package/modules/localmodels/service_stop.py +0 -52
- /package/modules/{simplex_localmodels-0.1.0.dev59.dist-info → simplex_localmodels-0.1.0.dev60.dist-info}/WHEEL +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev59.dist-info → simplex_localmodels-0.1.0.dev60.dist-info}/entry_points.txt +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev59.dist-info → simplex_localmodels-0.1.0.dev60.dist-info}/licenses/NOTICE +0 -0
- /package/modules/{simplex_localmodels-0.1.0.dev59.dist-info → simplex_localmodels-0.1.0.dev60.dist-info}/licenses/localmodels/HERMES-LICENSE +0 -0
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
"""Zero-sudo Apple Silicon hardware telemetry (AppleSMC, vm_stat/sysctl, IOAccelerator).
|
|
2
|
+
|
|
3
|
+
Never invokes ``sudo`` or ``powermetrics``. On non-macOS hosts or whenever a private IOKit/SMC
|
|
4
|
+
framework or sensor is unavailable or permission-denied, each sensor degrades independently to
|
|
5
|
+
``value: None, basis: "none"``. IOReport channel sampling is currently inactive without private
|
|
6
|
+
entitlements and reports ``basis: "none"``.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import ctypes
|
|
12
|
+
import re
|
|
13
|
+
import struct
|
|
14
|
+
import subprocess
|
|
15
|
+
import sys
|
|
16
|
+
from typing import Any, TypedDict
|
|
17
|
+
|
|
18
|
+
_IOREPORT_LIB = "/usr/lib/libIOReport.dylib"
|
|
19
|
+
_IOKIT_LIB = "/System/Library/Frameworks/IOKit.framework/IOKit"
|
|
20
|
+
_CF_LIB = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation"
|
|
21
|
+
|
|
22
|
+
# AppleSMC user-client constants (IOConnectCallStructMethod selector 2 = KERNEL_INDEX_SMC)
|
|
23
|
+
_KERNEL_INDEX_SMC = 2
|
|
24
|
+
_SMC_CMD_READ_KEYINFO = 9
|
|
25
|
+
_SMC_CMD_READ_BYTES = 5
|
|
26
|
+
# Candidate Apple Silicon GPU/SoC die temperature SMC keys (tried in order; first valid wins)
|
|
27
|
+
_SMC_TEMP_KEYS = (b"Tg05", b"Tg0D", b"Tp01", b"Tp09", b"TC0P", b"TC0C")
|
|
28
|
+
_PRESSURE_LABELS = {1: "nominal", 2: "elevated", 4: "critical"}
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class SensorReading(TypedDict):
|
|
32
|
+
value: float | None
|
|
33
|
+
basis: str # live | none
|
|
34
|
+
note: str
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ExternalGpuHolder(TypedDict):
|
|
38
|
+
name: str
|
|
39
|
+
pid: int
|
|
40
|
+
bytes: int
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class AppleTelemetry(TypedDict):
|
|
44
|
+
gpu_util_pct: SensorReading
|
|
45
|
+
power_w: SensorReading
|
|
46
|
+
temp_c: SensorReading
|
|
47
|
+
memory_pressure: str # nominal | elevated | critical | unknown
|
|
48
|
+
memory_pressure_basis: str # live | none
|
|
49
|
+
swap_used_bytes: int | None
|
|
50
|
+
gpu_in_use_bytes: int | None
|
|
51
|
+
holders: list[ExternalGpuHolder]
|
|
52
|
+
collector: str
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _missing(note: str) -> SensorReading:
|
|
56
|
+
return {"value": None, "basis": "none", "note": note}
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def _live(value: float, note: str, digits: int = 1) -> SensorReading:
|
|
60
|
+
return {"value": round(value, digits), "basis": "live", "note": note}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def decode_smc_temperature(data_type: bytes, raw: bytes) -> float | None:
|
|
64
|
+
"""Decode an AppleSMC temperature payload (``sp78`` fixed-point or IEEE ``flt ``)."""
|
|
65
|
+
if data_type == b"sp78" and len(raw) >= 2:
|
|
66
|
+
signed = struct.unpack(">h", raw[:2])[0]
|
|
67
|
+
temp = signed / 256.0
|
|
68
|
+
elif data_type == b"flt " and len(raw) >= 4:
|
|
69
|
+
temp = float(struct.unpack("<f", raw[:4])[0])
|
|
70
|
+
else:
|
|
71
|
+
return None
|
|
72
|
+
return round(temp, 1) if -20.0 <= temp <= 130.0 else None
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def parse_ioreport_gpu_residency(state_ticks: dict[str, int]) -> float | None:
|
|
76
|
+
"""Compute GPU active residency % from IOReport state residency ticks.
|
|
77
|
+
|
|
78
|
+
Idle/off states (``IDLE``, ``OFF``, ``DOWN``) are excluded from the active numerator.
|
|
79
|
+
"""
|
|
80
|
+
if not state_ticks:
|
|
81
|
+
return None
|
|
82
|
+
total = 0
|
|
83
|
+
active = 0
|
|
84
|
+
for state, ticks in state_ticks.items():
|
|
85
|
+
if ticks < 0:
|
|
86
|
+
continue
|
|
87
|
+
total += ticks
|
|
88
|
+
upper = state.strip().upper()
|
|
89
|
+
if upper not in ("IDLE", "OFF", "DOWN", ""):
|
|
90
|
+
active += ticks
|
|
91
|
+
if total <= 0:
|
|
92
|
+
return None
|
|
93
|
+
return round(min(100.0, max(0.0, (active / total) * 100.0)), 1)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def parse_ioreport_power_w(energy_nj: float, elapsed_s: float) -> float | None:
|
|
97
|
+
"""Convert IOReport energy delta (nanojoules) over ``elapsed_s`` seconds into watts."""
|
|
98
|
+
if elapsed_s <= 0 or energy_nj < 0:
|
|
99
|
+
return None
|
|
100
|
+
watts = (energy_nj / 1e9) / elapsed_s
|
|
101
|
+
return round(watts, 2) if 0.0 <= watts <= 500.0 else None
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def parse_pressure_level(text: str) -> tuple[str, str]:
|
|
105
|
+
"""Map macOS ``kern.memorystatus_vm_pressure_level`` integer to canonical level + basis."""
|
|
106
|
+
try:
|
|
107
|
+
code = int(text.strip())
|
|
108
|
+
except ValueError:
|
|
109
|
+
return "unknown", "none"
|
|
110
|
+
label = _PRESSURE_LABELS.get(code)
|
|
111
|
+
return (label, "live") if label is not None else ("unknown", "none")
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def parse_swap_used_bytes(text: str) -> int | None:
|
|
115
|
+
"""Parse ``sysctl -n vm.swapusage`` used bytes."""
|
|
116
|
+
match = re.search(r"used\s*=\s*([\d.]+)\s*([KMG])", text)
|
|
117
|
+
if not match:
|
|
118
|
+
return None
|
|
119
|
+
scale = {"K": 1 << 10, "M": 1 << 20, "G": 1 << 30}[match.group(2)]
|
|
120
|
+
return int(float(match.group(1)) * scale)
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
_CREATOR_RE = re.compile(r'"IOUserClientCreator"\s*=\s*"pid\s+(\d+),\s*([^"]+)"')
|
|
124
|
+
_ALLOC_RE = re.compile(r'"(?:In use system memory|Alloc system memory|phys_footprint)"\s*=\s*(\d+)')
|
|
125
|
+
_NODE_HEADER_MARKER = "+-o "
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _ioreg_indent(line: str) -> int:
|
|
129
|
+
idx = 0
|
|
130
|
+
while idx < len(line) and line[idx] in " |":
|
|
131
|
+
idx += 1
|
|
132
|
+
return idx
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
def parse_ioaccelerator_holders(
|
|
136
|
+
text: str,
|
|
137
|
+
*,
|
|
138
|
+
exclude_pids: set[int] | None = None,
|
|
139
|
+
min_bytes: int = 16 << 20,
|
|
140
|
+
limit: int = 8,
|
|
141
|
+
) -> list[ExternalGpuHolder]:
|
|
142
|
+
"""Extract external processes holding GPU memory from ``ioreg -r -c AGXAccelerator`` output."""
|
|
143
|
+
skip = exclude_pids or set()
|
|
144
|
+
by_pid: dict[int, tuple[str, int]] = {}
|
|
145
|
+
current_pid: int | None = None
|
|
146
|
+
current_name = ""
|
|
147
|
+
creator_indent: int | None = None
|
|
148
|
+
for line in text.splitlines():
|
|
149
|
+
if _NODE_HEADER_MARKER in line:
|
|
150
|
+
current_pid = None
|
|
151
|
+
current_name = ""
|
|
152
|
+
creator_indent = None
|
|
153
|
+
continue
|
|
154
|
+
indent = _ioreg_indent(line)
|
|
155
|
+
stripped = line[indent:].strip()
|
|
156
|
+
if creator_indent is not None and stripped.startswith("}") and indent < creator_indent:
|
|
157
|
+
current_pid = None
|
|
158
|
+
current_name = ""
|
|
159
|
+
creator_indent = None
|
|
160
|
+
continue
|
|
161
|
+
creator = _CREATOR_RE.search(line)
|
|
162
|
+
if creator:
|
|
163
|
+
current_pid = int(creator.group(1))
|
|
164
|
+
current_name = creator.group(2).strip()[:128]
|
|
165
|
+
creator_indent = indent
|
|
166
|
+
continue
|
|
167
|
+
if current_pid is not None:
|
|
168
|
+
alloc = _ALLOC_RE.search(line)
|
|
169
|
+
if alloc:
|
|
170
|
+
size = int(alloc.group(1))
|
|
171
|
+
if current_pid not in skip and size > 0:
|
|
172
|
+
prev_name, prev_size = by_pid.get(current_pid, (current_name, 0))
|
|
173
|
+
by_pid[current_pid] = (prev_name or current_name, max(prev_size, size))
|
|
174
|
+
ranked = sorted(
|
|
175
|
+
((name, pid, size) for pid, (name, size) in by_pid.items() if size >= min_bytes),
|
|
176
|
+
key=lambda row: (-row[2], row[0].lower()),
|
|
177
|
+
)
|
|
178
|
+
return [{"name": name, "pid": pid, "bytes": size} for name, pid, size in ranked[:limit]]
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
class _SmcKeyDataVers(ctypes.Structure):
|
|
182
|
+
_fields_ = [
|
|
183
|
+
("major", ctypes.c_uint8),
|
|
184
|
+
("minor", ctypes.c_uint8),
|
|
185
|
+
("build", ctypes.c_uint8),
|
|
186
|
+
("reserved", ctypes.c_uint8),
|
|
187
|
+
("release", ctypes.c_uint16),
|
|
188
|
+
]
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
class _SmcKeyDataPLimit(ctypes.Structure):
|
|
192
|
+
_fields_ = [
|
|
193
|
+
("version", ctypes.c_uint16),
|
|
194
|
+
("length", ctypes.c_uint16),
|
|
195
|
+
("cpuPLimit", ctypes.c_uint32),
|
|
196
|
+
("gpuPLimit", ctypes.c_uint32),
|
|
197
|
+
("memPLimit", ctypes.c_uint32),
|
|
198
|
+
]
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
class _SmcKeyDataKeyInfo(ctypes.Structure):
|
|
202
|
+
_fields_ = [
|
|
203
|
+
("dataSize", ctypes.c_uint32),
|
|
204
|
+
("dataType", ctypes.c_uint32),
|
|
205
|
+
("dataAttributes", ctypes.c_uint8),
|
|
206
|
+
]
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
class _SmcKeyData(ctypes.Structure):
|
|
210
|
+
_fields_ = [
|
|
211
|
+
("key", ctypes.c_uint32),
|
|
212
|
+
("vers", _SmcKeyDataVers),
|
|
213
|
+
("pLimitData", _SmcKeyDataPLimit),
|
|
214
|
+
("keyInfo", _SmcKeyDataKeyInfo),
|
|
215
|
+
("result", ctypes.c_uint8),
|
|
216
|
+
("status", ctypes.c_uint8),
|
|
217
|
+
("data8", ctypes.c_uint8),
|
|
218
|
+
("data32", ctypes.c_uint32),
|
|
219
|
+
("bytes", ctypes.c_uint8 * 32),
|
|
220
|
+
]
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+
def _smc_fourcc(key: bytes) -> int:
|
|
224
|
+
return int.from_bytes(key[:4], "big")
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def mach_task_self(libc: Any | None = None) -> int:
|
|
228
|
+
"""Return the current macOS Mach task port without calling the C macro ``mach_task_self()``."""
|
|
229
|
+
lib = ctypes.cdll.LoadLibrary("/usr/lib/libSystem.B.dylib") if libc is None else libc
|
|
230
|
+
try:
|
|
231
|
+
return int(ctypes.c_uint.in_dll(lib, "mach_task_self_").value)
|
|
232
|
+
except (ValueError, OSError, TypeError, AttributeError):
|
|
233
|
+
trap = lib.task_self_trap
|
|
234
|
+
trap.argtypes = []
|
|
235
|
+
trap.restype = ctypes.c_uint
|
|
236
|
+
return int(trap())
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def _bind_iokit(iokit: Any) -> None:
|
|
240
|
+
iokit.IOServiceMatching.argtypes = [ctypes.c_char_p]
|
|
241
|
+
iokit.IOServiceMatching.restype = ctypes.c_void_p
|
|
242
|
+
iokit.IOServiceGetMatchingService.argtypes = [ctypes.c_uint, ctypes.c_void_p]
|
|
243
|
+
iokit.IOServiceGetMatchingService.restype = ctypes.c_uint
|
|
244
|
+
iokit.IOServiceOpen.argtypes = [
|
|
245
|
+
ctypes.c_uint,
|
|
246
|
+
ctypes.c_uint,
|
|
247
|
+
ctypes.c_uint,
|
|
248
|
+
ctypes.POINTER(ctypes.c_uint),
|
|
249
|
+
]
|
|
250
|
+
iokit.IOServiceOpen.restype = ctypes.c_int
|
|
251
|
+
iokit.IOConnectCallStructMethod.argtypes = [
|
|
252
|
+
ctypes.c_uint,
|
|
253
|
+
ctypes.c_uint32,
|
|
254
|
+
ctypes.c_void_p,
|
|
255
|
+
ctypes.c_size_t,
|
|
256
|
+
ctypes.c_void_p,
|
|
257
|
+
ctypes.POINTER(ctypes.c_size_t),
|
|
258
|
+
]
|
|
259
|
+
iokit.IOConnectCallStructMethod.restype = ctypes.c_int
|
|
260
|
+
iokit.IOServiceClose.argtypes = [ctypes.c_uint]
|
|
261
|
+
iokit.IOServiceClose.restype = ctypes.c_int
|
|
262
|
+
iokit.IOObjectRelease.argtypes = [ctypes.c_uint]
|
|
263
|
+
iokit.IOObjectRelease.restype = ctypes.c_int
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def read_smc_temperature() -> SensorReading:
|
|
267
|
+
"""Read Apple Silicon die temperature via IOKit ``AppleSMC`` without sudo."""
|
|
268
|
+
if sys.platform != "darwin":
|
|
269
|
+
return _missing("AppleSMC is available on macOS only")
|
|
270
|
+
try:
|
|
271
|
+
iokit = ctypes.cdll.LoadLibrary(_IOKIT_LIB)
|
|
272
|
+
_bind_iokit(iokit)
|
|
273
|
+
matching = iokit.IOServiceMatching(b"AppleSMC")
|
|
274
|
+
if not matching:
|
|
275
|
+
return _missing("AppleSMC service not found")
|
|
276
|
+
service = int(iokit.IOServiceGetMatchingService(0, ctypes.c_void_p(matching)))
|
|
277
|
+
if not service:
|
|
278
|
+
return _missing("AppleSMC service unavailable")
|
|
279
|
+
try:
|
|
280
|
+
conn = ctypes.c_uint(0)
|
|
281
|
+
mach_self = mach_task_self()
|
|
282
|
+
if iokit.IOServiceOpen(service, mach_self, 0, ctypes.byref(conn)) != 0:
|
|
283
|
+
return _missing("AppleSMC IOServiceOpen refused")
|
|
284
|
+
try:
|
|
285
|
+
for key in _SMC_TEMP_KEYS:
|
|
286
|
+
reading = _read_smc_key(iokit, conn.value, key)
|
|
287
|
+
if reading is not None:
|
|
288
|
+
return _live(reading, "AppleSMC die temperature")
|
|
289
|
+
finally:
|
|
290
|
+
iokit.IOServiceClose(conn.value)
|
|
291
|
+
finally:
|
|
292
|
+
iokit.IOObjectRelease(service)
|
|
293
|
+
except (OSError, AttributeError, ValueError):
|
|
294
|
+
return _missing("AppleSMC unavailable")
|
|
295
|
+
return _missing("AppleSMC temperature key unavailable")
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def _read_smc_key(iokit: Any, conn: int, key: bytes) -> float | None:
|
|
299
|
+
inp = _SmcKeyData()
|
|
300
|
+
out = _SmcKeyData()
|
|
301
|
+
inp.key = _smc_fourcc(key)
|
|
302
|
+
inp.data8 = _SMC_CMD_READ_KEYINFO
|
|
303
|
+
out_size = ctypes.c_size_t(ctypes.sizeof(out))
|
|
304
|
+
rc = iokit.IOConnectCallStructMethod(
|
|
305
|
+
conn,
|
|
306
|
+
_KERNEL_INDEX_SMC,
|
|
307
|
+
ctypes.byref(inp),
|
|
308
|
+
ctypes.sizeof(inp),
|
|
309
|
+
ctypes.byref(out),
|
|
310
|
+
ctypes.byref(out_size),
|
|
311
|
+
)
|
|
312
|
+
if rc != 0 or out.keyInfo.dataSize == 0 or out.keyInfo.dataSize > 32:
|
|
313
|
+
return None
|
|
314
|
+
dtype = out.keyInfo.dataType.to_bytes(4, "big")
|
|
315
|
+
inp.keyInfo.dataSize = out.keyInfo.dataSize
|
|
316
|
+
inp.data8 = _SMC_CMD_READ_BYTES
|
|
317
|
+
out_size = ctypes.c_size_t(ctypes.sizeof(out))
|
|
318
|
+
rc = iokit.IOConnectCallStructMethod(
|
|
319
|
+
conn,
|
|
320
|
+
_KERNEL_INDEX_SMC,
|
|
321
|
+
ctypes.byref(inp),
|
|
322
|
+
ctypes.sizeof(inp),
|
|
323
|
+
ctypes.byref(out),
|
|
324
|
+
ctypes.byref(out_size),
|
|
325
|
+
)
|
|
326
|
+
if rc != 0:
|
|
327
|
+
return None
|
|
328
|
+
raw = bytes(out.bytes[: out.keyInfo.dataSize])
|
|
329
|
+
return decode_smc_temperature(dtype, raw)
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def read_ioreport_gpu_and_power() -> tuple[SensorReading, SensorReading]:
|
|
333
|
+
"""Read Apple Silicon GPU residency (%) and package power (W) via user-space IOReport.
|
|
334
|
+
|
|
335
|
+
Degrades gracefully to ``basis: "none"`` when ``libIOReport.dylib`` or channels are unavailable.
|
|
336
|
+
"""
|
|
337
|
+
if sys.platform != "darwin":
|
|
338
|
+
return (
|
|
339
|
+
_missing("IOReport GPU residency is available on macOS only"),
|
|
340
|
+
_missing("IOReport package power is available on macOS only"),
|
|
341
|
+
)
|
|
342
|
+
try:
|
|
343
|
+
ioreport = ctypes.cdll.LoadLibrary(_IOREPORT_LIB)
|
|
344
|
+
cf = ctypes.cdll.LoadLibrary(_CF_LIB)
|
|
345
|
+
# Verify required entry points exist before invoking private symbols.
|
|
346
|
+
for sym in (
|
|
347
|
+
"IOReportCopyChannelsInGroup",
|
|
348
|
+
"IOReportCreateSubscription",
|
|
349
|
+
"IOReportCreateSamples",
|
|
350
|
+
"IOReportCreateSamplesDelta",
|
|
351
|
+
):
|
|
352
|
+
if not hasattr(ioreport, sym):
|
|
353
|
+
return _missing("IOReport symbol unavailable"), _missing(
|
|
354
|
+
"IOReport symbol unavailable"
|
|
355
|
+
)
|
|
356
|
+
if not hasattr(cf, "CFStringCreateWithCString") or not hasattr(cf, "CFRelease"):
|
|
357
|
+
return _missing("CoreFoundation unavailable"), _missing("CoreFoundation unavailable")
|
|
358
|
+
except OSError:
|
|
359
|
+
return _missing("libIOReport unavailable"), _missing("libIOReport unavailable")
|
|
360
|
+
# Private IOReport dictionary iteration requires CFAllocator/CFArray callbacks that vary across
|
|
361
|
+
# macOS releases; fail closed to basis="none" rather than risking a segfault or invoking sudo.
|
|
362
|
+
return (
|
|
363
|
+
_missing("IOReport channel telemetry unavailable without entitlements"),
|
|
364
|
+
_missing("IOReport power telemetry unavailable without entitlements"),
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def _sysctl(key: str) -> str | None:
|
|
369
|
+
if sys.platform != "darwin":
|
|
370
|
+
return None
|
|
371
|
+
try:
|
|
372
|
+
proc = subprocess.run(
|
|
373
|
+
["/usr/sbin/sysctl", "-n", key],
|
|
374
|
+
capture_output=True,
|
|
375
|
+
text=True,
|
|
376
|
+
timeout=1.5,
|
|
377
|
+
check=False,
|
|
378
|
+
)
|
|
379
|
+
return proc.stdout if proc.returncode == 0 else None
|
|
380
|
+
except (OSError, subprocess.SubprocessError):
|
|
381
|
+
return None
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def _ioreg_agx() -> str | None:
|
|
385
|
+
if sys.platform != "darwin":
|
|
386
|
+
return None
|
|
387
|
+
try:
|
|
388
|
+
proc = subprocess.run(
|
|
389
|
+
["/usr/sbin/ioreg", "-r", "-c", "AGXAccelerator"],
|
|
390
|
+
capture_output=True,
|
|
391
|
+
text=True,
|
|
392
|
+
timeout=1.5,
|
|
393
|
+
check=False,
|
|
394
|
+
)
|
|
395
|
+
return proc.stdout if proc.returncode == 0 else None
|
|
396
|
+
except (OSError, subprocess.SubprocessError):
|
|
397
|
+
return None
|
|
398
|
+
|
|
399
|
+
|
|
400
|
+
def read_memory_pressure() -> tuple[str, str, int | None]:
|
|
401
|
+
"""Read macOS unified memory pressure level, basis, and swap usage via sysctl."""
|
|
402
|
+
raw_level = _sysctl("kern.memorystatus_vm_pressure_level")
|
|
403
|
+
level, basis = parse_pressure_level(raw_level) if raw_level is not None else ("unknown", "none")
|
|
404
|
+
raw_swap = _sysctl("vm.swapusage")
|
|
405
|
+
swap_bytes = parse_swap_used_bytes(raw_swap) if raw_swap is not None else None
|
|
406
|
+
return level, basis, swap_bytes
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def read_telemetry(*, exclude_pids: set[int] | None = None) -> AppleTelemetry:
|
|
410
|
+
"""Collect zero-sudo Apple Silicon telemetry; every unavailable field reports basis='none'."""
|
|
411
|
+
gpu_util, power_w = read_ioreport_gpu_and_power()
|
|
412
|
+
temp_c = read_smc_temperature()
|
|
413
|
+
level, pressure_basis, swap_bytes = read_memory_pressure()
|
|
414
|
+
agx_text = _ioreg_agx()
|
|
415
|
+
in_use: int | None = None
|
|
416
|
+
holders: list[ExternalGpuHolder] = []
|
|
417
|
+
if agx_text:
|
|
418
|
+
matches = [int(m) for m in re.findall(r'"In use system memory"\s*=\s*(\d+)', agx_text)]
|
|
419
|
+
in_use = max(matches) if matches else None
|
|
420
|
+
holders = parse_ioaccelerator_holders(agx_text, exclude_pids=exclude_pids)
|
|
421
|
+
return {
|
|
422
|
+
"gpu_util_pct": gpu_util,
|
|
423
|
+
"power_w": power_w,
|
|
424
|
+
"temp_c": temp_c,
|
|
425
|
+
"memory_pressure": level,
|
|
426
|
+
"memory_pressure_basis": pressure_basis,
|
|
427
|
+
"swap_used_bytes": swap_bytes,
|
|
428
|
+
"gpu_in_use_bytes": in_use,
|
|
429
|
+
"holders": holders,
|
|
430
|
+
"collector": "AppleSMC · sysctl · IOAccelerator",
|
|
431
|
+
}
|
|
@@ -380,3 +380,33 @@ def sync_live(
|
|
|
380
380
|
bifrost_config.write_config(app, config, params, drop_store=False)
|
|
381
381
|
if sheet_changed:
|
|
382
382
|
_request(addr, app, "POST", "/api/pricing/force-sync")
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
def drop_live_provider(
|
|
386
|
+
record: hosts.OwnedBifrost,
|
|
387
|
+
pid: str,
|
|
388
|
+
*,
|
|
389
|
+
desired_rules: list[dict[str, Any]] | None = None,
|
|
390
|
+
) -> bool:
|
|
391
|
+
"""Remove an orphaned ``pid`` from a running Bifrost via Admin API if present.
|
|
392
|
+
|
|
393
|
+
Returns True when ``pid`` existed in live Bifrost and was deleted (also reconciling routing
|
|
394
|
+
rules when ``desired_rules`` is given), or False when live Bifrost already omitted ``pid``.
|
|
395
|
+
"""
|
|
396
|
+
addr = record["addr"]
|
|
397
|
+
app = Path(record["app_dir"])
|
|
398
|
+
providers_resp = _request(addr, app, "GET", "/api/providers")
|
|
399
|
+
raw_providers = providers_resp.get("providers")
|
|
400
|
+
if not isinstance(raw_providers, list):
|
|
401
|
+
raise AdminError("Bifrost admin GET /api/providers returned no providers list")
|
|
402
|
+
if not any(isinstance(row, dict) and row.get("name") == pid for row in raw_providers):
|
|
403
|
+
return False
|
|
404
|
+
_request(addr, app, "DELETE", f"/api/providers/{urllib.parse.quote(pid, safe='')}")
|
|
405
|
+
if desired_rules is not None:
|
|
406
|
+
rules_resp = _request(addr, app, "GET", "/api/governance/routing-rules?limit=1000")
|
|
407
|
+
raw_rules = rules_resp.get("rules")
|
|
408
|
+
live_rules = (
|
|
409
|
+
[r for r in raw_rules if isinstance(r, dict)] if isinstance(raw_rules, list) else []
|
|
410
|
+
)
|
|
411
|
+
_sync_rules(addr, app, live_rules, desired_rules)
|
|
412
|
+
return True
|
|
@@ -19,7 +19,6 @@ from pathlib import Path
|
|
|
19
19
|
from typing import Any, Literal, TypedDict
|
|
20
20
|
|
|
21
21
|
from localmodels import bifrost_pi, gateway_requests, harness, harness_gateway, hosts
|
|
22
|
-
from localmodels.bifrost_unit import unit
|
|
23
22
|
from localmodels.gateway_bifrost import (
|
|
24
23
|
ADMIN_USER,
|
|
25
24
|
AGENT_MODEL,
|
|
@@ -36,6 +35,7 @@ from localmodels.gateway_bifrost import (
|
|
|
36
35
|
owned_listener,
|
|
37
36
|
read_config,
|
|
38
37
|
require_target,
|
|
38
|
+
unit,
|
|
39
39
|
)
|
|
40
40
|
from localmodels.gateway_requests import AuxEndpoint, Check
|
|
41
41
|
from localmodels.harness import HarnessPlan
|
|
@@ -273,7 +273,7 @@ def accept(
|
|
|
273
273
|
"listed" if model_id in ids else "not listed",
|
|
274
274
|
)
|
|
275
275
|
if gw.get("context"):
|
|
276
|
-
# From
|
|
276
|
+
# From bifrost_config datasheets; clients that read the listing size their prompts by it.
|
|
277
277
|
stated = sorted({str(rows.get(i, {}).get("context_length")) for i in llm_ids})
|
|
278
278
|
check(
|
|
279
279
|
"bifrost states the LLM's window",
|