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

Uploaded Source

Built Distributions

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

Uploaded PyPy macOS 10.9+ x86-64

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

Uploaded PyPy macOS 10.9+ x86-64

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

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.4.7-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.7-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.7-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.7-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.7-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.7-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.7-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.7-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.7-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.7-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.7-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.7-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.7-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.7-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.7-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.7.tar.gz.

File metadata

  • Download URL: mosec-0.4.7.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.7.tar.gz
Algorithm Hash digest
SHA256 7feebb7168e14abaa571bea949577ce28055666c81cbb2cdc7f88d884519836f
MD5 27f61bb7ee55941d1f0fda59cc10e546
BLAKE2b-256 3f28a06e545e6fbe65124502073c566be15b1aaa743e8be4dd2361d5fbc539a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f604a421e7f35be5064c906a6c55bf93c9564e2b5c9379011ff7293d285ca7ea
MD5 dfc5f03f79625ca0f26156b1a3ff7bd3
BLAKE2b-256 541d3a4d66d7d680c24bdb260ff67a8737559c487ea1a320d16bd110d2498a89

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 09bebe16a487a339195e4754ee8c042b3edc74bfa3f51c1ea11b224b178e261e
MD5 9dd314f31c56d2342dcc44bf626dc855
BLAKE2b-256 800b4b5095e2be4c92c3028d3200b0b800abd35fdb5dee2cc2e3e0aa1adace8a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8957cfb4776fd0e5bea0e0d6686f9cf24a7e7912ceba8cec6aa1c40bc4f2332d
MD5 3a1bdea70072068a2856b0807f819586
BLAKE2b-256 975404c91a3764f79ee9fb7694e8019def1af02c3f6ef3064e9525174b869a4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1fc75ef155e7d9e6da6e8eb8b7cacce67d511b6a9bb387d1cadfd73ee1ebcab8
MD5 34bdc716af9b700c0b47a90fa61b8120
BLAKE2b-256 001c41528d2dc4aaacbb72a232c73614cbfe233cca0176ae007113367f889e92

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 876dc58cac2a16862f94260dbdc60154c241c2294f5cef537d76f89a299e8658
MD5 cf6b75a218b155050300d53de08b3729
BLAKE2b-256 da2810eaee7b43a79819b13b2b56905ab773832f69545a1eaa41791f447cf428

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 b9a2f5c8f24ebc5a9acfb50feb62792510aef1f5c4f232c024753dd7189ff614
MD5 467b9b8a9373a7d23e0062c28e5be452
BLAKE2b-256 dde6c40c0f52c34c03559d9c0755d54b4288c82aec3219ad6ed20d8af29b03d4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 f45ae5b6c44f89d891ca7f47a425d01d1e22d05438efc06a4efdffdbc7bc2ac0
MD5 49f0c170736d4da38e9470f33705ad0a
BLAKE2b-256 5a7565a68a0ad4ac80fc097f8e76a83b1126bd75bb7d433a74761567729d4fac

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6cf03268307786dfaab24288ebd4291692e4c34af68e2a6af8da59da36c4ef81
MD5 5d3f2da07f5c1fd345843c563c9271af
BLAKE2b-256 629567d82b8de8249f23edbbad21d2480373de5c616dedf118ade0301e5ff4d8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5397dc96b92420e6cee2aaae7b2196edd7571abb10d227e5a25c020b93dced0f
MD5 0abcf2c5c3292d995277f0bfcf5063f1
BLAKE2b-256 b200207594a4fc1879d4460b2fd3ea83cc592216d89198da928229e7b4f25f97

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 f68f0afbef4f58e69a5911fcf50427a5b34318c5608e43c2e51d5cf65cf320b6
MD5 7b18f9d3b8450692ccf8288b25aabdbd
BLAKE2b-256 fe1cfcf11bbfbdafb9d7a41c5674dcb2c47d966abb889d5d6d9e7d9a6abe4756

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e27c7710d45cc0f2a5f05e76cfcebef6dc8ad7d33e96a70c8693751503ca07d9
MD5 9a0a6533e3477b414074722b80de9b32
BLAKE2b-256 0f5e169c39b4bdcb105db9b05e66db0273cba78a6de334325fa1db6b75e0469d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 5868bfce6288463de54b7f4e69fdc9d1d9f49ace23977cf48cd950cac3177f06
MD5 feb04995bc13c996f92ef52c7f35dd0a
BLAKE2b-256 777f1d5f2be6499ee6bdc0896baeedca0fefa7aa70ca6340abf45d9743ac2ed0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 91bbb67306d9bb4d2765b8b135966b9b504d33f22ff6e0853c4b25a0a56244bf
MD5 8ee046d4764476385e3ec36c535ae1a1
BLAKE2b-256 1fc92d5560f2c738343827e0f15f1f9d4d090c21468ad4e978db7bb272016769

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 580fd55c8f359a7b4a48499b57560d6065c0968cff355d1c3796c7001544551d
MD5 0450137584530c5ecec640684ff3123a
BLAKE2b-256 ff9e7d64139ca4f80e72eddad01ca6149cbc7efd6f4c0a5073ebb74a28fce5e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7457c71587839bba323da1f10217430cc0ce7a0d797b80e3bc7fb4b50959142a
MD5 dc87b1df28d5dba1697db99538e60ba2
BLAKE2b-256 3bfbe703203b420a8d50f308efabfa1d43d7a5759f408659396950fda2b9900d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 c902dd1e279fe35d9bd5f2c20afa3943a74f9e11496f7b226922b09debe1c568
MD5 f9714ee2f6eee2d221ba455d51e15692
BLAKE2b-256 4ac58df261c7b5e43b8b5d0952536ad03595b99ec39d801c22e40b36db2cb1b8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0919ace3815cffe66c5a22832f80112c8afaeccc3592b5545b6aeb95d7d63ae1
MD5 527064a8a5198662cbde2c76c0b00371
BLAKE2b-256 33f0cbad250cb82635d9fa41adb572f4a0e0e3dd1aa6193f279365467879007b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 9eae1a5ab8160c4eaaa94ba85ec55dd747c39e61ac345ac0e5cac752a2dfa748
MD5 0342aa9bd8d076baa94c1c68a37a61cf
BLAKE2b-256 bddc31502190eab2aaa555dc0e6483a9a8046ccf218f86f247bcdd5985339216

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 61cec1876e540c870819d379665f47e05e48ebaf3ad0180c0933c2fe55731515
MD5 0cf1bad3d3a5dfdccb8c7cc00ff0bc87
BLAKE2b-256 0132706a0c4679e537e6ba158e80532af283e9a986baf1878c73dc87a5ec508f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e99c63d593e0c2760cff11b6d1ac383a8306ecbe3d5fa6f983bdd79bbbf908a9
MD5 e70a95ec777a553a6eab0d800174a818
BLAKE2b-256 eaa1d51bc9d518242d62d01e671ff9ed5aa6491e22ea3373ebd45a22e31ed867

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.7-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f5f9aa811ca568eee691de1fe61e30de99ccd53ff765cb59f7af77b6271a44af
MD5 3fcfafa646b554201f0cb74ef51db149
BLAKE2b-256 1962133c9c5cbba2543fc73ffc6111fe7ecb8a0096b847e1eb226c5b2fd540e0

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