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

add backbone override to hydra #997

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/fairchem/core/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ def __init__(
# if finetune_config is provided, then attempt to load the model from the given finetune checkpoint
starting_model = None
if finetune_config is not None:
# Make it hard to sneak more fields into finetuneconfig
assert (
len(set(finetune_config.keys()) - {"starting_checkpoint", "override"})
== 0
)
starting_model: HydraModel = load_model_and_weights_from_checkpoint(
finetune_config["starting_checkpoint"]
)
Expand All @@ -260,6 +265,10 @@ def __init__(
assert isinstance(
starting_model, HydraModel
), "Can only finetune starting from other hydra models!"
# TODO this is a bit hacky to overrride attrs in the backbone
if "override" in finetune_config:
for key, value in finetune_config["override"].items():
setattr(starting_model.backbone, key, value)

if backbone is not None:
backbone = copy.deepcopy(backbone)
Expand Down
32 changes: 32 additions & 0 deletions tests/core/e2e/test_e2e_finetune_hydra.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,38 @@ def test_finetune_hydra_retain_backbone(tutorial_val_src):
)


def test_finetune_hydra_override(tutorial_val_src):
with tempfile.TemporaryDirectory() as orig_ckpt_dir:
starting_ckpt = make_checkpoint(orig_ckpt_dir, tutorial_val_src, 0)
# now finetune a the model with the checkpoint from the first job
with tempfile.TemporaryDirectory() as ft_temp_dir:
ft_yml = Path("tests/core/models/test_configs/test_finetune_hydra.yml")
ck_ft_path = os.path.join(ft_temp_dir, "checkpoint_ft.pt")
model_config = {
"name": "hydra",
"finetune_config": {
"starting_checkpoint": starting_ckpt,
"override": {"forward": None},
},
"heads": {
"energy": {"module": "equiformer_v2_energy_head"},
"forces": {"module": "equiformer_v2_force_head"},
},
}

# TODO add a better test for override when we get there
# for now just override .forward() with None
with pytest.raises(TypeError):
run_main_with_ft_hydra(
tempdir=ft_temp_dir,
yaml=ft_yml,
data_src=tutorial_val_src,
run_args={"seed": 1000},
model_config=model_config,
output_checkpoint=ck_ft_path,
)


def test_finetune_hydra_data_only(tutorial_val_src):
with tempfile.TemporaryDirectory() as orig_ckpt_dir:
starting_ckpt = make_checkpoint(orig_ckpt_dir, tutorial_val_src, 0)
Expand Down
Loading