이 문서의 지침은 엔드포인트 배포 프로세스를 기다리기 전에 모델 문제를 파악하는 데 도움이 될 수 있습니다. Databricks는 모델 제공을 사용할 때 더 나은 개발 환경을 보장하기 위해 이러한 유효성 검사 단계를 수행하는 것이 좋습니다.
배포 전 예측 테스트
모델을 서비스 엔드포인트에 배포하기 전에 mlflow.models.predict 및 입력 예제를 사용하여 가상 환경에서 오프라인 예측을 테스트합니다. MLflow는 배포 환경을 시뮬레이션하고 수정된 종속성 테스트를 허용하는 유효성 검사 API를 제공합니다.
배포 전 유효성 검사 옵션에는 MLflow Python API 와 MLflow CLI의 두 가지가 있습니다. 예측 테스트에 대한 자세한 지침은
다음 매개 변수를 지정할 수 있습니다.
- 모델 제공에 배포되는 모델의
model_uri입니다. - 다음 중 하나입니다.
- 모델의
input_data호출에 필요한 형식의mlflow.pyfunc.PyFuncModel.predict()입니다. -
input_path에 대한 호출에 로드되고 사용될 입력 데이터가 포함된 파일을 정의하는 파일인predict입니다.
- 모델의
-
content_type또는csv형식의json - 파일에 예측을 쓰는 선택 사항
output_path입니다. 이 매개 변수를 생략하면 예측이stdout에 출력됩니다. - 서비스 환경을 빌드하는 데 사용되는 환경 관리자
env_manager:- 기본값은
virtualenv입니다. 유효성 검사를 제공하는 데 권장됩니다. -
local은 사용할 수 있지만 유효성 검사를 제공하는 데 오류가 발생할 수 있습니다. 일반적으로 빠른 디버깅에만 사용됩니다.
- 기본값은
-
install_mlflow를 사용하여 사용자 환경에 있는 현재 버전의 MLflow를 가상 환경에 설치할지 여부를 지정합니다. 이 설정은 기본적으로False입니다. - 문제 해결 또는 디버깅을 위해 다양한 버전의 패키지 종속성을 업데이트하고 테스트할지 여부입니다. 재정의 매개변수를 사용하여 문자열 종속성 재정의나 추가 목록으로 지정할 수 있습니다
pip_requirements_override.
다음은 그 예입니다.
import mlflow
run_id = "..."
model_uri = f"runs:/{run_id}/model"
mlflow.models.predict(
model_uri=model_uri,
input_data={"col1": 34.2, "col2": 11.2, "col3": "green"},
content_type="json",
env_manager="virtualenv",
install_mlflow=False,
pip_requirements_override=["pillow==10.3.0", "scipy==1.13.0"],
)
모델 종속성 업데이트
로깅된 모델로 지정된 종속성에 문제가 있는 경우 다른 모델을 기록하지 않고 도 MLflow CLI 또는 mlflow.models.model.update_model_requirements() MLflow Python API를 사용하여 요구 사항을 업데이트할 수 있습니다.
다음 예제에서는 기록된 모델의 pip_requirements.txt을(를) 직접 업데이트하는 방법을 보여줍니다.
지정된 패키지 버전으로 기존 정의를 업데이트하거나 존재하지 않는 요구 사항을 pip_requirements.txt 파일에 추가할 수 있습니다. 이 파일은 지정된 model_uri 위치에 있는 MLflow 모델 아티팩트 내에 있습니다.
from mlflow.models.model import update_model_requirements
update_model_requirements(
model_uri=model_uri,
operation="add",
requirement_list=["pillow==10.2.0", "scipy==1.12.0"],
)
배포 전에 모델 입력 유효성 검사
엔드포인트를 제공하는 모델에는 특수한 형식의 JSON 입력이 필요합니다. MLflow에서 validate_serving_input을(를) 사용하여 배포 전에 모델 입력이 서비스 엔드포인트에서 작동하는지 확인할 수 있습니다.
다음은 모델이 유효한 입력 예제와 함께 기록되는 경우 실행의 아티팩트 탭에서 자동 생성된 코드의 예입니다.
from mlflow.models import validate_serving_input
model_uri = 'runs:/<run_id>/<artifact_path>'
serving_payload = """{
"messages": [
{
"content": "How many product categories are there?",
"role": "user"
}
]
}
"""
# Validate the serving payload works on the model
validate_serving_input(model_uri, serving_payload)
API를 사용하여 convert_input_example_to_serving_input 유효한 JSON 서비스 입력을 생성하여 기록된 모델에 대해 입력 예제를 테스트할 수도 있습니다.
from mlflow.models import validate_serving_input
from mlflow.models import convert_input_example_to_serving_input
model_uri = 'runs:/<run_id>/<artifact_path>'
# Define INPUT_EXAMPLE with your own input example to the model
# A valid input example is a data instance suitable for pyfunc prediction
serving_payload = convert_input_example_to_serving_input(INPUT_EXAMPLE)
# Validate the serving payload works on the model
validate_serving_input(model_uri, serving_payload)
모델 서빙 수동 테스트
다음 단계를 사용하여 모델의 서비스 동작을 수동으로 테스트할 수 있습니다.
- Notebook을 열어 머신러닝용 Databricks 런타임이 아닌 일반 Databricks 런타임 버전을 사용하는 다목적 클러스터에 연결합니다.
- MLflow를 사용하여 모델을 로드하고 여기에서 디버깅을 시도합니다.
PC에서 로컬로 모델을 로드하고 여기에서 디버그할 수도 있습니다. 다음을 사용하여 모델을 로컬로 로드합니다.
import os
import mlflow
os.environ["MLFLOW_TRACKING_URI"] = "databricks://PROFILE"
ARTIFACT_URI = "model_uri"
if '.' in ARTIFACT_URI:
mlflow.set_registry_uri('databricks-uc')
local_path = mlflow.artifacts.download_artifacts(ARTIFACT_URI)
print(local_path)
conda env create -f local_path/artifact_path/conda.yaml
conda activate mlflow-env
mlflow.pyfunc.load_model(local_path/artifact_path)