-
Notifications
You must be signed in to change notification settings - Fork 756
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add a Jax data container. - a MNIST Flax example.
- Loading branch information
Showing
33 changed files
with
1,168 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
==== | ||
Flax | ||
==== | ||
|
||
.. admonition:: About this page | ||
|
||
This is an API reference for FLax in BentoML. Please refer to | ||
:doc:`/frameworks/flax` for more information about how to use Flax in BentoML. | ||
|
||
|
||
.. note:: | ||
|
||
You can find more examples for **Flax** in our `bentoml/examples https://github.com/bentoml/BentoML/tree/main/examples`_ directory. | ||
|
||
.. currentmodule:: bentoml.flax | ||
|
||
.. autofunction:: bentoml.flax.save_model | ||
|
||
.. autofunction:: bentoml.flax.load_model | ||
|
||
.. autofunction:: bentoml.flax.get |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,7 @@ Framework APIs | |
onnx | ||
sklearn | ||
transformers | ||
flax | ||
tensorflow | ||
xgboost | ||
picklable_model | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
.ipynb_checkpoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
events.out* | ||
*.msgpack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
load("@bazel_skylib//rules:write_file.bzl", "write_file") | ||
|
||
write_file( | ||
name = "_train_sh", | ||
out = "_train.sh", | ||
content = [ | ||
"#!/usr/bin/env bash\n", | ||
"cd $BUILD_WORKING_DIRECTORY\n", | ||
"python -m pip install -r requirements.txt\n", | ||
"python train.py $@", | ||
], | ||
) | ||
|
||
sh_binary( | ||
name = "train", | ||
srcs = ["_train.sh"], | ||
data = ["train.py"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# MNIST classifier | ||
|
||
This project demonstrates a simple CNN for MNIST classifier served with BentoML. | ||
|
||
### Instruction | ||
|
||
Run training scripts: | ||
|
||
```bash | ||
# run with python3 | ||
pip install -r requirements.txt | ||
python3 train.py --num-epochs 2 | ||
|
||
# run with bazel | ||
bazel run :train -- --num-epochs 2 | ||
``` | ||
|
||
Serve with either gRPC or HTTP: | ||
|
||
```bash | ||
bentoml serve-grpc --production --enable-reflection | ||
``` | ||
|
||
Run the test suite: | ||
|
||
```bash | ||
pytest tests | ||
``` | ||
|
||
To run containerize do: | ||
|
||
```bash | ||
bentoml containerize mnist_flax --opt platform=linux/amd64 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
service: "service.py:svc" | ||
labels: | ||
owner: bentoml-team | ||
project: mnist-flax | ||
experiemental: true | ||
include: | ||
- "*.py" | ||
python: | ||
lock_packages: false | ||
extra_index_url: | ||
- https://storage.googleapis.com/jax-releases/jax_releases.html | ||
requirements_txt: ./requirements.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
-f https://storage.googleapis.com/jax-releases/jax_cuda_releases.html | ||
jax[cuda]==0.4.4 | ||
flax>=0.6.1 | ||
optax>=0.1.3 | ||
bentoml[grpc,grpc-reflection] | ||
tensorflow | ||
tensorflow-datasets | ||
Pillow | ||
pytest | ||
pytest-asyncio |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
jax[cpu]==0.4.4 | ||
flax>=0.6.1 | ||
optax>=0.1.3 | ||
bentoml[grpc,grpc-reflection] | ||
tensorflow;platform_system!="Darwin" | ||
tensorflow-macos;platform_system=="Darwin" | ||
tensorflow-datasets | ||
Pillow | ||
pytest | ||
pytest-asyncio |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from __future__ import annotations | ||
|
||
import typing as t | ||
from typing import TYPE_CHECKING | ||
|
||
import jax.numpy as jnp | ||
from PIL.Image import Image as PILImage | ||
|
||
import bentoml | ||
|
||
if TYPE_CHECKING: | ||
from numpy.typing import NDArray | ||
|
||
mnist_runner = bentoml.flax.get("mnist_flax").to_runner() | ||
|
||
svc = bentoml.Service(name="mnist_flax", runners=[mnist_runner]) | ||
|
||
|
||
@svc.api(input=bentoml.io.Image(), output=bentoml.io.NumpyNdarray()) | ||
async def predict(f: PILImage) -> NDArray[t.Any]: | ||
arr = jnp.array(f) / 255.0 | ||
arr = jnp.expand_dims(arr, (0, 3)) | ||
res = await mnist_runner.async_run(arr) | ||
return res.argmax() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
import sys | ||
import typing as t | ||
import contextlib | ||
import subprocess | ||
from typing import TYPE_CHECKING | ||
|
||
import psutil | ||
import pytest | ||
|
||
import bentoml | ||
from bentoml.testing.server import host_bento | ||
from bentoml._internal.configuration.containers import BentoMLContainer | ||
|
||
if TYPE_CHECKING: | ||
from contextlib import ExitStack | ||
|
||
from _pytest.main import Session | ||
from _pytest.nodes import Item | ||
from _pytest.config import Config | ||
from _pytest.fixtures import FixtureRequest as _PytestFixtureRequest | ||
|
||
class FixtureRequest(_PytestFixtureRequest): | ||
param: str | ||
|
||
|
||
PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | ||
|
||
|
||
def pytest_collection_modifyitems( | ||
session: Session, config: Config, items: list[Item] | ||
) -> None: | ||
try: | ||
m = bentoml.models.get("mnist_flax") | ||
print(f"Model exists: {m}") | ||
except bentoml.exceptions.NotFound: | ||
subprocess.check_call( | ||
[ | ||
sys.executable, | ||
f"{os.path.join(PROJECT_DIR, 'train.py')}", | ||
"--num-epochs", | ||
"2", # 2 epochs for faster testing | ||
"--lr", | ||
"0.22", # speed up training time | ||
"--enable-tensorboard", | ||
] | ||
) | ||
|
||
|
||
@pytest.fixture(name="enable_grpc", params=[True, False], scope="session") | ||
def fixture_enable_grpc(request: FixtureRequest) -> str: | ||
return request.param | ||
|
||
|
||
@pytest.fixture(scope="session", autouse=True) | ||
def clean_context() -> t.Generator[contextlib.ExitStack, None, None]: | ||
stack = contextlib.ExitStack() | ||
yield stack | ||
stack.close() | ||
|
||
|
||
@pytest.fixture( | ||
name="deployment_mode", | ||
params=["container", "distributed", "standalone"], | ||
scope="session", | ||
) | ||
def fixture_deployment_mode(request: FixtureRequest) -> str: | ||
return request.param | ||
|
||
|
||
@pytest.mark.usefixtures("change_test_dir") | ||
@pytest.fixture(scope="module") | ||
def host( | ||
deployment_mode: t.Literal["container", "distributed", "standalone"], | ||
clean_context: ExitStack, | ||
enable_grpc: bool, | ||
) -> t.Generator[str, None, None]: | ||
if enable_grpc and psutil.WINDOWS: | ||
pytest.skip("gRPC is not supported on Windows.") | ||
|
||
with host_bento( | ||
"service:svc", | ||
deployment_mode=deployment_mode, | ||
project_path=PROJECT_DIR, | ||
bentoml_home=BentoMLContainer.bentoml_home.get(), | ||
clean_context=clean_context, | ||
use_grpc=enable_grpc, | ||
) as _host: | ||
yield _host |
Oops, something went wrong.