Skip to content

Commit f5f678d

Browse files
tangankeCopilot
andauthored
Add OPCM configuration and implementation for distributed model merging (#205)
* Add OPCM configuration and implementation for distributed model merging * Update fusion_bench/method/opcm/opcm_general.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update fusion_bench/method/opcm/opcm_general.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Reorder class inheritance for OPCMForCLIP to improve clarity --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 4302a8a commit f5f678d

File tree

4 files changed

+402
-1
lines changed

4 files changed

+402
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# =============================================================================
2+
# FusionBench Method Configuration: OPCM
3+
# =============================================================================
4+
# Incrementally merges models via SVD projection and evaluation per step.
5+
# =============================================================================
6+
_target_: fusion_bench.method.opcm.opcm_general.OPCM
7+
# shuffle the order of the models
8+
shuffle_order: true
9+
# the scaling factor for the SVD projection
10+
alpha: 0.5
11+
# the random seed to use
12+
seed: null
13+
# save the merged model on every step
14+
save_on_every_step: true
15+
# evaluate the merged model on every step
16+
evaluate_on_every_step: true
17+
# the number of ray actors to use for distributed merging
18+
num_ray_actors: 0

examples/opcm/distributed_opcm.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from transformers import CLIPVisionModel
2+
3+
from fusion_bench import BaseModelPool
4+
from fusion_bench.constants.paths import DEFAULT_CONFIG_PATH
5+
from fusion_bench.method.opcm.opcm_general import OPCM
6+
from fusion_bench.utils import timeit_context
7+
8+
config_file = DEFAULT_CONFIG_PATH / "method/opcm/opcm_general.yaml"
9+
10+
11+
with timeit_context("loading models"):
12+
models = {
13+
"_pretrained_": CLIPVisionModel.from_pretrained("openai/clip-vit-base-patch32"),
14+
"sun397": CLIPVisionModel.from_pretrained(
15+
"tanganke/clip-vit-base-patch32_sun397"
16+
),
17+
"stanford-cars": CLIPVisionModel.from_pretrained(
18+
"tanganke/clip-vit-base-patch32_stanford-cars"
19+
),
20+
}
21+
22+
algo: OPCM = OPCM.from_yaml(config_file)
23+
algo.num_ray_actors = 2 # set the number of ray actors to use for parallel merging
24+
algo.run(BaseModelPool(models))

fusion_bench/method/opcm/opcm.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030

3131

3232
class OPCMForCLIP(
33-
BaseAlgorithm,
3433
LightningFabricMixin,
3534
SimpleProfilerMixin,
35+
BaseAlgorithm,
3636
):
3737
def __init__(
3838
self,
@@ -220,6 +220,9 @@ def run(self, modelpool: BaseModelPool):
220220
return merged_model
221221

222222
def save_merged_model(self, merged_model: CLIPVisionModel, step: int):
223+
if self.log_dir is None:
224+
print("Log dir is None, skip saving merged model.")
225+
return
223226
os.makedirs(Path(self.log_dir) / "checkpoints", exist_ok=True)
224227
merged_model.save_pretrained(
225228
Path(self.log_dir) / "checkpoints" / f"merged_model_{step}"

0 commit comments

Comments
 (0)