-
Notifications
You must be signed in to change notification settings - Fork 2k
Fix BootstrapFinetune example in index doc and add basic tests for bootstrap_finetune
.
#8435
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
Open
Hangzhi
wants to merge
14
commits into
stanfordnlp:main
Choose a base branch
from
Hangzhi:yiwei-dai_data/cot-fintune
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
03190bc
done
Hangzhi 4638146
done
Hangzhi 5330fbf
done
Hangzhi 6373e57
format
Hangzhi db80a11
done
Hangzhi 1e56d8a
Revert specific files to commit c3962172
Hangzhi e7e0039
fix set_lm
Hangzhi 6cd9a32
fix
Hangzhi ac91e91
fix testing
Hangzhi 83a6a04
lint
Hangzhi e57109e
link the tutorial to index sessions for clarification
Hangzhi 8a0bbc9
Merge branch 'main' into yiwei-dai_data/cot-fintune
okhat 5427474
resolve comments
Hangzhi 2a47ab7
format
Hangzhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
from unittest.mock import patch | ||
|
||
import dspy | ||
from dspy import Example | ||
from dspy.predict import Predict | ||
from dspy.teleprompt import BootstrapFinetune | ||
from dspy.utils.dummies import DummyLM | ||
|
||
|
||
# Define a simple metric function for testing | ||
def simple_metric(example, prediction, trace=None): | ||
return example.output == prediction.output | ||
|
||
|
||
examples = [ | ||
Example(input="What is the color of the sky?", output="blue").with_inputs("input"), | ||
Example(input="What does the fox say?", output="Ring-ding-ding-ding-dingeringeding!").with_inputs("input"), | ||
] | ||
trainset = [examples[0]] | ||
|
||
|
||
def test_bootstrap_finetune_initialization(): | ||
"""Test BootstrapFinetune initialization with various parameters.""" | ||
bootstrap = BootstrapFinetune(metric=simple_metric) | ||
assert bootstrap.metric == simple_metric, "Metric not correctly initialized" | ||
assert bootstrap.multitask == True, "Multitask should default to True" | ||
|
||
|
||
class SimpleModule(dspy.Module): | ||
def __init__(self, signature): | ||
super().__init__() | ||
self.predictor = Predict(signature) | ||
|
||
def forward(self, **kwargs): | ||
return self.predictor(**kwargs) | ||
|
||
|
||
def test_compile_with_predict_instances(): | ||
"""Test BootstrapFinetune compilation with Predict instances.""" | ||
# Create SimpleModule instances for student and teacher | ||
student = SimpleModule("input -> output") | ||
teacher = SimpleModule("input -> output") | ||
|
||
lm = DummyLM([{"output": "blue"}, {"output": "Ring-ding-ding-ding-dingeringeding!"}]) | ||
dspy.settings.configure(lm=lm) | ||
|
||
# Set LM for both student and teacher | ||
student.set_lm(lm) | ||
teacher.set_lm(lm) | ||
|
||
bootstrap = BootstrapFinetune(metric=simple_metric) | ||
|
||
# Mock the fine-tuning process since DummyLM doesn't support it | ||
with patch.object(bootstrap, "finetune_lms") as mock_finetune: | ||
mock_finetune.return_value = {(lm, None): lm} | ||
compiled_student = bootstrap.compile(student, teacher=teacher, trainset=trainset) | ||
|
||
assert compiled_student is not None, "Failed to compile student" | ||
assert hasattr(compiled_student, "_compiled") and compiled_student._compiled, "Student compilation flag not set" | ||
|
||
mock_finetune.assert_called_once() | ||
|
||
|
||
def test_error_handling_missing_lm(): | ||
"""Test error handling when predictor doesn't have an LM assigned.""" | ||
|
||
lm = DummyLM([{"output": "test"}]) | ||
dspy.settings.configure(lm=lm) | ||
|
||
student = SimpleModule("input -> output") | ||
# Intentionally NOT setting LM for the student module | ||
|
||
bootstrap = BootstrapFinetune(metric=simple_metric) | ||
|
||
# This should raise ValueError about missing LM and hint to use set_lm | ||
try: | ||
bootstrap.compile(student, trainset=trainset) | ||
assert False, "Should have raised ValueError for missing LM" | ||
except ValueError as e: | ||
assert "does not have an LM assigned" in str(e) | ||
assert "set_lm" in str(e) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chenmoneygithub let me know if there are better way refer to another doc page.