|
| 1 | +"""Pathways JobSet generator and builder (Skeleton).""" |
| 2 | + |
| 3 | +import math |
| 4 | +from typing import Any, Optional |
| 5 | +from kubernetes import client |
| 6 | +import yaml |
| 7 | + |
| 8 | +# Core constants |
| 9 | +PATHWAYS_HEAD_JOB_NAME = "pathways-head" |
| 10 | +PATHWAYS_WORKER_JOB_NAME = "pathways-worker" |
| 11 | + |
| 12 | +MACHINE_TYPE_TO_TPU_VERSION_MAP = { |
| 13 | + "tpu7x-standard-4t": "tpu7x", |
| 14 | + "tpu7x": "tpu7x", |
| 15 | + "ct6e-standard-4t": "tpuv6e", |
| 16 | + "v6e": "tpuv6e", |
| 17 | + "ct6e-standard-8t": "tpuv6e1t", |
| 18 | + "ct5p-hightpu-4t": "tpuv5", |
| 19 | + "v5p": "tpuv5", |
| 20 | + "ct5lp-hightpu-4t": "tpuv5e", |
| 21 | + "v5e": "tpuv5e", |
| 22 | + "ct5lp-hightpu-8t": "tpuv5e1t", |
| 23 | + "ct4p-hightpu-4t": "tpuv4", |
| 24 | + "v4": "tpuv4", |
| 25 | +} |
| 26 | + |
| 27 | +MACHINE_TYPE_TO_GKE_ACCELERATOR_TYPE_MAP = { |
| 28 | + "tpu7x-standard-4t": "tpu7x", |
| 29 | + "tpu7x": "tpu7x", |
| 30 | + "ct6e-standard-4t": "tpu-v6e-slice", |
| 31 | + "v6e": "tpu-v6e-slice", |
| 32 | + "ct6e-standard-8t": "tpu-v6e-slice", |
| 33 | + "ct5p-hightpu-4t": "tpu-v5p-slice", |
| 34 | + "v5p": "tpu-v5p-slice", |
| 35 | + "ct5lp-hightpu-4t": "tpu-v5-lite-podslice", |
| 36 | + "v5e": "tpu-v5-lite-podslice", |
| 37 | + "ct5lp-hightpu-8t": "tpu-v5-lite-podslice", |
| 38 | + "ct4p-hightpu-4t": "tpu-v4-podslice", |
| 39 | + "v4": "tpu-v4-podslice", |
| 40 | +} |
| 41 | + |
| 42 | + |
| 43 | +class PathwaysJobSet: |
| 44 | + """Generates JobSet configuration for Pathways (Skeleton).""" |
| 45 | + |
| 46 | + def __init__( |
| 47 | + self, |
| 48 | + name: str, |
| 49 | + namespace: str, |
| 50 | + pathways_dir: str, |
| 51 | + tpu_type: str, |
| 52 | + topology: str, |
| 53 | + num_slices: int, |
| 54 | + user_pod_template: Optional[dict[str, Any]] = None, |
| 55 | + max_restarts: int = 0, |
| 56 | + jobset_api_version: str = "v1alpha2", |
| 57 | + labels: Optional[dict[str, str]] = None, |
| 58 | + annotations: Optional[dict[str, str]] = None, |
| 59 | + ): |
| 60 | + """Initializes the JobSet configuration using K8s API models.""" |
| 61 | + self._name = name |
| 62 | + self._namespace = namespace |
| 63 | + self._jobset_api_version = jobset_api_version |
| 64 | + self._max_restarts = max_restarts |
| 65 | + self._worker_replicas = num_slices |
| 66 | + self._labels = labels or {} |
| 67 | + self._annotations = annotations or {} |
| 68 | + |
| 69 | + tpu_version = MACHINE_TYPE_TO_TPU_VERSION_MAP.get(tpu_type.lower()) |
| 70 | + if not tpu_version: |
| 71 | + raise ValueError(f"Unsupported TPU type: {tpu_type}") |
| 72 | + |
| 73 | + # Build minimal head template (placeholder) |
| 74 | + self._head_job_template = self._build_minimal_job_template("head") |
| 75 | + |
| 76 | + # Build minimal worker template (placeholder) |
| 77 | + self._worker_job_template = self._build_minimal_job_template("worker") |
| 78 | + |
| 79 | + self._success_policy = None |
| 80 | + if user_pod_template: |
| 81 | + self._success_policy = { |
| 82 | + "operator": "All", |
| 83 | + "targetReplicatedJobs": [PATHWAYS_HEAD_JOB_NAME], |
| 84 | + } |
| 85 | + |
| 86 | + def _build_minimal_job_template(self, role: str) -> client.V1JobTemplateSpec: |
| 87 | + pod_spec = client.V1PodSpec( |
| 88 | + containers=[client.V1Container(name=f"placeholder-{role}", image="ubuntu")] |
| 89 | + ) |
| 90 | + job_spec = client.V1JobSpec( |
| 91 | + template=client.V1PodTemplateSpec( |
| 92 | + metadata=client.V1ObjectMeta(labels={"role": role}), |
| 93 | + spec=pod_spec |
| 94 | + ) |
| 95 | + ) |
| 96 | + return client.V1JobTemplateSpec(spec=job_spec) |
| 97 | + |
| 98 | + def _compile_config(self) -> dict[str, Any]: |
| 99 | + api_client = client.ApiClient() |
| 100 | + serialized_head = api_client.sanitize_for_serialization( |
| 101 | + self._head_job_template |
| 102 | + ) |
| 103 | + serialized_worker = api_client.sanitize_for_serialization( |
| 104 | + self._worker_job_template |
| 105 | + ) |
| 106 | + |
| 107 | + replicated_jobs = [ |
| 108 | + { |
| 109 | + "name": PATHWAYS_HEAD_JOB_NAME, |
| 110 | + "replicas": 1, |
| 111 | + "template": serialized_head, |
| 112 | + }, |
| 113 | + { |
| 114 | + "name": PATHWAYS_WORKER_JOB_NAME, |
| 115 | + "replicas": self._worker_replicas, |
| 116 | + "template": serialized_worker, |
| 117 | + }, |
| 118 | + ] |
| 119 | + |
| 120 | + jobset_config = { |
| 121 | + "apiVersion": f"jobset.sigs.k8s.io/{self._jobset_api_version}", |
| 122 | + "kind": "JobSet", |
| 123 | + "metadata": { |
| 124 | + "name": self._name, |
| 125 | + "namespace": self._namespace, |
| 126 | + }, |
| 127 | + "spec": { |
| 128 | + "failurePolicy": {"maxRestarts": self._max_restarts}, |
| 129 | + "replicatedJobs": replicated_jobs, |
| 130 | + }, |
| 131 | + } |
| 132 | + if self._labels: |
| 133 | + jobset_config["metadata"]["labels"] = self._labels |
| 134 | + if self._annotations: |
| 135 | + jobset_config["metadata"]["annotations"] = self._annotations |
| 136 | + if self._success_policy: |
| 137 | + jobset_config["spec"]["successPolicy"] = self._success_policy |
| 138 | + |
| 139 | + return jobset_config |
| 140 | + |
| 141 | + def to_dict(self) -> dict[str, Any]: |
| 142 | + return self._compile_config() |
0 commit comments