Use ML Diagnostics with Cluster Toolkit
Cluster Toolkit is an open-source
tool that uses customizable YAML blueprints and the gcluster CLI to provision
AI and machine learning (ML) clusters on Google Cloud.
Cluster Toolkit integrates ML
Diagnostics directly into its
Google Kubernetes Engine (GKE) blueprints so that your deployed TPU clusters are
ready to collect workload metrics, configurations, and profiles.
When you enable ML Diagnostics in a Cluster Toolkit blueprint,
such as the gke-tpu-v6e
blueprint
or the gke-tpu-7x
blueprint,
Cluster Toolkit automates the cluster configuration and
Identity and Access Management (IAM) setup steps. You don't need to manually complete the
steps in IAM
permissions or Configure
GKE or self-managed Kubernetes.
Before you begin
Before you deploy a cluster and run workloads with ML Diagnostics, complete the following prerequisites:
- Enable the Cluster Director API on your Google Cloud project. For instructions, see Enable Cluster Director API.
- Enable Observability Analytics on the
_Defaultlog bucket so that model and performance metric charts render. For instructions, see Enable Observability Analytics on Cloud Logging. - Install the required dependencies and clone the
cluster-toolkitGitHub repository. For instructions, see Set up Cluster Toolkit.
Automated ML Diagnostics configuration
Cluster Toolkit automates the in-cluster components, namespace
labels, IAM service accounts, and version checks required for ML
Diagnostics. Setting the enable_ml_diagnostics setting to true in your
blueprint is the only action required to turn on this automated setup, which
prepares your GKE cluster to inject workload metadata, authenticate pods,
and collect metrics and profiles without manual cluster configuration.
When you set the enable_ml_diagnostics setting to true in a
Cluster Toolkit GKE blueprint,
Cluster Toolkit automatically provisions and configures the
following resources:
- Managed ML Diagnostics components. Configures the
managed_machine_learning_diagnostics_configblock with theenabledfield set totrueon the GKE cluster. This configuration deploys and manages themldiagnostics-injection-webhookandmldiagnostics-connection-operatorcomponents and theMLDiagnosticsConnectioncustom resource definitions without manually installing Helm charts or thecert-manageradd-on. - Automatic namespace labeling. Creates the target workload namespace from
the
vars.user_namespacevariable and applies themanaged-mldiagnostics-gke: "true"label to the namespace. AnyJobSet,LeaderWorkerSet, orRayJobworkload deployed in that namespace automatically receives pod metadata injection. Service accounts and Workload Identity Federation for GKE. Creates the default
gke-wl-saGoogle Cloud service account and associates it with the defaultworkload-identity-k8s-saKubernetes service account in your workload namespace by using Workload Identity Federation for GKE. Cluster Toolkit grants the following predefined IAM roles on the project to the Google Cloud service account:roles/hypercomputecluster.editorroles/logging.logWriterroles/monitoring.metricWriterroles/monitoring.viewerroles/stackdriver.resourceMetadata.writerroles/storage.adminroles/artifactregistry.readerroles/container.admin
GKE version validation. Enforces a Terraform precondition check that verifies that the GKE cluster runs GKE version
1.35.0-gke.3065000or later.
Enable ML Diagnostics in Cluster Toolkit
You can verify or customize ML Diagnostics settings in a
Cluster Toolkit blueprint, deploy your GKE cluster, and
confirm that the cluster resources are ready for ML workloads. ML Diagnostics is
enabled by default across all standard GKE TPU blueprints in
Cluster Toolkit, including the gke-tpu-v4, gke-tpu-v5e,
gke-tpu-v5p, gke-tpu-v6e, and gke-tpu-7x blueprints and their -advanced
blueprint variants. For other GKE blueprints, such as GPU or
custom cluster blueprints, the enable_ml_diagnostics setting defaults to
false in the gke-cluster
module.
To enable or customize ML Diagnostics in a Cluster Toolkit blueprint, complete the following steps:
- Configure blueprint settings: define the workload service account roles, Workload Identity Federation for GKE binding, and ML Diagnostics cluster settings in your blueprint YAML file.
- Deploy the cluster: build the
gclusterbinary and provision the GKE cluster and supporting Google Cloud resources from your blueprint. - Verify the cluster configuration: connect to the deployed cluster and confirm that the workload namespace label and Kubernetes service account are active.
Configure blueprint settings
Configure the service account and cluster modules in your blueprint so that
Cluster Toolkit provisions the required IAM roles,
identity binding, and GKE version. In your blueprint YAML file,
such as the examples/gke-tpu-v6e/gke-tpu-v6e.yaml file, verify the following
module settings:
- In the
workload_service_accountmodule, include thehypercomputecluster.editorrole in theproject_roleslist. - In the
modules/scheduler/gke-clustermodule, set theenable_ml_diagnosticssetting totrue, set theconfigure_workload_identity_sasetting totrue, and set theversion_prefixsetting to"1.35.".
The following example shows the relevant blueprint configuration:
vars:
# The namespace to configure and label for running ML workloads
user_namespace: default
deployment_groups:
- group: primary
modules:
- id: workload_service_account
source: modules/project/service-account
settings:
name: gke-wl-sa
project_roles:
- logging.logWriter
- monitoring.metricWriter
- monitoring.viewer
- stackdriver.resourceMetadata.writer
- storage.objectUser
- artifactregistry.reader
- hypercomputecluster.editor
- id: gke-tpu-v6e-cluster
source: modules/scheduler/gke-cluster
use: [gke-tpu-v6e-net-0, workload_service_account]
settings:
configure_workload_identity_sa: true
namespace: $(vars.user_namespace)
enable_ml_diagnostics: true
version_prefix: "1.35."
release_channel: REGULAR
Deploy the cluster
Compile the blueprint and provision the GKE cluster, TPU node pools, service
accounts, and managed ML Diagnostics components in your Google Cloud
project. From the root directory of your cloned cluster-toolkit repository,
build the gcluster binary if needed, and then deploy your blueprint by using
the gcluster deploy
command:
test -x ./gcluster || make
./gcluster deploy \
-d examples/gke-tpu-v6e/gke-tpu-v6e-deployment.yaml \
examples/gke-tpu-v6e/gke-tpu-v6e.yaml
Verify the cluster configuration
After deployment completes, connect to the cluster to confirm that the workload namespace label and Kubernetes service account are ready for ML workloads.
Fetch your cluster credentials, and verify that the workload namespace has the
managed-mldiagnostics-gke=true label and that the workload-identity-k8s-sa
service account exists:
gcloud container clusters get-credentials \
DEPLOYMENT_NAME \
--region=REGION \
--project=PROJECT_ID
kubectl get namespace USER_NAMESPACE --show-labels
kubectl get serviceaccount -n USER_NAMESPACE
Replace the following:
DEPLOYMENT_NAME: the name of your GKE clusterREGION: the compute region for your cluster, such as theus-central1regionPROJECT_ID: your Google Cloud project IDUSER_NAMESPACE: the workload namespace in thevars.user_namespacevariable, such as thedefaultnamespace
Run a sample workload
Cluster Toolkit provides a sample workload in the
examples/gke-tpu-v6e/ml-diagnostics-sample-workload-test directory
to validate the ML Diagnostics setup on your GKE TPU cluster. The
sample directory includes the following files:
sample_workload/sample_workload.py: initializes JAX distributed execution, registers a machine learning run by calling themachinelearning_runfunction with thePROJECT_ID,REGION, andGCS_PATHenvironment variables, captures a programmatic XProf trace session, and records model quality and performance metrics.Dockerfile: packages thesample_workload.pyscript with thejax[tpu],libtpu,google-cloud-mldiagnostics, andxprofPython packages, as described in Package workload for GKE.sample_job.yaml: defines the KubernetesServiceandJobSetresources that run the sample workload on your TPU slice.
Build and push the sample container image
Package the JAX test script and ML Diagnostics SDK into a container image in
Artifact Registry so that your GKE cluster can pull and run the sample workload.
From the examples/gke-tpu-v6e/ml-diagnostics-sample-workload-test directory in
your cloned cluster-toolkit repository, create an Artifact Registry Docker
repository, and then build and push the container image:
export REGION=REGION
export PROJECT_ID=PROJECT_ID
export REPO_NAME=mldiagnostics-tests
export IMAGE_URI="${REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/sample-workload:latest"
gcloud artifacts repositories create "${REPO_NAME}" \
--repository-format=docker \
--project="${PROJECT_ID}" \
--location="${REGION}"
docker build -t "${IMAGE_URI}" .
docker push "${IMAGE_URI}"
Replace the following:
REGION: the region for your Artifact Registry repository and cluster, such as theus-central1regionPROJECT_ID: your Google Cloud project ID
Configure the sample job manifest
Customize the Kubernetes JobSet manifest to target your TPU topology, attach
the workload service account, and set your Cloud Storage profile output path.
Update the sample_job.yaml
manifest
to match your Cluster Toolkit blueprint deployment:
apiVersion: v1
kind: Service
metadata:
name: tpu-discovery-service
namespace: USER_NAMESPACE
spec:
clusterIP: None
selector:
jobset.sigs.k8s.io/jobset-name: sample-tpu-jobset
---
apiVersion: jobset.x-k8s.io/v1alpha2
kind: JobSet
metadata:
name: sample-tpu-jobset
namespace: USER_NAMESPACE
labels:
diagon-enabled: "true"
kueue.x-k8s.io/queue-name: user-queue
spec:
network:
enableDNSHostnames: true
subdomain: tpu-discovery-service
replicatedJobs:
- name: tpu-slice
replicas: 1
template:
spec:
parallelism: 1
completions: 1
completionMode: Indexed
backoffLimit: 100
template:
spec:
serviceAccountName: K8S_SERVICE_ACCOUNT_NAME
automountServiceAccountToken: true
restartPolicy: Never
nodeSelector:
cloud.google.com/gke-tpu-accelerator: TPU_ACCELERATOR
cloud.google.com/gke-tpu-topology: TPU_TOPOLOGY
containers:
- name: jax-job
image: WORKLOAD_IMAGE
env:
- name: PROJECT_ID
value: PROJECT_ID
- name: REGION
value: REGION
- name: GCS_PATH
value: GCS_BUCKET_PATH
ports:
- containerPort: 8471
- containerPort: 8431
securityContext:
privileged: true
resources:
requests:
google.com/tpu: TPU_CHIPS_PER_NODE
limits:
google.com/tpu: TPU_CHIPS_PER_NODE
Replace the following:
USER_NAMESPACE: the workload namespace from thevars.user_namespacevariable in your blueprint, such as thedefaultnamespaceK8S_SERVICE_ACCOUNT_NAME: the Kubernetes service account for Workload Identity Federation for GKE, such as the defaultworkload-identity-k8s-saservice accountTPU_ACCELERATOR: the accelerator label from your blueprint, such as thetpu-v6e-slicelabel for TPU v6e or thetpu7xlabel for TPU7xTPU_TOPOLOGY: the topology label from your blueprint, such as the4x4topology for TPU v6e or the2x2x1topology for TPU7xWORKLOAD_IMAGE: the container image URI from theIMAGE_URIenvironment variable in the preceding stepGCS_BUCKET_PATH: an existing Cloud Storage bucket path for profiles, such as thegs://example-ml-diagnostics-bucket/testspathTPU_CHIPS_PER_NODE: the number of TPU chips per node for your machine type, such as 4
Submit the job and view diagnostics
Run the sample JobSet workload on your TPU slice to generate a machine
learning run, workload metrics, and XProf profiles in the Google Cloud console.
Submit the JobSet manifest and inspect the pod logs to retrieve the MLRun
URL for the Google Cloud console:
kubectl apply -f sample_job.yaml
kubectl get all -n USER_NAMESPACE
kubectl logs POD_NAME \
-n USER_NAMESPACE
Replace POD_NAME with the name of a workload pod from
the command output that starts with the sample-tpu-jobset-tpu-slice- prefix.
After the job starts, open the MLRun URL from the pod logs to inspect your
workload configurations, metrics, and XProf profiles in the
Google Cloud console. For more information, see View machine learning runs
with ML Diagnostics.