Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions dspy/teleprompt/mipro_optimizer_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
import random
from collections import defaultdict
from typing import TYPE_CHECKING, Any, Callable, Literal

import numpy as np

import dspy
from dspy.evaluate.evaluate import Evaluate
from dspy.propose import GroundedProposer
Expand All @@ -23,6 +21,8 @@
if TYPE_CHECKING:
import optuna

from optuna.samplers import BaseSampler

logger = logging.getLogger(__name__)

# Constants
Expand Down Expand Up @@ -114,6 +114,8 @@ def compile(
fewshot_aware_proposer: bool = True,
requires_permission_to_run: bool | None = None, # deprecated
provide_traceback: bool | None = None,
sampler: BaseSampler | None = None,
n_jobs: int = 1
) -> Any:
if requires_permission_to_run == False:
logger.warning(
Expand Down Expand Up @@ -257,6 +259,8 @@ def compile(
minibatch_size,
minibatch_full_eval_steps,
seed,
sampler,
n_jobs
)

return best_program
Expand Down Expand Up @@ -492,6 +496,8 @@ def _propose_instructions(

return instruction_candidates



def _optimize_prompt_parameters(
self,
program: Any,
Expand All @@ -504,6 +510,8 @@ def _optimize_prompt_parameters(
minibatch_size: int,
minibatch_full_eval_steps: int,
seed: int,
sampler: BaseSampler,
n_jobs: int
) -> Any | None:
import optuna

Expand Down Expand Up @@ -643,8 +651,16 @@ def objective(trial):

return score

sampler = optuna.samplers.TPESampler(seed=seed, multivariate=True)
study = optuna.create_study(direction="maximize", sampler=sampler)
if not sampler:
_sampler = optuna.samplers.TPESampler(seed=seed, multivariate=True)
elif sampler.__class__.__name__ == 'qEISampler':
# batch Expected Improvement Sampler; allows parallelization in optuna
_sampler = sampler
_sampler.create_search_space(instruction_candidates, demo_candidates)
else:
_sampler = sampler

study = optuna.create_study(direction="maximize", sampler=_sampler)

default_params = {f"{i}_predictor_instruction": 0 for i in range(len(program.predictors()))}
if demo_candidates:
Expand All @@ -657,7 +673,8 @@ def objective(trial):
value=default_score,
)
study.add_trial(trial)
study.optimize(objective, n_trials=num_trials)
study.optimize(objective, n_trials=num_trials, n_jobs=n_jobs)
self.study = study

# Attach logs to best program
if best_program is not None and self.track_stats:
Expand Down Expand Up @@ -766,7 +783,8 @@ def _select_and_insert_instructions_and_demos(
predictor.demos = demo_candidates[i][demos_idx]
trial_logs[trial_num][f"{i}_predictor_demos"] = demos_idx
chosen_params.append(f"Predictor {i}: Few-Shot Set {demos_idx}")
raw_chosen_params[f"{i}_predictor_demos"] = instruction_idx
raw_chosen_params[f"{i}_predictor_demos"] = demos_idx
# raw_chosen_params[f"{i}_predictor_demos"] = instruction_idx

return chosen_params, raw_chosen_params

Expand Down