mirror of
https://github.com/hanzo-ml/trainer.git
synced 2026-09-24 00:31:34 +00:00
9.9 KiB
9.9 KiB
In [ ]:
# !pip install -U kubeflowIn [ ]:
def jax_tpu_smoke_test():
import jax
import jax.numpy as jnp
# Initialize distributed JAX coordination
jax.distributed.initialize()
# Log device count
print("==========================")
print(f"Total global TPU device count: {jax.device_count()}")
print(f"Local TPU device count: {jax.local_device_count()}")
print(f"Global TPU devices details: {jax.devices()}")
print("==========================")
# Execute distributed matrix multiplication to verify JAX TPU backend
x = jnp.ones((1000, 1000))
y = (x @ x).block_until_ready()
print(f"JAX Matrix Multiplication verification successfully completed! Result shape: {y.shape}")In [ ]:
from kubeflow.trainer import TrainerClient, CustomTrainer
client = TrainerClient()In [ ]:
from kubeflow.trainer.options import RuntimePatch
from kubeflow.trainer.options.kubernetes import (
TrainingRuntimeSpecPatch,
JobSetTemplatePatch,
JobSetSpecPatch,
ReplicatedJobPatch,
JobTemplatePatch,
JobSpecPatch,
PodTemplatePatch,
PodSpecPatch,
)
# We define a GKE scheduling patch using typed SDK dataclasses
tpu_patch = RuntimePatch(
training_runtime_spec=TrainingRuntimeSpecPatch(
template=JobSetTemplatePatch(
spec=JobSetSpecPatch(
replicated_jobs=[
ReplicatedJobPatch(
name="node",
template=JobTemplatePatch(
spec=JobSpecPatch(
template=PodTemplatePatch(
spec=PodSpecPatch(
node_selector={
"cloud.google.com/gke-tpu-accelerator": "tpu-v5-lite-podslice",
"cloud.google.com/gke-tpu-topology": "2x2",
},
tolerations=[
{
"key": "google.com/tpu",
"operator": "Exists",
"effect": "NoSchedule",
}
],
)
)
)
)
)
]
)
)
)
)In [ ]:
num_tpu_per_node = 4
num_nodes = 1
job_name = client.train(
trainer=CustomTrainer(
func=jax_tpu_smoke_test,
image="us-docker.pkg.dev/cloud-tpu-images/jax-ai-image/tpu:latest",
num_nodes=num_nodes,
resources_per_node={
"google.com/tpu": num_tpu_per_node
},
env={
"JAX_PLATFORMS": "tpu,cpu",
"ENABLE_PJRT_COMPATIBILITY": "true"
}
),
runtime="jax-distributed",
options=[tpu_patch]
)
print(f"Training job '{job_name}' submitted successfully for JAX TPU execution!")In [ ]:
# Wait for the job to reach 'Running' status
client.wait_for_job_status(name=job_name, status={"Running"})
# Display step properties and active Pod metadata
for step in client.get_job(name=job_name).steps:
print(f"Step Name: {step.name}, Pod Name: {step.pod_name}, Status: {step.status}, Device Requested: {step.device}, Device Count: {step.device_count}")In [ ]:
import time
# Give JAX initialization some time to write stdout
time.sleep(10)
# Fetch and print logs from each worker replica
logs = client.get_job_logs(name=job_name)
for worker_name, log_text in logs.items():
print(f"\n=== Distributed JAX Logs for Worker Replica: {worker_name} ===")
print(log_text)
print("=======================================================")In [ ]:
# Wait for job to reach 'Succeeded' status
client.wait_for_job_status(name=job_name, status={"Succeeded"}, timeout=120)
print(f"Distributed JAX TPU TrainJob '{job_name}' finished and verified successfully!")In [ ]:
# client.delete_job(job_name)