flyte icon indicating copy to clipboard operation
flyte copied to clipboard

[flyte2] pod_spec_from_resources raises KeyError for Resources(disk=...) / Resources(shm=...)

Open Daksha1611 opened this issue 2 months ago • 2 comments

Summary

flyte.extend.pod_spec_from_resources() raises KeyError for any Resources that sets disk or shm. Its internal field→k8s-key map is still keyed on ephemeral_storage, a name that no longer exists on Resources — the v2 field is disk — and it has no entry for shm at all:

https://github.com/flyteorg/flyte-sdk/blob/main/src/flyte/_resources.py#L512-L529

resources_map = {
    "cpu": "cpu",
    "memory": "memory",
    "gpu": k8s_gpu_resource_key,
    "ephemeral_storage": "ephemeral-storage",   # no such field on Resources
}
...
for resource in fields(resources):
    resource_value = getattr(resources, resource.name)
    if resource_value is not None:
        ...
        k8s_pod_resources[resources_map[resource.name]] = resource_value   # unguarded lookup

Resources declares cpu, memory, gpu, disk, shm. The loop walks every field, so the first non-None disk or shm hits a key the map does not contain.

ephemeral_storage is the only occurrence of that spelling left in src/ — it is a leftover from the flytekit v1 field name.

Reproduction

from flyte import Resources
from flyte.extend import pod_spec_from_resources

pod_spec_from_resources(requests=Resources(cpu=1, memory="1Gi"))                 # ok
pod_spec_from_resources(requests=Resources(cpu=1, memory="1Gi", disk="10Gi"))    # KeyError: 'disk'
pod_spec_from_resources(requests=Resources(cpu=1, memory="1Gi", shm="2Gi"))      # KeyError: 'shm'

Expected: disk maps to the ephemeral-storage container resource key; shm is skipped, because shared memory is surfaced as a volume through ExtendedResources rather than as a container resource.

Actual: KeyError.

Impact

pod_spec_from_resources is public — exported from flyte.extend and listed in its __all__ — and the Ray plugin calls it on every task that sets resources:

https://github.com/flyteorg/flyte-sdk/blob/main/plugins/ray/src/flyteplugins/ray/task.py#L86

resource_pod_spec = pod_spec_from_resources(
    primary_container_name=primary_container_name,
    requests=requests,
    limits=limits,
)

So a Ray task that asks for ephemeral disk — an ordinary request for a shuffle- or spill-heavy workload — fails with KeyError: 'disk' before anything is submitted. disk and shm are both documented on Resources, and shm in particular is documented as being "useful for ML data loading", which is squarely Ray's use case.

There is currently no test coverage for pod_spec_from_resources.

Correct mapping

The serde path already establishes what these two fields mean, so the fix is unambiguous:

  • disk → Resources.ResourceName.EPHEMERAL_STORAGE in _get_disk_resource_entry (src/flyte/_internal/runtime/resources_serde.py:80), i.e. the ephemeral-storage container key.
  • shm → ExtendedResources.shared_memory in get_proto_extended_resources (src/flyte/_internal/runtime/resources_serde.py:96), mounted at /dev/shm. It is not a container resource key, so it should be skipped by this function rather than mapped.

I have a patch with regression tests and will open a PR against flyteorg/flyte-sdk.

Environment

  • flyte-sdk @ 4ea3902 (current main)
  • Python 3.13

Daksha1611 avatar Sep 06 '26 15:09 Daksha1611

You are correct: pod_spec_from_resources currently raises a KeyError for Resources(disk=...) or Resources(shm=...) because its internal resources_map uses the outdated key "ephemeral_storage" instead of "disk", and does not handle "shm" at all. The function should map disk to the Kubernetes ephemeral-storage resource and skip shm, which is handled elsewhere as a volume, not a container resource. This is confirmed by the code and documentation in flyte/_resources.py and the Flyte resources documentation.

Would you like a step-by-step breakdown of the correct mapping and a sample patch?

Sources:


This is an AI-generated response and your feedback is appreciated! Please leave a 👍 if this is helpful and 👎 if it is not.

Mention @herald to ask follow-up questions. See runllm.com for more!

runllm avatar Sep 06 '26 15:09 runllm

I will take this issue. Please assign it to me.

The problem is that pod_spec_from_resources() raises a KeyError when Resources includes disk or shm. The resources_map still uses ephemeral_storage, which should be updated to disk. Additionally, shm is not mapped at all. I would update the map to replace "ephemeral_storage": "ephemeral-storage" with "disk": "ephemeral-storage". I would also ensure shm is skipped in the loop. I will verify the fix by reproducing the error and confirming it resolves with the changes.

modelpath-dev avatar Sep 10 '26 21:09 modelpath-dev