Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
95658767
P
Paddle
项目概览
机器未来
/
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
95658767
编写于
3月 30, 2018
作者:
F
fengjiayi
提交者:
GitHub
3月 30, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #9428 from JiayiFeng/kernel_of_increment_op
kernels of IncrementOp
上级
b7b0342f
1e4f442a
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
133 addition
and
64 deletion
+133
-64
paddle/fluid/operators/compare_op.cc
paddle/fluid/operators/compare_op.cc
+8
-1
paddle/fluid/operators/conditional_block_op.cc
paddle/fluid/operators/conditional_block_op.cc
+12
-1
paddle/fluid/operators/increment_op.cc
paddle/fluid/operators/increment_op.cc
+37
-57
paddle/fluid/operators/increment_op.cu
paddle/fluid/operators/increment_op.cu
+22
-0
paddle/fluid/operators/increment_op.h
paddle/fluid/operators/increment_op.h
+39
-0
python/paddle/fluid/layers/control_flow.py
python/paddle/fluid/layers/control_flow.py
+15
-5
未找到文件。
paddle/fluid/operators/compare_op.cc
浏览文件 @
95658767
...
...
@@ -29,6 +29,11 @@ class CompareOpProtoMaker : public framework::OpProtoAndCheckerMaker {
AddInput
(
"Y"
,
string
::
Sprintf
(
"(LoDTensor) the right hand operand of %s operator"
,
comment
.
type
));
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"
,
string
::
Sprintf
(
"(LoDTensor) n-dim bool tensor. Each element is %s"
,
comment
.
equation
));
...
...
@@ -75,7 +80,9 @@ class CompareOp : public framework::OperatorWithKernel {
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
framework
::
OpKernelType
kt
=
OperatorWithKernel
::
GetExpectedKernelType
(
ctx
);
// CompareOp kernel's device type is decided by input tensor place
kt
.
place_
=
ctx
.
Input
<
framework
::
LoDTensor
>
(
"X"
)
->
place
();
bool
force_cpu
=
ctx
.
Attr
<
bool
>
(
"force_cpu"
);
kt
.
place_
=
force_cpu
?
platform
::
CPUPlace
()
:
ctx
.
Input
<
framework
::
LoDTensor
>
(
"X"
)
->
place
();
return
kt
;
}
};
...
...
paddle/fluid/operators/conditional_block_op.cc
浏览文件 @
95658767
...
...
@@ -54,7 +54,18 @@ class ConditionalOp : public framework::OperatorBase {
"numel should be 1, actual numel is %d"
,
ips
[
0
]
->
numel
());
}
return
ips
[
0
]
->
data
<
bool
>
()[
0
];
bool
res
=
false
;
if
(
platform
::
is_gpu_place
(
ips
[
0
]
->
place
()))
{
#ifdef PADDLE_WITH_CUDA
framework
::
LoDTensor
cpu_tensor
;
framework
::
TensorCopy
(
*
ips
[
0
],
platform
::
CPUPlace
(),
&
cpu_tensor
);
platform
::
DeviceContextPool
::
Instance
().
Get
(
ips
[
0
]
->
place
())
->
Wait
();
res
=
cpu_tensor
.
data
<
bool
>
()[
0
];
#endif
}
else
{
res
=
ips
[
0
]
->
data
<
bool
>
()[
0
];
}
return
res
;
}
};
...
...
paddle/fluid/operators/increment_op.cc
浏览文件 @
95658767
/
* Copyright (c) 2016
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 "paddle/fluid/
framework/op_registry
.h"
/
/ Copyright (c) 2018
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 "paddle/fluid/
operators/increment_op
.h"
namespace
paddle
{
namespace
operators
{
class
Increment
InferShape
:
public
framework
::
InferShapeBase
{
class
Increment
Op
:
public
framework
::
OperatorWithKernel
{
public:
void
operator
()(
framework
::
InferShapeContext
*
ctx
)
const
override
{
IncrementOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
OperatorWithKernel
(
type
,
inputs
,
outputs
,
attrs
)
{}
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"X"
),
"Input(X) of IncrementOp should not be null."
);
PADDLE_ENFORCE
(
ctx
->
HasOutput
(
"Out"
),
"Output(Out) of IncrementOp should not be null."
);
PADDLE_ENFORCE_EQ
(
1
,
framework
::
product
(
ctx
->
GetInputDim
(
"X"
)));
ctx
->
SetOutputDim
(
"Out"
,
ctx
->
GetInputDim
(
"X"
));
ctx
->
ShareLoD
(
"X"
,
"Out"
);
}
};
struct
IncrementFunctor
{
IncrementFunctor
(
const
framework
::
LoDTensor
&
x
,
framework
::
LoDTensor
*
out
,
float
value
)
:
x_
(
x
),
out_
(
out
),
value_
(
value
)
{}
template
<
typename
T
>
void
operator
()()
const
{
*
out_
->
data
<
T
>
()
=
*
x_
.
data
<
T
>
()
+
static_cast
<
T
>
(
value_
);
}
const
framework
::
LoDTensor
&
x_
;
framework
::
LoDTensor
*
out_
;
float
value_
;
};
class
IncrementOp
:
public
framework
::
OperatorBase
{
public:
IncrementOp
(
const
std
::
string
&
type
,
const
framework
::
VariableNameMap
&
inputs
,
const
framework
::
VariableNameMap
&
outputs
,
const
framework
::
AttributeMap
&
attrs
)
:
OperatorBase
(
type
,
inputs
,
outputs
,
attrs
)
{}
private:
void
RunImpl
(
const
framework
::
Scope
&
scope
,
const
platform
::
Place
&
place
)
const
override
{
auto
&
x
=
scope
.
FindVar
(
Input
(
"X"
))
->
Get
<
framework
::
LoDTensor
>
();
auto
&
out
=
*
scope
.
FindVar
(
Output
(
"Out"
))
->
GetMutable
<
framework
::
LoDTensor
>
();
PADDLE_ENFORCE
(
platform
::
is_cpu_place
(
x
.
place
()));
out
.
Resize
(
x
.
dims
());
out
.
mutable_data
(
x
.
place
(),
x
.
type
());
float
value
=
Attr
<
float
>
(
"step"
);
VLOG
(
10
)
<<
Output
(
"Out"
)
<<
" increase "
<<
Input
(
"X"
)
<<
" with "
<<
value
;
framework
::
VisitDataType
(
framework
::
ToDataType
(
out
.
type
()),
IncrementFunctor
(
x
,
&
out
,
value
));
protected:
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
framework
::
OpKernelType
kt
=
OperatorWithKernel
::
GetExpectedKernelType
(
ctx
);
// IncrementOp kernel's device type is decided by input tensor place
kt
.
place_
=
ctx
.
Input
<
framework
::
LoDTensor
>
(
"X"
)
->
place
();
return
kt
;
}
};
...
...
@@ -108,5 +83,10 @@ class IncrementGradOpMaker : public framework::SingleGradOpDescMaker {
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OPERATOR
(
increment
,
ops
::
IncrementOp
,
ops
::
IncrementInferShape
,
ops
::
IncrementOpMaker
,
ops
::
IncrementGradOpMaker
);
REGISTER_OPERATOR
(
increment
,
ops
::
IncrementOp
,
ops
::
IncrementOpMaker
,
ops
::
IncrementGradOpMaker
);
REGISTER_OP_CPU_KERNEL
(
increment
,
ops
::
IncrementKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
IncrementKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
,
ops
::
IncrementKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int
>
,
ops
::
IncrementKernel
<
paddle
::
platform
::
CPUDeviceContext
,
int64_t
>
)
paddle/fluid/operators/increment_op.cu
0 → 100644
浏览文件 @
95658767
// Copyright (c) 2018 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 "paddle/fluid/operators/increment_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_CUDA_KERNEL
(
increment
,
ops
::
IncrementKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
IncrementKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
>
,
ops
::
IncrementKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int
>
,
ops
::
IncrementKernel
<
paddle
::
platform
::
CUDADeviceContext
,
int64_t
>
)
paddle/fluid/operators/increment_op.h
0 → 100644
浏览文件 @
95658767
// Copyright (c) 2018 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.
#pragma once
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
DeviceContext
,
typename
T
>
class
IncrementKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
x_tensor
=
context
.
Input
<
framework
::
Tensor
>
(
"X"
);
auto
*
out_tensor
=
context
.
Output
<
framework
::
Tensor
>
(
"Out"
);
float
step
=
context
.
Attr
<
float
>
(
"step"
);
out_tensor
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
&
dev
=
*
context
.
template
device_context
<
DeviceContext
>().
eigen_device
();
framework
::
EigenScalar
<
T
>::
From
(
*
out_tensor
).
device
(
dev
)
=
framework
::
EigenScalar
<
T
>::
From
(
*
x_tensor
)
+
static_cast
<
T
>
(
step
);
}
};
}
// namespace operators
}
// namespace paddle
python/paddle/fluid/layers/control_flow.py
浏览文件 @
95658767
...
...
@@ -18,6 +18,7 @@ from tensor import assign, fill_constant
from
..
import
core
from
..framework
import
Program
,
Variable
,
Operator
from
..layer_helper
import
LayerHelper
,
unique_name
from
..initializer
import
force_init_on_cpu
from
ops
import
logical_and
,
logical_not
,
logical_or
__all__
=
[
...
...
@@ -949,7 +950,7 @@ def create_array(dtype):
dtype
=
dtype
)
def
less_than
(
x
,
y
,
cond
=
None
,
**
ignored
):
def
less_than
(
x
,
y
,
force_cpu
=
True
,
cond
=
None
,
**
ignored
):
"""
**Less than**
...
...
@@ -958,6 +959,7 @@ def less_than(x, y, cond=None, **ignored):
Args:
x(Variable): First operand of *less_than*
y(Variable): Second operand of *less_than*
force_cpu(Bool|True): The output data will be on CPU if set true.
cond(Variable|None): Optional output variable to store the result of *less_than*
Returns:
...
...
@@ -974,8 +976,11 @@ def less_than(x, y, cond=None, **ignored):
cond
.
stop_gradient
=
True
helper
.
append_op
(
type
=
'less_than'
,
inputs
=
{
'X'
:
[
x
],
'Y'
:
[
y
]},
outputs
=
{
'Out'
:
[
cond
]})
type
=
'less_than'
,
inputs
=
{
'X'
:
[
x
],
'Y'
:
[
y
]},
outputs
=
{
'Out'
:
[
cond
]},
attrs
=
{
'force_cpu'
:
force_cpu
or
force_init_on_cpu
()})
return
cond
...
...
@@ -1396,7 +1401,8 @@ class DynamicRNN(object):
type
=
'less_than'
,
inputs
=
{
'X'
:
self
.
step_idx
,
'Y'
:
self
.
max_seq_len
},
outputs
=
{
'Out'
:
self
.
cond
})
outputs
=
{
'Out'
:
self
.
cond
},
attrs
=
{
'force_cpu'
:
True
})
input_array
=
parent_block
.
create_var
(
name
=
unique_name
.
generate
(
'dynamic_rnn_input_array'
),
...
...
@@ -1445,7 +1451,11 @@ class DynamicRNN(object):
for
new_mem
,
mem_array
in
self
.
mem_link
:
array_write
(
x
=
new_mem
,
i
=
self
.
step_idx
,
array
=
mem_array
)
less_than
(
x
=
self
.
step_idx
,
y
=
self
.
max_seq_len
,
cond
=
self
.
cond
)
less_than
(
x
=
self
.
step_idx
,
y
=
self
.
max_seq_len
,
force_cpu
=
True
,
cond
=
self
.
cond
)
self
.
status
=
DynamicRNN
.
AFTER_RNN
for
each_array
in
self
.
output_array
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录