Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
e146fe83
P
Paddle
项目概览
Crayon鑫
/
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看板
提交
e146fe83
编写于
7月 03, 2017
作者:
F
fengjiayi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix compile errors and add assert test
上级
9f408dfb
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
76 addition
and
49 deletion
+76
-49
paddle/framework/tensor.h
paddle/framework/tensor.h
+19
-6
paddle/framework/tensor_test.cc
paddle/framework/tensor_test.cc
+57
-43
未找到文件。
paddle/framework/tensor.h
浏览文件 @
e146fe83
...
@@ -20,7 +20,6 @@ limitations under the License. */
...
@@ -20,7 +20,6 @@ limitations under the License. */
#include "paddle/framework/ddim.h"
#include "paddle/framework/ddim.h"
#include "paddle/framework/enforce.h"
#include "paddle/framework/enforce.h"
#include "paddle/memory/memory.h"
#include "paddle/memory/memory.h"
#include "paddle/platform/assert.h"
#include "paddle/platform/place.h"
#include "paddle/platform/place.h"
namespace
paddle
{
namespace
paddle
{
...
@@ -63,21 +62,35 @@ class Tensor {
...
@@ -63,21 +62,35 @@ class Tensor {
template
<
typename
T
>
template
<
typename
T
>
struct
PlaceholderImpl
:
public
Placeholder
{
struct
PlaceholderImpl
:
public
Placeholder
{
PlaceholderImpl
(
paddle
::
platform
::
Place
pl
,
size_t
size
)
private:
:
ptr_
(
paddle
::
memory
::
Alloc
(
pl
,
size
),
paddle
::
memory
::
Deleter
(
pl
)),
class
Deleter
{
place_
(
pl
),
public:
Deleter
(
platform
::
Place
place
)
:
place_
(
place
)
{}
void
operator
()(
T
*
ptr
)
{
paddle
::
memory
::
Free
(
place_
,
static_cast
<
void
*>
(
ptr
));
}
private:
paddle
::
platform
::
Place
place_
;
};
public:
PlaceholderImpl
(
paddle
::
platform
::
Place
place
,
size_t
size
)
:
ptr_
(
static_cast
<
T
*>
(
paddle
::
memory
::
Alloc
(
place
,
size
)),
Deleter
(
place
)),
place_
(
place
),
size_
(
size
)
{}
size_
(
size
)
{}
virtual
void
*
Ptr
()
const
{
return
static_cast
<
void
*>
(
ptr_
.
get
());
}
virtual
void
*
Ptr
()
const
{
return
static_cast
<
void
*>
(
ptr_
.
get
());
}
virtual
size_t
Size
()
const
{
return
size_
;
}
virtual
size_t
Size
()
const
{
return
size_
;
}
virtual
paddle
::
platform
::
Place
Place
()
const
{
return
place_
;
}
virtual
paddle
::
platform
::
Place
Place
()
const
{
return
place_
;
}
std
::
unique_ptr
<
T
,
memory
::
Deleter
>
ptr_
;
std
::
unique_ptr
<
T
,
Deleter
>
ptr_
;
paddle
::
platform
::
Place
place_
;
// record the place of ptr_.
paddle
::
platform
::
Place
place_
;
// record the place of ptr_.
size_t
size_
;
// size of the memory block.
size_t
size_
;
// size of the memory block.
};
};
std
::
unique
_ptr
<
Placeholder
>
holder_
;
// holds the memory block if allocated.
std
::
shared
_ptr
<
Placeholder
>
holder_
;
// holds the memory block if allocated.
};
};
}
// namespace framework
}
// namespace framework
...
...
paddle/framework/tensor_test.cc
浏览文件 @
e146fe83
...
@@ -13,12 +13,23 @@
...
@@ -13,12 +13,23 @@
#include "paddle/framework/tensor.h"
#include "paddle/framework/tensor.h"
#include <gtest/gtest.h>
#include <gtest/gtest.h>
#include <string>
TEST
(
Tensor
,
Data
)
{
TEST
(
Tensor
,
ASSERT
)
{
using
namespace
paddle
::
framework
;
paddle
::
framework
::
Tensor
cpu_tensor
;
using
namespace
paddle
::
platform
;
Tensor
cpu_tensor
;
bool
caught
=
false
;
try
{
const
double
*
p
__attribute__
((
unused
))
=
cpu_tensor
.
data
<
double
>
();
}
catch
(
paddle
::
framework
::
EnforceNotMet
err
)
{
caught
=
true
;
std
::
string
msg
=
"Tensor::data must be called after Tensor::mutable_data"
;
const
char
*
what
=
err
.
what
();
for
(
size_t
i
=
0
;
i
<
msg
.
length
();
++
i
)
{
ASSERT_EQ
(
what
[
i
],
msg
[
i
]);
}
}
ASSERT_TRUE
(
caught
);
}
}
/* mutable_data() is not tested at present
/* mutable_data() is not tested at present
...
@@ -27,45 +38,48 @@ TEST(Tensor, Data) {
...
@@ -27,45 +38,48 @@ TEST(Tensor, Data) {
TEST(Tensor, MutableData) {
TEST(Tensor, MutableData) {
using namespace paddle::framework;
using namespace paddle::framework;
using namespace paddle::platform;
using namespace paddle::platform;
{
Tensor cpu_tensor;
float* p1 = nullptr;
float* p2 = nullptr;
// initialization
p1 = cpu_tensor.mutable_data<float>(make_ddim({1, 2, 3}), CPUPlace());
EXPECT_NE(p1, nullptr);
// set cpu_tensor a new dim with large size
// momery is supposed to be re-allocated
p2 = cpu_tensor.mutable_data<float>(make_ddim({3, 4}));
EXPECT_NE(p2, nullptr);
EXPECT_NE(p1, p2);
// set cpu_tensor a new dim with same size
// momery block is supposed to be unchanged
p1 = cpu_tensor.mutable_data<float>(make_ddim({2, 2, 3}));
EXPECT_EQ(p1, p2);
// set cpu_tensor a new dim with smaller size
// momery block is supposed to be unchanged
p2 = cpu_tensor.mutable_data<float>(make_ddim({2, 2}));
EXPECT_EQ(p1, p2);
}
Tensor cpu_tensor;
{
float* p1 = nullptr;
Tensor gpu_tensor;
float* p2 = nullptr;
float* p1 = nullptr;
// initialization
float* p2 = nullptr;
p1 = cpu_tensor.mutable_data<float>(make_ddim({1, 2, 3}), CPUPlace());
// initialization
EXPECT_NE(p1, nullptr);
p1 = gpu_tensor.mutable_data<float>(make_ddim({1, 2, 3}), GPUPlace());
// set cpu_tensor a new dim with large size
EXPECT_NE(p1, nullptr);
// momery is supposed to be re-allocated
// set gpu_tensor a new dim with large size
p2 = cpu_tensor.mutable_data<float>(make_ddim({3, 4}));
// momery is supposed to be re-allocated
EXPECT_NE(p2, nullptr);
p2 = gpu_tensor.mutable_data<float>(make_ddim({3, 4}));
EXPECT_NE(p1, p2);
EXPECT_NE(p2, nullptr);
// set cpu_tensor a new dim with same size
EXPECT_NE(p1, p2);
// momery block is supposed to be unchanged
// set gpu_tensor a new dim with same size
p1 = cpu_tensor.mutable_data<float>(make_ddim({2, 2, 3}));
// momery block is supposed to be unchanged
EXPECT_EQ(p1, p2);
p1 = gpu_tensor.mutable_data<float>(make_ddim({2, 2, 3}));
// set cpu_tensor a new dim with smaller size
EXPECT_EQ(p1, p2);
// momery block is supposed to be unchanged
// set gpu_tensor a new dim with smaller size
p2 = cpu_tensor.mutable_data<float>(make_ddim({2, 2}));
// momery block is supposed to be unchanged
EXPECT_EQ(p1, p2);
p2 = gpu_tensor.mutable_data<float>(make_ddim({2, 2}));
EXPECT_EQ(p1, p2);
Tensor gpu_tensor;
}
float* p1 = nullptr;
float* p2 = nullptr;
// initialization
p1 = gpu_tensor.mutable_data<float>(make_ddim({1, 2, 3}), GPUPlace());
EXPECT_NE(p1, nullptr);
// set gpu_tensor a new dim with large size
// momery is supposed to be re-allocated
p2 = gpu_tensor.mutable_data<float>(make_ddim({3, 4}));
EXPECT_NE(p2, nullptr);
EXPECT_NE(p1, p2);
// set gpu_tensor a new dim with same size
// momery block is supposed to be unchanged
p1 = gpu_tensor.mutable_data<float>(make_ddim({2, 2, 3}));
EXPECT_EQ(p1, p2);
// set gpu_tensor a new dim with smaller size
// momery block is supposed to be unchanged
p2 = gpu_tensor.mutable_data<float>(make_ddim({2, 2}));
EXPECT_EQ(p1, p2);
}
}
*/
*/
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录