Skip to main content

Model Serving made Efficient in the Cloud

Reason this release was yanked:

potential deadlock when too many disconnects

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.

from mosec import Server, Worker, ValidationError, get_logger

logger = get_logger()

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 with debug logs:

> python server.py --debug

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

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
  • Jax jitted inference
  • PyTorch deep learning models:
    • sentiment analysis
    • image recognition
    • stable diffusion

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!

To start develop, you can use envd to create an isolated and clean Python & Rust environment. Check the envd-docs or build.envd for more information.

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.

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

Uploaded Source

Built Distributions

mosec-0.5.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

mosec-0.5.0-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (1.4 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.5.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

mosec-0.5.0-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (1.4 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.5.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

mosec-0.5.0-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (1.4 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.5.0-cp311-cp311-musllinux_1_1_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

mosec-0.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

mosec-0.5.0-cp311-cp311-macosx_10_9_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

mosec-0.5.0-cp310-cp310-musllinux_1_1_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

mosec-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

mosec-0.5.0-cp310-cp310-macosx_10_9_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

mosec-0.5.0-cp39-cp39-musllinux_1_1_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

mosec-0.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

mosec-0.5.0-cp39-cp39-macosx_10_9_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

mosec-0.5.0-cp38-cp38-musllinux_1_1_x86_64.whl (2.5 MB view details)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

mosec-0.5.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

mosec-0.5.0-cp38-cp38-macosx_10_9_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

mosec-0.5.0-cp37-cp37m-musllinux_1_1_x86_64.whl (2.5 MB view details)

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

mosec-0.5.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.4 MB view details)

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

mosec-0.5.0-cp37-cp37m-macosx_10_9_x86_64.whl (1.4 MB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

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

File metadata

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

File hashes

Hashes for mosec-0.5.0.tar.gz
Algorithm Hash digest
SHA256 e5b9f3c1dd951fe45c4f3abbda7e36816c77cfacfcdf02eb2e5dee2d46c045d0
MD5 21f079964568f4ce51d090b944d675c2
BLAKE2b-256 8f8556e15e1f4036c0a10ef85cc5412994db355d83530b418386661e37c9bb80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 53cba9864852ad8e525568e9670fe7618e5a3ca3d1e9ef327ea3ce81dc822d1c
MD5 242bc1a44ac271cdac1185106adf8bf0
BLAKE2b-256 35bc651eb3ca759e0507efa0834188c9270a0636ebae59a3ed9636fac48d6998

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 cbb0ab0be492d3de75ae65424541c8b466b4b3dff188f385235ed642f1ad9cf7
MD5 a7df9b4bd493f21ed4ba36401f428009
BLAKE2b-256 cda30647f7081246d47a35917a90e135618307907863c35d1575422898cb9cb3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a9c30f91de5718241c86d26d805bb2764ede1805930038db758dc94a44a56077
MD5 be96a24dec5b291e88941f06fa5e75e7
BLAKE2b-256 f2450de99ccbe8cf251df847d645fa3960707e6feba434ea657df659bb2709e4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e8b0114b0bb1c18c392e5e0898dd1578992d02f3ead6e4b24ff13540c2b07cc8
MD5 3fe01aeeac5ec6db39607f74bba78e90
BLAKE2b-256 c730a88dab485c8ac6bfbc719c08b2da7b9f73d173d9957571aed72a2d28fe2b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3e5acb28b15f06c85178d217dffc0aec2899d6b3bec1390d2c1286b7c2044d09
MD5 d7e05658fa16f14d1d8460f5870c2212
BLAKE2b-256 f4cc7a15fbed80eeb682b0f2419ba72f385241f79be22eae32e12ad9df56e19a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 deb120c95cbb2d65f9181604f8859b5ac798be7fbcdce25872c66756e4e1e023
MD5 8a6d5f6d90491bd297d78bd589c06bdb
BLAKE2b-256 41576775d24c0d105936bf3fe72f184144e144155e8c75fa9109d28b8d30c7c2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 78fad5945a89a6447a6c48511f8add593dd28b06e2a10eedb7155d8a8f5e11b4
MD5 602945db23edc073947d2a419409cafa
BLAKE2b-256 9247816ac4a9d907f30fb8b5090e2ed281250cef1f3ab90b9c3a7ae63384c598

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 842c4fa0a4016501605367ce62f91f42310c7e02ddecd861c8c4f072213037ec
MD5 5f2b61715e26a045a939cbbb39dad659
BLAKE2b-256 942da5c99848276fdacadbb16d2f96be69b8e56f5657a3e2e7812461e71dabc2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6ec7333cce203c05e91dc692156631c79589302bc66883385aafa5aa950545e3
MD5 18c89b6e6506434ae8c8f4e2a483cd4d
BLAKE2b-256 1dedd4be831f0aa3f25ec4c57e52edab93c89cdc596cec300450f81a45cd96e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 0529f69809e2435902854b364fea22eb6e8872050e953d1fcb3b65c3c8943018
MD5 46b75f6941a2c59998dff4fe4f429b43
BLAKE2b-256 d2b0e553923a06b230b2fd5f63939d715ba8c6a825b349f03d9a0ad7ff43c4c6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d35af3e1fe0d77a8957c2d2fa4c7323212066f61dae7fd4a55c41b807853af92
MD5 89620f4d077f28ae1c4a140f7022050f
BLAKE2b-256 fefe3461709972c7bdab70de00cb7d1298919c64464133ebb3749292b703c518

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c0fd2fb67f0dc0072fdfddc0aeb91b85106e0872e8205288da15f2245d05963c
MD5 944365d78f035421e0c9f86d8089c152
BLAKE2b-256 d0ee6466ed9425212b5a9a8c853aa3db09cea216c57c198cae927b043dcf069f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 8034affa196820a51881bbf79f5089ca0e6ddb249e31bf22eff631cf0c84568f
MD5 7df909d1c0cbdbf2779991f6418a8f9d
BLAKE2b-256 e2b82f575c6df3c2e2e1afd720d5472741158dc048a915ff416520238fc7dcc5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e603d898e7127d68ade4b19a84e9c6334de30c34032ffcc5b0c6fbb0200ffa16
MD5 acd6aea56a064e1d289e426515281cb7
BLAKE2b-256 c22a4bbe1329624515dddf6adb3f4f0c30fe195e6358894944bf8afc19d6cf2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 beedc961a86b14c2c9420432d54ab4ad0a6d450f56606d24c18f3474383317d3
MD5 5e1c4a93be070225c331c376a588de44
BLAKE2b-256 ae987116f937daa473e6d0acf18644b9d888b2116c530c96a21f26d811d802e0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 d7a3584021bbe3c7b530e92d7ef6b6fd2fbddecd5e4457d36fb74dd95980e0c6
MD5 ef7d731cacf83d5eaf4189c28dd984d8
BLAKE2b-256 ffbc63b2bb5e1dd29233eb491de0989b92c235b99b800e42989fab63b0ab9ba3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3878775e53d9b7c29ecf1002720229e5f20e4af9f144fb81e94ef4ddd641419b
MD5 4106a0eced92733c809c18e2a50386c2
BLAKE2b-256 a25e6d5fe37ec0ef3a8c8a7be2a052bfec70bf7de7707a89a8bd609197a7616f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 bf1c6d3c9b0e0216336f7478b1ef8de13ef0dff057d2e6632c331d96823b980c
MD5 75aa5b7c65ad17a0e8fbb5b1857e296f
BLAKE2b-256 84cbf13517ae0a7a6dbc07b765377995dce95551de1ab31cd333c61dff37c3b9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 06aed61235ec72f44c3f6e84eadf97b7887976f3370f57ccc20396594b0758f9
MD5 17c662a0acbb52362c344f297f76e315
BLAKE2b-256 558b0178a95449cfd94fdd5c5f0b4e9d8d3f137c94852f684077eff338ed6173

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e8da393c1a19feb9a4755dfb12c5e8011eaf7920f3698dc143724ce051831dad
MD5 81f83b03f9d7b95c7fc76b1009d06af0
BLAKE2b-256 1b62a655db00ff7970500bd903aff9aa98478d7e1c229cd65a694d3d6b64d036

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.5.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 841935c850c6b86139fddf81ce7feb2cc98f6edb8908733cba39f15ace1836a5
MD5 efa89362079eb85280bf5b25e66e1073
BLAKE2b-256 d31d365ab83de990fd9d35e46a4b2ec590287de7c9f4dfa814e7be97892f9e79

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