跨工作空間與平台管理 MLflow 模型

生產機器學習不僅需要訓練一個好的模型。 你需要可靠的工作流程,將模型從開發、驗證到生產環境。 Microsoft Fabric 中的跨工作空間日誌支援兩種關鍵情境:

  • 建立端對端的MLOps工作流程。 在開發工作區訓練與實驗,在測試工作區驗證,並使用標準 MLflow API 部署到生產服務工作區。 這種環境分離有助於團隊執行品質門檻,並維持從實驗到生產的清晰稽核軌跡。

  • 將現有的機器學習資產整合進Fabric。 如果你已經在 Azure Databricks、Azure Machine Learning、本地環境或其他支援 MLflow 的平台訓練模型,你可以直接將這些實驗和模型記錄到 Fabric 工作區。 你可以輕鬆將機器學習成果集中於一處,無需重建訓練流程。

跨工作空間日誌透過 synapseml-mlflow 套件運作,該套件提供相容Fabric的 MLflow 追蹤外掛。 你要用目標工作區進行認證,設定追蹤 URI,並使用標準的 MLflow 指令。

備註

跨工作空間日誌著重於 以程式碼為先的體驗。 跨工作空間情境的 UI 整合將在未來版本中提出。

先決條件

對於 Fabric 筆記本的情境,先建立一個新的筆記本,並在執行任何程式碼前附加一個湖屋。

小提示

在啟用 出站存取保護 的工作區中,支援跨工作空間日誌。 跨工作空間紀錄到不同工作區需要管理的私人端點。 在同一工作區內或外部 Fabric 日誌,無需額外設定即可運作。

安裝 MLflow 外掛

synapseml-mlflow 套件透過提供 Fabric MLflow 追蹤外掛,實現跨工作空間的日誌記錄。 根據你的環境選擇安裝指令。

備註

該 synapseml-mlflow 套件現已支援 MLflow 3。 你不再需要將 mlflow-skinny 釘選至 2.22.2 版。 如需了解 Fabric 中 MLflow 3 功能的概觀,請參閱 MLflow 3 in Fabric Data Science。

對於 Fabric 筆記本,請使用此命令來安裝包含線上筆記本相依性的套件:

%pip install -U "synapseml-mlflow[online-notebook]"

安裝後,先重新啟動核心,再執行剩餘程式碼。

將 MLflow 物件記錄到另一個 Fabric 工作區

在這種情況下,你會在一個 Fabric 工作區(來源)執行筆記本,並將實驗和模型記錄到另一個 Fabric 工作區(目標)。

設定目標工作區

透過設定 MLFLOW_TRACKING_URI 環境變數指向你的目標工作區:

import os

target_workspace_id = "<your-target-workspace-id>"
target_uri = f"sds://api.fabric.microsoft.com/v1/workspaces/{target_workspace_id}/mlflow"
os.environ["MLFLOW_TRACKING_URI"] = target_uri

記錄實驗與模型

建立一個實驗,然後記錄執行過程,記錄相關的參數、指標和模型:

import mlflow
import mlflow.sklearn
import numpy as np
from sklearn.linear_model import LogisticRegression
from mlflow.models.signature import infer_signature

# Create or set the experiment in the target workspace
EXP_NAME = "my-cross-workspace-experiment"
MODEL_NAME = "my-cross-workspace-model"
mlflow.set_experiment(EXP_NAME)

with mlflow.start_run() as run:
    lr = LogisticRegression()
    X = np.array([-2, -1, 0, 1, 2, 1]).reshape(-1, 1)
    y = np.array([0, 0, 1, 1, 1, 0])
    lr.fit(X, y)

    score = lr.score(X, y)
    signature = infer_signature(X, y)

    mlflow.log_params({
        "objective": "classification",
        "learning_rate": 0.05,
    })
    mlflow.log_metric("score", score)

    mlflow.sklearn.log_model(lr, "model", signature=signature)

    mlflow.register_model(
        f"runs:/{run.info.run_id}/model",
        MODEL_NAME
    )

執行結束後,實驗與註冊模型會顯示在目標工作區。

在 Fabric 工作區間移動 MLflow 物件

在這種情況下,你先在來源工作區記錄物件,然後下載工件並重新登錄到目標工作區。 當你需要將訓練好的模型從開發工作區升級到生產工作區時,這種方法非常有用。

步驟 1:在原始工作區記錄物件

import mlflow
import mlflow.sklearn
import numpy as np
from sklearn.linear_model import LogisticRegression
from mlflow.models.signature import infer_signature

# Log to the current (source) workspace
EXP_NAME = "source-experiment"
mlflow.set_experiment(EXP_NAME)

with mlflow.start_run() as run:
    lr = LogisticRegression()
    X = np.array([-2, -1, 0, 1, 2, 1]).reshape(-1, 1)
    y = np.array([0, 0, 1, 1, 1, 0])
    lr.fit(X, y)

    signature = infer_signature(X, y)
    mlflow.sklearn.log_model(lr, "model", signature=signature)

    source_run_id = run.info.run_id

步驟 2:從來源執行下載工件

import mlflow.artifacts

# Download the model artifacts locally
local_artifact_path = mlflow.artifacts.download_artifacts(
    run_id=source_run_id,
    artifact_path="model"
)

步驟 3:將工件重新登錄到目標工作區

import os

target_workspace_id = "<your-target-workspace-id>"
target_uri = f"sds://api.fabric.microsoft.com/v1/workspaces/{target_workspace_id}/mlflow"
os.environ["MLFLOW_TRACKING_URI"] = target_uri

TARGET_EXP_NAME = "promoted-experiment"
TARGET_MODEL_NAME = "promoted-model"
mlflow.set_experiment(TARGET_EXP_NAME)

with mlflow.start_run() as run:
    mlflow.log_artifacts(local_artifact_path, "model")
    mlflow.register_model(
        f"runs:/{run.info.run_id}/model",
        TARGET_MODEL_NAME
    )

從外部 Fabric 記錄 MLflow 對象

你可以從任何建置模型的環境,將 MLflow 實驗和模型記錄到 Fabric 工作區,包括:

  • 本地機器。 VS Code、Jupyter 筆記本,或任何本地的 Python 環境。
  • Azure Databricks。 Azure Databricks 筆記本與作業。
  • Azure Machine Learning。 Azure Machine Learning 計算實例與管線。
  • 任何其他平台。 任何支援 Python 和 MLflow 的環境。

步驟 1:安裝套件

將套件 synapseml-mlflow 安裝在您的環境中:

pip install -U "synapseml-mlflow" "mlflow-skinny<=2.22.2"

步驟 2:使用 Fabric 認證

根據你的環境選擇一種認證方式:

在具有瀏覽器存取功能的本地開發環境中,例如 VS Code 或 Jupyter,請使用此方法。

from fabric.analytics.environment.credentials import SetFabricAnalyticsDefaultTokenCredentialsGlobally
from azure.identity import DefaultAzureCredential

SetFabricAnalyticsDefaultTokenCredentialsGlobally(
    credential=DefaultAzureCredential(exclude_interactive_browser_credential=False)
)

步驟 3:設定目標工作區並記錄 MLflow 物件

驗證後,將追蹤 URI 設定為指向你的目標 Fabric 工作區,並使用標準 MLflow API 記錄實驗與模型:

import os
import mlflow
import mlflow.sklearn
import numpy as np
from sklearn.linear_model import LogisticRegression
from mlflow.models.signature import infer_signature

target_workspace_id = "<your-target-workspace-id>"
target_uri = f"sds://api.fabric.microsoft.com/v1/workspaces/{target_workspace_id}/mlflow"
os.environ["MLFLOW_TRACKING_URI"] = target_uri

EXP_NAME = "external-experiment"
MODEL_NAME = "external-model"
mlflow.set_experiment(EXP_NAME)

with mlflow.start_run() as run:
    lr = LogisticRegression()
    X = np.array([-2, -1, 0, 1, 2, 1]).reshape(-1, 1)
    y = np.array([0, 0, 1, 1, 1, 0])
    lr.fit(X, y)

    signature = infer_signature(X, y)
    mlflow.log_metric("score", lr.score(X, y))
    mlflow.sklearn.log_model(lr, "model", signature=signature)

    mlflow.register_model(
        f"runs:/{run.info.run_id}/model",
        MODEL_NAME
    )

使用跨工作區的日誌記錄功能,同時搭配出站存取保護功能

如果你的工作區啟用了 出站存取保護,要進行跨工作區日誌傳輸,需要從來源工作區到目標工作區的 跨工作區管理的私人端點。

在同一工作空間中記錄日誌,以及從 Fabric 外部(本地機器、Azure Databricks、Azure Machine Learning)記錄的日誌,皆無需額外設定即可運作。

有關支援情境及所需設定的詳細資訊,請參閱 Fabric 資料科學的 Workspace 外出存取保護。

將套件安裝在支援 OAP 的工作區中

標準 %pip install 指令要求外站網際網路存取,而在啟用外站存取保護(OAP)的工作區中會被阻擋。 安裝套件 synapseml-mlflow 時,先從非 OAP 環境下載,然後上傳到 Lakehouse。

  1. 從有網路連線的機器下載套件 synapseml-mlflow 。

    pip download synapseml-mlflow[online-notebook]
    
  2. 將已下載的檔案上傳到您的 OAP 啟用工作區中的 Lakehouse。 將所有 .whl 檔案上傳到湖屋的 檔案 區塊(例如, /lakehouse/default/Files)。

  3. 從湖邊小徑安裝在你的 Fabric 筆記本裡:

    %pip install --no-index --find-links=/lakehouse/default/Files "synapseml-mlflow[online-notebook]>2.0.0" "mlflow-skinny<=2.22.2" --pre
    
  4. 設定追蹤 URI 使用受管理的私有端點。 如果你目前的工作區啟用了 OAP,你必須從來源工作區設定一個跨 工作空間管理的私有端點 到目標工作區。 然後,將追蹤 URI 路由私有端點。

    import os
    from fabric.analytics.environment.context import FabricContext, InternalContext
    
    context = FabricContext(workspace_id=target_workspace_id, internal_context=InternalContext(is_wspl_enabled=True))
    print(context.pbi_shared_host)
    # You need to set up and use this private endpoint if your current workspace has OAP enabled
    
    os.environ["MLFLOW_TRACKING_URI"] = f"sds://{context.pbi_shared_host}/v1/workspaces/{target_workspace_id}/mlflow"
    

已知的限制

  • 需要寫入權限。 你必須在目標工作區擁有寫入權限。
  • 不支援跨工作空間的資料譜系。 當這些物件從不同工作區記錄時,你無法查看筆記本、實驗和模型之間的關係。
  • 來源筆記本在目標工作區中看不到。 原始筆記本不會出現在目標工作區中。 在項目細節和清單頁面中,來源筆記本的連結是空的。
  • 不支援物品快照功能。 機器學習實驗或模型若被記錄到其他工作區,則不會出現在原始碼執行的筆記本項目快照中。
  • 大型語言模型不被支援。 跨工作空間日誌不支援大型語言模型(LLM)。