Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Huggingface Peft Bone Finetuning Example

From Leeroopedia


Knowledge Sources
Domains Deep_Learning, Parameter_Efficient_Finetuning
Last Updated 2026-02-07 14:00 GMT

Overview

Example script demonstrating Bone (Block Affine) parameter-efficient fine-tuning using the Huggingface PEFT library.

Description

This script demonstrates how to fine-tune a causal language model using the Bone adapter method from PEFT. Bone applies block affine transformations to model weights, targeting attention and MLP projection layers. The script supports both standard Bone initialization and the Bat variant, uses the SFTTrainer from the TRL library for supervised fine-tuning, and includes optional merge-and-save functionality for deployment. The default dataset is IMDB.

Usage

Use this script when you want to fine-tune a causal language model with the Bone adapter method as an alternative to LoRA. Bone is useful when you need a different parameter-efficient adaptation strategy that applies block affine transformations. The script also demonstrates the Bat initialization variant via the --init_weights bat flag.

Code Reference

Source Location

  • Repository: Huggingface_Peft
  • File: examples/bone_finetuning/bone_finetuning.py
  • Lines: 1-105

Key Components

@dataclass
class ScriptArguments(SFTConfig):
    base_model_name_or_path: Optional[str] = field(default=None)
    bits: str = field(default="bf16")
    init_weights: Literal[True, "bat"] = field(default=True)
    bone_r: int = field(default=16)
    merge_and_save: bool = field(default=False)
    data_path: str = field(default="imdb")
    dataset_split: str = field(default="train[:1%]")
    dataset_field: list[str] = field(default=None)

# Main flow:
parser = HfArgumentParser(ScriptArguments)
script_args = parser.parse_args_into_dataclasses()[0]
model = AutoModelForCausalLM.from_pretrained(...)
bone_config = BoneConfig(r=script_args.bone_r, ...)
peft_model = get_peft_model(model, bone_config)
trainer = SFTTrainer(model=peft_model, ...)
trainer.train()

Import

from peft import BoneConfig, get_peft_model

I/O Contract

Inputs

Name Type Required Description
--base_model_name_or_path str Yes The name or path of the fp32/16 base model
--bits str No Precision format: bf16, fp16, or fp32 (default: bf16)
--init_weights str No Bone initialization: True for Bone, "bat" for Bat variant (default: True)
--bone_r int No Bone rank (default: 16)
--merge_and_save bool No Whether to merge adapter and save full model (default: False)
--data_path str No Path to the training data (default: imdb)
--dataset_split str No Dataset split to use (default: train[:1%])
--dataset_field list[str] No Fields of dataset input and output
--output_dir str Yes Output directory for saved model (inherited from SFTConfig)

Outputs

Name Type Description
bone_ft/ Directory Saved PEFT adapter weights in output_dir
bone_merged/ Directory Merged full model (only if --merge_and_save is set)
trainer state File Trainer checkpoint state

Usage Examples

Running the Script

python bone_finetuning.py \
    --base_model_name_or_path meta-llama/Llama-2-7b-hf \
    --data_path imdb \
    --dataset_split "train[:1%]" \
    --dataset_field text label \
    --bone_r 16 \
    --output_dir ./bone_output \
    --num_train_epochs 1

Key Code Pattern

from peft import BoneConfig, get_peft_model

bone_config = BoneConfig(
    r=16,
    target_modules=["q_proj", "o_proj", "k_proj", "v_proj", "gate_proj", "up_proj", "down_proj"],
    bias="none",
    task_type="CAUSAL_LM",
    init_weights=True,  # or "bat" for Bat variant
)
peft_model = get_peft_model(model, bone_config)
peft_model.print_trainable_parameters()

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment