- 
                Notifications
    You must be signed in to change notification settings 
- Fork 292
[Enhancement] Refactor buffer index handling for improved precision a… #671
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.
| 👋 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 adds a new example for FlashAttention-2 forward pass, specifically tailored for AMD platforms using tilelang. The implementation focuses on refining buffer index handling and overlap detection for greater precision and clarity, while also setting up robust performance tuning and correctness verification.
Highlights
- New FlashAttention-2 Example: Introduced a new example (example_amd_flash_attn_fwd.py) demonstrating a FlashAttention-2 forward pass implementation usingtilelangfor AMD platforms. This example incorporates enhanced buffer index handling and streamlined logic for determining buffer overlaps, aligning with the PR's goal of improved precision and clarity.
- Macro Refactoring for Clarity: Within the new FlashAttention-2 implementation, internal tilelangmacros (compute_blockandscale_and_write_back) were refactored and moved outside the main kernel definition. This change aims to improve code structure, clarity, and compiler understanding of the macro calls.
- Performance and Correctness Verification: The new FlashAttention-2 example includes a PyTorch reference implementation for correctness verification and utilizes tilelang.autotuneto find optimal configurations, providing benchmarking capabilities for performance comparison.
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. ↩ 
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 adds a new example implementation of FlashAttention-V2 for AMD GPUs using tilelang, along with a shell script for running it. The new Python example is well-structured and demonstrates the capabilities of tilelang for complex kernel implementation.
My review focuses on improving code clarity, maintainability, and adherence to common best practices. I've pointed out a few areas for improvement:
- Removing dead/unreachable code.
- Improving readability by replacing a magic number with a named constant.
- Ensuring consistency in comment language (English).
- Making the test script more portable and compliant with POSIX standards.
| from functools import partial | ||
|  | ||
|  | ||
| # PyTorch 参考实现保持不变 | 
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 comments in this file are in Chinese, which is inconsistent with the English used in the rest of the project. For better maintainability and accessibility for all contributors, please translate the comments to English.
| # PyTorch 参考实现保持不变 | |
| # PyTorch reference implementation remains unchanged | 
| if not valid_configs: | ||
| valid_configs.append({ | ||
| 'block_M': 64, | ||
| 'block_N': 64, | ||
| 'num_split_q': 64, | ||
| 'threads': 256, | ||
| 'num_stages': 1, | ||
| 'enable_rasterization': True, | ||
| 'k_pack': 2 | ||
| }) | 
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.
This if not valid_configs: block appears to be unreachable. The itertools.product call on line 49 will only produce an empty iterator if one of the input iterables is empty. In this function, all lists used in the product are non-empty, so valid_configs will always contain at least one configuration. This block is dead code and should be removed.
| enable_rasterization: bool, | ||
| k_pack: int, | ||
| ): | ||
| scale = (1.0 / dim)**0.5 * 1.44269504 | 
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.
| dtype = "float16" | ||
| accum_dtype = "float" | ||
|  | ||
| dtype_size = 2 | 
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.
        
          
                examples/amd/test.sh
              
                Outdated
          
        
      | @@ -0,0 +1,16 @@ | |||
| /bin/python /workspace/tilelang/examples/amd/example_amd_flash_attn_fwd.py --batch 2 --seq_len 4096 --dim 128 --heads 16 --groups 8 --is_causal | |||
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.
This script is missing a shebang (e.g., #!/bin/bash or #!/usr/bin/env bash) which prevents it from being directly executable. Additionally, it uses a hardcoded, non-standard path for the python interpreter (/bin/python), which harms portability.
Please add a shebang and use a more standard way to invoke python, like python3.
| /bin/python /workspace/tilelang/examples/amd/example_amd_flash_attn_fwd.py --batch 2 --seq_len 4096 --dim 128 --heads 16 --groups 8 --is_causal | |
| #!/usr/bin/env bash | |
| python3 /workspace/tilelang/examples/amd/example_amd_flash_attn_fwd.py --batch 2 --seq_len 4096 --dim 128 --heads 16 --groups 8 --is_causal | 
        
          
                examples/amd/test.sh
              
                Outdated
          
        
      | # ~/.tilelang/cache/0dad5010ab3d8e01b2a86e56208af334ad62865b92cb03038f9df84fda8d8a99/kernel.cu \ | ||
| # -o ./kernel.s \ | ||
| # -I/workspace/tilelang/3rdparty/composable_kernel/include \ | ||
| # -I/workspace/tilelang/src No newline at end of file | 
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.
This reverts commit e8cc372.
…nd clarity (#668)