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

Encoding with model in float16 leads to "mat1 and mat2 must have the same dtype" error #2887

Closed
ahmedkooli opened this issue Aug 14, 2024 · 3 comments · Fixed by #2889
Closed

Comments

@ahmedkooli
Copy link

Issue:

Hey! When loading the model dunzhang/stella_en_400M_v5 in torch.float16 and encoding a text, I run into this error: "RuntimeError: mat1 and mat2 must have the same dtype, but got Half and Float".

Code to reproduce:

from sentence_transformers import SentenceTransformer
import torch

model_name = "dunzhang/stella_en_400M_v5"
dev = torch.device("cuda")
model = SentenceTransformer(model_name, device=dev, trust_remote_code=True, 
                            model_kwargs={"torch_dtype": torch.float16})#.half()

sentences = ["This is a sentence."]
output = model.encode(sentences)

Uncommenting the .half() will fix the problem. However this problem doesn't appear with other models such as sentence-transformers/all-MiniLM-L6-v2.

Versions:

sentence-transformers==3.0.1
torch==2.4.0+cu121

Thanks for your help

@ir2718
Copy link
Contributor

ir2718 commented Aug 14, 2024

Hi,

this looks like a bug to me. What happens is if the loaded model contains any class other than Transformer, the parameters in it won't be cast to the torch_dtype value. Have a look (here).

@ahmedkooli
Copy link
Author

ahmedkooli commented Aug 16, 2024

Hey, thanks for your answer. What would you suggest? Should the code be changed to

module = module_class.load(module_path, **kwargs)

@ir2718
Copy link
Contributor

ir2718 commented Aug 16, 2024

@ahmedkooli

Have a look at the PR i added, that should help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants