-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
param.go
314 lines (269 loc) · 7.79 KB
/
param.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2019 spaGO Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package nn
import (
"bytes"
mat "github.com/nlpodyssey/spago/pkg/mat32"
"github.com/nlpodyssey/spago/pkg/ml/ag"
"github.com/nlpodyssey/spago/pkg/utils/kvdb"
"log"
"sync"
)
// Param is the interface for a Model parameter.
type Param interface {
ag.Node // it implies fn.Operand and ag.GradValue too
// Name returns the params name (can be empty string).
Name() string
// SetName set the params name (can be empty string).
SetName(name string)
// Type returns the params type (weights, biases, undefined).
Type() ParamsType
// SetType set the params type (weights, biases, undefined).
SetType(pType ParamsType)
// SetRequiresGrad set whether the param requires gradient, or not.
SetRequiresGrad(value bool)
// ReplaceValue replaces the value of the parameter and clears the support structure.
ReplaceValue(value mat.Matrix)
// ApplyDelta updates the value of the underlying storage applying the delta.
ApplyDelta(delta mat.Matrix)
// Payload returns the optimizer support structure (can be nil).
Payload() *Payload
// SetPayload is a thread safe operation to set the given Payload on the
// receiver Param.
SetPayload(payload *Payload)
// ClearPayload clears the support structure.
ClearPayload()
}
// Params extends a slice of Param with Nodes() method.
type Params []Param
// Nodes converts the slice of Param into a slice of ag.Node.
func (ps Params) Nodes() []ag.Node {
ns := make([]ag.Node, len(ps))
for i, p := range ps {
ns[i] = p
}
return ns
}
var _ Param = ¶m{}
type param struct {
name string
pType ParamsType // lazy initialization
mu sync.Mutex // to avoid data race
value mat.Matrix // store the results of a forward evaluation.
grad mat.Matrix // TODO: support of sparse gradients
payload *Payload // additional data used for example by gradient-descend optimization methods
hasGrad bool
requiresGrad bool
storage *kvdb.KeyValueDB // default nil
}
// ParamOption allows to configure a new Param with your specific needs.
type ParamOption func(*param)
// RequiresGrad is an option to specify whether a Param should be trained or not.
func RequiresGrad(value bool) ParamOption {
return func(p *param) {
p.requiresGrad = value
}
}
// SetStorage is an option to specify a kvdb.KeyValueDB storage.
// This is useful, for example, for a memory-efficient embeddings
// Param implementation.
func SetStorage(storage *kvdb.KeyValueDB) ParamOption {
return func(p *param) {
p.storage = storage
}
}
// NewParam returns a new param.
func NewParam(value mat.Matrix, opts ...ParamOption) Param {
p := ¶m{
name: "", // lazy initialization
pType: Undefined, // lazy initialization
value: value,
grad: nil, // lazy initialization
hasGrad: false,
requiresGrad: true, // true by default, can be modified with the options
payload: nil, // lazy initialization
storage: nil,
}
for _, opt := range opts {
opt(p)
}
return p
}
// SetName set the params name (can be empty string).
func (r *param) SetName(name string) {
r.name = name
}
// SetType set the params type (weights, biases, undefined).
func (r *param) SetType(pType ParamsType) {
r.pType = pType
}
// Name returns the params name (can be empty string).
func (r *param) Name() string {
return r.name
}
// Type returns the params type (weights, biases, undefined).
func (r *param) Type() ParamsType {
return r.pType
}
// Value returns the value of the delegate itself.
func (r *param) Value() mat.Matrix {
return r.value
}
// ReplaceValue replaces the value of the parameter and clears the support structure.
func (r *param) ReplaceValue(value mat.Matrix) {
r.mu.Lock()
defer r.mu.Unlock()
r.value = value
r.payload = nil
if r.storage != nil {
r.updateStorage()
}
}
// ScalarValue returns the the scalar value of the node.
// It panics if the value is not a scalar.
// Note that it is not possible to start the backward step from a scalar value.
func (r *param) ScalarValue() mat.Float {
return r.value.Scalar()
}
// Grad returns the gradients accumulated during the backward pass.
func (r *param) Grad() mat.Matrix {
return r.grad
}
// PropagateGrad accumulate the gradients
func (r *param) PropagateGrad(grad mat.Matrix) {
if !r.requiresGrad {
return
}
r.mu.Lock()
defer r.mu.Unlock()
if r.grad == nil {
r.grad = mat.GetEmptyDenseWorkspace(r.value.Dims()) // this could reduce the number of allocations
}
r.grad.AddInPlace(grad)
r.hasGrad = true
}
// HasGrad returns true if there are accumulated gradients.
func (r *param) HasGrad() bool {
return r.hasGrad
}
// RequiresGrad returns true if the param requires gradients.
func (r *param) RequiresGrad() bool {
return r.requiresGrad
}
// RequiresGrad is an option to specify whether a Param should be trained or not.
func (r *param) SetRequiresGrad(value bool) {
r.requiresGrad = value
}
// ZeroGrad clears the gradients.
func (r *param) ZeroGrad() {
if r.grad == nil {
return
}
r.mu.Lock()
defer r.mu.Unlock()
defer mat.ReleaseDense(r.grad.(*mat.Dense)) // release memory
r.grad = nil
r.hasGrad = false
}
// ApplyDelta updates the value of the underlying storage applying the delta.
func (r *param) ApplyDelta(delta mat.Matrix) {
r.mu.Lock()
defer r.mu.Unlock()
r.Value().SubInPlace(delta)
if r.storage != nil {
r.updateStorage()
}
}
// Payload returns the optimizer support structure (can be nil).
func (r *param) Payload() *Payload {
r.mu.Lock()
defer r.mu.Unlock()
return r.payload
}
// SetPayload is a thread safe operation to set the given Payload on the
// receiver Param.
func (r *param) SetPayload(payload *Payload) {
r.mu.Lock()
defer r.mu.Unlock()
r.payload = payload
if r.storage != nil {
r.updateStorage()
}
}
// ClearPayload clears the support structure.
func (r *param) ClearPayload() {
r.mu.Lock()
defer r.mu.Unlock()
r.payload = nil
if r.storage != nil {
r.updateStorage()
}
}
func (r *param) updateStorage() {
if r.storage == nil {
return
}
buf := new(bytes.Buffer)
err := MarshalBinaryParam(r, buf)
if err != nil {
log.Fatal(err)
}
err = r.storage.Put([]byte(r.name), buf.Bytes())
if err != nil {
log.Fatal(err)
}
}
// Graph returns always nil since the "pure" parameter is not associated with any graph.
func (r *param) Graph() *ag.Graph {
return nil
}
// ID returns always -1 since the "pure" parameter is not associated with any graph.
func (r *param) ID() int {
return -1
}
// TimeStep returns always 0 since the "pure" parameter is not associated with any graph.
func (r *param) TimeStep() int {
return 0
}
// wrappedParam returns a new wrappedParam from the param itself.
func (r *param) wrappedParam(g *ag.Graph) *wrappedParam {
if r.requiresGrad {
return &wrappedParam{param: r, Node: g.NewWrap(r)}
}
return &wrappedParam{param: r, Node: g.NewWrapNoGrad(r)}
}
var _ Param = &wrappedParam{}
// wrappedParam enriches a Param with a Node.
type wrappedParam struct {
*param
Node ag.Node
}
// ID dispatches the call to the Node.
func (r *wrappedParam) ID() int {
return r.Node.ID()
}
// Graph dispatches the call to the Node.
func (r *wrappedParam) Graph() *ag.Graph {
return r.Node.Graph()
}
// Grad dispatches the call to the Node.
func (r *wrappedParam) Grad() mat.Matrix {
return r.Node.Grad()
}
// PropagateGrad dispatches the call to the Node.
func (r *wrappedParam) PropagateGrad(gx mat.Matrix) {
r.Node.PropagateGrad(gx)
}
// HasGrad dispatches the call to the Node.
func (r *wrappedParam) HasGrad() bool {
return r.Node.HasGrad()
}
// RequiresGrad dispatches the call to the Node.
func (r *wrappedParam) RequiresGrad() bool {
return r.Node.RequiresGrad()
}
// ZeroGrad dispatches the call to the Node.
func (r *wrappedParam) ZeroGrad() {
r.Node.ZeroGrad()
}