Implementation:Pytorch Serve ModelArchiverConfig
Overview
Pytorch_Serve_ModelArchiverConfig is the configuration dataclass that defines all parameters required for creating a Model Archive (MAR) file. It centralizes the archiver's configuration into a single, typed structure using Python's @dataclass decorator.
Source
| Property | Value |
|---|---|
| File | model-archiver/model_archiver/model_archiver_config.py
|
| Lines | 28 |
| Language | Python |
| Key Class | ModelArchiverConfig (lines 9–28)
|
Class Definition
The ModelArchiverConfig class is decorated with @dataclass and contains all fields necessary to drive the model archiving process.
Fields
| Field | Type | Default | Description |
|---|---|---|---|
model_name |
str |
required | Name of the model to archive |
handler |
str |
required | Handler file or built-in handler name |
version |
str |
required | Model version string |
serialized_file |
Optional[str] |
None |
Path to the serialized model weights file (e.g., .pt, .pth)
|
model_file |
Optional[str] |
None |
Path to the model architecture definition file |
extra_files |
Optional[str] |
None |
Comma-separated list of additional files to include in the archive |
runtime |
str |
"PYTHON" |
Runtime environment for the model |
export_path |
str |
current working directory | Directory where the archive will be written |
archive_format |
Literal["default", "tgz", "no-archive"] |
N/A | Archive output format |
force |
bool |
N/A | Whether to overwrite an existing archive |
requirements_file |
Optional[str] |
None |
Path to a pip requirements file to bundle |
config_file |
Optional[str] |
None |
Path to a model-specific config file |
Class Method: from_args
@classmethod
def from_args(cls, args: Namespace) -> "ModelArchiverConfig":
...
The from_args class method constructs a ModelArchiverConfig instance from an argparse.Namespace object. This bridges the CLI argument parsing layer with the typed configuration layer, allowing the archiver to be invoked both programmatically and from the command line.
Usage Pattern
from model_archiver.model_archiver_config import ModelArchiverConfig
# From parsed CLI arguments
config = ModelArchiverConfig.from_args(parsed_args)
# Or directly via dataclass construction
config = ModelArchiverConfig(
model_name="resnet-18",
handler="image_classifier",
version="1.0",
serialized_file="resnet18.pt",
archive_format="default",
force=True,
)
Relationship to Principles
This implementation directly supports the Pytorch_Serve_Model_Archiving principle. The ModelArchiverConfig dataclass encapsulates every parameter the archiver needs, ensuring that the packaging of models into MAR files is driven by a well-defined, validated configuration object.
Metadata
Pytorch_Serve Pytorch_Serve_Model_Archiving 2026-02-13 18:52 GMT