Skip to content

Conversation

Fiona-Waters
Copy link

@Fiona-Waters Fiona-Waters commented Aug 8, 2025

RHOAIENG-29321

What this PR does / why we need it:
This PR adds support for utilising annotations to propogate checkpointing related env vars to the PyTorchJob pod. The annotations are specified in the PyTorchJob yaml (via create_job api) for example:

annotations={
        "checkpoint.config.kubeflow.org/enable-checkpointing": "true",
        "checkpoint.config.kubeflow.org/checkpointing_storage-path": "/mnt/checkpoints"
    }

The values are extracted and injected as environment variables:

env:
        - name: ENABLE_CHECKPOINTING
          value: 'true'
        - name: CHECKPOINTING_STORAGE_PATH
          value: /mnt/checkpoints

The env vars can be accessed in the training function:

os.getenv("CHECKPOINTING_STORAGE_PATH")

Which issue(s) this PR fixes (optional, in Fixes #<issue number>, #<issue number>, ... format, will close the issue(s) when PR gets merged):
Fixes #RHOAIENG-29321

Checklist:

  • Docs included if any changes are user facing

Summary by CodeRabbit

  • New Features
    • Environment variables are now automatically injected into PyTorchJob containers based on checkpoint-related annotations, making it easier to configure jobs using annotations.

Copy link

coderabbitai bot commented Aug 8, 2025

Walkthrough

A new constant for a checkpoint annotation prefix was introduced. The setPodEnv function now injects environment variables into containers based on PyTorchJob annotations with this prefix. A helper function, extractCheckpointEnvVars, was added to parse these annotations and generate environment variables accordingly.

Changes

Cohort / File(s) Change Summary
Checkpoint Annotation Env Injection
pkg/controller.v1/pytorch/envvar.go
Added CheckpointAnnotationPrefix constant. Modified setPodEnv to inject env vars from checkpoint-prefixed annotations. Added extractCheckpointEnvVars helper to process annotations and generate environment variables.

Sequence Diagram(s)

sequenceDiagram
    participant PyTorchJob
    participant Controller
    participant Pod
    participant Container

    PyTorchJob->>Controller: Submit PyTorchJob with annotations
    Controller->>Controller: extractCheckpointEnvVars(PyTorchJob)
    Controller->>Pod: Create Pod spec
    loop For each Container
        Controller->>Container: Inject env vars from checkpoint annotations
    end
    Pod->>Container: Start with injected env vars
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

In the warren of code, a prefix appears,
Checkpoints now whisper in PyTorchJob ears.
Annotations transform, with underscores and might,
Into env vars for pods, set perfectly right.
The bunnies approve, with a hop and a cheer—
Checkpoints are clearer, the purpose sincere!

Note

🔌 MCP (Model Context Protocol) integration is now available in Early Access!

Pro users can now connect to remote MCP servers under the Integrations page to get reviews and chat conversations that understand additional development context.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Fiona-Waters Fiona-Waters changed the title CARRY: Convert checkpointing annotations to env vars Convert checkpointing annotations to env vars Aug 8, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
pkg/controller.v1/pytorch/envvar.go (2)

36-38: Exported-constant comment doesn’t match identifier

Go lint expects the comment to start with the constant’s name.
Rename the comment to avoid golint noise.

- // CheckpointAnnotationEnvVarPrefix is the prefix of the environment variable name for the annotation.
+ // CheckpointAnnotationPrefix is the prefix used to detect checkpoint-config annotations.

180-196: Minor: compute once & deduplicate

extractCheckpointEnvVars iterates over annotations each time it’s called; invoked inside a loop this repeats work and returns identical slices.
Consider:

checkpointEnvVars := extractCheckpointEnvVars(pytorchjob)
envKeys := map[string]struct{}{}
for _, ev := range checkpointEnvVars { envKeys[ev.Name] = struct{}{} }

for i := range podTemplateSpec.Spec.Containers {
    // ...
    for _, ev := range checkpointEnvVars {
        if _, exists := envKeys[ev.Name]; !exists {
            podTemplateSpec.Spec.Containers[i].Env = append(podTemplateSpec.Spec.Containers[i].Env, ev)
        }
    }
}

This avoids redundant work and prevents accidental duplicates.
Not critical, but improves efficiency and robustness.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ca0ce39 and 468c0e5.

📒 Files selected for processing (1)
  • pkg/controller.v1/pytorch/envvar.go (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
pkg/controller.v1/pytorch/envvar.go (1)
pkg/apis/kubeflow.org/v1/pytorch_types.go (1)
  • PyTorchJob (56-68)

Comment on lines +56 to +63

// Inject checkpointing environment variables from annotations.
checkpointEnvVars := extractCheckpointEnvVars(pytorchjob)
if len(checkpointEnvVars) > 0 {
podTemplateSpec.Spec.Containers[i].Env = append(
podTemplateSpec.Spec.Containers[i].Env, checkpointEnvVars...)
}

Copy link

@coderabbitai coderabbitai bot Aug 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Duplicate-name env vars can break pod creation

extractCheckpointEnvVars is called for every container and its slice is blindly appended.
If an annotation produces WORLD_SIZE, RANK, etc., the later code appends the same names again, and the API server rejects the pod with “duplicate env var name”.
Guard against clashes (e.g. build a map[string]struct{} before appending or skip/override) or place the annotation-derived vars after all built-ins so they intentionally override.

🤖 Prompt for AI Agents
In pkg/controller.v1/pytorch/envvar.go around lines 56 to 63, the code appends
checkpoint environment variables to each container's Env slice without checking
for duplicates, causing pod creation to fail due to duplicate env var names. To
fix this, before appending checkpointEnvVars, build a map of existing env var
names in the container to detect duplicates, then either skip adding duplicates
or override existing entries. Alternatively, append the annotation-derived vars
after all built-in vars to intentionally override duplicates.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add something to stop built-in env variables like RANK etc from being propogated if they are added to annotations with the checkpointing prefix - if this is necessary. wdyt @efazal @astefanutti @sutaakar

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

@sutaakar
Copy link

Replaced by #75

@sutaakar sutaakar closed this Aug 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants