Configure model variants using the Config trait
mainUse the #[derive(Config)] macro to define how model components and variants (e.g., different parameter sizes) are initialized.
Key Features of Config:
- Default values: Use
#[config(default = ...)]to specify defaults. - Automatic constructors: Provides
new(...)andwith_methods for fields. - Serialization: Enables easy saving and loading of configuration files.
Pattern:
- Define a
Configstruct for each module. - Implement an
initmethod on the config struct that takes a&Device<B>and returns the initialized module. - For complex models, create a top-level composite config that delegates initialization to the sub-module configs.
#[derive(Config, Debug)]
pub struct FeedForwardConfig {
pub d_model: usize,
pub hidden_size: usize,
}
impl FeedForwardConfig {
pub fn init<B: Backend>(&self, device: &Device<B>) -> FeedForward<B> {
let swiglu = SwiGluConfig::new(self.d_model, self.hidden_size)
.with_bias(false)
.init(device);
let w2 = LinearConfig::new(self.hidden_size, self.d_model)
.with_bias(false)
.init(device);
FeedForward { swiglu, w2 }
}
}