-
Notifications
You must be signed in to change notification settings - Fork 20
feat(op): Paged attention example using pl.function frontend syntax #139
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
Summary of ChangesHello @Crystal-wzy, 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 comprehensive example of Paged Attention, a technique used in large language models, implemented within the PyPTO framework. It demonstrates how to structure complex computational graphs into modular kernels using PyPTO's Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe 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 by creating a comment using either
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 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. 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
|
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
The pull request introduces a comprehensive example of Paged Attention using the PyPTO pl.function frontend. It effectively demonstrates the breakdown of a complex operation into four distinct kernels, showcasing the use of pl.Tensor, pl.Scalar, and various pl.op.block operations. The implementation also provides a good illustration of conditional control flow with pl.yield_() for SSA phi node semantics. The inclusion of a main block for compilation and testing adds significant value by providing a runnable demonstration of the framework's capabilities.
| # Copyright (c) PyPTO Contributors. | ||
| # Paged Attention implementation using PyPTO | ||
| # | ||
| # Reference: /data/w00949583/simpler/examples/paged_attention_sim/kernels/ |
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 reference path /data/w00949583/simpler/examples/paged_attention_sim/kernels/ appears to be an absolute path specific to a local development environment. For better portability and maintainability, consider making this a relative path within the repository or a more generic reference if it's intended for public consumption.
| TILE_M = 16 # num_heads tile size | ||
| TILE_N = 16 # block_size / head_dim_chunk tile size | ||
| SCALE = 0.0884 # 1/sqrt(head_dim) = 1/sqrt(128) ~ 0.0884 |
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 SCALE constant is derived from 1/sqrt(head_dim) = 1/sqrt(128). It would improve clarity and maintainability if head_dim (e.g., HEAD_DIM = 128) was defined as a constant, and SCALE was calculated from it. This makes the relationship explicit and easier to modify if head_dim changes.
| TILE_M = 16 # num_heads tile size | |
| TILE_N = 16 # block_size / head_dim_chunk tile size | |
| SCALE = 0.0884 # 1/sqrt(head_dim) = 1/sqrt(128) ~ 0.0884 | |
| TILE_M = 16 # num_heads tile size | |
| TILE_N = 16 # block_size / head_dim_chunk tile size | |
| HEAD_DIM = 128 | |
| SCALE = 1 / (HEAD_DIM**0.5) # 1/sqrt(head_dim) |
| li_updated = pl.op.block.add(li_scaled, lij_scaled) | ||
|
|
||
| # Update accumulated output: oi = alpha * oi + beta * oi_new | ||
| # Use row_expand_mul for broadcasting [M,1] * [M,N] |
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 # Use row_expand_mul for broadcasting [M,1] * [M,N] is slightly misleading. row_expand_mul performs element-wise multiplication where the [M,1] vector is broadcast across the columns of the [M,N] tile. Clarifying the comment to reflect this broadcasting behavior would be helpful.
| # Use row_expand_mul for broadcasting [M,1] * [M,N] | |
| # Use row_expand_mul for broadcasting [M,1] across [M,N] |
| import os | ||
| os.makedirs("/data/w00949583/pypto/build_output", exist_ok=True) | ||
| output_path = "/data/w00949583/pypto/build_output/paged_attention.mlir" |
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 output directory and filename for the generated MLIR code are hardcoded. To make this example more flexible and reusable in different environments or for different test cases, consider making these paths configurable, perhaps through command-line arguments or environment variables.
| import os | |
| os.makedirs("/data/w00949583/pypto/build_output", exist_ok=True) | |
| output_path = "/data/w00949583/pypto/build_output/paged_attention.mlir" | |
| # Save to file | |
| import os | |
| output_dir = "./build_output" | |
| os.makedirs(output_dir, exist_ok=True) | |
| output_filename = "paged_attention.mlir" | |
| output_path = os.path.join(output_dir, output_filename) | |
| with open(output_path, "w") as f: |
Add paged attention implementation using PyPTO pl.function frontend with 4 kernels: - qk_matmul (AIC): Q @ K^T matrix multiplication - softmax_prepare (AIV): scale, rowmax, exp, rowsum - pv_matmul (AIC): P @ V matrix multiplication - online_update (AIV): online softmax accumulation + fused normalization The online_update kernel demonstrates conditional control flow using Python native if/else with pl.yield_() for SSA phi node semantics. Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Add paged attention implementation using PyPTO pl.function frontend with 4 kernels:
Co-authored-by: Claude Opus 4.5 noreply@anthropic.com