Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
45c4dcaa
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看板
提交
45c4dcaa
编写于
10月 05, 2017
作者:
Q
qijun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add fetch operator
上级
20725f2d
变更
8
显示空白变更内容
内联
并排
Showing
8 changed file
with
218 addition
and
22 deletion
+218
-22
paddle/framework/executor.cc
paddle/framework/executor.cc
+9
-9
paddle/framework/executor_test.cc
paddle/framework/executor_test.cc
+67
-0
paddle/framework/scope.cc
paddle/framework/scope.cc
+4
-1
paddle/operators/activation_op.cu
paddle/operators/activation_op.cu
+9
-9
paddle/operators/feed_op.cc
paddle/operators/feed_op.cc
+3
-3
paddle/operators/fetch_op.cc
paddle/operators/fetch_op.cc
+68
-0
paddle/operators/fetch_op.cu
paddle/operators/fetch_op.cu
+18
-0
paddle/operators/fetch_op.h
paddle/operators/fetch_op.h
+40
-0
未找到文件。
paddle/framework/executor.cc
浏览文件 @
45c4dcaa
...
...
@@ -75,15 +75,15 @@ void Executor::Run(const ProgramDesc& pdesc, Scope* scope) {
device_context
->
Wait
();
}
// // print tensor value
for
(
auto
&
var
:
block
.
vars
())
{
std
::
cout
<<
var
.
name
()
<<
std
::
endl
;
auto
v
=
scope
->
FindVar
(
var
.
name
());
const
LoDTensor
&
t
=
v
->
Get
<
LoDTensor
>
();
for
(
int
i
=
0
;
i
<
t
.
numel
();
++
i
)
{
std
::
cout
<<
t
.
data
<
float
>
()[
i
]
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
}
//
for (auto& var : block.vars()) {
//
std::cout << var.name() << std::endl;
//
auto v = scope->FindVar(var.name());
//
const LoDTensor& t = v->Get<LoDTensor>();
//
for (int i = 0; i < t.numel(); ++i) {
//
std::cout << t.data<float>()[i] << " ";
//
}
//
std::cout << std::endl;
//
}
}
}
// namespace framework
...
...
paddle/framework/executor_test.cc
浏览文件 @
45c4dcaa
...
...
@@ -25,6 +25,7 @@ limitations under the License. */
USE_OP
(
elementwise_add
);
USE_OP
(
gaussian_random
);
USE_OP
(
feed
);
USE_OP
(
fetch
);
using
std
::
string
;
using
namespace
paddle
::
platform
;
...
...
@@ -94,6 +95,41 @@ void add_feed_op(string var_name, int index, proto_block* block) {
Out
->
add_arguments
(
var_name
);
}
void
add_fetch_op
(
string
var_name
,
int
index
,
proto_block
*
block
)
{
std
::
vector
<
int
>
dim
{
3
};
// insert variable
auto
a
=
block
->
add_vars
();
a
->
set_name
(
var_name
);
auto
a_lt
=
a
->
mutable_lod_tensor
();
a_lt
->
set_data_type
(
paddle
::
framework
::
DataType
::
FP32
);
for
(
int
i
:
dim
)
{
a_lt
->
add_dims
(
i
);
}
// insert operation
auto
op
=
block
->
add_ops
();
op
->
set_type
(
"fetch"
);
// set dims attr
auto
dims
=
op
->
add_attrs
();
dims
->
set_name
(
"dims"
);
dims
->
set_type
(
paddle
::
framework
::
AttrType
::
INTS
);
for
(
int
i
:
dim
)
{
dims
->
add_ints
(
i
);
}
// set col attr
auto
col
=
op
->
add_attrs
();
col
->
set_name
(
"col"
);
col
->
set_type
(
paddle
::
framework
::
AttrType
::
INT
);
col
->
set_i
(
index
);
auto
Out
=
op
->
add_inputs
();
Out
->
set_parameter
(
"Input"
);
Out
->
add_arguments
(
var_name
);
}
std
::
once_flag
set_variable_flag
;
template
<
typename
T
>
...
...
@@ -119,6 +155,27 @@ void set_feed_variable(const std::vector<std::vector<T>>& inputs) {
}
}
template
<
typename
T
>
std
::
vector
<
std
::
vector
<
T
>>
get_fetch_variable
()
{
typedef
std
::
vector
<
paddle
::
framework
::
Tensor
>
FetchOutputs
;
Variable
*
g_fetch_value
=
GetScope
()
->
FindVar
(
"fetch_value"
);
FetchOutputs
&
fetch_outputs
=
*
(
g_fetch_value
->
GetMutable
<
FetchOutputs
>
());
auto
size
=
fetch_outputs
.
size
();
std
::
vector
<
std
::
vector
<
T
>>
result
;
result
.
reserve
(
size
);
for
(
size_t
i
=
0
;
i
<
size
;
i
++
)
{
std
::
vector
<
T
>
tmp
;
tmp
.
reserve
(
fetch_outputs
[
i
].
numel
());
memcpy
(
tmp
.
data
(),
fetch_outputs
[
i
].
data
<
T
>
(),
fetch_outputs
[
i
].
numel
()
*
sizeof
(
T
));
result
.
push_back
(
tmp
);
}
return
result
;
}
class
ExecutorTesterRandom
:
public
::
testing
::
Test
{
public:
virtual
void
SetUp
()
override
{
...
...
@@ -181,6 +238,8 @@ class ExecutorTesterFeed : public ::testing::Test {
Out
->
set_parameter
(
"Out"
);
Out
->
add_arguments
(
"c"
);
add_fetch_op
(
"c"
,
0
,
root_block
);
std
::
vector
<
float
>
vec1
=
{
1.0
,
2.0
,
3.0
};
std
::
vector
<
float
>
vec2
=
{
4.0
,
5.0
,
6.0
};
inputs_
.
push_back
(
vec1
);
...
...
@@ -213,8 +272,16 @@ TEST_F(ExecutorTesterFeed, CPU) {
// 3 mini-batch
for
(
int
i
=
0
;
i
<
3
;
i
++
)
{
// need to set feed variable before Executor::Run
std
::
cout
<<
"start mini-batch "
<<
i
<<
std
::
endl
;
set_feed_variable
<
float
>
(
inputs_
);
executor
->
Run
(
pdesc_
,
GetScope
());
std
::
vector
<
std
::
vector
<
float
>>
result
=
get_fetch_variable
<
float
>
();
for
(
auto
&
vec
:
result
)
{
for
(
auto
&
num
:
vec
)
{
std
::
cout
<<
num
<<
" "
;
}
std
::
cout
<<
std
::
endl
;
}
}
delete
executor
;
...
...
paddle/framework/scope.cc
浏览文件 @
45c4dcaa
...
...
@@ -74,7 +74,10 @@ std::unique_ptr<T> make_unique(Args&&... args) {
framework
::
Scope
*
GetScope
()
{
static
std
::
unique_ptr
<
framework
::
Scope
>
g_scope
=
make_unique
<
framework
::
Scope
>
();
std
::
call_once
(
feed_variable_flag
,
[
&
]()
{
g_scope
->
NewVar
(
"feed_value"
);
});
std
::
call_once
(
feed_variable_flag
,
[
&
]()
{
g_scope
->
NewVar
(
"feed_value"
);
g_scope
->
NewVar
(
"fetch_value"
);
});
return
g_scope
.
get
();
}
...
...
paddle/operators/activation_op.cu
浏览文件 @
45c4dcaa
/* 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
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
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. */
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/operators/activation_op.h"
...
...
paddle/operators/feed_op.cc
浏览文件 @
45c4dcaa
...
...
@@ -49,9 +49,9 @@ class FeedOpMaker : public framework::OpProtoAndCheckerMaker {
AddAttr
<
int
>
(
"data_type"
,
"output data type"
)
.
SetDefault
(
framework
::
DataType
::
FP32
);
AddAttr
<
int
>
(
"col"
,
"The col in global feed variable"
).
SetDefault
(
0
);
AddAttr
<
std
::
vector
<
int
>>
(
"dims"
,
"The dimension of
random
tensor."
);
AddOutput
(
"Out"
,
"The output of
dropout
op."
);
AddComment
(
R"DOC(Feed data
to
global feed variable)DOC"
);
AddAttr
<
std
::
vector
<
int
>>
(
"dims"
,
"The dimension of
feed
tensor."
);
AddOutput
(
"Out"
,
"The output of
feed
op."
);
AddComment
(
R"DOC(Feed data
from
global feed variable)DOC"
);
}
};
...
...
paddle/operators/fetch_op.cc
0 → 100644
浏览文件 @
45c4dcaa
/* 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. */
#include "paddle/operators/fetch_op.h"
namespace
paddle
{
namespace
operators
{
class
FetchOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContextBase
*
ctx
)
const
override
{
typedef
std
::
vector
<
framework
::
Tensor
>
FetchOutputs
;
PADDLE_ENFORCE
(
ctx
->
HasInput
(
"Input"
),
"Input should be not null."
);
int
col
=
ctx
->
Attrs
().
Get
<
int
>
(
"col"
);
framework
::
Variable
*
g_fetch_variable
=
framework
::
GetScope
()
->
FindVar
(
"fetch_value"
);
FetchOutputs
*
tensors
=
g_fetch_variable
->
GetMutable
<
FetchOutputs
>
();
if
(
tensors
->
size
()
<
col
)
{
tensors
->
resize
(
col
);
}
auto
input_dim
=
ctx
->
GetInputDim
(
"Input"
);
framework
::
Tensor
tmp
;
tmp
.
Resize
(
input_dim
);
(
*
tensors
)[
col
].
Resize
(
input_dim
);
// need to handle LodTensor later
}
framework
::
DataType
IndicateDataType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
return
static_cast
<
framework
::
DataType
>
(
Attr
<
int
>
(
"data_type"
));
}
};
class
FetchOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
FetchOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddAttr
<
int
>
(
"data_type"
,
"output data type"
)
.
SetDefault
(
framework
::
DataType
::
FP32
);
AddAttr
<
int
>
(
"col"
,
"The col in global fetch variable"
).
SetDefault
(
0
);
AddAttr
<
std
::
vector
<
int
>>
(
"dims"
,
"The dimension of fetch tensor."
);
AddInput
(
"Input"
,
"The output of fetch op."
);
AddComment
(
R"DOC(Fetch data to global fetch variable)DOC"
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_WITHOUT_GRADIENT
(
fetch
,
ops
::
FetchOp
,
ops
::
FetchOpMaker
);
REGISTER_OP_CPU_KERNEL
(
fetch
,
ops
::
FetchKernel
<
float
>
);
paddle/operators/fetch_op.cu
0 → 100644
浏览文件 @
45c4dcaa
/* 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. */
#include "paddle/operators/feed_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_GPU_KERNEL
(
fetch
,
ops
::
FetchKernel
<
float
>
);
paddle/operators/fetch_op.h
0 → 100644
浏览文件 @
45c4dcaa
/* 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
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
T
>
class
FetchKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
typedef
std
::
vector
<
framework
::
Tensor
>
FetchOutputs
;
Tensor
*
input
=
ctx
.
Output
<
Tensor
>
(
"Input"
);
int
col
=
ctx
.
template
Attr
<
int
>(
"col"
);
framework
::
Variable
*
g_fetch_variable
=
framework
::
GetScope
()
->
FindVar
(
"fetch_value"
);
FetchOutputs
tensors
=
g_fetch_variable
->
Get
<
FetchOutputs
>
();
tensors
[
col
].
mutable_data
<
T
>
(
platform
::
CPUPlace
());
tensors
[
col
].
CopyFrom
<
T
>
(
*
input
,
platform
::
CPUPlace
());
}
};
}
// namespace operators
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录