Skip to content

Commit a122c16

Browse files
authored
Fixes for MIPRO: Don't fail silently on bootstrapping! (#8548)
* Fixes for MIPRO: If `datasets` wasn't installed, bootstrapping would fail silently in 3.0.0b{1/2} * test relax
1 parent e27cd72 commit a122c16

File tree

5 files changed

+32
-29
lines changed

5 files changed

+32
-29
lines changed

dspy/primitives/module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ def __getattribute__(self, name):
169169

170170
if forward_called_directly:
171171
logger.warning(
172-
f"Calling {self.__class__.__name__}.forward() directly is discouraged. "
173-
f"Please use {self.__class__.__name__}() instead."
172+
f"Calling module.forward(...) on {self.__class__.__name__} directly is discouraged. "
173+
f"Please use module(...) instead."
174174
)
175175

176176
return attr

dspy/propose/grounded_proposer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ def propose_instruction_for_predictor(
422422

423423
with dspy.settings.context(lm=self.prompt_model):
424424
self.prompt_model.kwargs["temperature"] = modified_temp
425-
proposed_instruction = instruction_generator.forward(
425+
proposed_instruction = instruction_generator(
426426
demo_candidates=demo_candidates,
427427
pred_i=pred_i,
428428
demo_set_i=demo_set_i,

dspy/teleprompt/mipro_optimizer_v2.py

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -423,31 +423,33 @@ def _bootstrap_fewshot_examples(self, program: Any, trainset: list, seed: int, t
423423

424424
zeroshot = self.max_bootstrapped_demos == 0 and self.max_labeled_demos == 0
425425

426-
try:
427-
effective_max_errors = (
428-
self.max_errors if self.max_errors is not None else dspy.settings.max_errors
429-
)
426+
# try:
427+
effective_max_errors = (
428+
self.max_errors if self.max_errors is not None else dspy.settings.max_errors
429+
)
430430

431-
demo_candidates = create_n_fewshot_demo_sets(
432-
student=program,
433-
num_candidate_sets=self.num_fewshot_candidates,
434-
trainset=trainset,
435-
max_labeled_demos=(LABELED_FEWSHOT_EXAMPLES_IN_CONTEXT if zeroshot else self.max_labeled_demos),
436-
max_bootstrapped_demos=(
437-
BOOTSTRAPPED_FEWSHOT_EXAMPLES_IN_CONTEXT if zeroshot else self.max_bootstrapped_demos
438-
),
439-
metric=self.metric,
440-
max_errors=effective_max_errors,
441-
teacher=teacher,
442-
teacher_settings=self.teacher_settings,
443-
seed=seed,
444-
metric_threshold=self.metric_threshold,
445-
rng=self.rng,
446-
)
447-
except Exception as e:
448-
logger.info(f"Error generating few-shot examples: {e}")
449-
logger.info("Running without few-shot examples.")
450-
demo_candidates = None
431+
demo_candidates = create_n_fewshot_demo_sets(
432+
student=program,
433+
num_candidate_sets=self.num_fewshot_candidates,
434+
trainset=trainset,
435+
max_labeled_demos=(LABELED_FEWSHOT_EXAMPLES_IN_CONTEXT if zeroshot else self.max_labeled_demos),
436+
max_bootstrapped_demos=(
437+
BOOTSTRAPPED_FEWSHOT_EXAMPLES_IN_CONTEXT if zeroshot else self.max_bootstrapped_demos
438+
),
439+
metric=self.metric,
440+
max_errors=effective_max_errors,
441+
teacher=teacher,
442+
teacher_settings=self.teacher_settings,
443+
seed=seed,
444+
metric_threshold=self.metric_threshold,
445+
rng=self.rng,
446+
)
447+
# NOTE: Bootstrapping is essential to MIPRO!
448+
# Failing silently here makes the rest of the optimization far weaker as a result!
449+
# except Exception as e:
450+
# logger.info(f"!!!!\n\n\n\n\nError generating few-shot examples: {e}")
451+
# logger.info("Running without few-shot examples.!!!!\n\n\n\n\n")
452+
# demo_candidates = None
451453

452454
return demo_candidates
453455

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies = [
2727
"openai>=0.28.1",
2828
"datasets>=2.14.6", # needed for Bootstrap's Hasher
2929
"regex>=2023.10.3",
30+
"datasets>=2.14.6", # needed for Bootstrap's Hasher
3031
"ujson>=5.8.0",
3132
"tqdm>=4.66.1",
3233
"requests>=2.31.0",

tests/primitives/test_base_module.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ def forward(self, x):
501501
module = TestModule()
502502
module.forward("test")
503503
captured = capsys.readouterr()
504-
assert "Calling TestModule.forward() directly is discouraged" in captured.err
504+
assert "directly is discouraged" in captured.err
505505

506506

507507
def test_forward_through_call_no_warning(capsys):
@@ -512,4 +512,4 @@ def forward(self, x):
512512
module = TestModule()
513513
module(x="test")
514514
captured = capsys.readouterr()
515-
assert "Calling TestModule.forward() directly is discouraged" not in captured.err
515+
assert "directly is discouraged" not in captured.err

0 commit comments

Comments
 (0)