optimizer.go 2.7 KB
Newer Older
1 2
package pserver

D
dongzhihong 已提交
3
// #cgo CFLAGS: -I ../../
D
dongzhihong 已提交
4
// //FIXME: ldflags contain "build" path
W
wuyi05 已提交
5
// #cgo LDFLAGS: ${SRCDIR}/../../build/go/pserver/client/c/libpaddle_go_optimizer.a -lstdc++ -lm
D
dongzhihong 已提交
6 7 8
// #include "paddle/optimizer/optimizer.h"
// #include <stdlib.h>
// #include <string.h>
9
import "C"
D
dongzhihong 已提交
10

11 12 13
import (
	"fmt"
	"unsafe"
D
dongzhihong 已提交
14 15

	log "github.com/sirupsen/logrus"
16 17 18 19 20
)

var nullPtr = unsafe.Pointer(uintptr(0))

type optimizer struct {
D
dongzhihong 已提交
21 22
	opt         *C.struct_paddle_optimizer
	elementType ElementType
D
dongzhihong 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35
}

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]
36 37
}

D
dzhwinter 已提交
38
func newOptimizer(paramWithConfigs ParameterWithConfig) *optimizer {
39
	o := &optimizer{}
D
dongzhihong 已提交
40
	o.elementType = paramWithConfigs.Param.ElementType
D
dzhwinter 已提交
41 42
	p := paramWithConfigs.Param
	c := paramWithConfigs.Config
D
dongzhihong 已提交
43
	s := paramWithConfigs.State
D
dongzhihong 已提交
44 45 46 47
	log.WithFields(log.Fields{
		"ElementType": p.ElementType,
		"ParamSize":   len(p.Content),
		"ConfigSize":  len(c),
D
dongzhihong 已提交
48
		"StateSize":   len(s),
D
dongzhihong 已提交
49
	}).Info("New Optimizer Created with config:")
D
dongzhihong 已提交
50
	var cbuffer unsafe.Pointer
D
dongzhihong 已提交
51 52
	cbuffer = C.malloc(C.size_t(len(p.Content)))
	C.memcpy(cbuffer, unsafe.Pointer(&p.Content[0]), C.size_t(len(p.Content)))
D
dongzhihong 已提交
53 54 55 56 57
	var cstate unsafe.Pointer
	if len(s) != 0 {
		cstate = unsafe.Pointer(&s[0])
	}

D
dongzhihong 已提交
58
	o.opt = C.paddle_create_optimizer((*C.uchar)(&c[0]), C.int(len(c)),
D
dongzhihong 已提交
59
		C.paddle_element_type(p.ElementType), cbuffer, C.int(len(p.Content)/C.sizeof_float), (*C.char)(cstate), C.int(len(s)))
60 61 62
	return o
}

63
func (o *optimizer) GetWeights() []byte {
D
dongzhihong 已提交
64
	var buffer unsafe.Pointer
W
wuyi05 已提交
65 66
	bufferLen := C.paddle_optimizer_get_weights(o.opt, &buffer)
	return cArrayToSlice(buffer, int(bufferLen)*C.sizeof_float)
D
dongzhihong 已提交
67
}
68

D
dongzhihong 已提交
69 70 71 72 73 74
func (o *optimizer) GetStates() []byte {
	var cbuffer *C.char
	cbuffer_len := C.paddle_optimizer_get_state(o.opt, &cbuffer)
	return cArrayToSlice(unsafe.Pointer(cbuffer), int(cbuffer_len))
}

D
dongzhihong 已提交
75
func (o *optimizer) UpdateParameter(g Gradient) error {
D
dongzhihong 已提交
76 77
	if o.elementType != g.ElementType {
		return fmt.Errorf("Name: %s, parameter and gradient element type not match, parameter: %v, gradient: %v", g.Name, o.elementType, g.ElementType)
78 79
	}

D
dongzhihong 已提交
80
	r := C.paddle_update_parameter(o.opt, C.paddle_element_type(g.ElementType), unsafe.Pointer(&g.Content[0]), C.int(len(g.Content))/C.sizeof_float)
81
	if r != 0 {
H
Helin Wang 已提交
82
		return fmt.Errorf("optimizer update returned error code: %d", r)
83 84 85 86 87 88 89
	}
	return nil
}

func (o *optimizer) Cleanup() {
	if unsafe.Pointer(o.opt) != nullPtr {
		C.paddle_release_optimizer(o.opt)
90
		o.opt = (*C.struct_paddle_optimizer)(nullPtr)
91 92
	}
}