Last active
June 1, 2023 19:26
-
-
Save seyyedaliayati/22e1e04eceaaa25bf736c02af7d3ba08 to your computer and use it in GitHub Desktop.
Compare two models in huggingface format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from transformers import AutoModel | |
import torch | |
# Load the models | |
model1 = AutoModel.from_pretrained('bigcode/starcoderbase') # replace with your model names | |
model2 = AutoModel.from_pretrained('bigcode/starcoder') | |
# Retrieve the state dictionaries | |
state_dict1 = model1.state_dict() | |
state_dict2 = model2.state_dict() | |
# For each parameter in the state dictionaries | |
for param1, param2 in zip(state_dict1.items(), state_dict2.items()): | |
# Unpack the parameter names and tensors | |
name1, tensor1 = param1 | |
name2, tensor2 = param2 | |
# Check the names are the same (they should be, if the models are of the same architecture) | |
if name1 == name2: | |
try: | |
# Calculate the difference between the two tensors | |
difference = torch.nn.functional.mse_loss(tensor1, tensor2) | |
print(f"Difference in {name1}: {difference}") | |
except: | |
print(f"Cannot compare {name1} and {name2}") | |
else: | |
print(f"Parameter names do not match: {name1}, {name2}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment