モデル バージョンを Workspace Model Registry から Unity Catalog に移行する
Databricks では、ガバナンスの向上、ワークスペースや環境間での簡単な共有、より柔軟な MLOps ワークフローのために、Unity Catalog のモデルを使用することをお勧めします。モデルバージョンをWorkspace Model RegistryからUnity Catalogに移行するには、Databricks を使用することをお勧めしますcopy_model_version()
from mlflow import MLflowClient
# Registry must be set to workspace registry
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 Catalog に存在しない場合は、この API 呼び出しによって作成されます。
モデルシグネチャ
Unity Catalog のモデルには署名が必要です。ワークスペース モデル バージョンに署名がない場合は、MLflow ドキュメントの指示に従って署名を作成することをお勧めしますDatabricks。
移行を簡略化するために、環境変数 MLFLOW_SKIP_SIGNATURE_CHECK_FOR_UC_REGISTRY_MIGRATION
を使用できます。この環境変数は、 copy_model_version()
を使用する場合にのみ使用できます。この環境変数を「true」に設定すると、署名は必要ありません。
署名なしで登録されたモデル バージョンには制限があります。既存のモデル バージョンのシグニチャを追加または更新するを参照してください。
import os
os.environ["MLFLOW_SKIP_SIGNATURE_CHECK_FOR_UC_REGISTRY_MIGRATION"] = "true"
既存のモデル バージョンに署名を追加するには、 MLflow のドキュメントを参照してください。
モデル バージョンを Unity Catalog モデルに移行するスクリプトの例
次のスクリプトは、ワークスペースに登録されているモデル内のすべてのモデル バージョンを移行先の Unity Catalog モデルに移行する方法を示しています。このスクリプトは、モデルのシグネチャで説明されているように、環境変数 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
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")