Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
b8ffa8b9
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,发现更多精彩内容 >>
提交
b8ffa8b9
编写于
6月 28, 2017
作者:
Q
qiaolongfei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
move code to scope.h, remove scope.cc
上级
3e16f5fd
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
49 addition
and
82 deletion
+49
-82
paddle/framework/CMakeLists.txt
paddle/framework/CMakeLists.txt
+1
-3
paddle/framework/scope.cc
paddle/framework/scope.cc
+0
-50
paddle/framework/scope.h
paddle/framework/scope.h
+41
-20
paddle/framework/scope_test.cc
paddle/framework/scope_test.cc
+7
-9
未找到文件。
paddle/framework/CMakeLists.txt
浏览文件 @
b8ffa8b9
...
...
@@ -6,6 +6,4 @@ nv_test(dim_test SRCS dim_test.cu DEPS ddim)
cc_test
(
variable_test SRCS variable_test.cc
)
# scope lib
cc_library
(
scope SRCS scope.cc
)
cc_test
(
scope_test SRCS scope_test.cc DEPS scope
)
cc_test
(
scope_test SRCS scope_test.cc
)
paddle/framework/scope.cc
已删除
100644 → 0
浏览文件 @
3e16f5fd
/* 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/framework/scope.h"
namespace
paddle
{
namespace
framework
{
Variable
*
Scope
::
CreateVariable
(
const
std
::
string
&
name
)
{
if
(
!
HasVariable
(
name
))
{
vars_
[
name
]
=
std
::
unique_ptr
<
Variable
>
(
new
Variable
());
}
return
GetVariable
(
name
);
}
Variable
*
Scope
::
GetVarLocally
(
const
std
::
string
&
name
)
const
{
if
(
vars_
.
count
(
name
))
{
return
vars_
.
at
(
name
).
get
();
}
return
nullptr
;
}
Variable
*
Scope
::
GetVariable
(
const
std
::
string
&
name
)
const
{
Variable
*
var
=
GetVarLocally
(
name
);
if
(
var
!=
nullptr
)
{
return
var
;
}
else
if
(
parent_
!=
nullptr
)
{
return
parent_
->
GetVariable
(
name
);
}
else
{
return
nullptr
;
}
}
bool
Scope
::
HasVariable
(
const
std
::
string
&
name
)
{
return
(
vars_
.
count
(
name
)
>
0
||
(
parent_
&&
parent_
->
HasVariable
(
name
)));
}
}
// namespace framework
}
// namespace paddle
paddle/framework/scope.h
浏览文件 @
b8ffa8b9
...
...
@@ -19,37 +19,58 @@ limitations under the License. */
#include <vector>
#include "paddle/framework/variable.h"
#include "paddle/platform/assert.h"
namespace
paddle
{
namespace
framework
{
/**
* Scope is an association of a name to Variable. All variables belong to
*
`Scope`
. You need to specify a scope to run a Net, i.e., `net.Run(&scope)`.
*
Scope
. You need to specify a scope to run a Net, i.e., `net.Run(&scope)`.
* One net can run in different scopes and update different variable in the
* scope.
*/
class
Scope
{
public:
Scope
()
{}
explicit
Scope
(
const
std
::
shared_ptr
<
Scope
>&
scope
)
:
parent_
(
scope
)
{}
~
Scope
()
{}
// Create Variable in this Scope. Return error if Variable already been
// created.
Variable
*
CreateVariable
(
const
std
::
string
&
name
);
// Get Variable from this Scope, this function will recursive find Variable
// from it's parent scope. Return nullptr if not found.
Variable
*
GetVariable
(
const
std
::
string
&
name
)
const
;
// Find and return Variables in the scope it self.
Variable
*
GetVarLocally
(
const
std
::
string
&
name
)
const
;
// Find if there is a Variable in this scope and it's parent scope
bool
HasVariable
(
const
std
::
string
&
name
);
explicit
Scope
(
const
std
::
shared_ptr
<
Scope
>&
parent
=
nullptr
)
:
parent_
(
parent
)
{}
/// Create Variable in this Scope. Failed if Variable already been
/// created.
Variable
*
CreateVariable
(
const
std
::
string
&
name
)
{
PADDLE_ASSERT
(
!
HasVariable
(
name
));
vars_
[
name
]
=
std
::
unique_ptr
<
Variable
>
(
new
Variable
());
return
GetVariable
(
name
);
}
/// Get Variable from this Scope, this function will recursive find Variable
/// from it's parent scope. Return nullptr if not found.
Variable
*
GetVariable
(
const
std
::
string
&
name
)
const
{
auto
it
=
vars_
.
find
(
name
);
if
(
it
!=
vars_
.
end
())
{
return
it
->
second
.
get
();
}
else
if
(
parent_
!=
nullptr
)
{
return
parent_
->
GetVariable
(
name
);
}
else
{
return
nullptr
;
}
}
/// Get Variable from scope, if Variable is not exist, creat one and return.
Variable
*
GetOrCreateVariable
(
const
std
::
string
&
name
)
{
auto
var
=
GetVariable
(
name
);
if
(
var
)
{
return
var
;
}
else
{
return
CreateVariable
(
name
);
}
}
/// Find if there is a Variable in this scope and it's parent scope
bool
HasVariable
(
const
std
::
string
&
name
)
const
{
return
(
vars_
.
find
(
name
)
!=
vars_
.
end
()
||
(
parent_
&&
parent_
->
HasVariable
(
name
)));
}
private:
std
::
unordered_map
<
std
::
string
,
std
::
unique_ptr
<
Variable
>>
vars_
;
...
...
paddle/framework/scope_test.cc
浏览文件 @
b8ffa8b9
...
...
@@ -28,12 +28,13 @@ TEST(Scope, Create) {
EXPECT_EQ
(
var1
,
nullptr
);
Variable
*
var2
=
scope
->
CreateVariable
(
"a"
);
EXPECT_NE
(
var2
,
nullptr
);
Variable
*
var3
=
scope
->
CreateVariable
(
"a"
);
ASSERT_DEATH
({
scope
->
CreateVariable
(
"a"
);
},
""
);
Variable
*
var3
=
scope
->
GetVariable
(
"a"
);
EXPECT_EQ
(
var2
,
var3
);
Variable
*
var4
=
scope
->
GetVariable
(
"a"
);
Variable
*
var4
=
scope
->
Get
OrCreate
Variable
(
"a"
);
EXPECT_EQ
(
var2
,
var4
);
}
...
...
@@ -47,9 +48,6 @@ TEST(Scope, Parent) {
Variable
*
var0
=
parent_scope_ptr
->
CreateVariable
(
"a"
);
EXPECT_NE
(
var0
,
nullptr
);
Variable
*
var1
=
scope
->
GetVarLocally
(
"a"
);
EXPECT_EQ
(
var1
,
nullptr
);
Variable
*
var2
=
scope
->
GetVariable
(
"a"
);
EXPECT_EQ
(
var2
,
var0
);
Variable
*
var1
=
scope
->
GetVariable
(
"a"
);
EXPECT_EQ
(
var0
,
var1
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录