-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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 bincount op #36317
Add bincount op #36317
Conversation
Thanks for your contribution! |
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 op_function_generator.cc
python/paddle/tensor/linalg.py
Outdated
|
||
Args: | ||
x (Tensor): A Tensor with non-negative integer. Should be 1-D tensor. | ||
weights (Tensor, optional): Weight for each value in the input tensor. Should have the same shape as input. |
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.
Default is None.
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.
Done
python/paddle/tensor/linalg.py
Outdated
Args: | ||
x (Tensor): A Tensor with non-negative integer. Should be 1-D tensor. | ||
weights (Tensor, optional): Weight for each value in the input tensor. Should have the same shape as input. | ||
minlength (int): Minimum number of bins. Should be non-negative integer. |
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.
int -> int, optional
Default is 0.
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.
Done
static_cast<double>(0)); | ||
for (int64_t i = 0; i < input_numel; i++) { | ||
output_data[input_data[i]] += static_cast<double>(weights_data[i]); | ||
} |
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.
这个float和double的分支代码是否可以使用T合并,也和int类型的Weights更适配
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.
这边是为了保证与竞品一致。weights可以是任意数据类型,只有当weights float32时,output的类型才是float32。其他三种情况下output都是float64。
|
||
KernelBincount<T, InputT, double><<<GET_BLOCKS(input_numel), | ||
PADDLE_CUDA_NUM_THREADS, 0, stream>>>( | ||
input_data, input_numel, has_weights, weights_data, output_data); |
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.
同CPU,float和double的分支代码是否可以使用T合并
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.
同上
|
||
Args: | ||
x (Tensor): A Tensor with non-negative integer. Should be 1-D tensor. | ||
weights (Tensor, optional): Weight for each value in the input tensor. Should have the same shape as input. Default is None. |
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.
weight的dtype是否也要说明
@@ -44,6 +44,7 @@ | |||
from .linalg import cholesky # noqa: F401 | |||
from .linalg import bmm # noqa: F401 | |||
from .linalg import histogram # noqa: F401 | |||
from .linalg import bincount # noqa: F401 |
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.
we shall also add bincount in tensor_method_func list below to get paddle.Tensor.bincount
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.
Done, thanks!
PADDLE_ENFORCE_GE( | ||
input_min, static_cast<InputT>(0), | ||
platform::errors::InvalidArgument( | ||
"The elements in input tensor must be non-negative ints")); |
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 check of PADDLE_ENFORCE* should in InferShape rather than in Compute, so we can detect illegal input earlier
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.
Done, thanks!
PADDLE_ENFORCE_EQ(input_type_match, true, | ||
platform::errors::InvalidArgument( | ||
"Input(X) holds the wrong type, it holds %s, but " | ||
"desires to be %s or %s", | ||
paddle::framework::DataTypeToString(input_type), | ||
paddle::framework::DataTypeToString( | ||
framework::proto::VarType::INT32), | ||
paddle::framework::DataTypeToString( | ||
framework::proto::VarType::INT64))); |
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 check of PADDLE_ENFORCE* should in InferShape rather than in Compute, so we can detect illegal input earlier
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.
Done, thanks!
PADDLE_ENFORCE_EQ( | ||
platform::is_gpu_place(context.GetPlace()), true, | ||
platform::errors::InvalidArgument("It must use CUDAPlace.")); |
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 check of PADDLE_ENFORCE* should in InferShape rather than in Compute, so we can detect illegal input earlier. but in this case, because register BincountCUDAKernel in cuda, no need this check.
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.
Done, thanks!
PADDLE_ENFORCE_GE( | ||
*std::min_element(input_data, input_data + input_numel), | ||
static_cast<InputT>(0), | ||
platform::errors::InvalidArgument( | ||
"The elements in input tensor must be non-negative ints")); |
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 check of PADDLE_ENFORCE* should in InferShape rather than in Compute, so we can detect illegal input earlier
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.
Done, thanks!
paddle/fluid/operators/bincount_op.h
Outdated
PADDLE_ENFORCE_EQ(input_type_match, true, | ||
platform::errors::InvalidArgument( | ||
"Input(X) holds the wrong type, it holds %s, but " | ||
"desires to be %s or %s", | ||
paddle::framework::DataTypeToString(input_type), | ||
paddle::framework::DataTypeToString( | ||
framework::proto::VarType::INT32), | ||
paddle::framework::DataTypeToString( | ||
framework::proto::VarType::INT64))); |
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 check of PADDLE_ENFORCE* should in InferShape rather than in Compute, so we can detect illegal input earlier
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.
Done, thanks!
python/paddle/tensor/linalg.py
Outdated
if paddle.max(x) < 0: | ||
raise ValueError("Elements in Input(x) should all be non-negative") |
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.
x (Tensor): A Tensor with non-negative integer. Should be 1-D tensor.
shall we must check:
- paddle.min(x) >= 0:
- x.ndim == 1;
- x.numel() != 0
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
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
Args: | ||
x (Tensor): A Tensor with non-negative integer. Should be 1-D tensor. | ||
weights (Tensor, optional): Weight for each value in the input tensor. Should have the same shape as input. Default is None. | ||
minlength (int, optional): Minimum number of bins. Should be non-negative integer. Default is 0. |
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.
少了name参数
647aa0d
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
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 op_function_generator.cc
* Add bincount op * upload cpu version * fix unitest * fix unittest * fix unittest * fix en doc * add more test * fix en doc * add more test case * fix test * fix input vailidation * fix input check * fix unittest * fix test * fix en doc cherry-pick
PR types
New features
PR changes
OPs
Describe
Add bincount op
中文文档pr:PaddlePaddle/docs#3959
英文文档: