Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
9e9b02d3
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
9e9b02d3
编写于
7月 12, 2022
作者:
Y
Yuang Liu
提交者:
GitHub
7月 12, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[operator migration] Migrate unique consecutive infer shape and yaml (#44248)
上级
60bad464
变更
6
显示空白变更内容
内联
并排
Showing
6 changed file
with
107 addition
and
50 deletion
+107
-50
paddle/fluid/operators/unique_consecutive_op.cc
paddle/fluid/operators/unique_consecutive_op.cc
+8
-48
paddle/phi/api/yaml/legacy_api.yaml
paddle/phi/api/yaml/legacy_api.yaml
+9
-0
paddle/phi/infermeta/unary.cc
paddle/phi/infermeta/unary.cc
+60
-0
paddle/phi/infermeta/unary.h
paddle/phi/infermeta/unary.h
+9
-0
python/paddle/fluid/tests/unittests/test_unique_consecutive_op.py
...addle/fluid/tests/unittests/test_unique_consecutive_op.py
+9
-1
python/paddle/tensor/manipulation.py
python/paddle/tensor/manipulation.py
+12
-1
未找到文件。
paddle/fluid/operators/unique_consecutive_op.cc
浏览文件 @
9e9b02d3
...
...
@@ -12,8 +12,11 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/unary.h"
namespace
paddle
{
namespace
operators
{
...
...
@@ -22,53 +25,6 @@ class UniqueConsecutiveOp : public framework::OperatorWithKernel {
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"X"
),
"Input"
,
"X"
,
"unique_consecutive"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"unique_consecutive"
);
auto
in_dims
=
ctx
->
GetInputDim
(
"X"
);
bool
return_inverse
=
ctx
->
Attrs
().
Get
<
bool
>
(
"return_inverse"
);
bool
return_counts
=
ctx
->
Attrs
().
Get
<
bool
>
(
"return_counts"
);
auto
axis_vec
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"axis"
);
if
(
return_inverse
)
{
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Index"
),
"Output"
,
"Index"
,
"unique_consecutive"
);
}
if
(
return_counts
)
{
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Counts"
),
"Output"
,
"Counts"
,
"unique_consecutive"
);
}
if
(
axis_vec
.
empty
())
{
ctx
->
SetOutputDim
(
"Out"
,
{
-
1
});
if
(
return_inverse
)
{
ctx
->
SetOutputDim
(
"Index"
,
{
phi
::
product
(
in_dims
)});
}
}
else
{
int
axis
=
axis_vec
[
0
];
if
(
axis
<
0
)
{
axis
+=
in_dims
.
size
();
}
PADDLE_ENFORCE_LT
(
axis
,
in_dims
.
size
(),
platform
::
errors
::
InvalidArgument
(
"The axis(%d) should be less than "
"the dimension size(%d) of x."
,
axis
,
in_dims
.
size
()));
auto
out_dims
=
in_dims
;
out_dims
[
axis
]
=
-
1
;
ctx
->
SetOutputDim
(
"Out"
,
out_dims
);
if
(
return_inverse
)
{
ctx
->
SetOutputDim
(
"Index"
,
{
in_dims
[
axis
]});
}
}
if
(
return_counts
)
{
ctx
->
SetOutputDim
(
"Counts"
,
{
-
1
});
}
}
protected:
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
...
...
@@ -114,9 +70,13 @@ class UniqueConsecutiveOpMaker : public framework::OpProtoAndCheckerMaker {
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
DECLARE_INFER_SHAPE_FUNCTOR
(
unique_consecutive
,
UniqueConsecutiveInferShapeFunctor
,
PD_INFER_META
(
phi
::
UniqueConsecutiveInferMeta
));
REGISTER_OP_WITHOUT_GRADIENT
(
unique_consecutive
,
ops
::
UniqueConsecutiveOp
,
ops
::
UniqueConsecutiveOpMaker
);
ops
::
UniqueConsecutiveOpMaker
,
UniqueConsecutiveInferShapeFunctor
);
REGISTER_OP_VERSION
(
unique_consecutive
)
.
AddCheckpoint
(
R"ROC(
...
...
paddle/phi/api/yaml/legacy_api.yaml
浏览文件 @
9e9b02d3
...
...
@@ -2205,6 +2205,15 @@
func
:
unique
data_type
:
x
-
api
:
unique_consecutive
args
:
(Tensor x, bool return_inverse, bool return_counts, int[] axis, int dtype)
output
:
Tensor(out), Tensor(index), Tensor(counts)
infer_meta
:
func
:
UniqueConsecutiveInferMeta
kernel
:
func
:
unique_consecutive
data_type
:
x
-
api
:
unsqueeze
args
:
(Tensor x, IntArray axis)
output
:
Tensor(out), Tensor(xshape)
...
...
paddle/phi/infermeta/unary.cc
浏览文件 @
9e9b02d3
...
...
@@ -2999,6 +2999,66 @@ void UnfoldInferMeta(const MetaTensor& x,
out
->
set_dims
(
phi
::
make_ddim
(
out_dims
));
}
void
UniqueConsecutiveInferMeta
(
const
MetaTensor
&
x
,
bool
return_inverse
,
bool
return_counts
,
const
std
::
vector
<
int
>&
axis
,
int
dtype
,
MetaTensor
*
out
,
MetaTensor
*
index
,
MetaTensor
*
counts
)
{
PADDLE_ENFORCE_NE
(
out
,
nullptr
,
phi
::
errors
::
InvalidArgument
(
"unique_consecutive should have output tensor out."
));
auto
in_dims
=
x
.
dims
();
if
(
return_inverse
)
{
PADDLE_ENFORCE_NE
(
index
,
nullptr
,
phi
::
errors
::
InvalidArgument
(
"Tensor index should not be null if "
"return_inverse is set to True."
));
}
if
(
return_counts
)
{
PADDLE_ENFORCE_NE
(
counts
,
nullptr
,
phi
::
errors
::
InvalidArgument
(
"Tensor counts should not be null if "
"return_counts is set to True."
));
}
if
(
axis
.
empty
())
{
out
->
set_dims
({
-
1
});
out
->
set_dtype
(
x
.
dtype
());
if
(
return_inverse
)
{
index
->
set_dims
({
phi
::
product
(
in_dims
)});
}
}
else
{
int
axis_value
=
axis
[
0
];
if
(
axis_value
<
0
)
{
axis_value
+=
in_dims
.
size
();
}
PADDLE_ENFORCE_LT
(
axis_value
,
in_dims
.
size
(),
phi
::
errors
::
InvalidArgument
(
"The axis(%d) should be less than "
"the dimension size(%d) of x."
,
axis_value
,
in_dims
.
size
()));
auto
out_dims
=
in_dims
;
out_dims
[
axis_value
]
=
-
1
;
out
->
set_dims
(
out_dims
);
out
->
set_dtype
(
x
.
dtype
());
if
(
return_inverse
)
{
index
->
set_dims
({
in_dims
[
axis_value
]});
}
}
if
(
return_counts
)
{
counts
->
set_dims
({
-
1
});
}
}
void
UniqueInferMeta
(
const
MetaTensor
&
x
,
bool
return_index
,
bool
return_inverse
,
...
...
paddle/phi/infermeta/unary.h
浏览文件 @
9e9b02d3
...
...
@@ -420,6 +420,15 @@ void UnfoldInferMeta(const MetaTensor& x,
MetaTensor
*
out
,
MetaConfig
config
=
MetaConfig
());
void
UniqueConsecutiveInferMeta
(
const
MetaTensor
&
x
,
bool
return_inverse
,
bool
return_counts
,
const
std
::
vector
<
int
>&
axis
,
int
dtype
,
MetaTensor
*
out
,
MetaTensor
*
index
,
MetaTensor
*
counts
);
void
UniqueInferMeta
(
const
MetaTensor
&
x
,
bool
return_index
,
bool
return_inverse
,
...
...
python/paddle/fluid/tests/unittests/test_unique_consecutive_op.py
浏览文件 @
9e9b02d3
...
...
@@ -72,6 +72,7 @@ class TestUniqueConsecutiveOp(OpTest):
self
.
x_range
=
20
self
.
return_inverse
=
False
self
.
return_counts
=
False
self
.
python_api
=
paddle
.
unique_consecutive
def
init_kernel_type
(
self
):
self
.
dtype
=
"float32"
if
core
.
is_compiled_with_rocm
()
else
"float64"
...
...
@@ -88,13 +89,14 @@ class TestUniqueConsecutiveOp(OpTest):
self
.
inputs
=
{
'X'
:
x
,
}
self
.
python_out_sig
=
[
"Out"
]
self
.
attrs
=
{
'dtype'
:
int
(
core
.
VarDesc
.
VarType
.
INT32
)}
self
.
outputs
=
{
'Out'
:
out
,
}
def
test_check_output
(
self
):
self
.
check_output
()
self
.
check_output
(
check_eager
=
True
)
class
TestUniqueConsecutiveOp2
(
TestUniqueConsecutiveOp
):
...
...
@@ -105,6 +107,7 @@ class TestUniqueConsecutiveOp2(TestUniqueConsecutiveOp):
self
.
x_range
=
20
self
.
return_inverse
=
True
self
.
return_counts
=
False
self
.
python_api
=
paddle
.
unique_consecutive
def
setUp
(
self
):
self
.
init_kernel_type
()
...
...
@@ -122,6 +125,7 @@ class TestUniqueConsecutiveOp2(TestUniqueConsecutiveOp):
'return_inverse'
:
self
.
return_inverse
,
'dtype'
:
int
(
core
.
VarDesc
.
VarType
.
INT32
)
}
self
.
python_out_sig
=
[
"Out"
]
self
.
outputs
=
{
'Out'
:
result
,
'Index'
:
inverse
}
...
...
@@ -133,6 +137,7 @@ class TestUniqueConsecutiveOp3(TestUniqueConsecutiveOp):
self
.
x_range
=
20
self
.
return_inverse
=
False
self
.
return_counts
=
True
self
.
python_api
=
paddle
.
unique_consecutive
def
setUp
(
self
):
self
.
init_kernel_type
()
...
...
@@ -150,6 +155,7 @@ class TestUniqueConsecutiveOp3(TestUniqueConsecutiveOp):
'return_counts'
:
self
.
return_counts
,
'dtype'
:
int
(
core
.
VarDesc
.
VarType
.
INT32
)
}
self
.
python_out_sig
=
[
"Out"
]
self
.
outputs
=
{
'Out'
:
result
,
'Counts'
:
counts
}
...
...
@@ -161,6 +167,7 @@ class TestUniqueConsecutiveOp4(TestUniqueConsecutiveOp):
self
.
x_range
=
20
self
.
return_inverse
=
True
self
.
return_counts
=
True
self
.
python_api
=
paddle
.
unique_consecutive
def
setUp
(
self
):
self
.
init_kernel_type
()
...
...
@@ -180,6 +187,7 @@ class TestUniqueConsecutiveOp4(TestUniqueConsecutiveOp):
'return_counts'
:
self
.
return_counts
,
'dtype'
:
int
(
core
.
VarDesc
.
VarType
.
INT32
)
}
self
.
python_out_sig
=
[
"Out"
]
self
.
outputs
=
{
'Out'
:
result
,
'Index'
:
inverse
,
'Counts'
:
counts
}
...
...
python/paddle/tensor/manipulation.py
浏览文件 @
9e9b02d3
...
...
@@ -2066,7 +2066,18 @@ def unique_consecutive(x,
else
:
axis
=
[
axis
]
attr_dtype
=
convert_np_dtype_to_dtype_
(
dtype
)
if
paddle
.
in_dynamic_mode
():
if
in_dygraph_mode
():
out
,
inverse
,
counts
=
_C_ops
.
final_state_unique_consecutive
(
x
,
return_inverse
,
return_counts
,
axis
,
attr_dtype
)
outs
=
[
out
]
if
return_inverse
:
outs
.
append
(
inverse
)
if
return_counts
:
outs
.
append
(
counts
)
if
len
(
outs
)
==
1
:
return
outs
[
0
]
return
tuple
(
outs
)
elif
paddle
.
in_dynamic_mode
():
out
,
inverse
,
counts
=
_C_ops
.
unique_consecutive
(
x
,
'dtype'
,
attr_dtype
,
'return_inverse'
,
return_inverse
,
'return_counts'
,
return_counts
,
'axis'
,
axis
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录