Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
12749ad5
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看板
提交
12749ad5
编写于
6月 26, 2017
作者:
D
dongzhihong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
"fix cmake flags in optimizer"
上级
59b40ecb
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
60 addition
and
27 deletion
+60
-27
go/pserver/cclient/CMakeLists.txt
go/pserver/cclient/CMakeLists.txt
+2
-0
go/pserver/cclient/test/testdata/optimizer.pb.txt
go/pserver/cclient/test/testdata/optimizer.pb.txt
+0
-0
go/pserver/optimizer.go
go/pserver/optimizer.go
+32
-9
go/pserver/optimizer_test.go
go/pserver/optimizer_test.go
+16
-8
go/pserver/service.go
go/pserver/service.go
+7
-4
go/pserver/service_test.go
go/pserver/service_test.go
+2
-6
paddle/optimizer/CMakeLists.txt
paddle/optimizer/CMakeLists.txt
+1
-0
未找到文件。
go/pserver/cclient/CMakeLists.txt
浏览文件 @
12749ad5
...
...
@@ -9,6 +9,8 @@ project(cxx_go C Go)
include
(
golang
)
include
(
flags
)
cc_library
(
paddle_go_optimizer DEPS paddle_optimizer paddle_proto glog gflags
)
go_library
(
paddle_pserver_cclient STATIC
)
if
(
WITH_TESTING
)
add_subdirectory
(
test
)
...
...
go/pserver/cclient/test/optimizer.pb.txt
→
go/pserver/cclient/test/
testdata/
optimizer.pb.txt
浏览文件 @
12749ad5
文件已移动
go/pserver/optimizer.go
浏览文件 @
12749ad5
...
...
@@ -4,8 +4,7 @@ package pserver
// TODO(zhihong): move compile flags to cmake go_library
#cgo pkg-config: protobuf
#cgo CFLAGS: -I ../../
#cgo LDFLAGS: ../../build/paddle/optimizer/libpaddle_optimizer.a ../../build/proto/libpaddle_proto.a ../../third_party/install/glog/lib/libglog.a ../../third_party/install/gtest/lib/libgtest.a ../../third_party/install/gflags/lib/libgflags.a ../../third_party/install/openblas/lib/libopenblas.a -I/usr/local/lib/ -lprotobuf
#cgo LDFLAGS: /Users/dzh/.go/src/github.com/PaddlePaddle/Paddle/build/lib/libdep.a
#cgo LDFLAGS: /Users/dzh/.go/src/github.com/PaddlePaddle/Paddle/build/go/pserver/cclient/libpaddle_go_optimizer.a
#include "paddle/optimizer/optimizer.h"
*/
import
"C"
...
...
@@ -18,26 +17,50 @@ var nullPtr = unsafe.Pointer(uintptr(0))
type
optimizer
struct
{
opt
*
C
.
struct_paddle_optimizer
// used in GetParam, reconstruct Parameter from optimizer
ElementType
ElementType
}
func
cArrayToSlice
(
p
unsafe
.
Pointer
,
len
int
)
[]
byte
{
if
p
==
nullPtr
{
return
nil
}
// create a Go clice backed by a C array, reference:
// https://github.com/golang/go/wiki/cgo#turning-c-arrays-into-go-slices
//
// Go garbage collector will not interact with this data, need
// to be freed properly.
return
(
*
[
1
<<
30
]
byte
)(
p
)[
:
len
:
len
]
}
func
newOptimizer
(
paramWithConfigs
ParameterWithConfig
)
*
optimizer
{
o
:=
&
optimizer
{}
p
:=
paramWithConfigs
.
Param
c
:=
paramWithConfigs
.
Config
o
.
opt
=
C
.
paddle_create_optimizer
(
C
.
uchar
(
c
),
C
.
int
(
len
(
c
)),
unsafe
.
Pointer
(
p
.
Content
),
c
.
int
(
p
.
Length
),
nullPtr
,
0
)
buffer
:=
&
p
.
Content
[
0
]
o
.
opt
=
C
.
paddle_create_optimizer
(
C
.
uchar
(
c
),
C
.
int
(
len
(
c
)),
unsafe
.
Pointer
(
buffer
),
C
.
int
(
len
(
p
.
Content
)),
nullPtr
,
0
)
return
o
}
func
(
o
*
optimizer
)
UpdateParameter
(
p
Parameter
,
g
Gradient
)
error
{
if
p
.
Length
!=
g
.
Length
{
return
fmt
.
Errorf
(
"Name: %s, parameter and gradient length not match, parameter: %d, gradient: %d"
,
p
.
Name
,
p
.
Length
,
g
.
Length
)
func
(
o
*
optimizer
)
GetWeights
(
p
*
Parameter
)
error
{
var
buffer
unsafe
.
Pointer
buffer_len
:=
C
.
paddle_optimizer_get_weights
(
unsafe
.
Pointer
(
o
),
&
buffer
)
if
buffer_len
==
0
||
buffer
==
nullPtr
{
return
fmt
.
Errorf
(
"parameter optimizer error : %s get failed"
,
p
.
name
)
}
p
.
Content
=
cArrayToSlice
(
buffer
,
int
(
buffer_len
))
return
nil
}
if
p
.
ElementType
!=
g
.
ElementType
{
return
fmt
.
Errorf
(
"Name: %s, parameter and gradient element type not match, parameter: %v, gradient: %v"
,
p
.
Name
,
p
.
ElementType
,
g
.
ElementType
)
func
(
o
*
optimizer
)
UpdateParameter
(
g
Gradient
)
error
{
if
o
.
ElementType
!=
g
.
ElementType
{
return
fmt
.
Errorf
(
"Name: %s, parameter and gradient element type not match, parameter: %v, gradient: %v"
,
g
.
Name
,
g
.
ElementType
,
g
.
ElementType
)
}
r
:=
C
.
paddle_update_parameter
(
o
.
opt
,
C
.
paddle_element_type
(
p
.
ElementType
),
unsafe
.
Pointer
(
g
.
Content
),
C
.
int
(
g
.
Length
))
// FIXME: do we need a copy? discard g.Content by GC ok
r
:=
C
.
paddle_update_parameter
(
o
.
opt
,
C
.
paddle_element_type
(
g
.
ElementType
),
unsafe
.
Pointer
(
g
.
Content
),
C
.
int
(
len
(
g
.
Content
)))
if
r
!=
0
{
return
fmt
.
Errorf
(
"optimizer update returned error code: %d"
,
r
)
}
...
...
go/pserver/optimizer_test.go
浏览文件 @
12749ad5
package
pserver
import
"testing"
import
(
"io/ioutil"
"testing"
)
func
TestSGDCreateRelease
(
t
*
testing
.
T
)
{
param
:=
pserver
.
ParameterWithConfig
{
Param
:
pserver
.
Parameter
{
Name
:
"a"
,
ElementType
:
,
Content
:
,
Length
:
}
func
TestOptimizerCreateRelease
(
t
*
testing
.
T
)
{
p
:=
Parameter
{
Name
:
"a"
,
ElementType
:
Float32
,
}
o
:=
newOptimizer
(
sgd
,
1
)
p
.
Content
=
[]
byte
{
0.1
,
0.3
}
config
,
err
:=
ioutil
.
ReadFile
(
"./cclient/test/testdata/optimizer.pb.txt"
)
param
:=
ParameterWithConfig
{
Param
:
p
,
Config
:
config
,
}
o
:=
newOptimizer
(
param
)
o
.
Cleanup
()
}
go/pserver/service.go
浏览文件 @
12749ad5
...
...
@@ -107,7 +107,7 @@ func (s *Service) SendGrad(g Gradient, dummy *int) error {
return
fmt
.
Errorf
(
"parameter: %s does not exist"
,
g
.
Name
)
}
return
o
.
UpdateParameter
(
p
,
g
)
return
o
.
UpdateParameter
(
g
)
}
// GetParam gets parameters from the parameter server.
...
...
@@ -116,7 +116,7 @@ func (s *Service) GetParam(name string, parameter *Parameter) error {
s
.
mu
.
Lock
()
defer
s
.
mu
.
Unlock
()
p
,
ok
:=
s
.
param
Map
[
name
]
opt
,
ok
:=
s
.
opt
Map
[
name
]
if
!
ok
{
return
fmt
.
Errorf
(
"parameter: %s does not exist"
,
name
)
}
...
...
@@ -128,8 +128,11 @@ func (s *Service) GetParam(name string, parameter *Parameter) error {
// nature. This race condition is allowed deliberately
// to save the program from making a copy of the
// paramter content.
*
parameter
=
p
return
nil
p
.
Name
=
name
p
.
ElementType
=
opt
.
ElementType
ok
:=
opt
.
GetWeights
(
&
parameter
)
return
ok
}
// Save tells the parameter server to save parameters.
...
...
go/pserver/service_test.go
浏览文件 @
12749ad5
...
...
@@ -13,9 +13,7 @@ func TestFull(t *testing.T) {
s
:=
pserver
.
NewService
()
var
p
pserver
.
Parameter
p
.
Name
=
"param_a"
ElementValue
:=
[]
byte
{
1
,
0
,
0
,
0
,
2
,
0
,
0
,
0
,
3
,
0
,
0
,
0
}
p
.
Content
=
&
ElementValue
[
0
]
p
.
Length
=
len
(
ElementValue
)
p
.
Content
=
[]
byte
{
1
,
0
,
0
,
0
,
2
,
0
,
0
,
0
,
3
,
0
,
0
,
0
}
p
.
ElementType
=
pserver
.
Int32
err
:=
s
.
InitParam
(
pserver
.
ParameterWithConfig
{
Param
:
p
,
Config
:
nil
},
nil
)
if
err
!=
nil
{
...
...
@@ -24,9 +22,7 @@ func TestFull(t *testing.T) {
var
p1
pserver
.
Parameter
p1
.
Name
=
"param_b"
ElementValue
=
[]
byte
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
}
p1
.
Content
=
&
ElementValue
[
0
]
p1
.
Length
=
len
(
ElementValue
)
p1
.
Content
=
[]
byte
{
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
}
p1
.
ElementType
=
pserver
.
Float32
err
=
s
.
InitParam
(
pserver
.
ParameterWithConfig
{
Param
:
p1
,
Config
:
nil
},
nil
)
if
err
!=
nil
{
...
...
paddle/optimizer/CMakeLists.txt
浏览文件 @
12749ad5
...
...
@@ -12,6 +12,7 @@ set(OPITMIZER_SRCS
add_library
(
paddle_optimizer STATIC
${
OPITMIZER_SRCS
}
)
add_dependencies
(
paddle_optimizer gen_proto_cpp
)
if
(
WITH_TESTING
)
add_simple_unittest
(
serialization_test
)
add_simple_unittest
(
parameter_optimizer_test
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录