Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
5ee62383
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
695
Star
11112
Fork
2696
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
184
列表
看板
标记
里程碑
合并请求
40
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
184
Issue
184
列表
看板
标记
里程碑
合并请求
40
合并请求
40
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
5ee62383
编写于
11月 07, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rewrite fill_constant op
上级
aadb0981
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
57 addition
and
84 deletion
+57
-84
paddle/framework/data_type.h
paddle/framework/data_type.h
+15
-0
paddle/framework/ddim.cc
paddle/framework/ddim.cc
+7
-0
paddle/framework/ddim.h
paddle/framework/ddim.h
+2
-0
paddle/operators/fill_constant_op.cc
paddle/operators/fill_constant_op.cc
+33
-23
paddle/operators/fill_constant_op.cu
paddle/operators/fill_constant_op.cu
+0
-24
paddle/operators/fill_constant_op.h
paddle/operators/fill_constant_op.h
+0
-37
未找到文件。
paddle/framework/data_type.h
浏览文件 @
5ee62383
...
...
@@ -34,6 +34,21 @@ inline DataType ToDataType(std::type_index type) {
}
}
inline
std
::
type_index
ToTypeIndex
(
DataType
type
)
{
switch
(
type
)
{
case
DataType
::
FP32
:
return
typeid
(
float
);
case
DataType
::
FP64
:
return
typeid
(
double
);
case
DataType
::
INT32
:
return
typeid
(
int
);
case
DataType
::
INT64
:
return
typeid
(
int64_t
);
default:
PADDLE_THROW
(
"Not support type %d"
,
type
);
}
}
template
<
typename
Visitor
>
inline
void
VisitDataType
(
DataType
type
,
Visitor
visitor
)
{
switch
(
type
)
{
...
...
paddle/framework/ddim.cc
浏览文件 @
5ee62383
...
...
@@ -79,6 +79,13 @@ DDim make_ddim(const std::vector<int64_t>& dims) {
return
result
;
}
DDim
make_ddim
(
const
std
::
vector
<
int
>&
dims
)
{
std
::
vector
<
int64_t
>
res
(
dims
.
size
());
std
::
transform
(
dims
.
begin
(),
dims
.
end
(),
res
.
begin
(),
[](
int
d
)
{
return
static_cast
<
int64_t
>
(
d
);
});
return
make_ddim
(
res
);
}
/// @cond HIDDEN
// XXX For some reason, putting this in an anonymous namespace causes errors
class
DynamicMutableIndexer
:
public
boost
::
static_visitor
<
int64_t
&>
{
...
...
paddle/framework/ddim.h
浏览文件 @
5ee62383
...
...
@@ -81,6 +81,8 @@ struct DDim {
*/
DDim
make_ddim
(
const
std
::
vector
<
int64_t
>&
dims
);
DDim
make_ddim
(
const
std
::
vector
<
int
>&
dims
);
/**
* \brief Make a DDim from an initializer list
*
...
...
paddle/operators/fill_constant_op.cc
浏览文件 @
5ee62383
...
...
@@ -12,32 +12,41 @@ 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/operators/fill_constant_op.h"
#include "paddle/framework/data_type.h"
#include "paddle/framework/op_registry.h"
#include "paddle/operators/math/math_function.h"
namespace
paddle
{
namespace
operators
{
class
FillConstant
Op
:
public
framework
::
OperatorWithKernel
{
class
FillConstant
InferShape
:
public
framework
::
InferShapeBase
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
void
operator
()(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of FillConstantOp should not be null."
);
auto
&
shape
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
int
>>
(
"shape"
);
std
::
vector
<
int64_t
>
shape_int64
(
shape
.
size
(),
0
);
std
::
transform
(
shape
.
begin
(),
shape
.
end
(),
shape_int64
.
begin
(),
[](
int
a
)
{
return
static_cast
<
int64_t
>
(
a
);
});
auto
dims
=
framework
::
make_ddim
(
shape_int64
);
ctx
->
SetOutputDim
(
"Out"
,
dims
);
ctx
->
SetOutputDim
(
"Out"
,
framework
::
make_ddim
(
shape
));
}
};
protected:
framework
::
DataType
IndicateDataType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
int
data_type
=
ctx
.
Attr
<
int
>
(
"data_type"
);
VLOG
(
10
)
<<
" FillConstant data_type = "
<<
data_type
;
return
static_cast
<
framework
::
DataType
>
(
data_type
);
class
FillConstantOp
:
public
framework
::
OperatorBase
{
public:
using
framework
::
OperatorBase
::
OperatorBase
;
void
Run
(
const
framework
::
Scope
&
scope
,
const
platform
::
DeviceContext
&
dev_ctx
)
const
override
{
auto
data_type
=
static_cast
<
framework
::
DataType
>
(
Attr
<
int
>
(
"data_type"
));
auto
value
=
Attr
<
float
>
(
"value"
);
auto
force_cpu
=
Attr
<
bool
>
(
"force_cpu"
);
auto
&
out
=
*
scope
.
FindVar
(
Output
(
"Out"
))
->
GetMutable
<
framework
::
LoDTensor
>
();
out
.
Resize
(
framework
::
make_ddim
(
Attr
<
std
::
vector
<
int
>>
(
"shape"
)));
if
(
force_cpu
)
{
auto
cpu
=
platform
::
CPUPlace
();
out
.
mutable_data
(
cpu
,
framework
::
ToTypeIndex
(
data_type
));
}
else
{
out
.
mutable_data
(
dev_ctx
.
GetPlace
(),
framework
::
ToTypeIndex
(
data_type
));
}
math
::
set_constant
(
dev_ctx
,
&
out
,
value
);
}
};
...
...
@@ -53,6 +62,11 @@ class FillConstantOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr
<
std
::
vector
<
int
>>
(
"shape"
,
"(vector<int>) The shape of the output"
);
AddAttr
<
float
>
(
"value"
,
"(float, default 0) The value to be filled"
)
.
SetDefault
(
0.0
f
);
AddAttr
<
bool
>
(
"force_cpu"
,
"(bool, default false) Force fill output variable to cpu "
"memory. Otherwise, fill output variable to the running "
"device"
)
.
SetDefault
(
false
);
AddOutput
(
"Out"
,
"(Tensor) Tensor of specified shape will be filled "
"with the specified value"
);
...
...
@@ -68,10 +82,6 @@ Fill up a variable with specified constant value.
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_WITHOUT_GRADIENT
(
fill_constant
,
ops
::
FillConstantOp
,
ops
::
FillConstantOpMaker
);
REGISTER_OP_CPU_KERNEL
(
fill_constant
,
ops
::
FillConstantOpKernel
<
paddle
::
platform
::
CPUPlace
,
float
>
,
ops
::
FillConstantOpKernel
<
paddle
::
platform
::
CPUPlace
,
double
>
,
ops
::
FillConstantOpKernel
<
paddle
::
platform
::
CPUPlace
,
int
>
,
ops
::
FillConstantOpKernel
<
paddle
::
platform
::
CPUPlace
,
int64_t
>
);
REGISTER_OPERATOR
(
fill_constant
,
ops
::
FillConstantOp
,
ops
::
FillConstantInferShape
,
ops
::
FillConstantOpMaker
,
paddle
::
framework
::
EmptyGradOpMaker
);
paddle/operators/fill_constant_op.cu
已删除
100644 → 0
浏览文件 @
aadb0981
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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. */
#define EIGEN_USE_GPU
#include "paddle/framework/op_registry.h"
#include "paddle/operators/fill_constant_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
fill_constant
,
ops
::
FillConstantOpKernel
<
paddle
::
platform
::
GPUPlace
,
float
>
,
ops
::
FillConstantOpKernel
<
paddle
::
platform
::
GPUPlace
,
double
>
,
ops
::
FillConstantOpKernel
<
paddle
::
platform
::
GPUPlace
,
int
>
,
ops
::
FillConstantOpKernel
<
paddle
::
platform
::
GPUPlace
,
int64_t
>
);
paddle/operators/fill_constant_op.h
已删除
100644 → 0
浏览文件 @
aadb0981
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
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 "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
Place
,
typename
T
>
class
FillConstantOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
out
=
ctx
.
Output
<
framework
::
Tensor
>
(
"Out"
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
value
=
ctx
.
Attr
<
float
>
(
"value"
);
auto
out_eigen
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
out
);
auto
place
=
ctx
.
GetEigenDevice
<
Place
>
();
out_eigen
.
device
(
place
)
=
out_eigen
.
constant
(
static_cast
<
T
>
(
value
));
}
};
}
// namespace operators
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录