-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Relax][Op][PyTorch] Supported Median operator #18626
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
- Add relax.median - Apply median op into exported_program_translator
Summary of ChangesHello @locnd182644, 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 TVM Relax framework by adding support for the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe 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
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 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
|
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 adds support for the relax.median operator, including its frontend integration for PyTorch, operator definition, legalization to TE, and associated tests. The implementation is comprehensive, but I've identified a critical correctness issue in the struct info inference logic for dynamic shapes, which could lead to incorrect program compilation. I've also found some issues in the tests that validate this buggy behavior, along with a couple of minor opportunities for code simplification and type hint improvements. Addressing these points will ensure the new operator is robust and maintainable.
|
/gemini review |
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 adds support for the median operator in Relax, including frontend converters for PyTorch, the operator definition, struct info inference, and legalization to TE. The changes are well-structured and follow existing patterns in the codebase. I've found one correctness issue in the legalization logic for median when axis is None and keepdims is True, for which I've provided a suggestion. The rest of the implementation and the new tests look solid.
| shape_prod = _compute_shape_prod(x, axis) | ||
| mid_index = (shape_prod - 1) // 2 | ||
|
|
||
| if axis is None or len(axis) == 0: | ||
| x = topi.reshape(x, [shape_prod.value]) | ||
| ax = -1 | ||
| else: | ||
| ax = axis[0].value | ||
| index_sorted = topi.argsort(x, axis=ax, is_ascend=True, dtype="int64") | ||
| x_sorted = topi.gather(x, axis=ax, indices=index_sorted) | ||
|
|
||
| new_shape = list(x.shape) | ||
| new_shape[ax] = 1 | ||
| indices = topi.full(new_shape, fill_value=mid_index, dtype="int64") | ||
|
|
||
| median_val = topi.gather(x_sorted, axis=ax, indices=indices) | ||
| median_idx = topi.gather(index_sorted, axis=ax, indices=indices) | ||
|
|
||
| if axis is None or len(axis) == 0: | ||
| return median_val if keepdims else topi.squeeze(median_val, axis=axis) |
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.
When axis is None and keepdims is True, the output tensor should have the same rank as the input, with all dimensions of size 1. The current implementation returns a tensor of shape (1,) because the original rank of x is lost after it's reshaped.
To fix this, we should store the original rank of x before any modifications and use it to reshape median_val when keepdims is true and axis is None.
orig_ndim = len(x.shape)
shape_prod = _compute_shape_prod(x, axis)
mid_index = (shape_prod - 1) // 2
if axis is None or len(axis) == 0:
x = topi.reshape(x, [shape_prod.value])
ax = -1
else:
ax = axis[0].value
index_sorted = topi.argsort(x, axis=ax, is_ascend=True, dtype="int64")
x_sorted = topi.gather(x, axis=ax, indices=index_sorted)
new_shape = list(x.shape)
new_shape[ax] = 1
indices = topi.full(new_shape, fill_value=mid_index, dtype="int64")
median_val = topi.gather(x_sorted, axis=ax, indices=indices)
median_idx = topi.gather(index_sorted, axis=ax, indices=indices)
if axis is None or len(axis) == 0:
if keepdims:
return topi.reshape(median_val, [1] * orig_ndim)
return topi.squeeze(median_val, axis=axis)
tlopex
left a comment
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!
Summary:
Expected:
1. Axis = None, KeepDim = False
2. Axis = 0, KeepDim = False
3. Axis = -1, KeepDim = True