次の方法で共有


ワークスペース モデル レジストリから Unity カタログにモデル バージョンを移行する

Databricks では、ガバナンスの向上、ワークスペースと環境の間の簡単な共有、柔軟性の高い MLOps ワークフローのために、Unity Catalog のモデルを使用することをお勧めします。 モデル バージョンをワークスペース モデル レジストリから Unity カタログに移行するには、Databricks では、MLflow クライアント copy_model_version()= >で3.4.0を使用することをお勧めします。

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 カタログのモデルには署名が必要です。 ワークスペース モデルのバージョンに署名がない場合は、 MLflow ドキュメントの手順に従って作成することをお勧めします。

移行を簡略化するために、環境変数 MLFLOW_SKIP_SIGNATURE_CHECK_FOR_UC_REGISTRY_MIGRATIONを使用できます。 この環境変数は、 copy_model_version() を使用する場合にのみ使用でき、MLflow バージョン 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")