Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
b9fda2ff
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
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看板
未验证
提交
b9fda2ff
编写于
10月 01, 2020
作者:
J
Jacek Czaja
提交者:
GitHub
10月 01, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix to issue
#25537
(#27546)
* - condidate fix to issue
#25537
test=develop * - UT for transpose NHWC test=develop
上级
966447e3
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
133 addition
and
1 deletion
+133
-1
paddle/fluid/framework/data_layout_transform.cc
paddle/fluid/framework/data_layout_transform.cc
+1
-1
paddle/fluid/operators/CMakeLists.txt
paddle/fluid/operators/CMakeLists.txt
+1
-0
paddle/fluid/operators/mkldnn/nhwc_op_tests.cmake
paddle/fluid/operators/mkldnn/nhwc_op_tests.cmake
+2
-0
paddle/fluid/operators/mkldnn/test_mkldnn_op_nhwc.cc
paddle/fluid/operators/mkldnn/test_mkldnn_op_nhwc.cc
+94
-0
paddle/fluid/operators/transpose_op.cc
paddle/fluid/operators/transpose_op.cc
+13
-0
paddle/fluid/platform/mkldnn_helper.h
paddle/fluid/platform/mkldnn_helper.h
+22
-0
未找到文件。
paddle/fluid/framework/data_layout_transform.cc
浏览文件 @
b9fda2ff
...
@@ -203,7 +203,7 @@ void innerTransDataLayoutFromMKLDNN(DataLayout in_layout, DataLayout out_layout,
...
@@ -203,7 +203,7 @@ void innerTransDataLayoutFromMKLDNN(DataLayout in_layout, DataLayout out_layout,
// As MKL-DNN description was in NCHW and paddle is expecting NHWC
// As MKL-DNN description was in NCHW and paddle is expecting NHWC
platform
::
MatchShapeToLayout
(
out
,
in_layout
,
out_layout
);
platform
::
MatchShapeToLayout
(
out
,
in_layout
,
out_layout
);
out
->
set_layout
(
out_layout
);
out
->
set_layout
(
DataLayout
::
kNCHW
);
// reset format since the out tensor will be feed to non-MKLDNN OPkernel
// reset format since the out tensor will be feed to non-MKLDNN OPkernel
out
->
set_format
(
MKLDNNMemoryFormat
::
undef
);
out
->
set_format
(
MKLDNNMemoryFormat
::
undef
);
}
}
...
...
paddle/fluid/operators/CMakeLists.txt
浏览文件 @
b9fda2ff
...
@@ -144,4 +144,5 @@ cc_test(op_debug_string_test SRCS op_debug_string_test.cc DEPS elementwise_add_o
...
@@ -144,4 +144,5 @@ cc_test(op_debug_string_test SRCS op_debug_string_test.cc DEPS elementwise_add_o
if
(
WITH_MKLDNN
)
if
(
WITH_MKLDNN
)
include
(
mkldnn/inplace_op_tests.cmake
)
include
(
mkldnn/inplace_op_tests.cmake
)
include
(
mkldnn/nhwc_op_tests.cmake
)
endif
()
endif
()
paddle/fluid/operators/mkldnn/nhwc_op_tests.cmake
0 → 100644
浏览文件 @
b9fda2ff
cc_test
(
test_mkldnn_op_nhwc SRCS mkldnn/test_mkldnn_op_nhwc.cc DEPS op_registry pool_op pooling transpose_op scope device_context enforce executor
)
paddle/fluid/operators/mkldnn/test_mkldnn_op_nhwc.cc
0 → 100644
浏览文件 @
b9fda2ff
// Copyright (c) 2020 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.
#include <algorithm>
#include <cstdlib>
#include <memory>
#include <random>
#include "gtest/gtest.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/place.h"
USE_OP
(
pool2d
);
USE_OP_DEVICE_KERNEL
(
pool2d
,
MKLDNN
);
USE_OP
(
transpose
);
USE_OP_DEVICE_KERNEL
(
transpose
,
MKLDNN
);
namespace
paddle
{
namespace
operators
{
struct
InputVars
{
std
::
string
name
;
framework
::
LoDTensor
*
tensor
;
};
TEST
(
test_pool2d_transpose_nhwc
,
cpu_place
)
{
framework
::
DDim
dims
({
1
,
4
,
8
,
512
});
// NHWC shape
framework
::
DDim
expected_dims
({
1
,
7
,
512
,
3
});
// NHWC expected shape
platform
::
CPUPlace
p
;
framework
::
Scope
scope
;
InputVars
input_name
=
{
"x"
,
scope
.
Var
(
"x"
)
->
GetMutable
<
framework
::
LoDTensor
>
()};
// Initialize input data
std
::
uniform_real_distribution
<
float
>
dist
(
static_cast
<
float
>
(
10.0
),
static_cast
<
float
>
(
20.0
));
std
::
mt19937
engine
;
size_t
numel
=
static_cast
<
size_t
>
(
framework
::
product
(
dims
));
input_name
.
tensor
->
Resize
(
dims
);
auto
data_ptr
=
input_name
.
tensor
->
mutable_data
<
float
>
(
p
);
for
(
size_t
i
=
0
;
i
<
numel
;
++
i
)
{
data_ptr
[
i
]
=
dist
(
engine
);
}
scope
.
Var
(
"y"
)
->
GetMutable
<
framework
::
LoDTensor
>
();
auto
*
z
=
scope
.
Var
(
"z"
)
->
GetMutable
<
framework
::
LoDTensor
>
();
auto
&
pool
=
platform
::
DeviceContextPool
::
Instance
();
// Make pool2d followed by transpose
auto
ksize
=
std
::
vector
<
int
>
(
2
,
2
);
auto
op_pool
=
framework
::
OpRegistry
::
CreateOp
(
"pool2d"
,
{{
"X"
,
{
"x"
}}},
{{
"Out"
,
{
"y"
}}},
{{
"pooling_type"
,
{
std
::
string
(
"max"
)}},
{
"ksize"
,
{
ksize
}},
{
"data_format"
,
{
std
::
string
(
"NHWC"
)}},
{
"use_mkldnn"
,
{
true
}}});
auto
axis
=
std
::
vector
<
int
>
(
4
,
0
);
axis
[
1
]
=
2
;
axis
[
2
]
=
3
;
axis
[
3
]
=
1
;
auto
op_transpose
=
framework
::
OpRegistry
::
CreateOp
(
"transpose"
,
{{
"X"
,
{
"y"
}}},
{{
"Out"
,
{
"z"
}}},
{{
"axis"
,
{
axis
}},
{
"use_mkldnn"
,
{
true
}}});
op_pool
->
Run
(
scope
,
p
);
op_transpose
->
Run
(
scope
,
p
);
pool
.
Get
(
p
)
->
Wait
();
// Verify shape of output
PADDLE_ENFORCE_EQ
(
z
->
dims
(),
expected_dims
,
platform
::
errors
::
InvalidArgument
(
"Computed shape does not match expected shape"
));
}
}
// namespace operators
}
// namespace paddle
paddle/fluid/operators/transpose_op.cc
浏览文件 @
b9fda2ff
...
@@ -61,6 +61,19 @@ class TransposeOp : public framework::OperatorWithKernel {
...
@@ -61,6 +61,19 @@ class TransposeOp : public framework::OperatorWithKernel {
}
}
framework
::
DDim
out_dims
(
x_dims
);
framework
::
DDim
out_dims
(
x_dims
);
#ifdef PADDLE_WITH_MKLDNN
// Here we need to match dims to paddle layout
// as we are producing non-oneDNN result
if
((
x_dims
.
size
()
>=
3
)
&&
(
paddle
::
platform
::
MKLDNNDeviceContext
::
tls
()
.
get_cur_paddle_data_layout
()
==
framework
::
DataLayout
::
kNHWC
))
{
auto
dims
=
framework
::
vectorize
<
int
>
(
x_dims
);
std
::
rotate
(
dims
.
begin
()
+
1
,
dims
.
begin
()
+
2
,
dims
.
end
());
x_dims
=
x_dims
.
reshape
(
dims
);
VLOG
(
3
)
<<
"Rotating Shape in Transpose from: kMKLDNN to: kNHWC output_shape"
;
}
#endif
for
(
size_t
i
=
0
;
i
<
axis_size
;
i
++
)
{
for
(
size_t
i
=
0
;
i
<
axis_size
;
i
++
)
{
out_dims
[
i
]
=
x_dims
[
axis
[
i
]];
out_dims
[
i
]
=
x_dims
[
axis
[
i
]];
}
}
...
...
paddle/fluid/platform/mkldnn_helper.h
浏览文件 @
b9fda2ff
...
@@ -14,7 +14,9 @@ limitations under the License. */
...
@@ -14,7 +14,9 @@ limitations under the License. */
#pragma once
#pragma once
#include <algorithm>
#include <algorithm>
#include <iostream>
#include <memory>
#include <memory>
#include <sstream>
#include <string>
#include <string>
#include <utility>
#include <utility>
#include <vector>
#include <vector>
...
@@ -81,12 +83,30 @@ inline void MatchShapeToLayout(framework::Tensor* tensor_in,
...
@@ -81,12 +83,30 @@ inline void MatchShapeToLayout(framework::Tensor* tensor_in,
return
;
return
;
}
}
auto
print_dims
=
[](
const
std
::
vector
<
int
>&
dims
)
{
std
::
ostringstream
oss
;
if
(
!
dims
.
empty
())
{
oss
<<
"["
;
// Convert all but the last element to avoid a trailing ","
std
::
copy
(
dims
.
begin
(),
dims
.
end
()
-
1
,
std
::
ostream_iterator
<
int
>
(
oss
,
","
));
// Now add the last element with no delimiter
oss
<<
dims
.
back
()
<<
"]"
;
}
return
oss
.
str
();
};
switch
(
from
)
{
switch
(
from
)
{
case
framework
:
:
DataLayout
::
kMKLDNN
:
case
framework
:
:
DataLayout
::
kMKLDNN
:
if
(
to
==
framework
::
DataLayout
::
kNHWC
)
{
if
(
to
==
framework
::
DataLayout
::
kNHWC
)
{
auto
dims
=
framework
::
vectorize
<
int
>
(
tensor_in
->
dims
());
auto
dims
=
framework
::
vectorize
<
int
>
(
tensor_in
->
dims
());
std
::
rotate
(
dims
.
begin
()
+
1
,
dims
.
begin
()
+
2
,
dims
.
end
());
std
::
rotate
(
dims
.
begin
()
+
1
,
dims
.
begin
()
+
2
,
dims
.
end
());
tensor_in
->
Resize
(
framework
::
make_ddim
(
dims
));
tensor_in
->
Resize
(
framework
::
make_ddim
(
dims
));
VLOG
(
3
)
<<
"Rotating Shape from: kMKLDNN to: kNHWC output_shape"
<<
print_dims
(
dims
);
}
}
break
;
break
;
case
framework
:
:
DataLayout
::
kNHWC
:
case
framework
:
:
DataLayout
::
kNHWC
:
...
@@ -94,6 +114,8 @@ inline void MatchShapeToLayout(framework::Tensor* tensor_in,
...
@@ -94,6 +114,8 @@ inline void MatchShapeToLayout(framework::Tensor* tensor_in,
auto
dims
=
framework
::
vectorize
<
int
>
(
tensor_in
->
dims
());
auto
dims
=
framework
::
vectorize
<
int
>
(
tensor_in
->
dims
());
std
::
rotate
(
dims
.
begin
()
+
1
,
dims
.
end
()
-
1
,
dims
.
end
());
std
::
rotate
(
dims
.
begin
()
+
1
,
dims
.
end
()
-
1
,
dims
.
end
());
tensor_in
->
Resize
(
framework
::
make_ddim
(
dims
));
tensor_in
->
Resize
(
framework
::
make_ddim
(
dims
));
VLOG
(
3
)
<<
"Rotating Shape from: kNHWC to: kMKLDNN output_shape"
<<
print_dims
(
dims
);
}
}
break
;
break
;
default:
default:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录