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
  • 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

This version

0.4.3

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

mosec-0.4.3-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.3-pp39-pypy39_pp73-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.4.3-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.3-pp38-pypy38_pp73-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.4.3-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.3-pp37-pypy37_pp73-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded PyPy macOS 10.9+ x86-64

mosec-0.4.3-cp311-cp311-musllinux_1_1_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

mosec-0.4.3-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.3-cp311-cp311-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.11 macOS 10.9+ x86-64

mosec-0.4.3-cp310-cp310-musllinux_1_1_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

mosec-0.4.3-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.3-cp310-cp310-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.10 macOS 10.9+ x86-64

mosec-0.4.3-cp39-cp39-musllinux_1_1_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

mosec-0.4.3-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.3-cp39-cp39-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.9 macOS 10.9+ x86-64

mosec-0.4.3-cp38-cp38-musllinux_1_1_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

mosec-0.4.3-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.3-cp38-cp38-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.8 macOS 10.9+ x86-64

mosec-0.4.3-cp37-cp37m-musllinux_1_1_x86_64.whl (2.7 MB view details)

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

mosec-0.4.3-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.3-cp37-cp37m-macosx_10_9_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.7m macOS 10.9+ x86-64

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 31280461513bc3a2d0794514352bbfae0347f2aeeba1a2978d82fcccd2f8b546
MD5 b27b6973a9f1307072ae4224cec45f65
BLAKE2b-256 33b2f87c293ba4a8bd340c2c75ebc90dbf3b5d9d5579cc7eea8d340ef04e1f4a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 07e02c0e814a8573192454a8c0239650505ff421d5ae23557da559fa515ae343
MD5 5b303090f4041d870d0b8751b3dfbafe
BLAKE2b-256 e34d81ca4326f9e94ae63d1d54fc06cedff9eee6a5af205e35f12c0cf11a1720

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6fe15391eb7c37f34a216b434493c64ce9d2c5a9fc781d59b60f196134824505
MD5 fb1a88b7faa7477c0253b867fc624032
BLAKE2b-256 19771e878b2e1b9791a91706182da82a3915858287a0cd7a9281e7e35c594510

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 6006669136aa3ad51b72a8f8cf6305249c9adf66b74d65fcd910fd62bd45de09
MD5 e490f7c8fb4c105b9082e5e9e516befd
BLAKE2b-256 5c076659eb90e008e0e9cb04f6193e0a4b8669d952be8dc8258db43fb4d1ac80

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6eefd49c3c32cfd8f33bb964e2135a7930c36f06005ee5a4f1ff5910fba874dc
MD5 e06423991dfd5ccdaa3780c70c90ae5f
BLAKE2b-256 ad25698ead7f72c19360f8b9f9a1fd873fe57cab264cc7212036b74ac02bec7a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 f13cf0539edc0d30e520989f799dd70b1f54679e765b2879a02d409ff2f38d04
MD5 59635867cbadf4ae2b0b399876535902
BLAKE2b-256 f5780f76da692f4874ac20e691b6992986546cfce36daf054dac51eacfb0a747

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 01b5b38c53399c0414f314019bc7586ef544daa6264d19ce80f4e7645beedb22
MD5 935bdb3454a9b7fdad588a591e5f2bea
BLAKE2b-256 e850d1d5bed3b557fe924fcc73b896a7aef05df436720c903e45320431f4c992

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 24a0b0e3ba8ebb3b2b812bd5a8229cd83ed2f0946518b3058cd94e91fcf3f2fb
MD5 d5332afe64550e81620eb8d534be0583
BLAKE2b-256 0eac97d58e886ee0a7a86da9da34f77b48b42c3fca8aff635f69aa283bab4379

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 2b4b9e4109eb047aef75a64f62a719511aef4ea2cf4e8e8dbcfcf1dc1336cf5a
MD5 1027fde35925dc4885b8fa0f72ef7a33
BLAKE2b-256 588b93b1bc5d7a06ddf553019eac84722f35424c0f63d17e209b93bb95fe31f5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 c1b1c660347b5f98fec846960828880d31b5e82f18a868246b17a587be23d825
MD5 49794e29266c9f73818e949d97d57fd9
BLAKE2b-256 23edf3f2b325d6e11400173bb6b52b7668c8182a93e10a28ef7fc25f6ced425c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 39cd7fb6a856f037be4d43f651c8c1b23bbe4db74c38582c7bc20ad252a6525d
MD5 5e917074da5c89b827a4b474fd5c2707
BLAKE2b-256 f23a30fbb18d7d0557049e1dd1eb81fda5a6333c045ebaf2b646d898e9461b2c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 c75ba8efef5ab4f3d28d91b4de54f0edff0cb5e6fad33d01840bc5da9b78cb25
MD5 8a07ebc27255c34422a7087e57a8fd49
BLAKE2b-256 0cdeff6a9366c36980ba8d80ef4153cd4caa2eb597749aafb1f1c2c9d4822005

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 6df2fa624c244a52d58162adb5edd518bcd08b1867c23822f684e9c183be89d5
MD5 dace2f9664854afb8cefe9986049e107
BLAKE2b-256 70aaf0b0ceb4acc8573a3aaf00ade2f4b731e87da3be137142de66fe095e89a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0aec9e51cb82f52026d8fe639bad5fbc15a503466f225161a5977e388a709d1c
MD5 f9228775f36fddc2408d1a6bf50d7d8e
BLAKE2b-256 383064fdc296ba3f9e46a863360fedcb1c0a7bb86dba03f2c34abe4d3054bf2a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 7e7cd4943526d2a51a516e3db4f6d3d6f0d4da11b3e7bde5dd126980f7001d7b
MD5 d93fae8a0bf89ab7321b07b4758232c7
BLAKE2b-256 f9e4fb0b751697df04cc2711ab8088318fadd52fa815967bf91d7eced88abef9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 3ae91ab600474479ac6a30d518d53d2a7163fad243333cb428f9d1f152003561
MD5 eae92374e47fc5c138b4b81c57ab697f
BLAKE2b-256 b1512f1bbcd4dc1ad3dabf117ee1c1a032a3582db81010c4df7a6d0c294af4a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c65e5c23e7cc5f45e74914a6da24e10a40c0c4aadfabe11e8fcd1caaa6d717ae
MD5 50bce4b5376b6c7adf3f0dcd09936d4d
BLAKE2b-256 7c226a4dd03b863bd1c2ad16af4d7268768b5af53e5a4f7f6f3a3833a2e805a8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 ea5565b3232dbcefcada70f763cca6b98dbb690507fd25153598b29966ca2c65
MD5 cc587aaadd964f1dd877512c404077ec
BLAKE2b-256 d22fb591b6ecde9bdad3efb16db9d435ccdcebd3cc91e221ecf590c18a5df2eb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm Hash digest
SHA256 f9462b125328e9c426d0b87037d33010f1dd75bba99c2aba5bbc8ba5dc220fee
MD5 1d14cd5decc509691f52646c60359f7f
BLAKE2b-256 e6befbf034b2d7e136c63a8044544cb0281895eed5ff1022b04f3a9443c95b1f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 efc07f31beea586e1b52e558dafddf39852f85b8ef2ca9e5922671345a2bf170
MD5 11352ac401330748d0375f417d783802
BLAKE2b-256 1f815b22c641bb190c9142a9043edc71557a0664de3fe84d716cb318706e2cbd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mosec-0.4.3-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm Hash digest
SHA256 97d89eb898efea3dcc3db270ba6f031a5979ad17b232935277ec64da8f0a1851
MD5 054a5b6ab3cb9044aa12f164e5020516
BLAKE2b-256 edb722d46306483da22e97304e72e7de4a4d8ec1260bd4338617c349e0ee58db

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