|
| 1 | +# Copyright 2022 The DLRover Authors. All rights reserved. |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +# See the License for the specific language governing permissions and |
| 12 | +# limitations under the License. |
| 13 | + |
| 14 | +import os |
| 15 | +import time |
| 16 | + |
| 17 | +from dlrover.python.common.constants import ( |
| 18 | + DistributionStrategy, |
| 19 | + NodeEnv, |
| 20 | + NodeType, |
| 21 | + OptimizeMode, |
| 22 | +) |
| 23 | +from dlrover.python.common.log import default_logger as logger |
| 24 | +from dlrover.python.common.node import NodeGroupResource, NodeResource |
| 25 | +from dlrover.python.scheduler.job import ElasticJob, JobArgs, NodeArgs |
| 26 | +from dlrover.python.scheduler.kubernetes import ( |
| 27 | + _dlrover_context, |
| 28 | + convert_cpu_to_decimal, |
| 29 | + convert_memory_to_mb, |
| 30 | + k8sClient, |
| 31 | +) |
| 32 | + |
| 33 | +CONFIGMAP_SUFFIX = "-dlrover-conf" |
| 34 | + |
| 35 | + |
| 36 | +class VolcanoElasticJob(ElasticJob): |
| 37 | + def __init__(self, job_name, namespace): |
| 38 | + self._namespace = namespace |
| 39 | + self._job_name = job_name |
| 40 | + |
| 41 | + def get_node_name(self, type, id): |
| 42 | + return "pod-name" |
| 43 | + |
| 44 | + def get_node_service_addr(self, type, id): |
| 45 | + return "" |
| 46 | + |
| 47 | + |
| 48 | +class VolcanoJobArgs(JobArgs): |
| 49 | + def __init__(self, platform, namespace, job_name): |
| 50 | + super(VolcanoJobArgs, self).__init__(platform, namespace, job_name) |
| 51 | + |
| 52 | + def initilize(self): |
| 53 | + self.user = os.getenv("USER", "") |
| 54 | + k8s_client = k8sClient.singleton_instance(self.namespace) |
| 55 | + |
| 56 | + # Get parameters from configmap |
| 57 | + configmap = self._retry_to_get_configmap(k8s_client) |
| 58 | + self.job_uuid = os.getenv(NodeEnv.JOB_UID, "") |
| 59 | + self.distribution_strategy = configmap.data.get( |
| 60 | + "distributionStrategy", DistributionStrategy.ALLREDUCE |
| 61 | + ) |
| 62 | + self.optimizeMode = configmap.data.get( |
| 63 | + "optimizeMode", OptimizeMode.SINGLE_JOB |
| 64 | + ) |
| 65 | + |
| 66 | + # Get parameters from volcano job |
| 67 | + vcjob = self._retry_to_get_vcjob(k8s_client) |
| 68 | + for task in vcjob["spec"]["tasks"]: |
| 69 | + if task["name"] == NodeType.WORKER: |
| 70 | + restart_policy = task["template"]["spec"].get( |
| 71 | + "restartPolicy", "" |
| 72 | + ) |
| 73 | + self.relaunch_always = restart_policy == "Always" |
| 74 | + |
| 75 | + num = int(task.get("replicas", 0)) |
| 76 | + assert len(task["template"]["spec"]["containers"]) == 1 |
| 77 | + container = task["template"]["spec"]["containers"][0] |
| 78 | + resources = container.get("resources", {}) |
| 79 | + requests = resources.get("requests", {}) |
| 80 | + cpu = convert_cpu_to_decimal(requests.get("cpu", 0)) |
| 81 | + if "memory" in requests: |
| 82 | + memory = convert_memory_to_mb(requests["memory"]) |
| 83 | + else: |
| 84 | + memory = 0 |
| 85 | + gpu_type = None |
| 86 | + gpu_num = 0 |
| 87 | + for k, v in requests.items(): |
| 88 | + if "nvidia.com" in k: |
| 89 | + gpu_type = k |
| 90 | + gpu_num = int(v) |
| 91 | + group_resource = NodeGroupResource( |
| 92 | + num, |
| 93 | + NodeResource( |
| 94 | + cpu=cpu, |
| 95 | + memory=memory, |
| 96 | + gpu_type=gpu_type, |
| 97 | + gpu_num=gpu_num, |
| 98 | + ), |
| 99 | + ) |
| 100 | + self.node_args[task["name"]] = NodeArgs( |
| 101 | + group_resource, |
| 102 | + process_timeout=_dlrover_context.seconds_to_timeout_task_process, |
| 103 | + ) |
| 104 | + logger.info("Job args = %s", self.__dict__) |
| 105 | + |
| 106 | + def _retry_to_get_configmap(self, k8s_client: k8sClient): |
| 107 | + for _ in range(3): |
| 108 | + configmap = k8s_client.get_configmap( |
| 109 | + name=self.job_name + CONFIGMAP_SUFFIX, |
| 110 | + ) |
| 111 | + if configmap: |
| 112 | + return configmap |
| 113 | + else: |
| 114 | + time.sleep(5) |
| 115 | + raise ValueError("Cannot get the conifgmap %s" % self.job_name) |
| 116 | + |
| 117 | + def _retry_to_get_vcjob(self, k8s_client: k8sClient): |
| 118 | + for _ in range(3): |
| 119 | + vcjob = k8s_client.get_custom_resource( |
| 120 | + name=self.job_name, |
| 121 | + group="batch.volcano.sh", |
| 122 | + version="v1alpha1", |
| 123 | + plural="jobs", |
| 124 | + ) |
| 125 | + if vcjob: |
| 126 | + return vcjob |
| 127 | + else: |
| 128 | + time.sleep(5) |
| 129 | + raise ValueError( |
| 130 | + "Cannot get the training volcano job %s" % self.job_name |
| 131 | + ) |
0 commit comments