Skip to content

fix: mutable default arg and bool comparison#10297

Open
LincolnBurrows2017 wants to merge 1 commit intohiyouga:mainfrom
LincolnBurrows2017:fix/mutable-default-arg
Open

fix: mutable default arg and bool comparison#10297
LincolnBurrows2017 wants to merge 1 commit intohiyouga:mainfrom
LincolnBurrows2017:fix/mutable-default-arg

Conversation

@LincolnBurrows2017
Copy link
Copy Markdown
Contributor

Fixed 3 issues:

  1. Mutable default argument: list[str] = [] -> Optional[list[str]] = None
  2. Bool comparison: is False -> not
  3. Typo: state_b -> state_dict_b

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on enhancing the robustness and readability of the compare_model utility function. It addresses a common Python pitfall by correctly handling mutable default arguments and refactors a boolean comparison for improved clarity, ensuring more predictable and maintainable code.

Highlights

  • Mutable Default Argument: The diff_keys parameter in the compare_model function was updated from a mutable default list (list[str] = []) to Optional[list[str]] = None, with an explicit check to initialize an empty list if None is passed. This prevents unintended shared state across function calls.
  • Boolean Comparison: The boolean comparison is False was replaced with the more idiomatic and robust not operator in the compare_model function, improving code readability and adherence to best practices.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request correctly refactors the compare_model function to address a mutable default argument and improve boolean comparisons, aligning with Python best practices. I've added one suggestion to further improve the test utility by adding descriptive messages to assertions, which will enhance debuggability.

Comment on lines 41 to +44
if any(key in name for key in diff_keys):
assert torch.allclose(state_dict_a[name], state_dict_b[name], rtol=1e-4, atol=1e-5) is False
assert not torch.allclose(state_dict_a[name], state_dict_b[name], rtol=1e-4, atol=1e-5)
else:
assert torch.allclose(state_dict_a[name], state_dict_b[name], rtol=1e-4, atol=1e-5) is True
assert torch.allclose(state_dict_a[name], state_dict_b[name], rtol=1e-4, atol=1e-5)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

It's good practice to include a message with assert statements. This can be very helpful for debugging when a test fails, as it provides immediate context on what condition was not met. Consider adding descriptive messages to your assertions to improve debuggability.

Suggested change
if any(key in name for key in diff_keys):
assert torch.allclose(state_dict_a[name], state_dict_b[name], rtol=1e-4, atol=1e-5) is False
assert not torch.allclose(state_dict_a[name], state_dict_b[name], rtol=1e-4, atol=1e-5)
else:
assert torch.allclose(state_dict_a[name], state_dict_b[name], rtol=1e-4, atol=1e-5) is True
assert torch.allclose(state_dict_a[name], state_dict_b[name], rtol=1e-4, atol=1e-5)
if any(key in name for key in diff_keys):
assert not torch.allclose(state_dict_a[name], state_dict_b[name], rtol=1e-4, atol=1e-5), f"Tensors for key '{name}' should be different but are close."
else:
assert torch.allclose(state_dict_a[name], state_dict_b[name], rtol=1e-4, atol=1e-5), f"Tensors for key '{name}' should be close but are different."

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.

1 participant