Skip to content

Commit d124389

Browse files
authored
Merge pull request #703 from TengJiao33/codex/fix-grace-wise-ultraedit-multigpu
Fix multi-GPU placement for GRACE, WISE, and ULTRAEDIT
2 parents fa28b62 + fcdd658 commit d124389

3 files changed

Lines changed: 6 additions & 2 deletions

File tree

easyeditor/models/grace/GRACE.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def __init__(self, config, model, device):
5353
edit_module = parent_module(self.model, brackets_to_periods(self.layer))
5454
layer_name = self.layer.rsplit(".", 1)[-1]
5555
original_layer = getattr(edit_module, layer_name)
56+
self.device = original_layer.weight.device
5657
if type(original_layer) is not GRACEAdapter:
5758
setattr(edit_module, layer_name, GRACEAdapter(config, original_layer, transpose=transpose).to(self.device))
5859
self.original_layer = copy.deepcopy(original_layer)

easyeditor/models/wise/WISE.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def euc(query, key, config, act_mask=None, infer=False):
3939
return topk.values.mean()
4040

4141
if act_mask is not None:
42+
act_mask = act_mask.to(device=l2_norm.device, dtype=l2_norm.dtype)
4243
return torch.sum(l2_norm * act_mask, dim=1) / torch.sum(act_mask, dim=1)
4344
else:
4445
return torch.mean(l2_norm, dim=-1)

easyeditor/trainer/algs/malmen/util.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,16 @@ def forward_hook(
125125
inputs: Tuple[torch.FloatTensor],
126126
outputs: Tuple[torch.FloatTensor]
127127
):
128-
self.keys = inputs[0][cache_indices].detach()
128+
layer_cache_indices = tuple(index.to(inputs[0].device) for index in cache_indices)
129+
self.keys = inputs[0][layer_cache_indices].detach()
129130

130131
def backward_hook(
131132
module: nn.Module,
132133
inputs_grad: Tuple[torch.FloatTensor],
133134
outputs_grad: Tuple[torch.FloatTensor]
134135
):
135-
self.values_grad = outputs_grad[0][cache_indices].detach()
136+
layer_cache_indices = tuple(index.to(outputs_grad[0].device) for index in cache_indices)
137+
self.values_grad = outputs_grad[0][layer_cache_indices].detach()
136138

137139
self.handles = [
138140
module.register_forward_hook(forward_hook),

0 commit comments

Comments
 (0)