Sub topic 4
Serving runtimes: how a model format maps to a server
This chapter assumes you understand Kubernetes pods, Deployments, Services, namespaces, and YAML manifests. You should also know how to save a trained model and call an HTTP endpoint with JSON.
Why the runtime matters
A model file is not an HTTP service by itself. A serving runtime is the server process that loads the model, accepts inference requests, and returns predictions. It also handles details such as model loading, health checks, request serialization, and protocol support.
KServe connects an InferenceService to a runtime. The InferenceService describes the model and its storage location. The runtime supplies the container image and the code needed to serve that model.
For example, a scikit-learn model can use a runtime that knows how to load scikit-learn artifacts. A TensorFlow model needs a runtime that understands TensorFlow SavedModel files. The model format and the runtime must agree.
| Model format | Typical runtime capability | Common use |
|---|---|---|
| scikit-learn | Loads scikit-learn model artifacts | Classical machine learning |
| TensorFlow | Loads TensorFlow SavedModel | Neural networks |
| PyTorch | Loads TorchScript or PyTorch artifacts | Neural networks |
| ONNX | Loads portable ONNX graphs | Cross-framework serving |
| Custom | Runs your own predictor code | Special preprocessing or model formats |
ServingRuntime resources
A ServingRuntime is a namespaced Kubernetes resource. It defines one or more containers and declares the model formats that those containers support. A ClusterServingRuntime has the same purpose but can be used by InferenceServices across namespaces.
KServe can choose a compatible runtime when the InferenceService specifies a model format. The important value is the modelFormat name, such as sklearn, tensorflow, or pytorch. Runtime selection also considers whether the runtime is available in the service's namespace and whether it supports the requested format version.
This relationship can be viewed as a lookup:
A runtime may support multiple versions of a format. If a runtime declares a format without the version you request, KServe may not select it. Check the runtime definitions installed in your cluster before changing modelFormat values.
Selecting a runtime from an InferenceService
This manifest asks KServe to serve a scikit-learn model from object storage:
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
name: sklearn-iris
spec:
predictor:
model:
modelFormat:
name: sklearn
storageUri: gs://kfserving-examples/models/sklearn/1.0/modelThe name sklearn does not convert an arbitrary file into a scikit-learn model. The files at storageUri must have the structure expected by the selected runtime. If the artifact was saved with a different library, use its matching format and runtime instead.
Built-in and custom runtimes
Built-in runtimes reduce setup work. They provide tested images and standard request handling for supported frameworks. They are a good choice when your model uses a common format and needs little custom logic.
Use a custom runtime when you need unusual loading code, custom dependencies, special request handling, or a model format that KServe does not provide. A custom runtime must still expose the health and inference behavior expected by KServe. You can package it as a container or implement it with the KServe Python SDK.
Common runtime errors include:
- The model format name does not match any available runtime.
- The runtime exists only in another namespace.
- The model files are incomplete or saved in the wrong format.
- The container image cannot pull, or its dependencies are incompatible.
- The runtime starts, but readiness fails while loading the model.
A practical selection process
- Identify the library and artifact format used when the model was saved.
- Find a built-in ServingRuntime that supports that format.
- Confirm that the runtime is available to the target namespace.
- Set modelFormat in the InferenceService.
- Check the InferenceService and pod conditions after applying the manifest.
- Use a custom runtime only when a built-in runtime cannot meet the workload's needs.
Exercise
An InferenceService points to a PyTorch artifact but sets modelFormat.name to sklearn. What should you investigate first?
Show answer
Check how the artifact was saved, then change the model format and runtime to a compatible PyTorch option, or convert the artifact before deploying it.