-
Notifications
You must be signed in to change notification settings - Fork 603
[Stacked PR 2/5] Import local Tokamax GDN forward kernel with custom remat for backward pass support #5152
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
Open
Rohan-Bierneni
wants to merge
1
commit into
rbierneni-gdn-1-ci-hygiene
from
rbierneni-gdn-2-fwd-kernel
Open
[Stacked PR 2/5] Import local Tokamax GDN forward kernel with custom remat for backward pass support #5152
Changes from all commits
Commits
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 | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,75 @@ | ||||||||
| # Copyright 2026 Google LLC | ||||||||
| # | ||||||||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||
| # you may not use this file except in compliance with the License. | ||||||||
| # You may obtain a copy of the License at | ||||||||
| # | ||||||||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||
| # | ||||||||
| # Unless required by applicable law or agreed to in writing, software | ||||||||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||
| # See the License for the specific language governing permissions and | ||||||||
| # limitations under the License. | ||||||||
| # ============================================================================== | ||||||||
|
|
||||||||
| """In-VMEM causal depthwise Conv1D computation.""" | ||||||||
|
|
||||||||
| import jax | ||||||||
| import jax.numpy as jnp | ||||||||
|
|
||||||||
| try: | ||||||||
| from maxtext.models.kernels.gdn import config | ||||||||
| except (ImportError, ModuleNotFoundError): | ||||||||
| try: | ||||||||
| from maxtext.src.maxtext.models.kernels.gdn import config | ||||||||
| except (ImportError, ModuleNotFoundError): | ||||||||
| from . import config | ||||||||
|
|
||||||||
|
|
||||||||
| def causal_conv1d( | ||||||||
| real_sizes: jax.Array, # [seq] | ||||||||
| lhs: jax.Array, # [seq, chunk, q, dim_size] | ||||||||
| conv_weight: jax.Array, # [prev_kernel_size, 1, dim_size] | ||||||||
| conv_bias: jax.Array | None, # [dim_size] | ||||||||
| cfg: config.GDNConfig, | ||||||||
| ) -> tuple[jax.Array, jax.Array]: | ||||||||
| """Perform causal Conv1D. Returns Conv1D output and convolution states.""" | ||||||||
|
|
||||||||
| assert lhs.ndim == 4 | ||||||||
|
|
||||||||
| out_list = [] | ||||||||
|
|
||||||||
| for c_idx in range(cfg.chunk_size): | ||||||||
| out = jnp.zeros((cfg.seq_tile_size, 1, cfg.dim_size), jnp.float32) | ||||||||
|
|
||||||||
| end_idx = c_idx + cfg.prev_kernel_size | ||||||||
| start_idx = 1 + end_idx - cfg.kernel_size | ||||||||
|
Comment on lines
+46
to
+47
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. The calculation of
Suggested change
|
||||||||
| for k in range(cfg.kernel_size): | ||||||||
| lhs_curr = lhs[:, start_idx + k] | ||||||||
| out += lhs_curr * conv_weight[k : k + 1] | ||||||||
|
|
||||||||
| if conv_bias is not None: | ||||||||
| out += conv_bias.reshape(1, 1, -1) | ||||||||
|
|
||||||||
| out_list.append(out) | ||||||||
|
|
||||||||
| # Last prev_kernel_size elements needs to be returned as conv_state. However, | ||||||||
| # real_sizes may be smaller than chunk_size. Therefore, slicing last | ||||||||
| # prev_kernel_size elements does not guarantee numeric correctness. Instead, | ||||||||
| # kernel iterate each rows and perform masking to fetch correct values. | ||||||||
| # NOTE: lhs[:, : prev_kernel_size] can be skipped since they were loaded from | ||||||||
| # previous conv states. | ||||||||
| new_conv_state = lhs[:, 1 : cfg.kernel_size] | ||||||||
| real_sizes = real_sizes.reshape(-1, 1, 1, 1) | ||||||||
| # NOTE: Even though for loop is invoked twice, since they are static loops, | ||||||||
| # compiler will perform loop fusion. | ||||||||
| for c_idx in range(2, cfg.chunk_size + 1): | ||||||||
| row_end = c_idx + cfg.prev_kernel_size | ||||||||
| new_conv_state = jnp.where( | ||||||||
| c_idx == real_sizes, | ||||||||
| lhs[:, c_idx:row_end], | ||||||||
| new_conv_state, | ||||||||
| ) | ||||||||
|
|
||||||||
| return jnp.stack(out_list, axis=1), new_conv_state | ||||||||
Oops, something went wrong.
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.
The comment indicates that
conv_weighthas shape[prev_kernel_size, 1, dim_size], but it actually has shape[kernel_size, 1, dim_size]. Updating the comment prevents confusion.