Model packaging into .mar files helps you bundle your PyTorch model and its code so it can be easily shared and deployed.
Model packaging (.mar files) in PyTorch
torch-model-archiver --model-name <model_name> --version <version> --serialized-file <model_file> --handler <handler_file> --extra-files <extra_files> --export-path <export_path>
Replace placeholders like <model_name> with your actual model name.
The --handler is a Python script or a built-in handler that defines how to load and run your model.
resnet18.pt using the built-in image_classifier handler and saves the .mar file in model_store.torch-model-archiver --model-name resnet18 --version 1.0 --serialized-file resnet18.pt --handler image_classifier --export-path model_storetorch-model-archiver --model-name mymodel --version 2.0 --serialized-file model.pt --handler my_handler.py --extra-files index_to_name.json --export-path model_storeThis example saves a pretrained ResNet18 model and shows the command to package it into a .mar file for TorchServe deployment.
import torch from torchvision import models # Step 1: Save a pretrained model model = models.resnet18(pretrained=True) model.eval() # Save the model as TorchScript for TorchServe example_input = torch.randn(1, 3, 224, 224) traced_model = torch.jit.trace(model, example_input) traced_model.save('resnet18.pt') # Step 2: Use torch-model-archiver command (run in terminal, not Python) # torch-model-archiver --model-name resnet18 --version 1.0 --serialized-file resnet18.pt --handler image_classifier --export-path model_store # Step 3: After packaging, you get resnet18.mar in model_store folder print('Model saved as resnet18.pt') print('Run torch-model-archiver command in terminal to create .mar file')
The torch-model-archiver tool is a command-line utility, so you run it outside Python in your terminal.
Make sure to install TorchServe and torch-model-archiver via pip before packaging.
The .mar file contains your model and code, making deployment easier and consistent.
Packaging models into .mar files bundles model and code for easy deployment.
Use torch-model-archiver command with model file and handler script.
.mar files are used by TorchServe to serve models as APIs.
Practice
.mar file?Solution
Step 1: Understand what a .mar file contains
A .mar file bundles the trained model and the handler code needed to serve it.Step 2: Identify the use case of .mar files
These files are used by TorchServe to deploy models as APIs easily.Final Answer:
To bundle the model and code for easy deployment with TorchServe -> Option DQuick Check:
Model packaging = deployment bundle [OK]
- Confusing .mar with just compression
- Thinking .mar changes training format
- Assuming .mar is for visualization
.mar file using torch-model-archiver?Solution
Step 1: Identify correct model file type
The model file should be a PyTorch model file like.pth, not a Python script or text file.Step 2: Confirm handler file type
The handler must be a Python script.py, not a text file.Final Answer:
torch-model-archiver --model-name mymodel --model-file model.pth --handler handler.py --export-path model_store -> Option AQuick Check:
Model file = .pth, handler = .py [OK]
- Using .py file as model file
- Using .txt as handler file
- Mixing file extensions incorrectly
torch-model-archiver --model-name resnet --model-file resnet.pth --handler image_classifier.py --export-path model_store
What will be the output?
Solution
Step 1: Understand torch-model-archiver output
The command packages the model and handler into a single .mar file.Step 2: Check output location and file name
The .mar file is named after the model name and saved in the export path.Final Answer:
A file named resnet.mar created in model_store directory -> Option CQuick Check:
.mar file = model + handler bundle [OK]
- Expecting a Python script output
- Thinking .mar is a folder
- Assuming error due to .pth file
torch-model-archiver --model-name mymodel --model-file model.pth --handler handler.py --export-path model_store
But got an error:
Handler file not found. What is the likely cause?Solution
Step 1: Analyze the error message
The error says handler file not found, so the problem is with the handler.py file location or name.Step 2: Check other options
Model file corruption or export path issues would cause different errors, not handler missing.Final Answer:
The handler.py file is missing or path is incorrect -> Option AQuick Check:
Error message = handler file missing [OK]
- Blaming model file for handler error
- Ignoring file path correctness
- Assuming export path causes handler error
Solution
Step 1: Understand packaging multiple models
Each model requires its own .mar file with its specific handler and unique model name.Step 2: Confirm correct packaging method
Running torch-model-archiver twice with different parameters and exporting to the same folder is the correct way.Final Answer:
Run torch-model-archiver twice, each time with the respective model file, handler, and unique model name, exporting to the same model_store folder -> Option BQuick Check:
One .mar per model + handler [OK]
- Trying to combine models in one .mar file
- Copying model files without packaging
- Passing multiple files in one command
Ready to prove it?
Pass the test and this pattern moves unknown
