-
Notifications
You must be signed in to change notification settings - Fork 39
Workspace #10
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
Workspace #10
Changes from all commits
Commits
Show all changes
2 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,111 @@ | ||
| import argparse | ||
| import itertools | ||
| import os | ||
| from dataclasses import dataclass | ||
| from pathlib import Path | ||
| from typing import List, Optional | ||
|
|
||
| DTYPE_MAP = { | ||
| "fp16": "cutlass::half_t", | ||
| "bf16": "cutlass::bfloat16_t", | ||
| } | ||
|
|
||
| SM = [80] # Sm80 kernels support up to | ||
| HEAD_DIMENSIONS = [32, 64, 96, 128, 192, 256] | ||
| IS_CAUSAL = ["false", "true"] | ||
| NAMESPACE_INCLUDE = '#include "namespace_config.h"\n' | ||
|
|
||
| def get_fwd_template() -> str: | ||
| return NAMESPACE_INCLUDE + """#include "flash_fwd_launch_template.h" | ||
|
|
||
| namespace FLASH_NAMESPACE {{ | ||
|
|
||
| template<> | ||
| void run_mha_fwd_<{DTYPE}, {HEAD_DIM}, {IS_CAUSAL}>(Flash_fwd_params ¶ms, cudaStream_t stream) {{ | ||
| run_mha_fwd_hdim{HEAD_DIM}<{DTYPE}, {IS_CAUSAL}>(params, stream); | ||
| }} | ||
|
|
||
| }} // namespace FLASH_NAMESPACE""" | ||
|
|
||
| def get_fwd_split_template() -> str: | ||
| return NAMESPACE_INCLUDE + """#include "flash_fwd_launch_template.h" | ||
|
|
||
| namespace FLASH_NAMESPACE {{ | ||
|
|
||
| template void run_mha_fwd_splitkv_dispatch<{DTYPE}, {HEAD_DIM}, {IS_CAUSAL}>(Flash_fwd_params ¶ms, cudaStream_t stream); | ||
|
|
||
| }} // namespace FLASH_NAMESPACE""" | ||
|
|
||
| def get_bwd_template() -> str: | ||
| return NAMESPACE_INCLUDE + """#include "flash_bwd_launch_template.h" | ||
|
|
||
| namespace FLASH_NAMESPACE {{ | ||
|
|
||
| template<> | ||
| void run_mha_bwd_<{DTYPE}, {HEAD_DIM}, {IS_CAUSAL}>(Flash_bwd_params ¶ms, cudaStream_t stream) {{ | ||
| run_mha_bwd_hdim{HEAD_DIM}<{DTYPE}, {IS_CAUSAL}>(params, stream); | ||
| }} | ||
|
|
||
| }} // namespace FLASH_NAMESPACE""" | ||
|
|
||
| @dataclass | ||
| class Kernel: | ||
| sm: int | ||
| dtype: str | ||
| head_dim: int | ||
| is_causal: str | ||
| direction: str | ||
|
|
||
| @property | ||
| def template(self) -> str: | ||
| template_funcs = { | ||
| "fwd": get_fwd_template, | ||
| # "bwd": get_bwd_template, | ||
| # "fwd_split": get_fwd_split_template | ||
| } | ||
| template_func = template_funcs[self.direction] | ||
| return template_func().format( | ||
| DTYPE=DTYPE_MAP[self.dtype], | ||
| HEAD_DIM=self.head_dim, | ||
| IS_CAUSAL=self.is_causal | ||
| ) | ||
|
|
||
| @property | ||
| def filename(self) -> str: | ||
| return f"flash_{self.direction}_hdim{self.head_dim}_{self.dtype}_{'causal_' if self.is_causal == 'true' else ''}sm{self.sm}.cu" | ||
|
|
||
| def get_all_kernels() -> List[Kernel]: | ||
| for direction in ["fwd"]: #, "fwd_split", "bwd"]: | ||
| for dtype, head_dim, is_causal, sm in itertools.product(DTYPE_MAP.keys(), HEAD_DIMENSIONS, IS_CAUSAL, SM): | ||
| yield Kernel(sm=sm, dtype=dtype, head_dim=head_dim, is_causal=is_causal, direction=direction) | ||
|
|
||
| def write_kernel(kernel: Kernel, autogen_dir: Path) -> None: | ||
| prelude = """// Copyright (c) 2025, Jingze Shi and Tri Dao. | ||
| // Splitting the different head dimensions to different files to speed up compilation. | ||
| // This file is auto-generated. See "generate_kernels.py"\n""" | ||
| content = prelude + kernel.template | ||
| (autogen_dir / kernel.filename).write_text(content) | ||
|
|
||
| def main(output_dir: Optional[str]) -> None: | ||
| if output_dir is None: | ||
| output_dir = Path(__file__).parent | ||
| else: | ||
| output_dir = Path(output_dir) | ||
|
|
||
| for kernel in get_all_kernels(): | ||
| write_kernel(kernel, output_dir) | ||
|
|
||
| if __name__ == "__main__": | ||
| parser = argparse.ArgumentParser( | ||
| prog="generate_kernels", | ||
| description="Generate the flash_attention kernels template instantiations", | ||
| ) | ||
| parser.add_argument( | ||
| "-o", | ||
| "--output_dir", | ||
| required=False, | ||
| help="Where to generate the kernels " | ||
| " will default to the current directory ", | ||
| ) | ||
| args = parser.parse_args() | ||
| main(args.output_dir) |
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 global typedef for index_t now makes local alias definitions in structs redundant. Ensure any previous local definitions are removed so that all modules use the global index_t consistently.