BentoML
An ML model serving framework
An ML model serving framework
BentoML is an open-source framework for deploying machine learning models to production environments. It simplifies the entire process from model packaging, API server creation, and containerization to deployment, allowing ML engineers to focus on model development rather than serving infrastructure.
First released in 2019, it has grown rapidly and, as of 2023, has recorded more than 5,000 GitHub stars. It supports all major ML frameworks, including TensorFlow, PyTorch, scikit-learn, XGBoost, and Hugging Face Transformers, and can be easily deployed to clouds such as AWS, GCP, and Azure, as well as Kubernetes environments.
The core concept is the 'Bento'. A Bento packages a model, its dependencies, and API definitions into a single standardized artifact, and is portable like a Docker image. When you save a model with bentoml.save_model() and define an API with the @bentoml.service decorator, bentoml build generates a deployable Bento.
In practice, BentoML is used as a key tool in MLOps pipelines. In particular, it offers advanced features such as adaptive batching (dynamic batching), multi-model serving, and GPU resource management, making it suitable for large-scale services as well. Compared to building a server directly with FastAPI or Flask, setup is simpler and performance optimization is handled automatically.
# BentoML로 ML 모델 서빙하기
# pip install bentoml scikit-learn
# 1. 모델 학습 및 저장 (train.py)
import bentoml
from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
# 모델 학습
iris = load_iris()
model = RandomForestClassifier(n_estimators=100)
model.fit(iris.data, iris.target)
# BentoML에 모델 저장
saved_model = bentoml.sklearn.save_model(
"iris_classifier",
model,
signatures={"predict": {"batchable": True}},
metadata={"accuracy": 0.96}
)
print(f"모델 저장됨: {saved_model}")
# 2. 서비스 정의 (service.py)
import bentoml
import numpy as np
from bentoml.io import NumpyNdarray, JSON
# 저장된 모델 로드
iris_model = bentoml.sklearn.get("iris_classifier:latest")
runner = iris_model.to_runner()
# BentoML 서비스 생성
svc = bentoml.Service("iris_classifier_service", runners=[runner])
@svc.api(input=NumpyNdarray(), output=JSON())
async def predict(input_array: np.ndarray) -> dict:
"""꽃받침/꽃잎 치수로 붓꽃 품종 예측"""
predictions = await runner.predict.async_run(input_array)
species = ["setosa", "versicolor", "virginica"]
return {
"predictions": [species[p] for p in predictions],
"count": len(predictions)
}
# 3. 로컬 테스트
# bentoml serve service:svc --reload
# 4. Bento 빌드 (bentofile.yaml)
"""
service: "service:svc"
include:
- "*.py"
python:
packages:
- scikit-learn
- numpy
docker:
distro: debian
python_version: "3.10"
"""
# 5. 빌드 및 컨테이너화
# bentoml build
# bentoml containerize iris_classifier_service:latest
# 6. API 호출 테스트
import requests
response = requests.post(
"http://localhost:3000/predict",
headers={"Content-Type": "application/json"},
json=[[5.1, 3.5, 1.4, 0.2], [6.7, 3.0, 5.2, 2.3]]
)
print(response.json())
"If we implement model serving ourselves with FastAPI, we have to handle batching, caching, and GPU scheduling all manually. BentoML provides adaptive batching by default, and multi-model serving takes just one line of configuration. Considering development speed, BentoML is more efficient."
"We create a Bento artifact with BentoML, then automatically containerize it in GitHub Actions and push it to ECR. For EKS, we can deploy with Yatai or bentoctl. Model version management also works naturally through Bento tags, so rollbacks are easy too."
"The advantage of BentoML is that it bundles the model and serving logic into a single package, solving environment dependency issues. However, customization can be limited, and if you have special requirements, TensorFlow Serving or Triton may be more suitable."
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
BentoML 1.x is not compatible with 0.x. When migrating existing code, check the API changes carefully. In particular, syntax such as @bentoml.env and @bentoml.artifacts has changed significantly.
For large models such as LLMs, the Bento artifact size can reach tens of GB. In this case, consider keeping the model in a separate storage and loading it at runtime.
BentoML itself has limited monitoring capabilities. Integrate it with external monitoring tools such as Prometheus, Grafana, and Datadog, and set up a health check endpoint.