Skip to content
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

skip progressbar if needed #516

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open

skip progressbar if needed #516

wants to merge 3 commits into from

Conversation

ziofil
Copy link
Collaborator

@ziofil ziofil commented Oct 30, 2024

User description

Context:
when launching jobs on the command line, using settings.PROGRESSBAR = False is not enough because it's just invisible, but some output lines are still skipped.

Description of the Change:
Entirely skips creating the ProgressBar object

Benefits:
Slightly faster optimizations with no progress bar, cleaner console output


PR Type

enhancement, bug_fix


Description

  • Added a condition to skip the creation of the ProgressBar object if settings.PROGRESSBAR is set to False, improving console output and optimization speed.
  • Refactored the optimization process by introducing a new _optimization_loop method that handles the optimization steps with an optional progress bar.
  • Updated imports to include settings for accessing the PROGRESSBAR setting.

Changes walkthrough 📝

Relevant files
Enhancement
optimizer.py
Conditional Progress Bar Creation in Optimizer                     

mrmustard/training/optimizer.py

  • Added conditional logic to skip progress bar creation if
    settings.PROGRESSBAR is False.
  • Introduced _optimization_loop method to handle optimization with
    optional progress bar.
  • Updated imports to include settings.
  • Improved console output and optimization speed by avoiding unnecessary
    progress bar creation.
  • +45/-22 

    💡 PR-Agent usage: Comment /help "your question" on any pull request to receive relevant information

    Copy link
    Contributor

    PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
    🏅 Score: 92
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Code Duplication
    The new _optimization_loop method contains code that is very similar to the original implementation. Consider further refactoring to reduce duplication.

    Error Handling
    The new implementation doesn't include explicit error handling for the optimization loop. Consider adding try-except blocks to handle potential exceptions.

    Copy link
    Contributor

    codiumai-pr-agent-pro bot commented Oct 30, 2024

    PR-Agent was enabled for this repository. To continue using it, please link your git user with your CodiumAI identity here.

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Score
    Enhancement
    Improve code structure and resource management by using a context manager consistently

    Consider using a context manager for the progress bar to ensure proper resource
    management, even when settings.PROGRESSBAR is False. This can be achieved by
    creating a dummy context manager when the progress bar is not needed.

    mrmustard/training/optimizer.py [101-106]

    -bar = ProgressBar(max_steps)
    -if settings.PROGRESSBAR:
    -    with bar:
    -        self._optimization_loop(cost_fn, trainable_params, max_steps, callbacks, bar)
    -else:
    -    self._optimization_loop(cost_fn, trainable_params, max_steps, callbacks)
    +from contextlib import nullcontext
     
    +bar = ProgressBar(max_steps) if settings.PROGRESSBAR else None
    +with bar or nullcontext():
    +    self._optimization_loop(cost_fn, trainable_params, max_steps, callbacks, bar)
    +
    • Apply this suggestion
    Suggestion importance[1-10]: 8

    Why: This suggestion enhances resource management by using a consistent context manager pattern, even when the progress bar is not needed. It improves code readability and ensures that resources are properly managed, which is a significant improvement.

    8
    Simplify method signature and improve code readability by using a default argument

    Consider using a default argument for the progress_bar parameter in the
    _optimization_loop method to simplify the method call and improve code readability.

    mrmustard/training/optimizer.py [108-110]

     def _optimization_loop(
    -    self, cost_fn, trainable_params, max_steps, callbacks, progress_bar=None
    +    self, cost_fn, trainable_params, max_steps, callbacks, progress_bar=ProgressBar(0)
     ):
    • Apply this suggestion
    Suggestion importance[1-10]: 4

    Why: While using a default argument can simplify the method call, setting a default ProgressBar with 0 steps may not be meaningful and could lead to confusion. The suggestion offers a minor improvement in readability but lacks practical utility.

    4
    Best practice
    Reduce code duplication and improve maintainability by using partial function application

    Consider using the functools.partial function to create a modified version of
    _optimization_loop with preset arguments, reducing code duplication in the _minimize
    method.

    mrmustard/training/optimizer.py [102-106]

    +from functools import partial
    +
    +optimization_func = partial(self._optimization_loop, cost_fn, trainable_params, max_steps, callbacks)
     if settings.PROGRESSBAR:
         with bar:
    -        self._optimization_loop(cost_fn, trainable_params, max_steps, callbacks, bar)
    +        optimization_func(bar)
     else:
    -    self._optimization_loop(cost_fn, trainable_params, max_steps, callbacks)
    +    optimization_func()
    • Apply this suggestion
    Suggestion importance[1-10]: 7

    Why: Using functools.partial reduces code duplication and enhances maintainability by centralizing the function call setup. This is a good practice that simplifies the code structure and reduces potential errors.

    7
    Maintainability
    Improve code organization by encapsulating related functionality within a single method

    Consider moving the cost_fn_modified flag and related logic into the
    _optimization_loop method to improve encapsulation and reduce the complexity of the
    _minimize method.

    mrmustard/training/optimizer.py [98-102]

     def _minimize(self, cost_fn, by_optimizing, max_steps, callbacks):
         # finding out which parameters are trainable from the ops
         trainable_params = self._get_trainable_params(by_optimizing)
    -    cost_fn_modified = False
    -    orig_cost_fn = cost_fn
    +    self._optimization_loop(cost_fn, trainable_params, max_steps, callbacks)
    • Apply this suggestion
    Suggestion importance[1-10]: 6

    Why: Moving the cost_fn_modified logic into _optimization_loop improves encapsulation and reduces complexity in _minimize. This enhances code organization and maintainability, though it requires careful consideration of method responsibilities.

    6

    💡 Need additional feedback ? start a PR chat

    @ziofil ziofil added the no changelog Pull request does not require a CHANGELOG entry label Oct 31, 2024
    Copy link

    codecov bot commented Oct 31, 2024

    Codecov Report

    All modified and coverable lines are covered by tests ✅

    Project coverage is 89.77%. Comparing base (3a1d8bb) to head (b1e7e95).

    Additional details and impacted files
    @@           Coverage Diff            @@
    ##           develop     #516   +/-   ##
    ========================================
      Coverage    89.77%   89.77%           
    ========================================
      Files           92       92           
      Lines         6072     6072           
    ========================================
      Hits          5451     5451           
      Misses         621      621           

    Continue to review full report in Codecov by Sentry.

    Legend - Click here to learn more
    Δ = absolute <relative> (impact), ø = not affected, ? = missing data
    Powered by Codecov. Last update 3a1d8bb...b1e7e95. Read the comment docs.

    Copy link
    Collaborator

    @apchytr apchytr left a comment

    Choose a reason for hiding this comment

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

    Thanks Filippo!

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    bug_fix enhancement New feature or request no changelog Pull request does not require a CHANGELOG entry Review effort [1-5]: 2
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    2 participants