Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions sgl-kernel/python/sgl_kernel/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ def _to_tensor_scalar_tuple(x):
return (None, x)


_IS_HOPPER_ARCH = None
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think it would be better to cache it after this line

from sgl_kernel.utils import get_cuda_stream, is_hopper_arch

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

But implementing in the function also has its advantages, except for element wise ops, everything else can be used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah maybe keep it here because other functions may call it in the future?



def is_hopper_arch() -> bool:
# Hopper arch's compute capability == 9.0
device = torch.cuda.current_device()
major, minor = torch.cuda.get_device_capability(device)
return major == 9
global _IS_HOPPER_ARCH
if _IS_HOPPER_ARCH is None:
# Hopper arch's compute capability == 9.0
device = torch.cuda.current_device()
major, _ = torch.cuda.get_device_capability(device)
_IS_HOPPER_ARCH = major == 9
return _IS_HOPPER_ARCH
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The current implementation for caching is_hopper_arch using a global variable is functional. However, Python's functools.lru_cache decorator offers a more idiomatic and often cleaner way to achieve the same result with less boilerplate.

Using @functools.lru_cache(maxsize=1) (or maxsize=None if Python 3.9+ and you prefer cache) would:

  • Eliminate the need for the explicit _IS_HOPPER_ARCH global variable and the manual check-and-set logic.
  • Handle thread-safety aspects of cache access automatically (Python's lru_cache is thread-safe).
  • Make the intent of caching immediately clear through the decorator.

To implement this, you would need to add import functools at the top of the file.

What are your thoughts on refactoring to use lru_cache for this?

@functools.lru_cache(maxsize=1)
def is_hopper_arch() -> bool:
    # Hopper arch's compute capability == 9.0
    device = torch.cuda.current_device()
    major, _ = torch.cuda.get_device_capability(device)
    return major == 9