Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
470bbcf9
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看板
提交
470bbcf9
编写于
3月 24, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add example
上级
9c1c19b6
变更
25
隐藏空白更改
内联
并排
Showing
25 changed file
with
550 addition
and
19 deletion
+550
-19
paddle/capi/Matrix.cpp
paddle/capi/Matrix.cpp
+45
-2
paddle/capi/config.h.in
paddle/capi/config.h.in
+1
-1
paddle/capi/error.h
paddle/capi/error.h
+1
-0
paddle/capi/examples/.gitignore
paddle/capi/examples/.gitignore
+2
-0
paddle/capi/examples/common/common.h
paddle/capi/examples/common/common.h
+26
-0
paddle/capi/examples/dense/CMakeLists.txt
paddle/capi/examples/dense/CMakeLists.txt
+6
-0
paddle/capi/examples/dense/convert_protobin.sh
paddle/capi/examples/dense/convert_protobin.sh
+2
-0
paddle/capi/examples/dense/main.c
paddle/capi/examples/dense/main.c
+63
-0
paddle/capi/examples/dense/trainer_config.py
paddle/capi/examples/dense/trainer_config.py
+18
-0
paddle/capi/examples/multi_thread/.gitignore
paddle/capi/examples/multi_thread/.gitignore
+73
-0
paddle/capi/examples/multi_thread/CMakeLists.txt
paddle/capi/examples/multi_thread/CMakeLists.txt
+8
-0
paddle/capi/examples/multi_thread/convert_protobin.sh
paddle/capi/examples/multi_thread/convert_protobin.sh
+1
-0
paddle/capi/examples/multi_thread/main.c
paddle/capi/examples/multi_thread/main.c
+96
-0
paddle/capi/examples/multi_thread/trainer_config.py
paddle/capi/examples/multi_thread/trainer_config.py
+1
-0
paddle/capi/examples/sparse_binary/.gitignore
paddle/capi/examples/sparse_binary/.gitignore
+73
-0
paddle/capi/examples/sparse_binary/CMakeLists.txt
paddle/capi/examples/sparse_binary/CMakeLists.txt
+7
-0
paddle/capi/examples/sparse_binary/convert_protobin.sh
paddle/capi/examples/sparse_binary/convert_protobin.sh
+1
-0
paddle/capi/examples/sparse_binary/main.c
paddle/capi/examples/sparse_binary/main.c
+64
-0
paddle/capi/examples/sparse_binary/trainer_config.py
paddle/capi/examples/sparse_binary/trainer_config.py
+1
-0
paddle/capi/gradient_machine.cpp
paddle/capi/gradient_machine.cpp
+8
-0
paddle/capi/gradient_machine.h
paddle/capi/gradient_machine.h
+3
-0
paddle/capi/matrix.h
paddle/capi/matrix.h
+36
-2
paddle/capi/tests/test_Arguments.cpp
paddle/capi/tests/test_Arguments.cpp
+6
-6
paddle/capi/tests/test_GradientMachine.cpp
paddle/capi/tests/test_GradientMachine.cpp
+6
-6
paddle/capi/tests/test_Matrix.cpp
paddle/capi/tests/test_Matrix.cpp
+2
-2
未找到文件。
paddle/capi/Matrix.cpp
浏览文件 @
470bbcf9
...
...
@@ -39,7 +39,7 @@ paddle_error paddle_matrix_destroy(paddle_matrix mat) {
paddle_error
paddle_matrix_set_row
(
paddle_matrix
mat
,
uint64_t
rowID
,
p
d
_real
*
rowArray
)
{
p
addle
_real
*
rowArray
)
{
if
(
mat
==
nullptr
)
return
kPD_NULLPTR
;
auto
ptr
=
cast
(
mat
);
if
(
ptr
->
mat
==
nullptr
)
return
kPD_NULLPTR
;
...
...
@@ -56,7 +56,7 @@ paddle_error paddle_matrix_set_row(paddle_matrix mat,
paddle_error
paddle_matrix_get_row
(
paddle_matrix
mat
,
uint64_t
rowID
,
p
d
_real
**
rawRowBuffer
)
{
p
addle
_real
**
rawRowBuffer
)
{
if
(
mat
==
nullptr
)
return
kPD_NULLPTR
;
auto
ptr
=
cast
(
mat
);
if
(
ptr
->
mat
==
nullptr
)
return
kPD_NULLPTR
;
...
...
@@ -78,3 +78,46 @@ paddle_error paddle_matrix_get_shape(paddle_matrix mat,
return
kPD_NO_ERROR
;
}
}
paddle_matrix
paddle_matrix_create_sparse
(
uint64_t
height
,
uint64_t
width
,
uint64_t
nnz
,
bool
isBinary
,
bool
useGpu
)
{
auto
ptr
=
new
paddle
::
capi
::
CMatrix
();
ptr
->
mat
=
paddle
::
Matrix
::
createSparseMatrix
(
height
,
width
,
nnz
,
isBinary
?
paddle
::
NO_VALUE
:
paddle
::
FLOAT_VALUE
,
paddle
::
SPARSE_CSR
,
false
,
useGpu
);
return
ptr
;
}
paddle_error
paddle_matrix_sparse_copy_from
(
paddle_matrix
mat
,
int
*
rowArray
,
uint64_t
rowSize
,
int
*
colArray
,
uint64_t
colSize
,
float
*
valueArray
,
uint64_t
valueSize
)
{
if
(
mat
==
nullptr
)
return
kPD_NULLPTR
;
auto
ptr
=
cast
(
mat
);
if
(
rowArray
==
nullptr
||
colArray
==
nullptr
||
(
valueSize
!=
0
&&
valueArray
==
nullptr
)
||
ptr
->
mat
==
nullptr
)
{
return
kPD_NULLPTR
;
}
if
(
auto
sparseMat
=
dynamic_cast
<
paddle
::
CpuSparseMatrix
*>
(
ptr
->
mat
.
get
()))
{
std
::
vector
<
int
>
row
(
rowSize
);
row
.
assign
(
rowArray
,
rowArray
+
rowSize
);
std
::
vector
<
int
>
col
(
colSize
);
col
.
assign
(
colArray
,
colArray
+
colSize
);
std
::
vector
<
paddle_real
>
val
(
valueSize
);
if
(
valueSize
)
{
val
.
assign
(
valueArray
,
valueArray
+
valueSize
);
}
sparseMat
->
copyFrom
(
row
,
col
,
val
);
return
kPD_NO_ERROR
;
}
else
{
return
kPD_NOT_SUPPORTED
;
}
}
paddle/capi/config.h.in
浏览文件 @
470bbcf9
#ifndef __PADDLE_PADDLE_CAPI_CONFIG_H_INCLUDED__
#define __PADDLE_PADDLE_CAPI_CONFIG_H_INCLUDED__
typedef @PADDLE_FLOAT_TYPE@ p
d
_real;
typedef @PADDLE_FLOAT_TYPE@ p
addle
_real;
// Since we only support linux and macos in compile, always use clang or
// gcc 4.8+. DLL_IMPORT/DLL_EXPORT is as simple as below.
...
...
paddle/capi/error.h
浏览文件 @
470bbcf9
...
...
@@ -23,6 +23,7 @@ typedef enum {
kPD_NULLPTR
=
1
,
kPD_OUT_OF_RANGE
=
2
,
kPD_PROTOBUF_ERROR
=
3
,
kPD_NOT_SUPPORTED
=
4
,
kPD_UNDEFINED_ERROR
=
-
1
,
}
paddle_error
;
...
...
paddle/capi/examples/.gitignore
0 → 100644
浏览文件 @
470bbcf9
*.bin
build-*
paddle/capi/examples/common/common.h
0 → 100644
浏览文件 @
470bbcf9
#ifndef __CAPI_EXAMPLE_COMMON_H__
#define __CAPI_EXAMPLE_COMMON_H__
#include <stdio.h>
#include <stdlib.h>
#define CHECK(stmt) \
do { \
paddle_error __err__ = stmt; \
if (__err__ != kPD_NO_ERROR) { \
fprintf(stderr, "Invoke paddle error %d \n" #stmt, __err__); \
exit(__err__); \
} \
} while (0)
void
*
read_config
(
const
char
*
filename
,
long
*
size
)
{
FILE
*
file
=
fopen
(
filename
,
"r"
);
if
(
file
==
NULL
)
return
NULL
;
fseek
(
file
,
0L
,
SEEK_END
);
*
size
=
ftell
(
file
);
fseek
(
file
,
0L
,
SEEK_SET
);
void
*
buf
=
malloc
(
*
size
);
fread
(
buf
,
1
,
*
size
,
file
);
fclose
(
file
);
return
buf
;
}
#endif
paddle/capi/examples/dense/CMakeLists.txt
0 → 100644
浏览文件 @
470bbcf9
project
(
dense
)
cmake_minimum_required
(
VERSION 2.8
)
aux_source_directory
(
. SRC_LIST
)
add_executable
(
${
PROJECT_NAME
}
${
SRC_LIST
}
)
set_property
(
TARGET
${
PROJECT_NAME
}
PROPERTY C_STANDARD 99
)
target_link_libraries
(
${
PROJECT_NAME
}
-lpaddle_capi_shared
)
paddle/capi/examples/dense/convert_protobin.sh
0 → 100755
浏览文件 @
470bbcf9
#!/bin/bash
python
-m
paddle.utils.dump_config trainer_config.py
''
--binary
>
trainer_config.bin
paddle/capi/examples/dense/main.c
0 → 100644
浏览文件 @
470bbcf9
#include <paddle/capi.h>
#include <time.h>
#include "../common/common.h"
#define CONFIG_BIN "./trainer_config.bin"
int
main
()
{
// Initalize Paddle
char
*
argv
[]
=
{
"--use_gpu=False"
};
CHECK
(
paddle_init
(
1
,
(
char
**
)
argv
));
// Reading config binary file. It is generated by `convert_protobin.sh`
long
size
;
void
*
buf
=
read_config
(
CONFIG_BIN
,
&
size
);
// Create a gradient machine for inference.
paddle_gradient_machine
machine
;
CHECK
(
paddle_gradient_machine_create_for_inference
(
&
machine
,
buf
,
(
int
)
size
));
CHECK
(
paddle_gradient_machine_randomize_param
(
machine
));
// Loading parameter. Uncomment the following line and change the directory.
// CHECK(paddle_gradient_machine_load_parameter_from_disk(machine,
// "./some_where_to_params"));
paddle_arguments
in_args
=
paddle_arguments_create_none
();
// There is only one input of this network.
CHECK
(
paddle_arguments_resize
(
in_args
,
1
));
// Create input matrix.
paddle_matrix
mat
=
paddle_matrix_create
(
/* sample_num */
1
,
/* size */
784
,
/* useGPU */
false
);
srand
(
time
(
0
));
paddle_real
*
array
;
// Get First row.
CHECK
(
paddle_matrix_get_row
(
mat
,
0
,
&
array
));
for
(
int
i
=
0
;
i
<
784
;
++
i
)
{
array
[
i
]
=
rand
()
/
((
float
)
RAND_MAX
);
}
CHECK
(
paddle_arguments_set_value
(
in_args
,
0
,
mat
));
paddle_arguments
out_args
=
paddle_arguments_create_none
();
CHECK
(
paddle_gradient_machine_forward
(
machine
,
in_args
,
out_args
,
/* isTrain */
false
));
paddle_matrix
prob
=
paddle_matrix_create_none
();
CHECK
(
paddle_arguments_value
(
out_args
,
0
,
prob
));
CHECK
(
paddle_matrix_get_row
(
prob
,
0
,
&
array
));
printf
(
"Prob: "
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
printf
(
"%.2f "
,
array
[
i
]);
}
printf
(
"
\n
"
);
return
0
;
}
paddle/capi/examples/dense/trainer_config.py
0 → 100644
浏览文件 @
470bbcf9
from
paddle.trainer_config_helpers
import
*
img
=
data_layer
(
name
=
'pixel'
,
size
=
784
)
hidden
=
fc_layer
(
input
=
img
,
size
=
200
,
param_attr
=
ParamAttr
(
name
=
'hidden.w'
),
bias_attr
=
ParamAttr
(
name
=
'hidden.b'
))
prob
=
fc_layer
(
input
=
hidden
,
size
=
10
,
act
=
SoftmaxActivation
(),
param_attr
=
ParamAttr
(
name
=
'prob.w'
),
bias_attr
=
ParamAttr
(
name
=
'prob.b'
))
outputs
(
prob
)
paddle/capi/examples/multi_thread/.gitignore
0 → 100644
浏览文件 @
470bbcf9
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe
paddle/capi/examples/multi_thread/CMakeLists.txt
0 → 100644
浏览文件 @
470bbcf9
project
(
multi_thread
)
cmake_minimum_required
(
VERSION 2.8
)
aux_source_directory
(
. SRC_LIST
)
add_executable
(
${
PROJECT_NAME
}
${
SRC_LIST
}
)
find_package
(
Threads
)
set_property
(
TARGET
${
PROJECT_NAME
}
PROPERTY C_STANDARD 99
)
target_link_libraries
(
${
PROJECT_NAME
}
-lpaddle_capi_shared
${
CMAKE_THREAD_LIBS_INIT
}
)
paddle/capi/examples/multi_thread/convert_protobin.sh
0 → 120000
浏览文件 @
470bbcf9
../dense/convert_protobin.sh
\ No newline at end of file
paddle/capi/examples/multi_thread/main.c
0 → 100644
浏览文件 @
470bbcf9
#include <paddle/capi.h>
#include <pthread.h>
#include <time.h>
#include "../common/common.h"
#define CONFIG_BIN "./trainer_config.bin"
#define NUM_THREAD 1000
#define NUM_ITER 1000
pthread_mutex_t
mutex
;
void
*
thread_main
(
void
*
gm_ptr
)
{
paddle_gradient_machine
machine
=
(
paddle_gradient_machine
)(
gm_ptr
);
for
(
int
iter
=
0
;
iter
<
NUM_ITER
;
++
iter
)
{
paddle_arguments
in_args
=
paddle_arguments_create_none
();
// There is only one input of this network.
CHECK
(
paddle_arguments_resize
(
in_args
,
1
));
// Create input matrix.
paddle_matrix
mat
=
paddle_matrix_create
(
/* sample_num */
1
,
/* size */
784
,
/* useGPU */
false
);
paddle_real
*
array
;
// Get First row.
CHECK
(
paddle_matrix_get_row
(
mat
,
0
,
&
array
));
for
(
int
i
=
0
;
i
<
784
;
++
i
)
{
array
[
i
]
=
rand
()
/
((
float
)
RAND_MAX
);
}
CHECK
(
paddle_arguments_set_value
(
in_args
,
0
,
mat
));
paddle_arguments
out_args
=
paddle_arguments_create_none
();
CHECK
(
paddle_gradient_machine_forward
(
machine
,
in_args
,
out_args
,
/* isTrain */
false
));
paddle_matrix
prob
=
paddle_matrix_create_none
();
CHECK
(
paddle_arguments_value
(
out_args
,
0
,
prob
));
CHECK
(
paddle_matrix_get_row
(
prob
,
0
,
&
array
));
pthread_mutex_lock
(
&
mutex
);
printf
(
"Prob: "
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
printf
(
"%.2f "
,
array
[
i
]);
}
printf
(
"
\n
"
);
pthread_mutex_unlock
(
&
mutex
);
}
CHECK
(
paddle_gradient_machine_destroy
(
machine
));
return
NULL
;
}
int
main
()
{
// Initalize Paddle
char
*
argv
[]
=
{
"--use_gpu=False"
};
CHECK
(
paddle_init
(
1
,
(
char
**
)
argv
));
// Reading config binary file. It is generated by `convert_protobin.sh`
long
size
;
void
*
buf
=
read_config
(
CONFIG_BIN
,
&
size
);
// Create a gradient machine for inference.
paddle_gradient_machine
machine
;
CHECK
(
paddle_gradient_machine_create_for_inference
(
&
machine
,
buf
,
(
int
)
size
));
CHECK
(
paddle_gradient_machine_randomize_param
(
machine
));
// Loading parameter. Uncomment the following line and change the directory.
// CHECK(paddle_gradient_machine_load_parameter_from_disk(machine,
// "./some_where_to_params"));
srand
(
time
(
0
));
pthread_mutex_init
(
&
mutex
,
NULL
);
pthread_t
threads
[
NUM_THREAD
];
for
(
int
i
=
0
;
i
<
NUM_THREAD
;
++
i
)
{
paddle_gradient_machine
thread_local_machine
;
CHECK
(
paddle_gradient_machine_create_shared_param
(
machine
,
buf
,
size
,
&
thread_local_machine
));
pthread_create
(
&
threads
[
i
],
NULL
,
thread_main
,
thread_local_machine
);
}
for
(
int
i
=
0
;
i
<
NUM_THREAD
;
++
i
)
{
pthread_join
(
threads
[
i
],
NULL
);
}
pthread_mutex_destroy
(
&
mutex
);
return
0
;
}
paddle/capi/examples/multi_thread/trainer_config.py
0 → 120000
浏览文件 @
470bbcf9
..
/
dense
/
trainer_config
.
py
\ No newline at end of file
paddle/capi/examples/sparse_binary/.gitignore
0 → 100644
浏览文件 @
470bbcf9
# This file is used to ignore files which are generated
# ----------------------------------------------------------------------------
*~
*.autosave
*.a
*.core
*.moc
*.o
*.obj
*.orig
*.rej
*.so
*.so.*
*_pch.h.cpp
*_resource.rc
*.qm
.#*
*.*#
core
!core/
tags
.DS_Store
.directory
*.debug
Makefile*
*.prl
*.app
moc_*.cpp
ui_*.h
qrc_*.cpp
Thumbs.db
*.res
*.rc
/.qmake.cache
/.qmake.stash
# qtcreator generated files
*.pro.user*
# xemacs temporary files
*.flc
# Vim temporary files
.*.swp
# Visual Studio generated files
*.ib_pdb_index
*.idb
*.ilk
*.pdb
*.sln
*.suo
*.vcproj
*vcproj.*.*.user
*.ncb
*.sdf
*.opensdf
*.vcxproj
*vcxproj.*
# MinGW generated files
*.Debug
*.Release
# Python byte code
*.pyc
# Binaries
# --------
*.dll
*.exe
paddle/capi/examples/sparse_binary/CMakeLists.txt
0 → 100644
浏览文件 @
470bbcf9
project
(
sparse_binary
)
cmake_minimum_required
(
VERSION 2.8
)
aux_source_directory
(
. SRC_LIST
)
add_executable
(
${
PROJECT_NAME
}
${
SRC_LIST
}
)
find_package
(
Threads
)
set_property
(
TARGET
${
PROJECT_NAME
}
PROPERTY C_STANDARD 99
)
target_link_libraries
(
${
PROJECT_NAME
}
-lpaddle_capi_shared
)
paddle/capi/examples/sparse_binary/convert_protobin.sh
0 → 120000
浏览文件 @
470bbcf9
../dense/convert_protobin.sh
\ No newline at end of file
paddle/capi/examples/sparse_binary/main.c
0 → 100644
浏览文件 @
470bbcf9
#include <paddle/capi.h>
#include <time.h>
#include "../common/common.h"
#define CONFIG_BIN "./trainer_config.bin"
int
main
()
{
// Initalize Paddle
char
*
argv
[]
=
{
"--use_gpu=False"
};
CHECK
(
paddle_init
(
1
,
(
char
**
)
argv
));
// Reading config binary file. It is generated by `convert_protobin.sh`
long
size
;
void
*
buf
=
read_config
(
CONFIG_BIN
,
&
size
);
// Create a gradient machine for inference.
paddle_gradient_machine
machine
;
CHECK
(
paddle_gradient_machine_create_for_inference
(
&
machine
,
buf
,
(
int
)
size
));
CHECK
(
paddle_gradient_machine_randomize_param
(
machine
));
// Loading parameter. Uncomment the following line and change the directory.
// CHECK(paddle_gradient_machine_load_parameter_from_disk(machine,
// "./some_where_to_params"));
paddle_arguments
in_args
=
paddle_arguments_create_none
();
// There is only one input of this network.
CHECK
(
paddle_arguments_resize
(
in_args
,
1
));
// Create input matrix.
paddle_matrix
mat
=
paddle_matrix_create_sparse
(
1
,
784
,
3
,
true
,
false
);
srand
(
time
(
0
));
paddle_real
*
array
;
int
colBuf
[]
=
{
9
,
93
,
109
};
int
rowBuf
[]
=
{
0
,
sizeof
(
colBuf
)
/
sizeof
(
int
)};
CHECK
(
paddle_matrix_sparse_copy_from
(
mat
,
rowBuf
,
sizeof
(
rowBuf
)
/
sizeof
(
int
),
colBuf
,
sizeof
(
colBuf
)
/
sizeof
(
int
),
NULL
,
0
));
CHECK
(
paddle_arguments_set_value
(
in_args
,
0
,
mat
));
paddle_arguments
out_args
=
paddle_arguments_create_none
();
CHECK
(
paddle_gradient_machine_forward
(
machine
,
in_args
,
out_args
,
/* isTrain */
false
));
paddle_matrix
prob
=
paddle_matrix_create_none
();
CHECK
(
paddle_arguments_value
(
out_args
,
0
,
prob
));
CHECK
(
paddle_matrix_get_row
(
prob
,
0
,
&
array
));
printf
(
"Prob: "
);
for
(
int
i
=
0
;
i
<
10
;
++
i
)
{
printf
(
"%.2f "
,
array
[
i
]);
}
printf
(
"
\n
"
);
return
0
;
}
paddle/capi/examples/sparse_binary/trainer_config.py
0 → 120000
浏览文件 @
470bbcf9
..
/
dense
/
trainer_config
.
py
\ No newline at end of file
paddle/capi/gradient_machine.cpp
浏览文件 @
470bbcf9
...
...
@@ -113,3 +113,11 @@ paddle_error paddle_gradient_machine_create_shared_param(
return
kPD_NO_ERROR
;
}
}
paddle_error
paddle_gradient_machine_randomize_param
(
paddle_gradient_machine
machine
)
{
auto
m
=
cast
(
machine
);
if
(
m
==
nullptr
||
m
->
machine
==
nullptr
)
return
kPD_NULLPTR
;
m
->
machine
->
randParameters
();
return
kPD_NO_ERROR
;
}
paddle/capi/gradient_machine.h
浏览文件 @
470bbcf9
...
...
@@ -74,6 +74,9 @@ paddle_gradient_machine_create_shared_param(paddle_gradient_machine origin,
int
size
,
paddle_gradient_machine
*
slave
);
PD_API
paddle_error
paddle_gradient_machine_randomize_param
(
paddle_gradient_machine
machine
);
/**
* @brief Destroy a gradient machine
* @param machine that need to destroy
...
...
paddle/capi/matrix.h
浏览文件 @
470bbcf9
...
...
@@ -15,6 +15,7 @@ limitations under the License. */
#ifndef __PADDLE_CAPI_MATRIX_H__
#define __PADDLE_CAPI_MATRIX_H__
#include <stdbool.h>
#include <stdint.h>
#include "config.h"
#include "error.h"
...
...
@@ -39,6 +40,18 @@ PD_API paddle_matrix paddle_matrix_create(uint64_t height,
uint64_t
width
,
bool
useGpu
);
/**
* @brief paddle_matrix_create_sparse Create a sparse matrix.
* @param height the matrix height.
* @param width the matrix width.
* @param nnz the number of non-zero elements.
* @param isBinary is binary (either 1 or 0 in matrix) or not.
* @param useGpu is using GPU or not.
* @return paddle_matrix.
*/
PD_API
paddle_matrix
paddle_matrix_create_sparse
(
uint64_t
height
,
uint64_t
width
,
uint64_t
nnz
,
bool
isBinary
,
bool
useGpu
);
/**
* @brief paddle_matrix_destroy Destroy a matrix.
* @param mat
...
...
@@ -55,7 +68,7 @@ PD_API paddle_error paddle_matrix_destroy(paddle_matrix mat);
*/
PD_API
paddle_error
paddle_matrix_set_row
(
paddle_matrix
mat
,
uint64_t
rowID
,
p
d
_real
*
rowArray
);
p
addle
_real
*
rowArray
);
/**
* @brief PDMatGetRow Get raw row buffer from matrix
...
...
@@ -66,7 +79,7 @@ PD_API paddle_error paddle_matrix_set_row(paddle_matrix mat,
*/
PD_API
paddle_error
paddle_matrix_get_row
(
paddle_matrix
mat
,
uint64_t
rowID
,
p
d
_real
**
rawRowBuffer
);
p
addle
_real
**
rawRowBuffer
);
/**
* @brief PDMatCreateNone Create None Matrix
...
...
@@ -85,6 +98,27 @@ PD_API paddle_error paddle_matrix_get_shape(paddle_matrix mat,
uint64_t
*
height
,
uint64_t
*
width
);
/**
* @brief paddle_matrix_sparse_copy_from Copy from a CSR format matrix
* @param [out] mat output matrix
* @param [in] rowArray row array. The array slices in column array.
* @param [in] rowSize length of row array.
* @param [in] colArray the column array. It means the non-zero element indices
* in each row.
* @param [in] colSize length of column array.
* @param [in] valueArray the value array. It means the non-zero elemnt values.
* NULL if the matrix is binary.
* @param [in] valueSize length of value array. Zero if the matrix is binary.
* @return paddle_error
*/
PD_API
paddle_error
paddle_matrix_sparse_copy_from
(
paddle_matrix
mat
,
int
*
rowArray
,
uint64_t
rowSize
,
int
*
colArray
,
uint64_t
colSize
,
float
*
valueArray
,
uint64_t
valueSize
);
#ifdef __cplusplus
}
#endif
...
...
paddle/capi/tests/test_Arguments.cpp
浏览文件 @
470bbcf9
...
...
@@ -17,10 +17,10 @@ limitations under the License. */
#include "gtest/gtest.h"
#include "paddle/utils/ThreadLocal.h"
static
std
::
vector
<
p
d
_real
>
randomBuffer
(
size_t
bufSize
)
{
static
std
::
vector
<
p
addle
_real
>
randomBuffer
(
size_t
bufSize
)
{
auto
&
eng
=
paddle
::
ThreadLocalRandomEngine
::
get
();
std
::
uniform_real_distribution
<
p
d
_real
>
dist
(
-
1.0
,
1.0
);
std
::
vector
<
p
d
_real
>
retv
;
std
::
uniform_real_distribution
<
p
addle
_real
>
dist
(
-
1.0
,
1.0
);
std
::
vector
<
p
addle
_real
>
retv
;
retv
.
reserve
(
bufSize
);
for
(
size_t
i
=
0
;
i
<
bufSize
;
++
i
)
{
retv
.
push_back
(
dist
(
eng
));
...
...
@@ -42,7 +42,7 @@ TEST(CAPIArguments, value) {
paddle_matrix
mat
=
paddle_matrix_create
(
128
,
64
,
false
);
for
(
size_t
i
=
0
;
i
<
128
;
++
i
)
{
std
::
vector
<
p
d
_real
>
sampleBuf
=
randomBuffer
(
64
);
std
::
vector
<
p
addle
_real
>
sampleBuf
=
randomBuffer
(
64
);
paddle_matrix_set_row
(
mat
,
i
,
sampleBuf
.
data
());
}
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_arguments_set_value
(
args
,
0
,
mat
));
...
...
@@ -52,8 +52,8 @@ TEST(CAPIArguments, value) {
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_arguments_value
(
args
,
0
,
val
));
for
(
size_t
i
=
0
;
i
<
128
;
++
i
)
{
p
d
_real
*
row1
;
p
d
_real
*
row2
;
p
addle
_real
*
row1
;
p
addle
_real
*
row2
;
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_matrix_get_row
(
mat
,
i
,
&
row1
));
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_matrix_get_row
(
val
,
i
,
&
row2
));
...
...
paddle/capi/tests/test_GradientMachine.cpp
浏览文件 @
470bbcf9
...
...
@@ -21,10 +21,10 @@ limitations under the License. */
#include "capi.h"
#include "paddle/utils/ThreadLocal.h"
static
std
::
vector
<
p
d
_real
>
randomBuffer
(
size_t
bufSize
)
{
static
std
::
vector
<
p
addle
_real
>
randomBuffer
(
size_t
bufSize
)
{
auto
&
eng
=
paddle
::
ThreadLocalRandomEngine
::
get
();
std
::
uniform_real_distribution
<
p
d
_real
>
dist
(
-
1.0
,
1.0
);
std
::
vector
<
p
d
_real
>
retv
;
std
::
uniform_real_distribution
<
p
addle
_real
>
dist
(
-
1.0
,
1.0
);
std
::
vector
<
p
addle
_real
>
retv
;
retv
.
reserve
(
bufSize
);
for
(
size_t
i
=
0
;
i
<
bufSize
;
++
i
)
{
retv
.
push_back
(
dist
(
eng
));
...
...
@@ -60,12 +60,12 @@ TEST(GradientMachine, testPredict) {
paddle_arguments
inArgs
=
paddle_arguments_create_none
();
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_arguments_resize
(
inArgs
,
1
));
paddle_matrix
mat
=
paddle_matrix_create
(
1
,
100
,
false
);
static_assert
(
std
::
is_same
<
p
d
_real
,
paddle
::
real
>::
value
,
""
);
static_assert
(
std
::
is_same
<
p
addle
_real
,
paddle
::
real
>::
value
,
""
);
auto
data
=
randomBuffer
(
100
);
p
d
_real
*
rowPtr
;
p
addle
_real
*
rowPtr
;
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_matrix_get_row
(
mat
,
0
,
&
rowPtr
));
memcpy
(
rowPtr
,
data
.
data
(),
data
.
size
()
*
sizeof
(
p
d
_real
));
memcpy
(
rowPtr
,
data
.
data
(),
data
.
size
()
*
sizeof
(
p
addle
_real
));
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_arguments_set_value
(
inArgs
,
0
,
mat
));
ASSERT_EQ
(
kPD_NO_ERROR
,
...
...
paddle/capi/tests/test_Matrix.cpp
浏览文件 @
470bbcf9
...
...
@@ -17,7 +17,7 @@ limitations under the License. */
TEST
(
CAPIMatrix
,
create
)
{
paddle_matrix
mat
=
paddle_matrix_create
(
128
,
32
,
false
);
std
::
vector
<
p
d
_real
>
sampleRow
;
std
::
vector
<
p
addle
_real
>
sampleRow
;
sampleRow
.
resize
(
32
);
for
(
size_t
i
=
0
;
i
<
sampleRow
.
size
();
++
i
)
{
sampleRow
[
i
]
=
1.0
/
(
i
+
1.0
);
...
...
@@ -26,7 +26,7 @@ TEST(CAPIMatrix, create) {
ASSERT_EQ
(
kPD_OUT_OF_RANGE
,
paddle_matrix_set_row
(
mat
,
128
,
sampleRow
.
data
()));
p
d
_real
*
arrayPtr
;
p
addle
_real
*
arrayPtr
;
ASSERT_EQ
(
kPD_NO_ERROR
,
paddle_matrix_get_row
(
mat
,
0
,
&
arrayPtr
));
for
(
size_t
i
=
0
;
i
<
sampleRow
.
size
();
++
i
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录