Ir para o conteúdo

tempest_fastapi_sdk.genai.inventory

O que está carregado em memória agora e quanto isso custa.

« IA e visão computacional


inventory

What is loaded in memory right now, and what it is costing.

A self-hosted service can hold several models at once — a language model, an embedder, a reranker, a diffusion pipeline — each holding gigabytes of VRAM for as long as it stays loaded. Until this module, nothing could answer the operational question: what is resident right now? The registry knew how many entries it held, not which, and every loader kept its idle clock to itself.

  • :func:describe_model turns any SDK loader (or any object that walks like one) into a :class:LoadedModel, reading whatever it exposes and reporting None for the rest rather than guessing.
  • :func:runtime_report puts those next to the host's memory picture in a :class:ModelRuntimeReport — the two halves of "is this box healthy" in one payload.

Both are pure Python: this module imports and tests with no extra installed, and describe_model never triggers a load.

LoadedModel

Bases: BaseSchema

One model handle, as it stands right now.

Every field except kind and loaded is optional, because the inventory reports what a handle actually exposes. A third-party object that only implements is_loaded still appears — with the rest None — which beats omitting it from a memory audit.

Attributes:

Name Type Description
key str | None

The registry key, when it came from one.

kind str

The handle's class name ("TextGenerator"). The SDK reports the class rather than inventing a taxonomy over it.

model_id str | None

Hub id, Whisper size or graph path.

device str | None

Where it is resident (cuda / cpu).

dtype str | None

Compute precision, when the handle resolves one.

loaded bool

Whether the weights are in memory now.

seconds_idle float | None

Time since the last use, when the handle tracks it.

idle_unload_seconds float | None

The handle's configured idle threshold, when it has one.

unloadable bool

Whether the handle exposes unload().

idle_past_threshold property

idle_past_threshold: bool

Return whether this handle is due to be unloaded.

Returns:

Name Type Description
bool bool

True only when the handle is loaded, tracks its idle

bool

time, has a threshold configured, and has crossed it. Any

bool

missing piece answers False — an unknown is not a reason to

bool

free someone's VRAM.

ModelRuntimeReport

Bases: BaseSchema

The models resident right now, next to the host's memory picture.

Attributes:

Name Type Description
models list[LoadedModel]

Every known handle, loaded first and then by longest idle — the order an operator reads when looking for what to free.

loaded_count int

How many are resident.

total_count int

How many handles are known, loaded or not.

hardware HardwareInfo | None

The host snapshot, when probed.

describe_model

describe_model(model: object, *, key: str | None = None) -> LoadedModel

Describe one model handle without loading or touching it.

Reads only attributes, so it is safe to call on a lazily-configured handle: a generator that has never loaded reports loaded=False and stays unloaded.

Example:

>>> info = describe_model(generator, key="chat-lm")
>>> info.kind, info.loaded
('TextGenerator', True)

Parameters:

Name Type Description Default
model object

Any SDK loader — TextGenerator, Embedder, ImageGenerator, Reranker, SpeechToText, OnnxEmbedder, ClassifierModerator — or any object exposing a similar surface.

required
key str | None

The registry key to record alongside it.

None

Returns:

Name Type Description
LoadedModel LoadedModel

What the handle exposes; None for what it does

LoadedModel

not, never a guess.

runtime_report

runtime_report(
    models: Mapping[str, object] | Sequence[object],
    *,
    hardware: HardwareInfo | None = None,
    probe: bool = True,
) -> ModelRuntimeReport

Describe every handle you hold, next to the host's memory.

Sorting puts loaded handles first and, among them, the longest-idle first: that is the order someone reads when a card is full and they need to decide what to free.

Example:

>>> report = runtime_report({"chat": generator, "embed": embedder})
>>> report.loaded_count
1

Parameters:

Name Type Description Default
models Mapping[str, object] | Sequence[object]

The handles, either keyed (the keys land on LoadedModel.key) or as a sequence.

required
hardware HardwareInfo | None

A snapshot to reuse instead of probing again.

None
probe bool

When no hardware is given, probe the host. Pass False to skip it — probing reads NVML (or, without pynvml installed, initializes CUDA through torch, which creates a context on every GPU) and is the only part of this call that costs anything.

True

Returns:

Name Type Description
ModelRuntimeReport ModelRuntimeReport

The handles plus, optionally, the host

ModelRuntimeReport

snapshot.