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

Uploaded Source

Built Distributions

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

Uploaded PyPy macOS 10.9+ x86-64

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

Uploaded PyPy macOS 10.9+ x86-64

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

Uploaded PyPy macOS 10.9+ x86-64

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

File metadata

  • Download URL: mosec-0.4.9.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.9.tar.gz
Algorithm Hash digest
SHA256 bd75688ef516999d541355be607c4ee80ead1eef2d3b81076866cd4107f43290
MD5 2ac79fb099ead6b13acae80174aff07b
BLAKE2b-256 b250d5d74d3edb356ba9223afa026403a40a31aa280d18595cda644a0297ff4b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7c088105667eb8ac765ecc71d3a9daa435bd102465b38855f70e9103c062169e
MD5 53f792626ec8b0f6b1f3d05746132ba9
BLAKE2b-256 88d7abf31d13104a58fe28b12aea4dcebac081288575e1862305f85d62b7bab2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f3fe8600b2107a784ce23dd2d68797f201d08cd064fa8d983973503f4cd41960
MD5 55bc0a6609f32a91549d0987df6b8b68
BLAKE2b-256 64b7b6882d54b082ff4114810904ef57f3f3bd65b5f704575fdb46719fc81182

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ad6f792a342f0de619ccdd3ac7faef1db1771d3a6afb7cf88b415801c56c46bc
MD5 4bb04b8175fce161be1397a13efdb39e
BLAKE2b-256 262309454ee207e8f9a52494af83e6cbe655618fdc02fb79f4d6610b55fcd8db

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 1062613ac65c4488634e688a1c9e2201a80c008a2e260607b2b47ad282ad1370
MD5 f69073975215f0c84371e9f95e72e284
BLAKE2b-256 37359c99ef78da02f1720eacd747ea1381251f04efd5d7669ed482cf0cf8312f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1f0ca0198d37b04bdbd81b22ef5f14eb6b9e636ba01133f8f08342bc26b228b0
MD5 8c21039eac05f69234683b85858e7734
BLAKE2b-256 afa34d666f7022ba98ab9cc06d3f7f1eb40d1087933b6b38905520f791941ee2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6b75ff6ab0a28cab9b2ffc784fa99a03004e2d73acd36e9696362818f50e5c69
MD5 2d617cf80e4c149f52e73a722f8b9ceb
BLAKE2b-256 dc0c13c3eaf891a10e4c79ee8ef03076e40640164e703df945277d4926ee549d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 b8782a72f0feae6fdd1519f879ebd7fb332f3e91f36b895e82562ac308bcbc4d
MD5 cb3ac24ecfa95f7996602a3d3a551837
BLAKE2b-256 3ef8c481c2ad5ac6d5e813d55ed180774c2b8fd91004eb08c635df6339bebb87

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e0ceb6d7ed71c69a27316e26ad8399ba563b56c8f8a3b416a102cbbba2cb99e5
MD5 da0ef61a27f446b81c919ec7d0b7ecd5
BLAKE2b-256 ade5df1032c6d1d06690d90fc5884f5896342a51f072478e44261fd47b4ad6aa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ececb5a849a06d4379965a88ee00cbba7bb99fc47847b1546f3216f86c32f236
MD5 a7aa484b2368379d0156eb248fac4ce9
BLAKE2b-256 5363be4342a3b72c0459c575dd8ec840871982acf476fb7809580047cb79b7b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 9798dc3a9369e9fb24014d57eb74e7949de58885d388d43a7a61b8145bbfcbea
MD5 2352c56810259f82b2ded8a219f2d7c1
BLAKE2b-256 9d7a0e08492e4a045a90ea646664be478dab5aa3f1cd0c9fc7e8997f6dd8f979

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5d1498bc73d092e30eb3df04ad81113c8cf5f91de80a99794f4f88c3f76b429f
MD5 573aa4c5cfffea746d651025b6377f29
BLAKE2b-256 031be376abfb45d0830997a70b7481de7ac3fe340172524f0014bee5ff6cb98d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 97ad636f378b1cc1678475bd01e36d47eb21fd2ced72a5e53db2d829b269641e
MD5 fb28a3e8bc4ee525a44e03e5451df306
BLAKE2b-256 7c97ef78a3dbdddd0c34c452c180eb33630c7bf5f6f0ccde3f19b41675a3f644

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 6f71c35a2a122111b55bee4a5a326b75f3768afa9bd845ebe1e7f4d55cfb060d
MD5 ba4d41bf4d15841e3c5b6efaaac88f8f
BLAKE2b-256 96958ddcf82c46db65e30159d8878c38e06d052717685256d5867e5c2419e6af

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 958ef00051571aea21abd6a66a748b95623a7d738bf7e80ef64a9c69ad8255cb
MD5 762a8559310cdfb265066b34676bccbc
BLAKE2b-256 d349b0da20c659e44cb08094ebf82c6265ec25d38f9c86ce76405923d6474588

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 69abb69f61e604c86cca936d69403bb9d08175383e5e66e3e234f9054b250050
MD5 ff3060369a6e95633cdfffc6d13864b2
BLAKE2b-256 cacdeb51083e5695531253300416f3f917d80d0e8775fcfd07182b889338accc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 b0d3e4098f74c3876024761265d1ddffaddf65c8f84fb556971c924b050048b1
MD5 ef6c3ecfaf81cbf0410b0e6184c1c34b
BLAKE2b-256 9f8f33b4e70ece332a00260a473b35247c47bfd97fb1f8a0a04e22b3b44dce21

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e59cff834169a386873eb8fa59985ac1317e95a62f85adc99251efcefee9f527
MD5 e3f90f16a5e073e0c0fb2f029058cce8
BLAKE2b-256 bb4f0c7840874eefe93e2ae4add345196e98e30d2e2b94949975c0c20d283ad6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f26de3a654df21fdfe5a18b366618fd1a958c98d0ed0b41ef1dad3c665e0ad86
MD5 f972f45fcd943c3f08f58aaaa55ba720
BLAKE2b-256 64dc3b01b658cd4384c200f86947f8dd133716f44845ec5a0c43654c1e2c2cdb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 c0832870c765a51f54bc0803d921a569329614f6de0e885ef90903caafb9db1b
MD5 d1d89365ce3cf53e98117b5a7e117c54
BLAKE2b-256 7427af383e3733c91164473159b191bf6aa6f0b1c63d587ed1dd322991c5b550

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5866f1e3baf98b203b8708717e7a310093362434b0f2222b08e7f01107f7c33f
MD5 00f361f45a1ca50ce9b14149de0e32af
BLAKE2b-256 8526df3adfd228b83abd4b0d100537138450ba0bed07ffa2407ef79a8aef6591

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.9-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 488d89ecafbee590c63866f236b9758a7d080f3b190f81e55bddcf45f9e9dfcc
MD5 12b0eca3ebd42c6093e04effb2cc64fe
BLAKE2b-256 58346c056f7f5a870a304d2a69c5f7552d8c37e3ad0d47e0d8a7b814974f67e9

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