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.

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
  • 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.4.8.tar.gz (52.1 kB view details)

Uploaded Source

Built Distributions

mosec-0.4.8-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.8-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (1.7 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.4.8-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.8-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (1.7 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.4.8-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.8-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (1.7 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.4.8-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.8-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.8-cp311-cp311-macosx_10_9_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

mosec-0.4.8-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.8-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.8-cp310-cp310-macosx_10_9_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

mosec-0.4.8-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.8-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.8-cp39-cp39-macosx_10_9_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

mosec-0.4.8-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.8-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.8-cp38-cp38-macosx_10_9_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

mosec-0.4.8-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.8-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.8-cp37-cp37m-macosx_10_9_x86_64.whl (1.7 MB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: mosec-0.4.8.tar.gz
  • Upload date:
  • Size: 52.1 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.8.tar.gz
Algorithm Hash digest
SHA256 d3d70232621700db121585c886b24c2a320fcfcdef119effa823cb11b1eace48
MD5 1435f5f2fcefcae3680a10e312a46ca9
BLAKE2b-256 476c11a219fac40522db57d121af14cf4e334cb1935077438e1d6fb6746b5db0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 29a8c469cad0e4964d5afcb4dff0a919e47a410a78b43544fd689244dadf1c0e
MD5 34df5e0b112be27047278697a0b257dd
BLAKE2b-256 d2db8367b4b1078e59df08622099cf71710940f85470bb938ba6cf2286a39fe3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 70e76ec5f86f177c32b8e5bf5dee4cdcbebf6047d0d99bdb602e1b446a023e65
MD5 cefb8d68fb285ef08d7073fb4949c0d1
BLAKE2b-256 0423a97367691c43ac4184f88f12ca468b6746aa0c5e905f611bde522c8138aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9cc66e9f1ceb007c51266fdff6ab0d52996aa6ca544ebe8b6a6bfae647d0e3e3
MD5 4692076e42a4152f1c9f7125e817ec71
BLAKE2b-256 6c75a3c7de60f37d794400e1a5eacf97979a7eaff9dbf500a3258bd309f859d2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 86b0f1bdca1ae58adfc6544372b2c2b30c50d6e6f9f2e8b7f2791f59bdd9950f
MD5 30fae9d548bb6c6a609989c80eb57417
BLAKE2b-256 1886a73efdd64b1815e25f261c8fe2ff963580a05ee09f33ec58ead4f4063db4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ed514743b0f69791131979142fc2f140dc2644485b0f4c782e9901a9796a6516
MD5 091457bdc7420c2ee3d62b0547f369d5
BLAKE2b-256 5f8689b3d2f8ab124b770ed02d11a7420d2f8d26afb8a01277f6f1bd68d2e340

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 15e632969e177aaeafea85e49a768561613acf8b37187ed8976f0d5adcdae407
MD5 1c8b59ebfd34b77fa534701aadaba571
BLAKE2b-256 ddaec802033041b81e7a092b9d5eb689e1f5a61f004ead452ebcb7ae6078c3cc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 7223207cbe76dee68b291e7f0ede16c27d5265201e41ecc1b52262e48f78b7b6
MD5 71308217f89bd0b1c99d353faa8ccf47
BLAKE2b-256 ddf8141d87b14fd5964e7ff940666a3527173d0d565f537157f8e52a817161e1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5cdc305d060af53dd1462e21f71af542773fbd98f03f91ac1a203ab4f28b8333
MD5 4f168601fb628f2abf462a1e30c68a5c
BLAKE2b-256 02294b600f66f06e5f1759689eacb42a80ec884a5de9296800b0df258776fbdc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 829a251339011cd4eb4203cdef76c5521a5c78bf41fc187bdbca7e2411f81fdc
MD5 09eda6e5f588f68e46344ddcd8f9d09c
BLAKE2b-256 ab87ad4e87d6fc9ca19de3f0374dd632fcc0210bef0d9193bad4c169a4e20c95

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 71cfc57c9a4430d24c56693cae4e422f5df6ccfff6251e421fd9438ce4da4fe8
MD5 5d9d5eb35c3dd25d7ece38653722626a
BLAKE2b-256 122639947d7c65357e1ec671e61f6904385ca1ff3b17cc5658915bec4309b632

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b367dc6095cbf8fd90765e4a90d673facd8c5ced50b3b517c3e92ea45bc22715
MD5 137d8c4635297e5d02d63e20e546e5a3
BLAKE2b-256 13877eec30617c2f5ad4baf62dd0377d8cf8527366f0a2d1d7c06e96a75c2789

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f8d4c4916a4ab0c7b2378e37ff77bb81d277e3fc1c8d4cb23adba7b061073723
MD5 47922f4a0d7d9d564c0adadb2644577f
BLAKE2b-256 e7cc2ff9fdb6bc8b5798bc556101d818e0039d0dbc474d4bd0f40ed537ae16a9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 72ba4c27b5bfc7809f3be76d06cd417239bf0b1b5b193f90c8a03cbfca013afb
MD5 58b2d6f798a75912fcfc68e2994324ca
BLAKE2b-256 9bb3b8395ddd284fe03cc50568cd3d6a8a18e9e0d81269be56729b2658d57a99

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6b857fdfc7345a6962ae8873aa39ed8b4267af0946671c6a1c1951de258aa192
MD5 a320da3d0071eef65221f899422a2bbf
BLAKE2b-256 ca8bba7e086259222274dcc39a391c2df08ff614e07a4eb0ab2372dc43782215

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 58a5ebbb2c292d5ac711a452ee893893cb008ecbe29b0b6fc5753f603e3c0a65
MD5 9c18877fe8469433485711ff106f2890
BLAKE2b-256 fb450e180c7d5380e6edd4455d1ba8f95587076bf0d4d8b7f8a221b907bafcf2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 5b891fa92f92932c0c5c2c9c50898d6334292cb563e4a9ead15d0900398498dd
MD5 904c91731e5f99fe4451f766708e50ac
BLAKE2b-256 e4b694cd14beb9423eabb46c03bccba435b1a5d399e0fb0177391b7e738c27c1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 259253f688212fff3af3dae0d509b080afae53943019c3ca434ef4cc3fa53647
MD5 41d2b90964f044fc49c3e224a56cda0c
BLAKE2b-256 7dc91ea08cf9fba395b55a4cd02ddf90826e281fd87a2efd67611bbcc0d90706

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b0e3f0246122e5230cf1f70e97148d6c2e7b3579a6d1d20a2b0b29dd8fc3a3c3
MD5 9629d4d7477ae17e0fa11eea07c9f623
BLAKE2b-256 893a5a04780a7831d23fa2d2acae1c49928795c614c1fa3852ccc2ce0cd902d3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 5b6ea6106cc8b0404515a5a700518e0fca25fc452f266d37005f17f7c61bf19b
MD5 960ff8cacb7832234296d43cc9dc74d3
BLAKE2b-256 88a1cd636dbb2cf595aaa9074cb60a625409376e5e3f02338e694ed49051bb03

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b45ff51f4e0a8bfcebd27540c5977d1737883c856fa2d28e41e550bbfb1351c5
MD5 adcaf2875312985945f78e13e662c6be
BLAKE2b-256 90cd13ac445ddce6ebd2612ad342a67d12a53bd52328b6e39a5f2e7f3c60ce70

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.8-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 74488d946b4e42c81e01fc0e0fdcbb755306f8d35a7ec0a4009b9bfe2f3e348b
MD5 c93bd89fa8586ac1cab675a8bd1aa804
BLAKE2b-256 5e16d79b9e852eea98224bedf20ec9963e73a7e7bdc54461904ff66b303b58f5

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