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

Fix dspy Module's deepcopy #1519

Merged
merged 2 commits into from
Sep 22, 2024
Merged

Conversation

chenmoneygithub
Copy link
Collaborator

The current solution has issues if dspy.BaseModule contains non-copyable instances. The proposed approach is:

  1. Get all attributes, if it is a BaseModule, try deep copying, if failed then do recursive call.
  2. If the attr is not a BaseModule, try deep copying, if failed try shallow copying, if still failed just copying over the reference.
  3. It won't hit infinite recursion because once a module has no attributes that is a BaseModule, the recursion stops.

Added unit tests to test it.

for attr, value in self.__dict__.items():
if isinstance(value, BaseModule):
try:
# Try deep copying the module to reduce recursion depth.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good catch, I should remove this part.

@okhat
Copy link
Collaborator

okhat commented Sep 22, 2024

This ... is probably OK? Do we want to issue a warning on the first deepcopy that faces trouble and tell the user about it? I worry about silent bugs. People do expect deep copy to give them a real deep copy, but maybe we can explain that it only guarantees a deep copy of DSPy-related attributes.

@okhat okhat merged commit f5773c4 into stanfordnlp:main Sep 22, 2024
5 checks passed
@chenmoneygithub
Copy link
Collaborator Author

@okhat Yea I think that would be fine, but I will keep monitoring if this is causing trouble for users.

@chenmoneygithub chenmoneygithub deleted the fix-reset-copy branch December 27, 2024 22:02
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.

2 participants