-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add searchsorted op #35159
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
Add searchsorted op #35159
Conversation
|
Thanks for your contribution! |
sneaxiy
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.
Excellent job with tiny comments.
python/paddle/tensor/search.py
Outdated
| - ``sorted_sequence[m][n]...[l][i-1] <= values[m][n]...[l][x] < sorted_sequence[m][n]...[l][i]`` | ||
| Args: | ||
| sorted_sequence(Tensor): N-D or 1-D tensor, containing monotonically increasing sequence on the innermost dimension. The data type can be int32, int64, float32, float64. |
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.
Add indent here. Same below.
加个缩进,下同。
python/paddle/tensor/search.py
Outdated
| refer to :ref:`api_guide_Name`. | ||
| Returns: | ||
| output (Tensor):return the indices from the innermost dimension of sorted_sequence. The output tensor is the same size as values. |
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.
Add indent here.
| import paddle.nn.functional as F | ||
| import paddle.fluid as fluid | ||
| import paddle.fluid.core as core | ||
| from paddle.fluid import compiler, Program, program_guard |
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.
Not all imports are necessary. Please remove useless import expressions.
| fetch_list=out) | ||
| out_ref = np.searchsorted(self.sorted_sequence, self.values) | ||
| for r in res: | ||
| self.assertEqual(np.allclose(out_ref, r), True) |
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.
The res is a list with only one element. Please remove the for loop.
Use self.assertTrue(a) instead of self.assertEqual(a, True).
|
|
||
| paddle.disable_static(place) | ||
| SortedSequence = paddle.to_tensor(self.sorted_sequence) | ||
| Values = paddle.to_tensor(self.values) |
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.
Try to use sorted_sequence/values instead of SearchSequence/Values. The snake case naming rule is more Pythonic.
python/paddle/tensor/__init__.py
Outdated
| 'round_', | ||
| 'rsqrt', | ||
| 'rsqrt_', | ||
| 'searchsorted', |
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.
Why add this? Remove it.
| "dimensions of sorted_sequence tensor " | ||
| "and input values tensor must " | ||
| "match, but we got sorted_sequence " | ||
| "tensor ( %s ), and input value " |
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.
input value -> input values
| #include <memory> | ||
| #include <string> | ||
| #include <unordered_map> | ||
| #include <vector> |
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.
I do not think that you should include these headers. Useless below.
|
|
||
| template <typename T> | ||
| HOSTDEVICE inline int64_t BinarySearch(const T *x, int64_t num, const T &val) { | ||
| HOSTDEVICE inline int64_t BinarySearch(const T *x, size_t num, const T &val) { |
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.
No need to change. Keep int64_t.
| auto values_dims = ctx->GetInputDim("Values"); | ||
| auto out_int32 = ctx->Attrs().Get<bool>("out_int32"); | ||
|
|
||
| if (sequences_dims.size() != 1) |
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.
Add brackets after if (...). It would be more readable.
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 for codes. Reviewing docs ...
sneaxiy
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 for codes but some docs need to be updated.
python/paddle/tensor/search.py
Outdated
| Args: | ||
| sorted_sequence(Tensor): An input N-D or 1-D tensor with type int32, int64, float32, float64. The value of the tensor monotonically increases in the innermost dimension. | ||
| values(Tensor or Scalar): An input N-D tensor or a Scalar value with type int32, int64, float32, float64. |
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.
No Scalar. Paddle does not support Scalar.
python/paddle/tensor/search.py
Outdated
| sorted_sequence(Tensor): An input N-D or 1-D tensor with type int32, int64, float32, float64. The value of the tensor monotonically increases in the innermost dimension. | ||
| values(Tensor or Scalar): An input N-D tensor or a Scalar value with type int32, int64, float32, float64. | ||
| out_int32(bool, optional): Data type of the output tensor which can be int32, int64. The default value is False, and it indicates that the output data type is int64. | ||
| right(bool, optional): Find the upper and lower bounds of the sorted_sequence range in the innermost dimension based on the given `values`. If the sorted_sequence value is nan or inf, return the size of the innermost dimension |
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.
upper and lower bounds -> upper or lower bounds.
return the size of the innermost dimension -> return the size of the innermost dimension.
python/paddle/tensor/search.py
Outdated
| right(bool, optional): Find the upper and lower bounds of the sorted_sequence range in the innermost dimension based on the given `values`. If the sorted_sequence value is nan or inf, return the size of the innermost dimension | ||
| The default value is False and it shows the lower bounds. | ||
| name(str, optional): The default value is None. Normally there is no need for user to set this property. For more information, please | ||
| refer to :ref:`api_guide_Name`. |
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.
Put this line to the previous line.
sneaxiy
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 now.
qingqing01
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 for API
qingqing01
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 for API
qingqing01
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 for API.
TCChenlong
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
XiaoguangHu01
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.
LG API
jzhang533
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
* fix github name * fix CI error * fix review and CI error * fix inf,nan error and modify unittest samples * add unittest samples * add unittest samples * fix unittest error * test=document_fix * test=document_fix * modify doc and add unittest samples * fix error newline in constant * modify doc after mentor review * modify __all__ and doc * modify doc
PR types
New features
PR changes
OPs
Describe
Add paddle.searchsorted op