Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
109b5aed
P
Paddle
项目概览
PaddlePaddle
/
Paddle
12 个月 前同步成功
通知
2292
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
109b5aed
编写于
5月 22, 2019
作者:
M
mozga-intel
提交者:
tensor-tang
5月 23, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[NGraph] Enable reshape operator test=develop (#17512)
上级
9bb6a421
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
182 addition
and
6 deletion
+182
-6
paddle/fluid/operators/ngraph/ngraph_bridge.cc
paddle/fluid/operators/ngraph/ngraph_bridge.cc
+31
-0
paddle/fluid/operators/ngraph/ngraph_bridge.h
paddle/fluid/operators/ngraph/ngraph_bridge.h
+2
-0
paddle/fluid/operators/ngraph/ngraph_engine.cc
paddle/fluid/operators/ngraph/ngraph_engine.cc
+2
-3
paddle/fluid/operators/ngraph/ops/reshape_op.h
paddle/fluid/operators/ngraph/ops/reshape_op.h
+107
-0
paddle/fluid/platform/ngraph_helper.h
paddle/fluid/platform/ngraph_helper.h
+17
-3
python/paddle/fluid/tests/unittests/ngraph/test_reshape_ngraph_op.py
...le/fluid/tests/unittests/ngraph/test_reshape_ngraph_op.py
+23
-0
未找到文件。
paddle/fluid/operators/ngraph/ngraph_bridge.cc
浏览文件 @
109b5aed
...
...
@@ -15,6 +15,7 @@ limitations under the License. */
#include <algorithm>
#include <functional>
#include <memory>
#include <unordered_set>
#include <vector>
#include "ngraph/ngraph.hpp"
...
...
@@ -24,6 +25,8 @@ limitations under the License. */
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/ngraph_helper.h"
constexpr
int64_t
kNoPadding
=
-
1
;
namespace
paddle
{
namespace
operators
{
...
...
@@ -31,6 +34,34 @@ bool NgraphBridge::isRegister(const std::string& str) {
return
ops
::
NgraphSingleton
::
Lookup
(
str
);
}
bool
NgraphBridge
::
isSupported
(
const
std
::
unique_ptr
<
framework
::
OperatorBase
>&
op
)
{
static
std
::
unordered_set
<
std
::
string
>
skip_op_list
{
"reshape"
,
"reshape2"
,
"lookup_table"
};
bool
result
=
true
;
auto
&
op_type
=
op
->
Type
();
auto
op_attrs
=
paddle
::
framework
::
AttrReader
(
op
->
Attrs
());
if
(
!
isRegister
(
op_type
))
{
if
(
skip_op_list
.
count
(
op_type
))
{
if
(
op_type
==
"lookup_table"
)
{
if
(
op_attrs
.
Get
<
bool
>
(
"is_sparse"
)
||
(
op_attrs
.
Get
<
int64_t
>
(
"padding_idx"
)
!=
kNoPadding
))
{
result
=
false
;
}
}
else
if
((
op_type
==
"reshape"
)
||
(
op_type
==
"reshape2"
))
{
if
(
op
->
Input
(
"Shape"
)
!=
paddle
::
framework
::
kEmptyVarName
)
{
result
=
false
;
}
}
else
{
result
=
false
;
}
}
}
else
{
result
=
false
;
}
return
result
;
}
void
NgraphBridge
::
BuildNgNode
(
const
std
::
shared_ptr
<
framework
::
OperatorBase
>&
op
)
{
auto
&
op_type
=
op
->
Type
();
...
...
paddle/fluid/operators/ngraph/ngraph_bridge.h
浏览文件 @
109b5aed
...
...
@@ -39,6 +39,8 @@ class NgraphBridge {
static
bool
isRegister
(
const
std
::
string
&
str
);
static
bool
isSupported
(
const
std
::
unique_ptr
<
framework
::
OperatorBase
>&
op
);
private:
std
::
shared_ptr
<
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
ngraph
::
Node
>>>
...
...
paddle/fluid/operators/ngraph/ngraph_engine.cc
浏览文件 @
109b5aed
...
...
@@ -134,12 +134,11 @@ static std::vector<std::vector<int>> NgraphOpIntervals(
int
pivot
=
left
;
while
(
pivot
<
right
)
{
auto
op_type
=
ops
->
at
(
pivot
)
->
Type
();
if
(
NgraphBridge
::
isRegister
(
op_type
))
{
if
(
!
NgraphBridge
::
isSupported
(
ops
->
at
(
pivot
)
))
{
++
pivot
;
}
else
{
int
start
=
pivot
,
end
=
start
;
while
(
pivot
<
right
&&
(
!
NgraphBridge
::
isRegister
(
ops
->
at
(
pivot
)
->
Type
())))
{
while
(
pivot
<
right
&&
(
NgraphBridge
::
isSupported
(
ops
->
at
(
pivot
))))
{
++
pivot
;
++
end
;
}
...
...
paddle/fluid/operators/ngraph/ops/reshape_op.h
0 → 100644
浏览文件 @
109b5aed
/*Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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. */
#pragma once
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include "ngraph/ngraph.hpp"
#include "paddle/fluid/operators/ngraph/ops/op_bridge.h"
#include "paddle/fluid/platform/ngraph_helper.h"
namespace
paddle
{
namespace
operators
{
namespace
ngraphs
{
ngraph
::
Shape
calc_output_shape
(
const
ngraph
::
Shape
&
input_shape
,
const
std
::
vector
<
int
>&
v_shape
)
{
auto
out_shape
=
v_shape
;
for
(
size_t
i
=
0
;
i
<
v_shape
.
size
();
++
i
)
{
if
(
v_shape
[
i
]
==
0
)
{
out_shape
[
i
]
=
input_shape
[
i
];
}
}
int
size_input
=
ngraph
::
shape_size
(
input_shape
);
int
size_out
=
1
;
for
(
auto
o
:
out_shape
)
{
if
(
o
>
0
)
size_out
*=
o
;
}
for
(
auto
&
o
:
out_shape
)
{
if
(
o
==
-
1
)
o
=
size_input
/
size_out
;
}
return
ngraph
::
Shape
(
out_shape
.
begin
(),
out_shape
.
end
());
}
template
<
bool
is_v2
>
static
void
BuildReshapeNode
(
const
std
::
shared_ptr
<
framework
::
OperatorBase
>&
op
,
std
::
shared_ptr
<
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
ngraph
::
Node
>>>
ngb_node_map
)
{
std
::
shared_ptr
<
ngraph
::
Node
>
input
=
platform
::
GetInputNode
(
op
,
"X"
,
ngb_node_map
);
auto
input_shape
=
input
->
get_shape
();
// TODO(mozga-intel) The vector of shape is not supported yet, that's
// asDispensable() operator"
std
::
shared_ptr
<
ngraph
::
Node
>
shape
=
platform
::
GetInputNode
(
op
,
"Shape"
,
ngb_node_map
);
auto
op_attrs
=
framework
::
AttrReader
(
op
->
Attrs
());
std
::
vector
<
int
>
v_shape
=
op_attrs
.
Get
<
std
::
vector
<
int
>>
(
"shape"
);
auto
out
=
input
;
if
(
shape
!=
nullptr
)
{
ngraph
::
Shape
new_shape
;
for
(
auto
&
it
:
shape
->
get_shape
())
{
new_shape
.
push_back
(
it
);
}
out
=
platform
::
NgReshaper
(
input
,
shape
->
get_shape
());
}
else
{
auto
out_shape
=
calc_output_shape
(
input_shape
,
v_shape
);
out
=
platform
::
NgReshaper
(
input
,
out_shape
);
}
if
(
is_v2
)
{
platform
::
SetOutputNode
(
op
,
"XShape"
,
input
,
ngb_node_map
);
}
platform
::
SetOutputNode
(
op
,
"Out"
,
out
,
ngb_node_map
);
}
template
<
bool
is_v2
>
void
BuildReshapeGradNode
(
const
std
::
shared_ptr
<
paddle
::
framework
::
OperatorBase
>&
op
,
std
::
shared_ptr
<
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
ngraph
::
Node
>>>
ngb_node_map
)
{
auto
dout
=
paddle
::
platform
::
GetInputNode
(
op
,
"Out@GRAD"
,
ngb_node_map
);
std
::
shared_ptr
<
ngraph
::
Node
>
input
;
if
(
is_v2
)
{
input
=
paddle
::
platform
::
GetInputNode
(
op
,
"XShape"
,
ngb_node_map
);
}
else
{
input
=
paddle
::
platform
::
GetInputNode
(
op
,
"X"
,
ngb_node_map
);
}
auto
dx
=
platform
::
NgReshaper
(
dout
,
input
->
get_shape
());
paddle
::
platform
::
SetOutputNode
(
op
,
"X@GRAD"
,
dx
,
ngb_node_map
);
}
}
// namespace ngraphs
}
// namespace operators
}
// namespace paddle
REGISTER_NG_OP
(
reshape
,
BuildReshapeNode
<
false
>
);
REGISTER_NG_OP
(
reshape2
,
BuildReshapeNode
<
true
>
);
REGISTER_NG_OP
(
reshape_grad
,
BuildReshapeGradNode
<
false
>
);
REGISTER_NG_OP
(
reshape2_grad
,
BuildReshapeGradNode
<
true
>
);
paddle/fluid/platform/ngraph_helper.h
浏览文件 @
109b5aed
...
...
@@ -77,9 +77,7 @@ std::shared_ptr<ngraph::Node> GetNode(
std
::
unordered_map
<
std
::
string
,
std
::
shared_ptr
<
ngraph
::
Node
>>>
ngb_node_map
)
{
auto
&
var_names
=
var_map
.
at
(
name
);
PADDLE_ENFORCE_EQ
(
var_names
.
size
(),
1
,
"op %s name %s expects one associated var"
,
op
->
Type
(),
name
);
if
(
var_names
.
size
()
==
0
)
return
nullptr
;
if
(
ngb_node_map
->
find
(
var_names
[
0
])
!=
ngb_node_map
->
end
())
{
return
(
*
ngb_node_map
)[
var_names
[
0
]];
}
else
{
...
...
@@ -189,6 +187,22 @@ inline void TrimTrailingSingularDims(ngraph::Shape* shape) {
}
}
}
ngraph
::
element
::
Type
GetNgType
(
paddle
::
framework
::
proto
::
VarType
::
Type
dtype
)
{
ngraph
::
element
::
Type
ng_dtype
;
if
(
dtype
==
paddle
::
framework
::
proto
::
VarType
::
FP32
)
{
ng_dtype
=
ngraph
::
element
::
f32
;
}
else
if
(
dtype
==
paddle
::
framework
::
proto
::
VarType
::
FP64
)
{
ng_dtype
=
ngraph
::
element
::
f64
;
}
else
if
(
dtype
==
paddle
::
framework
::
proto
::
VarType
::
INT64
)
{
ng_dtype
=
ngraph
::
element
::
i64
;
}
else
if
(
dtype
==
paddle
::
framework
::
proto
::
VarType
::
INT32
)
{
ng_dtype
=
ngraph
::
element
::
i32
;
}
else
{
PADDLE_THROW
(
"unsupported data type: %s"
,
dtype
);
}
return
ng_dtype
;
}
}
// namespace platform
}
// namespace paddle
...
...
python/paddle/fluid/tests/unittests/ngraph/test_reshape_ngraph_op.py
0 → 100644
浏览文件 @
109b5aed
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
from
__future__
import
print_function
import
unittest
,
sys
sys
.
path
.
append
(
"../"
)
from
test_reshape_op
import
TestReshapeOp
,
TestReshapeOpDimInfer1
,
TestReshapeOpDimInfer2
,
TestReshapeOpWithInputShape
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录