MLServe.Config (MLServe v0.1.3)

Copy Markdown View Source

Reads and validates MLServe configuration.

Two jobs: expose the application-wide settings as typed accessors, and turn a per-model keyword list into a validated MLServe.ModelSpec. The same build/2 path serves both static configuration and runtime MLServe.load_model/2, so a model registered at runtime is validated exactly as strictly as one declared in config.exs.

Validation is hand-rolled rather than delegated to a schema library. MLServe has one runtime dependency on purpose, and a few hundred lines of explicit validation buys clearer error messages than a generic schema violation.

Application configuration

config :ml_serve,
  model_root: "priv/models",
  start_mode: :async,
  default_timeout: 5_000,
  max_batch_size: 1_000,
  max_model_bytes: 2_147_483_648,
  cache: [enabled: true, max_size: 10_000, ttl: :timer.minutes(5)],
  models: [
    fraud_detection: [backend: MyApp.Backends.ONNX, path: "fraud.onnx", workers: 4]
  ]

Model options

OptionDefaultMeaning
:backendrequiredModule implementing MLServe.Model
:version"1.0.0"Version string; models are keyed by {name, version}
:pathnilModel artifact, validated by MLServe.Security
:checksumnil{:sha256, hex} integrity check
:workersSystem.schedulers_online()Pool size; ignored for :shared backends
:timeout:default_timeoutPer-request inference timeout
:drain_timeout5_000How long unload_model/2 waits for in-flight requests
:max_concurrency:infinityAdmission limit; excess returns {:error, :overloaded}
:max_batch_size:max_batch_sizeLargest accepted batch_predict/3 list
:batchingnil[max_size: 16, timeout: 10] enables dynamic batching
:cachenil[enabled: true, ttl: 60_000]
:preprocess / :postprocessnil{Mod, :fun, args} hooks run in the caller
:restart_on_errorfalseCrash the worker on a backend exception instead of returning it
:selection:round_robin:round_robin, :least_loaded or :random
:config[]Opaque keyword list passed to the backend's load/1

Summary

Functions

Builds a validated MLServe.ModelSpec from a model name and options.

Same as build/2 but raises MLServe.Error on invalid configuration.

Inference cache settings.

Models declared in application configuration, as {name, opts} pairs.

Default per-request inference timeout in milliseconds.

Default upper bound on MLServe.batch_predict/3 list length.

Largest model artifact MLServe.Security will accept, in bytes.

Root directory model paths must resolve inside. Defaults to priv/models of the app.

Whether models declared in configuration load in the background (:async) or block boot.

Functions

build(name, opts)

@spec build(
  atom(),
  keyword()
) :: {:ok, MLServe.ModelSpec.t()} | {:error, MLServe.Error.t()}

Builds a validated MLServe.ModelSpec from a model name and options.

Parameters

  • name: the model's atom name
  • opts: the model options documented in the moduledoc

Examples

iex> {:ok, spec} = MLServe.Config.build(:fraud, backend: MLServe.Backend.Static, config: [result: :ok])
iex> {spec.name, spec.version, spec.backend}
{:fraud, "1.0.0", MLServe.Backend.Static}

build!(name, opts)

@spec build!(
  atom(),
  keyword()
) :: MLServe.ModelSpec.t()

Same as build/2 but raises MLServe.Error on invalid configuration.

cache()

@spec cache() :: %{
  enabled: boolean(),
  max_size: pos_integer(),
  ttl: pos_integer(),
  sweep_interval: pos_integer()
}

Inference cache settings.

configured_models()

@spec configured_models() :: keyword()

Models declared in application configuration, as {name, opts} pairs.

default_timeout()

@spec default_timeout() :: timeout()

Default per-request inference timeout in milliseconds.

max_batch_size()

@spec max_batch_size() :: pos_integer()

Default upper bound on MLServe.batch_predict/3 list length.

max_model_bytes()

@spec max_model_bytes() :: pos_integer() | :infinity

Largest model artifact MLServe.Security will accept, in bytes.

model_root()

@spec model_root() :: String.t()

Root directory model paths must resolve inside. Defaults to priv/models of the app.

start_mode()

@spec start_mode() :: :async | :sync

Whether models declared in configuration load in the background (:async) or block boot.