-
Notifications
You must be signed in to change notification settings - Fork 166
[Advanced Compiler]Add TE.reglu&dreglu #1055
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
AdvancedCompiler
wants to merge
11
commits into
flagos-ai:master
Choose a base branch
from
AdvancedCompiler:add-TE.reglu&dreglu
base: master
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.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ce65528
create reglu&dreglu
zhoubo567 96d0cf7
Merge branch 'add-TE.reglu&dreglu' of https://github.com/AdvancedComp…
zhoubo567 72edbb4
change ops
zhoubo567 8806fc9
Merge branch 'flagos-ai:master' into add-TE.reglu&dreglu
AdvancedCompiler ca0d044
Fix: Apply pre-commit formatting fixes
zhoubo567 459213a
formatMerge branch 'add-TE.reglu&dreglu' of https://github.com/Advanc…
zhoubo567 4470cbc
Merge branch 'master' into add-TE.reglu&dreglu
zhoubo567 d6ba899
Merge branch 'master' of https://github.com/AdvancedCompiler/FlagGems…
zhoubo567 1661a12
mergeMerge branch 'master' of https://github.com/AdvancedCompiler/Fla…
zhoubo567 1925bf0
update test_transformer_engine_perf.py and add dreglu®lu benchmark
zhoubo567 99807d0
pre-commit
zhoubo567 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
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
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,175 @@ | ||
| import logging | ||
| from typing import Any, Optional | ||
|
|
||
| import torch | ||
| import triton | ||
| import triton.language as tl | ||
|
|
||
| from flag_gems import runtime | ||
| from flag_gems.utils import libentry, libtuner | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| @libentry() | ||
| @libtuner( | ||
| configs=runtime.get_tuned_config("gated_activation"), | ||
| key=["M", "N"], | ||
| ) | ||
| @triton.jit | ||
| def dreglu_kernel( | ||
| grad_output_ptr, | ||
| input_ptr, | ||
| grad_input_ptr, | ||
| M, | ||
| N, | ||
| stride_grad_out_m, | ||
| stride_grad_out_n, | ||
| stride_in_m, | ||
| stride_in_n, | ||
| stride_grad_in_m, | ||
| stride_grad_in_n, | ||
| BLOCK_M: tl.constexpr, | ||
| BLOCK_N: tl.constexpr, | ||
| ): | ||
| pid_m = tl.program_id(axis=0) | ||
| pid_n = tl.program_id(axis=1) | ||
| offs_m = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) | ||
| offs_n = pid_n * BLOCK_N + tl.arange(0, BLOCK_N) | ||
| grad_output_ptr += ( | ||
| offs_m[:, None] * stride_grad_out_m + offs_n[None, :] * stride_grad_out_n | ||
| ) | ||
| input_ptr_a = ( | ||
| input_ptr + offs_m[:, None] * stride_in_m + offs_n[None, :] * stride_in_n | ||
| ) | ||
| input_ptr_b = ( | ||
| input_ptr + offs_m[:, None] * stride_in_m + (offs_n[None, :] + N) * stride_in_n | ||
| ) | ||
| grad_input_ptr_a = ( | ||
| grad_input_ptr | ||
| + offs_m[:, None] * stride_grad_in_m | ||
| + offs_n[None, :] * stride_grad_in_n | ||
| ) | ||
| grad_input_ptr_b = ( | ||
| grad_input_ptr | ||
| + offs_m[:, None] * stride_grad_in_m | ||
| + (offs_n[None, :] + N) * stride_grad_in_n | ||
| ) | ||
| mask = (offs_m[:, None] < M) & (offs_n[None, :] < N) | ||
| grad_out = tl.load(grad_output_ptr, mask=mask, other=0.0) | ||
| block_a = tl.load(input_ptr_a, mask=mask, other=0.0) | ||
| block_b = tl.load(input_ptr_b, mask=mask, other=0.0) | ||
| relu_a = tl.maximum(block_a, 0.0) | ||
| d_relu_a = tl.where(block_a > 0, 1.0, 0.0) | ||
| grad_a = grad_out * d_relu_a * block_b | ||
| grad_b = grad_out * relu_a | ||
| tl.store(grad_input_ptr_a, grad_a, mask=mask) | ||
| tl.store(grad_input_ptr_b, grad_b, mask=mask) | ||
|
|
||
|
|
||
| @libentry() | ||
| @libtuner( | ||
| configs=runtime.get_tuned_config("gated_activation"), | ||
| key=["M", "N_OUT"], | ||
| ) | ||
| @triton.jit | ||
| def reglu_kernel( | ||
| x_ptr, | ||
| y_ptr, | ||
| M, | ||
| N_OUT, | ||
| stride_x_m, | ||
| stride_x_n, | ||
| stride_y_m, | ||
| stride_y_n, | ||
| BLOCK_M: tl.constexpr, | ||
| BLOCK_N: tl.constexpr, | ||
| ): | ||
| pid_m = tl.program_id(axis=0) | ||
| pid_n = tl.program_id(axis=1) | ||
| offs_m = pid_m * BLOCK_M + tl.arange(0, BLOCK_M) | ||
| offs_n = pid_n * BLOCK_N + tl.arange(0, BLOCK_N) | ||
| x_ptr_a = x_ptr + offs_m[:, None] * stride_x_m + offs_n[None, :] * stride_x_n | ||
| x_ptr_b = ( | ||
| x_ptr + offs_m[:, None] * stride_x_m + (offs_n[None, :] + N_OUT) * stride_x_n | ||
| ) | ||
| y_ptr = y_ptr + offs_m[:, None] * stride_y_m + offs_n[None, :] * stride_y_n | ||
| mask = (offs_m[:, None] < M) & (offs_n[None, :] < N_OUT) | ||
| block_a = tl.load(x_ptr_a, mask=mask, other=0.0) | ||
| block_b = tl.load(x_ptr_b, mask=mask, other=0.0) | ||
| gate = tl.where(block_a > 0, block_a, 0.0) | ||
| output = gate * block_b | ||
| tl.store(y_ptr, output, mask=mask) | ||
|
|
||
|
|
||
| def reglu(input_tensor: torch.Tensor, quantizer: Optional[Any] = None) -> torch.Tensor: | ||
| shape = input_tensor.shape | ||
| if input_tensor.dim() < 1: | ||
| raise ValueError("Input tensor must have at least 1 dimension.") | ||
| last_dim = shape[-1] | ||
| if last_dim % 2 != 0: | ||
| raise ValueError( | ||
| f"The last dimension of the input tensor must be even, but got {last_dim}." | ||
| ) | ||
| N_OUT = last_dim // 2 | ||
| M = input_tensor.numel() // last_dim | ||
| if input_tensor.numel() == 0: | ||
| output_shape = (*shape[:-1], N_OUT) | ||
| return torch.empty( | ||
| output_shape, device=input_tensor.device, dtype=input_tensor.dtype | ||
| ) | ||
| input_2d = input_tensor.contiguous().view(M, last_dim) | ||
| output_2d = torch.empty( | ||
| (M, N_OUT), device=input_tensor.device, dtype=input_tensor.dtype | ||
| ) | ||
| grid = lambda META: ( | ||
| triton.cdiv(M, META["BLOCK_M"]), | ||
| triton.cdiv(N_OUT, META["BLOCK_N"]), | ||
| ) | ||
| reglu_kernel[grid]( | ||
| input_2d, | ||
| output_2d, | ||
| M, | ||
| N_OUT, | ||
| input_2d.stride(0), | ||
| input_2d.stride(1), | ||
| output_2d.stride(0), | ||
| output_2d.stride(1), | ||
| ) | ||
| output_shape = (*shape[:-1], N_OUT) | ||
| return output_2d.view(output_shape) | ||
|
|
||
|
|
||
| def dreglu( | ||
| grad_output: torch.Tensor, | ||
| input_tensor: torch.Tensor, | ||
| quantizer: Optional[Any] = None, | ||
| ) -> torch.Tensor: | ||
| shape = input_tensor.shape | ||
| if shape[:-1] != grad_output.shape[:-1] or shape[-1] != 2 * grad_output.shape[-1]: | ||
| raise ValueError( | ||
| f"Shape mismatch: input {shape} vs grad_output {grad_output.shape}" | ||
| ) | ||
| M = grad_output.numel() // grad_output.shape[-1] | ||
| N = grad_output.shape[-1] | ||
| grad_output_2d = grad_output.contiguous().view(M, N) | ||
| input_2d = input_tensor.contiguous().view(M, 2 * N) | ||
| grad_input = torch.empty_like(input_2d) | ||
| grid = lambda META: ( | ||
| triton.cdiv(M, META["BLOCK_M"]), | ||
| triton.cdiv(N, META["BLOCK_N"]), | ||
| ) | ||
| dreglu_kernel[grid]( | ||
| grad_output_2d, | ||
| input_2d, | ||
| grad_input, | ||
| M, | ||
| N, | ||
| grad_output_2d.stride(0), | ||
| grad_output_2d.stride(1), | ||
| input_2d.stride(0), | ||
| input_2d.stride(1), | ||
| grad_input.stride(0), | ||
| grad_input.stride(1), | ||
| ) | ||
| return grad_input.view(shape) | ||
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.
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.