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

This version

0.4.1

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

Uploaded Source

Built Distributions

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

mosec-0.4.1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

mosec-0.4.1-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded PyPymacOS 10.9+ x86-64

mosec-0.4.1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

mosec-0.4.1-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded PyPymacOS 10.9+ x86-64

mosec-0.4.1-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

mosec-0.4.1-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded PyPymacOS 10.9+ x86-64

mosec-0.4.1-cp311-cp311-musllinux_1_1_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.11musllinux: musl 1.1+ x86-64

mosec-0.4.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

mosec-0.4.1-cp311-cp311-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.11macOS 10.9+ x86-64

mosec-0.4.1-cp310-cp310-musllinux_1_1_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.10musllinux: musl 1.1+ x86-64

mosec-0.4.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.10macOS 10.9+ x86-64

mosec-0.4.1-cp39-cp39-musllinux_1_1_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.9musllinux: musl 1.1+ x86-64

mosec-0.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.9macOS 10.9+ x86-64

mosec-0.4.1-cp38-cp38-musllinux_1_1_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.8musllinux: musl 1.1+ x86-64

mosec-0.4.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.8macOS 10.9+ x86-64

mosec-0.4.1-cp37-cp37m-musllinux_1_1_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.7mmusllinux: musl 1.1+ x86-64

mosec-0.4.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.7mmanylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.7mmacOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: mosec-0.4.1.tar.gz
  • Upload date:
  • Size: 49.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for mosec-0.4.1.tar.gz
Algorithm Hash digest
SHA256 f9ba4246e81e22d599187cab0e9c108ccb97030ddeb1a3f1cd021807f8a68ea2
MD5 a0629119ba2ba08630669438bce5ac84
BLAKE2b-256 eee1fa246b02db0a889ffe01dbcd01b94bbb2be68260b977b9623f611b4f1fdb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 bbac0f035670a0e1f891c46feaa81fc870adc929f1a0c23647a776d1924eac95
MD5 388b915a83c6f94a1163905bec055bb2
BLAKE2b-256 cc0bf4fb05e2b5d47753d4a558b1b6e74b572f345cec37a3bfc67e1e666760cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 21ab917dfece5c43ed5d492134b28c46aa623142edf3d40d6e01c06af8c35edb
MD5 2dacb4d5151d1bdfb47d53f7b05d55fd
BLAKE2b-256 ddc297bc6ee2d9c2c720bb0d3b3e4f0e6466939b87f757e061b6b32b54bfa399

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f19fa6f0ef28e8486aae6c37aea0569753d11e1df62bbbed822f99308c0ea27c
MD5 fc138923071a2ecf2a3c89d9c2d4606a
BLAKE2b-256 5d6b00b6f0f53ad2c2b66d451b9cd0d10291b70e940fb52dd03cced2f8d74faa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 579fea96f3862e86837bcbe4e900ae83c1cd5d9f03bb6e8837a8d3b642784f81
MD5 e70c00d77714de2fc67bf21c6552ffa9
BLAKE2b-256 072ef5ceeabc01195ab6fcaa9a22140f064197a8607fda116bcbf69da6403606

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f7404eb4daa07b12f6e4b92d9ea1ee0dc73b384d438826ae4c11902f80aa2e45
MD5 5056b9d78b4f443c6dd75dbad65e52cb
BLAKE2b-256 acca30cb186baf570c341889e3af10c573e2127b8a0544f3fe5cb93ceabe4b55

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 d236571e4a99d7fb4d9a1dbe8b6c7e88ec5d379104b378ca5ea016b55be5753b
MD5 ef1b418a5f76f294840456e3f17c8f8e
BLAKE2b-256 19a61e86daa69644d66e8c271ba2493884cde21d3fbe2ffb628606a6726e7630

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 6ff9511f1f4046a7c011b0185f0b88ff75397e1ea8c60e98c3c7261e47432084
MD5 9941fa64e44af3d586a9196404413e7a
BLAKE2b-256 b7a271f04a2a589b6292dc371632b29983e8316d8f51ca1c1f8d7d4caa8d0830

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c10865242abd85dc612ab250e10e1035654749b437dc5df513952ead0e67283a
MD5 a500c9e7643af80563d1b73d4f3f489b
BLAKE2b-256 f41802c062605b28de24cd9c3f395e11ef8dbc55a2f7f9059c6212fab5ab4f41

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c3c3a0aed22ef7686354d56049449b6134d09b487e7ebc1248dc867661b5b99d
MD5 bb0da1b7cba47f39f54e7f767e997cd6
BLAKE2b-256 faff61156f7a71d507b1249277adb9bc412643da0919c88847268fe6dd22b670

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 ff6c544991a692be66efb19c984fb8a858f9d1b1acf8f9b037a3b9f492e00507
MD5 c5d96bfb6c532a3e67cff641af28278b
BLAKE2b-256 6db9312c9dac46b348ebe558553e32e4b979a55da5cc505e638f869dc839892b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5be275daa9fdbae439f6a43c52cf8b49fd322a4a998db404d2f7580b1fb37025
MD5 53a3fe71be8144c8533d14aa5fba849d
BLAKE2b-256 602aec31b4eed9411ecdf3b485096db4d0f652e66de837f51b4810562b36e3cb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7311e8497dc8f49b629704e67867816dba28f8c60ee278f4f12077d8795d4124
MD5 4d2ccee2ac7b24411b7b3d425fa82895
BLAKE2b-256 e6f6167b1916b19f7ef6d12857b94d6c8011745f6ff2cf4a01eff8d2d44723af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 b4f86639139da599bfe74f28678587c05c39527cd55b6c8d9a876da0f4e143df
MD5 2cf6b02877d752c9becf554ff452f7b2
BLAKE2b-256 efdfb8716341ad7c9ac22aadcf9efec1b16c1560ba3003c78a7ed00434963e19

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d8070c2d5e3d2a0d2eaf2984bad3c4e2ceca63ee5a93ed7a6dcc6364b26dfd5a
MD5 392748e668a59382901e6ccdf0aea281
BLAKE2b-256 fe6435c404084b109d56105781ef2581c7a54a645eefe03db74e7a0b9f0d2522

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 8fc2daf5a2cac65f2ed12648a31fe7093a67f003410f3fbb279893912a36f7f8
MD5 1948329c08569c8bc1cc42559de91155
BLAKE2b-256 563013582e52e2aadb57f8ec0cbd9971e0d5c733380ae2ae7ef6f18d541224a3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 6e5045b47d7ec939d032b30fd410ab72ea10555c0c09115f0a7a1eb529e3aa8d
MD5 4cc22e27d9d311b27f596c7c6506cffb
BLAKE2b-256 73a640171836fcc0b36076945ac436f066df46458a6bd2f73258cfb747a79170

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9c62ade04994dbd0e8e2a51aca4545124b1030943148abca3a677e7b4655b25d
MD5 c1fa2c3aa1fa537f249f988f0f14a6ac
BLAKE2b-256 92911cde26769eb2bc260289847368f070e012254ffc3df3bf279f27c7c2ec26

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 a76d8def6fd9569a108c3abf9fa9e3e47becca15de5657df29de71d59d897aef
MD5 d8126929ddad638b9672dec14e6a46bd
BLAKE2b-256 5b2330fc9a7d3fb8154a8da0021fd5a5de59dee482648bf73042ce6a475e2760

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 8338dec10a2e015713fe27512e88fd169ff550c14abcbc69f876127f03bdfa1a
MD5 e2eff109c221dcd13ebf18427f68b9bf
BLAKE2b-256 4e9a2581bef6866e5e1189ac0379598b61bdb0266dbd1b764e86f1391f4d9fce

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 24c27a3d196ff4adc0bc54650c52316856cf9b8e962fb05367053f0b05f7d60b
MD5 3a909fbddd983a84df2ec04236a4561f
BLAKE2b-256 31f785cc6f539ea1d6c683fce5a3606d03d7cdf0021088a3e3530ba604a26349

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 10c7077cdcc6059e49cb13cec750bd4fc0d9dd8f294be744021253839d986b42
MD5 706d88b645a2fd2ab7586c1a6d734df2
BLAKE2b-256 9ea8d055ed56220f151df894394b353d19ff52a3d9bf16e952981625f3bee953

See more details on using hashes here.

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