Skip to main content

Model deployment, portability, and architecture-independent compilation

Project description

ModelPort 🚀

PyPI version Python Version License GitHub issues GitHub forks GitHub stars

ModelPort addresses the challenges of deploying machine learning models across diverse hardware and software environments. It provides a streamlined workflow to convert models into standard formats (like ONNX) and optionally compile them into high-performance, dependency-free executables suitable for servers, edge devices, and various operating systems.

Goal: Simplify ML model deployment, making it portable and architecture-independent.

Key Features

  • 🔥 Framework Agnostic Export: Automatically detect and export models from popular frameworks (e.g., PyTorch) to ONNX format.
  • 🚀 Native Compilation (Optional, via TVM): Compile ONNX models into optimized, platform-specific shared libraries using Apache TVM integration. This enables:
    • Zero-Dependency Execution: Run compiled models without requiring Python or the original ML framework installed.
    • Cross-Platform Deployment: Target diverse architectures like x86_64, ARM64 (including Apple Silicon), AArch64, and more.
    • Hardware Acceleration: Leverage GPU acceleration via CUDA, Metal, or OpenCL for compiled models.
  • ⚙️ Simplified Inference: Run inference using either the standard ONNX Runtime or the compiled native libraries through a consistent Python API or command-line interface.
  • C++ Integration: Load and execute compiled model artifacts directly from C++ applications.
  • 📊 Benchmarking Tools: Facilitates performance analysis of exported and compiled models.

📦 Installation

Prerequisites:

  • Python >= 3.8

Standard Installation:

pip install model-port

Optional Dependencies:

  • GPU Support (ONNX Runtime): If using ONNX Runtime for inference and require GPU acceleration:
    pip install model-port[gpu]
    
  • Development: To install for development, including testing tools:
    git clone https://github.com/SaiKrishna-KK/model-port.git
    cd model-port
    pip install -e ".[dev]"
    

Installing TVM Support (Required for Native Compilation)

The native compilation features of model-port (modelport compile, mp.compile, etc.) rely on Apache TVM. Due to the complexities of TVM installation and the lack of universal pre-built packages on PyPI, TVM is NOT installed automatically.

To enable native compilation:

  1. Install model-port first (as shown above).
  2. Install Apache TVM separately into the same Python environment by following the official TVM installation instructions:

Important: Always refer to the official TVM documentation for the most accurate and up-to-date installation method for your specific OS, Python version, and hardware requirements.

If you encounter installation issues, please refer to the Troubleshooting section below.

🚀 Quick Start

Get started quickly with ModelPort using either the CLI or the Python API.

Command Line Interface (CLI)

# Export a model (e.g., PyTorch) to ONNX - framework is auto-detected
modelport export /path/to/your/model.pt --output-path my_model.onnx

# (Optional) Compile the ONNX model to a native shared library
# Assumes TVM is installed separately - see Installation section
modelport compile my_model.onnx --target-arch x86_64 --output-dir compiled_model

# Run inference using the compiled model artifact
# Assumes input data is a .npy file matching model input name 'input'
modelport run compiled_model --input /path/to/input_data.npy

# Run inference using the original ONNX file (via ONNX Runtime)
modelport run my_model.onnx --input /path/to/input_data.npy

Python API

import torch
import numpy as np
import modelport as mp

# --- 1. Prepare or Load Your Model ---
# Example: Simple PyTorch model
model = torch.nn.Sequential(
    torch.nn.Linear(10, 5),
    torch.nn.ReLU(),
    torch.nn.Linear(5, 2)
)
model.eval()
dummy_input = torch.randn(1, 10) # Example input for tracing/shape inference

# --- 2. Export to ONNX ---
onnx_path = "my_model.onnx"
mp.export.to_onnx(
    model=model,
    input_data=dummy_input, # Provide sample input data
    output_path=onnx_path
)
print(f"Model exported to: {onnx_path}")

# --- 3. (Optional) Compile to Native Artifact ---
# Ensure TVM is installed separately first!
compiled_model_dir = "compiled_model"
try:
    mp.compile.compile_model(
        model_path=onnx_path,
        target_arch="x86_64",  # e.g., "x86_64", "aarch64", "arm64" (for Apple Silicon)
        target_device="cpu",   # e.g., "cpu", "cuda", "metal"
        output_dir=compiled_model_dir
    )
    print(f"Model compiled to: {compiled_model_dir}")
    use_compiled = True
except ImportError:
    print("TVM not found. Skipping native compilation.")
    print("Install TVM separately to enable this feature (see README).")
    use_compiled = False
except Exception as e:
    print(f"TVM compilation failed: {e}")
    use_compiled = False


# --- 4. Run Inference ---
input_name = "input0" # Default or check your model's input name
input_data_np = dummy_input.numpy().astype(np.float32)
inference_input = {input_name: input_data_np}

model_to_run = compiled_model_dir if use_compiled else onnx_path

outputs = mp.inference.run(model_to_run, inference_input)

# Process outputs (structure depends on your model)
output_data = outputs[0] # Assuming first output tensor
print(f"Inference executed using: {model_to_run}")
print(f"Output shape: {output_data.shape}")
print(f"Output sample: {output_data[0, :5]}") # Print first 5 elements

🔧 Supported Target Platforms (for Native Compilation via TVM)

ModelPort, via TVM, aims to compile models for a wide range of targets:

  • CPUs: x86_64 (Intel/AMD), arm64 (Apple Silicon), aarch64 (Generic ARM Linux)
  • GPUs: NVIDIA (CUDA), AMD (ROCm - Linux only), Apple (Metal), Vulkan, OpenCL

Note: Successful compilation depends on having the correct TVM version and build configuration installed, along with necessary drivers/SDKs (like CUDA toolkit for NVIDIA GPUs).

📚 Documentation & Resources

🛠️ Troubleshooting

If you encounter problems during installation or usage:

  1. Check Prerequisites: Ensure you have Python >= 3.8 installed.
  2. TVM Issues: If facing issues with native compilation (modelport compile):
    • Verify that you have installed Apache TVM separately after installing model-port.
    • Confirm your installed TVM version meets any requirements mentioned for specific features.
    • Consult the official Apache TVM documentation and installation guides for your platform.
  3. Search Issues: Check the GitHub Issues page for similar problems.
  4. Report New Issues: If your issue is new, please open a new issue. Include:
    • Your operating system and version.
    • Your Python version.
    • model-port version (pip show model-port).
    • TVM version (if relevant: python -c "import tvm; print(tvm.__version__)").
    • The exact command or code snippet causing the issue.
    • The full error message and traceback.

🤝 Contributing

Contributions are welcome! Whether it's reporting bugs, suggesting features, improving documentation, or submitting code changes, your help is appreciated.

Please check the CONTRIBUTING.md file for guidelines (if it exists). If not, feel free to open an issue to discuss your idea or submit a pull request.

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

🙏 Acknowledgements

  • The PyTorch, ONNX, and Apache TVM communities for their foundational work.
  • All contributors and users of ModelPort.

Made with ❤️ by SaiKrishna-KK

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

model_port-2.0.6.tar.gz (59.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

model_port-2.0.6-py3-none-any.whl (50.0 kB view details)

Uploaded Python 3

File details

Details for the file model_port-2.0.6.tar.gz.

File metadata

  • Download URL: model_port-2.0.6.tar.gz
  • Upload date:
  • Size: 59.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for model_port-2.0.6.tar.gz
Algorithm Hash digest
SHA256 971b16fc3bf628dc6357719ca0ead86a477ae0539c0177cce87c9ab3c8fbca64
MD5 29bf7e4e5b2e3f80568bb70438e95be7
BLAKE2b-256 d9445e3b9f6c0a43d7a69de5fd641e078f3e375f4ef984a19ec663d0a1d0e5a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for model_port-2.0.6.tar.gz:

Publisher: publish.yml on SaiKrishna-KK/model-port

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file model_port-2.0.6-py3-none-any.whl.

File metadata

  • Download URL: model_port-2.0.6-py3-none-any.whl
  • Upload date:
  • Size: 50.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for model_port-2.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 31f4bbaadbb56b97fe85976654e83846a81a0dede0c5bf683f7b6831da8251fe
MD5 a35318a962c029ebbcf8da5fb461a871
BLAKE2b-256 88d37c1e35855a6dbe940584a91711368f979ac7af4dca7646c12f371d06583a

See more details on using hashes here.

Provenance

The following attestation bundles were made for model_port-2.0.6-py3-none-any.whl:

Publisher: publish.yml on SaiKrishna-KK/model-port

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page