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

support musa backend in FlagEmbedding #1350

Merged
merged 3 commits into from
Feb 7, 2025

Conversation

qiyulei-mt
Copy link
Contributor

What does this PR do?
It supports utilization of Moore Threads GPU in FlagEmbedding. The input of devices can be 0, [0], ["musa:0"], "musa:0". For example, the case below, you can set devices as 0, [0], ["musa:0"], "musa:0".

import os
from FlagEmbedding import FlagModel

model = FlagModel(
    'BAAI/bge-small-en-v1.5',
    query_instruction_for_retrieval="Represent this sentence for searching relevant passages: ",
    query_instruction_format="{}{}",
    devices="musa:3",
    pooling_method='cls',
    cache_dir=os.getenv('HF_HUB_CACHE', None),
)

queries = [
    "What is the capital of France?",
    "What is the population of China?",
] * 100
passages = [
    "Paris is the capital of France.",
    "The population of China is over 1.4 billion people."
] * 100

queries_embeddings = model.encode_queries(queries)
passages_embeddings = model.encode_corpus(passages)

cos_scores = queries_embeddings @ passages_embeddings.T
print(cos_scores[:2, :2])

After running the code, you can get corresponding results.

@caizhi-mt
Copy link

@hanhainebula please take a look at this PR

@hanhainebula
Copy link
Collaborator

Looks great! However, a few modifications may be necessary. Before calling torch.musa.is_available(), it would be prudent to check if torch has the musa attribute. Otherwise, it may raise an AttributeError for other backends. A possible workable modification could be:

...
if hasattr(torch, 'musa') and torch.musa.is_available():
...

Hello, @qiyulei-mt, could you please update all the corresponding code? Once that's done, I will merge this PR. Thanks!

@hanhainebula
Copy link
Collaborator

Hello, @qiyulei-mt and @caizhi-mt. I just directly modified the corresponding code in qiyulei-mt:musa_support. Now this PR will be merged. Thanks for your submission.

@hanhainebula hanhainebula merged commit 44e5525 into FlagOpen:master Feb 7, 2025
@caizhi-mt
Copy link

Hello, @qiyulei-mt and @caizhi-mt. I just directly modified the corresponding code in qiyulei-mt:musa_support. Now this PR will be merged. Thanks for your submission.

Thank you very much for your work. We will continue to contribute code to this repository.

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 this pull request may close these issues.

3 participants