Skip to content

yival.finetune.back_up_trainer

This module is the back up trainer

It will only be called when the dependency is not imported correctly

yival.finetune.sft_trainer

This module provides an implementation of Supervised Fine-tuning trainer.

SFTTrainer Objects

class SFTTrainer(BaseTrainer)

SFT Trainer implement

yival.finetune.openai_finetune_utils

finetune

def finetune(input_file: str,
             condition: str,
             custom_function: str,
             system_prompt: str = "",
             model_suffx: str = "") -> str

Fine-tunes a gpt-3.5 using provided data and conditions.

Arguments:

  • input_file (str): Path to the input file containing experiment results.
  • condition (str): Condition to evaluate for extracting relevant results.
  • custom_function (str): Path or module containing the custom function used in the experiment.
  • system_prompt (str, optional): System message to prepend to each chat. Defaults to None.
  • model_suffix: (str, optional): Suffix to append to the model name. Defaults to None.

Returns:

  • str: ID of the fine-tuned model.

yival.finetune.base_trainer

This module defines the base class for trainer

Trainers are responsible for finetune llms locally based on the data and experiment results

BaseTrainer Objects

class BaseTrainer(ABC)

Abstract base class for all trainers

Attributes:

  • _register Dict[str, Dict[str,Any]] - A register to keep track of trainers
  • default_config Optional[BaseTrainerConfig] - Default configuration for the trainers

get_trainer

@classmethod
def get_trainer(cls, name: str) -> Optional[Type['BaseTrainer']]

Retrieve trainer class from registry by its name.

get_default_config

@classmethod
def get_default_config(cls, name: str) -> Optional[BaseTrainerConfig]

Retrieve the default configuration of a trainer from the name

register_trainer

@classmethod
def register_trainer(cls,
                     name: str,
                     trainer_cls: Type['BaseTrainer'],
                     config_cls: Optional[Type[BaseTrainerConfig]] = None)

Register a new trainer along with its defualt configuration and configuration class.

train

@abstractmethod
def train(experiment: Experiment, config: ExperimentConfig) -> TrainerOutput

Train models based on the configs and datas

Arguments:

  • experiment Experiment - The experiment with its results.
  • config ExperimentConfig - The original experiment configuration.
  • evaluator Evaluator - A utility class to evaluate the ExperimentResult. token_logger (TokenLogger): Logs the token usage.

Returns:

TrainerOutput

yival.finetune.utils

def print_trainable_parameters(model)

Prints the number of trainable parameters in the model.

extract_from_input_data

def extract_from_input_data(experiment: Experiment, prompt_key: str,
                            completion_key: str | None,
                            condition: str | None) -> HgDataset

if experiment doesn't support custom_func , extract all data from group_experiment_results

else extract data from combination_aggregated_metrics according to condition

An example of condition: 'name == openai_prompt_based_evaluator AND result >= 0 AND display_name == clarity'

yival.finetune.replicate_finetune_utils

finetune

def finetune(input_file: str,
             condition: str,
             custom_function: str,
             destination: str,
             model_name: str,
             num_train_epochs: int = 3,
             support_expected_value: bool = False,
             system_prompt: str = "") -> str

Fine-tunes a replicate model using provided data and conditions.

Arguments:

  • input_file (str): Path to the input file containing experiment results.
  • condition (str): Condition to evaluate for extracting relevant results.
  • custom_function (str): Path or module containing the custom function used in the experiment.
  • destination: (str): The model to push the trained version to .
  • model_name: (str): Model name that will be used for finetune.
  • num_train_epochs: (int, optional): Number of epochs to train the model.
  • system_prompt (str, optional): System message to prepend to each chat. Defaults to None.

Returns:

  • str: ID of the fine-tuned model.