-
Notifications
You must be signed in to change notification settings - Fork 33.1k
[kernels] refactor function kernel calling #41577
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
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 |
|---|---|---|
|
|
@@ -30,12 +30,11 @@ | |
| from ...activations import ACT2FN | ||
| from ...configuration_utils import PreTrainedConfig | ||
| from ...generation import GenerationMixin | ||
| from ...integrations.hub_kernels import lazy_load_kernel | ||
| from ...modeling_layers import GradientCheckpointingLayer | ||
| from ...modeling_utils import PreTrainedModel | ||
| from ...utils import ModelOutput, auto_docstring, logging | ||
| from ...utils.import_utils import ( | ||
| is_causal_conv1d_available, | ||
| is_kernels_available, | ||
| is_mamba_ssm_available, | ||
| is_mambapy_available, | ||
| ) | ||
|
|
@@ -162,33 +161,6 @@ def reset(self): | |
| self.ssm_states[layer_idx].zero_() | ||
|
|
||
|
|
||
| def _lazy_load_causal_conv1d(): | ||
| global _causal_conv1d_cache | ||
| if _causal_conv1d_cache is not None: | ||
| return _causal_conv1d_cache | ||
|
|
||
| if is_kernels_available(): | ||
| from kernels import get_kernel | ||
|
|
||
| try: | ||
| _causal_conv1d_kernel = get_kernel("kernels-community/causal-conv1d") | ||
| except FileNotFoundError: | ||
| # no kernel binary match, fallback to slow path | ||
| _causal_conv1d_cache = (None, None) | ||
| else: | ||
| _causal_conv1d_cache = (_causal_conv1d_kernel.causal_conv1d_update, _causal_conv1d_kernel.causal_conv1d_fn) | ||
| elif is_causal_conv1d_available(): | ||
| from causal_conv1d import causal_conv1d_fn, causal_conv1d_update | ||
|
|
||
| _causal_conv1d_cache = (causal_conv1d_update, causal_conv1d_fn) | ||
| else: | ||
| _causal_conv1d_cache = (None, None) | ||
| return _causal_conv1d_cache | ||
|
|
||
|
|
||
| _causal_conv1d_cache = None | ||
|
|
||
|
|
||
| def rms_forward(hidden_states, variance_epsilon=1e-6): | ||
| """ | ||
| Calculates simple RMSNorm with no learnable weights. `MambaRMSNorm` will | ||
|
|
@@ -268,7 +240,12 @@ def __init__(self, config: FalconMambaConfig, layer_idx: int): | |
| self.rms_eps = config.mixer_rms_eps | ||
|
|
||
| def warn_slow_implementation(self): | ||
| causal_conv1d_update, causal_conv1d_fn = _lazy_load_causal_conv1d() | ||
| causal_conv1d = lazy_load_kernel("causal-conv1d") | ||
| causal_conv1d_update, causal_conv1d_fn = ( | ||
| (causal_conv1d.causal_conv1d_update, causal_conv1d.causal_conv1d_fn) | ||
| if causal_conv1d is not None | ||
| else (None, None) | ||
| ) | ||
|
Comment on lines
+243
to
+248
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. nice and simple |
||
| is_fast_path_available = all( | ||
| (selective_state_update, selective_scan_fn, causal_conv1d_fn, causal_conv1d_update, mamba_inner_fn) | ||
| ) | ||
|
|
@@ -323,7 +300,12 @@ def cuda_kernels_forward( | |
| ) | ||
|
|
||
| else: | ||
| causal_conv1d_update, causal_conv1d_fn = _lazy_load_causal_conv1d() | ||
| causal_conv1d = lazy_load_kernel("causal-conv1d") | ||
| causal_conv1d_update, causal_conv1d_fn = ( | ||
| (causal_conv1d.causal_conv1d_update, causal_conv1d.causal_conv1d_fn) | ||
| if causal_conv1d is not None | ||
| else (None, None) | ||
| ) | ||
| hidden_states, gate = projected_states.chunk(2, dim=1) | ||
|
|
||
| if attention_mask is not None: | ||
|
|
@@ -518,7 +500,12 @@ def forward( | |
| cache_position: Optional[torch.LongTensor] = None, | ||
| attention_mask: Optional[torch.LongTensor] = None, | ||
| ): | ||
| causal_conv1d_update, causal_conv1d_fn = _lazy_load_causal_conv1d() | ||
| causal_conv1d = lazy_load_kernel("causal-conv1d") | ||
| causal_conv1d_update, causal_conv1d_fn = ( | ||
| (causal_conv1d.causal_conv1d_update, causal_conv1d.causal_conv1d_fn) | ||
| if causal_conv1d is not None | ||
| else (None, None) | ||
| ) | ||
| is_fast_path_available = all( | ||
| (selective_state_update, selective_scan_fn, causal_conv1d_fn, causal_conv1d_update, mamba_inner_fn) | ||
| ) | ||
|
|
||
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
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.
should be None, if none default to kernel mapping python will cry otherwise!