Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleDetection
提交
6ad1d21c
P
PaddleDetection
项目概览
PaddlePaddle
/
PaddleDetection
大约 1 年 前同步成功
通知
694
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6ad1d21c
编写于
6月 28, 2017
作者:
Q
qiaolongfei
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine code of operator
上级
7d138593
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
65 addition
and
52 deletion
+65
-52
paddle/framework/scope.cc
paddle/framework/scope.cc
+20
-25
paddle/framework/scope.h
paddle/framework/scope.h
+20
-10
paddle/framework/scope_test.cc
paddle/framework/scope_test.cc
+25
-17
未找到文件。
paddle/framework/scope.cc
浏览文件 @
6ad1d21c
/* 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"
#include "paddle/framework/scope.h"
namespace
paddle
{
namespace
paddle
{
namespace
framework
{
namespace
framework
{
Error
Scope
::
CreateVariable
(
const
std
::
string
&
name
)
{
Variable
*
Scope
::
CreateVariable
(
const
std
::
string
&
name
)
{
if
(
name
==
""
)
{
if
(
!
HasVariable
(
name
)
)
{
return
Error
(
"Variable name should not be empty"
);
vars_
[
name
]
=
std
::
unique_ptr
<
Variable
>
(
new
Variable
()
);
}
}
return
GetVariable
(
name
);
if
(
HaveVariable
(
name
))
{
return
AlreadyCreated
;
}
vars_
[
name
]
=
std
::
unique_ptr
<
Variable
>
(
new
Variable
());
return
Error
();
}
}
Variable
*
Scope
::
GetVarLocally
(
const
std
::
string
&
name
)
const
{
Variable
*
Scope
::
GetVarLocally
(
const
std
::
string
&
name
)
const
{
...
@@ -33,22 +42,8 @@ Variable* Scope::GetVariable(const std::string& name) const {
...
@@ -33,22 +42,8 @@ Variable* Scope::GetVariable(const std::string& name) const {
}
}
}
}
Variable
*
Scope
::
GetOrCreateVariable
(
const
std
::
string
&
name
)
{
bool
Scope
::
HasVariable
(
const
std
::
string
&
name
)
{
Variable
*
var
=
GetVariable
(
name
);
return
(
vars_
.
count
(
name
)
>
0
||
(
parent_
&&
parent_
->
HasVariable
(
name
)));
if
(
var
!=
nullptr
)
{
return
var
;
}
Error
err
=
CreateVariable
(
name
);
if
(
!
err
.
isOK
())
{
return
nullptr
;
}
else
{
return
GetVariable
(
name
);
}
}
bool
Scope
::
HaveVariable
(
const
std
::
string
&
name
)
{
return
vars_
.
count
(
name
)
!=
0
;
}
}
}
// namespace framework
}
// namespace framework
...
...
paddle/framework/scope.h
浏览文件 @
6ad1d21c
/* 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
#pragma once
#include <unordered_map>
#include <unordered_map>
#include <vector>
#include <vector>
#include <string>
#include "paddle/framework/variable.h"
#include "paddle/framework/variable.h"
#include "paddle/utils/Error.h"
namespace
paddle
{
namespace
paddle
{
namespace
framework
{
namespace
framework
{
const
static
Error
AlreadyCreated
(
"Variable has already been created"
);
/**
/**
* Scope is an association of a name to Variable. All variables belong to
* 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)`.
...
@@ -26,20 +39,17 @@ class Scope {
...
@@ -26,20 +39,17 @@ class Scope {
// Create Variable in this Scope. Return error if Variable already been
// Create Variable in this Scope. Return error if Variable already been
// created.
// created.
Error
__must_check
CreateVariable
(
const
std
::
string
&
name
);
Variable
*
CreateVariable
(
const
std
::
string
&
name
);
// Get Variable from this Scope, this function will recursive find Variable
// Get Variable from this Scope, this function will recursive find Variable
// from it's parent scope. Return nullptr if not found.
// from it's parent scope. Return nullptr if not found.
Variable
*
GetVariable
(
const
std
::
string
&
name
)
const
;
Variable
*
GetVariable
(
const
std
::
string
&
name
)
const
;
//
f
ind and return Variables in the scope it self.
//
F
ind and return Variables in the scope it self.
Variable
*
GetVarLocally
(
const
std
::
string
&
name
)
const
;
Variable
*
GetVarLocally
(
const
std
::
string
&
name
)
const
;
// Get a Variable from Scope, if the Variable is not exist then create it.
// Find if there is a Variable in this scope and it's parent scope
// User should call this function most of time.
bool
HasVariable
(
const
std
::
string
&
name
);
Variable
*
GetOrCreateVariable
(
const
std
::
string
&
name
);
bool
HaveVariable
(
const
std
::
string
&
name
);
private:
private:
std
::
unordered_map
<
std
::
string
,
std
::
unique_ptr
<
Variable
>>
vars_
;
std
::
unordered_map
<
std
::
string
,
std
::
unique_ptr
<
Variable
>>
vars_
;
...
...
paddle/framework/scope_test.cc
浏览文件 @
6ad1d21c
/* 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"
#include "paddle/framework/scope.h"
#include "gtest/gtest.h"
#include "gtest/gtest.h"
TEST
(
Scope
,
Create
)
{
TEST
(
Scope
,
Create
)
{
using
paddle
::
framework
::
Scope
;
using
paddle
::
framework
::
Scope
;
using
paddle
::
Error
;
using
paddle
::
framework
::
Variable
;
using
paddle
::
framework
::
Variable
;
using
paddle
::
framework
::
AlreadyCreated
;
Scope
*
scope
=
new
Scope
();
Scope
*
scope
=
new
Scope
();
Error
err
=
scope
->
CreateVariable
(
""
);
Variable
*
var0
=
scope
->
CreateVariable
(
""
);
EXPECT_
FALSE
(
err
.
isOK
()
);
EXPECT_
NE
(
var0
,
nullptr
);
Variable
*
var1
=
scope
->
GetVariable
(
"a"
);
Variable
*
var1
=
scope
->
GetVariable
(
"a"
);
EXPECT_EQ
(
var1
,
nullptr
);
EXPECT_EQ
(
var1
,
nullptr
);
Error
err1
=
scope
->
CreateVariable
(
"a"
);
Variable
*
var2
=
scope
->
CreateVariable
(
"a"
);
EXPECT_TRUE
(
err1
.
isOK
());
Error
err2
=
scope
->
CreateVariable
(
"a"
);
EXPECT_EQ
(
err2
,
AlreadyCreated
);
Variable
*
var2
=
scope
->
GetVariable
(
"a"
);
EXPECT_NE
(
var2
,
nullptr
);
EXPECT_NE
(
var2
,
nullptr
);
Variable
*
var3
=
scope
->
GetOrCreateVariable
(
"b"
);
Variable
*
var3
=
scope
->
CreateVariable
(
"a"
);
EXPECT_NE
(
var3
,
nullptr
);
EXPECT_EQ
(
var2
,
var3
);
Variable
*
var4
=
scope
->
GetVariable
(
"a"
);
EXPECT_EQ
(
var2
,
var4
);
}
}
TEST
(
Scope
,
Parent
)
{
TEST
(
Scope
,
Parent
)
{
using
paddle
::
framework
::
Scope
;
using
paddle
::
framework
::
Scope
;
using
paddle
::
framework
::
Variable
;
using
paddle
::
framework
::
Variable
;
using
paddle
::
Error
;
const
auto
parent_scope_ptr
=
std
::
shared_ptr
<
Scope
>
(
new
Scope
());
const
auto
parent_scope_ptr
=
std
::
shared_ptr
<
Scope
>
(
new
Scope
());
Scope
*
scope
=
new
Scope
(
parent_scope_ptr
);
Scope
*
scope
=
new
Scope
(
parent_scope_ptr
);
Error
err
=
parent_scope_ptr
->
CreateVariable
(
"a"
);
Variable
*
var0
=
parent_scope_ptr
->
CreateVariable
(
"a"
);
EXPECT_
TRUE
(
err
.
isOK
()
);
EXPECT_
NE
(
var0
,
nullptr
);
Variable
*
var1
=
scope
->
GetVarLocally
(
"a"
);
Variable
*
var1
=
scope
->
GetVarLocally
(
"a"
);
EXPECT_EQ
(
var1
,
nullptr
);
EXPECT_EQ
(
var1
,
nullptr
);
Variable
*
var2
=
scope
->
GetVariable
(
"a"
);
Variable
*
var2
=
scope
->
GetVariable
(
"a"
);
EXPECT_
NE
(
var2
,
nullptr
);
EXPECT_
EQ
(
var2
,
var0
);
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录