Skip to content
Open
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
40 changes: 17 additions & 23 deletions mace/tools/finetuning_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,10 @@ def load_foundations_elements(

if load_readout:
# Transferring readouts
for i, readout in enumerate(model.readouts):
for readout, foundation_readout in zip(model.readouts, model_foundations.readouts):
if readout.__class__.__name__ == "LinearReadoutBlock":
model_readouts_zero_linear_weight = readout.linear.weight.clone()
model_readouts_zero_linear_weight = (
model_foundations.readouts[i]
foundation_readout
.linear.weight.view(num_channels_foundation, -1)
.repeat(1, len(model_heads))
.flatten()
Expand All @@ -217,17 +216,16 @@ def load_foundations_elements(
), "Readout block must have linear_1 or linear_mid"
if hasattr(readout, "linear_1"):
shape_input_1 = (
model_foundations.readouts[i]
foundation_readout
.linear_1.__dict__["irreps_out"]
.num_irreps
)
shape_output_1 = readout.linear_1.__dict__["irreps_out"].num_irreps
else:
raise ValueError("Readout block must have linear_1")
if hasattr(readout, "linear_1"):
model_readouts_one_linear_1_weight = readout.linear_1.weight.clone()
model_readouts_one_linear_1_weight = (
model_foundations.readouts[i]
foundation_readout
.linear_1.weight.view(num_channels_foundation, -1)
.repeat(1, len(model_heads))
.flatten()
Expand All @@ -236,10 +234,9 @@ def load_foundations_elements(
readout.linear_1.weight = torch.nn.Parameter(
model_readouts_one_linear_1_weight
)
if readout.linear_1.bias is not None:
model_readouts_one_linear_1_bias = readout.linear_1.bias.clone()
if readout.linear_1.bias is not None and readout.linear_1.bias.nelement() != 0:
model_readouts_one_linear_1_bias = (
model_foundations.readouts[i]
foundation_readout
.linear_1.bias.view(-1)
.repeat(len(model_heads))
.clone()
Expand All @@ -249,7 +246,7 @@ def load_foundations_elements(
)
if hasattr(readout, "linear_mid"):
readout.linear_mid.weight = torch.nn.Parameter(
model_foundations.readouts[i]
foundation_readout
.linear_mid.weight.view(
shape_input_1,
shape_input_1,
Expand All @@ -260,28 +257,25 @@ def load_foundations_elements(
/ ((shape_input_1) / (shape_output_1)) ** 0.5
)
# if it has biases transfer them too
if readout.linear_mid.bias is not None:
if readout.linear_mid.bias is not None and readout.linear_mid.bias.nelement() != 0:
readout.linear_mid.bias = torch.nn.Parameter(
model_foundations.readouts[i]
foundation_readout
.linear_mid.bias.repeat(len(model_heads))
.clone()
)
if hasattr(readout, "linear_2"):
model_readouts_one_linear_2_weight = readout.linear_2.weight.clone()
model_readouts_one_linear_2_weight = model_foundations.readouts[
i
].linear_2.weight.view(shape_input_1, -1).repeat(
len(model_heads), len(model_heads)
).flatten().clone() / (
((shape_input_1) / (shape_output_1)) ** 0.5
)
model_readouts_one_linear_2_weight = (foundation_readout
.linear_2.weight.view(shape_input_1, -1).repeat(
len(model_heads), len(model_heads)
).flatten().clone() / (
((shape_input_1) / (shape_output_1)) ** 0.5
))
readout.linear_2.weight = torch.nn.Parameter(
model_readouts_one_linear_2_weight
)
if readout.linear_2.bias is not None:
model_readouts_one_linear_2_bias = readout.linear_2.bias.clone()
if readout.linear_2.bias is not None and readout.linear_2.bias.nelement() != 0:
model_readouts_one_linear_2_bias = (
model_foundations.readouts[i]
foundation_readout
.linear_2.bias.view(-1)
.repeat(len(model_heads))
.flatten()
Expand Down
Loading