<#7515 fix(ray): apply GPU accelerator tolerations...
# flyte-github
a
#7515 fix(ray): apply GPU accelerator tolerations when GPU is requested via group k8s_pod Pull request opened by pingsutw Why are the changes needed? When a Ray head/worker group requests its GPU only through the group's custom
k8s_pod
(rather than the task container resources), the pod ends up with the generic
nvidia.com/gpu:NoSchedule op=Exists
toleration but without the accelerator-specific node affinity and toleration (e.g. `k8s.amazonaws.com/accelerator=nvidia-t4:NoSchedule`), even though the group's
extended_resources
declare the accelerator. The pod then can't schedule onto the tainted accelerator nodes it targets. Root cause is an ordering problem in the ray plugin: 1.
buildGroupPodSpec
→
ToK8sPodSpec
runs
ApplyGPUNodeSelectors
, whose
podRequiresAccelerator
guard checks container GPU limits — which aren't there yet, so it silently short-circuits. 2. `buildHeadPodTemplate`/`buildWorkerPodTemplate` later merge the group's
k8s_pod
(which carries the GPU limits) via
mergeCustomPodSpec
, but the accelerator logic never runs again. 3.
AddTolerationsForExtendedResources
does run post-merge — which is why the generic
Exists
toleration appears while the device toleration doesn't. What changes were proposed in this pull request? •
buildGroupPodSpec
now also returns the group's effective GPU accelerator (task-level ⊕ execution overrides ⊕ group
extended_resources
). • `buildHeadPodTemplate`/`buildWorkerPodTemplate` re-apply
ApplyGPUNodeSelectors
right after
mergeCustomPodSpec
, mirroring how
AddTolerationsForExtendedResources
already runs post-merge. Merging earlier instead is not viable:
ToK8sPodSpec
rebuilds the spec from the task context, and the overlay merges containers by the `ray-head`/`ray-worker` names that only exist after the builders rename the primary container. •
ApplyGPUNodeSelectors
is made idempotent so the re-application is a no-op when the selectors were already applied inside `ToK8sPodSpec`: tolerations go through the existing
addTolerationInPodSpec
dedupe helper, and
AddRequiredNodeSelectorRequirements
now skips requirements already present in a term (previously it appended blindly). How was this patch tested? import asyncio import typing import ray from flyteplugins.ray.task import HeadNodeConfig, RayJobConfig, WorkerNodeConfig from kubernetes.client import V1Container, V1EnvVar, V1PodSpec, V1ResourceRequirements import flyte.remote import flyte.storage @ray.remote def f(x): return x * x # A custom pod template for the worker group: it sets a custom env var and cpu/memory on the # ``ray-worker`` container. Combined with ``requests=Resources(gpu=...)`` below, this exercises the # fix that merges requests/limits into the pod_template instead of dropping the template. worker_pod_template = flyte.PodTemplate( pod_spec=V1PodSpec( containers=[ V1Container( name="ray-worker", env=[V1EnvVar(name="RAY_WORKER_CUSTOM", value="merged-pod-template")], resources=V1ResourceRequirements( requests={"cpu": "1", "memory": "2Gi"}, limits={"cpu": "1", "memory": "2Gi"}, ), ) ] ) ) ray_config = RayJobConfig( head_node_config=HeadNodeConfig(ray_start_params={"log-color": "True"}), worker_node_config=[ WorkerNodeConfig( group_name="ray-group", replicas=1, # GPU type/count for the worker group. Change "T4:1" to whatever your cluster offers. requests=flyte.Resources(gpu="L4:1"), pod_template=worker_pod_template, ) ], runtime_env={"pip": ["numpy", "pandas"]}, enable_autoscaling=False, shutdown_after_job_finishes=True, ttl_seconds_after_finished=300, ) image = ( flyte.Image.from_debian_base(name="ray") .with_apt_packages("wget", "git") .with_pip_packages( "ray[default]==2.46.0", # Install the plugin from the PR branch so the cluster runtime matches the patched SDK. # The merge itself happens during local serialization, so also install this branch locally: # pip install --no-deps --force-reinstall \ # "git+https://github.com/flyteorg/flyte-sdk.git@fix/ray-pod-template-resources-merge#subdirectory=plugins/ray" "flyteplugins-ray @ git+https://github.com/flyteorg/flyte-sdk.git" "@fix/ray-pod-template-resources-merge#subdirectory=plugins/ray", "pip", "mypy", ) ) task_env = flyte.TaskEnvironment( name="hello_ray", resources=flyte.Resources(cpu=(1, 2), memory=("400Mi", "1000Mi")), image=image ) ray_env = flyte.TaskEnvironment( name="ray_env", plugin_config=ray_config, image=image, resources=flyte.Resources(cpu=(3, 4), memory=("3000Mi", "5000Mi")), depends_on=[task_env], ) @task_env.task() async def hello_ray(): await asyncio.sleep(20) print("Hello from the Ray task!") @ray_env.task async def hello_ray_nested(n: int = 3) -> typing.List[int]: print("running ray task") t = asyncio.create_task(hello_ray()) futures = [f.remote(i) for i in range(n)] res = ray.get(futures) await t return res if name == "__main__": flyte.init_from_config() run = flyte.run(hello_ray_nested) print("run name:", run.name) print("run url:", run.url) run.wait() action_details = flyte.remote.ActionDetails.get(run_name=run.name, name="a0") for log in action_details.pb2.attempts[-1].log_info: print(f"{log.name}: {log.uri}") • New regression test `TestBuildResourceRayGroupK8SPodGPU`: GPU requested only via the worker group's
k8s_pod
, accelerator declared via group-level and task-level
extended_resources
(both variants). Verified it fails without the ray.go change and passes with it. Also asserts the GPU-less head pod picks up no GPU scheduling constraints. • New idempotence tests for
ApplyGPUNodeSelectors
and
AddRequiredNodeSelectorRequirements
. • Full
go test ./flyteplugins/...
passes. Check all the applicable boxes • I updated the documentation accordingly. • All new and existing tests passed. • All commits are signed-off. flyteorg/flyte