Skip to content

Commit a0512c7

Browse files
ShangyintXenonMolecule
authored andcommitted
Add teacher module to MIPROv2 (#1830)
* Add teacher module to MIPROv2 * Update MIPROv2 Docs Add teacher program to the compile method --------- Co-authored-by: Michael Ryan <michael_ryan_2000@yahoo.com>
1 parent 32e539f commit a0512c7

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

docs/docs/deep-dive/optimizers/miprov2.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ evaluate(optimized_program, devset=devset[:])
192192
| `student` | `dspy.Module` | **Required** | The base program to optimize. |
193193
| `trainset` | `List[dspy.Example]` | **Required** | Training dataset which is used to bootstrap few-shot examples and instructions. If a separate `valset` is not specified, 80% of this training set will also be used as a validation set for evaluating new candidate prompts. |
194194
| `valset` | `List[dspy.Example]` | Defaults to 80% of trainset | Dataset which is used to evaluate candidate prompts. We recommend using somewhere between 50-500 examples for optimization. |
195+
| `teacher` | `dspy.Module` | Defaults to student | The program to run in order to bootstrap the few-shot examples. |
195196
| `num_trials` | `int` | `30` | Number of optimization trials to run. When `minibatch` is set to `True`, this represents the number of minibatch trials that will be run on batches of size `minibatch_size`. When minibatch is set to `False`, each trial uses a full evaluation on the training set. In both cases, we recommend setting `num_trials` to a *minimum* of .75 x # modules in program x # variables per module (2 if few-shot examples & instructions will both be optimized, 1 in the 0-shot case). |
196197
| `minibatch` | `bool` | `True` | Flag to enable evaluating over minibatches of data (instead of the full validation set) for evaluation each trial. |
197198
| `minibatch_size` | `int` | `25.0` | Size of minibatches for evaluations. |
@@ -216,4 +217,4 @@ These steps are broken down in more detail below:
216217
3. **Find an Optimized Combination of Few-Shot Examples & Instructions**. Finally, now that we've created these few-shot examples and instructions, we use Bayesian Optimization to choose which set of these would work best for each predictor in our program. This works by running a series of `num_trials` trials, where a new set of prompts are evaluated over our validation set at each trial. This helps the Bayesian Optimizer learn which combination of prompts work best over time. If `minibatch` is set to `True` (which it is by default), then the new set of prompts are only evaluated on a minibatch of size `minibatch_size` at each trial which generally allows for more efficient exploration / exploitation. The best averaging set of prompts is then evaluated on the full validation set every `minibatch_full_eval_steps` get a less noisey performance benchmark. At the end of the optimization process, the LM program with the set of prompts that performed best on the full validation set is returned.
217218

218219

219-
For those interested in more details, more information on `MIPROv2` along with a study on `MIPROv2` compared with other DSPy optimizers can be found in [this paper](https://arxiv.org/abs/2406.11695).
220+
For those interested in more details, more information on `MIPROv2` along with a study on `MIPROv2` compared with other DSPy optimizers can be found in [this paper](https://arxiv.org/abs/2406.11695).

dspy/teleprompt/mipro_optimizer_v2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def compile(
9595
student: Any,
9696
*,
9797
trainset: List,
98+
teacher: Any = None,
9899
valset: Optional[List] = None,
99100
num_trials: int = 30,
100101
max_bootstrapped_demos: Optional[int] = None,
@@ -165,7 +166,7 @@ def compile(
165166
)
166167

167168
# Step 1: Bootstrap few-shot examples
168-
demo_candidates = self._bootstrap_fewshot_examples(program, trainset, seed)
169+
demo_candidates = self._bootstrap_fewshot_examples(program, trainset, seed, teacher)
169170

170171
# Step 2: Propose instruction candidates
171172
instruction_candidates = self._propose_instructions(
@@ -368,7 +369,7 @@ def _get_user_confirmation(
368369
return user_input == "y"
369370

370371
def _bootstrap_fewshot_examples(
371-
self, program: Any, trainset: List, seed: int
372+
self, program: Any, trainset: List, seed: int, teacher: Any
372373
) -> Optional[List]:
373374
logger.info("\n==> STEP 1: BOOTSTRAP FEWSHOT EXAMPLES <==")
374375
if self.max_bootstrapped_demos > 0:
@@ -399,6 +400,7 @@ def _bootstrap_fewshot_examples(
399400
),
400401
metric=self.metric,
401402
max_errors=self.max_errors,
403+
teacher=teacher,
402404
teacher_settings=self.teacher_settings,
403405
seed=seed,
404406
metric_threshold=self.metric_threshold,

0 commit comments

Comments
 (0)