← Back to Pull requests

fix(trainer): handle empty and memory-only container resource limits in get_container_devices #806

Open
rautaditya2606 opened this pull request with 87 lines diff across 3 files
View on GitHub
rautaditya2606 rautaditya2606 commented on Sep 26, 2026 10:28 UTC
Author

What this PR does / why we need it:

In kubeflow.trainer.backends.kubernetes.utils.get_container_devices, an unhandled generic Exception was raised whenever a container's resources.limits was empty ({}) or only contained non-compute resources (such as memory-only limits or ephemeral-storage).

In real-world training jobs:
1. Dataset or model initializer containers often specify memory limits without CPU or accelerator limits.
2. Sidecars or pods with empty limits ({}) caused get_container_devices to raise Exception: Unknown device type in the container resources: {'memory': ...}.

Because get_trainjob_initializer_step, get_trainjob_node_step, and get_runtime_trainer rely on get_container_devices, this unhandled exception crashed TrainerClient.get_job(), wait_for_job_status(), and list_jobs().

This PR fixes that by:

  • Returning None when resources.limits is empty or lacks recognized compute device keys, and logging a debug message with the limits.

  • Callers already handle None by setting device="Unknown" and device_count="Unknown", so downstream behavior remains consistent.

  • Keeping the ValueError check for multiple MIG keys intact as that represents an invalid/unsupported configuration rather than an unmapped non-compute limit.

  • Adding comprehensive unit tests covering empty limits, None, memory-only, memory+ephemeral storage, and compute devices.

  • Updating mock initializer pods in backend_test.py with memory limits to verify end-to-end TrainJob step retrieval.
  • Which issue(s) this PR fixes:

    Fixes #805

    Checklist:

  • Docs included if any changes are user facing
  • Diff: 87 lines (+81 / -6) across 3 files Reviewer: aniket-3001 Review duration: 78496m
    All Vouch review confidence checks passed
    Residual risk 0.04 = Change Risk (0.40) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 87 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.92 · 5 substantive comment(s)
    0.92
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.268
    Real PR Conversation Timeline
    8 items
    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Sep 26, 2026 10:28 UTC
    Reviewer

    Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

    google-oss-prow[bot] google-oss-prow[bot] commented on Sep 26, 2026 10:28 UTC
    Bot

    [APPROVALNOTIFIER] This PR is NOT APPROVED

    This pull-request has been approved by:
    Once this PR has been reviewed and has the lgtm label, please assign electronic-waste for approval. For more information see the Kubernetes Code Review Process.

    The full list of commands accepted by this bot can be found here.

    <details open>
    Needs approval from an approver in each of these files:

  • OWNERS
  • Approvers can indicate their approval by writing /approve in a comment
    Approvers can cancel approval by writing /approve cancel in a comment
    </details>
    <!-- META={"approvers":["electronic-waste"]} -->

    aniket-3001 aniket-3001 commented on code on Oct 09, 2026 18:39 UTC
    Code Review
    kubeflow/trainer/backends/kubernetes/utils.py:1
    @@ -76,9 +79,12 @@ def get_container_devices(
             device = constants.CPU_LABEL
             device_count = resources.limits[constants.CPU_LABEL].actual_instance
         else:
    -        raise Exception(f"Unknown device type in the container resources: {resources.limits}")
    +        _logger.debug(
    +            "No recognized compute device found in container limits: %s", resources.limits

    reproduced the crash on main and this fixes it, nice. the new tests fail without the fix too, so they cover it well.

    one question: this also changes amd.com/gpu (or any other accelerator we don't list) from raising to showing "Unknown". that seems fine to me, just wanted to check it's intended, or should AMD be recognized here at some point?

    aniket-3001 aniket-3001 commented on code on Oct 09, 2026 18:39 UTC
    Code Review
    kubeflow/trainer/backends/kubernetes/utils.py:1
    @@ -76,9 +79,12 @@ def get_container_devices(
             device = constants.CPU_LABEL
             device_count = resources.limits[constants.CPU_LABEL].actual_instance
         else:
    -        raise Exception(f"Unknown device type in the container resources: {resources.limits}")
    +        _logger.debug(
    +            "No recognized compute device found in container limits: %s", resources.limits
    +        )
    +        return None
         if device_count is None:

    I couldn't find a way to hit this one. a null limit value fails earlier when the response is parsed, so device_count always has a value by this point. fine to keep as a guard, just mentioning in case you'd rather drop it.

    aniket-3001 aniket-3001 commented on Oct 09, 2026 18:39 UTC
    Reviewer

    checked this against main and it works as described. memory-only and empty limits no longer crash get_job / list_jobs, and the mock pod change in backend_test makes those paths exercise it. also merged cleanly with current main for me, tests pass.

    rautaditya2606 rautaditya2606 commented on code on Oct 11, 2026 16:36 UTC
    Code Review
    kubeflow/trainer/backends/kubernetes/utils.py:1
    @@ -76,9 +79,12 @@ def get_container_devices(
             device = constants.CPU_LABEL
             device_count = resources.limits[constants.CPU_LABEL].actual_instance
         else:
    -        raise Exception(f"Unknown device type in the container resources: {resources.limits}")
    +        _logger.debug(
    +            "No recognized compute device found in container limits: %s", resources.limits

    yeah intended, before it just crashed on anything we don't list. AMD can be its own follow-up if maintainers want it, didn't want to widen this PR

    rautaditya2606 rautaditya2606 commented on code on Oct 11, 2026 16:36 UTC
    Code Review
    kubeflow/trainer/backends/kubernetes/utils.py:1
    @@ -76,9 +79,12 @@ def get_container_devices(
             device = constants.CPU_LABEL
             device_count = resources.limits[constants.CPU_LABEL].actual_instance
         else:
    -        raise Exception(f"Unknown device type in the container resources: {resources.limits}")
    +        _logger.debug(
    +            "No recognized compute device found in container limits: %s", resources.limits
    +        )
    +        return None
         if device_count is None:

    yep, can't be hit. dropped it in 47bf299

    Reviewers
    Assignees
    rautaditya2606 rautaditya2606
    Labels
    risk: low confidence: 89.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 39.7% (MEDIUM) · Higher = larger blast radius
    39.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.7% · Lower = rushed or shallow review
    89.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 4.1% · Safe to merge
    4.1%
    Repository