Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
f5dca05c
P
PaddleDetection
项目概览
s920243400
/
PaddleDetection
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleDetection
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
f5dca05c
编写于
11月 10, 2017
作者:
武
武毅
提交者:
GitHub
11月 10, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #5519 from QingshuChen/develop
add API for copying data from/to paddle matrix
上级
2b201889
23efaa74
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
122 addition
and
9 deletion
+122
-9
paddle/capi/Matrix.cpp
paddle/capi/Matrix.cpp
+40
-0
paddle/capi/examples/model_inference/dense/main.c
paddle/capi/examples/model_inference/dense/main.c
+17
-9
paddle/capi/matrix.h
paddle/capi/matrix.h
+19
-0
paddle/capi/tests/test_Matrix.cpp
paddle/capi/tests/test_Matrix.cpp
+46
-0
未找到文件。
paddle/capi/Matrix.cpp
浏览文件 @
f5dca05c
...
...
@@ -54,6 +54,46 @@ paddle_error paddle_matrix_set_row(paddle_matrix mat,
return
kPD_NO_ERROR
;
}
PD_API
paddle_error
paddle_matrix_set_value
(
paddle_matrix
mat
,
paddle_real
*
value
)
{
if
(
mat
==
nullptr
||
value
==
nullptr
)
return
kPD_NULLPTR
;
auto
ptr
=
cast
(
mat
);
if
(
ptr
->
mat
==
nullptr
)
return
kPD_NULLPTR
;
paddle
::
real
*
buf
=
ptr
->
mat
->
getRowBuf
(
0
);
size_t
width
=
ptr
->
mat
->
getWidth
();
size_t
height
=
ptr
->
mat
->
getHeight
();
if
(
ptr
->
mat
->
useGpu
())
{
#ifdef PADDLE_WITH_CUDA
hl_memcpy
(
buf
,
value
,
sizeof
(
paddle
::
real
)
*
width
*
height
);
#else
return
kPD_NOT_SUPPORTED
;
#endif
}
else
{
std
::
copy
(
value
,
value
+
width
*
height
,
buf
);
}
return
kPD_NO_ERROR
;
}
PD_API
paddle_error
paddle_matrix_get_value
(
paddle_matrix
mat
,
paddle_real
*
result
)
{
if
(
mat
==
nullptr
||
result
==
nullptr
)
return
kPD_NULLPTR
;
auto
ptr
=
cast
(
mat
);
if
(
ptr
->
mat
==
nullptr
)
return
kPD_NULLPTR
;
paddle
::
real
*
buf
=
ptr
->
mat
->
getRowBuf
(
0
);
size_t
width
=
ptr
->
mat
->
getWidth
();
size_t
height
=
ptr
->
mat
->
getHeight
();
if
(
ptr
->
mat
->
useGpu
())
{
#ifdef PADDLE_WITH_CUDA
hl_memcpy
(
result
,
buf
,
width
*
height
*
sizeof
(
paddle
::
real
));
#else
return
kPD_NOT_SUPPORTED
;
#endif
}
else
{
std
::
copy
(
buf
,
buf
+
width
*
height
,
result
);
}
return
kPD_NO_ERROR
;
}
paddle_error
paddle_matrix_get_row
(
paddle_matrix
mat
,
uint64_t
rowID
,
paddle_real
**
rawRowBuffer
)
{
...
...
paddle/capi/examples/model_inference/dense/main.c
浏览文件 @
f5dca05c
...
...
@@ -27,18 +27,20 @@ int main() {
CHECK
(
paddle_arguments_resize
(
in_args
,
1
));
// Create input matrix.
paddle_matrix
mat
=
paddle_matrix_create
(
/* sample_num */
1
,
paddle_matrix
mat
=
paddle_matrix_create
(
/* sample_num */
1
0
,
/* size */
784
,
/* useGPU */
false
);
srand
(
time
(
0
));
paddle_real
*
array
;
// Get First row.
CHECK
(
paddle_matrix_get_row
(
mat
,
0
,
&
array
)
);
std
::
vector
<
paddle_real
>
input
;
input
.
resize
(
784
*
10
);
for
(
int
i
=
0
;
i
<
784
;
++
i
)
{
array
[
i
]
=
rand
()
/
((
float
)
RAND_MAX
);
for
(
int
i
=
0
;
i
<
input
.
size
()
;
++
i
)
{
input
[
i
]
=
rand
()
/
((
float
)
RAND_MAX
);
}
// Set value for the input matrix
CHECK
(
paddle_matrix_set_value
(
mat
,
input
.
data
()));
CHECK
(
paddle_arguments_set_value
(
in_args
,
0
,
mat
));
...
...
@@ -51,11 +53,17 @@ int main() {
CHECK
(
paddle_arguments_get_value
(
out_args
,
0
,
prob
));
CHECK
(
paddle_matrix_get_row
(
prob
,
0
,
&
array
));
std
::
std
::
vector
<
paddle_real
>
result
;
int
height
;
int
width
;
CHECK
(
paddle_matrix_get_shape
(
prob
,
&
height
,
&
width
);
result
.
resize
(
height
*
width
);
CHECK
(
paddle_matrix_get_value
(
prob
,
result
.
data
()));
printf
(
"Prob: "
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
printf
(
"%.2f "
,
array
[
i
]);
for
(
int
i
=
0
;
i
<
height
*
width
;
++
i
)
{
printf
(
"%.2f "
,
result
[
i
]);
}
printf
(
"
\n
"
);
...
...
paddle/capi/matrix.h
浏览文件 @
f5dca05c
...
...
@@ -70,6 +70,16 @@ PD_API paddle_error paddle_matrix_set_row(paddle_matrix mat,
uint64_t
rowID
,
paddle_real
*
rowArray
);
/**
* @brief paddle_matrix_set_value Set value to matrix.
* @param mat Target Matrix
* @param value Row data.
* @return paddle_error
* @note value should contain enough element of data to init the mat
*/
PD_API
paddle_error
paddle_matrix_set_value
(
paddle_matrix
mat
,
paddle_real
*
value
);
/**
* @brief PDMatGetRow Get raw row buffer from matrix
* @param [in] mat Target matrix
...
...
@@ -81,6 +91,15 @@ PD_API paddle_error paddle_matrix_get_row(paddle_matrix mat,
uint64_t
rowID
,
paddle_real
**
rawRowBuffer
);
/**
* @brief copy data from the matrix
* @param [in] mat Target matrix
* @param [out] result pointer to store the matrix data
* @return paddle_error
* @note the space of the result should allocated before invoke this API
*/
PD_API
paddle_error
paddle_matrix_get_value
(
paddle_matrix
mat
,
paddle_real
*
result
);
/**
* @brief PDMatCreateNone Create None Matrix
* @return
...
...
paddle/capi/tests/test_Matrix.cpp
浏览文件 @
f5dca05c
...
...
@@ -45,3 +45,49 @@ TEST(CAPIMatrix, createNone) {
paddle_matrix
mat
=
paddle_matrix_create_none
();
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_matrix_destroy
(
mat
));
}
TEST
(
CAPIMatrix
,
cpu_get_set_value
)
{
paddle_matrix
mat
=
paddle_matrix_create
(
128
,
32
,
false
);
std
::
vector
<
paddle_real
>
sample
;
std
::
vector
<
paddle_real
>
result
;
sample
.
resize
(
128
*
32
);
result
.
resize
(
128
*
32
);
for
(
size_t
i
=
0
;
i
<
sample
.
size
();
++
i
)
{
sample
[
i
]
=
1.0
/
(
i
+
1.0
);
}
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_matrix_set_value
(
mat
,
sample
.
data
()));
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_matrix_get_value
(
mat
,
result
.
data
()));
for
(
size_t
i
=
0
;
i
<
sample
.
size
();
++
i
)
{
ASSERT_NEAR
(
sample
[
i
],
result
[
i
],
1e-5
);
}
uint64_t
height
,
width
;
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_matrix_get_shape
(
mat
,
&
height
,
&
width
));
ASSERT_EQ
(
128UL
,
height
);
ASSERT_EQ
(
32UL
,
width
);
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_matrix_destroy
(
mat
));
}
#ifdef PADDLE_WITH_CUDA
TEST
(
CAPIMatrix
,
gpu_get_set_value
)
{
paddle_matrix
mat
=
paddle_matrix_create
(
128
,
32
,
true
);
std
::
vector
<
paddle_real
>
sample
;
std
::
vector
<
paddle_real
>
result
;
sample
.
resize
(
128
*
32
);
result
.
resize
(
128
*
32
);
for
(
size_t
i
=
0
;
i
<
sample
.
size
();
++
i
)
{
sample
[
i
]
=
1.0
/
(
i
+
1.0
);
}
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_matrix_set_value
(
mat
,
sample
.
data
()));
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_matrix_get_value
(
mat
,
result
.
data
()));
for
(
size_t
i
=
0
;
i
<
sample
.
size
();
++
i
)
{
ASSERT_NEAR
(
sample
[
i
],
result
[
i
],
1e-5
);
}
uint64_t
height
,
width
;
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_matrix_get_shape
(
mat
,
&
height
,
&
width
));
ASSERT_EQ
(
128UL
,
height
);
ASSERT_EQ
(
32UL
,
width
);
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_matrix_destroy
(
mat
));
}
#endif
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录