-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1519 from chenmoneygithub/fix-reset-copy
Fix dspy Module's deepcopy
- Loading branch information
Showing
2 changed files
with
83 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import dspy | ||
import threading | ||
|
||
|
||
def test_deepcopy_basic(): | ||
signature = dspy.Signature("q -> a") | ||
cot = dspy.ChainOfThought(signature) | ||
cot_copy = cot.deepcopy() | ||
assert len(cot.parameters()) == len(cot_copy.parameters()) | ||
# Parameters should be different objects with the same values. | ||
assert id(cot.parameters()[0]) != id(cot_copy.parameters()[0]) | ||
assert cot.parameters()[0].__dict__ == cot_copy.parameters()[0].__dict__ | ||
|
||
|
||
def test_deepcopy_with_uncopyable_modules(): | ||
class CustomClass(dspy.Module): | ||
def __init__(self): | ||
self.lock = threading.Lock() # Non-copyable object. | ||
self.cot = dspy.ChainOfThought(dspy.Signature("q -> a")) | ||
|
||
model = CustomClass() | ||
model_copy = model.deepcopy() | ||
assert len(model.parameters()) == len(model_copy.parameters()) | ||
# The lock should be refer to the same object (shallow copy). | ||
assert id(model.lock) == id(model_copy.lock) | ||
# Parameters should be different objects with the same values. | ||
assert id(model.parameters()[0]) != id(model_copy.parameters()[0]) | ||
assert model.parameters()[0].__dict__ == model_copy.parameters()[0].__dict__ | ||
|
||
|
||
def test_deepcopy_with_nested_modules(): | ||
class CustomClass1(dspy.Module): | ||
def __init__(self): | ||
self.lock = threading.Lock() # Non-copyable object. | ||
self.cot = dspy.ChainOfThought(dspy.Signature("q -> a")) | ||
|
||
class CustomClass2(dspy.Module): | ||
def __init__(self): | ||
self.submodel = CustomClass1() | ||
|
||
model = CustomClass2() | ||
model_copy = model.deepcopy() | ||
assert len(model.parameters()) == len(model_copy.parameters()) | ||
# The lock should be refer to the same object (shallow copy). | ||
assert id(model.submodel.lock) == id(model_copy.submodel.lock) | ||
# Parameters should be different objects with the same values. | ||
assert id(model.parameters()[0]) != id(model_copy.parameters()[0]) | ||
assert model.parameters()[0].__dict__ == model_copy.parameters()[0].__dict__ |