Skip to content

Add ability to specify default resource requests/limits for tasks via pyflyte run and pyflyte register #3229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 5, 2025

Conversation

redartera
Copy link
Contributor

@redartera redartera commented Apr 14, 2025

Why are the changes needed?

When registering tasks with Flyte, users often need flexibility to specify resource requirements at registration time without modifying code. This is particularly useful in scenarios where:

  1. Users want to test workflows with varied resource configurations
  2. Teams need to quickly adjust resources for existing tasks without code changes during rapid development phases.

Currently, the only options are to specify resources explicitly in task definitions or rely on cluster-level defaults, which lacks flexibility for selective overrides.

What changes were proposed in this pull request?

This PR introduces the ability to specify default resource requests and limits for tasks that don't have resources explicitly defined. The implementation:

  1. Adds a default_resources parameter to SerializationSettings that, if set, applies to tasks with unspecified resources
  2. Implements CLI options --resource-requests and --resource-limits for both pyflyte run and pyflyte register commands
  3. Adds a resource specification parser (resource_callback) that handles comma-separated key-value pairs
  4. Ensures tasks with explicitly defined resources are not overridden
  5. Updates the serialization pipeline to apply default resources during task registration

Example CLI usage:

# Specify resource requests only
pyflyte run --remote --resource-requests 'cpu=1,mem=2Gi,gpu=1' workflow.py my_workflow

# Specify both requests and limits
pyflyte register --resource-requests 'cpu=500m,mem=1Gi' --resource-limits 'cpu=1,mem=2Gi' package

# Register with resource overrides
pyflyte register --resource-requests 'cpu=1300m,mem=1100Mi' --image myimage --project myproject --domain development workflow.py

How was this patch tested?

Added comprehensive unit and integration tests:

  1. Tests for the resource specification parser in tests/flytekit/unit/interaction/test_click_types.py
  2. Integration tests in tests/flytekit/integration/remote/test_remote.py for both:
    • pyflyte register with resource overrides
    • pyflyte run --remote with resource overrides
  3. Tests verify that the registered tasks have the correct resource specifications applied
  4. Tests ensure proper error handling for invalid resource specifications (wrong separators, invalid keys, duplicate keys)

The integration tests verify end-to-end functionality by:

  • Registering workflows with resource overrides
  • Fetching the created tasks from the remote
  • Validating that the task templates contain the expected resource specifications

Check all the applicable boxes

  • I updated the documentation accordingly.
  • All new and existing tests passed.
  • All commits are signed-off.

Summary by Bito

This pull request enhances the Flyte framework by allowing users to specify default resource requests and limits for tasks during registration and execution. It introduces new CLI options for `pyflyte run` and `pyflyte register`, along with improved help text for clarity. Comprehensive tests ensure the new functionality operates correctly and handles errors effectively.

@flyte-bot
Copy link
Contributor

flyte-bot commented Apr 14, 2025

Code Review Agent Run Status

  • Limitations and other issues: ❌ Failure - Bito Automatic Review Skipped - Draft PR

    Bito didn't auto-review because this pull request is in draft status.
    To trigger review, mark the PR as ready or type /review in the comment and save.
    You can change draft PR review settings here, or contact the agent instance creator at [email protected].

Copy link

codecov bot commented Apr 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.67%. Comparing base (c085352) to head (79035a0).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3229      +/-   ##
==========================================
+ Coverage   83.58%   87.67%   +4.08%     
==========================================
  Files           3       88      +85     
  Lines         195     4267    +4072     
==========================================
+ Hits          163     3741    +3578     
- Misses         32      526     +494     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@flyte-bot
Copy link
Contributor

flyte-bot commented Apr 14, 2025

Code Review Agent Run Status

  • Limitations and other issues: ❌ Failure - Bito Automatic Review Skipped - Draft PR

    Bito didn't auto-review because this pull request is in draft status.
    To trigger review, mark the PR as ready or type /review in the comment and save.
    You can change draft PR review settings here, or contact the agent instance creator at [email protected].

@flyte-bot
Copy link
Contributor

flyte-bot commented Apr 14, 2025

Code Review Agent Run #85e95a

Actionable Suggestions - 0
Review Details
  • Files reviewed - 10 · Commit Range: 0eb6200..74afde8
    • flytekit/clis/sdk_in_container/register.py
    • flytekit/clis/sdk_in_container/run.py
    • flytekit/configuration/__init__.py
    • flytekit/core/python_auto_container.py
    • flytekit/interaction/click_types.py
    • flytekit/remote/remote.py
    • flytekit/tools/repo.py
    • tests/flytekit/unit/core/test_context_manager.py
    • tests/flytekit/unit/core/test_serialization.py
    • tests/flytekit/unit/interaction/test_click_types.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

Refer to the documentation for additional commands.

Configuration

This repository uses code_review_bito You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@flyte-bot
Copy link
Contributor

flyte-bot commented Apr 14, 2025

Changelist by Bito

This pull request implements the following key changes.

Key Change Files Impacted
New Feature - CLI Enhancements for Default Resources

register.py - Added '--default-resources' CLI option and updated resource spec parsing; adjusted import structure.

run.py - Introduced support for '--default-resources' in run command and ensured proper parameter passing.

click_types.py - Implemented resource_spec_callback to parse default resource strings and updated associated test cases.

Feature Improvement - Resource Default Settings Implementation

__init__.py - Integrated default_resources into configuration settings with updated registration parameters.

python_auto_container.py - Added logic to override task resource specs when not explicitly set.

remote.py - Propagated default_resources parameter for remote task and workflow registrations.

repo.py - Updated repository functions to include default resource specifications during task registration.

Testing - Enhanced Test Coverage for Default Resources

test_remote.py - Introduced integration tests validating default resource overrides in both register and run commands.

test_context_manager.py - Incorporated tests to verify the context manager handles default_resources correctly.

test_serialization.py - Added tests to check that default resource settings apply to tasks without explicit resource declarations while preserving explicit settings.

test_click_types.py - Enhanced tests for resource_spec_callback to ensure robust parsing of various resource specification formats.

@flyte-bot
Copy link
Contributor

flyte-bot commented Apr 14, 2025

Code Review Agent Run #f176ed

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: 74afde8..4bb75b7
    • tests/flytekit/integration/remote/test_remote.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

Refer to the documentation for additional commands.

Configuration

This repository uses code_review_bito You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@redartera redartera force-pushed the pyflyte-override-resources branch from 4bb75b7 to 8291b77 Compare April 15, 2025 12:40
@redartera redartera force-pushed the pyflyte-override-resources branch from 8291b77 to 59d0aec Compare April 15, 2025 12:41
@flyte-bot
Copy link
Contributor

flyte-bot commented Apr 15, 2025

Code Review Agent Run #a4a15b

Actionable Suggestions - 0
Review Details
  • Files reviewed - 11 · Commit Range: 4bb75b7..59d0aec
    • flytekit/clis/sdk_in_container/register.py
    • flytekit/clis/sdk_in_container/run.py
    • flytekit/configuration/__init__.py
    • flytekit/core/python_auto_container.py
    • flytekit/interaction/click_types.py
    • flytekit/remote/remote.py
    • flytekit/tools/repo.py
    • tests/flytekit/integration/remote/test_remote.py
    • tests/flytekit/unit/core/test_context_manager.py
    • tests/flytekit/unit/core/test_serialization.py
    • tests/flytekit/unit/interaction/test_click_types.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

Refer to the documentation for additional commands.

Configuration

This repository uses code_review_bito You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@flyte-bot
Copy link
Contributor

flyte-bot commented Apr 18, 2025

Code Review Agent Run Status

  • Limitations and other issues: ❌ Failure - Bito Automatic Review Skipped - Branch Excluded

    Bito didn't auto-review because the source or target branch is excluded from automatic reviews.
    To trigger review, type /review in the comment and save.
    You can change the settings here, or contact the agent instance creator at [email protected].

@flyte-bot
Copy link
Contributor

flyte-bot commented Apr 29, 2025

Code Review Agent Run Status

  • Limitations and other issues: ❌ Failure - Bito Code Review Skipped - No Changes

    Bito didn't review because we did not see any changes in the PR to review.
    To trigger review again, type /review in the comment and save.
    You can change the settings here, or contact the agent instance creator at [email protected].

@flyte-bot
Copy link
Contributor

flyte-bot commented May 2, 2025

Code Review Agent Run Status

  • Limitations and other issues: ❌ Failure - Bito Code Review Skipped - No Changes

    Bito didn't review because we did not see any changes in the PR to review.
    To trigger review again, type /review in the comment and save.
    You can change the settings here, or contact the agent instance creator at [email protected].

Copy link
Member

@pingsutw pingsutw left a comment

Choose a reason for hiding this comment

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

Thanks, it looks great. I think it's useful, and I'd like to get other reviewers' opinions as well

Signed-off-by: redartera <[email protected]>
@flyte-bot
Copy link
Contributor

flyte-bot commented May 4, 2025

Code Review Agent Run #7dbdeb

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: 59d0aec..e25d8d4
    • flytekit/core/python_auto_container.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers a full AI review.

Refer to the documentation for additional commands.

Configuration

This repository uses code_review_bito You can customize the agent settings here or contact your Bito workspace admin at [email protected].

Documentation & Help

AI Code Review powered by Bito Logo

@flyte-bot
Copy link
Contributor

flyte-bot commented May 7, 2025

Code Review Agent Run Status

  • Limitations and other issues: ❌ Failure - Bito Code Review Skipped - No Changes

    Bito didn't review because we did not see any changes in the PR to review.
    To trigger review again, type /review in the comment and save.
    You can change the settings here, or contact the agent instance creator at [email protected].

Signed-off-by: redartera <[email protected]>
@flyte-bot
Copy link
Contributor

Code Review Agent Run Status

  • Limitations and other issues: ❌ Failure - Bito Code Review Skipped - No Changes

    Bito didn't review because we did not see any changes in the PR to review.
    To trigger review again, type /review in the comment and save.
    You can change the settings here, or contact the agent instance creator at [email protected].

@machichima
Copy link
Member

machichima commented May 25, 2025

Hi @redartera ,

Could we use separate option instead? Like:

❯ pyflyte run file.py --cpu=1 --mem=2Gi
❯ pyflyte run file.py --cpu-limit=1 --mem-limit=2Gi

This has follow benefits:

  1. Prevent typo when using, like using "cpi" instead of "cpu", click will directly gives error
  2. We do not need to split string into parts
  3. Easier to read (in my perspective)

@redartera
Copy link
Contributor Author

redartera commented May 26, 2025

Hi @redartera ,

Could we use separate option instead? Like:

❯ pyflyte run file.py --cpu=1 --mem=2Gi
❯ pyflyte run file.py --cpu-limit=1 --mem-limit=2Gi

This has follow benefits:

  1. Prevent typo when using, like using "cpi" instead of "cpu", click will directly gives error
  2. We do not need to split string into parts
  3. Easier to read (in my perspective)

Makes sense @machichima - how about just --cpu=1 and --cpu=(1,3) to be consistent with the syntax of Resources and avoid the need for a separate click option for requests and limits?

Also, this would probably require we remove --default-resources - users would need to read the --help message to understand that this only overrides default resources and tasks with pre-defined resource specifications will remain intact

@flyte-bot
Copy link
Contributor

Bito Review Skipped - No Changes Detected

Bito didn't review this pull request because we did not detect any changes in the pull request to review.

@machichima
Copy link
Member

machichima commented Jun 1, 2025

Hi @redartera

Makes sense @machichima - how about just --cpu=1 and --cpu=(1,3) to be consistent with the syntax of Resources and avoid the need for a separate click option for requests and limits?

I still prefer separating request and limit to two, like what majority of CLI tools did (e.g. kubectl), which provide a more explicit way of setting.

kubectl ... --limits=cpu=200m,memory=512Mi --requests=cpu=0,memory=0

It's unfortunate that click cannot set a flexible nargs, otherwise being able to use the following will be the best...

--default-resources cpu=1 mem=2Gi gpu=1 --limit-resources cpu=2 mem=4Gi
--default-resources cpu=1 mem=2Gi --limit-resources cpu=2

Also, this would probably require we remove --default-resources - users would need to read the --help message to understand that this only overrides default resources and tasks with pre-defined resource specifications will remain intact

I think it's fine as long as we document it well

@redartera
Copy link
Contributor Author

Hi @redartera

Makes sense @machichima - how about just --cpu=1 and --cpu=(1,3) to be consistent with the syntax of Resources and avoid the need for a separate click option for requests and limits?

I still prefer separating request and limit to two, like what majority of CLI tools did (e.g. kubectl), which provide a more explicit way of setting.

kubectl ... --limits=cpu=200m,memory=512Mi --requests=cpu=0,memory=0

It's unfortunate that click cannot set a flexible nargs, otherwise being able to use the following will be the best...

--default-resources cpu=1 mem=2Gi gpu=1 --limit-resources cpu=2 mem=4Gi
--default-resources cpu=1 mem=2Gi --limit-resources cpu=2

Also, this would probably require we remove --default-resources - users would need to read the --help message to understand that this only overrides default resources and tasks with pre-defined resource specifications will remain intact

I think it's fine as long as we document it well

@machichima I separated requests and limits into two options as recommended, PTAL. I chose --resource-limits as opposed to just --limits to avoid confusion with another --limit CLI option that exists in multiple places.

Copy link
Member

@machichima machichima left a comment

Choose a reason for hiding this comment

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

Just did a quick walk through. Overall looks good, just some nit.

Will look into detail and try it out later

Signed-off-by: redartera <[email protected]>
@redartera redartera force-pushed the pyflyte-override-resources branch from 8d76fd5 to a5ca772 Compare June 3, 2025 21:20
Signed-off-by: redartera <[email protected]>
@machichima
Copy link
Member

Verified that it works:

image

image

Copy link
Member

@machichima machichima left a comment

Choose a reason for hiding this comment

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

Thank you for the complete tests!!

Generally, my comment is about putting the resource settings into a variable to use when setting and asserting, making it easier if we want to extend or modify the test in the future

@redartera
Copy link
Contributor Author

@machichima PTAL at 968a041

@machichima
Copy link
Member

machichima commented Jun 5, 2025

Hi @redartera ,
Just rerun the integration test as it failed. Please ping me again when the test passed

Copy link
Member

@machichima machichima left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!!

@machichima
Copy link
Member

cc @pingsutw

@pingsutw pingsutw merged commit 1a7133a into flyteorg:master Jun 5, 2025
227 of 228 checks passed
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.

4 participants