Skip to main content

Model Serving made Efficient in the Cloud

Project description

MOSEC

discord invitation link PyPI 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
  • PyTorch deep learning models:
    • sentiment analysis
    • image recognition

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.

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!

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

Uploaded Source

Built Distributions

mosec-0.4.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

mosec-0.4.0-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.4.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

mosec-0.4.0-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.4.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded PyPy manylinux: glibc 2.17+ x86-64

mosec-0.4.0-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.4.0-cp310-cp310-musllinux_1_1_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

mosec-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

mosec-0.4.0-cp310-cp310-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

mosec-0.4.0-cp39-cp39-musllinux_1_1_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

mosec-0.4.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

mosec-0.4.0-cp39-cp39-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

mosec-0.4.0-cp38-cp38-musllinux_1_1_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

mosec-0.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

mosec-0.4.0-cp38-cp38-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

mosec-0.4.0-cp37-cp37m-musllinux_1_1_x86_64.whl (3.0 MB view details)

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

mosec-0.4.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.9 MB view details)

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

mosec-0.4.0-cp37-cp37m-macosx_10_9_x86_64.whl (1.8 MB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

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

File metadata

  • Download URL: mosec-0.4.0.tar.gz
  • Upload date:
  • Size: 49.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.4.0.tar.gz
Algorithm Hash digest
SHA256 e211fdcbcee5b2302cc4a0c7b57320d7d7f9451bdc8ada81c60d757e32781f3f
MD5 05a8c8a11b7c9b47d1a2933871acd84f
BLAKE2b-256 2f21e4006e67cf82775b9d2a9f9197dc6d9f9135ef1c8df12b9bfbd157e52259

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.0-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 750153103b9ac5bec24ed8f74fc7ddf634ccf1b646a2e8c54c18ad2f3a6fdac1
MD5 bdfa2d5328c3eb41d728e8154f3b5201
BLAKE2b-256 c928d1806bbedfab3f613fbe81fd82fc9f1c1460d7aa46e3ab5d9b82ffb8fb81

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mosec-0.4.0-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: PyPy, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.4.0-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 e6c65e47e535658f3b8ab49c6c48a1c78703dc6b0e5a3248e4fc0eae2fbf6ce3
MD5 26ba73c233bb95bf2e10a00aefc412e6
BLAKE2b-256 e152f7370ff232f05a914f7267b440ee9d4efadc971a3066b392b3d843860d9c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.0-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9c63cd0b55ae4fbc1ef0d8a359785a16d9d6b63159b6fc84051666b711d50977
MD5 c7d5eacd498908142f63b89167f160c2
BLAKE2b-256 7abd4602b4c8e6e50ff18983d091e4841a270cf2e2ff24cf85a390031a0f4e1f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mosec-0.4.0-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: PyPy, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.4.0-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 54aefb3a07112f8b28fecb8d2ede7130f00bfdb8bd98447e72c8354c0b614064
MD5 0401d73f6a24a639fa10ca4bc9db3ab1
BLAKE2b-256 9c16d00dcdf111ea6d07e3646be1f65a1f691985f81a9190a61746491cee0d88

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.0-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 d35c5d899182b0327a4ea1a080ab976067fb21ddbf36fdfaf8736733653c1764
MD5 a124e8bafaeed313c8230c28c98722a9
BLAKE2b-256 17a8a87c7ffb9d77578f3e8edcb42f95126a61e51e30960a254b8a9409d747ef

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mosec-0.4.0-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: PyPy, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.4.0-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 692cedfd834b43679673238ec6c6a9912bf3c7a5a752c7d39672a4924a4fa4a0
MD5 dce3618d7ac76b2f20fe475b18c1f182
BLAKE2b-256 7e837161a1bb283f8d65298911d5602047b004796a26e8093f60adf3c60e7601

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.0-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 f1cc858b842f2d195db8624ea356715cabe429e760bbd007c8a39d1a0911ed76
MD5 6e4154a8474f66c4e8216abb0637356e
BLAKE2b-256 94100cec9da50a760daf8ad829c7af91e993b0c53e94c3c20dd14a148b01f1c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b2594b7791be155c237cdca911e24afaa5114e9882d093e0ec53eb2c46ce501e
MD5 1cc874ed7415adf146c94c7ebe1aeb3a
BLAKE2b-256 f7a1734c83ec2a73411769ac1876f200acd12846e2240e80db6026f3f7f0aca8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.0-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c7ea513dc68a74b33d3f0933c88e78cd8e21bb790441676b478bf7330d6f0b8d
MD5 349e643b9a9d04df5ec99b9bf519e48a
BLAKE2b-256 26fb5185cd2b09f855162668cdc28e453ab94409b305dc160dfad6d8f440f8fe

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mosec-0.4.0-cp39-cp39-musllinux_1_1_x86_64.whl
  • Upload date:
  • Size: 3.0 MB
  • Tags: CPython 3.9, musllinux: musl 1.1+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.4.0-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 f1016253659ea26bd8bfa1eac0b07a222a0fd7d425cdee46d1942563d309bbd9
MD5 6cf6b94c18132c4287e64fcfc2025a59
BLAKE2b-256 bc112bec5fec7624d034795ad3eb388c618dcddf7749c6494745b54035c450e5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9fa8a2f4e73c712c534eb2d7b8801479fb2c6cc82f03ece94550678f8622fcc4
MD5 1225a9880b9cfbc1ee1cb16f725dd24c
BLAKE2b-256 ee9cb11c3664773024e4a128779b15503cf7c52145d6cb42a29f0b81400ddb89

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mosec-0.4.0-cp39-cp39-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.9, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.4.0-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 15de2a7920b92ff9831c2a5923f5eb4d26a732edefd95f541691403cd952b206
MD5 e3fa122332815cec8358b7516fd2ca3b
BLAKE2b-256 77b62da30bdce4076b7263f87069f1dfde523e0b072c47e7e15992fa60e3a33e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mosec-0.4.0-cp38-cp38-musllinux_1_1_x86_64.whl
  • Upload date:
  • Size: 3.0 MB
  • Tags: CPython 3.8, musllinux: musl 1.1+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.4.0-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 79556808ff465fe170282885906af30698ffab9fb71a1525b7509067b062add8
MD5 33e715e80970fa9014cd6f6ffd47dba8
BLAKE2b-256 143c4d10fad2b0e1be7afe383ffbc01d390f36cd9dd7579b3a971d5fa0038f5e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 529c42ffdb6629565b5b90ac8d4b3d2c3922bdcca511690a444bdce2a3aa6dd9
MD5 a3d43d71eb366c3b6ff0405aa1b2f6da
BLAKE2b-256 09db78976462fb867c267f27ca93d5b278c7654cc9163677a926fad1e55af7b0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mosec-0.4.0-cp38-cp38-macosx_10_9_x86_64.whl
  • Upload date:
  • Size: 1.8 MB
  • Tags: CPython 3.8, macOS 10.9+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.28.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.13

File hashes

Hashes for mosec-0.4.0-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 56871cbaf4d07ae28bc3d3e858137e8f6e5b0f928f005147a69db22c9120abf9
MD5 ea9466d0a9eebd105a076104d7d931a0
BLAKE2b-256 7739653a0b3e352904e53aa298fe0e0b61a94dfd4efb6eb59a4e35ff1d8ddcd5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.0-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 d5ac8f45e31bcf8180e1391ee4b923482f7d38fe02fade435d3c27983d2510d1
MD5 3a1d6c16b1e5f840d30d06843d5f62d7
BLAKE2b-256 a3b8396a8b12c7b067220e8743f1aea0c7b6e3882cfe4ce634bce27824b415ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.0-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9cb208810fad0501520276328533fbe8908edf1fa016bcfce1907233e64b1d93
MD5 bafd9340bf61688440393adb618ee6a2
BLAKE2b-256 ebda91a057318ad2a0f0e699720ad5d6094f936f4ba77935983f5b21adfff925

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.0-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 54edae62203cc2599bd786a2091ad8ad5d709d99c28b693f6a95df0ae3c93e16
MD5 c3094f74a6a6365aa0735311de767c9d
BLAKE2b-256 f4b8ec141edfb74dafdf29481a758da3477bed9d769074c44704f7702fcf54c4

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