목적: AI 제품 빌드를 위한 플랫폼 (효율적인 다양한 ML 모델 아키텍처 빌드, 파이프라인 관리, 서빙)
특징: 폭 넓은 ML 프레임워크 지원, 용이한 파이프라인 정의, Transformer-friendly
BentoML 구조
모델을 Runner에 받게 됨
API라고 하는 서비스 코드 내에서 실행되게 됨
BentoML은 docker image를 지정해줄 필요 없이, 모델 패키징 후에 serve 또는 containerize를 하게 되면 "알아서" docker image를 불러옴.
Runner
정의: 원격 python worker에서 실행 가능한 계산 단위
작동: Runner에 할당을 하게 병렬적으로 실행시킨 후, 결과를 merge하여 최종결과를 도출하여 output
Service: 사용자의 input에 따라 어떤 처리를 할지를 정의한 것
설치
pip install bentoml
모델 패키징
# 모델 저장
name = "model_name"
bentoml.transformers.save_model(
name,
pipline("text-classification", model=model, tokenizer=tokenizer)
)
모델 서빙
%%shell
# service.py
cat > service.py <<EOF # service.py 파일을 바로 만들어서 사용
import bentoml
runner = bentoml.models.get("model_name:latest").to_runner()
svc = bentoml.Service(
name = "service_model_name", runners=[runner] # runner 1개만 포함
)
@svc.api(input=bentoml.to.Text(), output=bentoml.io.JSON()) # input으로는 text, output으로는 JSON
async def classify(text: str) -> dict[str, int[float]: # typing
output = await runner.async_run(text, max_length=512)
return output[0] # output을 바로 return
EOF
background에서 모델 serve하는 bash 실행
%%script bash --bg
bentoml serve service:svc
잘 되었는지 확인
# 아래와 같이 뜨면 정상 실행
# HTTP/1.1 200 OK
# date: Sat, DD MM YYYY HH:mm:ss GMT
# server: uvicorn
# content-length: 1
# content-type: text/plain; charset=utf-8
!curl -I -X GET localhost:3000/healthz
평가
!curl -X "POST" \
"http://0.0.0.0:3000/classify" \
-H "accept: application/json" \
-H "Content-Type: text/plain" \
-d "Today's weather is cold"
output = {"label": "LABEL_n", "score": 0.xxx}
출처
패스트 캠퍼스 (개발자를 위한 MLOps : 추천 시스템 구축부터 최적화까지) 강의자료