다음을 통해 공유


작업 영역 모델 레지스트리에서 Unity 카탈로그로 모델 버전 마이그레이션

Databricks는 향상된 거버넌스, 작업 영역 및 환경 간 간편한 공유, 보다 유연한 MLOps 워크플로를 위해 Unity 카탈로그의 모델을 사용하는 것이 좋습니다. Unity 카탈로그로 모델 버전을 마이그레이션하기 위해 워크스페이스 모델 레지스트리에서 MLflow 클라이언트 copy_model_version()= >를 사용하여 3.4.0 하는 것을 Databricks에서 추천합니다.

import mlflow
from mlflow import MLflowClient

# Registry must be set to workspace registry
mlflow.set_registry_uri("databricks")
client = MlflowClient(registry_uri="databricks")

src_model_uri = f"models:/my_wmr_model/1"
uc_migrated_copy = client.copy_model_version(
   src_model_uri, "mycatalog.myschema.my_uc_model"
)

대상 모델이 Unity 카탈로그에 없는 경우 이 API 호출을 통해 생성됩니다.

모델 서명

Unity 카탈로그의 모델에는 서명이 필요합니다. 작업 영역 모델 버전에 서명이 없는 경우 Databricks는 MLflow 설명서의 지침에 따라 서명을 만드는 것이 좋습니다.

마이그레이션을 간소화하기 위해 환경 변수 MLFLOW_SKIP_SIGNATURE_CHECK_FOR_UC_REGISTRY_MIGRATION를 사용할 수 있습니다. 이 환경 변수는 MLflow copy_model_version() 버전 3.4.0 이상이 필요한 경우에만 사용할 수 있습니다. 이 환경 변수를 "true"설정하면 서명이 필요하지 않습니다.

서명 없이 등록된 모델 버전에는 제한이 있습니다. 기존 모델 버전의 서명 추가 또는 업데이트를 참조하세요.

import os

os.environ["MLFLOW_SKIP_SIGNATURE_CHECK_FOR_UC_REGISTRY_MIGRATION"] = "true"

기존 모델 버전에 서명을 추가하려면 MLflow 설명서를 참조하세요.

모델 버전을 Unity 카탈로그 모델로 마이그레이션하는 예제 스크립트

다음 스크립트는 작업 영역 등록 모델의 모든 모델 버전을 대상 Unity 카탈로그 모델로 마이그레이션하는 방법을 보여 줍니다. 이 스크립트는 MLFLOW_SKIP_SIGNATURE_CHECK_FOR_UC_REGISTRY_MIGRATION에 설명된 대로 환경 변수 를 "true"로 설정한다고 가정합니다.

import mlflow
from mlflow import MlflowClient
from mlflow.exceptions import MlflowException
from mlflow.models import ModelSignature
from mlflow.types.schema import Schema, ColSpec, AnyType

mlflow.set_registry_uri("databricks")
workspace_client = MlflowClient(registry_uri="databricks")
uc_client = MlflowClient(registry_uri="databricks-uc")


# Make a placeholder model that can be used to increment the version number
def make_placeholder_model() -> str:
    class _Placeholder(mlflow.pyfunc.PythonModel):
        def predict(self, ctx, x):
            return None

    with mlflow.start_run() as run:
        schema = Schema([ColSpec(AnyType())])
        model = mlflow.pyfunc.log_model(
            name="m",
            python_model=_Placeholder(),
            signature=ModelSignature(inputs=schema, outputs=schema),
        )
        return f"models:/{model.model_id}"


# Check if the source model has a particular version number
def workspace_model_exists(name: str, version: int) -> bool:
    try:
        workspace_client.get_model_version(name, str(version))
        return True
    except MlflowException as e:
        if e.error_code == "RESOURCE_DOES_NOT_EXIST":
            # Convert the RESOURCE_DOES_NOT_EXIST error into False
            return False
        # Raise all other exceptions
        raise e


# Copy model versions from a source Databricks workspace-registered model to
# a destination Databricks Unity Catalog registered model
def copy_model_versions_to_uc(src: str, dst: str) -> None:
    latest_versions = workspace_client.get_latest_versions(src)
    max_version_number = max(int(v.version) for v in latest_versions)
    placeholder_model = make_placeholder_model()

    for v in range(1, max_version_number + 1):
        if workspace_model_exists(src, v):
            workspace_client.copy_model_version(f"models:/{src}/{str(v)}", dst)
        else:
            # Create and immediately delete a placeholder model version to increment
            # the version counter on the UC model, so the version numbers on the UC
            # model match those on the workspace registered model.
            mv = uc_client.create_model_version(dst, placeholder_model)
            uc_client.delete_model_version(dst, mv.version)


copy_model_versions_to_uc("my_workspace_model", "mycatalog.myschema.my_uc_model")