Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
d1a089f5
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
332
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
271
列表
看板
标记
里程碑
合并请求
78
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle-Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
271
Issue
271
列表
看板
标记
里程碑
合并请求
78
合并请求
78
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
d1a089f5
编写于
3月 18, 2020
作者:
J
jiaopu
提交者:
jackzhang235
3月 24, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add scale_op
上级
3153a201
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
224 addition
and
0 deletion
+224
-0
lite/kernels/mlu/bridges/CMakeLists.txt
lite/kernels/mlu/bridges/CMakeLists.txt
+3
-0
lite/kernels/mlu/bridges/scale_op.cc
lite/kernels/mlu/bridges/scale_op.cc
+74
-0
lite/kernels/mlu/bridges/scale_op_test.cc
lite/kernels/mlu/bridges/scale_op_test.cc
+147
-0
未找到文件。
lite/kernels/mlu/bridges/CMakeLists.txt
浏览文件 @
d1a089f5
...
...
@@ -15,6 +15,7 @@ lite_cc_library(subgraph_bridge_elementwise_ops_mlu SRCS elementwise_ops.cc DEPS
lite_cc_library
(
subgraph_bridge_pool_op_mlu SRCS pool_op.cc DEPS
${
subgraph_bridge_deps_mlu
}
)
lite_cc_library
(
subgraph_bridge_softmax_op_mlu SRCS softmax_op.cc DEPS
${
subgraph_bridge_deps_mlu
}
)
lite_cc_library
(
subgraph_bridge_fc_op_mlu SRCS fc_op.cc DEPS
${
subgraph_bridge_deps_mlu
}
)
lite_cc_library
(
subgraph_bridge_scale_op_mlu SRCS scale_op.cc DEPS
${
subgraph_bridge_deps_mlu
}
)
set
(
mlu_subgraph_bridges
subgraph_bridge_registry
subgraph_bridge_utility_mlu
...
...
@@ -26,6 +27,7 @@ set(mlu_subgraph_bridges
subgraph_bridge_softmax_op_mlu
subgraph_bridge_fc_op_mlu
subgraph_bridge_batch_norm_op_mlu
subgraph_bridge_scale_op_mlu
CACHE INTERNAL
"mlu_subgraph_bridges"
)
lite_cc_library
(
subgraph_test_helper_mlu SRCS test_helper.cc DEPS
${
mlu_subgraph_bridges
}
)
...
...
@@ -36,5 +38,6 @@ lite_cc_test(test_elementwise_converter_mlu SRCS elementwise_ops_test.cc DEPS sc
lite_cc_test
(
test_pool_converter_mlu SRCS pool_op_test.cc DEPS scope optimizer target_wrapper_host model_parser program
${
mlu_subgraph_bridges
}
subgraph_compute_mlu subgraph_test_helper_mlu
)
lite_cc_test
(
test_softmax_converter_mlu SRCS softmax_op_test.cc DEPS scope optimizer target_wrapper_host model_parser program
${
mlu_subgraph_bridges
}
subgraph_compute_mlu subgraph_test_helper_mlu
)
lite_cc_test
(
test_fc_converter_mlu SRCS fc_op_test.cc DEPS scope optimizer target_wrapper_host model_parser program
${
mlu_subgraph_bridges
}
subgraph_compute_mlu subgraph_test_helper_mlu
)
lite_cc_test
(
test_scale_converter_mlu SRCS scale_op_test.cc DEPS scope optimizer target_wrapper_host model_parser program
${
mlu_subgraph_bridges
}
subgraph_compute_mlu subgraph_test_helper_mlu
)
message
(
STATUS
"+++++ mlu_subgraph_bridges:
${
mlu_subgraph_bridges
}
"
)
lite/kernels/mlu/bridges/scale_op.cc
0 → 100644
浏览文件 @
d1a089f5
// 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.
#include "lite/kernels/mlu/bridges/graph.h"
#include "lite/kernels/mlu/bridges/utility.h"
#include "lite/kernels/npu/bridges/registry.h"
namespace
paddle
{
namespace
lite
{
namespace
subgraph
{
namespace
mlu
{
int
ScaleConverter
(
void
*
ctx
,
OpLite
*
op
,
KernelBase
*
kernel
)
{
CHECK
(
ctx
!=
nullptr
);
CHECK
(
op
!=
nullptr
);
auto
graph
=
static_cast
<
Graph
*>
(
ctx
);
auto
op_info
=
op
->
op_info
();
auto
op_type
=
op_info
->
Type
();
auto
scope
=
op
->
scope
();
VLOG
(
3
)
<<
"[MLU] Converting "
+
op_type
+
"..."
;
// Create act node and set params from op
auto
x_var_name
=
op_info
->
Input
(
"X"
).
front
();
auto
out_var_name
=
op_info
->
Output
(
"Out"
).
front
();
auto
output
=
scope
->
FindVar
(
out_var_name
)
->
GetMutable
<
Tensor
>
();
auto
output_dims
=
output
->
dims
().
Vectorize
();
auto
output_tensor
=
graph
->
AddNode
(
out_var_name
,
output_dims
,
CNML_TENSOR
,
CNML_NHWC
,
graph
->
FPType
());
auto
bias_after_scale
=
op_info
->
GetAttr
<
bool
>
(
"bias_after_scale"
);
auto
scale
=
op_info
->
GetAttr
<
float
>
(
"scale"
);
auto
bias
=
op_info
->
GetAttr
<
float
>
(
"bias"
);
auto
beta
=
bias_after_scale
?
bias
:
bias
*
scale
;
std
::
vector
<
int64_t
>
shape
=
{
1
,
1
,
1
,
1
};
std
::
string
prefix
=
string_format
(
"_%p"
,
op
);
auto
alpha_tensor
=
graph
->
AddNode
(
"Alpha"
+
prefix
,
shape
,
CNML_CONST
,
CNML_NHWC
,
graph
->
FPType
());
auto
beta_tensor
=
graph
->
AddNode
(
"Beta"
+
prefix
,
shape
,
CNML_CONST
,
CNML_NHWC
,
graph
->
FPType
());
graph
->
BindConstRawData
(
"Alpha"
+
prefix
,
&
scale
,
1
);
graph
->
BindConstRawData
(
"Beta"
+
prefix
,
&
beta
,
1
);
auto
input_tensor
=
graph
->
GetNode
(
x_var_name
);
cnmlBaseOp_t
scale_op
;
CNML_CALL
(
cnmlCreateScaleOp
(
&
scale_op
,
input_tensor
->
mlu_tensor
(),
output_tensor
->
mlu_tensor
(),
alpha_tensor
->
mlu_tensor
(),
beta_tensor
->
mlu_tensor
()));
graph
->
FuseOp
(
scale_op
);
return
SUCCESS
;
}
}
// namespace mlu
}
// namespace subgraph
}
// namespace lite
}
// namespace paddle
REGISTER_SUBGRAPH_BRIDGE
(
scale
,
kMLU
,
paddle
::
lite
::
subgraph
::
mlu
::
ScaleConverter
);
lite/kernels/mlu/bridges/scale_op_test.cc
0 → 100644
浏览文件 @
d1a089f5
// 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.
#include "lite/operators/scale_op.h"
#include <gtest/gtest.h>
#include <random>
#include "lite/core/op_registry.h"
#include "lite/kernels/mlu/bridges/test_helper.h"
#include "lite/kernels/npu/bridges/registry.h"
namespace
paddle
{
namespace
lite
{
namespace
subgraph
{
namespace
mlu
{
void
scale_ref
(
const
std
::
shared_ptr
<
operators
::
ScaleOp
>
op
)
{
Scope
*
scope
=
op
->
scope
();
const
OpInfo
*
op_info
=
op
->
op_info
();
auto
x
=
scope
->
FindVar
(
op_info
->
Input
(
"X"
).
front
())
->
GetMutable
<
Tensor
>
();
auto
out
=
scope
->
FindVar
(
op_info
->
Output
(
"Out"
).
front
())
->
GetMutable
<
Tensor
>
();
float
scale
=
op_info
->
GetAttr
<
float
>
(
"scale"
);
float
bias
=
op_info
->
GetAttr
<
float
>
(
"bias"
);
bool
bias_after_scale
=
op_info
->
GetAttr
<
bool
>
(
"bias_after_scale"
);
if
(
!
bias_after_scale
)
{
bias
*=
scale
;
}
auto
x_data
=
x
->
data
<
float
>
();
auto
out_data
=
out
->
mutable_data
<
float
>
();
DDim
x_dims
=
x
->
dims
();
DDim
out_dims
=
out
->
dims
();
CHECK_EQ
(
x_dims
.
production
(),
out_dims
.
production
());
for
(
int
i
=
0
;
i
<
out_dims
.
production
();
i
++
)
{
out_data
[
i
]
=
x_data
[
i
]
*
scale
+
bias
;
}
}
void
test_scale
(
int
bs
,
int
ic
,
int
ih
,
int
iw
,
bool
bias_after_scale
,
float
scale
,
float
bias
)
{
// prepare input&output variables
Scope
scope
;
std
::
string
x_var_name
(
"x"
);
std
::
string
out_var_name
(
"out"
);
std
::
string
out_ref_var_name
(
"out_ref"
);
auto
*
x
=
scope
.
Var
(
x_var_name
)
->
GetMutable
<
Tensor
>
();
auto
*
out
=
scope
.
Var
(
out_var_name
)
->
GetMutable
<
Tensor
>
();
auto
*
out_ref
=
scope
.
Var
(
out_ref_var_name
)
->
GetMutable
<
Tensor
>
();
x
->
Resize
({
bs
,
ic
,
ih
,
iw
});
// initialize input&output data
FillTensor
<
float
,
int
>
(
x
);
// initialize op desc
cpp
::
OpDesc
opdesc
;
opdesc
.
SetType
(
"scale"
);
opdesc
.
SetInput
(
"X"
,
{
x_var_name
});
opdesc
.
SetOutput
(
"Out"
,
{
out_var_name
});
opdesc
.
SetAttr
(
"bias_after_scale"
,
bias_after_scale
);
opdesc
.
SetAttr
(
"scale"
,
scale
);
opdesc
.
SetAttr
(
"bias"
,
bias
);
// create and convert op to MLU model, then run it on MLU
auto
op
=
CreateOp
<
operators
::
ScaleOp
>
(
opdesc
,
&
scope
);
scale_ref
(
op
);
out_ref
->
CopyDataFrom
(
*
out
);
Tensor
input_trans
;
input_trans
.
Resize
({
bs
,
ic
,
ih
,
iw
});
transpose
(
x
->
mutable_data
<
float
>
(),
input_trans
.
mutable_data
<
float
>
(),
{
bs
,
ic
,
ih
,
iw
},
{
0
,
2
,
3
,
1
});
auto
os
=
out
->
dims
();
out
->
Resize
({
static_cast
<
int
>
(
os
[
0
]),
static_cast
<
int
>
(
os
[
2
]),
static_cast
<
int
>
(
os
[
3
]),
static_cast
<
int
>
(
os
[
1
])});
x
->
CopyDataFrom
(
input_trans
);
x
->
Resize
({
bs
,
ih
,
iw
,
ic
});
LaunchOp
(
op
,
{
x_var_name
},
{
out_var_name
});
// execute reference implementation and save to output tensor('out')
// compare results
auto
*
out_data
=
out
->
mutable_data
<
float
>
();
auto
*
out_ref_data
=
out_ref
->
mutable_data
<
float
>
();
Tensor
output_trans
;
output_trans
.
Resize
(
os
);
transpose
(
out_data
,
output_trans
.
mutable_data
<
float
>
(),
{
static_cast
<
int
>
(
os
[
0
]),
static_cast
<
int
>
(
os
[
2
]),
static_cast
<
int
>
(
os
[
3
]),
static_cast
<
int
>
(
os
[
1
])},
{
0
,
3
,
1
,
2
});
out_data
=
output_trans
.
mutable_data
<
float
>
();
for
(
int
i
=
0
;
i
<
out
->
dims
().
production
();
i
++
)
{
VLOG
(
5
)
<<
i
;
EXPECT_NEAR
(
out_data
[
i
],
out_ref_data
[
i
],
1e-5
);
}
}
TEST
(
MLUBridges
,
scale
)
{
for
(
auto
bs
:
{
1
,
3
})
{
for
(
auto
ic
:
{
1
,
3
})
{
for
(
auto
ih
:
{
3
,
4
})
{
for
(
auto
iw
:
{
4
,
3
})
{
for
(
auto
bias_after_scale
:
{
false
,
true
})
{
for
(
auto
scale
:
{
-
1.0
f
,
5.0
f
})
{
for
(
auto
bias
:
{
-
2.0
f
,
30.0
f
})
{
VLOG
(
3
)
<<
"bs: "
<<
bs
<<
" ic: "
<<
ic
<<
" ih: "
<<
ih
<<
" iw: "
<<
iw
// << " bias_after_scale: " << bias_after_scale
<<
" scale: "
<<
scale
<<
" bias: "
<<
bias
;
test_scale
(
bs
,
ic
,
ih
,
iw
,
bias_after_scale
,
scale
,
bias
);
}
}
}
}
}
}
}
}
}
// namespace mlu
}
// namespace subgraph
}
// namespace lite
}
// namespace paddle
USE_SUBGRAPH_BRIDGE
(
scale
,
kMLU
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录