Skip to content

Conversation

Conless
Copy link
Contributor

@Conless Conless commented Jun 28, 2025

📌 Description

Add split_device_green_ctx_by_sm_count to create green context by a list of SM count.

🔍 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

cc @yzh119

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 @Conless, 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 significantly enhances the flashinfer library's ability to manage GPU resources by introducing a flexible mechanism for creating CUDA Green Contexts with explicit control over the number of Streaming Multiprocessors (SMs allocated to each context. This feature is crucial for advanced workload partitioning and performance optimization on NVIDIA GPUs.

Highlights

  • New Green Context Creation API: I've introduced a new public function, split_device_green_ctx_by_sm_count, which allows users to create CUDA Green Contexts by specifying a list of desired SM (Streaming Multiprocessor) counts for each partition. This provides more granular control over GPU resource allocation compared to the existing split_device_green_ctx function.
  • SM Count Alignment Logic: The new API automatically handles the complexities of SM count alignment and granularity requirements based on the specific CUDA compute capability of the device. It includes a new helper function, get_sm_count_constraint, to determine these requirements (minimum SM count and alignment factor).
  • Resource Splitting Helper: A new internal helper function, split_resource_by_sm_count, was added to manage the iterative splitting of GPU resources based on the provided list of SM counts, ensuring that the remaining resources are correctly updated for subsequent allocations.
  • Comprehensive Testing: New tests have been added to verify the correct creation of green contexts with specified SM counts, successful kernel execution on these contexts, and proper adherence to SM alignment rules for different CUDA capabilities.
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 is currently in preview and 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 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

The code changes introduce the split_device_green_ctx_by_sm_count function to create green contexts by specifying a list of SM counts. The review identifies a critical resource leak and suggests improvements to function purity and state management.

Comment on lines +85 to +95
desc = checkCudaErrors(driver.cuDevResourceGenerateDesc([remaining], 1))
green_ctx = checkCudaErrors(
driver.cuGreenCtxCreate(
desc, cu_dev, driver.CUgreenCtxCreate_flags.CU_GREEN_CTX_DEFAULT_STREAM
)
)
resource = checkCudaErrors(
driver.cuGreenCtxGetDevResource(
green_ctx, driver.CUdevResourceType.CU_DEV_RESOURCE_TYPE_SM
)
)
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The green_ctx created in the loop is not destroyed, leading to a CUDA resource leak. cuGreenCtxCreate should be paired with cuGreenCtxDestroy. Use a try...finally block to ensure destruction even if cuGreenCtxGetDevResource raises an exception.

Suggested change
desc = checkCudaErrors(driver.cuDevResourceGenerateDesc([remaining], 1))
green_ctx = checkCudaErrors(
driver.cuGreenCtxCreate(
desc, cu_dev, driver.CUgreenCtxCreate_flags.CU_GREEN_CTX_DEFAULT_STREAM
)
)
resource = checkCudaErrors(
driver.cuGreenCtxGetDevResource(
green_ctx, driver.CUdevResourceType.CU_DEV_RESOURCE_TYPE_SM
)
)
desc = checkCudaErrors(driver.cuDevResourceGenerateDesc([remaining], 1))
green_ctx = checkCudaErrors(
driver.cuGreenCtxCreate(
desc, cu_dev, driver.CUgreenCtxCreate_flags.CU_GREEN_CTX_DEFAULT_STREAM
)
)
try:
resource = checkCudaErrors(
driver.cuGreenCtxGetDevResource(
green_ctx, driver.CUdevResourceType.CU_DEV_RESOURCE_TYPE_SM
)
)
finally:
checkCudaErrors(driver.cuGreenCtxDestroy(green_ctx))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

explicit destruction may not be that important (memory overhead is 4MB per context)

@Conless Conless requested a review from yzh119 June 30, 2025 00:33
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.

Overall LGTM, left some suggestions.

@yzh119 yzh119 merged commit 2c894d2 into flashinfer-ai:main Jun 30, 2025
2 checks passed
@pachinko
Copy link

The official documentation link https://docs.flashinfer.ai/ returns a 404 error.

flashinfer.green_ctx might not be properly imported or initialized during the documentation build.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants