Implementation:Pytorch Serve Marsgen
Overview
Pytorch_Serve_Marsgen is the MAR file generation script that automates downloading model artifacts, constructing torch-model-archiver commands, and producing Model Archive files for the TorchServe test and example ecosystem.
Source
| Property | Value |
|---|---|
| File | ts_scripts/marsgen.py
|
| Lines | 199 |
| Language | Python |
| Key Functions | delete_model_store_gen_dir, gen_mar, generate_model, generate_mars, model_archiver_command_builder
|
Key Functions
delete_model_store_gen_dir (lines 16–23)
def delete_model_store_gen_dir():
...
Removes the generated model store directory to ensure a clean slate before regenerating MAR files. This prevents stale artifacts from polluting new builds.
gen_mar(model_store) (lines 29–43)
def gen_mar(model_store):
...
Top-level entry point for MAR generation. Reads the mar_config.json configuration file, iterates over defined models, and delegates to generate_mars for batch processing.
generate_model(model, model_store_dir) (lines 46–101)
def generate_model(model, model_store_dir):
...
Handles the generation of a single model archive. This function:
- Downloads the serialized model file if a URL is specified
- Resolves handler, model file, and extra file paths
- Manages symlinks for shared artifacts
- Invokes
model_archiver_command_builderto assemble the archiver command - Executes the command via subprocess
generate_mars(mar_config, model_store_dir) (lines 104–129)
def generate_mars(mar_config, model_store_dir):
...
Batch generation driver. Iterates over all model entries in the parsed mar_config dictionary and calls generate_model for each one. Handles error aggregation and reporting.
model_archiver_command_builder(...) (lines 132–180)
def model_archiver_command_builder(
model_name, version, serialized_file, handler,
model_file, extra_files, export_path, ...
):
...
Constructs the full torch-model-archiver CLI command as a list of arguments. This function maps the model configuration fields to their corresponding CLI flags:
| Parameter | CLI Flag |
|---|---|
model_name |
--model-name
|
version |
--version
|
serialized_file |
--serialized-file
|
handler |
--handler
|
model_file |
--model-file
|
extra_files |
--extra-files
|
export_path |
--export-path
|
The --force flag is always appended to allow overwriting existing archives during generation.
Configuration: mar_config.json
The script reads from a mar_config.json file that declares each model's metadata, download URLs, handler paths, and archiver options. This JSON-driven approach decouples model definitions from the generation logic.
Relationship to Principles
This implementation directly supports the Pytorch_Serve_Model_Archiving principle. While ModelArchiverConfig defines what parameters an archive requires, marsgen.py automates the how -- orchestrating downloads, path resolution, and CLI invocation to produce MAR files at scale.
Metadata
Pytorch_Serve Pytorch_Serve_Model_Archiving 2026-02-13 18:52 GMT