Skip to main content

Model Serving made Efficient in the Cloud

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

This version

0.4.6

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

Uploaded Source

Built Distributions

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

Uploaded PyPy macOS 10.9+ x86-64

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

Uploaded PyPy macOS 10.9+ x86-64

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

Uploaded PyPy macOS 10.9+ x86-64

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

File metadata

  • Download URL: mosec-0.4.6.tar.gz
  • Upload date:
  • Size: 52.0 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.6.tar.gz
Algorithm Hash digest
SHA256 7e7aed9e17dbc37d3bfe7933b523b8377253290d9b2895dcd6bb3805fbd15e41
MD5 b4342682a4517ee37fbed8fde0bba8ab
BLAKE2b-256 2bb487c9cc18098ac7e1244df8191694542983114f6455b16976ae8b28d27f41

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 8fed84b118b22e269f32370ad7859c4ec07bfa440bfe0f27ccb416a9ada0fddb
MD5 546fd31316fbcc3747c19f7955244956
BLAKE2b-256 e972cf0d438f3faf1f561cf7429e20b477c49363c07b6a2429e2c1df6eed9820

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ed6dd1487756f74fc0ea95d65b957c63fa950077f20ba483f82a683251e9d510
MD5 e82ea34c56afbbf2f85948984f919dd7
BLAKE2b-256 7d43a415f9edfd300c65cc5acea90ac5e4c58f08fb35c109316560e57bc7ad46

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9c66c79bf502d396dd653e2da27bd3e60768bf36b53cfbe12a538aa6830b551f
MD5 09ba1ac77914f76a81d48e2b01121c8e
BLAKE2b-256 af80e9e0987ab15e6fd31c169bf4d8e8ade09fb4144bdd8dfdbb1c81eaa2813c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 21937fecfa22401a222b6095adca30097419adfa98a3d6e872e96162c6292096
MD5 8297de7f91d0cd5744f8ebbcd27e367a
BLAKE2b-256 68946cfbfdcc9efc812d5e71f2e4b4dce364119e6d36c850a9f54de91606f8b6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2ba3d3877e7d843f253d99f998ce1db3c2820ace4366542b19601db71a4191b8
MD5 c385b894ef94da691bc9d8bc831b9fbc
BLAKE2b-256 b71b7cb52022b4f7df5f73193c17e8458e12db22d35e8bc2239c76b621915577

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3cc7cd5daaf4805ab940a6370e5926790e52a86d180b92c8a2d9878f9e6db016
MD5 88dfd1b5c3a9e29b67646adc5bc31036
BLAKE2b-256 8c957a160ed0987f914a4cd76c14791938c60f2eaa3aed862a34b14dcf8411a6

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 16081810255b7230b08bf20bc11e804d3773180f0259dd180688002333b9b9e3
MD5 b1345316daa173966100fd85560e3568
BLAKE2b-256 b6f204e605b2da19ee45182e7ce60d2f145bd9f5a135c3ef2b5b11028b4c5d25

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 fa603f5244691f89025d63822e10a85b564ddfd763f74e60df2312d8ba5f219c
MD5 60c28ed1ca37702e4c8dcf529c83ff73
BLAKE2b-256 6691b13d4d6bdd12320fc269864abfac3e4adbfaa76dc5e850209a04c942b168

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 723f35013be4781ec6bda0c7cacf112ebb3f50366447723b7b8c589e810ef58d
MD5 d62a242b03c9b79d7dc873a87a691916
BLAKE2b-256 bb237fd6aac19b567a76d35a62e6cada58c6e16017e0210e568c011b8c513a7d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 83d8cc6f604a3b92517d6a80a486483ffedc671f7922ae807b3c0bab6b55b016
MD5 3e848624eb4af491ddd45baae71893b9
BLAKE2b-256 cc69368137d32c715a9512182ea2eedeca28dd8c63cee5f06e3d42b200225631

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1219d8faa04a4d2cc2486b729319b43b2fc7696d7db4e88f64a82979d5fd15e4
MD5 e84306da9d3738f32245044fdad564b0
BLAKE2b-256 8d66684188fa8f80f3220a35d03bb38cfbfc582d8912fd2800f30d654dca6580

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 84de0676517891a31e67fa8b4d09d49c0ecfcb3eb6c79650c6e6e58e4f425d4e
MD5 977bf09822e7fe88de67fa3c2f98c196
BLAKE2b-256 dc5233a6aabd36008609cde31dcd783ffaa00c04e6589d444c45bcc811cb30b4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 07dbaa167526efeaa6f4acf8e1bd5f786a587f6c2f9bab16275feb7321220e20
MD5 306a25ebcbc80396560b42a2d941aebd
BLAKE2b-256 fa156e5fed1b723e69e772b7f006a7a199492118a68ff32fef25b1cfb7a1b882

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1d854ba89b755962ad1eb8ddc0f164f6d0a57dc0d859394b20f305c911b38de7
MD5 10756363adfc977fa7ec8714632fd315
BLAKE2b-256 91fda4bbe7bb2b9207063ebc427fcba0192f23fe47e647d2a771d8641a6280d1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 3916a12c4040d8ebf2923f34f580493b123cb1bcdb81c36b1faee50ba0c5434b
MD5 d8e6c8388e87108821a8da2c42f51c13
BLAKE2b-256 bb4e2b2a7d9314686a0878d35e0835cb34e208f86458b3a7aeba1fccc790454f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 dfc689aab40a1365943273a680cac3d4add2bdabd0f4bf570ff04222262526a4
MD5 669eb78aff6880a216d8ab3f807d8525
BLAKE2b-256 4dbde836dee795445eddbb054256a0aa9d0ac90c8582cc37dcab101e3e56077d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 df4e43c3b53c28f7c8da76894884b091abd2bb9e18cc578ac9631a3dd4d9aee3
MD5 385984c765bb8c7915823b3314ee7a88
BLAKE2b-256 2835135826822b78edb0fba053e4d00f1bf0a3cee6ea22d0558d7e2322123209

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 273f5a12f92b89060140ed4992f5b83d360d4d62a3774ed193467affa650b03f
MD5 688d49859e8f2856200ab4c3f173f2a8
BLAKE2b-256 c0acd249dc6effcdd285da69037a70a962b641d4f89a39369e8a4d5771f8d204

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 1ef31d4a86c6d5449027309bc21e1cee9fe3d97568a3f39471d227811c3854d0
MD5 4ce479025a5cd786f2b9e981e1d8f0f3
BLAKE2b-256 81f1771946360d9d9f2d5b4ac9e705bb64e1f0ebd461380da162c5e6601c3ec8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b3c55f9fb07e8e7d73d41e6362b04ed93f1c3015a8d9411933134c7ff1333984
MD5 e31ea6d3d55ae5079167f542576069b2
BLAKE2b-256 90f7f10a4fb09dbad3bd8c4f55eb85f3fec5af4b32ea18de8ff7314d00f84248

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.6-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f2dc20f1e2369baac0d963083d5044e1a36192de7b3c3c53680288946cb35c56
MD5 293b3cdd5927719fee8008b6162ea69d
BLAKE2b-256 ebcb5062a1fde8e9dddc97d19d156b3ab81db4d238abba3899007de3b188dc51

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