-
Notifications
You must be signed in to change notification settings - Fork 472
refactor: Improved metainfo for trtllm-gen kernels #1328
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
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 @cyx-6, 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 refactors the handling of metainfo headers for trtllm-gen
kernels within the FlashInfer library. It reverts a previous change, streamlining the JIT compilation process by moving metainfo headers to a cache directory and linking them directly during compilation, rather than relying on a runtime callback mechanism. This change aims to improve the efficiency and clarity of how kernel metadata is accessed and utilized.
Highlights
- Metainfo Loading Refactor: The previous
setup_metainfo_loader
mechanism, which used a Python callback to fetch metainfo at runtime, has been removed from both Python and C++ components. - Direct Metainfo Inclusion for JIT: Metainfo headers for
trtllm-gen
kernels (both FMHA and Fused MoE) are now explicitly fetched from a cache directory usingget_cubin
and included directly in the JIT compilation process viaextra_include_paths
. - C++ Metainfo Integration: The C++ backend (
fmhaKernels.cuh
) no longer relies on a runtimegetMetaInfo
callback. Instead, kernel metadata is passed directly to the kernel factory, suggesting it's now statically defined or linked within the C++ binary. - Header File Restructuring: The
kernelMetaInfo.h
file has been deleted, indicating a shift in how kernel metadata is structured and accessed, moving towards a more integrated approach. - Pipeline Hash Update: A
pipeline_hash
inBatchedGemmInterface.h
has been updated, likely reflecting changes in the build or generation process for batched GEMM kernels.
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 refactors how kernel metainfo is handled for trtllm-gen
kernels, moving from a dynamic callback-based loading mechanism to including header files at JIT compilation time. The changes are consistent across Python and C++ code. My review focuses on improving maintainability by replacing hardcoded strings with constants and suggesting modern C++ idioms.
flashinfer/fused_moe.py
Outdated
hash = "f5deee96023f1d74b1ff71ac69f782a96741a053" | ||
metainfo = get_cubin( | ||
f"{hash}/batched_gemm-c603ed2-3fa89e1/include/KernelMetaInfo", | ||
"d789c63aaeee1aa0a68ebf22fa693b6b82a7c2319bd933a00a10306ca08d9e0e", | ||
".h", | ||
) | ||
assert metainfo, "KernelMetaInfo.h not found" |
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 function contains several hardcoded strings for hashes and path components. To improve maintainability and readability, define these as constants at the module level and reference them here. This also helps avoid duplicating the path construction logic.
_TRTLLM_FUSED_MOE_SM100_PIPELINE_HASH = "f5deee96023f1d74b1ff71ac69f782a96741a053"
_TRTLLM_FUSED_MOE_SM100_CUBIN_PATH_COMPONENT = "batched_gemm-c603ed2-3fa89e1"
_TRTLLM_FUSED_MOE_SM100_METAINFO_SHA256 = "d789c63aaeee1aa0a68ebf22fa693b6b82a7c2319bd933a00a10306ca08d9e0e"
and then use them inside the function. I've also de-duplicated the path construction in the suggestion.
pipeline_hash = "f5deee96023f1d74b1ff71ac69f782a96741a053"
cubin_path_component = "batched_gemm-c603ed2-3fa89e1"
metainfo_sha256 = "d789c63aaeee1aa0a68ebf22fa693b6b82a7c2319bd933a00a10306ca08d9e0e"
cubin_path = f"{pipeline_hash}/{cubin_path_component}"
metainfo = get_cubin(
f"{cubin_path}/include/KernelMetaInfo",
metainfo_sha256,
".h",
)
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.
Seems like a good suggestion to me!
flashinfer/jit/attention/pytorch.py
Outdated
hash = "5f2779e6df822bc0b26940b6d3b0059c86f0a6a1" | ||
metainfo = get_cubin( | ||
f"{hash}/fmha/trtllm-gen/include/flashInferMetaInfo", | ||
"11f31dc81f996e39c3f1d85d773864c9113c5837619e21418a846befa4f8dddd", | ||
".h", | ||
) | ||
assert metainfo, "flashInferMetaInfo.h not found" |
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 function contains several hardcoded strings for hashes and path components. To improve maintainability and readability, it's better to define these as constants at the module level and reference them here. This also helps avoid duplicating the path construction logic.
For example, you could define:
_TRTLLM_FMHA_PIPELINE_HASH = "5f2779e6df822bc0b26940b6d3b0059c86f0a6a1"
_TRTLLM_FMHA_CUBIN_PATH_COMPONENT = "fmha/trtllm-gen"
_TRTLLM_FMHA_METAINFO_SHA256 = "11f31dc81f996e39c3f1d85d773864c9113c5837619e21418a846befa4f8dddd"
and then use them inside the function. I've also de-duplicated the path construction in the suggestion.
pipeline_hash = "5f2779e6df822bc0b26940b6d3b0059c86f0a6a1"
cubin_path_component = "fmha/trtllm-gen"
metainfo_sha256 = "11f31dc81f996e39c3f1d85d773864c9113c5837619e21418a846befa4f8dddd"
cubin_path = f"{pipeline_hash}/{cubin_path_component}"
metainfo = get_cubin(
f"{cubin_path}/include/flashInferMetaInfo",
metainfo_sha256,
".h",
)
sTllmGenFmhaKernelMetaInfos, | ||
sizeof(sTllmGenFmhaKernelMetaInfos) / sizeof(sTllmGenFmhaKernelMetaInfos[0]), dtypeQ, dtypeKv, | ||
dtypeOut, sm); |
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.
For better readability and to use a more modern C++ approach, consider using std::size
to get the number of elements in the array. This requires including the <iterator>
header at the top of the file.
The sizeof(array) / sizeof(array[0])
idiom is correct, but std::size
is generally preferred in C++17 and later for type safety and clarity.
#include <iterator>
return TllmFmhaKernelFactory::Get().getKernels(
sTllmGenFmhaKernelMetaInfos,
std::size(sTllmGenFmhaKernelMetaInfos), dtypeQ, dtypeKv,
dtypeOut, sm);
e0eca6a
to
d98f090
Compare
Co-authored-by: Mehdi Amini <[email protected]>
flashinfer/fused_moe/core.py
Outdated
@@ -846,6 +848,8 @@ def trtllm_gen_fused_moe_sm100_module() -> JitSpec: | |||
+ sm100a_nvcc_flags, | |||
extra_ldflags=["-lcuda"], | |||
extra_include_paths=[ | |||
# link "include" sub-directory in cache | |||
jit_env.FLASHINFER_CACHE_DIR / "cubins" / include_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.
This might be conflicting with #1462, considering using "FLASHINFER_CUBIN_DIR" instead
flashinfer/gemm.py
Outdated
/ "flashinfer/trtllm/gemm/trtllmGen_gemm_export", | ||
], | ||
# link "include" sub-directory in cache | ||
extra_include_paths=[jit_env.FLASHINFER_CACHE_DIR / "cubins" / include_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.
ditto
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.
LGTM, thanks @cyx-6 for the refactor work and @joker-eph for all the suggestions and discussions!
📌 Description
Move trtllm-gen batched-gemm and gemm metainfo headers into cache directory and link when jit.
🔍 Related Issues
🚀 Pull Request Checklist
Thank you for contributing to FlashInfer! Before we review your pull request, please make sure the following items are complete.
✅ Pre-commit Checks
pre-commit
by runningpip install pre-commit
(or used your preferred method).pre-commit install
.pre-commit run --all-files
and fixed any reported issues.🧪 Tests
unittest
, etc.).Reviewer Notes