or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-methods.mdauto-classes.mdcore-models.mdindex.mdlora-methods.mdprompt-learning.mdutilities.md

core-models.mddocs/

0

# Core Models

1

2

Core PEFT model classes and the primary function for creating PEFT models. These provide the foundation for all parameter-efficient fine-tuning operations in the library.

3

4

## Capabilities

5

6

### Main PEFT Model Creation

7

8

The primary entry point for creating PEFT models by wrapping base transformers models with parameter-efficient adapters.

9

10

```python { .api }

11

def get_peft_model(

12

model: PreTrainedModel,

13

peft_config: PeftConfig,

14

adapter_name: str = "default",

15

mixed: bool = False,

16

autocast_adapter_dtype: bool = True,

17

revision: Optional[str] = None,

18

low_cpu_mem_usage: bool = False

19

) -> PeftModel | PeftMixedModel:

20

"""

21

Returns a PEFT model object from a model and a config.

22

23

Args:

24

model: Base transformer model to be wrapped

25

peft_config: Configuration object containing PEFT parameters

26

adapter_name: Name of the adapter to be injected

27

mixed: Whether to allow mixing different adapter types

28

autocast_adapter_dtype: Whether to autocast adapter dtype

29

revision: Revision of the base model

30

low_cpu_mem_usage: Create empty adapter weights on meta device

31

32

Returns:

33

PEFT model with adapter capabilities

34

"""

35

```

36

37

### Base PEFT Model

38

39

The foundational PEFT model class that wraps transformer models and provides adapter functionality.

40

41

```python { .api }

42

class PeftModel(torch.nn.Module):

43

"""

44

Base model encompassing various PEFT methods.

45

46

Args:

47

model: The base transformer model used for PEFT

48

peft_config: The configuration of the PEFT model

49

adapter_name: The name of the adapter

50

autocast_adapter_dtype: Whether to autocast the adapter dtype

51

low_cpu_mem_usage: Create empty adapter weights on meta device

52

"""

53

def __init__(

54

self,

55

model,

56

peft_config: PeftConfig,

57

adapter_name: str = "default",

58

autocast_adapter_dtype: bool = True,

59

low_cpu_mem_usage: bool = False

60

): ...

61

62

def forward(self, *args, **kwargs): ...

63

64

def generate(self, **kwargs): ...

65

66

def save_pretrained(

67

self,

68

save_directory: str,

69

safe_serialization: bool = True,

70

selected_adapters: Optional[List[str]] = None,

71

**kwargs

72

): ...

73

74

def load_adapter(

75

self,

76

model_id: str,

77

adapter_name: str = "default",

78

is_trainable: bool = True,

79

**kwargs

80

): ...

81

82

def set_adapter(self, adapter_name: str): ...

83

84

def disable_adapter(self): ...

85

86

def enable_adapter(self): ...

87

88

def add_adapter(self, adapter_name: str, peft_config: PeftConfig): ...

89

90

def delete_adapter(self, adapter_name: str): ...

91

92

def merge_and_unload(self, progressbar: bool = False, safe_merge: bool = False): ...

93

94

def unload(self): ...

95

96

def print_trainable_parameters(self): ...

97

```

98

99

### Task-Specific Model Classes

100

101

Specialized PEFT model classes for different NLP tasks, providing task-specific forward methods and outputs.

102

103

```python { .api }

104

class PeftModelForCausalLM(PeftModel):

105

"""PEFT model for causal language modeling tasks."""

106

def forward(

107

self,

108

input_ids=None,

109

attention_mask=None,

110

inputs_embeds=None,

111

labels=None,

112

output_attentions=None,

113

output_hidden_states=None,

114

return_dict=None,

115

**kwargs

116

): ...

117

118

class PeftModelForSequenceClassification(PeftModel):

119

"""PEFT model for sequence classification tasks."""

120

def forward(

121

self,

122

input_ids=None,

123

attention_mask=None,

124

inputs_embeds=None,

125

labels=None,

126

output_attentions=None,

127

output_hidden_states=None,

128

return_dict=None,

129

**kwargs

130

) -> SequenceClassifierOutput: ...

131

132

class PeftModelForTokenClassification(PeftModel):

133

"""PEFT model for token classification tasks."""

134

def forward(

135

self,

136

input_ids=None,

137

attention_mask=None,

138

inputs_embeds=None,

139

labels=None,

140

output_attentions=None,

141

output_hidden_states=None,

142

return_dict=None,

143

**kwargs

144

) -> TokenClassifierOutput: ...

145

146

class PeftModelForQuestionAnswering(PeftModel):

147

"""PEFT model for question answering tasks."""

148

def forward(

149

self,

150

input_ids=None,

151

attention_mask=None,

152

inputs_embeds=None,

153

start_positions=None,

154

end_positions=None,

155

output_attentions=None,

156

output_hidden_states=None,

157

return_dict=None,

158

**kwargs

159

) -> QuestionAnsweringModelOutput: ...

160

161

class PeftModelForSeq2SeqLM(PeftModel):

162

"""PEFT model for sequence-to-sequence language modeling tasks."""

163

def forward(

164

self,

165

input_ids=None,

166

attention_mask=None,

167

inputs_embeds=None,

168

decoder_input_ids=None,

169

decoder_attention_mask=None,

170

decoder_inputs_embeds=None,

171

labels=None,

172

output_attentions=None,

173

output_hidden_states=None,

174

return_dict=None,

175

**kwargs

176

): ...

177

178

class PeftModelForFeatureExtraction(PeftModel):

179

"""PEFT model for feature extraction tasks."""

180

def forward(

181

self,

182

input_ids=None,

183

attention_mask=None,

184

inputs_embeds=None,

185

output_attentions=None,

186

output_hidden_states=None,

187

return_dict=None,

188

**kwargs

189

): ...

190

```

191

192

### Mixed Model Support

193

194

Support for combining multiple PEFT methods in a single model.

195

196

```python { .api }

197

class PeftMixedModel(torch.nn.Module):

198

"""

199

Model for combining multiple PEFT methods.

200

201

Allows mixing different adapter types (e.g., LoRA + IA3) for enhanced

202

parameter efficiency and task performance.

203

"""

204

def __init__(

205

self,

206

model: PreTrainedModel,

207

peft_config: dict,

208

adapter_name: str = "default"

209

): ...

210

211

def add_adapter(

212

self,

213

peft_config: PeftConfig,

214

adapter_name: str

215

): ...

216

217

def set_adapter(self, adapter_name: str): ...

218

```

219

220

### Model State Management

221

222

Functions for managing model state, adapters, and layers.

223

224

```python { .api }

225

def get_layer_status(model) -> dict:

226

"""

227

Get the status of each layer in the model.

228

229

Returns:

230

Dictionary mapping layer names to their status information

231

"""

232

233

def get_model_status(model) -> dict:

234

"""

235

Get comprehensive status information about the PEFT model.

236

237

Returns:

238

Model status including adapter information and layer states

239

"""

240

```

241

242

class PeftModelForFeatureExtraction(PeftModel):

243

"""PEFT model for feature extraction tasks."""

244

def forward(

245

self,

246

input_ids=None,

247

attention_mask=None,

248

inputs_embeds=None,

249

output_attentions=None,

250

output_hidden_states=None,

251

return_dict=None,

252

**kwargs

253

): ...

254

```

255

256

### Mixed Model Support

257

258

Support for combining multiple PEFT methods in a single model.

259

260

```python { .api }

261

class PeftMixedModel:

262

"""Model that supports mixing different PEFT methods."""

263

def __init__(

264

self,

265

model,

266

peft_config: PeftConfig,

267

adapter_name: str = "default"

268

): ...

269

270

def add_adapter(self, adapter_name: str, peft_config: PeftConfig): ...

271

272

def set_adapter(self, adapter_name: str): ...

273

```

274

275

### Model Status Functions

276

277

Utility functions for inspecting model and layer status.

278

279

```python { .api }

280

def get_model_status(model) -> dict:

281

"""

282

Get the status of the model including active adapters.

283

284

Args:

285

model: The PEFT model to inspect

286

287

Returns:

288

Dictionary containing model status information

289

"""

290

291

def get_layer_status(model, layer_name: str) -> dict:

292

"""

293

Get the status of a specific layer.

294

295

Args:

296

model: The PEFT model to inspect

297

layer_name: Name of the layer to inspect

298

299

Returns:

300

Dictionary containing layer status information

301

"""

302

```

303

304

## Usage Examples

305

306

### Basic Model Creation

307

308

```python

309

from transformers import AutoModelForCausalLM

310

from peft import get_peft_model, LoraConfig

311

312

# Load base model

313

model = AutoModelForCausalLM.from_pretrained("gpt2")

314

315

# Create PEFT config

316

config = LoraConfig(

317

task_type="CAUSAL_LM",

318

r=16,

319

lora_alpha=32,

320

lora_dropout=0.1,

321

target_modules=["c_attn", "c_proj"]

322

)

323

324

# Get PEFT model

325

peft_model = get_peft_model(model, config)

326

```

327

328

### Adapter Management

329

330

```python

331

# Save adapter

332

peft_model.save_pretrained("./my-adapter")

333

334

# Load additional adapter

335

peft_model.load_adapter("./another-adapter", adapter_name="task2")

336

337

# Switch between adapters

338

peft_model.set_adapter("task2")

339

340

# Disable all adapters

341

peft_model.disable_adapter()

342

343

# Enable adapters

344

peft_model.enable_adapter()

345

346

# Delete adapter

347

peft_model.delete_adapter("task2")

348

```

349

350

### Model Merging and Unloading

351

352

```python

353

# Merge adapter weights into base model and unload

354

merged_model = peft_model.merge_and_unload()

355

356

# Just unload adapters without merging

357

base_model = peft_model.unload()

358

```