LoRA
mindnlp.peft.tuners.lora.config
¶
lora config
mindnlp.peft.tuners.lora.config.LoftQConfig
dataclass
¶
This is the sub-configuration class to store the configuration of a [LoraModel
].
PARAMETER | DESCRIPTION |
---|---|
bits_pattern |
The mapping from layer names or regexp expression to bits which are different from the
default bits specified by
TYPE:
|
bits |
Quantization bits for LoftQ.
TYPE:
|
iter |
Alternating iterations for LoftQ.
TYPE:
|
fake |
models. weights can't be saved. Recommend to set to True, save the weights and load the saved weights in 4 bits.
TYPE:
|
Source code in mindnlp/peft/tuners/lora/config.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
mindnlp.peft.tuners.lora.config.LoraConfig
dataclass
¶
Bases: PeftConfig
This is the configuration class to store the configuration of a [LoraModel
].
PARAMETER | DESCRIPTION |
---|---|
r |
Lora attention dimension (the "rank").
TYPE:
|
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:
|
lora_alpha |
The alpha parameter for Lora scaling.
TYPE:
|
lora_dropout |
The dropout probability for Lora layers.
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:
|
bias |
Bias type for LoRA. Can be 'none', 'all' or 'lora_only'. If 'all' or 'lora_only', the corresponding biases will be updated during training. Be aware that this means that, even when disabling the adapters, the model will not produce the same output as the base model would have without adaptation.
TYPE:
|
use_rslora |
When set to True, uses Rank-Stabilized LoRA which
sets the adapter scaling factor to
TYPE:
|
cells_to_save |
List of cells apart from adapter layers to be set as trainable and saved in the final checkpoint.
TYPE:
|
init_lora_weights |
How to initialize the weights of the adapter layers. Passing True (default) results in the default
initialization from the reference implementation from Microsoft. Passing 'gaussian' results in Gaussian
initialization scaled by the LoRA rank for linear and layers. Setting the initialization to False leads to
completely random initialization and is discouraged. Pass
TYPE:
|
layers_to_transform |
The layer indices to transform. If a list of ints is passed, it will apply the adapter to the layer indices that are specified in this list. If a single integer is passed, it will apply the transformations on the layer at this index.
TYPE:
|
layers_pattern |
The layer pattern name, used only if
TYPE:
|
rank_pattern |
The mapping from layer names or regexp expression to ranks which are different from the default rank
specified by
TYPE:
|
alpha_pattern |
The mapping from layer names or regexp expression to alphas which are different from the default alpha
specified by
TYPE:
|
megatron_config |
The TransformerConfig arguments for Megatron. It is used to create LoRA's parallel linear layer. You can
get it like this,
TYPE:
|
megatron_core |
The core cell from Megatron to use, defaults to
TYPE:
|
loftq_config |
The configuration of LoftQ. If this is not None, then LoftQ will be used to quantize the backbone weights
and initialize Lora layers. Also pass
TYPE:
|
use_dora |
Enable 'Weight-Decomposed Low-Rank Adaptation' (DoRA). This technique decomposes the updates of the weights into two parts, magnitude and direction. Direction is handled by normal LoRA, whereas the magnitude is handled by a separate learnable parameter. This can improve the performance of LoRA especially at low ranks. Right now, DoRA only supports linear and Conv2D layers. DoRA introduces a bigger overhead than pure LoRA, so it is recommended to merge weights for inference. For more information, see https://arxiv.org/abs/2402.09353.
TYPE:
|
layer_replication |
Build a new stack of layers by stacking the original model layers according to the ranges specified. This allows expanding (or shrinking) the model without duplicating the base model weights. The new layers will all have separate LoRA adapters attached to them.
TYPE:
|
Source code in mindnlp/peft/tuners/lora/config.py
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 |
|
mindnlp.peft.tuners.lora.config.LoraConfig.__post_init__()
¶
Performs post-initialization operations for the LoraConfig class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of LoraConfig to be initialized.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
ValueError
|
If |
ValueError
|
If DoRA does not support megatron_core and |
ValueError
|
If |
ImportError
|
If the required package 'scipy' is not installed. |
Source code in mindnlp/peft/tuners/lora/config.py
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 |
|
mindnlp.peft.tuners.lora.model
¶
lora model
mindnlp.peft.tuners.lora.model.LoraModel
¶
Bases: BaseTuner
Creates Low Rank Adapter (LoRA) model from a pretrained transformers model.
The method is described in detail in https://arxiv.org/abs/2106.09685.
PARAMETER | DESCRIPTION |
---|---|
model |
The model to be adapted.
TYPE:
|
config |
The configuration of the Lora model.
TYPE:
|
adapter_name |
The name of the adapter, defaults to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
LoraModel
|
The Lora model.
TYPE:
|
```py
>>> from transformers import AutoModelForSeq2SeqLM
>>> from peft import LoraModel, LoraConfig
>>> config = LoraConfig(
... task_type="SEQ_2_SEQ_LM",
... r=8,
... lora_alpha=32,
... target_cells=["q", "v"],
... lora_dropout=0.01,
... )
>>> model = AutoModelForSeq2SeqLM.from_pretrained("t5-base")
>>> lora_model = LoraModel(model, config, "default")
```
```py
>>> import torch
>>> import transformers
>>> from peft import LoraConfig, PeftModel, get_peft_model, prepare_model_for_kbit_training
>>> rank = ...
>>> target_cells = ["q_proj", "k_proj", "v_proj", "out_proj", "fc_in", "fc_out", "wte"]
>>> config = LoraConfig(
... r=4, lora_alpha=16, target_cells=target_cells, lora_dropout=0.1, bias="none", task_type="CAUSAL_LM"
... )
>>> quantization_config = transformers.BitsAndBytesConfig(load_in_8bit=True)
>>> tokenizer = transformers.AutoTokenizer.from_pretrained(
... "kakaobrain/kogpt",
... revision="KoGPT6B-ryan1.5b-float16", # or float32 version: revision=KoGPT6B-ryan1.5b
... bos_token="[BOS]",
... eos_token="[EOS]",
... unk_token="[UNK]",
... pad_token="[PAD]",
... mask_token="[MASK]",
... )
>>> model = transformers.GPTJForCausalLM.from_pretrained(
... "kakaobrain/kogpt",
... revision="KoGPT6B-ryan1.5b-float16", # or float32 version: revision=KoGPT6B-ryan1.5b
... pad_token_id=tokenizer.eos_token_id,
... use_cache=False,
... device_map={"": rank},
... torch_dtype=torch.float16,
... quantization_config=quantization_config,
... )
>>> model = prepare_model_for_kbit_training(model)
>>> lora_model = get_peft_model(model, config)
```
Attributes:
model ([
transformers.PreTrainedModel
])— The model to be adapted.peft_config ([
LoraConfig
]): The configuration of the Lora model.
Source code in mindnlp/peft/tuners/lora/model.py
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 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 |
|
mindnlp.peft.tuners.lora.model.LoraModel.__getattr__(name)
¶
Forward missing attributes to the wrapped cell.
Source code in mindnlp/peft/tuners/lora/model.py
404 405 406 407 408 409 |
|
mindnlp.peft.tuners.lora.model.LoraModel.add_weighted_adapter(adapters, weights, adapter_name, combination_type='svd', svd_rank=None, svd_clamp=None, svd_full_matrices=True, density=None, majority_sign_method='total')
¶
This method adds a new adapter by merging the given adapters with the given weights.
When using the cat
combination_type you should be aware that rank of the resulting adapter will be equal to
the sum of all adapters ranks. So it's possible that the mixed adapter may become too big and result in OOM
errors.
PARAMETER | DESCRIPTION |
---|---|
adapters |
List of adapter names to be merged.
TYPE:
|
weights |
List of weights for each adapter.
TYPE:
|
adapter_name |
Name of the new adapter.
TYPE:
|
combination_type |
The merging type can be one of [
TYPE:
|
svd_rank |
Rank of output adapter for svd. If None provided, will use max rank of merging adapters.
TYPE:
|
svd_clamp |
A quantile threshold for clamping SVD decomposition output. If None is provided, do not perform clamping. Defaults to None.
TYPE:
|
svd_full_matrices |
Controls whether to compute the full or reduced SVD, and consequently, the shape of the returned tensors U and Vh. Defaults to True.
TYPE:
|
density |
Value between 0 and 1. 0 means all values are pruned and 1 means no values are pruned. Should be used
with [
TYPE:
|
majority_sign_method |
The method, should be one of ["total", "frequency"], to use to get the magnitude of the sign values.
Should be used with [
TYPE:
|
Source code in mindnlp/peft/tuners/lora/model.py
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 |
|
mindnlp.peft.tuners.lora.model.LoraModel.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/lora/model.py
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 |
|
mindnlp.peft.tuners.lora.model.LoraModel.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/lora/model.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 |
|
mindnlp.peft.tuners.lora.model.LoraModel.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/lora/model.py
469 470 471 472 473 474 |
|
mindnlp.peft.tuners.lora.model.LoraModel.get_peft_config_as_dict(inference=False)
¶
Returns a dictionary representation of the PEFT config.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the LoraModel class.
|
inference |
A flag indicating whether the method is called for inference. Default is False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the PEFT config. The keys represent the configuration options, and the values represent their corresponding values. If 'inference' is True, the dictionary will also include the 'inference_mode' key set to True. |
Note
- The method uses the 'peft_config' attribute of the LoraModel instance to create the dictionary.
- If a value in the 'peft_config' attribute is an instance of Enum, its value will be extracted using the 'value' attribute.
- The 'config_dict' dictionary will only contain one key-value pair. If the 'inference' flag is True, the 'config_dict' will be updated to include the 'inference_mode' key.
Example usage
model = LoraModel() config = model.get_peft_config_as_dict(inference=True) print(config) # {'inference_mode': True}
config = model.get_peft_config_as_dict() print(config) # {}
Source code in mindnlp/peft/tuners/lora/model.py
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 |
|
mindnlp.peft.tuners.lora.model.LoraModel.merge_and_unload(progressbar=False, safe_merge=False, adapter_names=None)
¶
This method merges the LoRa layers into the base model. This is needed if someone wants to use the base model as a standalone model.
PARAMETER | DESCRIPTION |
---|---|
progressbar |
whether to show a progressbar indicating the unload and merge process
TYPE:
|
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/lora/model.py
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 |
|
mindnlp.peft.tuners.lora.model.LoraModel.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.parameters_and_names():
... 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/lora/model.py
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
|
mindnlp.peft.tuners.lora.model.LoraModel.unload()
¶
Gets back the base model by removing all the lora cells without merging. This gives back the original base model.
Source code in mindnlp/peft/tuners/lora/model.py
1061 1062 1063 1064 1065 1066 |
|