Skip to main content

Model Serving made Efficient in the Cloud.

Project description

MOSEC

PyPI version PyPi Downloads License Check status

Model Serving made Efficient in the Cloud.

Introduction

Mosec is a high-performance and flexible model serving framework for building ML model-enabled backend and microservices. It bridges the gap between any machine learning models you just trained and the efficient online service API.

  • Highly performant: web layer and task coordination built with Rust 🦀, which offers blazing speed in addition to efficient CPU utilization powered by async I/O
  • Ease of use: user interface purely in Python 🐍, by which users can serve their models in an ML framework-agnostic manner using the same code as they do for offline testing
  • Dynamic batching: aggregate requests from different users for batched inference and distribute results back
  • Pipelined stages: spawn multiple processes for pipelined stages to handle CPU/GPU/IO mixed workloads
  • Cloud friendly: designed to run in the cloud, with the model warmup, graceful shutdown, and Prometheus monitoring metrics, easily managed by Kubernetes or any container orchestration systems
  • Do one thing well: focus on the online serving part, users can pay attention to the model performance and business logic

Installation

Mosec requires Python 3.6 or above. Install the latest PyPI package with:

pip install -U mosec

Usage

Write the server

Import the libraries and set up a basic logger to better observe what happens.

import logging

from mosec import Server, Worker
from mosec.errors import ValidationError

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
    "%(asctime)s - %(process)d - %(levelname)s - %(filename)s:%(lineno)s - %(message)s"
)
sh = logging.StreamHandler()
sh.setFormatter(formatter)
logger.addHandler(sh)

Then, we build an API to calculate the exponential with base e for a given number. To achieve that, we simply inherit the Worker class and override the forward method. Note that the input req is by default a JSON-decoded object, e.g., a dictionary here (wishfully it receives data like {"x": 1}). We also enclose the input parsing part with a try...except... block to reject invalid input (e.g., no key named "x" or field "x" cannot be converted to float).

import math


class CalculateExp(Worker):
    def forward(self, req: dict) -> dict:
        try:
            x = float(req["x"])
        except KeyError:
            raise ValidationError("cannot find key 'x'")
        except ValueError:
            raise ValidationError("cannot convert 'x' value to float")
        y = math.exp(x)  # f(x) = e ^ x
        logger.debug(f"e ^ {x} = {y}")
        return {"y": y}

Finally, we append the worker to the server to construct a single-stage workflow, and we specify the number of processes we want it to run in parallel. Then we run the server.

if __name__ == "__main__":
    server = Server()
    server.append_worker(
        CalculateExp, num=2
    )  # we spawn two processes for parallel computing
    server.run()

Run the server

After merging the snippets above into a file named server.py, we can first have a look at the command line arguments:

python server.py --help

Then let's start the server...

python server.py

and in another terminal, test it:

curl -X POST http://127.0.0.1:8000/inference -d '{"x": 2}'

or check the metrics:

curl http://127.0.0.1:8000/metrics

That's it! You have just hosted your exponential-computing model as a server! 😉

Example

More ready-to-use examples can be found in the Example section. It includes:

  • Multi-stage workflow
  • Batch processing worker
  • PyTorch deep learning models:
    • sentiment analysis
    • image recognition

Qualitative Comparison*

Batcher Pipeline Parallel I/O Format(1) Framework(2) Backend Activity
TF Serving Limited(a) Heavily TF C++
Triton Limited Multiple C++
MMS Limited Heavily MX Java
BentoML Limited(b) Multiple Python
Streamer Customizable Agnostic Python
Flask(3) Customizable Agnostic Python
Mosec Customizable Agnostic Rust

*As accessed on 08 Oct 2021. By no means is this comparison showing that other frameworks are inferior, but rather it is used to illustrate the trade-off. The information is not guaranteed to be absolutely accurate. Please let us know if you find anything that may be incorrect.

(1): Data format of the service's request and response. "Limited" in the sense that the framework has pre-defined requirements on the format. (2): Supported machine learning frameworks. "Heavily" means the serving framework is designed towards a specific ML framework. Thus it is hard, if not impossible, to adapt to others. "Multiple" means the serving framework provides adaptation to several existing ML frameworks. "Agnostic" means the serving framework does not necessarily care about the ML framework. Hence it supports all ML frameworks (in Python). (3): Flask is a representative of general purpose web frameworks to host ML models.

Contributing

We welcome any kind of contribution. Please give us feedback by raising issues or directly contribute your code and pull request!

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

mosec-0.2.1.tar.gz (20.9 kB view details)

Uploaded Source

Built Distributions

mosec-0.2.1-cp39-cp39-manylinux1_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.9

mosec-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

mosec-0.2.1-cp38-cp38-manylinux1_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.8

mosec-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

mosec-0.2.1-cp37-cp37m-manylinux1_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.7m

mosec-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

mosec-0.2.1-cp36-cp36m-manylinux1_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.6m

mosec-0.2.1-cp36-cp36m-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.6m macOS 10.9+ x86-64

File details

Details for the file mosec-0.2.1.tar.gz.

File metadata

  • Download URL: mosec-0.2.1.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for mosec-0.2.1.tar.gz
Algorithm Hash digest
SHA256 fd6b24f531917ad607b855e77bfaf9d33c74215f1954f8554699122700703d48
MD5 1aad71b815db959903c0ab79fff81b15
BLAKE2b-256 dd7389ad568bc177fde9658406779bbd0aac5f67e0518329d8e6d93305d804ab

See more details on using hashes here.

File details

Details for the file mosec-0.2.1-cp39-cp39-manylinux1_x86_64.whl.

File metadata

  • Download URL: mosec-0.2.1-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.7 MB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for mosec-0.2.1-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 7b2476e99510e1b3ac99c0bbff1069fbd178008a5aa84d6aaa6c8658337c18f9
MD5 aedbae6f514d821338f5286eb614d89c
BLAKE2b-256 0d895bf97ca1bf3697cbbe413cd26e3c650ae1b5670954b9e16f049106f28eb5

See more details on using hashes here.

File details

Details for the file mosec-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: mosec-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for mosec-0.2.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6e7e3442dbbb1bb53718878561a4d13f50627521a5cd906351a73c23d51e43b7
MD5 0cdc6952901f3f2a2ce2d2c1ba710958
BLAKE2b-256 ddf526662216837eaf0b2dc289e02524be9a7fe0d35accdd50879267e7f05415

See more details on using hashes here.

File details

Details for the file mosec-0.2.1-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: mosec-0.2.1-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.7 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for mosec-0.2.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 814350c73712c2ae7b48500db0dfd3adfd14c30a305c314249f535afab8f6146
MD5 d03fefd9fad622be7cb9be66f06994c0
BLAKE2b-256 5c7fa040e80212984aaab4879087e17882a74bf55c3f08b4715a9fdad723dd17

See more details on using hashes here.

File details

Details for the file mosec-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: mosec-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for mosec-0.2.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 fcdd926c741e945e6e2482e515bdbafd6417db8875c2aba68393b5273ddba3d0
MD5 2644e75e3e2d1fe240cada629c4d5176
BLAKE2b-256 aab11f9c3af7481b7a9833f3424cb3dd9b6bbf8c1271828850372dafca9d26fa

See more details on using hashes here.

File details

Details for the file mosec-0.2.1-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: mosec-0.2.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.7 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for mosec-0.2.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5e615cd97707df4c4189366099002eb06b6b9641f6abdac9827e6bb155a1e43f
MD5 5f562594a37eaeb826592713bbe0d893
BLAKE2b-256 61172cd7a5193a2dfc07e1fa89fa46ce3b2ae63c9b2a220f113b2ac9415252e6

See more details on using hashes here.

File details

Details for the file mosec-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: mosec-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.7m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for mosec-0.2.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f0eb6487c6e6a3f131d9add4ec84abe314e4a12368d6c29c9ee7dc2858a0c57f
MD5 ed4cbaf8d85fbeefcbd96aa862e4e8c2
BLAKE2b-256 bfff6edccd0b30de8baa43b6be6a33be8035c988054f4eb3849018e9f53801f8

See more details on using hashes here.

File details

Details for the file mosec-0.2.1-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

  • Download URL: mosec-0.2.1-cp36-cp36m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.7 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for mosec-0.2.1-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 bbe0f87c71c9f14ad402d9769feb8abc49b8454ffb67dff5b2916edd7abfd500
MD5 3bc313a4a3266f0f8b5936e3dc7de1a1
BLAKE2b-256 237a63210f8d9485c54091af7dc3726995038dc416940f07b3e17855f427d81a

See more details on using hashes here.

File details

Details for the file mosec-0.2.1-cp36-cp36m-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: mosec-0.2.1-cp36-cp36m-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.6m, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for mosec-0.2.1-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e8172d1453e3df7f5880d57a14be36710ce1a0637ac8d24bcd1a98fd778bedc7
MD5 53e20d98d2f89d4e5ce5106a5144200b
BLAKE2b-256 fb878a8bcb17c7b8e013523b32d8e5859166fe680ad940de5c58c85ff437d94c

See more details on using hashes here.

Supported by

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