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 for MooreThreads #3456

Open
wants to merge 1 commit 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: 8 additions & 1 deletion fastchat/model/model_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ def load_model(
import torch_npu
except ImportError:
warnings.warn("Ascend Extension for PyTorch is not installed.")
elif device == "musa":
kwargs = {"torch_dtype": torch.float16}
try:
import torch_musa
except ImportError:
warnings.warn("Musa Extension for PyTorch is not installed.")
else:
raise ValueError(f"Invalid device: {device}")

Expand Down Expand Up @@ -377,6 +383,7 @@ def load_model(
"mps",
"xpu",
"npu",
"musa",
):
model.to(device)

Expand Down Expand Up @@ -495,7 +502,7 @@ def add_model_args(parser):
parser.add_argument(
"--device",
type=str,
choices=["cpu", "cuda", "mps", "xpu", "npu"],
choices=["cpu", "cuda", "mps", "xpu", "npu", "musa"],
default="cuda",
help="The device type",
)
Expand Down
15 changes: 12 additions & 3 deletions fastchat/serve/model_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def __init__(
xft_config=xft_config,
debug=debug,
)
self.model_path = model_path
self.device = device
if self.tokenizer.pad_token == None:
self.tokenizer.pad_token = self.tokenizer.eos_token
Expand Down Expand Up @@ -165,6 +166,9 @@ def __process_embed_chunk(self, input_ids, attention_mask, **model_type_dict):
else:
data = model_output.hidden_states[-1]

#import pdb
#pdb.set_trace()
#sum_embeddings = data[:, 0]
if hasattr(self.model, "use_cls_pooling") and self.model.use_cls_pooling:
sum_embeddings = data[:, 0]
else:
Expand Down Expand Up @@ -245,9 +249,10 @@ def get_embeddings(self, params):
)
+ tokenizer.cls_token_id
)
chunk_input_ids = torch.cat(
[cls_tokens, chunk_input_ids], dim=-1
)
if "bge-m3" not in self.model_path.lower():
chunk_input_ids = torch.cat(
[cls_tokens, chunk_input_ids], dim=-1
)
mask = torch.ones(
(chunk_attention_mask.size(0), 1),
dtype=chunk_attention_mask.dtype,
Expand All @@ -260,6 +265,8 @@ def get_embeddings(self, params):
chunk_embeddings, token_num = self.__process_embed_chunk(
chunk_input_ids, chunk_attention_mask, **model_type_dict
)
import pdb
pdb.set_trace()
if (
hasattr(self.model, "use_cls_pooling")
and self.model.use_cls_pooling
Expand All @@ -271,6 +278,8 @@ def get_embeddings(self, params):

all_embeddings_tensor = torch.stack(all_embeddings)
embedding = torch.sum(all_embeddings_tensor, dim=0) / all_token_num
import pdb
pdb.set_trace()
normalized_embeddings = F.normalize(embedding, p=2, dim=1)

ret["token_num"] = all_token_num
Expand Down
Loading