-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
C-API for inference. #1062
C-API for inference. #1062
Conversation
paddle/capi/Arguments.cpp
Outdated
@@ -0,0 +1,49 @@ | |||
#include "PaddleCAPI.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
看到大家都把这个目录叫做 "c", 这个作为参考
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/dmlc/mxnet/tree/master/src/c_api
一般项目目录第一层是编码语言,就比如Paddle
-- paddle
|- python
|- proto
所以有一些项目的会把这部分放到c目录下。而对于Paddle,一来C/CPP区分界限不明显,二来但就这个C-API来说,本身也是C与C++的混合写法。所以很难独立成C目录。
paddle/capi/PaddleCAPI.h
Outdated
@@ -0,0 +1,54 @@ | |||
#ifndef __PADDLE_PADDLE_CAPI_PADDLECAPI_H_INCLUDED__ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#pragma once 我们的规范貌似是这个?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个因为是会被用户使用的库,所以 还是用 #if 作为guard把。
@@ -0,0 +1,27 @@ | |||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
后边的test和前面的关系不大是么
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
抱歉,这个文件是勿提交的
paddle/capi/PaddleCAPI.h
Outdated
extern "C" { | ||
#endif | ||
|
||
#define PD_NO_ERROR 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是不是定义一个enum更好
typedef enum {
PD_NO_ERROR = 0,
PD_NULLPTR = 1
} PD_Code;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是。
eca863e
to
4fd6888
Compare
@wangkuiyi @hedaoyuan @jacquesqiao This C-API is only used for model inference now. And it is not part of current API design. However, neural network inference API is an urgent issue now. It just exposes some RAW Paddle APIs to C. |
exampleAdd sequence exampleAdd sequence exampleAdd sequence exampleAdd sequence exampleAdd sequence example
Also change previous design file, make concept consistant.
麻烦 @wangkuiyi @jacquesqiao @hedaoyuan @gangliao @QiJune 来Review一下这个PR吧。设计文档在 这里 |
*width = m->width(); | ||
*height = m->height(); | ||
} | ||
``` | ||
|
||
其中`paddle/math/matrix.hpp`文件内容为: | ||
其中`paddle/capi/CMatrix.hpp`文件内容为: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
文件名采用哪种命名呢,代码里面是matrix.h
和Matrix.cpp
,也需要统一一下吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个命名确实是统一的。。
对于C的header,为全小写加下划线形式。
对于CPP的source,为大写间隔。
只是这两个文件面向的语言不一样,因而采用更适合那个语言的命名风格。C语言的函数命名同理。
|
||
通常,这个结构体包含两个项目。 | ||
|
||
* `type`是一个类型的标志。对于每种类型,type字段均不尽相同。这样,即使C-API接受的类型全是`void *`,我们也可以确定每一个参数的类型。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type
在目前的代码里面好像没有起到什么作用,建议在cast
之后检查一下type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
目前确实没用,但是每次cast都检查也有点难受。毕竟检查都是要耗时的。。
这个type预留的目的还是为了有一定的多态性。。。
譬如,可以写一个 paddle_destroy(void*)
,这样可以删除任意类型的paddle对象。或者,paddle_tensor_get_data
可以接受matrix和vector都行。。
@@ -70,20 +70,20 @@ extern "C" | |||
paddle_error paddle_matrix_shape(paddle_matrix matrix, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
函数名是不是统一使用paddle__动词词组比较好,代码里面是paddle_matrix_get_shape,arguments里面有几个函数不是。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
|
||
### libpaddle\_capi_shared.{so, dylib} | ||
|
||
`libpaddle_capi_shared`是C-API导出的动态库。这个动态库的连接参数与Paddle的其他二进制(例如`paddle_traienr`)类似。用户可以直接使用这个动态库来引入Paddle C-API。具体使用方法为`-lpaddle_capi_shared`。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: paddle_traienr
-> paddle_trainer
|
||
using paddle::capi::cast; | ||
|
||
#define castArg(v) cast<paddle::capi::CArguments>(v) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上文提到的,castArg
时,是否需要检查type
是不是kARGUMENTS
printf("%.2f ", array[i]); | ||
} | ||
printf("\n"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没有调用paddle_matrix_destroy
等接口释放资源。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
确实,原来想程序退出了资源总会被OS回收,但是在example里面这么写确实不好。
NeuralNetwork* network) { | ||
return new MyNeuralNetwork(name, network); | ||
} | ||
} // namespace paddle |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MyNeuralNetwork
定义在其他地方没有用到。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NerualNetwork本身不能直接new的。他的构造函数是私有的。。这里只是为了把它的构造函数expose出来。
} | ||
|
||
paddle_error paddle_gradient_machine_create_shared_param( | ||
paddle_gradient_machine origin, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CGradientMachine
是否可以添加config
和size
这两个成员,这样这个接口可以简化成
paddle_error paddle_gradient_machine_create_shared_param(paddle_gradient_machine origin,
paddle_gradient_machine* slave)
也能保证origin肯定用到的是正确的config
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
share参数并不一定非要配置一样。有可能是两个不同的网络结构,但是参数有共享。
paddle_arguments args = paddle_arguments_create_none(); | ||
ASSERT_EQ(kPD_NO_ERROR, paddle_arguments_resize(args, 1)); | ||
|
||
paddle_matrix mat = paddle_matrix_create(128, 64, false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
没有GPU的单测?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
确实没有,这就加一下。
@@ -0,0 +1,46 @@ | |||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
上面那个text_Init.cpp是空的,是误加还是尚未实现?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
误加的
设计文档