dspy.BootstrapFinetune

dspy.BootstrapFinetune

dspy.BootstrapFinetune는 부트스트랩으로 trace 데이터를 생성해 언어 모델을 파인튜닝(finetune)하는 텔레프롬프터입니다.

출처: 문서

본문

dspy.BootstrapFinetune(
    metric: Callable | None = None,
    multitask: bool = True,
    train_kwargs: dict[str, Any] | dict[LM, dict[str, Any]] | None = None,
    adapter: Adapter | dict[LM, Adapter] | None = None,
    exclude_demos: bool = False,
    num_threads: int | None = None,
)
  • Bases: FinetuneTeleprompter

소스 코드는 dspy/teleprompt/bootstrap_finetune.py에 있습니다.

def __init__(
    self,
    metric: Callable | None = None,
    multitask: bool = True,
    train_kwargs: dict[str, Any] | dict[LM, dict[str, Any]] | None = None,
    adapter: Adapter | dict[LM, Adapter] | None = None,
    exclude_demos: bool = False,
    num_threads: int | None = None,
):
    # TODO(feature): Inputs train_kwargs (a dict with string keys) and
    # adapter (Adapter) can depend on the LM they are used with. We are
    # takingthese as parameters for the time being. However, they can be
    # attached to LMs themselves -- an LM could know which adapter it should
    # be used with along with the train_kwargs. This will lead the only
    # required argument for LM.finetune() to be the train dataset.

    super().__init__(train_kwargs=train_kwargs)
    self.metric = metric
    self.multitask = multitask
    self.adapter: dict[LM, Adapter] = self.convert_to_lm_dict(adapter)
    self.exclude_demos = exclude_demos
    self.num_threads = num_threads

Methods

compile(student: Module, trainset: list[Example], teacher: Module | list[Module] | None = None) -> Module

소스 코드는 dspy/teleprompt/bootstrap_finetune.py에 있습니다.

compile은 teacher 프로그램으로 부트스트랩 trace 데이터를 생성한 뒤, 해당 데이터로 LM을 파인튜닝합니다. multitask가 True면 모든 predictor가 같은 데이터로, False면 각 predictor가 자기 데이터로 학습합니다. 각 predictor에 LM이 할당되어 있지 않으면 ValueError를 발생시킵니다.

def compile(
    self, student: Module, trainset: list[Example], teacher: Module | list[Module] | None = None
) -> Module:
    # TODO: Print statements can be converted to logger.info if we ensure
    # that the default DSPy logger logs info level messages in notebook
    # environments.
    logger.info("Preparing the student and teacher programs...")
    all_predictors_have_lms(student)

    logger.info("Bootstrapping data...")
    trace_data = []

    teachers = teacher if isinstance(teacher, list) else [teacher]
    teachers = [prepare_teacher(student, t) for t in teachers]
    num_threads = self.num_threads or dspy.settings.num_threads
    for t in teachers:
        trace_data += bootstrap_trace_data(program=t, dataset=trainset, metric=self.metric, num_threads=num_threads)

    logger.info("Preparing the train data...")
    key_to_data = {}
    for pred_ind, pred in enumerate(student.predictors()):
        data_pred_ind = None if self.multitask else pred_ind
        if pred.lm is None:
            raise ValueError(
                f"Predictor {pred_ind} does not have an LM assigned. "
                f"Please ensure the module's predictors have their LM set before fine-tuning. "
                f"You can set it using: your_module.set_lm(your_lm)"
            )
        training_key = (pred.lm, data_pred_ind)
        ...

convert_to_lm_dict(arg) -> dict[LM, Any]

인자를 LM 키 딕셔너리로 변환합니다.

finetune_lms(finetune_dict) -> dict[Any, LM]

파인튜닝 딕셔너리에서 LM을 파인튜닝합니다.

get_params() -> dict[str, Any]

텔레프롬프터의 파라미터를 반환합니다.

더 알아보기 (Learn more)