-
Notifications
You must be signed in to change notification settings - Fork 32
[bugfix]: Add compatibility handling for Qwen3.5 GatedDeltaNet padding-free training and fix create_causal_mask patch when cache_positions removed in transformers >5.3.0 #202
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
Draft
meichangsu1
wants to merge
6
commits into
modelscope:main
Choose a base branch
from
meichangsu1:padding_free_bufix_ljl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+145
−51
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eae429b
feat: add support for forward methods with incompatible kwargs
aa9bf73
fix: support native padding-free in GatedDeltaNet and improve kwargs …
584ad66
fix(sequence_parallel): restore global query length for no-cache pref…
93eea38
fix: remove native padding-free version check for GDN patch
a0443e5
fix: conditionally patch chunk_gated_delta_rule for transformers < 5.9.0
1822cdd
fix gemini code review
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| # Copyright (c) ModelScope Contributors. All rights reserved. | ||
| import inspect | ||
| import math | ||
| import torch | ||
| import torch.distributed as dist | ||
|
|
@@ -28,6 +29,38 @@ def is_qwen3_omni(model): | |
| return 'qwen3_omni' in mt | ||
|
|
||
|
|
||
| def _call_with_supported_kwargs(fn, *args, **kwargs): | ||
| signature = inspect.signature(fn) | ||
| if not any(param.kind == inspect.Parameter.VAR_KEYWORD for param in signature.parameters.values()): | ||
| kwargs = {key: value for key, value in kwargs.items() if key in signature.parameters} | ||
| return fn(*args, **kwargs) | ||
|
|
||
|
|
||
| def _call_create_causal_mask(fn, config, input_embeds, attention_mask, cache_position_or_past_key_values, *args, | ||
| **kwargs): | ||
| if 'cache_position' in inspect.signature(fn).parameters: | ||
| return _call_with_supported_kwargs( | ||
| fn, | ||
| config, | ||
| input_embeds, | ||
| attention_mask, | ||
| cache_position_or_past_key_values, | ||
| *args, | ||
| **kwargs, | ||
| ) | ||
| if cache_position_or_past_key_values is None and 'past_key_values' in kwargs: | ||
| return _call_with_supported_kwargs(fn, config, input_embeds, attention_mask, *args, **kwargs) | ||
| return _call_with_supported_kwargs( | ||
| fn, | ||
| config, | ||
| input_embeds, | ||
| attention_mask, | ||
| cache_position_or_past_key_values, | ||
| *args, | ||
| **kwargs, | ||
| ) | ||
|
|
||
|
|
||
| # main content copied from ms-swift | ||
| class SequenceParallel: | ||
|
|
||
|
|
@@ -77,59 +110,89 @@ def _prepare_flash_attn(self, base_model: torch.nn.Module): | |
| try: | ||
| from transformers import masking_utils | ||
|
|
||
| _origin_flash_attention_mask = masking_utils.flash_attention_mask | ||
|
|
||
| # Patch attention masks for SP: avoid masking when full sequence is reconstructed. | ||
| def flash_attention_mask(batch_size, | ||
| cache_position, | ||
| kv_length, | ||
| kv_offset=0, | ||
| mask_function=masking_utils.causal_mask_function, | ||
| attention_mask=None, | ||
| **kwargs): | ||
| if self.world_size == 1: | ||
| return _origin_flash_attention_mask(batch_size, cache_position, kv_length, kv_offset, mask_function, | ||
| attention_mask, **kwargs) | ||
| if attention_mask is not None: | ||
| if attention_mask.all(): | ||
| attention_mask = None | ||
|
|
||
| return attention_mask | ||
|
|
||
| masking_utils.flash_attention_mask = flash_attention_mask | ||
| masking_utils.ALL_MASK_ATTENTION_FUNCTIONS._global_mapping['flash_attention_2'] = flash_attention_mask | ||
|
|
||
| def sdpa_mask(batch_size, cache_position, kv_length, *args, **kwargs): | ||
| if self.world_size == 1: | ||
| return masking_utils.ALL_MASK_ATTENTION_FUNCTIONS._global_mapping['sdpa_origin'](batch_size, | ||
| cache_position, | ||
| kv_length, *args, | ||
| **kwargs) | ||
| device = cache_position.device | ||
| cache_position = self.real_position_ids[0] | ||
| cache_position = self.pad(cache_position, padding_value=-1, position_ids=self.real_position_ids, dim=0) | ||
| cache_position = torch.arange(0, cache_position.shape[0], device=device) | ||
| kv_length = cache_position.shape[0] | ||
| return masking_utils.ALL_MASK_ATTENTION_FUNCTIONS._global_mapping['sdpa_origin'](batch_size, | ||
| cache_position, | ||
| kv_length, *args, | ||
| **kwargs) | ||
|
|
||
| masking_utils.ALL_MASK_ATTENTION_FUNCTIONS._global_mapping[ | ||
| 'sdpa_origin'] = masking_utils.ALL_MASK_ATTENTION_FUNCTIONS._global_mapping['sdpa'] | ||
| origin_sdpa = masking_utils.ALL_MASK_ATTENTION_FUNCTIONS._global_mapping['sdpa'] | ||
| origin_uses_cache_position = 'cache_position' in inspect.signature(origin_sdpa).parameters | ||
|
|
||
| def sdpa_mask(batch_size, q_length=None, kv_length=None, *args, **kwargs): | ||
| q_length = q_length if q_length is not None else kwargs.pop('cache_position', None) | ||
| device = q_length.device if torch.is_tensor(q_length) else kwargs.pop('device', None) | ||
| if device is None: | ||
| device = self.real_position_ids.device | ||
|
|
||
| cache_position = None | ||
| if self.world_size > 1: | ||
| padded_position_ids = self.pad( | ||
| self.real_position_ids[0], | ||
| padding_value=-1, | ||
| position_ids=self.real_position_ids, | ||
| dim=0, | ||
| ) | ||
| global_length = padded_position_ids.shape[0] | ||
| if origin_uses_cache_position: | ||
| cache_position = torch.arange(0, global_length, device=device) | ||
| kv_length = global_length | ||
| else: | ||
| # Newer Transformers passes q_length/q_offset instead of cache_position. In SP training, | ||
| # create_causal_mask may still see the local shard length, so restore the global query length | ||
| # only for the no-cache prefill path; cache/sliding paths keep their upstream offsets. | ||
| q_offset = kwargs.get('q_offset', 0) | ||
| kv_offset = kwargs.get('kv_offset', 0) | ||
| no_cache_offsets = ((not torch.is_tensor(q_offset) and q_offset == 0) | ||
| and (not torch.is_tensor(kv_offset) and kv_offset == 0)) | ||
| if no_cache_offsets: | ||
| q_length = global_length | ||
| attention_mask = kwargs.get('attention_mask') | ||
| if attention_mask is not None and torch.is_tensor(attention_mask): | ||
| kv_length = attention_mask.shape[-1] | ||
| else: | ||
| kv_length = global_length | ||
|
|
||
| if origin_uses_cache_position: | ||
| if cache_position is None: | ||
| cache_position = q_length if torch.is_tensor(q_length) else torch.arange( | ||
| q_length, device=device) | ||
| return origin_sdpa(batch_size, cache_position, kv_length, *args, **kwargs) | ||
|
|
||
| return origin_sdpa(batch_size, q_length, kv_length, *args, device=device, **kwargs) | ||
|
meichangsu1 marked this conversation as resolved.
|
||
|
|
||
| masking_utils.ALL_MASK_ATTENTION_FUNCTIONS._global_mapping['sdpa_origin'] = origin_sdpa | ||
| masking_utils.ALL_MASK_ATTENTION_FUNCTIONS._global_mapping['sdpa'] = sdpa_mask | ||
|
|
||
| def create_causal_mask(config, input_embeds, attention_mask, cache_position, *args, **kwargs): | ||
| def create_causal_mask(config, | ||
| input_embeds, | ||
| attention_mask, | ||
| cache_position_or_past_key_values=None, | ||
| *args, | ||
| **kwargs): | ||
| if self.world_size == 1: | ||
| return masking_utils.origin_create_causal_mask(config, input_embeds, attention_mask, cache_position, | ||
| *args, **kwargs) | ||
| return _call_create_causal_mask( | ||
| masking_utils.origin_create_causal_mask, | ||
| config, | ||
| input_embeds, | ||
| attention_mask, | ||
| cache_position_or_past_key_values, | ||
| *args, | ||
| **kwargs, | ||
| ) | ||
| input_embeds = torch.ones( | ||
| (input_embeds.shape[0], input_embeds.shape[1] * self.sp_world_size, input_embeds.shape[2]), | ||
| dtype=input_embeds.dtype, | ||
| device=input_embeds.device) | ||
| cache_position = torch.arange(0, input_embeds.shape[1], device=input_embeds.device) | ||
| return masking_utils.origin_create_causal_mask(config, input_embeds, attention_mask, cache_position, | ||
| *args, **kwargs) | ||
| if 'cache_position' in inspect.signature(masking_utils.origin_create_causal_mask).parameters: | ||
| cache_position_or_past_key_values = torch.arange( | ||
| 0, | ||
| input_embeds.shape[1], | ||
| device=input_embeds.device, | ||
| ) | ||
|
Comment on lines
+181
to
+186
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| return _call_create_causal_mask( | ||
| masking_utils.origin_create_causal_mask, | ||
| config, | ||
| input_embeds, | ||
| attention_mask, | ||
| cache_position_or_past_key_values, | ||
| *args, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| masking_utils.origin_create_causal_mask = masking_utils.create_causal_mask | ||
| masking_utils.create_causal_mask = create_causal_mask | ||
|
|
||
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
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.