Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
aadb0981
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
aadb0981
编写于
11月 07, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add `op::math::set_constant` without template
上级
f74fb790
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
91 addition
and
0 deletion
+91
-0
paddle/operators/math/math_function.cc
paddle/operators/math/math_function.cc
+48
-0
paddle/operators/math/math_function.cu
paddle/operators/math/math_function.cu
+24
-0
paddle/operators/math/math_function.h
paddle/operators/math/math_function.h
+7
-0
paddle/operators/math/math_function_test.cc
paddle/operators/math/math_function_test.cc
+12
-0
未找到文件。
paddle/operators/math/math_function.cc
浏览文件 @
aadb0981
...
...
@@ -13,6 +13,7 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/operators/math/math_function.h"
#include "paddle/framework/data_type.h"
namespace
paddle
{
namespace
operators
{
...
...
@@ -233,6 +234,53 @@ void gemv<platform::CPUPlace, double>(const platform::DeviceContext& context,
template
struct
SetConstant
<
platform
::
CPUPlace
,
float
>;
struct
TensorSetConstant
{
TensorSetConstant
(
framework
::
Tensor
*
tensor
,
float
value
)
:
tensor_
(
tensor
),
value_
(
value
)
{}
template
<
typename
T
>
void
operator
()()
const
{
auto
cpu
=
platform
::
CPUPlace
();
auto
*
begin
=
tensor_
->
mutable_data
<
T
>
(
cpu
);
std
::
fill
(
begin
,
begin
+
tensor_
->
numel
(),
static_cast
<
T
>
(
value_
));
}
framework
::
Tensor
*
tensor_
;
float
value_
;
};
template
<
>
void
set_constant_with_place
<
platform
::
CPUPlace
>
(
const
platform
::
DeviceContext
&
context
,
framework
::
Tensor
*
tensor
,
float
value
)
{
framework
::
VisitDataType
(
framework
::
ToDataType
(
tensor
->
type
()),
TensorSetConstant
(
tensor
,
value
));
}
struct
TensorSetConstantWithPlace
:
public
boost
::
static_visitor
<
void
>
{
TensorSetConstantWithPlace
(
const
platform
::
DeviceContext
&
context
,
framework
::
Tensor
*
tensor
,
float
value
)
:
context_
(
context
),
tensor_
(
tensor
),
value_
(
value
)
{}
template
<
typename
Place
>
void
operator
()(
Place
place
)
const
{
set_constant_with_place
<
Place
>
(
context_
,
tensor_
,
value_
);
}
const
platform
::
DeviceContext
&
context_
;
framework
::
Tensor
*
tensor_
;
float
value_
;
};
void
set_constant
(
const
platform
::
DeviceContext
&
context
,
framework
::
Tensor
*
tensor
,
float
value
)
{
#ifdef PADDLE_WITH_CUDA
boost
::
apply_visitor
(
TensorSetConstantWithPlace
(
context
,
tensor
,
value
),
tensor
->
place
());
#else
TensorSetConstantWithPlace
func
(
context
,
tensor
,
value
);
func
(
platform
::
CPUPlace
());
#endif
}
}
// namespace math
}
// namespace operators
}
// namespace paddle
paddle/operators/math/math_function.cu
浏览文件 @
aadb0981
...
...
@@ -232,6 +232,30 @@ void gemv<platform::GPUPlace, double>(const platform::DeviceContext& context,
template
struct
SetConstant
<
platform
::
GPUPlace
,
float
>;
struct
TensorSetConstant
{
TensorSetConstant
(
const
platform
::
DeviceContext
&
context
,
framework
::
Tensor
*
tensor
,
float
value
)
:
context_
(
context
),
tensor_
(
tensor
),
value_
(
value
)
{}
template
<
typename
T
>
void
operator
()()
const
{
SetConstant
<
platform
::
GPUPlace
,
T
>
functor
;
functor
(
context_
,
tensor_
,
static_cast
<
T
>
(
value_
));
}
const
platform
::
DeviceContext
&
context_
;
framework
::
Tensor
*
tensor_
;
float
value_
;
};
template
<
>
void
set_constant_with_place
<
platform
::
GPUPlace
>
(
const
platform
::
DeviceContext
&
context
,
framework
::
Tensor
*
tensor
,
float
value
)
{
framework
::
VisitDataType
(
framework
::
ToDataType
(
tensor
->
type
()),
TensorSetConstant
(
context
,
tensor
,
value
));
}
}
// namespace math
}
// namespace operators
}
// namespace paddle
paddle/operators/math/math_function.h
浏览文件 @
aadb0981
...
...
@@ -108,6 +108,13 @@ struct SetConstant {
}
};
template
<
typename
Place
>
void
set_constant_with_place
(
const
platform
::
DeviceContext
&
context
,
framework
::
Tensor
*
tensor
,
float
value
);
void
set_constant
(
const
platform
::
DeviceContext
&
context
,
framework
::
Tensor
*
tensor
,
float
value
);
}
// namespace math
}
// namespace operators
}
// namespace paddle
paddle/operators/math/math_function_test.cc
浏览文件 @
aadb0981
...
...
@@ -139,3 +139,15 @@ TEST(math_function, gemv) {
GemvTest
<
float
>
(
12
,
7
,
true
);
GemvTest
<
double
>
(
7
,
9
,
true
);
}
TEST
(
math_funciton
,
set_constant
)
{
paddle
::
framework
::
Tensor
t
;
t
.
Resize
({
10
,
10
});
t
.
mutable_data
<
int
>
(
paddle
::
platform
::
CPUPlace
());
auto
*
ctx
=
new
paddle
::
platform
::
CPUDeviceContext
();
paddle
::
operators
::
math
::
set_constant
(
*
ctx
,
&
t
,
10
);
for
(
int64_t
i
=
0
;
i
<
t
.
numel
();
++
i
)
{
PADDLE_ENFORCE_EQ
(
10
,
t
.
data
<
int
>
()[
i
]);
}
delete
ctx
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录