Skip to main content

Model Serving made Efficient in the Cloud.

Project description

MOSEC

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

Installation

Mosec requires Python 3.6 or above. Install the latest PyPI package with:

pip install -U mosec

Usage

Write the server

Import the libraries and setup a basic logger to better observe what happens:

import logging

from pydantic import BaseModel  # we need this to define our input/output schemas

from mosec import Server, Worker

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)

Define our service schemas for both input and output. These schemas will help us for data validation:

class Request(BaseModel):
    x: float


class Response(BaseModel):
    y: float

Now, we are going to 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:

import math


class CalculateExp(Worker):
    def forward(self, req: Request):
        y = math.exp(req.x)  # f(x) = e ^ x
        logger.debug(f"e ^ {req.x} = {y}")
        return Response(y=y)

Finally, we run the server when the file is executed:

if __name__ == "__main__":
    server = Server(Request, Response)
    server.append_worker(
        CalculateExp, num=2
    )  # we spawn two processes for our calculator
    server.run()

Run the server

After merging the snippets above into a file named server.py, we can first have a look at the supported arguments:

python server.py --help

Then let's start the server...

python server.py

and test it:

curl -X POST http://127.0.0.1:8000/inference -d '{"x": 2}'

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.

Contributing

We welcome any kind of contributions. Please give us feedback by raising issues or 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.1.0.tar.gz (17.5 kB view hashes)

Uploaded Source

Built Distributions

mosec-0.1.0-cp39-cp39-manylinux1_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.9

mosec-0.1.0-cp39-cp39-macosx_10_9_x86_64.whl (2.3 MB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

mosec-0.1.0-cp38-cp38-manylinux1_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.8

mosec-0.1.0-cp38-cp38-macosx_10_9_x86_64.whl (2.3 MB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

mosec-0.1.0-cp37-cp37m-manylinux1_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.7m

mosec-0.1.0-cp37-cp37m-macosx_10_9_x86_64.whl (2.3 MB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

mosec-0.1.0-cp36-cp36m-manylinux1_x86_64.whl (3.3 MB view hashes)

Uploaded CPython 3.6m

mosec-0.1.0-cp36-cp36m-macosx_10_9_x86_64.whl (2.3 MB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

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