-
Notifications
You must be signed in to change notification settings - Fork 10
[Feat] Support real input #55
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
base: refactor
Are you sure you want to change the base?
Conversation
RMLYC
commented
Nov 21, 2025
- support real input in test_mha.py
Summary of ChangesHello @RMLYC, 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 the capability to use real input data for testing and benchmarking Multi-Head Attention (MHA) operations. Previously, tests relied solely on randomly generated inputs. By adding a utility function to load tensors from files and integrating this into the MHA benchmark and test scripts, users can now specify actual data paths, enabling more realistic and specific performance evaluations. Highlights
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
This pull request adds support for loading real input data for MHA benchmarks, which is a valuable enhancement. However, there are a few issues that need to be addressed. Most critically, the changes will cause the backward pass test to fail with a TypeError because the corresponding benchmark function was not updated. There is also a significant security concern in the new data loading utility due to the use of torch.load. Additionally, there are minor improvements that can be made regarding code consistency and logging practices.
| benchmark = mha_bwd_benchmark(B, H, S, D, causal, dtype) | ||
|
|
||
| inputs = benchmark.gen_inputs() | ||
| inputs = benchmark.gen_inputs(input_path) |
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 gen_inputs method of mha_bwd_benchmark (defined in benchmarks/flash_attn/mha.py) does not accept an input_path argument. This call will raise a TypeError. You need to update mha_bwd_benchmark.gen_inputs to accept input_path and load data from it, similar to mha_fwd_benchmark.gen_inputs. Also, remember that for the backward pass, the loaded tensors for Q, K, and V will need to have requires_grad set to True.
| if not os.path.exists(path): | ||
| raise FileNotFoundError(f"Input file not found: {path}") | ||
|
|
||
| tensor = torch.load(path) |
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.
Using torch.load is a security risk because it uses pickle internally, which can lead to arbitrary code execution if a malicious file is loaded. Since the file path is provided via a command-line argument, it's possible for a user to provide a malicious file. Consider using a safer format for saving and loading tensors, such as safetensors, especially if the input files could come from an untrusted source.
| if input_path is None: | ||
| # gen random inputs | ||
| print("Gen random inputs!") | ||
| Q = torch.randn( | ||
| self.batch, self.seq_len, self.heads, self.dim, device='cuda', dtype=self.dtype) | ||
| K = torch.randn( | ||
| self.batch, self.seq_len, self.heads, self.dim, device='cuda', dtype=self.dtype) | ||
| V = torch.randn( | ||
| self.batch, self.seq_len, self.heads, self.dim, device='cuda', dtype=self.dtype) | ||
| else: | ||
| # Load input data from file paths | ||
| print("Gen inputs from 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.
| """ | ||
| 从文件路径加载输入数据的公共函数 | ||
| Args: | ||
| path: 文件路径 | ||
| expected_shape: 期望的张量形状 | ||
| dtype: 数据类型 | ||
| device: 设备类型 | ||
| Returns: | ||
| 加载的张量 | ||
| """ |
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 docstring is written in Chinese, which is inconsistent with the rest of the codebase being in English. To maintain consistency and improve readability for all contributors, please translate it to English.
| """ | |
| 从文件路径加载输入数据的公共函数 | |
| Args: | |
| path: 文件路径 | |
| expected_shape: 期望的张量形状 | |
| dtype: 数据类型 | |
| device: 设备类型 | |
| Returns: | |
| 加载的张量 | |
| """ | |
| """ | |
| Loads input data from a file path. | |
| Args: | |
| path: The file path. | |
| expected_shape: The expected shape of the tensor. | |
| dtype: The data type. | |
| device: The device. | |
| Returns: | |
| The loaded tensor. | |
| """ |