Install & Compatibility
Where this runs
tested against v0.12.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.9MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
torch-model-archiver
✓ This library is primarily a command-line interface (CLI) tool and is not typically imported for direct Python usage. It's executed as a shell command.
The primary interface is the `torch-model-archiver` command-line tool. Python programs would typically execute it via `subprocess` if programmatic archiving is required, rather than importing specific classes or functions.
The primary use of `torch-model-archiver` is via its command-line interface to package model artifacts into a `.mar` file. This example demonstrates the typical command structure. A real execution requires actual model architecture (`.py`) and serialized model (`.pth`, TorchScript, etc.) files, and optionally a custom handler (`.py`) or use of a default one (e.g., `image_classifier`). The `-f` flag forces overwriting an existing archive.
# Assume you have a PyTorch model 'model.py' and a serialized state_dict 'model.pth'
# Also assume you have a handler 'handler.py' (or use a default one like 'image_classifier')
# Create a simple dummy model.py and handler.py for demonstration:
# model.py:
# import torch.nn as nn
# class MyModel(nn.Module):
# def __init__(self):
# super(MyModel, self).__init__()
# self.linear = nn.Linear(10, 1)
# def forward(self, x):
# return self.linear(x)
#
# handler.py (minimal):
# from ts.torch_handler.base_handler import BaseHandler
# class MyHandler(BaseHandler):
# def preprocess(self, data):
# # Implement your data preprocessing logic
# return data
# def postprocess(self, data):
# # Implement your data postprocessing logic
# return data
# Command to archive a model (example with a hypothetical densenet161 setup):
# Ensure 'densenet161_model.py', 'densenet161_state.pth', and 'index_to_name.json' exist
# For a real run, replace paths with actual files and ensure handler logic matches the model.
# Example from TorchServe docs (adjust paths if running locally without cloning the repo)
# This assumes a model file like 'densenet_161/model.py' and a state dict like 'densenet161-8d451a50.pth'
# and a default handler 'image_classifier'
#
# Make a dummy model_store directory
import os
os.makedirs('model_store', exist_ok=True)
# This example is illustrative. For a runnable quickstart, you'd need to provide actual model.py, .pth, and handler files.
# A fully runnable quickstart often involves downloading example assets from the TorchServe repo.
# This specific command uses a generic handler and placeholder files.
# In a real scenario, you'd replace 'my_model.py', 'my_model_state.pth', and 'my_handler.py' with your actual files.
# We are using 'image_classifier' as a built-in handler for demonstration purposes.
print("To create a model archive (.mar) file:")
print("torch-model-archiver --model-name mymodel --version 1.0 --model-file path/to/my_model.py --serialized-file path/to/my_model_state.pth --handler image_classifier --export-path model_store -f")
print("\nThis command will create 'model_store/mymodel.mar'")
# Example using subprocess (if you wanted to run it from Python)
import subprocess
# This path is relative to the torchserve repo; adjust if you cloned it elsewhere or use your own model files.
# For a truly isolated example, you'd need to create dummy files or download real ones.
model_name = "densenet161"
model_version = "1.0"
# Placeholder paths for demonstration
model_file_path = "./dummy_model.py"
serialized_file_path = "./dummy_state.pth"
export_path = "model_store"
handler_name = "image_classifier" # Using a default handler for simplicity
# Create dummy files if they don't exist for the subprocess command to not error immediately
with open(model_file_path, "w") as f:
f.write("import torch.nn as nn\nclass MyModel(nn.Module):\n def __init__(self):\n super().__init__()\n self.linear = nn.Linear(10, 1)\n def forward(self, x):\n return self.linear(x)")
# Create a dummy serialized file (e.g., an empty file or a minimal PyTorch save)
import torch
torch.save({'state_dict': {}}, serialized_file_path)
cmd = [
"torch-model-archiver",
"--model-name", model_name,
"--version", model_version,
"--model-file", model_file_path,
"--serialized-file", serialized_file_path,
"--handler", handler_name,
"--export-path", export_path,
"-f" # Force overwrite if file exists
]
try:
# Not actually running this in a quickstart as it requires external files, just showing the structure
# subprocess.run(cmd, check=True, capture_output=True)
# print(f"Successfully created {export_path}/{model_name}.mar")
pass # Suppress actual execution for quickstart to avoid requiring external files
except subprocess.CalledProcessError as e:
print(f"Error archiving model: {e.stderr.decode()}")
except FileNotFoundError:
print("Error: 'torch-model-archiver' command not found. Please ensure the library is installed and in your PATH.")
# Clean up dummy files
os.remove(model_file_path)
os.remove(serialized_file_path)
# os.rmdir(export_path) # Don't remove if you expect a .mar file for a real test
torch-model-archiver --version
Upgrade
Version history
0.12.0latest on PyPI · released Sep 30, 2024
Audit
Dependencies
torchrequiredUsed for working with PyTorch models that are to be archived.