Miscellaneous
Difference between .mar and .pt files and why we exclude the .mar file:
π¦ File Types Explained
.pt file (PyTorch Weights)
- What: Pure PyTorch model weights/parameters
- Contains: Just the trained neural network weights (state_dict)
- Used for: Loading model directly in Python/PyTorch code
- Loading:
torch.load("model_weight.pt")
.mar file (Model Archive)
- What: TorchServe deployment package (like a zip file)
- Contains:
- The
.ptweights file - Model architecture code (
model.py,module.py) - All config files (
.yaml,.json) - Handler code for serving
- Everything bundled together
- The
- Used for: Deploying model to TorchServe production environment
- Loading: Not directly loadable - needs TorchServe
π―Howβs .mar file created?
Sample training code job.py:
def torch_model_archiver(model_dir, export_path, args):
"""
Archive all model artifacts into single mar file
"""
extra_file = [
model_file_path,
module_file_path,
f"{model_dir}/model_config.yaml",
f"{model_dir}/....json",
...
]
rc = [
"torch-model-archiver",
f"--model-name={model_name}",
f"--serialized-file={model_dir}/model_weight.pt", # β .pt goes INSIDE .mar
f"--extra-files={','.join(extra_file)}",
"--handler=./handler.py",
f"--version={args.version}",
f"--export-path={export_path}",
"-f",
]The .mar file is created by bundling all the individual files together. So when we download from S3, we have two options:
β Option 1: Download .mar only
# Would need to extract it
!aws s3 cp s3://.../model.mar .
# Then extract/unpack the .mar file
# Extra steps, more complexβ Option 2: Download individual files (what we do)
!aws s3 cp {s3_model_path} {local_model_dir} --recursive --exclude="*.mar"
# Downloads:
# - model_weight.pt β What we need!
# - model_config.yaml
# - ....json
# etc.π Comparison
| Aspect | .pt file | .mar file |
|---|---|---|
| Purpose | Model weights for inference | Deployment package |
| Used in | Jupyter notebooks, Python scripts | TorchServe production |
| Contains | Only weights | Weights + code + configs |
| Size | Smaller | Larger (has duplicates) |
| Loading | torch.load() | TorchServe API |
| Need for testing? | β YES | β NO |
π‘ In a Jupyter Notebook
Weβre usually doing model testing/evaluation, not deployment, so we:
- β
Download
.ptfile β Load weights into model - β Download config files β Configure model architecture
- β Skip
.marfile β Would be redundant, just wastes bandwidth
The .mar file is used when you want to:
- Deploy the model to a production serving environment
- Use TorchServe to handle HTTP requests
- Package everything for model registry/deployment
Since weβre just running inference in a notebook, we only need the raw .pt weights and configs! This saves download time and disk space. π