Skip to content
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

Go optimizer: integrate Go with optimizer library #2610

Merged
merged 28 commits into from
Jul 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
5128714
"integrate go and optimizer library"
dzhwinter Jun 21, 2017
1f217f0
"add c testing, python testing TODO"
dzhwinter Jun 22, 2017
cc487c0
"fix typo delete"
dzhwinter Jun 22, 2017
eaed87b
"fix optimizer include error"
dzhwinter Jun 22, 2017
7386b06
"add optimizer naive link option"
dzhwinter Jun 22, 2017
59b40ec
revert parameter with []byte
dzhwinter Jun 24, 2017
12749ad
"fix cmake flags in optimizer"
dzhwinter Jun 26, 2017
c44a94b
"fix cmake build flags"
dzhwinter Jun 26, 2017
cebfae9
"move proto.txt to testdata folder"
dzhwinter Jun 26, 2017
864386d
"change log in optimizer"
dzhwinter Jun 26, 2017
242df4f
"copy parameter content with memcpy"
dzhwinter Jun 27, 2017
7f03817
"cgo ldflags config"
dzhwinter Jun 27, 2017
80d9150
" add elementtype in optimizer"
dzhwinter Jun 27, 2017
b49c9ba
"remove unuse comment"
dzhwinter Jun 27, 2017
a2fabcc
"fix client test"
dzhwinter Jun 27, 2017
05ddf23
"add log of create optimizer"
dzhwinter Jun 28, 2017
42dcffc
"add optimizer full test"
dzhwinter Jun 28, 2017
6bb84b9
"remove unused debug info"
dzhwinter Jun 28, 2017
36f8b84
Merge branch 'develop' into go_optimizer
dzhwinter Jun 30, 2017
c383313
Merge branch 'develop' into go_optimizer
dzhwinter Jun 30, 2017
5b07a31
Merge remote-tracking branch 'origin/develop' into go_optimizer
dzhwinter Jun 30, 2017
f712b02
"fix protobuf depend"
dzhwinter Jun 30, 2017
7b2bcbe
"polish cgo link option"
dzhwinter Jul 2, 2017
1d4f02c
Merge remote-tracking branch 'origin/develop' into go_optimizer
dzhwinter Jul 2, 2017
b9c15b6
"remove unused tests"
dzhwinter Jul 3, 2017
7228533
test
dzhwinter Jul 3, 2017
99e22a8
"fix pre-commit hook failed"
dzhwinter Jul 3, 2017
85c4352
"pass style check"
dzhwinter Jul 3, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions go/pserver/cclient/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cc_library(paddle_go_optimizer DEPS paddle_optimizer paddle_proto glog gflags protobuf)
go_library(paddle_pserver_cclient STATIC)

add_subdirectory(test)
if(WITH_TESTING)
add_subdirectory(test)
endif()
3 changes: 1 addition & 2 deletions go/pserver/cclient/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

cc_binary(main SRCS main.c DEPS paddle_pserver_cclient)
cc_test(test_cclient SRCS test_cclient.c DEPS paddle_pserver_cclient)
add_style_check_target(test_cclient test_cclient.c)
93 changes: 0 additions & 93 deletions go/pserver/cclient/test/main.c

This file was deleted.

160 changes: 74 additions & 86 deletions go/pserver/cclient/test/test_cclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,113 +3,101 @@

#include "libpaddle_pserver_cclient.h"

typedef float real;

void fail() {
// TODO(helin): fix: gtest using cmake is not working, using this
// hacky way for now.
printf("test failed.\n");
// TODO(helin): Fix: gtest using cmake is not working, using this
// hacky way for now.
#define fail() \
fprintf(stderr, "info: %s:%d: ", __FILE__, __LINE__); \
exit(-1);

void sendGrads(paddle_pserver_client c) {
unsigned char grad_a[2000] = {2};
unsigned char grad_b[3000] = {3};
paddle_gradient grad1 = {
"param_a", PADDLE_ELEMENT_TYPE_FLOAT32, grad_a, 2000};
paddle_gradient grad2 = {
"param_b", PADDLE_ELEMENT_TYPE_FLOAT32, grad_b, 3000};
paddle_gradient *grads[2] = {&grad1, &grad2};
if (paddle_send_grads(c, grads, 2)) {
fail();
}
}

void print_parameter(paddle_gradient* param) {
if (param == NULL) {
printf("param is NULL!!\n");
} else {
printf("==== parameter ====\n");
printf("name: %s\n", param->name);
printf("content_len: %d\n", param->content_len);
printf("content_type: %d\n", param->element_type);
int i;
for (i = 0; i < param->content_len / (int)sizeof(real); ++i) {
printf("%f ", ((float*)param->content)[i]);
}
printf("\n\n");
void getParams(paddle_pserver_client c) {
paddle_parameter param_a;
paddle_parameter param_b;
char name_a[] = "param_a";
char name_b[] = "param_b";
// Must pre-allocate the prameter content before calling paddle_get_params.
unsigned char content_a[2000] = {};
unsigned char content_b[3000] = {};
param_a.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
param_a.name = name_a;
param_a.content = content_a;
param_a.content_len = 2000;
param_b.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
param_b.name = name_b;
param_b.content = content_b;
param_b.content_len = 3000;

paddle_parameter *params[2] = {&param_a, &param_b};
if (paddle_get_params(c, params, 2)) {
fail();
}
}

int main() {
char addr[] = "localhost:3000";
paddle_pserver_client c = paddle_new_pserver_client(addr, 1);

char* names[] = {"param_a", "param_b"};

char *config_proto;
size_t config_proto_len = 0;
ssize_t nread;
FILE *fp = fopen("testdata/optimizer.pb.txt", "r");
if (!fp) {
fail();
}
while ((nread = getline(&config_proto, &config_proto_len, fp)) != -1) {
printf("%s", config_proto);
}
fclose(fp);
retry:
printf("init parameter to pserver:\n");

real param_content1[] = {0.1, 0.2, 0.3};
real param_content2[] = {0.4, 0.5, 0.6};
paddle_parameter** params =
(paddle_parameter**)malloc(sizeof(paddle_parameter*) * 2);
params[0] = (paddle_parameter*)malloc(sizeof(paddle_parameter));
params[0]->name = names[0];
params[0]->content = (unsigned char*)param_content1;
params[0]->content_len = 3 * sizeof(real);
params[0]->element_type = PADDLE_ELEMENT_TYPE_FLOAT32;

params[1] = (paddle_parameter*)malloc(sizeof(paddle_parameter));
params[1]->name = names[1];
params[1]->content = (unsigned char*)param_content2;
params[1]->content_len = 3 * sizeof(real);
params[1]->element_type = PADDLE_ELEMENT_TYPE_INT32;

if (paddle_begin_init_params(c)) {
if (paddle_init_param(c, *params[0], NULL, 0) != 0) {
paddle_parameter param;
char name_a[] = "param_a";
char name_b[] = "param_b";
unsigned char content_a[2000] = {1};
unsigned char content_b[3000] = {0};
param.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
param.name = name_a;
param.content = content_a;
param.content_len = 2000;
int error =
paddle_init_param(c, param, (void *)config_proto, config_proto_len);
if (error != 0) {
goto retry;
}
if (paddle_init_param(c, *params[1], NULL, 0) != 0) {

param.element_type = PADDLE_ELEMENT_TYPE_FLOAT32;
param.name = name_b;
param.content = content_b;
param.content_len = 3000;
error = paddle_init_param(c, param, (void *)config_proto, config_proto_len);
if (error != 0) {
goto retry;
}
if (paddle_finish_init_params(c) != 0) {

error = paddle_finish_init_params(c);
if (error != 0) {
goto retry;
}
} else {
fail();
}

printf("get inited parameters from pserver:\n");
// get parameters again by reusing the allocated parameter buffers.
if (paddle_get_params(c, params, 2) != 0) {
fail();
}
print_parameter(params[0]);
print_parameter(params[1]);

printf("send gradient to pserver:\n");
real gradient_content1[] = {0.01, 0.02, 0.03};
real gradinet_content2[] = {0.04, 0.05, 0.06};

paddle_gradient** grads =
(paddle_gradient**)malloc(sizeof(paddle_gradient*) * 2);
grads[0] = (paddle_gradient*)malloc(sizeof(paddle_gradient));
grads[0]->name = names[0];
grads[0]->content = (unsigned char*)gradient_content1;
grads[0]->content_len = 3 * sizeof(real);
grads[0]->element_type = PADDLE_ELEMENT_TYPE_FLOAT32;

grads[1] = (paddle_gradient*)malloc(sizeof(paddle_gradient));
grads[1]->name = names[1];
grads[1]->content = (unsigned char*)gradinet_content2;
grads[1]->content_len = 3 * sizeof(real);
grads[1]->element_type = PADDLE_ELEMENT_TYPE_INT32;

printf("print gradient sent to pserver:\n");
print_parameter(grads[0]);
print_parameter(grads[1]);

if (paddle_send_grads(c, grads, 2) != 0) {
fail();
}

printf("get updated parameters from pserver:\n");
// get parameters again by reusing the allocated parameter buffers.
if (paddle_get_params(c, params, 2) != 0) {
fail();
int i;
for (i = 0; i < 100; i++) {
sendGrads(c);
getParams(c);
}
print_parameter(params[0]);
print_parameter(params[1]);

if (paddle_save_model(c, "/tmp/") != 0) {
if (paddle_save_model(c, "/tmp/")) {
fail();
}

Expand Down
2 changes: 2 additions & 0 deletions go/pserver/cclient/test/test_train.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def main():
# create optimizer
optimizer = paddle.optimizer.Momentum(momentum=0)

#TODO(zhihong) : replace optimizer with new OptimizerConfig

trainer = paddle.trainer.SGD(cost=cost,
parameters=parameters,
update_equation=optimizer,
Expand Down
Binary file added go/pserver/cclient/test/testdata/optimizer.pb.txt
Binary file not shown.
9 changes: 7 additions & 2 deletions go/pserver/client_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pserver_test

import (
"io/ioutil"
"net"
"net/http"
"net/rpc"
Expand Down Expand Up @@ -74,18 +75,22 @@ func TestClientFull(t *testing.T) {
}

const numParameter = 100
config, err := ioutil.ReadFile("./cclient/test/testdata/optimizer.pb.txt")
if err != nil {
t.Fatalf("read optimizer proto failed")
}
for i := 0; i < numParameter; i++ {
var p pserver.Parameter
p.Name = "p_" + strconv.Itoa(i)
p.ElementType = pserver.Float32
p.Content = make([]byte, (i+1)*100)
err := c.InitParam(pserver.ParameterWithConfig{Param: p})
err := c.InitParam(pserver.ParameterWithConfig{Param: p, Config: config})
if err != nil {
t.Fatal(err)
}
}

err := c.FinishInitParams()
err = c.FinishInitParams()
if err != nil {
t.Fatal(err)
}
Expand Down
58 changes: 0 additions & 58 deletions go/pserver/optimizer.c

This file was deleted.

Loading