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

Fixed TIES merging to calculate sign before applying weights #239

Merged
merged 1 commit into from
Feb 14, 2024
Merged
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
23 changes: 14 additions & 9 deletions server/lorax_server/utils/merges/strategies.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC
from collections import defaultdict
import copy
from typing import TYPE_CHECKING, Dict, List, Tuple, Type
from typing import TYPE_CHECKING, Dict, List, Tuple, Type, Union

import torch
from peft import LoraConfig
Expand All @@ -17,8 +17,11 @@
from lorax_server.utils.adapter import ModuleMap


def _apply_weights(tensors: List[torch.Tensor], w: torch.Tensor) -> torch.Tensor:
t = torch.stack(tensors, dim=0)
def _apply_weights(tensors: Union[torch.Tensor, List[torch.Tensor]], w: torch.Tensor) -> torch.Tensor:
if isinstance(tensors, torch.Tensor):
t = tensors
else:
t = torch.stack(tensors, dim=0)

# element-wise weighting of each task tensor
# need to unsqueeze weights to match task tensor dimensions
Expand Down Expand Up @@ -50,11 +53,12 @@ def __init__(self, density: float, majority_sign_method: str = "total", **kwargs
def merge(self, task_tensors: List[torch.Tensor], weights: torch.Tensor) -> torch.Tensor:
# sparsify
task_tensors = [prune(tensor, self.density, method="magnitude") for tensor in task_tensors]
task_tensors = torch.stack(task_tensors, dim=0)

# elect sign before applying weights
majority_sign_mask = calculate_majority_sign_mask(task_tensors, method=self.majority_sign_method)
weighted_task_tensors = _apply_weights(task_tensors, weights)

# elect sign
majority_sign_mask = calculate_majority_sign_mask(weighted_task_tensors, method=self.majority_sign_method)

# disjoint merge
return disjoint_merge(weighted_task_tensors, majority_sign_mask)

Expand All @@ -78,11 +82,12 @@ def __init__(self, density: float, majority_sign_method: str = "total", **kwargs
def merge(self, task_tensors: List[torch.Tensor], weights: torch.Tensor) -> torch.Tensor:
# sparsify
task_tensors = [prune(tensor, self.density, method="random", rescale=True) for tensor in task_tensors]
task_tensors = torch.stack(task_tensors, dim=0)

# elect sign before applying weights
majority_sign_mask = calculate_majority_sign_mask(task_tensors, method=self.majority_sign_method)
weighted_task_tensors = _apply_weights(task_tensors, weights)

# elect sign
majority_sign_mask = calculate_majority_sign_mask(weighted_task_tensors, method=self.majority_sign_method)

# disjoint merge
mixed_task_tensors = disjoint_merge(weighted_task_tensors, majority_sign_mask)
return mixed_task_tensors
Expand Down
Loading