service_test.go 5.3 KB
Newer Older
1
// Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
D
dongzhihong 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15 16 17
package pserver_test

import (
18
	"fmt"
19
	"io/ioutil"
D
dongzhihong 已提交
20
	"reflect"
21 22
	"sync"
	"testing"
23
	"time"
24

25
	"github.com/PaddlePaddle/Paddle/go/pserver"
26 27
)

Q
Qiao Longfei 已提交
28 29 30 31
const (
	OptimizerConfig = "./client/c/test/testdata/optimizer.pb"
)

D
dongzhihong 已提交
32
func TestServiceFull(t *testing.T) {
D
dongzhihong 已提交
33
	var cp pserver.Checkpoint
34
	s, err := pserver.NewService(0, time.Hour, "", nil, cp)
W
wuyi05 已提交
35 36 37
	if err != nil {
		t.Error(err)
	}
38 39
	var p pserver.Parameter
	p.Name = "param_a"
D
dongzhihong 已提交
40
	p.Content = []byte{1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0}
41
	p.ElementType = pserver.Int32
Q
Qiao Longfei 已提交
42
	config, err := ioutil.ReadFile(OptimizerConfig)
43
	if err != nil {
44
		t.Fatalf("read optimizer proto failed")
45 46
	}

47
	err = s.InitParam(pserver.ParameterWithConfig{Param: p, Config: config}, nil)
48
	if err != nil {
49
		t.Fatal(err)
50 51
	}

D
dongzhihong 已提交
52 53 54 55 56 57
	var p1 pserver.Parameter
	p1.Name = "param_b"
	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: config}, nil)
	if err != nil {
58
		t.Fatal(err)
D
dongzhihong 已提交
59 60 61 62
	}

	err = s.FinishInitParams(0, nil)
	if err != nil {
63
		t.Fatal(err)
D
dongzhihong 已提交
64 65 66 67 68
	}

	var param pserver.Parameter
	err = s.GetParam("param_b", &param)
	if err != nil {
69
		t.Fatal(err)
D
dongzhihong 已提交
70 71 72
	}

	if !reflect.DeepEqual(param, p1) {
73
		t.Fatal("not equal:", param, p1)
D
dongzhihong 已提交
74 75 76 77 78 79
	}

	g1, g2 := pserver.Gradient(p1), pserver.Gradient(p)

	err = s.SendGrad(g1, nil)
	if err != nil {
80
		t.Fatal(err)
D
dongzhihong 已提交
81 82 83 84
	}
	err = s.SendGrad(g2, nil)

	if err != nil {
85
		t.Fatal(err)
D
dongzhihong 已提交
86 87 88 89 90
	}

	var param1 pserver.Parameter
	err = s.GetParam("param_a", &param1)
	if err != nil {
91
		t.Fatal(err)
D
dongzhihong 已提交
92 93 94 95 96 97 98 99
	}

	// don't compare content, since it's already changed by
	// gradient update.
	param1.Content = nil
	p.Content = nil

	if !reflect.DeepEqual(param1, p) {
100
		t.Fatal("not equal:", param1, p)
D
dongzhihong 已提交
101 102 103 104
	}
}

func TestMultipleInit(t *testing.T) {
D
dongzhihong 已提交
105
	var cp pserver.Checkpoint
106
	s, err := pserver.NewService(0, time.Hour, "", nil, cp)
W
wuyi05 已提交
107
	if err != nil {
108
		t.Fatal(err)
W
wuyi05 已提交
109 110
	}
	err = s.FinishInitParams(0, nil)
D
dongzhihong 已提交
111
	if err != nil {
112
		t.Fatal(err)
D
dongzhihong 已提交
113 114 115 116
	}

	err = s.FinishInitParams(0, nil)
	if err.Error() != pserver.AlreadyInitialized {
117
		t.Fatal(err)
D
dongzhihong 已提交
118
	}
119 120
}

121
func TestUninitialized(t *testing.T) {
D
dongzhihong 已提交
122
	var cp pserver.Checkpoint
123
	s, err := pserver.NewService(0, time.Hour, "", nil, cp)
W
wuyi05 已提交
124
	err = s.SendGrad(pserver.Gradient{}, nil)
H
Helin Wang 已提交
125
	if err.Error() != pserver.Uninitialized {
126
		t.Fatal(err)
127 128 129
	}
}

130
func TestBlockUntilInitialized(t *testing.T) {
D
dongzhihong 已提交
131
	var cp pserver.Checkpoint
132
	s, err := pserver.NewService(0, time.Hour, "", nil, cp)
W
wuyi05 已提交
133 134 135
	if err != nil {
		t.Error(err)
	}
136
	ch := make(chan struct{}, 2)
H
Helin Wang 已提交
137
	errCh := make(chan error, 2)
138 139 140
	var wg sync.WaitGroup
	wg.Add(1)
	go func() {
141 142
		var param pserver.Parameter
		err := s.GetParam("param_a", &param)
143
		if err != nil {
H
Helin Wang 已提交
144
			errCh <- err
145 146
		}
		wg.Done()
H
Helin Wang 已提交
147
		ch <- struct{}{}
148 149
	}()

150
	time.Sleep(50 * time.Millisecond)
151

H
Helin Wang 已提交
152 153 154 155
	select {
	case <-ch:
		// some function returned before initialization is completed.
		t.FailNow()
H
Helin Wang 已提交
156 157
	case <-errCh:
		t.FailNow()
H
Helin Wang 已提交
158 159 160
	default:
	}

161 162 163 164
	var p pserver.Parameter
	p.Name = "param_a"
	p.Content = []byte{1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0}
	p.ElementType = pserver.Int32
Q
Qiao Longfei 已提交
165
	config, err := ioutil.ReadFile(OptimizerConfig)
166 167 168 169
	if err != nil {
		t.Fatalf("read optimizer proto failed")
	}
	err = s.InitParam(pserver.ParameterWithConfig{Param: p, Config: config}, nil)
170

171
	if err != nil {
172
		t.Fatal(err)
173 174
	}

175
	err = s.FinishInitParams(0, nil)
176
	if err != nil {
177
		t.Fatal(err)
178 179 180 181
	}

	wg.Wait()
}
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

func TestGradientString(t *testing.T) {
	g := pserver.Parameter{}
	g.ElementType = pserver.Float32
	g.Content = []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40, 0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
	if g.String() != "[3.3702806e+12 2.142699 3.3702806e+12 2.142699]" {
		t.Fatal("get float data error!")
	}

	g.Content = []byte{0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40,
		0x18, 0x2d, 0x44, 0x54, 0xfb, 0x21, 0x09, 0x40}
	if g.String() != "[3.3702806e+12 2.142699 3.3702806e+12 2.142699 3.3702806e+12 2.142699 3.3702806e+12 2.142699 3.3702806e+12 2.142699...3.3702806e+12 2.142699 3.3702806e+12 2.142699 3.3702806e+12 2.142699 3.3702806e+12 2.142699 3.3702806e+12 2.142699]" {
		t.Fatal("get float data error!", g.String())
	}
	fmt.Println(g)
}