Skip to content

[1 / 2] register weak_ref_tensor in sgl-kernel#12999

Merged
ispobock merged 1 commit intomainfrom
register_weak_ref_tensor_in_sgl_kernel
Nov 10, 2025
Merged

[1 / 2] register weak_ref_tensor in sgl-kernel#12999
ispobock merged 1 commit intomainfrom
register_weak_ref_tensor_in_sgl_kernel

Conversation

@BBuf
Copy link
Copy Markdown
Collaborator

@BBuf BBuf commented Nov 10, 2025

Motivation

Modifications

Accuracy Tests

Benchmarking and Profiling

Checklist

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @BBuf, 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 a new mechanism for handling weak references to PyTorch tensors within the sgl-kernel library. By adding a weak_ref_tensor function that leverages a custom kernel operation, the change aims to provide more controlled memory management for tensors, potentially preventing memory leaks or improving resource utilization in specific scenarios where explicit weak referencing is beneficial.

Highlights

  • New weak_ref_tensor function: A new function, weak_ref_tensor, has been introduced in sgl_kernel/memory.py. This function acts as a wrapper, conditionally calling torch.ops.sgl_kernel.weak_ref_tensor if the input is a PyTorch tensor, otherwise returning the input directly. This suggests the integration of a custom kernel operation for handling weak references to tensors.
  • Module Export: The newly added weak_ref_tensor function has been imported and made available at the top level of the sgl_kernel package through sgl_kernel/__init__.py, allowing for easier access and usage within the library.
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 by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

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 pull request 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. 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

  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
Copy Markdown
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 introduces a weak_ref_tensor utility function, which creates a weak reference to a PyTorch tensor by wrapping a custom C++ operator. The changes include adding the Python wrapper and exporting it from the sgl_kernel package.

My review has identified a critical issue: the custom C++ operator sgl_kernel.weak_ref_tensor is called, but its registration appears to be missing from this pull request. This will cause a runtime failure. I've also included a suggestion to add type hints to the new function for consistency and better maintainability. The critical issue must be addressed for the code to be functional.


def weak_ref_tensor(tensor):
return (
torch.ops.sgl_kernel.weak_ref_tensor(tensor)
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.

critical

This line calls a custom PyTorch operator sgl_kernel.weak_ref_tensor. Based on the provided context, the C++ implementation in sgl-kernel/csrc/memory/weak_ref_tensor.cpp appears to be missing the necessary TORCH_LIBRARY macros to register this operator. Without this registration, the Python code will raise an AttributeError at runtime. The C++ operator registration needs to be included in this pull request or must already exist in the target branch for this code to work.

v_cache[loc] = v


def weak_ref_tensor(tensor):
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

For consistency with other functions in this file and for better readability, please add type hints to this function. Since it handles non-tensor types, you could import Any from typing and use tensor: Any and -> Any as annotations.

@BBuf BBuf changed the title register weak_ref_tensor in sgl-kernel [1 / 2] register weak_ref_tensor in sgl-kernel Nov 10, 2025
@ispobock ispobock merged commit 547de8c into main Nov 10, 2025
44 of 68 checks passed
@ispobock ispobock deleted the register_weak_ref_tensor_in_sgl_kernel branch November 10, 2025 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants