Skip to main content

peft_rs/
config.rs

1//! Configuration types for PEFT adapters.
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::{PeftError, Result};
6use crate::traits::AdapterConfig;
7
8/// Common configuration shared across adapter types.
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct BaseAdapterConfig {
11    /// Target modules to apply adapters to (e.g., [`q_proj`, `v_proj`]).
12    pub target_modules: Vec<String>,
13
14    /// Whether to apply to all linear layers matching the pattern.
15    #[serde(default)]
16    pub fan_in_fan_out: bool,
17
18    /// Modules to exclude from adaptation.
19    #[serde(default)]
20    pub modules_to_exclude: Vec<String>,
21}
22
23impl Default for BaseAdapterConfig {
24    fn default() -> Self {
25        Self {
26            target_modules: vec![
27                "q_proj".into(),
28                "k_proj".into(),
29                "v_proj".into(),
30                "o_proj".into(),
31            ],
32            fan_in_fan_out: false,
33            modules_to_exclude: Vec::new(),
34        }
35    }
36}
37
38impl AdapterConfig for BaseAdapterConfig {
39    fn validate(&self) -> Result<()> {
40        if self.target_modules.is_empty() {
41            return Err(PeftError::InvalidConfig(
42                "target_modules cannot be empty".into(),
43            ));
44        }
45        Ok(())
46    }
47}