Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
ee82e660
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看板
未验证
提交
ee82e660
编写于
11月 07, 2017
作者:
Y
Yu Yang
提交者:
GitHub
11月 07, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5447 from reyoung/feature/fill_constant_no_template
Add `op::math::set_constant` without template
上级
900b2b19
25711eda
变更
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
+47
-0
paddle/operators/math/math_function.cu
paddle/operators/math/math_function.cu
+25
-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
浏览文件 @
ee82e660
...
...
@@ -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,52 @@ 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
)
{
TensorSetConstantWithPlace
func
(
context
,
tensor
,
value
);
#ifdef PADDLE_WITH_CUDA
tensor
->
place
().
apply_visitor
(
func
);
#else
func
(
platform
::
CPUPlace
());
#endif
}
}
// namespace math
}
// namespace operators
}
// namespace paddle
paddle/operators/math/math_function.cu
浏览文件 @
ee82e660
...
...
@@ -12,6 +12,7 @@ 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/framework/data_type.h"
#include "paddle/operators/math/math_function.h"
namespace
paddle
{
...
...
@@ -232,6 +233,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
浏览文件 @
ee82e660
...
...
@@ -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
浏览文件 @
ee82e660
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录