forked from VowpalWabbit/vowpal_wabbit
-
Notifications
You must be signed in to change notification settings - Fork 11
/
EvalOperation.cs
233 lines (196 loc) · 8.71 KB
/
EvalOperation.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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EvalData.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 Microsoft.ApplicationInsights;
using Microsoft.ServiceBus.Messaging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks.Dataflow;
using VW.Azure.Trainer.Data;
namespace VW.Azure.Trainer.Operations
{
internal sealed class EvalData
{
internal EvalEventData Data { get; set; }
internal string JSON { get; set; }
internal string PartitionKey { get; set; }
}
public sealed class EvalEventData
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("cost")]
public float Cost { get; set; }
[JsonProperty("prob")]
public float Probability { get; set; }
[JsonProperty("timestamp")]
public DateTime Timestamp { get; set; }
[JsonProperty("eventid")]
public string EventId { get; set; }
}
internal sealed class EvalOperation : IDisposable
{
private readonly EventHubClient evalEventHubClient;
private TransformManyBlock<object, EvalData> evalBlock;
private IDisposable evalBlockDisposable;
private TelemetryClient telemetry;
private PerformanceCounters performanceCounters;
internal EvalOperation(OnlineTrainerSettingsInternal settings, PerformanceCounters performanceCounters)
{
this.performanceCounters = performanceCounters;
this.telemetry = new TelemetryClient();
// evaluation pipeline
this.evalEventHubClient = EventHubClient.CreateFromConnectionString(settings.EvalEventHubConnectionString);
this.evalBlock = new TransformManyBlock<object, EvalData>(
(Func<object, IEnumerable<EvalData>>)this.OfflineEvaluate,
new ExecutionDataflowBlockOptions
{
MaxDegreeOfParallelism = 4,
BoundedCapacity = 1024
});
this.evalBlock.Completion.Trace(this.telemetry, "Stage 4 - Evaluation pipeline");
// batch output together to match EventHub throughput by maintaining maximum latency of 1 seconds
this.evalBlockDisposable = this.evalBlock.AsObservable()
.GroupBy(k => k.PartitionKey)
.Select(g =>
g.Window(TimeSpan.FromSeconds(1))
.Select(w => w.Buffer(245 * 1024, e => Encoding.UTF8.GetByteCount(e.JSON)))
.SelectMany(w => w)
.Subscribe(this.UploadEvaluation))
.Publish()
.Connect();
}
internal ITargetBlock<object> TargetBlock { get { return this.evalBlock; } }
private List<EvalData> OfflineEvaluate(object trainerResultObj)
{
try
{
var trainerResult = trainerResultObj as TrainerResult;
if (trainerResult == null)
{
this.telemetry.TrackTrace($"Received invalid data: trainerResult is null");
return new List<EvalData>();
}
return this.OfflineEvaluateInternal(trainerResult)
// insert event id & timestamp to enable data correlation
.Select(e => {
e.EventId = trainerResult.EventId;
e.Timestamp = trainerResult.Timestamp;
var ed = new EvalData
{
PartitionKey = trainerResult.PartitionKey,
Data = e,
JSON = JsonConvert.SerializeObject(e)
};
return ed;
})
.ToList();
}
catch (Exception e)
{
this.telemetry.TrackException(e);
return new List<EvalData>();
}
}
private IEnumerable<EvalEventData> OfflineEvaluateInternal(TrainerResult trainerResult)
{
this.performanceCounters.Stage4_Evaluation_PerSec.Increment();
this.performanceCounters.Stage4_Evaluation_Total.Increment();
if (trainerResult == null)
{
yield break;
}
if (trainerResult.Label == null)
{
this.telemetry.TrackTrace($"Received invalid data: trainerResult.Label is null");
yield break;
}
if (trainerResult.ProgressiveProbabilities == null)
{
this.telemetry.TrackTrace($"Received invalid data: trainerResult.Probabilities is null");
yield break;
}
var pi_a_x = trainerResult.ProgressiveProbabilities[trainerResult.Label.Action - 1];
var p_a_x = trainerResult.Label.Probability * (1 - trainerResult.ProbabilityOfDrop);
// the latest one we're currently training
yield return new EvalEventData
{
Name = "Latest Policy",
// calcuate expectation under current randomized policy (using current exploration strategy)
// VW action is 0-based, label Action is 1 based
Cost = (trainerResult.Label.Cost * pi_a_x) / p_a_x,
Probability = pi_a_x / p_a_x
};
// the one currently running
yield return new EvalEventData
{
Name = "Deployed Policy",
Cost = trainerResult.Label.Cost,
Probability = 1 // for deployed policy just use the observed cost
};
// Default = choosing the action that's supplied by caller
yield return new EvalEventData
{
Name = "Default Policy",
Cost = VowpalWabbitContextualBanditUtil.GetUnbiasedCost(trainerResult.Label.Action, (uint)1, trainerResult.Label.Cost, trainerResult.Label.Probability),
Probability = trainerResult.Label.Action == 1 ? 1 / (trainerResult.ObservedProbabilities[0] * (1 - trainerResult.ProbabilityOfDrop)) : 0
};
// per action tag policies
for (int action = 1; action <= trainerResult.ProgressiveRanking.Length; action++)
{
string tag;
if (!trainerResult.ActionsTags.TryGetValue(action, out tag))
tag = action.ToString(CultureInfo.InvariantCulture);
var name = $"Constant Policy {tag}";
yield return new EvalEventData
{
Name = name,
Cost = VowpalWabbitContextualBanditUtil.GetUnbiasedCost(trainerResult.Label.Action, (uint)action, trainerResult.Label.Cost, trainerResult.Label.Probability),
Probability = trainerResult.Label.Action == action ? 1 / (trainerResult.ObservedProbabilities[action - 1] * (1 - trainerResult.ProbabilityOfDrop)) : 0
};
}
}
private void UploadEvaluation(IList<EvalData> batch)
{
try
{
this.performanceCounters.Stage4_Evaluation_Total.Increment();
this.performanceCounters.Stage4_Evaluation_BatchesPerSec.Increment();
// construct multi-line JSON
// TODO: check on how we batch in client library, we should use the EventId
var eventData = new EventData(Encoding.UTF8.GetBytes(string.Join("\n", batch.Select(b => b.JSON))))
{
PartitionKey = batch.First().PartitionKey
};
this.evalEventHubClient.Send(eventData);
}
catch (Exception e)
{
this.telemetry.TrackException(e);
}
}
public void Dispose()
{
if (this.evalBlock != null)
{
this.evalBlock.Complete();
this.evalBlock.Completion.Wait(TimeSpan.FromMinutes(1));
this.evalBlock = null;
}
if (this.evalBlockDisposable != null)
{
this.evalBlockDisposable.Dispose();
this.evalBlockDisposable = null;
}
}
}
}