Skip to content

fix: mem and disk inference fixes#3040

Merged
johanneskoester merged 7 commits intosnakemake:mainfrom
BEFH:mem_disk_fix
Aug 27, 2024
Merged

fix: mem and disk inference fixes#3040
johanneskoester merged 7 commits intosnakemake:mainfrom
BEFH:mem_disk_fix

Conversation

@BEFH
Copy link
Copy Markdown
Contributor

@BEFH BEFH commented Aug 22, 2024

This fixes #2774.

  1. When inferring memory or disk from a string, *_mb now divides the bytes by 1e6 (SI) instead of 2^20 (binary) and takes the ceiling instead of rounding.
  2. The inference of *_mib now properly multiplies by 1.048576 instead of 0.95367431640625 (this is also equivalent to dividing by the original value, which used the wrong operation)
  3. After inferring resources from string-based resources, Snakemake now updates resources so that the inference of mem_mib and disk_mib will use the newly inferred value.
  4. mem_mib and disk_mib are now inferred regardless of whether resources are inferred from human readable values.

All space requests are now correct, but we may want to consider using humanfriendly.parse_size(value, binary=True) instead of humanfriendly.parse_size(value) for mem, since SI memory units are rarely used, but this would be an enhancement rather than a pure bugfix.

Many thanks to @hermidalc for their help.

QC

  • The PR contains a test case for the changes or the changes are already covered by an existing test case.
  • The documentation (docs/) is updated to reflect the changes or this is not necessary (e.g. if the change does neither modify the language nor the behavior or functionalities of Snakemake).

Summary by CodeRabbit

  • New Features

    • Enhanced precision for converting megabytes to mebibytes, improving accuracy in data handling.
    • Updated resource inference to ensure memory and disk values are rounded up, increasing the minimum allocations.
  • Bug Fixes

    • Streamlined resource inference logic for clarity and maintainability, reducing potential errors.
  • Tests

    • Added new Snakemake rules for testing resource allocation across different capacities, ensuring accurate output in multiple units.

@BEFH BEFH requested a review from johanneskoester as a code owner August 22, 2024 19:01
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Aug 22, 2024

Walkthrough

The changes made primarily involve adjustments to the Snakemake framework regarding resource handling and conversions between memory units. Key modifications include correcting the conversion from megabytes to mebibytes for improved accuracy, refining the resource inference logic for memory and disk allocation, and simplifying the control flow in resource application. Additionally, new test rules have been introduced to validate these changes.

Changes

Files Change Summary
snakemake/common/__init__.py Updated conversion factor in mb_to_mib function for accurate MB to MiB conversion.
snakemake/resources.py Changed memory and disk resource inference from round to math.ceil to ensure values are always rounded up.
snakemake/rules.py Refactored apply function to streamline resource inference logic, enhancing readability and maintainability.
tests/test_inferred_resources_mib/Snakefile Introduced test rules a, b, and c to validate the new resource specifications and output handling.

Assessment against linked issues

Objective Addressed Explanation
mem_mb and mem_mib do not match mem when mem is specified with unit. (#2774)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

@BEFH BEFH changed the title mem and disk inference fixes fix: mem and disk inference fixes Aug 22, 2024
Copy link
Copy Markdown
Contributor

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 9a60046 and cd111d9.

Files ignored due to path filters (3)
  • tests/test_inferred_resources_mib/expected-results/test1.out is excluded by !**/*.out
  • tests/test_inferred_resources_mib/expected-results/test2.out is excluded by !**/*.out
  • tests/test_inferred_resources_mib/expected-results/test3.out is excluded by !**/*.out
Files selected for processing (4)
  • snakemake/common/init.py (1 hunks)
  • snakemake/resources.py (2 hunks)
  • snakemake/rules.py (1 hunks)
  • tests/test_inferred_resources_mib/Snakefile (1 hunks)
Additional context used
Path-based instructions (3)
snakemake/common/__init__.py (1)

Pattern **/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

snakemake/resources.py (1)

Pattern **/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

snakemake/rules.py (1)

Pattern **/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

Ruff
snakemake/rules.py

1041-1041: Use key not in dict instead of key not in dict.keys()

Remove .keys()

(SIM118)

Additional comments not posted (7)
tests/test_inferred_resources_mib/Snakefile (3)

1-8: LGTM! Rule a is correct.

The rule correctly tests the conversion of resources specified in MB to inferred MiB and MB values.


11-18: LGTM! Rule b is correct.

The rule correctly tests the conversion of resources specified in GB to inferred MiB and MB values.


21-28: LGTM! Rule c is correct.

The rule correctly tests the conversion of resources specified in TB to inferred MiB and MB values.

snakemake/common/__init__.py (1)

60-60: LGTM! Improved accuracy in mb_to_mib conversion.

The updated conversion factor enhances precision in converting MB to MiB.

snakemake/resources.py (1)

7-7: LGTM! Enhanced precision in resource inference.

Switching to math.ceil ensures that resource values are not underestimated, aligning with the PR objectives.

Also applies to: 645-645

snakemake/rules.py (2)

1032-1034: LGTM! Refactored resource inference logic.

The refactoring enhances readability and maintainability by consolidating the logic for inferring additional resources. The functionality remains consistent with the PR objectives.


1035-1038: LGTM! Iteration over resource pairs.

The iteration over resource pairs for inferring additional resources is clear and aligns with the PR objectives.

@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@johanneskoester johanneskoester left a comment

Choose a reason for hiding this comment

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

Thanks a lot!

@johanneskoester johanneskoester merged commit 7530794 into snakemake:main Aug 27, 2024
johanneskoester pushed a commit that referenced this pull request Aug 29, 2024
🤖 I have created a release *beep* *boop*
---


##
[8.19.0](v8.18.2...v8.19.0)
(2024-08-29)


### Features

* check consistency of output file mtimes (must be newer than input
files) ([#3050](#3050))
([666cf62](666cf62))
* print host name when executing workflow
([#3048](#3048))
([b0ff787](b0ff787))


### Bug Fixes

* `mem` and `disk` inference fixes
([#3040](#3040))
([7530794](7530794))
* avoid error accessing superclass in ioutils
([#3056](#3056))
([a66a5f5](a66a5f5))
* disable execute after print compilation
([#3041](#3041))
([86ed3cd](86ed3cd))
* download report assets upon package build such that reports become
possible offline (cont. of
[#2904](#2904))
([#3026](#3026))
([e8dad4b](e8dad4b))


### Documentation

* Add 'Editor integrations' section to Installation page
([#3045](#3045))
([9a4006d](9a4006d))
* Fix typo (seesee to see)
([#3037](#3037))
([de201fb](de201fb))
* various documentation fixes
([#3052](#3052))
([b11460c](b11460c))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
vandalt pushed a commit to vandalt/snakemake that referenced this pull request Aug 29, 2024
<!--Add a description of your PR here-->

This fixes snakemake#2774.

1. When inferring memory or disk from a string, *_mb now divides the
bytes by 1e6 (SI) instead of 2^20 (binary) and takes the ceiling instead
of rounding.
2. The inference of *_mib now properly multiplies by 1.048576 instead of
0.95367431640625 (this is also equivalent to dividing by the original
value, which used the wrong operation)
3. After inferring resources from string-based resources, Snakemake now
updates `resources` so that the inference of mem_mib and disk_mib will
use the newly inferred value.
4. mem_mib and disk_mib are now inferred regardless of whether resources
are inferred from human readable values.

All space requests are now correct, but we may want to consider using
`humanfriendly.parse_size(value, binary=True)` instead of
`humanfriendly.parse_size(value)` for `mem`, since SI memory units are
rarely used, but this would be an enhancement rather than a pure bugfix.

Many thanks to @hermidalc for their help.

### QC
<!-- Make sure that you can tick the boxes below. -->

* [x] The PR contains a test case for the changes or the changes are
already covered by an existing test case.
* [x] The documentation (`docs/`) is updated to reflect the changes or
this is not necessary (e.g. if the change does neither modify the
language nor the behavior or functionalities of Snakemake).


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Enhanced precision for converting megabytes to mebibytes, improving
accuracy in data handling.
- Updated resource inference to ensure memory and disk values are
rounded up, increasing the minimum allocations.

- **Bug Fixes**
- Streamlined resource inference logic for clarity and maintainability,
reducing potential errors.

- **Tests**
- Added new Snakemake rules for testing resource allocation across
different capacities, ensuring accurate output in multiple units.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Leandro Hermida <softdev@leandrohermida.com>
vandalt pushed a commit to vandalt/snakemake that referenced this pull request Aug 29, 2024
🤖 I have created a release *beep* *boop*
---


##
[8.19.0](snakemake/snakemake@v8.18.2...v8.19.0)
(2024-08-29)


### Features

* check consistency of output file mtimes (must be newer than input
files) ([snakemake#3050](snakemake#3050))
([666cf62](snakemake@666cf62))
* print host name when executing workflow
([snakemake#3048](snakemake#3048))
([b0ff787](snakemake@b0ff787))


### Bug Fixes

* `mem` and `disk` inference fixes
([snakemake#3040](snakemake#3040))
([7530794](snakemake@7530794))
* avoid error accessing superclass in ioutils
([snakemake#3056](snakemake#3056))
([a66a5f5](snakemake@a66a5f5))
* disable execute after print compilation
([snakemake#3041](snakemake#3041))
([86ed3cd](snakemake@86ed3cd))
* download report assets upon package build such that reports become
possible offline (cont. of
[snakemake#2904](snakemake#2904))
([snakemake#3026](snakemake#3026))
([e8dad4b](snakemake@e8dad4b))


### Documentation

* Add 'Editor integrations' section to Installation page
([snakemake#3045](snakemake#3045))
([9a4006d](snakemake@9a4006d))
* Fix typo (seesee to see)
([snakemake#3037](snakemake#3037))
([de201fb](snakemake@de201fb))
* various documentation fixes
([snakemake#3052](snakemake#3052))
([b11460c](snakemake@b11460c))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.

mem_mb and mem_mib do not match mem when mem is specified with unit.

3 participants