1. Installation

1.1 Pack

Pack CLI 설치가 먼저 필요합니다.
자세한 설치문서는 링크 를 참조합니다.

sudo add-apt-repository ppa:cncf-buildpacks/pack-cli
sudo apt-get update
sudo apt-get install pack-cli

.bashrc 에 다음을 추가합니다.
Autocomplete 기능이 추가가 됩니다.

# Pack Autocompletion
. $(pack completion)

1.2 Install KServe

pip install --upgrade kserve google-api-core 

2. Custom Model

2.1 Custom Inference Code

Kserve.Model 이 base class 이고, preprocess, predict, 그리고 postprocess 핸들러를 정의하고 있으며, 순서대로 실행됩니다.
predict 함수에서 inference를 실행시켜야 하며, postprocess 에서 raw prediction 결과를 user-friendly response 로 변형을 해 줍니다.
기본적인 서버 정보는 다음과 같습니다.

  • default port: 8080
  • default grpc port: 8081

app.py

cat <<EOF > app.py
import kserve
from typing import Dict

class CustomModel(kserve.KFModel):
    def __init__(self, name: str):
        super().__init__(name)
        self.name = name

    def preprocess(self, request: Dict) -> Dict:
        print('preprocess', request)
        data = request['data']
        return data

    def predict(self, request: Dict) -> Dict:
        print('predict', request)
        return sum(request)

    def postprocess(self, request: Dict) -> Dict:
        print('postprocess', request)
        return {'result': request}

if __name__ == "__main__":
    model = CustomModel("api/anderson-custom-model")
    kserve.KFServer(workers=1).start([model], nest_asyncio=True)
EOF

requirements.txt

cat <<EOF > requirements.txt
kserve==0.7.0
nest-asyncio==1.5.4
EOF

input.json

cat <<EOF > input.json
{
  "data": [13, 20, 45, 5]
}
EOF

테스트는 먼저 서버를 띄워놓고 request를 날려봅니다.
내부적으로 tornado server 가 띄워집니다.

$ python app.py
$ curl localhost:8080/v1/models/anderson-custom-model:predict -d @./input.json
{"result": 83}

2.2 Docker Build

cat <<EOF > Dockerfile
FROM python:3.7-slim

ENV APP_HOME=/app
WORKDIR \$APP_HOME
COPY app.py requirements.txt ./
RUN pip install --no-cache-dir -r ./requirements.txt

# Run the web service on container startup. Here we use the gunicorn
# webserver, with one worker process and 8 threads.
# For environments with multiple CPU cores, increase the number of workers
# to be equal to the cores available.
CMD ["python", "app.py"]
EOF
# 빌드
$ docker build -t andersonjo/kserve-custom-model:v1 .

# 테스트
$ docker run -p 8080:8080 -t andersonjo/kserve-custom-model:v1
$ curl localhost:8080/v1/models/anderson-custom-model:predict -d @./input.json

2.2 Build the custom image with Buildpacks

Buildpacks은 위의 inference code를 Dockerfile 작성없이 배포가능한 docker image 로 변형시켜줍니다.
Buildpack은 자동으로 Python application을 확인한뒤, requirements.txt에 있는 dependencies를 설치해줍니다.

아래 andersonjo 부분은 docker hub의 user name 으로 변경하면 됩니다.

# 먼저 builder 추천을 받습니다. 
$ pack builder suggest
$ pack build --builder=heroku/buildpacks:20 andersonjo/kserve-custom-model:v1
$ docker push andersonjo/kserve-custom-model:v1

InferenceService (Deployment)

cat <<EOF > custom-model.yaml
apiVersion: serving.kserve.io/v1beta1
kind: InferenceService
metadata:
  name: custom-model
  namespace: kserve-test
spec:
  predictor:
    containers:
      - name: kserve-container
        image: andersonjo/kserve-custom-model:v1
EOF
$ kubectl apply -f custom-model.yaml