Principle:Kserve Kserve Model Packaging
| Knowledge Sources | |
|---|---|
| Domains | MLOps, Model_Serving, Packaging |
| Last Updated | 2026-02-13 00:00 GMT |
Overview
A pattern for bundling trained model artifacts with their inference handlers, dependencies, and client interfaces into deployable and testable serving units.
Description
Model Packaging defines how trained model weights, custom handler code, and serving configuration are assembled into a format that a model server can load and serve. In the TorchServe ecosystem, this involves the Model Archiver tool which packages a model definition (e.g., a PyTorch nn.Module), its trained weights, a handler script, and optional supporting files into a .mar (Model Archive) file.
The packaging pattern includes:
- Model definition -- the neural network architecture class (e.g., DenseNet161 for image classification, CNN for MNIST digit recognition).
- Handler -- the inference logic that implements preprocessing, inference, and postprocessing for a specific use case.
- Client code -- utilities for testing the packaged model via HTTP or gRPC protocols, validating that the model serves correctly before deployment.
This separation of model definition from serving infrastructure allows the same model to be deployed on different serving platforms (TorchServe standalone, KServe with TorchServe runtime) without modification.
Usage
Use this principle when:
- Packaging PyTorch models for deployment on TorchServe via KServe
- Creating custom inference handlers for domain-specific preprocessing
- Testing model serving via gRPC or REST clients before production deployment
- Organizing model artifacts for reproducible model deployment pipelines
Theoretical Basis
# Model packaging lifecycle (NOT implementation code)
Packaging phase:
1. Define model architecture class (e.g., DenseNet161, MNIST CNN)
2. Train model and save weights (state_dict or full model)
3. Write handler (preprocess → inference → postprocess)
4. Package with Model Archiver:
Input: model_file + weights + handler + extra_files
Output: model_name.mar (compressed archive)
Serving phase:
1. Upload .mar to storage (S3, GCS, PVC)
2. Deploy via KServe InferenceService with TorchServe runtime
3. TorchServe loads .mar, instantiates model and handler
4. Clients send requests via REST (v1/v2 protocol) or gRPC
Client interaction pattern:
REST: POST /v1/models/<name>:predict { "instances": [...] }
gRPC: PredictionService.Predict(PredictRequest)
- Client serializes input to protocol format
- Server deserializes, runs handler pipeline, returns response
Related Pages
Implemented By
- Implementation:Kserve_Kserve_DenseNet161_ImageClassifier
- Implementation:Kserve_Kserve_MNIST_CNN_Net
- Implementation:Kserve_Kserve_TorchServe_gRPC_Client