ramtorch + quanto + PEFT: fixes for moving between devices#2884
Merged
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request introduces improvements to device management for LoRA adapters in both quantized and standard PEFT Linear layers. The main focus is to ensure LoRA adapter weights are always moved to the correct device (e.g., GPU or CPU) during forward passes, preventing device mismatch errors, especially when quantization or offloading is in use. The logic for handling this device movement is now centralized and shared across custom and patched PEFT layers.
Device management improvements:
_move_peft_lora_adapters_to_deviceutility to centralize logic for moving LoRA adapter weights to the correct device, handling both single and multiple active adapters. (simpletuner/helpers/utils/ramtorch.py)RamTorchPeftLinear._ensure_lora_on_deviceto use the new centralized device movement logic, reducing code duplication and improving reliability. (simpletuner/helpers/utils/ramtorch.py) [1] [2]PEFT Linear patching:
_maybe_patch_peft_lora_forwardto monkey-patch the forward method of PEFT's stockLinearlayer, ensuring device movement logic is applied even when the standard PEFT wrapper is used. (simpletuner/helpers/utils/ramtorch.py)ensure_availablecalls the new patching function to activate the forward patch when RamTorch is in use. (simpletuner/helpers/utils/ramtorch.py)Quantized LoRA support:
_ensure_lora_adapter_on_devicehelper and integrated it into the forward passes of quantized LoRA layers to keep adapter weights on the correct device. (simpletuner/helpers/training/quantisation/peft_workarounds.py) [1] [2] [3]These changes make device handling for LoRA adapters more robust and consistent across quantized and non-quantized workflows, reducing the risk of runtime errors due to device mismatches.