Skip to main content

Model Serving made Efficient in the Cloud

Project description

MOSEC

discord invitation link 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.7 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}'
{
  "y": 7.38905609893065
}

> curl -X POST http://127.0.0.1:8000/inference -d '{"input": 2}' # wrong schema
validation error: cannot find key 'x'

or check the metrics:

> curl http://127.0.0.1:8000/metrics

For more debug logs, you can enable it by changing the Python & Rust log level:

logger.setLevel(logging.DEBUG)
> RUST_LOG=debug python server.py

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
  • Shared memory IPC
  • Customized GPU allocation
  • 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 discussing on Discord. You could also 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.3.6.tar.gz (45.9 kB view details)

Uploaded Source

Built Distributions

mosec-0.3.6-cp310-cp310-manylinux1_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.10

mosec-0.3.6-cp310-cp310-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

mosec-0.3.6-cp39-cp39-manylinux1_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.9

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

Uploaded CPython 3.9 macOS 10.9+ x86-64

mosec-0.3.6-cp38-cp38-manylinux1_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.8

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

Uploaded CPython 3.8 macOS 10.9+ x86-64

mosec-0.3.6-cp37-cp37m-manylinux1_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.7m

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

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: mosec-0.3.6.tar.gz
  • Upload date:
  • Size: 45.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.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.3.6.tar.gz
Algorithm Hash digest
SHA256 e91e739185d5062e27d7d9f06b84f6b43b491a6597926834e8fdb24a146aea49
MD5 89dae4cfbb7650f17a1cf4d011f811ee
BLAKE2b-256 b5ff8f6d721028d7b5798f966b076342e8cc82a1ba1d5eef93672a8b0ad85ce0

See more details on using hashes here.

File details

Details for the file mosec-0.3.6-cp310-cp310-manylinux1_x86_64.whl.

File metadata

  • Download URL: mosec-0.3.6-cp310-cp310-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.9 MB
  • Tags: CPython 3.10
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.3.6-cp310-cp310-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 771a61ecadb3ec74efca195659471853c13119b1a65f6f5d65c6a350a6202ad3
MD5 8070aec2368cc3269c24224863f07c00
BLAKE2b-256 4b03d4e438a72756bc770d214b375f250c927bc98cd12df52a2b178d8b17694b

See more details on using hashes here.

File details

Details for the file mosec-0.3.6-cp310-cp310-macosx_10_9_x86_64.whl.

File metadata

  • Download URL: mosec-0.3.6-cp310-cp310-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.10, 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.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.3.6-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d6c5a589ba80316fd7aee1c51a83518d3bcfad83aeae8c4145c4491425ea70ba
MD5 507c8af6d393fcec468d38798b5159b2
BLAKE2b-256 81939b8d13068d9807d2c0334c83a8aa794063cd94c219bb3693d01039f02593

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mosec-0.3.6-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.9 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.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.3.6-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 db053369a56a2f43f6628b74f8d724efc8497bdd494db332abd221ca844e9fe2
MD5 435c9460d9eed6fdff77d1449e6f5e29
BLAKE2b-256 aab1017aa87372b454e40ace18af3692560ae3b0324e0b35ebd4d310fc9f39c4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mosec-0.3.6-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.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.3.6-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 38535b8686130c41b58e55b35ed8e2d18e771e7ada30cb9dc31cecbfd063ab94
MD5 916a3b425bdfa6edea528f601561e3a6
BLAKE2b-256 db82c40547d1f69188682a5ec75e22b330c3d3aa19536d54ad226f1bd90ac8a0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mosec-0.3.6-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.9 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.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.3.6-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 e63ba5326cc58e7bbccf120f0aaa7df361fb934fb7389da88a766a7fb358e65d
MD5 0df86494ed449e12b76aa84db06183e6
BLAKE2b-256 e06178e49699af52ff385b3394cd3239f3639bf437d69ecf74647e4f86972026

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mosec-0.3.6-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.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.3.6-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bb7dd2692f5d66fc13f9d8662eb74922b0ae29d3a1392fe4b499a69b87c5b7c5
MD5 c5b2f3bf1a971c91e6a3ded01a00e8e5
BLAKE2b-256 7aa395c7f33f812e055ffa195f4f0235897ed30e78095234b5f7d55eda8b102f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mosec-0.3.6-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.9 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.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.3.6-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 1a670e6ac5612baf2da173798f5bd24083785a38197e8209fe146f7177fd6b78
MD5 69a96c2a24ef8e8a76b66a9b5294c004
BLAKE2b-256 795a578d5fb24f8baa149a312fdf87fca5d1a7addc54133d10517276454fba42

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mosec-0.3.6-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.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.3.6-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7c3907a0af92a5586b9d8d7aaee9bb797666b3869da77c3f64e291274751a186
MD5 92580227fa944d96bacdd32ad90edda2
BLAKE2b-256 97e82e5b3fa6e955a6204372979d4e59bfab649ae41cbf1a384f4517603c553f

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