-
Notifications
You must be signed in to change notification settings - Fork 33.7k
GptOss experts implementation #43227
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
Changes from 27 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
2aff4a8
experts impl gpt oss
IlyasMoutawwakil 9958efb
no need to transpose dequantized experts
IlyasMoutawwakil b23e1ff
skip test_reverse_loading_mapping
IlyasMoutawwakil e28f155
fix custom gating
IlyasMoutawwakil e57d0a8
Merge branch 'main' into gpt-oss-experts-impl
IlyasMoutawwakil be08fe4
revert transposition and simply support transposed experts to avoid m…
IlyasMoutawwakil e1dba4d
style
IlyasMoutawwakil 0261a46
don't rely on weight shapes as they can be square matrices
IlyasMoutawwakil 5bd25c7
no need to relaod
IlyasMoutawwakil 846adca
fallback to eager
IlyasMoutawwakil b1a71a7
Update src/transformers/models/gpt_oss/modeling_gpt_oss.py
IlyasMoutawwakil 9dbed89
fix
IlyasMoutawwakil 2f3fd11
force 16 bytes alignmenet during weight loading
IlyasMoutawwakil dd377e1
simplify logic
IlyasMoutawwakil 52e0778
quantization conversions should be applied first
IlyasMoutawwakil 1c49112
avoid baddbmm as it is less performant / less optimizable by max-auto…
IlyasMoutawwakil 4b0323c
no need for logger
IlyasMoutawwakil aa34996
Merge branch 'main' into gpt-oss-experts-impl
IlyasMoutawwakil f094c31
add comment explaining limitation
IlyasMoutawwakil 221f9bd
standarize operations and only reshape when needed
IlyasMoutawwakil 944afb5
Merge branch 'main' into gpt-oss-experts-impl
IlyasMoutawwakil 1fc01dc
fixup conversion and test
vasqu d820713
Update src/transformers/conversion_mapping.py
IlyasMoutawwakil 71fdb18
force alignment docstring
IlyasMoutawwakil e852cbb
move default apply gate
IlyasMoutawwakil d698dcb
offsets
IlyasMoutawwakil 5c2ca3c
Merge branch 'main' into gpt-oss-experts-impl
IlyasMoutawwakil d6631bb
add docs and make kernel_config optional
IlyasMoutawwakil 4f7226d
use reshapes as they are equivalent to views when memory is contiguous
IlyasMoutawwakil 2117303
fix and better notes
IlyasMoutawwakil 944a0ec
reshapes instead of views
IlyasMoutawwakil 1a0ea12
Merge branch 'main' into gpt-oss-experts-impl
IlyasMoutawwakil 16e6536
keep model saving and reloading in grouped_mm test to catch misalignm…
IlyasMoutawwakil 75ab275
Merge branch 'main' into gpt-oss-experts-impl
IlyasMoutawwakil 711a652
Merge branch 'main' into gpt-oss-experts-impl
IlyasMoutawwakil 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -440,6 +440,42 @@ def reverse_op(self) -> ConversionOps: | |
| return ErnieFuseAndSplitTextVisionExperts(stack_dim=self.stack_dim, concat_dim=self.concat_dim) | ||
|
|
||
|
|
||
| class Force16BytesAlignment(ConversionOps): | ||
| """ | ||
| Ensures that the given tensor is 16-bytes aligned in memory and clones it if not. | ||
| This garantees 16-bytes alignmenet for kernels / implementations that use TMA or SIMD instructions like torch._grouped_mm. | ||
| """ | ||
|
Comment on lines
+444
to
+447
Collaborator
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. very nice 🫡 |
||
|
|
||
| @torch.no_grad() | ||
| def convert( | ||
| self, input_dict: dict[str, torch.Tensor], source_patterns: list[str], target_patterns: list[str], **kwargs | ||
| ) -> dict[str, torch.Tensor]: | ||
| target_pattern = self.get_target_pattern(input_dict, source_patterns, target_patterns) | ||
| tensors = next(iter(input_dict.values())) | ||
| tensor = tensors[0] if isinstance(tensors, list) else tensors | ||
| tensor = tensor.clone() if tensor.data_ptr() % 16 != 0 else tensor | ||
| return {target_pattern: tensor} | ||
|
|
||
| def get_target_pattern( | ||
| self, input_dict: dict[str, torch.Tensor], source_patterns: list[str], target_patterns: list[str] | ||
| ) -> str: | ||
| if len(input_dict) != 1: | ||
| raise ValueError("Undefined Operation encountered!") | ||
| # Here it's the first operation of a chain, so return the source | ||
| if len(target_patterns) > 1: | ||
| if len(source_patterns) == 1: | ||
| return source_patterns[0] | ||
| else: | ||
| raise ValueError("Undefined Operation encountered!") | ||
| # Here it's the only operation, or the last operation in a chain, so we return the target | ||
| else: | ||
| return target_patterns[0] | ||
|
|
||
| @property | ||
| def reverse_op(self) -> ConversionOps: | ||
| return Force16BytesAlignment() | ||
|
|
||
|
|
||
| @dataclass(slots=True) | ||
| class WeightTransform: | ||
| source_patterns: str | list[str] = field(init=True) | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure it makes a difference at all no? Because the operation are ordered by length of collected tensors I thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah now that I'm digging deeper into the weight loader I think the reason I got the error above is because it's not possible to cascade converters (i.e., applying model-specific conversions on top of tensors created by the dequantization conversions). Not because you can't match one tensor with two converters (that's a valid limitation, but not the one happening here in gpt oss).
I added $ to the end of Force16BytesAlignement source pattern to fix my error without changing this order. Basically making sure that the sources of the mxfp dequant converter and Force16BytesAlignement are exclusive.
I will revert the line change and make this comment instead:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool, as we talked offline let's add more comments about how to handle the weight converter