Repository navigation
🚨 [Kernels] Fix kernel function registration
#45420
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dcd4295
fix attmpt
vasqu 348944b
proper fix - also works with deepspeed
vasqu 638cd8b
rely less on internals and add rotary to training
vasqu b997755
Merge branch 'main' into fix-kernels-loading-order
vasqu 4e8e748
move under the try as well
vasqu 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 |
|---|---|---|
|
|
@@ -4466,15 +4466,31 @@ def loss_function(self, value): | |
| self._loss_function = value | ||
|
|
||
| def kernelize(self, mode=None): | ||
| """Temporarily register hidden kernel wrappers so `kernelize` can discover and replace them.""" | ||
| if not is_kernels_available(): | ||
| raise ValueError( | ||
| "Kernels are not available. To use kernels, please install kernels using `pip install kernels`" | ||
| "Kernels are not available. To use kernels, please install kernels using `pip install -U kernels`" | ||
| ) | ||
| from kernels import Device, Mode, kernelize | ||
|
|
||
| mode = Mode.INFERENCE if not self.training else Mode.TRAINING if mode is None else mode | ||
| kernelize(self, device=Device(type=self.device.type), mode=mode) | ||
| self._use_kernels = True | ||
| def attach_hidden_kernels(module): | ||
| for name, fn in getattr(module, "_hidden_kernels", {}).items(): | ||
| if name not in dict(module.named_children()): | ||
| module.register_module(name, fn) | ||
|
|
||
| def detach_hidden_kernels(module): | ||
| for name in getattr(module, "_hidden_kernels", {}): | ||
| delattr(module, name) | ||
|
Comment on lines
+4476
to
+4483
Collaborator
Author
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. Removed the internals structure and rely on native APIs instead as suggested |
||
|
|
||
| try: | ||
| self.apply(attach_hidden_kernels) | ||
|
|
||
| mode = Mode.INFERENCE if not self.training else Mode.TRAINING if mode is None else mode | ||
| kernelize(self, device=Device(type=self.device.type), mode=mode) | ||
| self._use_kernels = True | ||
|
|
||
| finally: | ||
| self.apply(detach_hidden_kernels) | ||
|
|
||
| @property | ||
| def use_kernels(self) -> bool: | ||
|
|
||
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.
Not sure why this wasnt included for training before but it runs with deepspeed just fine + https://github.com/Dao-AILab/flash-attention/blob/b65ae6b175f2438de55601695b6a21971fc5e429/flash_attn/layers/rotary.py#L38-L90