best_engine_ai_helper.detect module
detect — turn raw hardware facts into AI-inference throughput estimates.
Raw probing (subprocess calls to nvidia-smi / rocm-smi /
system_profiler / lspci, CPU/RAM introspection) is NOT done here: it
lives in os_helper.hardware_utils, a generic cross-platform hardware probe
shared by every repo in the AI Helpers suite. This module is the thin,
AI-domain layer on top of it — it takes the vendor + chip/GPU name + memory
size that os_helper reports and turns them into what a local model picker
actually needs: an accelerator kind, a memory-bandwidth estimate, and the
memory pool available for inference.
Memory bandwidth is the ceiling on decode throughput (token generation reads
the whole active model from memory once per token), so it — not raw core
count — is what compute_profile() reports and what
score.estimated_tokens_per_second keys its tokens/s estimate on. Apple
Silicon publishes per-chip bandwidth; NVIDIA/AMD publish it per GPU model.
Both are tabulated below by substring match on the chip/GPU name os_helper
reports; an unrecognised model degrades gracefully to “throughput not
estimated” rather than a wrong number.
- best_engine_ai_helper.detect.available_memory()[source]
Detect available memory for model inference.
Reads three pools from
os_helper’s hardware facts:Apple Silicon unified memory (macOS with Apple chip)
NVIDIA/AMD VRAM (summed across all visible GPUs)
System RAM (always populated)
- Returns:
A dict with the following keys:
- unified_gbfloat or None
Apple Silicon unified memory pool, in GB.
- vram_gbfloat or None
Discrete GPU VRAM (sum of all visible GPUs), in GB.
- ram_gbfloat
Total system RAM in GB. Always a positive float.
- Return type:
Examples
>>> mem = available_memory() >>> set(mem.keys()) == {'unified_gb', 'vram_gb', 'ram_gb'} True >>> mem['ram_gb'] > 0 True
- best_engine_ai_helper.detect.chip_name()[source]
Return the Apple Silicon chip name (e.g.
"Apple M2 Max"), or None.Delegates to
os_helper.apple_chip_name(); None on non-macOS platforms or when the chip line is absent (old Intel Macs).- Return type:
str | None
- best_engine_ai_helper.detect.chip_vendor()[source]
Identify the primary compute vendor for model inference.
- Returns:
One of: ‘apple’, ‘nvidia’, ‘amd’, ‘intel’, ‘cpu’. Delegates to
os_helper.gpu_vendor().- Return type:
Examples
>>> chip_vendor() in ('apple', 'nvidia', 'amd', 'intel', 'cpu') True
- best_engine_ai_helper.detect.compute_profile()[source]
Describe the machine’s inference accelerator and memory bandwidth.
Returns a dict with:
accelerator:"gpu-metal"(Apple Silicon),"gpu-cuda"(NVIDIA),"gpu-rocm"(AMD), or"cpu"(no discrete accelerator detected).chip: the chip / GPU name when known, else None.bandwidth_gbs: memory bandwidth in GB/s when the chip/GPU matches a known model in_APPLE_BANDWIDTH_GBS/_NVIDIA_BANDWIDTH_GBS/_AMD_BANDWIDTH_GBS, else None. This is the ceiling on decode throughput; token generation reads the whole active model from memory once per token, so tokens/s scales with it.
Bandwidth is tabulated for Apple Silicon (per-chip) and for discrete NVIDIA/AMD GPUs (per-board, matched on the model name
os_helperreports). An unrecognised GPU model — a new SKU not yet in the table, or a multi-GPU box where the name string is ambiguous — degrades tobandwidth_gbs: Nonerather than a fabricated number; callers treat that as “throughput not estimated”, never as zero.
- best_engine_ai_helper.detect.disk_profile(measure=False, directory=None)[source]
Describe the storage this machine would stream model layers from.
The counterpart of
compute_profile()for a disk-bound backend. Where memory bandwidth bounds an in-memory decode, disk bandwidth bounds a layer-streaming one (offload/lightweight), which reads every layer back per token.- Parameters:
measure (bool) – Run
measure_disk_read_gbs()instead of reading the class table. Off by default: it writes a few hundred MB and takes a second or two, and a recommendation should stay a deterministic function of the hardware unless the caller asks for a measurement.directory (str or None) – Volume to measure, when
measureis True. None uses the temp directory.
- Returns:
storagestrClass from
storage_class().read_gbsfloat or NoneSequential read bandwidth in GB/s; None when the class is unknown and no measurement succeeded.
basis{‘measured’, ‘declared’, ‘unknown’}Where
read_gbscame from — the same honesty rule the throughput estimates follow: a measured number is never presented as a table lookup, nor the reverse.free_gbfloat or NoneFree space on the volume holding the home directory, where both the HuggingFace cache lands.
- Return type:
Examples
>>> p = disk_profile() >>> p['basis'] in ('measured', 'declared', 'unknown') True >>> p['read_gbs'] is None or p['read_gbs'] > 0 True
- best_engine_ai_helper.detect.measure_disk_read_gbs(total_bytes=268435456, directory=None)[source]
Measure sequential read bandwidth (GB/s), defeating the page cache.
Writes a scratch file, flushes it to the device, then reads it back with caching disabled for that descriptor (
F_NOCACHEon macOS,O_DIRECTon Linux). Without that step the read is served from RAM and reports the memory bus, not the drive — a number three times too high, which would make layer streaming look comfortable when it is not.Returns None rather than a cached-read figure on any platform that offers no way to bypass the cache (Windows), so an unmeasurable machine falls back to the declared class figure instead of to a flattering one.
- Parameters:
total_bytes (int) – Bytes to write and read back. Rounded down to a multiple of 4 MiB, the alignment
O_DIRECTrequires. Defaults to 256 MiB.directory (str or None) – Where to put the scratch file. None uses the system temp directory. Point it at the volume holding the model cache when that differs.
- Returns:
Measured GB/s (rounded to two decimals), or None when the measurement is unavailable or the platform cannot bypass its cache.
- Return type:
float or None
Examples
>>> v = measure_disk_read_gbs(total_bytes=4 * 1024 * 1024) >>> v is None or v > 0 True
- best_engine_ai_helper.detect.platform_name()[source]
Return the current OS as a short lowercase string.
- Returns:
One of: ‘darwin’, ‘linux’, ‘windows’. Delegates to
os_helper.platform_name().- Return type:
Examples
>>> platform_name() in ('darwin', 'linux', 'windows') True
- best_engine_ai_helper.detect.server_load()[source]
Snapshot the machine’s CURRENT load — as opposed to its static capacity.
Requires an os-helper release whose
hardware_info()includes the live fieldscpu.percent/available_ram_gb/disk/gpu_utilization_percent— available since os-helper 2.3.0, the floor pinned inpyproject.toml(os-helper>=2.3.0,<3), so any install of this package already has them.Feeds
score.effective_budget()’s optionalloadparameter, so a recommendation reflects what else is happening on this machine right now: another process (or an already-running engine) can hold memory the static hardware totals fromavailable_memory()know nothing about.- Returns:
available_ram_gbfloatFree system RAM right now.
cpu_percentfloatInstantaneous CPU utilization, 0-100.
gpu_percentfloat or NoneLive discrete-GPU utilization, 0-100; None on Apple Silicon, CPU-only machines, or when the vendor CLI is unavailable.
disk_free_gbfloatFree space on the disk holding the home directory (where model caches live).
disk_percent_usedfloat0-100.
running_enginesintBest-effort count of already-loaded Ollama models plus running vLLM server processes.
- Return type:
Examples
>>> load = server_load() >>> load["available_ram_gb"] >= 0 True >>> 0 <= load["cpu_percent"] <= 100 True >>> load["running_engines"] >= 0 True
- best_engine_ai_helper.detect.storage_class()[source]
Classify the drive that holds the model cache.
Layer streaming reads the weights off this drive once per token, so its class — not the memory bus — is what bounds throughput. Classification is deliberately coarse (four classes, see
_DISK_READ_GBS): the goal is the right order of magnitude for an estimate, not a device database.- Returns:
One of
'apple-internal','nvme','ssd-sata','hdd'or'unknown'.'unknown'is returned rather than a guess when the platform exposes no rotational/transport hint, so callers can say “not estimated” instead of inventing a speed.- Return type:
Examples
>>> storage_class() in ('apple-internal', 'nvme', 'ssd-sata', 'hdd', 'unknown') True