Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
TonyTonyFun
Paddle
提交
12917c8c
P
Paddle
项目概览
TonyTonyFun
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
12917c8c
编写于
8月 24, 2022
作者:
W
WangZhen
提交者:
GitHub
8月 24, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[OpAttr]Adapt tensor minlength for bincount (#45342)
* Adapt minlength attr for bincount
上级
8da6b72b
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
93 addition
and
17 deletion
+93
-17
paddle/fluid/operators/bincount_op.cc
paddle/fluid/operators/bincount_op.cc
+2
-1
paddle/phi/infermeta/binary.cc
paddle/phi/infermeta/binary.cc
+1
-8
paddle/phi/infermeta/binary.h
paddle/phi/infermeta/binary.h
+1
-1
paddle/phi/kernels/bincount_kernel.h
paddle/phi/kernels/bincount_kernel.h
+2
-1
paddle/phi/kernels/cpu/bincount_kernel.cc
paddle/phi/kernels/cpu/bincount_kernel.cc
+11
-3
paddle/phi/kernels/gpu/bincount_kernel.cu
paddle/phi/kernels/gpu/bincount_kernel.cu
+12
-3
python/paddle/fluid/tests/unittests/test_bincount_op.py
python/paddle/fluid/tests/unittests/test_bincount_op.py
+64
-0
未找到文件。
paddle/fluid/operators/bincount_op.cc
浏览文件 @
12917c8c
...
...
@@ -50,7 +50,8 @@ class BincountOpMaker : public framework::OpProtoAndCheckerMaker {
AddOutput
(
"Out"
,
"(Tensor) The output tensor of Bincount op,"
);
AddAttr
<
int
>
(
"minlength"
,
"(int) The minimal numbers of bins"
)
.
SetDefault
(
0
)
.
EqualGreaterThan
(
0
);
.
EqualGreaterThan
(
0
)
.
SupportTensor
();
AddComment
(
R"DOC(
Bincount Operator.
Computes frequency of each value in the input tensor.
...
...
paddle/phi/infermeta/binary.cc
浏览文件 @
12917c8c
...
...
@@ -210,17 +210,10 @@ void BCELossInferMeta(const MetaTensor& input,
void
BincountInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
weights
,
int
minlength
,
const
Scalar
&
minlength
,
MetaTensor
*
out
)
{
auto
input_dim
=
x
.
dims
();
PADDLE_ENFORCE_GE
(
minlength
,
0
,
phi
::
errors
::
InvalidArgument
(
"The minlength should be greater than or equal to 0."
"But received minlength is %d"
,
minlength
));
PADDLE_ENFORCE_EQ
(
input_dim
.
size
(),
1
,
...
...
paddle/phi/infermeta/binary.h
浏览文件 @
12917c8c
...
...
@@ -57,7 +57,7 @@ void BCELossInferMeta(const MetaTensor& input,
void
BincountInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
weights
,
int
minlength
,
const
Scalar
&
minlength
,
MetaTensor
*
out
);
void
BmmInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
y
,
MetaTensor
*
out
);
...
...
paddle/phi/kernels/bincount_kernel.h
浏览文件 @
12917c8c
...
...
@@ -14,6 +14,7 @@
#pragma once
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/dense_tensor.h"
namespace
phi
{
...
...
@@ -22,7 +23,7 @@ template <typename T, typename Context>
void
BincountKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
const
paddle
::
optional
<
DenseTensor
>&
weights
,
int
minlength
,
const
Scalar
&
minlength
,
DenseTensor
*
out
);
}
// namespace phi
paddle/phi/kernels/cpu/bincount_kernel.cc
浏览文件 @
12917c8c
...
...
@@ -86,12 +86,20 @@ template <typename T, typename Context>
void
BincountKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
const
paddle
::
optional
<
DenseTensor
>&
weights
,
int
minlength
,
const
Scalar
&
minlength
,
DenseTensor
*
out
)
{
int
int_minlength
=
minlength
.
to
<
int
>
();
PADDLE_ENFORCE_GE
(
int_minlength
,
0
,
phi
::
errors
::
InvalidArgument
(
"The minlength should be greater than or equal to 0."
"But received minlength is %d"
,
int_minlength
));
if
(
x
.
dtype
()
==
DataType
::
INT32
)
{
BincountInner
<
Context
,
T
,
int
>
(
dev_ctx
,
x
,
weights
,
minlength
,
out
);
BincountInner
<
Context
,
T
,
int
>
(
dev_ctx
,
x
,
weights
,
int_
minlength
,
out
);
}
else
if
(
x
.
dtype
()
==
DataType
::
INT64
)
{
BincountInner
<
Context
,
T
,
int64_t
>
(
dev_ctx
,
x
,
weights
,
minlength
,
out
);
BincountInner
<
Context
,
T
,
int64_t
>
(
dev_ctx
,
x
,
weights
,
int_
minlength
,
out
);
}
}
}
// namespace phi
...
...
paddle/phi/kernels/gpu/bincount_kernel.cu
浏览文件 @
12917c8c
...
...
@@ -138,12 +138,21 @@ template <typename T, typename Context>
void
BincountKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
const
paddle
::
optional
<
DenseTensor
>&
weights
,
int
minlength
,
const
Scalar
&
minlength
,
DenseTensor
*
out
)
{
int
int_minlength
=
minlength
.
to
<
int
>
();
PADDLE_ENFORCE_GE
(
int_minlength
,
0
,
phi
::
errors
::
InvalidArgument
(
"The minlength should be greater than or equal to 0."
"But received minlength is %d"
,
int_minlength
));
if
(
x
.
dtype
()
==
DataType
::
INT32
)
{
BincountCUDAInner
<
Context
,
T
,
int
>
(
dev_ctx
,
x
,
weights
,
minlength
,
out
);
BincountCUDAInner
<
Context
,
T
,
int
>
(
dev_ctx
,
x
,
weights
,
int_
minlength
,
out
);
}
else
if
(
x
.
dtype
()
==
DataType
::
INT64
)
{
BincountCUDAInner
<
Context
,
T
,
int64_t
>
(
dev_ctx
,
x
,
weights
,
minlength
,
out
);
BincountCUDAInner
<
Context
,
T
,
int64_t
>
(
dev_ctx
,
x
,
weights
,
int_minlength
,
out
);
}
}
}
// namespace phi
...
...
python/paddle/fluid/tests/unittests/test_bincount_op.py
浏览文件 @
12917c8c
...
...
@@ -14,13 +14,16 @@
from
__future__
import
print_function
import
os
import
unittest
import
tempfile
import
numpy
as
np
import
paddle
import
paddle.fluid
as
fluid
import
paddle.fluid.core
as
core
from
paddle.fluid
import
Program
,
program_guard
from
op_test
import
OpTest
import
paddle.inference
as
paddle_infer
paddle
.
enable_static
()
...
...
@@ -206,5 +209,66 @@ class TestCase5(TestBincountOp):
self
.
Out
=
np
.
bincount
(
self
.
np_input
,
minlength
=
self
.
minlength
)
class
TestTensorMinlength
(
unittest
.
TestCase
):
def
setUp
(
self
):
paddle
.
disable_static
()
paddle
.
seed
(
2022
)
self
.
temp_dir
=
tempfile
.
TemporaryDirectory
()
self
.
save_path
=
os
.
path
.
join
(
self
.
temp_dir
.
name
,
'tensor_minlength_bincount'
)
self
.
place
=
paddle
.
CUDAPlace
(
0
)
if
paddle
.
is_compiled_with_cuda
()
else
paddle
.
CPUPlace
()
def
test_dygraph
(
self
):
paddle
.
disable_static
()
x
=
np
.
random
.
randint
(
0
,
10
,
[
20
])
minlength
=
2
np_out
=
np
.
bincount
(
x
,
minlength
=
minlength
)
pd_out
=
paddle
.
bincount
(
paddle
.
to_tensor
(
x
),
minlength
=
paddle
.
to_tensor
([
2
],
dtype
=
'int32'
))
np
.
testing
.
assert_allclose
(
np_out
,
pd_out
.
numpy
())
def
test_static_and_infer
(
self
):
paddle
.
enable_static
()
np_x
=
np
.
random
.
randn
(
100
).
astype
(
'float32'
)
main_prog
=
paddle
.
static
.
Program
()
starup_prog
=
paddle
.
static
.
Program
()
with
paddle
.
static
.
program_guard
(
main_prog
,
starup_prog
):
# run static
x
=
paddle
.
static
.
data
(
shape
=
np_x
.
shape
,
name
=
'x'
,
dtype
=
np_x
.
dtype
)
linear
=
paddle
.
nn
.
Linear
(
np_x
.
shape
[
0
],
np_x
.
shape
[
0
])
linear_out
=
linear
(
x
)
relu_out
=
paddle
.
nn
.
functional
.
relu
(
linear_out
)
minlength
=
paddle
.
full
([
1
],
3
,
dtype
=
'int32'
)
out
=
paddle
.
bincount
(
paddle
.
cast
(
relu_out
,
'int32'
),
minlength
=
minlength
)
exe
=
paddle
.
static
.
Executor
(
self
.
place
)
exe
.
run
(
starup_prog
)
static_out
=
exe
.
run
(
feed
=
{
'x'
:
np_x
},
fetch_list
=
[
out
])
# run infer
paddle
.
static
.
save_inference_model
(
self
.
save_path
,
[
x
],
[
out
],
exe
)
config
=
paddle_infer
.
Config
(
self
.
save_path
+
'.pdmodel'
,
self
.
save_path
+
'.pdiparams'
)
if
paddle
.
is_compiled_with_cuda
():
config
.
enable_use_gpu
(
100
,
0
)
else
:
config
.
disable_gpu
()
predictor
=
paddle_infer
.
create_predictor
(
config
)
input_names
=
predictor
.
get_input_names
()
input_handle
=
predictor
.
get_input_handle
(
input_names
[
0
])
fake_input
=
np_x
input_handle
.
reshape
(
np_x
.
shape
)
input_handle
.
copy_from_cpu
(
fake_input
)
predictor
.
run
()
output_names
=
predictor
.
get_output_names
()
output_handle
=
predictor
.
get_output_handle
(
output_names
[
0
])
infer_out
=
output_handle
.
copy_to_cpu
()
np
.
testing
.
assert_allclose
(
static_out
[
0
],
infer_out
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录