Overview
Model_Manifest defines the Model class that stores model-specific metadata for inclusion in a TorchServe model archive's MANIFEST.json. The class accepts model name, serialized file, handler, model file, model version, extensions, requirements file, and config file as constructor parameters. Its __to_dict__ method serializes all fields, extracting basenames from file paths for portable archive references.
Description
The Model class is a data container that holds all model-specific metadata needed for a TorchServe model archive. When a .mar file is created, the Model instance is passed to the Manifest class, which includes it in the MANIFEST.json. The __to_dict__ method carefully extracts only the basename of file paths (using os.path.basename) so that the manifest references are portable across different directory structures.
Key Responsibilities
- Metadata Storage: Stores all model-related metadata fields (name, version, handler, files)
- Path Normalization: Extracts basenames from full file paths for portable references in the archive
- Conditional Serialization: Only includes fields in the output dictionary when they have non-None values
- Archive Integration: Provides the model section of
MANIFEST.json via __to_dict__
Code Reference
Source Location
| File |
Lines |
Repository
|
model-archiver/model_archiver/manifest_components/model.py |
L1-68 |
pytorch/serve
|
Signature
class Model:
"""
Model metadata for MANIFEST.json in a .mar model archive.
Stores all model-specific fields and serializes them with
basename-only file references for portability.
"""
def __init__(
self,
model_name,
serialized_file,
handler,
model_file=None,
model_version=None,
extensions=None,
requirements_file=None,
config_file=None,
):
"""
Initialize model metadata.
Args:
model_name (str): Name of the model.
serialized_file (str): Path to the serialized model weights
(e.g., .pt, .pth, .onnx file).
handler (str): Path to the handler file or built-in handler name.
model_file (str, optional): Path to the model architecture
definition file. Defaults to None.
model_version (str, optional): Semantic version string.
Defaults to None.
extensions (str, optional): Path to extensions/plugins.
Defaults to None.
requirements_file (str, optional): Path to pip requirements
file for dependencies. Defaults to None.
config_file (str, optional): Path to model configuration
YAML file. Defaults to None.
"""
...
def __to_dict__(self):
"""
Serialize model metadata to a dictionary.
Extracts os.path.basename from all file path fields to
ensure portability. Only includes fields with non-None values.
Returns:
dict: Model metadata with keys such as:
- 'modelName': model name string
- 'serializedFile': basename of serialized file
- 'handler': handler name or basename
- 'modelFile': basename of model file (if set)
- 'modelVersion': version string (if set)
- 'extensions': basename of extensions (if set)
- 'requirementsFile': basename of requirements (if set)
- 'configFile': basename of config file (if set)
"""
...
Import
from model_archiver.manifest_components.model import Model
# Standard library dependency:
import os
I/O Contract
| Method |
Input |
Output |
Notes
|
__init__(...) |
model_name: str; serialized_file: str; handler: str; optional: model_file, model_version, extensions, requirements_file, config_file |
None (sets instance attributes) |
Lines 7-27; all file paths stored as-is
|
__to_dict__() |
None |
dict: serialized model metadata |
Lines 29-68; uses os.path.basename on file paths; omits None fields
|
Serialized Output Format
{
"modelName": "text_classifier",
"serializedFile": "model.pt",
"handler": "handler.py",
"modelFile": "model.py",
"modelVersion": "1.0",
"requirementsFile": "requirements.txt",
"configFile": "model-config.yaml"
}
Usage Examples
Example 1: Basic Model Metadata
from model_archiver.manifest_components.model import Model
# Create model metadata with required fields only
model = Model(
model_name="resnet50",
serialized_file="/path/to/resnet50.pt",
handler="image_classifier"
)
# Serialize - note basename extraction
model_dict = model.__to_dict__()
# {'modelName': 'resnet50', 'serializedFile': 'resnet50.pt', 'handler': 'image_classifier'}
Example 2: Full Model Metadata
from model_archiver.manifest_components.model import Model
# Create model metadata with all optional fields
model = Model(
model_name="bert_classifier",
serialized_file="/models/bert/bert_classifier.pt",
handler="/handlers/bert_handler.py",
model_file="/models/bert/model.py",
model_version="2.1.0",
extensions=None,
requirements_file="/models/bert/requirements.txt",
config_file="/models/bert/model-config.yaml"
)
model_dict = model.__to_dict__()
# {
# 'modelName': 'bert_classifier',
# 'serializedFile': 'bert_classifier.pt',
# 'handler': 'bert_handler.py',
# 'modelFile': 'model.py',
# 'modelVersion': '2.1.0',
# 'requirementsFile': 'requirements.txt',
# 'configFile': 'model-config.yaml'
# }
Example 3: Integration with Manifest
from model_archiver.manifest_components.model import Model
from model_archiver.manifest_components.manifest import Manifest, RuntimeType
# Model metadata feeds into the top-level Manifest
model = Model(
model_name="dlrm",
serialized_file="dlrm_model.pt",
handler="dlrm_handler.py",
model_version="1.0"
)
manifest = Manifest(runtime=RuntimeType.PYTHON3, model=model)
# The manifest __to_dict__ includes the model dict under the "model" key
manifest_dict = manifest.__to_dict__()
# manifest_dict["model"] == model.__to_dict__()
Related Pages