- 
                Notifications
    You must be signed in to change notification settings 
- Fork 292
Add Flash Attn example on amd mi300 series #682
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
Conversation
…nd clarity (tile-ai#668) - Enhanced buffer index handling to address precision issues by removing redundant operations. - Streamlined the logic for determining buffer overlaps, ensuring more accurate conflict detection. - Updated related documentation to reflect changes in buffer management practices.
…ed flexibility - Introduced a new input.txt file for configurable parameters. - Modified the example_amd_flash_attn_fwd.py script to allow for a wider range of configurations, including additional options for num_stages, enable_rasterization, and k_pack. - Streamlined the main function for better clarity and organization. - Added a new test script to facilitate running the example with specified parameters.
… example with swizzle layout annotations - Deleted input.txt and test.sh files as they are no longer needed. - Updated example_amd_flash_attn_fwd.py to include swizzle layout annotations for shared memory, improving bank conflict avoidance. - Reintroduced swizzle usage in the kernel for better performance.
| 👋 Hi! Thank you for contributing to the TileLang project. Please remember to run  We appreciate you taking this step! Our team will review your contribution, and we look forward to your awesome work! 🚀 | 
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.
Summary of Changes
Hello @Alex4210987, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request introduces a new example demonstrating an optimized FlashAttention-2 forward pass specifically tailored for AMD MI300 series GPUs. It leverages the tilelang framework to implement a highly performant attention mechanism, including autotuning capabilities and correctness verification against a PyTorch reference implementation. Minor adjustments to a core reduction template are also included to support this new functionality.
Highlights
- New FlashAttention-2 Example for AMD MI300: I've added a new Python example (examples/amd/example_amd_flash_attn_fwd.py) that implements a FlashAttention-2 forward pass. This example is designed to run on AMD MI300 series GPUs, leveragingtilelangfor optimized performance.
- Autotuning and Verification: The new FlashAttention-2 example includes an autotuner to discover optimal configurations for various parameters (e.g., block_M,block_N,threads,num_stages). It also provides a PyTorch reference implementation and asserts correctness, ensuring the optimized kernel produces accurate results.
- Core Kernel Implementation: The FlashAttention-2 logic is encapsulated within a T.prim_functhat handles block-wise processing of Q, K, V tensors, shared memory management, GEMM operations, and causal masking, following the principles of FlashAttention-2.
- HIP Reduction Template Enhancement: I've updated the AllReducestruct insrc/tl_templates/hip/reduce.hby adding athread_offsettemplate parameter. This modification provides more flexibility for reduction operations within the HIP backend, which may be utilized by the new FlashAttention kernel.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description | 
|---|---|---|
| Code Review | /gemini review | Performs a code review for the current pull request in its current state. | 
| Pull Request Summary | /gemini summary | Provides a summary of the current pull request in its current state. | 
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. | 
| Help | /gemini help | Displays a list of available commands. | 
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
- 
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩ 
- Updated function names for clarity, changing `get_v2_configs` to `get_configs` and `fast_flashattn_v2` to `fast_flashattn`. - Streamlined the main function by renaming `main_v2` to `main` and adjusting the corresponding calls. - Removed outdated comments and improved code organization for better readability.
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.
Code Review
This pull request introduces a new example for the FlashAttention forward pass on AMD MI300 series GPUs using tilelang. The implementation follows the principles of FlashAttention-2 and includes autotuning, verification against a PyTorch reference, and benchmarking. A minor change is also made to a C++ template in reduce.h.
|  | ||
| def ref_program(Q, K, V, is_causal, groups=1): | ||
| assert Q.size( | ||
| 2) == K.size(2) * groups, f"Q heads {Q.size(2)} K heads {K.size(2)} groups {groups}" | 
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.
| """Generates configurations for the autotuner, tailored for FA-2 style parallelism.""" | ||
| block_M = [64, 128, 256] | ||
| block_N = [32, 64, 128] | ||
| threads = [128, 256, 512] | 
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.
| scale = (1.0 / dim)**0.5 * 1.44269504 | ||
| head_kv = heads // groups | ||
| q_shape = [batch, seq_len, heads, dim] | ||
| kv_shape = [batch, seq_len, head_kv, dim] | 
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.
| def main(batch: int = 1, | ||
| heads: int = 8, | ||
| seq_len: int = 4096, | ||
| dim: int = 128, | 
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.
| }; | ||
|  | ||
| template <class Reducer, int threads, int scale> struct AllReduce { | ||
| template <class Reducer, int threads, int scale, int thread_offset = 0> struct AllReduce { | 
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 new template parameter thread_offset is introduced but it is not used within the AllReduce struct. This is potentially dead code and makes the template signature more complex than necessary. If this parameter is intended for future use, a comment explaining its purpose would be beneficial. Otherwise, it should be removed to improve code clarity.
- Improved code readability by adjusting line breaks and indentation in the `fast_flashattn` function. - Streamlined the `main` function parameter formatting for consistency. - Removed unnecessary blank lines to enhance overall code organization.
| @@ -0,0 +1,240 @@ | |||
| # Copyright (c) Tile-AI Corporation. | |||
| # Licensed under the MIT License. | |||
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.
lisence should be removed.
No description provided.