-
Notifications
You must be signed in to change notification settings - Fork 271
DeepSeek_v3 support #1735
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
Merged
Merged
DeepSeek_v3 support #1735
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
aca9778
Resolve rebase conflicts on DeepSeek_v3
srajabos a8fdecd
Update __init__.py
srajabos 3e7a00e
Update __init__.py
srajabos 8629818
Update modeling_deepseek_v3.py
srajabos a161a36
Support optimized KV cache, static MOE and expert parallelism
skavulya da80fba
Commented out attention_mask assertion for the mmlu tests
pallavijaini0525 34f8ff7
Support optimized fusedDSPA, RoPE and RMS
srajabos 937deeb
Commented out attention_mask assertionn
pallavijaini0525 e32aadd
Change references to deepseekv2 to deepseekv3
skavulya 37a0431
Override load_state_dict to support deepseek-R1
skavulya 7b0b9cf
Added dynamic MoE changes
srajabos 33e792c
Fix multicard expert parallelism for deepseekv3
skavulya 71de78c
Delete duplicate tests accidentally copied
skavulya 1336c14
Refactor deepseek_v3 and add clarifying comments
skavulya e02c6de
Fix edge case in expert slices in DeepSeek-V3
skavulya 96a3473
Add deepseekv3 to list of models supporting reuse_cache
skavulya 5e95f58
Style fix in modeling_utils
skavulya 9bb5601
Updated the README.md for the deepseek-r1-bf16
pallavijaini0525 c66d9d9
Updated the README.md with hostfile reference
pallavijaini0525 8a1a460
Move load_state_dict to modeling_utils_transformers
skavulya eb03fad
Removed the deepseek tests from CI, will enable when FP8 is supported
pallavijaini0525 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
optimum/habana/transformers/modeling_utils_transformers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import os | ||
| from typing import Optional, Union | ||
| from zipfile import is_zipfile | ||
|
|
||
| import torch | ||
| from packaging import version | ||
| from transformers.integrations import is_deepspeed_zero3_enabled | ||
| from transformers.modeling_utils import is_fsdp_enabled, is_local_dist_rank_0 | ||
| from transformers.utils import ( | ||
| is_safetensors_available, | ||
| ) | ||
|
|
||
|
|
||
| if is_safetensors_available(): | ||
| from safetensors import safe_open | ||
| from safetensors.torch import load_file as safe_load_file | ||
|
|
||
|
|
||
| def load_state_dict( | ||
| checkpoint_file: Union[str, os.PathLike], | ||
| is_quantized: bool = False, | ||
| map_location: Optional[Union[str, torch.device]] = None, | ||
| weights_only: bool = True, | ||
| ): | ||
| """ | ||
| Reads a PyTorch checkpoint file, returning properly formatted errors if they arise. | ||
|
|
||
| Copied from transformers v4.48.2 for DeepSeek-R1 support https://github.com/huggingface/transformers/blob/b673c16cad81c71f70903a9a63f5b5f06014aa9e/src/transformers/modeling_utils.py#L493 | ||
| Delete after upgrade transformers v4.45.2 to v4.48 | ||
| """ | ||
| if checkpoint_file.endswith(".safetensors") and is_safetensors_available(): | ||
| # Check format of the archive | ||
| with safe_open(checkpoint_file, framework="pt") as f: | ||
| metadata = f.metadata() | ||
| if metadata is not None and metadata.get("format") not in ["pt", "tf", "flax", "mlx"]: | ||
| raise OSError( | ||
| f"The safetensors archive passed at {checkpoint_file} does not contain the valid metadata. Make sure " | ||
| "you save your model with the `save_pretrained` method." | ||
| ) | ||
| return safe_load_file(checkpoint_file) | ||
| try: | ||
| if map_location is None: | ||
| if ( | ||
| ( | ||
| is_deepspeed_zero3_enabled() | ||
| and torch.distributed.is_initialized() | ||
| and torch.distributed.get_rank() > 0 | ||
| ) | ||
| or (is_fsdp_enabled() and not is_local_dist_rank_0()) | ||
| ) and not is_quantized: | ||
| map_location = "meta" | ||
| else: | ||
| map_location = "cpu" | ||
| extra_args = {} | ||
| # mmap can only be used with files serialized with zipfile-based format. | ||
| if ( | ||
| isinstance(checkpoint_file, str) | ||
| and map_location != "meta" | ||
| and version.parse(torch.__version__) >= version.parse("2.1.0") | ||
| and is_zipfile(checkpoint_file) | ||
| ): | ||
| extra_args = {"mmap": True} | ||
| weights_only_kwarg = {"weights_only": weights_only} | ||
| return torch.load( | ||
| checkpoint_file, | ||
| map_location=map_location, | ||
| **weights_only_kwarg, | ||
| **extra_args, | ||
| ) | ||
| except Exception as e: | ||
| try: | ||
| with open(checkpoint_file) as f: | ||
| if f.read(7) == "version": | ||
| raise OSError( | ||
| "You seem to have cloned a repository without having git-lfs installed. Please install " | ||
| "git-lfs and run `git lfs install` followed by `git lfs pull` in the folder " | ||
| "you cloned." | ||
| ) | ||
| else: | ||
| raise ValueError( | ||
| f"Unable to locate the file {checkpoint_file} which is necessary to load this pretrained " | ||
| "model. Make sure you have saved the model properly." | ||
| ) from e | ||
| except (UnicodeDecodeError, ValueError): | ||
| raise OSError( | ||
| f"Unable to load weights from pytorch checkpoint file for '{checkpoint_file}' " | ||
| f"at '{checkpoint_file}'. " | ||
| "If you tried to load a PyTorch model from a TF 2.0 checkpoint, please set from_tf=True." | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from .configuration_deepseek_v3 import DeepseekV3Config | ||
| from .modeling_deepseek_v3 import DeepseekV3ForCausalLM |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.