Guarded invocation of MLServe.Model callbacks.
Every call into a backend goes through this module. It exists for three reasons:
- Nothing raises out of a backend into MLServe's runtime. A raise, throw or exit is
converted to
{:error, {:backend_error, %MLServe.BackendError{}}}carrying the original exception and stacktrace. Errors are surfaced, never swallowed — the point of catching is to attach context, not to hide the failure. - Optional callbacks are resolved once.
MLServe.Model.batch_predict/2and friends are detected withfunction_exported?/3and given sane fallbacks. - Return values are validated. A backend that returns a bare value instead of an
{:ok, term}tuple gets a clear error naming the callback, rather than a confusing mismatch three layers up.
Application code does not normally call this module; it is public because backend authors and the guides refer to its semantics.
Summary
Functions
Calls MLServe.Model.batch_predict/2, falling back to a mapped MLServe.Model.predict/2.
Calls MLServe.Model.load/1.
Calls MLServe.Model.metadata/1 when exported, returning %{} otherwise.
Returns whether the backend exports MLServe.Model.batch_predict/2.
Calls MLServe.Model.unload/1 when exported. Always returns :ok.
Types
Functions
@spec batch_predict(MLServe.ModelSpec.t(), term(), [term()]) :: {:ok, [term()]} | {:error, term()}
Calls MLServe.Model.batch_predict/2, falling back to a mapped MLServe.Model.predict/2.
The fallback stops at the first error rather than running the remaining inputs, since a batch
result is all-or-nothing. Backends that can vectorise should implement batch_predict/2; the
whole point of MLServe.batch_predict/3 is one backend round-trip instead of N.
A backend that exports batch_predict/2 but cannot batch this particular model returns
{:error, :not_supported} to opt back into the mapped fallback.
@spec load(MLServe.ModelSpec.t()) :: {:ok, term()} | {:error, term()}
Calls MLServe.Model.load/1.
The spec's :config already carries :version, and :path when one is configured.
@spec metadata(MLServe.ModelSpec.t(), term()) :: map()
Calls MLServe.Model.metadata/1 when exported, returning %{} otherwise.
@spec predict(MLServe.ModelSpec.t(), term(), term()) :: invocation_result()
Calls MLServe.Model.predict/2.
Returns whether the backend exports MLServe.Model.batch_predict/2.
Surfaced in MLServe.model_status/2 so operators can see whether a batch call is one backend
round-trip or N.
Examples
iex> MLServe.Backend.supports_batching?(MLServe.Backend.Static)
true
@spec unload(MLServe.ModelSpec.t(), term()) :: :ok
Calls MLServe.Model.unload/1 when exported. Always returns :ok.
Unload failures are logged, not propagated: the model is going away regardless, and refusing to finish an unload because a backend's cleanup raised would leak the whole model instance.