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

Enable intfloat/e5-mistral-7b-instruct model with 32k token lens on hpu device #2715

Open
wants to merge 2 commits into
base: master
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
11 changes: 9 additions & 2 deletions sentence_transformers/SentenceTransformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ def encode(
convert_to_tensor: bool = False,
device: str = None,
normalize_embeddings: bool = False,
kwargs: Optional[Dict[str, Any]] = None,
) -> Union[List[Tensor], ndarray, Tensor]:
"""
Computes sentence embeddings.
Expand Down Expand Up @@ -485,11 +486,17 @@ def encode(
if self.device.type == "hpu":
if "input_ids" in features:
curr_tokenize_len = features["input_ids"].shape
additional_pad_len = 2 ** math.ceil(math.log2(curr_tokenize_len[1])) - curr_tokenize_len[1]
if curr_tokenize_len[1] > 4096:
additional_pad_len = math.ceil(curr_tokenize_len[1] / 128) * 128 - curr_tokenize_len[1]

extra_features.update(kwargs["hpu_kwargs"])
else:
additional_pad_len = 2 ** math.ceil(math.log2(curr_tokenize_len[1])) - curr_tokenize_len[1]

features["input_ids"] = torch.cat(
(
features["input_ids"],
torch.ones((curr_tokenize_len[0], additional_pad_len), dtype=torch.int8),
torch.zeros((curr_tokenize_len[0], additional_pad_len), dtype=torch.int8),
),
-1,
)
Expand Down
18 changes: 18 additions & 0 deletions sentence_transformers/models/Transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from torch import nn
from transformers import AutoConfig, AutoModel, AutoTokenizer, MT5Config, T5Config
from sentence_transformers.util import get_device_name


class Transformer(nn.Module):
Expand Down Expand Up @@ -114,6 +115,23 @@ def forward(self, features):
if "token_type_ids" in features:
trans_features["token_type_ids"] = features["token_type_ids"]

device = get_device_name()
curr_tokenize_len = features["input_ids"].shape
if (
device == "hpu"
and curr_tokenize_len[1] > 4096
and "attn_softmax_bf16" in features
and "reuse_cache" in features
and "use_flash_attention" in features
and "flash_attention_recompute" in features
and "flash_attention_causal_mask" in features
):
trans_features["attn_softmax_bf16"] = features["attn_softmax_bf16"]
trans_features["reuse_cache"] = features["reuse_cache"]
trans_features["use_flash_attention"] = features["use_flash_attention"]
trans_features["flash_attention_recompute"] = features["flash_attention_recompute"]
trans_features["flash_attention_causal_mask"] = features["flash_attention_causal_mask"]

output_states = self.auto_model(**trans_features, return_dict=False)
output_tokens = output_states[0]

Expand Down
Loading