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

Refactor cli and runner. Implement reserved config keys. Add feature to pass args to Trainer's methods. #124

Merged
merged 14 commits into from
Jun 13, 2024

Conversation

ibro45
Copy link
Collaborator

@ibro45 ibro45 commented May 29, 2024

Description

Related Issue

#99
#119 (comment)

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

  • Documentation

    • Updated command-line argument in documentation from --config_file to --config.
  • Refactor

    • Introduced a new CLI function for command-line interfaces using fire.
    • Enhanced configuration parsing and validation logic for improved reliability and ease of use.
  • Chores

    • Changed script entry point for lighter command to streamline command execution.
  • Tests

    • Updated tests to reflect changes in command-line arguments and function names for consistency.

Copy link
Contributor

coderabbitai bot commented May 29, 2024

Walkthrough

The recent changes enhance the lighter codebase significantly by updating CLI functionality. The --config_file argument is replaced with --config, and the script entry point is updated to use lighter.utils.runner:cli. The new cli() function adds improved command-line interface capabilities through fire, with configuration and validation changes for better control flow and error checking.

Changes

File Change Summary
docs/.../*.md Updated command-line argument from --config_file to --config in documentation files.
lighter/utils/logging.py Removed "lighter.utils.cli" from the SUPPRESSED_MODULES list.
lighter/utils/runner.py Introduced a new cli() function, updated parse_config(), added validate_config(), and enhanced the run() function to validate parsed config and specific overrides, including various checks and importing project folder if specified.
pyproject.toml Changed script entry point for the lighter command from "lighter.utils.cli:interface" to "lighter.utils.runner:cli".
tests/integration/test_cifar.py Replaced run_trainer_method usage with run, renamed config_file parameter to config, and updated the kwargs dictionary accordingly.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant CLI
    participant Runner
    participant ConfigParser
    participant TrainerTuner

    User->>CLI: lighter fit --config config.yaml
    CLI->>Runner: cli() function call
    Runner->>ConfigParser: parse_config(config.yaml)
    ConfigParser-->>Runner: parsed config
    Runner->>ConfigParser: validate_config(parsed config)
    ConfigParser-->>Runner: validation success
    Runner->>TrainerTuner: run(trainer_method, parsed config)
    TrainerTuner-->>Runner: training result
    Runner-->>CLI: output result
    CLI-->>User: result displayed
Loading

Poem

Amidst the code, a change is found,
Configs simplified, now more sound,
From _file to short and sweet,
Commands now easy, quite the feat.
Fire ignites the CLI's grace,
Validation finds its place,
A smarter Runner sets the pace. 🚀

Warning

Review ran into problems

Problems (1)
  • Git: Failed to clone repository. Please contact CodeRabbit support.

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

@ibro45 ibro45 marked this pull request as ready for review June 6, 2024 14:56
@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jun 6, 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: 1

Outside diff range and nitpick comments (3)
tests/integration/test_cifar.py (1)

21-21: Add a descriptive docstring to the test function.

It's good practice to include a brief description of what the test does, which can be helpful for future maintenance or for other developers who might work on this test.

docs/basics/quickstart.md (1)

Line range hint 1-1: Address markdown lint issues.

- Trailing spaces
- Multiple consecutive blank lines
- Headings should be surrounded by blank lines
- Fenced code blocks should be surrounded by blank lines
- Code block style

Please fix the markdown lint issues as per the hints provided by the static analysis tool.

Also applies to: 2-2, 17-17, 19-19, 92-92, 15-15, 135-135, 16-16, 24-24, 38-38, 84-84, 11-11, 30-30, 93-93

Tools
Markdownlint

135-135: Expected: 1; Actual: 2 (MD012, no-multiple-blanks)
Multiple consecutive blank lines

docs/index.md (1)

Line range hint 7-7: Address markdown lint issues.

- Multiple top-level headings in the same document
- Fenced code blocks should be surrounded by blank lines
- Images should have alternate text (alt text)
- Code block style

Please fix the markdown lint issues as per the hints provided by the static analysis tool.

Also applies to: 18-18, 10-10, 54-54

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between b16851b and 8e73be8.

Files selected for processing (7)
  • docs/basics/projects.md (1 hunks)
  • docs/basics/quickstart.md (1 hunks)
  • docs/index.md (1 hunks)
  • lighter/utils/logging.py (1 hunks)
  • lighter/utils/runner.py (1 hunks)
  • pyproject.toml (1 hunks)
  • tests/integration/test_cifar.py (2 hunks)
Files skipped from review due to trivial changes (2)
  • lighter/utils/logging.py
  • pyproject.toml
Additional context used
LanguageTool
docs/basics/quickstart.md

[uncategorized] ~10-~10: If this is a compound adjective that modifies the following noun, use a hyphen. (EN_COMPOUND_ADJECTIVE_INTERNAL)
Context: ...on pip install project-lighter For bleeding edge version, runpython pip install proj...


[duplication] ~24-~24: Possible typo: you repeated a word (ENGLISH_WORD_REPEAT_RULE)
Context: ...nents: - Trainer - LighterSystem ### Trainer Trainer contains all the information about runn...


[duplication] ~38-~38: Possible typo: you repeated a word (ENGLISH_WORD_REPEAT_RULE)
Context: ...nformation see here ### LighterSystem LighterSystem encapsulates all parts of a deep learni...


[uncategorized] ~41-~41: Possible missing comma found. (AI_HYDRA_LEO_MISSING_COMMA)
Context: ...riterion, etc.) This provides powerful extensibility as training experiments for classificat...

docs/basics/projects.md

[uncategorized] ~9-~9: It seems likely that a singular genitive (’s) apostrophe is missing. (AI_HYDRA_LEO_APOSTROPHE_S_XS)
Context: ...] Customization per your imagination! Lets start by looking at each of these one b...


[uncategorized] ~14-~14: Possible missing comma found. (AI_HYDRA_LEO_MISSING_COMMA)
Context: ...dataset If you are reproducing another study you often start with a pre-defined conf...


[style] ~96-~96: Using many exclamation marks might seem excessive (in this case: 5 exclamation marks for a text that’s 2135 characters long) (EN_EXCESSIVE_EXCLAMATION)
Context: ...ed to do is add it to the lighter config! But wait, how will Lighter know where y...

Markdownlint
docs/basics/quickstart.md

1-1: Expected: 0 or 2; Actual: 1 (MD009, no-trailing-spaces)
Trailing spaces


2-2: Expected: 0 or 2; Actual: 1 (MD009, no-trailing-spaces)
Trailing spaces


17-17: Expected: 0 or 2; Actual: 1 (MD009, no-trailing-spaces)
Trailing spaces


19-19: Expected: 0 or 2; Actual: 1 (MD009, no-trailing-spaces)
Trailing spaces


92-92: Expected: 0 or 2; Actual: 6 (MD009, no-trailing-spaces)
Trailing spaces


15-15: Expected: 1; Actual: 2 (MD012, no-multiple-blanks)
Multiple consecutive blank lines


135-135: Expected: 1; Actual: 2 (MD012, no-multiple-blanks)
Multiple consecutive blank lines


1-1: Expected: 1; Actual: 0; Below (MD022, blanks-around-headings)
Headings should be surrounded by blank lines


16-16: Expected: 1; Actual: 0; Below (MD022, blanks-around-headings)
Headings should be surrounded by blank lines


24-24: Expected: 1; Actual: 0; Below (MD022, blanks-around-headings)
Headings should be surrounded by blank lines


38-38: Expected: 1; Actual: 0; Below (MD022, blanks-around-headings)
Headings should be surrounded by blank lines


84-84: Expected: 1; Actual: 0; Below (MD022, blanks-around-headings)
Headings should be surrounded by blank lines


11-11: null (MD031, blanks-around-fences)
Fenced code blocks should be surrounded by blank lines


30-30: null (MD031, blanks-around-fences)
Fenced code blocks should be surrounded by blank lines


93-93: Expected: fenced; Actual: indented (MD046, code-block-style)
Code block style

docs/index.md

7-7: null (MD025, single-title, single-h1)
Multiple top-level headings in the same document


18-18: null (MD031, blanks-around-fences)
Fenced code blocks should be surrounded by blank lines


10-10: null (MD045, no-alt-text)
Images should have alternate text (alt text)


54-54: Expected: fenced; Actual: indented (MD046, code-block-style)
Code block style

docs/basics/projects.md

12-12: Expected: h2; Actual: h3 (MD001, heading-increment)
Heading levels should only increment by one level at a time


8-8: Expected: 0 or 2; Actual: 1 (MD009, no-trailing-spaces)
Trailing spaces


96-96: Expected: 0 or 2; Actual: 1 (MD009, no-trailing-spaces)
Trailing spaces


135-135: Expected: 0 or 2; Actual: 1 (MD009, no-trailing-spaces)
Trailing spaces


142-142: Expected: 0 or 2; Actual: 3 (MD009, no-trailing-spaces)
Trailing spaces


95-95: Expected: 1; Actual: 2 (MD012, no-multiple-blanks)
Multiple consecutive blank lines


103-103: Expected: 1; Actual: 2 (MD012, no-multiple-blanks)
Multiple consecutive blank lines


136-136: Expected: 1; Actual: 2 (MD012, no-multiple-blanks)
Multiple consecutive blank lines


104-104: Expected: fenced; Actual: indented (MD046, code-block-style)
Code block style

Additional comments not posted (5)
tests/integration/test_cifar.py (1)

5-5: The updates to use the new run function and the config parameter are correctly implemented.

Also applies to: 11-11, 23-25

docs/basics/quickstart.md (1)

132-132: The update to the command-line argument is correctly reflected in the documentation.

docs/index.md (1)

102-102: The update to the command-line argument is correctly reflected in the documentation.

lighter/utils/runner.py (1)

16-19: The implementation of the CLI interface and the run function are well-structured and align with the PR objectives.

Also applies to: 72-111

docs/basics/projects.md (1)

139-139: Update CLI command to match new syntax.

The change from --config_file to --config aligns with the PR's objective to simplify command syntax.

Comment on lines 33 to 35
config = kwargs.pop("config", None)
if config is None:
raise ValueError("'--config' not specified. Exiting.")
Copy link
Contributor

Choose a reason for hiding this comment

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

Improve error handling and validation.

- raise ValueError("'--config' not specified. Exiting.")
+ raise ValueError("'--config' not specified. Please provide a valid configuration file.")
- raise ValueError(f"Invalid top-level config keys: {list(invalid_keys)}. Allowed keys: {list(CONFIG_STRUCTURE.keys())}")
+ raise ValueError(f"Invalid top-level config keys: {invalid_keys}. Allowed keys are: {CONFIG_STRUCTURE.keys()}")
- raise ValueError(f"Invalid method names in 'args': {invalid_keys}. Allowed methods are: {TRAINER_METHOD_NAMES}")
+ raise ValueError(f"Invalid method names in 'args': {invalid_keys}. Allowed methods are: {TRAINER_METHOD_NAMES}")

Consider rephrasing the error messages for better clarity and consistency.

Also applies to: 63-63, 69-69

Committable suggestion was skipped due to low confidence.

@ibro45
Copy link
Collaborator Author

ibro45 commented Jun 6, 2024

https://data.safetycli.com/v/70612/97c/

Some kind of jinja2 vulnerability that the maintainers disagree with is preventing the checks from running @surajpaib

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 8e73be8 and d983a60.

Files ignored due to path filters (1)
  • poetry.lock is excluded by !**/*.lock
Files selected for processing (2)
  • lighter/utils/runner.py (1 hunks)
  • pyproject.toml (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • pyproject.toml
Additional comments not posted (5)
lighter/utils/runner.py (5)

16-20: Refactor the CLI definition to use the fire library.

This change simplifies the CLI definition by dynamically creating commands based on the TRAINER_METHOD_NAMES. It's a clean and scalable approach.


33-35: Ensure a configuration file is specified.

The addition of a check to ensure that a configuration file is specified before proceeding is a good practice to avoid runtime errors.


97-99: Validate Trainer method existence.

The validation that checks if the trainer object has the method specified is crucial to prevent runtime errors. This ensures that only valid methods are called, aligning with the robustness of the system.


105-105: Handle special keys in configuration.

Removing the 'meta' key from the configuration before saving hyperparameters is a good practice to clean up internal metadata that should not be exposed or logged.


111-111: Execute the Trainer method dynamically.

Using getattr to dynamically call a method on the trainer object based on the method name provided is a flexible approach that supports the extensibility of the Trainer class.

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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8e73be8 and 270ce0f.

Files selected for processing (1)
  • lighter/utils/runner.py (1 hunks)
Additional comments not posted (3)
lighter/utils/runner.py (3)

16-19: The implementation of the CLI using the fire library is clean and effective.


33-41: Ensure the mandatory 'config' parameter is well-documented in user guides or help sections, as this is a significant change.


72-111: The sequence of operations in the run function is well-structured and ensures robust setup before method execution.

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: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 270ce0f and 9b9853b.

Files selected for processing (1)
  • lighter/utils/runner.py (1 hunks)
Additional comments not posted (5)
lighter/utils/runner.py (5)

3-5: Imports added for partial, fire, and ConfigParser are appropriate for the new functionalities introduced.


9-9: Import of LighterSystem is necessary for the system configuration handling.


12-13: The CONFIG_STRUCTURE and TRAINER_METHOD_NAMES are well-defined and align with the new configuration validation logic.


16-19: The cli() function effectively leverages the fire library to map trainer methods to command line arguments, which simplifies the CLI interface.


44-58: The validate_config function correctly ensures that only valid keys and trainer methods are used, which is crucial for maintaining configuration integrity.

lighter/utils/runner.py Show resolved Hide resolved
Comment on lines +72 to +111
def run(method: str, **kwargs: Any):
"""Run the trainer method.

Args:
method (str): name of the Trainer method to run. ["fit", "validate", "test", "predict", "tune"].
**kwargs (Any): keyword arguments passed to the `monai.bundle.run` function.
method (str): name of the trainer method to run.
**kwargs (Any): keyword arguments that include 'config' and specific config overrides passed to `parse_config()`.
"""
# Sets the random seed to `PL_GLOBAL_SEED` env variable. If not specified, it picks a random seed.
seed_everything()

# Parse the config file(s).
# Parse and validate the config.
parser = parse_config(**kwargs)
validate_config(parser)

# Get trainer and system
trainer = parser.get_parsed_content("trainer")
# Import the project folder as a module, if specified.
project = parser.get_parsed_content("project")
if project is not None:
import_module_from_path("project", project)

# Get the main components from the parsed config.
system = parser.get_parsed_content("system")
trainer = parser.get_parsed_content("trainer")
trainer_method_args = parser.get_parsed_content(f"args#{method}", default={})

# Checks
if not isinstance(system, LighterSystem):
raise ValueError(f"Expected 'system' to be an instance of LighterSystem, got {system.__class__.__name__}.")
if not hasattr(trainer, method):
raise ValueError(f"{trainer.__class__.__name__} has no method named '{method}'.")
if any("dataloaders" in key or "datamodule" in key for key in trainer_method_args):
raise ValueError("All dataloaders should be defined as part of the LighterSystem, not passed as method arguments.")

# Save the config to checkpoints under "hyper_parameters" and log it if a logger is defined.
config = parser.get()
config.pop("_meta_")
# Save the config to model checkpoints under the "hyper_parameters" key.
config.pop("_meta_") # MONAI Bundle adds this automatically, remove it.
system.save_hyperparameters(config)
# Log the config.
if trainer.logger is not None:
trainer.logger.log_hyperparams(config)

# Run the Trainer method.
if not hasattr(trainer, method):
raise ValueError(f"Trainer has no method named {method}.")
getattr(trainer, method)(system)
# Run the trainer method.
getattr(trainer, method)(system, **trainer_method_args)
Copy link
Contributor

Choose a reason for hiding this comment

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

The run function is well-structured and includes comprehensive checks and validations to ensure the trainer method is executed correctly. However, consider adding more detailed logging for debugging purposes.

+ if trainer.logger is not None:
+     trainer.logger.debug(f"Running method: {method} with args: {trainer_method_args}")

Committable suggestion was skipped due to low confidence.

lighter/utils/runner.py Show resolved Hide resolved
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 9b9853b and ed3d891.

Files selected for processing (1)
  • pyproject.toml (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • pyproject.toml

@surajpaib surajpaib merged commit 0e1298b into main Jun 13, 2024
3 checks passed
@surajpaib surajpaib deleted the method_args branch June 13, 2024 04:05
@coderabbitai coderabbitai bot mentioned this pull request Nov 30, 2024
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants