IA3
mindnlp.peft.tuners.ia3.config
¶
IA3 Config
mindnlp.peft.tuners.ia3.config.IA3Config
dataclass
¶
Bases: PeftConfig
This is the configuration class to store the configuration of a [IA3Model
].
PARAMETER | DESCRIPTION |
---|---|
target_cells |
The names of the cells to apply the adapter to. If this is specified, only the cells with the specified names will be replaced. When passing a string, a regex match will be performed. When passing a list of strings, either an exact match will be performed or it is checked if the name of the cell ends with any of the passed strings. If this is specified as 'all-linear', then all linear/Conv1D cells are chosen, excluding the output layer. If this is not specified, cells will be chosen according to the model architecture. If the architecture is not known, an error will be raised -- in this case, you should specify the target cells manually.
TYPE:
|
feedforward_cells |
The names of the cells to be treated as feedforward cells, as in the original paper. These cells will
have (IA)³ vectors multiplied to the input, instead of the output.
TYPE:
|
fan_in_fan_out |
Set this to True if the layer to replace stores weight like (fan_in, fan_out). For example, gpt-2 uses
TYPE:
|
cells_to_save |
List of cells apart from (IA)³ layers to be set as trainable and saved in the final checkpoint.
TYPE:
|
init_ia3_weights |
Whether to initialize the vectors in the (IA)³ layers, defaults to
TYPE:
|
Source code in mindnlp/peft/tuners/ia3/config.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
mindnlp.peft.tuners.ia3.config.IA3Config.__post_init__()
¶
This method initializes the IA3Config class after its instance has been created.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the IA3Config class.
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the |
Description
The post_init method sets default values for the IA3Config instance. It assigns the PeftType.IA3 value to the peft_type attribute. The target_cells and feedforward_cells attributes are converted to sets if they are provided as lists, or left unchanged if they are already sets.
The method then performs a check to ensure that if both target_cells and feedforward_cells are sets, the
feedforward_cells subset is a subset of the target_cells set. If this check fails, a ValueError exception is raised
with the message 'feedforward_cells
should be a subset of target_cells
'.
Source code in mindnlp/peft/tuners/ia3/config.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
|
mindnlp.peft.tuners.ia3.model
¶
IA3 Model
mindnlp.peft.tuners.ia3.model.IA3Model
¶
Bases: BaseTuner
Creates a Infused Adapter by Inhibiting and Amplifying Inner Activations ((IA)^3) model from a pretrained transformers model. The method is described in detail in https://arxiv.org/abs/2205.05638
PARAMETER | DESCRIPTION |
---|---|
model |
The model to be adapted.
TYPE:
|
config |
The configuration of the (IA)^3 model.
TYPE:
|
adapter_name |
The name of the adapter, defaults to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
IA3Model
|
The IA3Lora model.
TYPE:
|
```py
>>> from transformers import AutoModelForSeq2SeqLM, ia3Config
>>> from peft import IA3Model, IA3Config
>>> config = IA3Config(
... peft_type="IA3",
... task_type="SEQ_2_SEQ_LM",
... target_cells=["k", "v", "w0"],
... feedforward_cells=["w0"],
... )
>>> model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
>>> ia3_model = IA3Model(config, model)
```
Attributes:
model ([
transformers.PreTrainedModel
])— The model to be adapted.peft_config ([
IA3Config
]): The configuration of the (IA)^3 model.
Source code in mindnlp/peft/tuners/ia3/model.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
|
mindnlp.peft.tuners.ia3.model.IA3Model.__getattr__(name)
¶
Forward missing attributes to the wrapped cell.
Source code in mindnlp/peft/tuners/ia3/model.py
364 365 366 367 368 369 |
|
mindnlp.peft.tuners.ia3.model.IA3Model.__init__(model, config, adapter_name)
¶
Initializes an instance of the IA3Model class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the IA3Model class.
|
model |
The model object to be initialized.
|
config |
The configuration settings for the model.
|
adapter_name |
The name of the adapter.
|
RETURNS | DESCRIPTION |
---|---|
None. This method does not return any value. |
Source code in mindnlp/peft/tuners/ia3/model.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
mindnlp.peft.tuners.ia3.model.IA3Model.delete_adapter(adapter_name)
¶
Deletes an existing adapter.
PARAMETER | DESCRIPTION |
---|---|
adapter_name |
Name of the adapter to be deleted.
TYPE:
|
Source code in mindnlp/peft/tuners/ia3/model.py
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
|
mindnlp.peft.tuners.ia3.model.IA3Model.disable_adapter_layers()
¶
Disable all adapters.
When disabling all adapters, the model output corresponds to the output of the base model.
Source code in mindnlp/peft/tuners/ia3/model.py
408 409 410 411 412 413 |
|
mindnlp.peft.tuners.ia3.model.IA3Model.enable_adapter_layers()
¶
Enable all adapters.
Call this if you have previously disabled all adapters and want to re-enable them.
Source code in mindnlp/peft/tuners/ia3/model.py
401 402 403 404 405 406 |
|
mindnlp.peft.tuners.ia3.model.IA3Model.get_peft_config_as_dict(inference=False)
¶
Get the configuration of the (IA)^3 model as a dictionary.
Source code in mindnlp/peft/tuners/ia3/model.py
371 372 373 374 375 376 377 378 379 |
|
mindnlp.peft.tuners.ia3.model.IA3Model.merge_and_unload(safe_merge=False, adapter_names=None)
¶
This method merges the IA³ layers into the base model. This is needed if someone wants to use the base model as a standalone model.
PARAMETER | DESCRIPTION |
---|---|
safe_merge |
whether to activate the safe merging check to check if there is any potential Nan in the adapter weights
TYPE:
|
adapter_names |
The list of adapter names that should be merged. If None, all active adapters will be merged. Defaults
to
TYPE:
|
>>> from transformers import AutoModelForCausalLM
>>> from peft import PeftModel
>>> base_model = AutoModelForCausalLM.from_pretrained("tiiuae/falcon-40b")
>>> peft_model_id = "smangrul/falcon-40B-int4-peft-lora-sfttrainer-sample"
>>> model = PeftModel.from_pretrained(base_model, peft_model_id)
>>> merged_model = model.merge_and_unload()
Source code in mindnlp/peft/tuners/ia3/model.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
|
mindnlp.peft.tuners.ia3.model.IA3Model.set_adapter(adapter_name)
¶
Set the active adapter(s).
Additionally, this function will set the specified adapters to trainable (i.e., requires_grad=True). If this is not desired, use the following code.
>>> for name, param in model_peft.named_parameters():
... if ...: # some check on name (ex. if 'lora' in name)
... param.requires_grad = False
PARAMETER | DESCRIPTION |
---|---|
adapter_name |
Name of the adapter(s) to be activated.
TYPE:
|
Source code in mindnlp/peft/tuners/ia3/model.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
|
mindnlp.peft.tuners.ia3.model.IA3Model.unload()
¶
Gets back the base model by removing all the IA³ cells without merging. This gives back the original base model.
Source code in mindnlp/peft/tuners/ia3/model.py
539 540 541 542 543 544 |
|