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

fix(model): Fix device MPS load llama error #1033

Merged
merged 1 commit into from
Jan 5, 2024
Merged
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
1 change: 1 addition & 0 deletions dbgpt/model/llm/monkey_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def forward(
past_key_value: Optional[Tuple[torch.Tensor]] = None,
output_attentions: bool = False,
use_cache: bool = False,
padding_mask: Optional[torch.LongTensor] = None,
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
bsz, q_len, _ = hidden_states.size()

Expand Down
19 changes: 15 additions & 4 deletions dbgpt/model/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,22 @@ def huggingface_loader(llm_adapter: LLMModelAdapter, model_params: ModelParamete

elif device == "mps":
kwargs = {"torch_dtype": torch.float16}
from dbgpt.model.llm.monkey_patch import (
replace_llama_attn_with_non_inplace_operations,
)

replace_llama_attn_with_non_inplace_operations()
import transformers

version = tuple(int(v) for v in transformers.__version__.split("."))
if version < (4, 35, 0):
from dbgpt.model.llm.monkey_patch import (
replace_llama_attn_with_non_inplace_operations,
)

# NOTE: Recent transformers library seems to fix the mps issue, also
# it has made some changes causing compatibility issues with our
# original patch. So we only apply the patch for older versions.

# Avoid bugs in mps backend by not using in-place operations.
replace_llama_attn_with_non_inplace_operations()

else:
raise ValueError(f"Invalid device: {device}")

Expand Down
Loading