Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX Allow same layer adapters on different devices #1742

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/peft/tuners/adalora/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ def update_layer(self, adapter_name, r, lora_alpha, lora_dropout, init_lora_weig
if init_lora_weights:
self.reset_lora_parameters(adapter_name)

if hasattr(self.get_base_layer(), "qweight"):
# QuantLinear
self.to(self.get_base_layer().qweight.device)
else:
self.to(self.get_base_layer().weight.device)
self._move_adapter_to_device_of_base_layer(adapter_name)
self.set_adapter(self.active_adapters)

def reset_lora_parameters(self, adapter_name):
Expand Down
21 changes: 4 additions & 17 deletions src/peft/tuners/boft/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,18 +310,11 @@ def update_layer(

self.reset_boft_parameters(adapter_name, init_weights)

weight = getattr(self, "weight", None)
if weight is not None:
# the layer is already completely initialized, this is an update
if weight.dtype.is_floating_point or weight.dtype.is_complex:
self.to(weight.device, dtype=weight.dtype)
else:
self.to(weight.device)

# set the boft block size and number
self.boft_block_size[adapter_name] = boft_block_size
self.boft_block_num[adapter_name] = boft_block_num

self._move_adapter_to_device_of_base_layer(adapter_name)
self.set_adapter(self.active_adapters)

def reset_boft_parameters(self, adapter_name, init_weights):
Expand Down Expand Up @@ -742,19 +735,13 @@ def update_layer(

self.reset_boft_parameters(adapter_name, init_weights)

weight = getattr(self, "weight", None)
if weight is not None:
# the layer is already completely initialized, this is an update
if weight.dtype.is_floating_point or weight.dtype.is_complex:
self.to(weight.device, dtype=weight.dtype)
else:
self.to(weight.device)
self.set_adapter(self.active_adapters)

# set the boft block size and number
self.boft_block_size[adapter_name] = boft_block_size
self.boft_block_num[adapter_name] = boft_block_num

self._move_adapter_to_device_of_base_layer(adapter_name)
self.set_adapter(self.active_adapters)

def merge(self, safe_merge: bool = False, adapter_names: Optional[list[str]] = None) -> None:
"""
Merge the active adapter weights into the base weights
Expand Down
4 changes: 2 additions & 2 deletions src/peft/tuners/ia3/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def update_layer(self, adapter_name, init_ia3_weights):
self.ia3_l[adapter_name] = nn.Parameter(weight)
if init_ia3_weights:
self.reset_ia3_parameters(adapter_name)
self.to(self.get_base_layer().weight.device)
self._move_adapter_to_device_of_base_layer(adapter_name)
self.set_adapter(self.active_adapters)

def reset_ia3_parameters(self, adapter_name):
Expand Down Expand Up @@ -210,7 +210,7 @@ def update_layer(self, adapter_name, init_ia3_weights):
self.ia3_l[adapter_name] = nn.Parameter(weight)
if init_ia3_weights:
self.reset_ia3_parameters(adapter_name)
self.to(self.get_base_layer().weight.device)
self._move_adapter_to_device_of_base_layer(adapter_name)
self.set_adapter(self.active_adapters)

def merge(self, safe_merge: bool = False, adapter_names: Optional[List[str]] = None) -> None:
Expand Down
8 changes: 1 addition & 7 deletions src/peft/tuners/loha/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,7 @@ def update_layer(
self.reset_adapter_parameters_random(adapter_name)

# Move new weights to device
weight = getattr(self.get_base_layer(), "weight", None)
if weight is not None:
# the layer is already completely initialized, this is an update
if weight.dtype.is_floating_point or weight.dtype.is_complex:
self.to(weight.device, dtype=weight.dtype)
else:
self.to(weight.device)
self._move_adapter_to_device_of_base_layer(adapter_name)
self.set_adapter(self.active_adapters)

def get_delta_weight(self, adapter_name: str) -> torch.Tensor:
Expand Down
8 changes: 1 addition & 7 deletions src/peft/tuners/lokr/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,7 @@ def update_layer(
self.reset_adapter_parameters_random(adapter_name)

# Move new weights to device
weight = getattr(self.get_base_layer(), "weight", None)
if weight is not None:
# the layer is already completely initialized, this is an update
if weight.dtype.is_floating_point or weight.dtype.is_complex:
self.to(weight.device, dtype=weight.dtype)
else:
self.to(weight.device)
self._move_adapter_to_device_of_base_layer(adapter_name)
self.set_adapter(self.active_adapters)

def get_delta_weight(self, adapter_name: str) -> torch.Tensor:
Expand Down
28 changes: 7 additions & 21 deletions src/peft/tuners/lora/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,8 @@ def update_layer(
elif init_lora_weights:
self.reset_lora_parameters(adapter_name, init_lora_weights)

# check weight and qweight (for GPTQ)
for weight_name in ("weight", "qweight"):
weight = getattr(self.get_base_layer(), weight_name, None)
if weight is not None:
# the layer is already completely initialized, this is an update
if weight.dtype.is_floating_point or weight.dtype.is_complex:
self.to(weight.device, dtype=weight.dtype)
else:
self.to(weight.device)
break
# call this before dora_init
self._move_adapter_to_device_of_base_layer(adapter_name)

if use_dora:
self.dora_init(adapter_name)
Expand Down Expand Up @@ -245,7 +237,8 @@ def dora_init(self, adapter_name: str) -> None:
lora_weight = lora_weight.half()
weight_norm = self._get_weight_norm(weight, lora_weight, scaling)

self.lora_magnitude_vector = nn.ParameterDict()
if self.lora_magnitude_vector is None:
self.lora_magnitude_vector = nn.ParameterDict()
self.lora_magnitude_vector[adapter_name] = nn.Parameter(weight_norm, requires_grad=True)
# add lora_magnitude_vector to the list of learnable parameters
self.adapter_layer_names = self.adapter_layer_names[:] + ("lora_magnitude_vector",)
Expand Down Expand Up @@ -638,12 +631,7 @@ def update_layer(self, adapter_name, r, lora_alpha, lora_dropout, init_lora_weig
elif init_lora_weights:
self.reset_lora_parameters(adapter_name, init_lora_weights)

base_layer = self.get_base_layer()
weight = getattr(base_layer, "weight", None)
if weight is not None:
# the layer is already completely initialized, this is an update
self.to(base_layer.weight.device, dtype=weight.dtype)

self._move_adapter_to_device_of_base_layer(adapter_name)
self.set_adapter(self.active_adapters)

def merge(self, safe_merge: bool = False, adapter_names: Optional[list[str]] = None) -> None:
Expand Down Expand Up @@ -861,10 +849,8 @@ def update_layer(self, adapter_name, r, lora_alpha, lora_dropout, init_lora_weig
elif init_lora_weights:
self.reset_lora_parameters(adapter_name, init_lora_weights)

weight = getattr(base_layer, "weight", None)
if weight is not None:
# the layer is already completely initialized, this is an update
self.to(base_layer.weight.device, dtype=weight.dtype)
# call this before dora_init
self._move_adapter_to_device_of_base_layer(adapter_name)

if use_dora:
self.dora_init(adapter_name)
Expand Down
8 changes: 1 addition & 7 deletions src/peft/tuners/lora/tp_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,7 @@ def update_layer(
if init_lora_weights:
self.reset_lora_parameters(adapter_name, init_lora_weights)

weight = getattr(self.get_base_layer(), "weight", None)
if weight is not None:
# the layer is already completely initialized, this is an update
if weight.dtype.is_floating_point or weight.dtype.is_complex:
self.to(weight.device, dtype=weight.dtype)
else:
self.to(weight.device)
self._move_adapter_to_device_of_base_layer(adapter_name)
self.set_adapter(self.active_adapters)

def forward(self, x: torch.Tensor, *args: Any, **kwargs: Any):
Expand Down
8 changes: 1 addition & 7 deletions src/peft/tuners/oft/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,7 @@ def update_layer(
self.reset_adapter_parameters_random(adapter_name)

# Move new weights to device
weight = getattr(self.get_base_layer(), "weight", None)
if weight is not None:
# the layer is already completely initialized, this is an update
if weight.dtype.is_floating_point or weight.dtype.is_complex:
self.to(weight.device, dtype=weight.dtype)
else:
self.to(weight.device)
self._move_adapter_to_device_of_base_layer(adapter_name)
self.set_adapter(self.active_adapters)

def unscale_layer(self, scale=None) -> None:
Expand Down
8 changes: 1 addition & 7 deletions src/peft/tuners/poly/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,7 @@ def update_layer(self, adapter_name, poly_config):

self.reset_poly_parameters(adapter_name, init_weights=poly_config.init_weights)

weight = getattr(self.get_base_layer(), "weight", None)
if weight is not None:
# the layer is already completely initialized, this is an update
if weight.dtype.is_floating_point or weight.dtype.is_complex:
self.to(weight.device, dtype=weight.dtype)
else:
self.to(weight.device)
self._move_adapter_to_device_of_base_layer(adapter_name)
self.set_adapter(self.active_adapters)

def reset_poly_parameters(self, adapter_name, init_weights):
Expand Down
32 changes: 32 additions & 0 deletions src/peft/tuners/tuners_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,38 @@ def delete_adapter(self, adapter_name: str) -> None:
)
self.set_adapter(remaining_adapters[0])

def _move_adapter_to_device_of_base_layer(self, adapter_name: str, device: Optional[torch.device] = None) -> None:
"""
Move the adapter of the given name to the device of the base layer.
"""
from peft.tuners.vera.buffer_dict import BufferDict

if device is None:
# check weight and qweight (for GPTQ)
for weight_name in ("weight", "qweight"):
weight = getattr(self.get_base_layer(), weight_name, None)
if weight is not None:
device = weight.device
dtype = weight.dtype
break
else:
# no break encountered: could not determine the device
return

# loop through all potential adapter layers and move them to the device of the base layer; be careful to only
# move this specific adapter to the device, as the other adapters could be on different devices
# see #1639
for adapter_layer_name in self.adapter_layer_names + self.other_param_names:
adapter_layer = getattr(self, adapter_layer_name, None)
if not isinstance(adapter_layer, (nn.ModuleDict, nn.ParameterDict, BufferDict)):
continue
if adapter_name not in adapter_layer:
continue
if weight.dtype.is_floating_point or weight.dtype.is_complex:
adapter_layer[adapter_name] = adapter_layer[adapter_name].to(device, dtype=dtype)
else:
adapter_layer[adapter_name] = adapter_layer[adapter_name].to(device)


def check_target_module_exists(config, key: str) -> bool | re.Match[str] | None:
"""A helper method to check if the passed module's key name matches any of the target modules in the adapter_config.
Expand Down
9 changes: 1 addition & 8 deletions src/peft/tuners/vera/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,7 @@ def update_layer(
if init_weights:
self.reset_vera_parameters(adapter_name, d_initial=d_initial)

weight = getattr(self.get_base_layer(), "weight", None)
if weight is not None:
# the layer is already completely initialized, this is an update
if weight.dtype.is_floating_point or weight.dtype.is_complex:
self.to(weight.device, dtype=weight.dtype)
else:
self.to(weight.device)

self._move_adapter_to_device_of_base_layer(adapter_name)
self.set_adapter(self.active_adapters)

def reset_vera_parameters(self, adapter_name, d_initial: float = 0.1):
Expand Down
Loading
Loading