Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
f075141b
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看板
提交
f075141b
编写于
8月 28, 2017
作者:
Z
Zhuoyuan
提交者:
GitHub
8月 28, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
switch_op.cc
上级
ff3ec3c4
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
120 addition
and
0 deletion
+120
-0
paddle/operators/switch_op.cc
paddle/operators/switch_op.cc
+120
-0
未找到文件。
paddle/operators/switch_op.cc
0 → 100644
浏览文件 @
f075141b
#include "paddle/operators/switch_op.h"
namespace
paddle
{
namespace
operators
{
// namespace if_else{
void
CondOp
::
Init
()
override
{
}
void
InferShape
(
const
std
::
shared_ptr
<
Scope
>&
scope
)
const
override
{
subnet_t
=
GetAttr
<
std
::
string
>
(
"subnet_t"
);
subnet_f
=
GetAttr
<
std
::
string
>
(
"subnet_f"
);
// Create two Nets
// I use the same style as Recurrent_op, but does it create the net?
// can be called like
Variable
*
net_t
=
scope
.
FindVar
(
subnet_t
);
Variable
*
net_f
=
scope
.
FindVar
(
subnet_f
);
net_op_t
=
scope
.
FindVar
(
net_t
)
->
GetMutable
<
NetOp
>
();
net_op_f
=
scope
.
FindVar
(
net_f
)
->
GetMutable
<
NetOp
>
();
// Create two scopes
scope_t
=
scope
.
NewScope
();
scope_f
=
scope
.
NewScope
();
// check cond of size (batch_size), type bool
net_op_t
->
InferShape
(
scope_t
);
net_op_f
->
InferShape
(
scope_f
);
// check net_op_t and net_op_f of exactly same shape?
}
void
IfElseOp
::
Run
(
const
std
::
shared_ptr
<
Scope
>&
scope
,
const
platform
::
DeviceContext
&
dev_ctx
)
const
{
/* step 1: create two subnets and scopes, supposed done in Infershape() */
/* step 2: get true and false index */
cond
=
Input
(
name
.
cond
);
// get condition tensor
auto
cond_tensor
=
scope
.
get
<
Tensor
>
(
cond
);
// tensor to cpu, whatever device it used to be in
cond_cpu
.
CopyFrom
(
cond_tensor
,
platform
::
CPUPlace
());
size_t
batch_size
=
cond_cpu
.
dims
()[
0
];
// keep index of true and false to slice, clear them first before each batch
true_index
.
clear
();
false_index
.
clear
();
// get a DDim type variable dims, check dimension
auto
dims
=
input0
.
dims
();
for
(
int
i
=
0
;
i
<
dims
;
i
++
)
{
if
(
cond_cpu
->
data
[
i
])
true_index
.
push_back
(
i
);
else
false_index
.
push_back
(
i
);
}
// turn true_index and false_index to tensors
Tensor
*
true_index_tensor
=
new
Tensor
(
true_index
);
Tensor
*
false_index_tensor
=
new
Tensor
(
false_index
);
/* Step 3: Gather */
{
// True Scope
// Create new stuff
for
(
auto
&
input
:
net_op_t
->
inputs_
)
{
scope_t
.
NewVar
(
input
);
if
(
input
.
type
()
!=
PARAMETER
)
{
// gather and slice required
// Get Tensor and gather
Tensor
*
input_gather_
=
scope_t
.
FindVar
(
input
)
->
GetMutable
<
Tensor
>
();
Tensor
*
input_full_
=
scope
.
FindVar
(
input
)
->
GetMutable
<
Tensor
>
();
input_gather_
=
Gather
(
input_full_
,
true_index_tensor
);
}
}
for
(
auto
&
output
:
net_op
->
outputs_
)
{
scope_t
.
NewVar
(
output
);
}
net_op_t
.
Run
();
}
{
// False Scope
// Create new stuff
for
(
auto
&
input
:
net_op_f
->
inputs_
)
{
scope_f
.
NewVar
(
input
);
if
(
input
.
type
()
!=
PARAMETER
)
{
// gather and slice required
// Get Tensor and gather
Tensor
*
input_gather_
=
scope_f
.
FindVar
(
input
)
->
GetMutable
<
Tensor
>
();
Tensor
*
input_full_
=
scope
.
FindVar
(
input
)
->
GetMutable
<
Tensor
>
();
input_gather_
=
Gather
(
input_full_
,
false_index_tensor
);
}
}
for
(
auto
&
output
:
net_op
->
outputs_
)
{
scope_t
.
NewVar
(
output
);
}
net_op_f
.
Run
();
}
/* Merge Output Together by scatter update */
for
(
auto
&
ouput
:
outputs_
)
{
Tensor
*
output_t
=
scope_t
->
FindVar
(
output
)
->
GetMutable
<
Tensor
>
();
Tensor
*
output_f
=
scope_f
->
FindVar
(
output
)
->
GetMutable
<
Tensor
>
();
Tensor
*
output_tensor
=
scope
->
FindVar
(
output
)
->
GetMutable
<
Tensor
>
();
Scatter
(
output_t
,
output_tensor
,
true_index_tensor
);
Scatter
(
output_f
,
output_tensor
,
false_index_tensor
);
}
}
}
// namespace operators
}
// namespace paddle
REGISTER_OP
(
ifelse_op
,
paddle
::
operators
::
IfElseOp
,
paddle
::
operators
::
RecurrentAlgorithmProtoAndCheckerMaker
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录