Ft Libraries
Framework registry for managing fine-tuning libraries.
This module provides access to the registered fine-tuning frameworks and helper functions to get a library instance by name.
FrameworkRegistry
Registry for available fine-tuning frameworks.
get(name, **kwargs)
classmethod
Get an instantiated fine-tuning library by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
Name of the framework. |
required |
**kwargs |
Any
|
Additional arguments to pass to the library's constructor (if any). |
{}
|
Returns:
| Type | Description |
|---|---|
BaseFineTuningLibrary
|
An instance of the fine-tuning library. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the framework name is not recognized. |
list_available_frameworks()
classmethod
List all registered framework names.
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of framework names. |
register(name, library_class)
classmethod
Register a fine-tuning library class.
Libraries are instantiated on demand.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
Name of the framework (e.g., "unsloth"). |
required |
library_class |
Type[BaseFineTuningLibrary]
|
The class of the fine-tuning library. |
required |
UnslothFineTuningLibrary()
Bases: UnslothBaseLibrary, FineTuningLibraryProtocol[Union[UnslothModel, UnslothMultimodalModel], SFTTrainer, UnslothTokenizer]
FineTuningLibrary implementation for the Unsloth framework with optimized model preparation.
Initialize UnslothFineTuningLibrary.
Raises:
| Type | Description |
|---|---|
ImportError
|
If unsloth is not available (GPU not available or not installed). |
framework_name: str
property
Get the name of the framework.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The name of the framework. |
load_model(experiment_args, hyperparameters, **kwargs)
Load an Unsloth model and initialize its tokenizer.
The tokenizer is stored internally and can be retrieved with get_tokenizer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment_args |
ExperimentConfig
|
Configuration for the experiment. |
required |
hyperparameters |
FinetunedHyperparameters
|
Configuration for the hyperparameters. |
required |
**kwargs |
Any
|
Additional arguments (not used by Unsloth library currently). |
{}
|
Returns:
| Type | Description |
|---|---|
Tuple[Union[UnslothModel, UnslothMultimodalModel], UnslothTokenizer]
|
The loaded model instance. |
Raises:
| Type | Description |
|---|---|
ImportError
|
If Unsloth is not installed and is required. |
ValueError
|
If required model arguments are missing. |
save_model(trainer, path=None, experiment_args=None, hyperparameters=None, storage_args=None, validator=None, results=None, model_path=None)
Save the trained model (and its tokenizer) to the specified path.
This method supports two calling conventions: 1. Simple: save_model(trainer, path) - for use by utility functions 2. Extended: save_model(trainer, experiment_args=..., hyperparameters=..., ...) - for use by trainers
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trainer |
Any
|
The trainer instance (TrlSFTTrainer, TrlDPOTrainer, or TrlGRPOTrainer). |
required |
path |
str | None
|
The directory path to save the model (simple mode). Defaults to None. |
None
|
experiment_args |
ExperimentConfig | None
|
Experiment configuration (extended mode). Defaults to None. |
None
|
hyperparameters |
FinetunedHyperparameters | None
|
Hyperparameters configuration (extended mode). Defaults to None. |
None
|
storage_args |
StorageConfig | None
|
Storage configuration (extended mode). Defaults to None. |
None
|
validator |
Any
|
Validator instance for model validation (extended mode). Defaults to None. |
None
|
results |
dict[str, Any] | None
|
Training results for newly trained models (extended mode). Defaults to None. |
None
|
model_path |
str | None
|
Path to the model directory for existing models (extended mode). Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str | None
|
Path where the model was saved or uploaded (extended mode), or None (simple mode). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If neither model_path nor results is provided in extended mode, or if configuration is invalid. |
RuntimeError
|
If the save operation fails. |
save_tokenizer(tokenizer, path, **kwargs)
Saves the tokenizer to the specified path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokenizer |
UnslothTokenizer
|
The tokenizer instance to save. |
required |
path |
str
|
The directory path to save the tokenizer to. |
required |
**kwargs |
Any
|
Additional framework-specific arguments. |
{}
|
setup_training(model, tokenizer, hyperparameters, train_dataset, eval_dataset=None, is_bfloat16_supported=False, dataset_text_field='prompt', formatting_func=None)
Set up a TRL SFTTrainer for the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
Union[UnslothModel, UnslothMultimodalModel]
|
The model instance to train (should be from load_model). |
required |
tokenizer |
UnslothTokenizer
|
The tokenizer instance to use. |
required |
hyperparameters |
FinetunedHyperparameters
|
Configuration for the hyperparameters. |
required |
is_bfloat16_supported |
bool
|
Whether bfloat16 is supported by hardware. |
False
|
train_dataset |
Any
|
The training dataset. |
required |
eval_dataset |
Optional[Any]
|
The evaluation dataset (optional). |
None
|
dataset_text_field |
str
|
Field in dataset containing the text data. Defaults to "prompt". |
'prompt'
|
formatting_func |
Callable[[dict], list[str]]
|
Function to format the dataset. |
None
|
Returns:
| Type | Description |
|---|---|
SFTTrainer
|
The initialized TRL SFTTrainer instance. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model or tokenizer (passed as arg) is invalid. |
ImportError
|
If Unsloth/TRL is not available. |
ValueError
|
If evaluation strategy is set but no validation dataset is provided. |
setup_training_dpo(model, tokenizer, hyperparameters, train_dataset, eval_dataset=None, **kwargs)
Set up a TRL DPOTrainer for the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
Union[UnslothModel, UnslothMultimodalModel]
|
The model instance to train (should be from load_model). |
required |
tokenizer |
UnslothTokenizer
|
The tokenizer instance to use. |
required |
hyperparameters |
FinetunedHyperparameters
|
Configuration for the hyperparameters. |
required |
train_dataset |
Any
|
The training dataset. |
required |
eval_dataset |
Optional[Any]
|
The evaluation dataset (optional). |
None
|
**kwargs |
Any
|
Additional arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
DPOTrainer
|
The initialized TRL DPOTrainer instance. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model or tokenizer (passed as arg) is invalid. |
ImportError
|
If Unsloth/TRL is not available. |
setup_training_grpo(model, tokenizer, hyperparameters, vllm_sampling_params_config, train_dataset, eval_dataset=None, reward_funcs=None, **kwargs)
Set up a TRL GRPOTrainer for the model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
Union[UnslothModel, UnslothMultimodalModel]
|
The model instance to train (should be from load_model). |
required |
tokenizer |
UnslothTokenizer
|
The tokenizer instance to use. |
required |
hyperparameters |
FinetunedHyperparameters
|
Configuration for the hyperparameters. |
required |
vllm_sampling_params_config |
VllmSamplingParamsConfig
|
Configuration for vLLM sampling parameters. |
required |
train_dataset |
Any
|
The training dataset. |
required |
eval_dataset |
Optional[Any]
|
The evaluation dataset (optional). |
None
|
reward_funcs |
list[Callable[[str], float]]
|
List of reward functions for GRPO training. |
None
|
**kwargs |
Any
|
Additional arguments. |
{}
|
Returns:
| Type | Description |
|---|---|
GRPOTrainer
|
The initialized TRL GRPOTrainer instance. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the model or tokenizer (passed as arg) is invalid. |
ImportError
|
If Unsloth/TRL is not available. |
get_framework_library(name, **kwargs)
High-level function to get a framework library instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name |
str
|
The name of the framework. |
required |
**kwargs |
Any
|
Additional arguments for the library's constructor. |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
BaseFineTuningLibrary |
BaseFineTuningLibrary
|
An instance of the requested library. |
list_available_frameworks()
List all available frameworks.
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of framework names. |
trainer_preparation(experiment_args, hyperparameters)
Create a trainer using the specified framework.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment_args |
ExperimentConfig
|
Configuration for the experiment. |
required |
hyperparameters |
FinetunedHyperparameters
|
Configuration for the hyperparameters. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Any, Any, Any]
|
Tuple[Any, Any, Any]: A tuple containing: - library: The framework library instance. - model: The loaded model instance. - tokenizer: The tokenizer instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the framework is not registered, initialization fails, or any other error occurs. |