Skip to content

Conversation

cyx-6
Copy link
Collaborator

@cyx-6 cyx-6 commented Jul 25, 2025

📌 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

  • I have installed pre-commit by running pip install pre-commit (or used your preferred method).
  • I have installed the hooks with pre-commit install.
  • I have run the hooks manually with pre-commit run --all-files and fixed any reported issues.

If you are unsure about how to set up pre-commit, see the pre-commit documentation.

🧪 Tests

  • Tests have been added or updated as needed.
  • All tests are passing (unittest, etc.).

Reviewer Notes

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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 using get_cubin and included directly in the JIT compilation process via extra_include_paths.
  • C++ Metainfo Integration: The C++ backend (fmhaKernels.cuh) no longer relies on a runtime getMetaInfo 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 in BatchedGemmInterface.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

  1. 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.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

Comment on lines 760 to 788
hash = "f5deee96023f1d74b1ff71ac69f782a96741a053"
metainfo = get_cubin(
f"{hash}/batched_gemm-c603ed2-3fa89e1/include/KernelMetaInfo",
"d789c63aaeee1aa0a68ebf22fa693b6b82a7c2319bd933a00a10306ca08d9e0e",
".h",
)
assert metainfo, "KernelMetaInfo.h not found"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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",
)

Copy link
Collaborator

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!

Comment on lines 1486 to 1498
hash = "5f2779e6df822bc0b26940b6d3b0059c86f0a6a1"
metainfo = get_cubin(
f"{hash}/fmha/trtllm-gen/include/flashInferMetaInfo",
"11f31dc81f996e39c3f1d85d773864c9113c5837619e21418a846befa4f8dddd",
".h",
)
assert metainfo, "flashInferMetaInfo.h not found"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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",
)

Comment on lines 634 to 633
sTllmGenFmhaKernelMetaInfos,
sizeof(sTllmGenFmhaKernelMetaInfos) / sizeof(sTllmGenFmhaKernelMetaInfos[0]), dtypeQ, dtypeKv,
dtypeOut, sm);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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);

@@ -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,
Copy link
Collaborator

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/trtllm/gemm/trtllmGen_gemm_export",
],
# link "include" sub-directory in cache
extra_include_paths=[jit_env.FLASHINFER_CACHE_DIR / "cubins" / include_path],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Collaborator

@yzh119 yzh119 left a 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!

@yzh119 yzh119 merged commit e0aa4e7 into flashinfer-ai:main Aug 13, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants