Skip to main content

Model Serving made Efficient in the Cloud

Project description

MOSEC

discord invitation link PyPI version Python 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

This version

0.4.4

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.4.4.tar.gz (50.4 kB view details)

Uploaded Source

Built Distributions

mosec-0.4.4-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

mosec-0.4.4-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.4.4-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

mosec-0.4.4-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.4.4-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

mosec-0.4.4-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.4.4-cp311-cp311-musllinux_1_1_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

mosec-0.4.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

mosec-0.4.4-cp311-cp311-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

mosec-0.4.4-cp310-cp310-musllinux_1_1_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

mosec-0.4.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

mosec-0.4.4-cp310-cp310-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

mosec-0.4.4-cp39-cp39-musllinux_1_1_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

mosec-0.4.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

mosec-0.4.4-cp39-cp39-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

mosec-0.4.4-cp38-cp38-musllinux_1_1_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

mosec-0.4.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

mosec-0.4.4-cp38-cp38-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

mosec-0.4.4-cp37-cp37m-musllinux_1_1_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

mosec-0.4.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

mosec-0.4.4-cp37-cp37m-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: mosec-0.4.4.tar.gz
  • Upload date:
  • Size: 50.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.16

File hashes

Hashes for mosec-0.4.4.tar.gz
Algorithm Hash digest
SHA256 35dc9cf70636ffe0e28ce4b8a4a7ae65a9b0daf690ed21bc166f34f7d17e0bd2
MD5 c1bbf720d09e91fa71e21aa57cd623cb
BLAKE2b-256 57017de162543283e7402448b0c00d3ec097827cbbef464a56743e19725716df

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1744115eb0a8e15cdf3a45fb3e51ae4cd1bbb0d9afbc266adde602a3a25f3631
MD5 47ea71f580ddd0754e3e79c0d955f2f5
BLAKE2b-256 1c75200f8ff1635013d0b8c1eb2ba01484285954afc0e0b3c062cdfc980f0847

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-pp39-pypy39_pp73-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 17ecfd75f7c6a628d6f415ed21c40d1b8b65d67de0d1188339074bc89f0a0235
MD5 2e228073d45406c24d6affcdead24e8d
BLAKE2b-256 fe02f050af0c9e84e83cafc646d537e9d9ba4b5a006a063ae847f968f3e42739

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 232afaf375a5a291ce2e33f6aaa932b58a97ad2e53e9038e8c9526ce6402cd00
MD5 2e372efc79099c96179e46c8c29cb260
BLAKE2b-256 e49a745bf085f6c33e17c51e0cbd823a2cc6a33caa996a0e75b6dc65948f0b04

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-pp38-pypy38_pp73-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bb6dfec44280d6ddbfe1ace6e312f4fd982d9ee387ec9d0c461c36c7cd3bb8b1
MD5 cacfc7adcee804f3a478fc19bd8fb992
BLAKE2b-256 ea72cad515847d037bfcec8ed7dd71883fc6d36a6127c47a8d653c219a030522

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 593296fdbb8710ddb2e2ad2f731ac74c6945c93df1825b2ae610d1e088d4fe29
MD5 3a7126aadcc28d169e601af7a1794938
BLAKE2b-256 2255f35d64c81f3484648dd08c7d3288da72cf6481b8645aafae8be9a313b5af

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-pp37-pypy37_pp73-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f33a3d2473da4f9d9e63e3b494c5854ca855ec74762c6ed0190aad20a03f06a2
MD5 477ab0fb529258cd5c4911c5d8e01d4a
BLAKE2b-256 7e3272f037349dc9e76fff5482cd5e7887932d64189a2db2ebdc09415b398c71

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-cp311-cp311-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 f00129a60d084799f6315b37a4bbbac63f9b444c13c7cbe86aea08a1c2cfffeb
MD5 7c41b00d8968c89c1df249bde7a1f512
BLAKE2b-256 51b4f2728c3d2203ea9ce602d4075c8d603d4439f88347794f469eba0bec0bf5

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7e06ab4f4ed2b57f2532f6604f689486cbcf88a794e854985383b859acaa587d
MD5 d3ee3fa7510cf00c32b1d17831bb4fa7
BLAKE2b-256 920c6bd03b97ffb7b2374219a29c29998132b09fb4ea7fa49bb428f9f4480444

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-cp311-cp311-macosx_10_9_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a9a5b74137ade168a702223c54516c41aefb43a8f322b0096b5fc7f9ea20a45f
MD5 d04ae9ac1d6c45adbd5220439c751134
BLAKE2b-256 602c8a48656b4b8fbae4d760438e3ec9bab5a40bc3a9dc4fb0c2178fea55e789

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-cp310-cp310-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 c5afc8d9dd3ec8f6f1140fdf9e66e6f9612b5a0618ef16b3f30f2d6735bca575
MD5 1e72807bab1c319851f2c44ce3d4b605
BLAKE2b-256 56d6f1bc5fe6a822a513284d6a0346f7b1a2a9b5267a3f207135c9e48136d2f5

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 31d72887e30f3718eeefe548ceb62c90e8e6b1e647777df2a760cded13ecc636
MD5 3b038cc39ca54a1c05ae0a3c0505bd06
BLAKE2b-256 34771aa15e4b29f2d6c825d267320e2c18d2ce3bfc43558c50d56c25280e9113

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.4-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f988c4f5e7f6341e79de32490d1161b40bdc4d98297ad0cb73617b7f8cabda68
MD5 4c6f26a5f9bf5cc67103a3c9073d5d61
BLAKE2b-256 46393b5c2a4e4f1838a3aad574686f34149f85f182fb6bcbf1a93f569879f65a

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-cp39-cp39-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 e3db857e2a2a70aa567e9bf5b4ad438cdc1c060167704ec15c3a9c39e86f7c60
MD5 d6a087a023a6163b5d22035d1a877bb0
BLAKE2b-256 502f7099d7465af8a1c29c3c962f680a468b209b5d06b1777715dc6085d19e82

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 eecbbc88f53d994ca3ee6d677c350e443d1305239f007410485f1d89f015ec6b
MD5 c5af11c56e712be937060d3decc9be21
BLAKE2b-256 913270b2c676f8e7ac758889b7fa2b09ed4739a485a1f8bc64abd77deba8e5f3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.4-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 50e98fa9018fd83a972e099444ff7f86cc1ec2119281c88369268d858d2248b3
MD5 4adfce53ab5849ac859018fcff72db97
BLAKE2b-256 bc14044c677f8916e97a548173809ddeb1d04f96dfa2956e19b2f4a208dbf210

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-cp38-cp38-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 a1aeeb74ea31bb167dffd3d1617469d6de2584a462e7ed541907c698c3aa3857
MD5 100271d39b7c66d9302724e2ecb97103
BLAKE2b-256 352403b6bb992b1102a05155f2d0d9e67244b79e2cf114e31e7017f984ee4b4f

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 be9875bbb8452bc43123ea97c74c6675876f034468c971b2651d728eaa2ef635
MD5 601d9982a1e3eed493f3c0f63f322736
BLAKE2b-256 43d810985575b55d8d136fa8ccfa03718660be46ba140257644f893456c7175c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.4-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c4da3bedf2a56490913a5b654a36d58324b45f1508ee1b31a73d812c8f90f35f
MD5 3c3cf67270265b482922ef93b98e2275
BLAKE2b-256 10a858448a81b2b529d1944adbb95d141f7c7a9ee0b59f408be5a2017acacf0d

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-cp37-cp37m-musllinux_1_1_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 f521e1a1fb7cb05b28445d73750b0cb9814b2017675eaf44b443562eb43cc79c
MD5 c82d3eda1fd69dc18f58fdc1028f170e
BLAKE2b-256 3ccefadfbc50edb68a922969add2fd5bd6466aabd68cdade64cbaee92d4cddbe

See more details on using hashes here.

File details

Details for the file mosec-0.4.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for mosec-0.4.4-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 11d8429414e028573096e5d83e9362c265f2013b02bcd0a3ec7646e6ed3f317a
MD5 6f079fe584bd4e848ed3e54892e63acc
BLAKE2b-256 7eabdf313034fd79a5ab61d22543baccc60bd94db115851d45041de746a9e2e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.4-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 784172824acac7f9fdc3b67cbea382aae9749502ecb96e3910508425f3aaa9fc
MD5 aab6730a24565bd6bffea0119f969d90
BLAKE2b-256 c494b36e592361fc00ccbd47cb023f036b8d3f0cd2178976a87be4dc6104dbcf

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