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

Support subloss logging through loss dicts #111

Merged
merged 6 commits into from
May 24, 2024
Merged

Support subloss logging through loss dicts #111

merged 6 commits into from
May 24, 2024

Conversation

ibro45
Copy link
Collaborator

@ibro45 ibro45 commented Apr 26, 2024

Description

Related Issue

Type of Change

  • 📚 Examples / docs / tutorials / dependencies update
  • 🔧 Bug fix (non-breaking change which fixes an issue)
  • 🥂 Improvement (non-breaking change which improves an existing feature)
  • 🚀 New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to change)
  • 🔐 Security fix

Checklist

  • I've read the CODE_OF_CONDUCT.md document.
  • I've read the CONTRIBUTING.md guide.
  • I've updated the code style using make codestyle.
  • I've written tests for all new methods and classes that I created.
  • I've written the docstring in Google format for all the methods and classes that I used.

Summary by CodeRabbit

  • New Features
    • Enhanced loss handling to support dictionary-based sublosses, providing a combined total loss for better transparency.
    • Improved logging to display individual sublosses, metrics, and optimizer statistics for more detailed performance insights.

@dosubot dosubot bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Apr 26, 2024
Copy link
Contributor

coderabbitai bot commented Apr 26, 2024

Walkthrough

The recent updates enhance the handling of losses in the lighter/system.py module. Specifically, when the loss is a dictionary of sublosses, these changes ensure that sublosses are summed under a "combined" key. The logging mechanisms for losses and metrics have also been refined to accommodate dictionary structures, ensuring comprehensive and accurate recording of all relevant data.

Changes

File Change Summary
lighter/system.py Updated _base_step to handle dictionary losses with a "total" key and modified return handling.
lighter/system.py Refactored _log_stats to support logging individual sublosses, metrics, and optimizer statistics.

Changes made, the code's delight,
Losses now combined just right,
Logging metrics, sublosses too,
A system refined, through and through.

🐇✨


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.

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 a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

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

CodeRabbit Configration 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.

Copy link
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: 2

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 388c9b6 and bcffb91.
Files selected for processing (1)
  • lighter/system.py (1 hunks)
Additional comments not posted (2)
lighter/system.py (2)

217-223: LGTM! The handling of loss as either a tensor or a dictionary is well-implemented.


244-250: Effective handling of different loss types in logging.

lighter/system.py Outdated Show resolved Hide resolved
lighter/system.py Outdated Show resolved Hide resolved
ibro45 and others added 3 commits May 17, 2024 12:42
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
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: 2

Out of diff range and nitpick comments (1)
lighter/system.py (1)

260-261: Optimize logging of optimizer statistics.

Consider logging optimizer statistics only once per epoch to reduce the overhead and improve performance. This can be achieved by moving the logging logic to a separate method that is called only once per epoch.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between bcffb91 and 9c2d300.
Files selected for processing (1)
  • lighter/system.py (1 hunks)
Additional comments not posted (2)
lighter/system.py (2)

208-210: Ensure robust error handling for loss dictionaries.

Consider enhancing the error message to guide the user on how to properly structure the loss dictionary. For example:

- raise ValueError("The loss dictionary must include a 'total' key that combines all sublosses. Example: {'total': combined_loss, 'subloss1': loss1, ...}")
+ raise ValueError("The loss dictionary must include a 'total' key that combines all sublosses. Example: {'total': combined_loss, 'subloss1': loss1, ...}")

253-255: Ensure consistent metric logging.

Consider adding a check to ensure that each metric in the metrics dictionary is an instance of torchmetrics.Metric to prevent runtime errors during logging:

+ if not isinstance(metric, Metric):
+     raise TypeError(f"Expected type for metric is 'Metric', got '{type(metric).__name__}' instead.")

Comment on lines +260 to +261
for name, optimizer_stat in get_optimizer_stats(self.optimizer).items():
on_epoch_log(f"{mode}/{name}", optimizer_stat)
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure robust configuration of optimizers.

Consider adding error handling for cases where the scheduler is not specified but is expected to be used. This can help prevent runtime errors and improve the robustness of the system.


Refactor dynamic method setup.

Consider refactoring the dynamic setup of methods to improve readability and maintainability. This can be achieved by separating the logic for each stage into separate methods and calling these methods from the setup method.

@ibro45 ibro45 mentioned this pull request May 23, 2024
Copy link
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: 0

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 9c2d300 and af4c595.
Files selected for processing (1)
  • lighter/system.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • lighter/system.py

@ibro45 ibro45 merged commit 2e5614b into main May 24, 2024
7 checks passed
@ibro45 ibro45 deleted the subloss branch May 24, 2024 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:M This PR changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants