-
-
Save vicpada/2d3150fc51c17f50b035dfa5c2dd6bed to your computer and use it in GitHub Desktop.
PEFT reduce_intruder_dimension post-training mitigation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from peft import LoraConfig, get_peft_model, TaskType | |
| from transformers import AutoModelForCausalLM | |
| import torch | |
| # Load base model | |
| model = AutoModelForCausalLM.from_pretrained( | |
| 'meta-llama/Meta-Llama-3-8B', | |
| load_in_4bit=True, # QLoRA | |
| device_map='auto', | |
| torch_dtype=torch.float16 | |
| ) | |
| # Configure LoRA | |
| config = LoraConfig( | |
| task_type=TaskType.CAUSAL_LM, | |
| r=16, | |
| lora_alpha=32, | |
| lora_dropout=0.05, | |
| target_modules=['q_proj', 'k_proj', 'v_proj', 'o_proj'], | |
| use_rslora=True, # rank-stabilized scaling (recommended) | |
| bias='none' | |
| ) | |
| peft_model = get_peft_model(model, config) | |
| # ... training ... | |
| # Post-training: mitigate intruder dimensions (arXiv 2410.21228) | |
| # This step reduces forgetting by 33-34% with only 0.1% task accuracy drop. | |
| # Apply standard hygiene after every LoRA fine-tuning run. | |
| from peft.tuners.lora.intruders import reduce_intruder_dimension | |
| reduced_model = reduce_intruder_dimension( | |
| peft_model, | |
| top_k=10, | |
| threshold_epsilon=0.5, | |
| mitigation_lambda=0.75 # 0.7-0.8 range: trade-off forgetting vs task accuracy | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment