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 .pt weights file
    • Model architecture code (model.py, module.py)
    • All config files (.yaml, .json)
    • Handler code for serving
    • Everything bundled together
  • 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
PurposeModel weights for inferenceDeployment package
Used inJupyter notebooks, Python scriptsTorchServe production
ContainsOnly weightsWeights + code + configs
SizeSmallerLarger (has duplicates)
Loadingtorch.load()TorchServe API
Need for testing?βœ… YES❌ NO

πŸ’‘ In a Jupyter Notebook

We’re usually doing model testing/evaluation, not deployment, so we:

  1. βœ… Download .pt file β†’ Load weights into model
  2. βœ… Download config files β†’ Configure model architecture
  3. ❌ Skip .mar file β†’ 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. πŸš€