-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
VowpalWabbitAsync.cs
322 lines (282 loc) · 12.9 KB
/
VowpalWabbitAsync.cs
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
315
316
317
318
319
320
321
322
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VowpalWabbitAsync.cs">
// Copyright (c) by respective owners including Yahoo!, Microsoft, and
// individual contributors. All rights reserved. Released under a BSD
// license as described in the file LICENSE.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VW.Labels;
using VW.Serializer;
namespace VW
{
/// <summary>
/// An async wrapper VW supporting data ingest using declarative serializer infrastructure used with <see cref="VowpalWabbitThreadedLearning"/>.
/// </summary>
/// <typeparam name="TExample">The user type to be serialized.</typeparam>
public class VowpalWabbitAsync<TExample> : IDisposable
{
/// <summary>
/// The owning manager.
/// </summary>
private VowpalWabbitThreadedLearning manager;
/// <summary>
/// The serializers are not thread-safe. Thus we need to allocate one for each VW instance.
/// </summary>
private IVowpalWabbitSerializer<TExample>[] serializers;
internal VowpalWabbitAsync(VowpalWabbitThreadedLearning manager)
{
Contract.Requires(manager != null);
Contract.Ensures(this.serializers != null);
this.manager = manager;
// create a serializer for each instance - maintaining separate example caches
var serializer = VowpalWabbitSerializerFactory.CreateSerializer<TExample>(manager.Settings);
this.serializers = this.manager.VowpalWabbits
.Select(vw => serializer.Create(vw))
.ToArray();
}
/// <summary>
/// Learns from the given example.
/// </summary>
/// <param name="example">The example to learn.</param>
/// <param name="label">The label for this <paramref name="example"/>.</param>
/// <remarks>
/// The method only enqueues the example for learning and returns immediately.
/// You must not re-use the example.
/// </remarks>
public void Learn(TExample example, ILabel label = null)
{
Contract.Requires(example != null);
Contract.Requires(label != null);
manager.Post(vw =>
{
using (var ex = this.serializers[vw.Settings.Node].Serialize(example, label))
{
ex.Learn();
}
});
}
/// <summary>
/// Predicts for the given example.
/// </summary>
/// <param name="example">The example to predict for.</param>
/// <remarks>
/// The method only enqueues the example for prediction and returns immediately.
/// You must not re-use the example.
/// </remarks>
public void Predict(TExample example)
{
Contract.Requires(example != null);
manager.Post(vw =>
{
using (var ex = this.serializers[vw.Settings.Node].Serialize(example))
{
ex.Predict();
}
});
}
/// <summary>
/// Learns from the given example.
/// </summary>
/// <param name="example">The example to learn.</param>
/// <param name="label">The label for this <paramref name="example"/>.</param>
/// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param>
/// <returns>The prediction for the given <paramref name="example"/>.</returns>
/// <remarks>
/// The method only enqueues the example for learning and returns immediately.
/// Await the returned task to receive the prediction result.
/// </remarks>
public Task<TPrediction> Learn<TPrediction>(TExample example, ILabel label, IVowpalWabbitPredictionFactory<TPrediction> predictionFactory)
{
Contract.Requires(example != null);
Contract.Requires(label != null);
Contract.Requires(predictionFactory != null);
return manager.Post(vw =>
{
using (var ex = this.serializers[vw.Settings.Node].Serialize(example, label))
{
return ex.Learn(predictionFactory);
}
});
}
/// <summary>
/// Predicts for the given example.
/// </summary>
/// <param name="example">The example to predict for.</param>
/// <param name="predictionFactory">The prediction factory to be used. See <see cref="VowpalWabbitPredictionType"/>.</param>
/// <returns>The prediction for the given <paramref name="example"/>.</returns>
/// <remarks>
/// The method only enqueues the example for learning and returns immediately.
/// Await the returned task to receive the prediction result.
/// </remarks>
public Task<TPrediction> Predict<TPrediction>(TExample example, IVowpalWabbitPredictionFactory<TPrediction> predictionFactory)
{
Contract.Requires(example != null);
Contract.Requires(predictionFactory != null);
return manager.Post(vw =>
{
using (var ex = this.serializers[vw.Settings.Node].Serialize(example))
{
return ex.Predict(predictionFactory);
}
});
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing)
{
if (this.serializers != null)
{
foreach (var serializer in this.serializers)
{
// free cached examples
serializer.Dispose();
}
this.serializers = null;
}
}
}
}
/// <summary>
/// An async VW wrapper for multiline ingest.
/// </summary>
/// <typeparam name="TExample">The user type of the shared feature.</typeparam>
/// <typeparam name="TActionDependentFeature">The user type for each action dependent feature.</typeparam>
public class VowpalWabbitAsync<TExample, TActionDependentFeature> : IDisposable
{
/// <summary>
/// The owning manager.
/// </summary>
private readonly VowpalWabbitThreadedLearning manager;
/// <summary>
/// The serializers are not thread-safe. Thus we need to allocate one for each VW instance.
/// </summary>
private VowpalWabbitSingleExampleSerializer<TExample>[] serializers;
/// <summary>
/// The serializers are not thread-safe. Thus we need to allocate one for each VW instance.
/// </summary>
private VowpalWabbitSingleExampleSerializer<TActionDependentFeature>[] actionDependentFeatureSerializers;
internal VowpalWabbitAsync(VowpalWabbitThreadedLearning manager)
{
if (manager == null)
throw new ArgumentNullException("manager");
if (manager.Settings == null)
throw new ArgumentNullException("manager.Settings");
if (manager.Settings.ParallelOptions == null)
throw new ArgumentNullException("manager.Settings.ParallelOptions");
if (manager.Settings.ParallelOptions.MaxDegreeOfParallelism <= 0)
throw new ArgumentOutOfRangeException("MaxDegreeOfParallelism must be greater than zero.");
Contract.Ensures(this.serializers != null);
Contract.Ensures(this.actionDependentFeatureSerializers != null);
Contract.EndContractBlock();
this.manager = manager;
// create a serializer for each instance - maintaining separate example caches
var serializer = VowpalWabbitSerializerFactory.CreateSerializer<TExample>(manager.Settings) as VowpalWabbitSingleExampleSerializerCompiler<TExample>;
if (serializer == null)
throw new ArgumentException(string.Format(
"{0} maps to a multiline example. Use VowpalWabbitAsync<{0}> instead.",
typeof(TExample)));
var adfSerializer = VowpalWabbitSerializerFactory.CreateSerializer<TActionDependentFeature>(manager.Settings) as VowpalWabbitSingleExampleSerializerCompiler<TActionDependentFeature>;
if (adfSerializer == null)
throw new ArgumentException(string.Format(
"{0} maps to a multiline example. Use VowpalWabbitAsync<{0}> instead.",
typeof(TActionDependentFeature)));
this.serializers = this.manager.VowpalWabbits
.Select(vw => serializer.Create(vw))
.ToArray();
this.actionDependentFeatureSerializers = this.manager.VowpalWabbits
.Select(vw => adfSerializer.Create(vw))
.ToArray();
}
/// <summary>
/// Learn from the given example and return the current prediction for it.
/// </summary>
/// <param name="example">The shared example.</param>
/// <param name="actionDependentFeatures">The action dependent features.</param>
/// <param name="index">The index of the example to learn within <paramref name="actionDependentFeatures"/>.</param>
/// <param name="label">The label for the example to learn.</param>
public void Learn(TExample example, IReadOnlyCollection<TActionDependentFeature> actionDependentFeatures, int index, ILabel label)
{
Contract.Requires(example != null);
Contract.Requires(actionDependentFeatures != null);
Contract.Requires(index >= 0);
Contract.Requires(label != null);
manager.Post(vw => VowpalWabbitMultiLine.Learn(
vw,
this.serializers[vw.Settings.Node],
this.actionDependentFeatureSerializers[vw.Settings.Node],
example,
actionDependentFeatures,
index,
label));
}
/// <summary>
/// Learn from the given example and return the current prediction for it.
/// </summary>
/// <param name="example">The shared example.</param>
/// <param name="actionDependentFeatures">The action dependent features.</param>
/// <param name="index">The index of the example to learn within <paramref name="actionDependentFeatures"/>.</param>
/// <param name="label">The label for the example to learn.</param>
/// <returns>The ranked prediction for the given examples.</returns>
public Task<ActionDependentFeature<TActionDependentFeature>[]> LearnAndPredict(TExample example, IReadOnlyCollection<TActionDependentFeature> actionDependentFeatures, int index, ILabel label)
{
Contract.Requires(example != null);
Contract.Requires(actionDependentFeatures != null);
Contract.Requires(index >= 0);
Contract.Requires(label != null);
return manager.Post(vw => VowpalWabbitMultiLine.LearnAndPredict(
vw,
this.serializers[vw.Settings.Node],
this.actionDependentFeatureSerializers[vw.Settings.Node],
example,
actionDependentFeatures,
index,
label));
}
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing)
{
if (this.serializers != null)
{
foreach (var serializer in this.serializers)
{
// free cached examples
serializer.Dispose();
}
this.serializers = null;
}
if (this.actionDependentFeatureSerializers != null)
{
foreach (var serializer in this.actionDependentFeatureSerializers)
{
// free cached examples
serializer.Dispose();
}
this.actionDependentFeatureSerializers = null;
}
}
}
}
}