此篇主要講述如何使用TXF(TensorFlow Extended) library的ML整體架構,並且在CI/CD還有CT的原則下使用 Cloud Build與kubeflow pipeline。
這篇文章適用於希望調整其 CI/CD practices以將 ML 解決方案遷移到 Google Cloud 上的生產環境,並希望幫助確保其 ML pipeline的quality、maintainability和adaptability的資料科學家和 ML 工程師。
這篇會包含以下的一些主題:
MLOps
要將 ML 系統整合到production環境中,我們需要編排(orchestrate) ML pipeline中的步驟。 此外,我們需要自動執行pipeline以持續訓練模型。 要嘗試新的想法和features,我們需要在pipeline的new implementations中採用 CI/CD practices。 以下部分對 ML 中的 CI/CD 和 CT 做了一些High level的overview。
ML pipeline的自動化
在某些use cases中,訓練、驗證和部署 ML 模型的手動過程其實就足夠了。 如果我們的團隊僅管理幾個不用重新訓練或不經常更改的 ML 模型,哪這種手動方法還是有效。 然而,在實踐中,模型在現實世界中部署時經常會掛掉,因為它們無法適應環境是動態的或能看見資料動態的變化。
為了讓我們的 ML 系統適應這些變化,我們需要應用以下 MLOps 技術:
我們可以自動化 ML production pipeline,以使用新資料重新訓練我們的模型。 我們可以by on demand、on schedule、根據新資料的可用性、模型效能下降、資料統計屬性的顯著變化或基於其他條件等來觸發pipeline。
CI/CD pipeline與 CT pipeline的比較
新資料的可用性是重新訓練 ML 模型的一個觸發點。 ML pipeline的 new implementation(包括 new model architecture/feature engineering/hyperparameters)的可用性是重新執行 ML pipeline的另一個重要觸發因素。 ML pipeline的這種new implementation充當模型預測服務的新版本,例如,具有用於online serving的 REST API 的microservice。兩種情況的區別如下:
為了使用新資料訓練新的 ML 模型,需要執行之前部署的 CT pipeline。 但沒有部署新的pipeline或component; 在pipeline的末端只提供新的預測服務或新訓練的模型。
為了使用new implementation訓練新的 ML 模型,需要通過 CI/CD pipeline部署新的pipeline。
要快速部署新的 ML pipeline,我們需要設置 CI/CD pipeline。 這個pipeline負責在new implementation是可用並被批准用於各種環境(例如development, test, staging, pre-production, canary,production)時自動部署新的 ML pipeline和component。
下圖展示了 CI/CD pipeline和 ML CT pipeline之間的關係。
上圖的pipeline的output如下:
設計一個 TFX-based的ML系統
以下部分討論如何使用 TFX設計一個整合式的 ML 系統,為 ML 系統設置 CI/CD pipeline。 儘管有多種建置 ML 模型的框架,但 TFX 是一個整合的 ML 平台,用於開發和部署production ML 系統。 TFX pipeline是實現 ML 系統的一系列components。 這個TFX pipeline專為可擴展的高性能 ML 任務而設計。 這些任務包括建模、訓練、驗證、serving inference和部署管理。 TFX的key libraries如下:
TFX ML system overview
下圖由各種 TFX libraries整合成ML 系統。
上圖顯示了一個典型的TFX-based ML system. 以下的步驟說明可以是手動或是自動的pipeline:
Google Cloud的 TFX ML system
在生產環境中,system component必須在可靠的平台上大規模運作。 下圖顯示了 TFX ML pipeline的每個步驟如何使用 Google Cloud 上的託管服務運作,從而確保大規模的敏捷性、可靠性和效能。
下表描述了GCP的服務對應整體TFX-based ML system
Dataflow 是一項全託管、serverless的服務,用於在 Google Cloud 上大規模運作的 Apache Beam pipeline。 Dataflow 用於擴展以下流程:
Cloud Storage 是一種用於binary large objects的high available和durable storage。 Cloud Storage 託管在 ML pipeline執行過程中產生的artifacts,包括以下:
AI Hub 是一個企業等級的託管repository,用於discovering、sharing和reusing的AI和 ML 資產。 要存儲經過訓練和驗證的模型及其相關metadata,我們可以使用 AI Hub 作為model registry。
AI Platform 是一項託管服務,用於大規模訓練和服務 ML 模型。 AI Platform Training 不僅支援 TensorFlow、Scikit-learn 和 XGboost 模型,還支援使用客戶自己的容器在任何框架中implement的模型。 此外,還提供可擴展的、基於貝葉斯(Bayesian)優化的hyperparametering tuning。 經過訓練的模型可以作為具有 REST API 的microservice部署到 AI Platform Prediction。
使用 Kubeflow pipeline 編排 ML system
本篇介紹瞭解如何設計基於 TFX 的ML系統,以及如何在 Google Cloud 上大規模運行系統的每個component。 但是,我們需要一個orchestrator來將系統的這些不同compoenent連接在一起。 orchestrator按順序運作pipeline,並根據定好的條件自動從一個步驟跳到另一個步驟。 例如,如果評估指標滿足已經定好的thresholds,則定好的條件(condition)可能會在模型評估步驟之後執行模型上線的步驟。 編排 ML pipeline在開發和生產階段都是有效的:
ML與 Kubeflow pipeline
Kubeflow 是一個開源的Kubernetes 框架,用於開發和運行portable的 ML workload。 Kubeflow Pipelines 是一個 Kubeflow service,可以讓我們compose、orchestrate和自動化 ML 系統,其中系統的每個component都可以在 Kubeflow、Google Cloud 或其他cloud platform上運行。 Kubeflow Pipelines 平台包括以下內容:
下面說明如何建置一個kubeflow pipeline:
一組容器化的 ML tasks或component。 pipeline component是一種打包成Docker image的self-contained code。 component會take input arguments, produces output files,,並在pipeline中執行一個step。
ML tasks順序的規範,通過 Python DSL來定義。 workflow的topology是通過將upstream step的output連接到downstream step的input來定義的。 pipeline definition中的一個step會調用pipeline中的一個component。 在複雜的pipeline中,component可以循環執行多次,也可以有條件地執行。
一組pipeline input parameters,其values傳遞給pipeline的component,包括filter資料的標準以及pipeline產生的artifacts的存儲位置。
下面顯示了一個簡單的 kubeflow pipelines圖解
Kubeflow Pipeline components
對於要在pipeline中調用的component,我們需要建立一個 component op。 我們可以使用以下方法create component op:
實現lightweight Python component:這個component不需要我們為每次的code change建立新的container image,並且主要用在Notebook環境中的快速iteration。 我們可以使用 kfp.components.func_to_container_op function從 Python function建立lightweight component。
Create reusable component:這個功能要求我們的component在 component.yaml 中包含component specification。 component specification在參數、要執行的 Docker container image URL 和output描述了 Kubeflow pipeline的component。 component ops是在pipeline compilation期間使用 Kubeflow Pipelines SDK 中的 ComponentStore.load_components function從 component.yaml 自動建立的。 reusable的 component.yaml specification可以共享到 AI Hub,以便在不同的 Kubeflow Pipelines project中進行組合。
使用predefined GCP component:Kubeflow Pipelines 提供預定義的component,這些component通過提供所需的參數在 GCP 上執行各種託管服務。 這些component可幫助我們使用 BigQuery、Dataflow、Dataproc 和 AI Platform 等服務執行task。 這些預定義的 Google Cloud component也可在 AI Hub 中使用。 與使用reusable component類似,這些component ops是通過 ComponentStore.load_components 根據預定義的component specification自動建立的。 其他預定義component可用於在 Kubeflow 和其他平台中執行作業。
我們還可以使用 TFX Pipeline DSL 與TFX component。 TFX component封裝了metadata functionality。 Driver 通過查詢metadata store向executor提供metadata。 Publisher接受executor的結果並將其存儲在metadata中。 我們還可以實現與metadata具有相同integration的自定義component。 TFX 提供了一個CLI,可將pipeline的 Python code編譯為 YAML file並描述 Argo workflow。 然後我們可以將這個file submit到 Kubeflow Pipelines。
下圖顯示了在 Kubeflow Pipelines 中,containerized task如何調用其他services,例如 BigQuery jobs、AI Platform(分佈式)訓練作業和 Dataflow jobs。
Kubeflow Pipelines 可讓我們通過執行所需的 GCP services來編排和自動化production ML pipeline。 上圖中,Cloud SQL 用作 Kubeflow Pipelines 的 ML metadata store。
Kubeflow Pipelines component不僅限於在 GCP 上執行與 TFX 相關的services。 這些component可以執行任何與data相關和compute相關的服務,包括用於 Dataproc for SparkML jobs、AutoML 與其他相關的compute workloads。
Kubeflow Pipelines 中的containerizing tasks具有以下優點:
觸發與scheduling Kubeflow Pipelines
當我們將 Kubeflow pipeline部署到生產環境時,我們需要自動化來執行,具體取決於 我們在“ML與 Kubeflow pipeline”部分中討論的場景。
Kubeflow Pipelines 提供了一個 Python SDK 來以寫程式的方式操作pipeline。 kfp.Client class包括用於建立experiments以及部署和運行pipeline的 API。 通過使用 Kubeflow Pipelines SDK,我們可以使用以下service調用 Kubeflow Pipelines:
Kubeflow Pipeline同時也有內建的scheduler功能來負責重複性的pipelines.
在 Google Cloud設定 CI/CD
Kubeflow Pipelines 使我們能夠編排涉及多個步驟的 ML 系統,包括data preprocessing, model training and evaluation, model deployment。 在資料科學探索階段,Kubeflow Pipelines 有助於對整個系統進行快速實驗。 在生產階段,Kubeflow Pipelines 使我們能夠根據新資料自動執行pipeline來train or retrain ML 模型。