Repository navigation
Expand file tree
/
Copy pathSkillOptOptimizer.cs
More file actions
258 lines (235 loc) · 13.6 KB
/
Copy pathSkillOptOptimizer.cs
File metadata and controls
258 lines (235 loc) · 13.6 KB
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
using System.Text.Json;
using Microsoft.Extensions.AI;
namespace ManagedCode.SkillOpt;
/// <summary>Runs SkillOpt's text-space optimization loop using caller-supplied Microsoft clients.</summary>
public static partial class SkillOptOptimizer
{
/// <summary>Optimizes one skill and returns the best skill plus held-out evidence.</summary>
public static async Task<SkillOptResult> OptimizeAsync(SkillOptRequest request, CancellationToken cancellationToken = default)
{
Validate(request);
var options = request.Options;
var fingerprint = OptimizationHelpers.Fingerprint(request);
var state = request.ResumeState ?? await InitializeAsync(request, fingerprint, cancellationToken);
ValidateResume(state, request, fingerprint);
await SaveExternalCheckpointAsync(request, state, cancellationToken);
var history = state.History.ToList();
var rejected = state.RejectedEdits.ToList();
var counters = new RunCounters(state.RolloutsUsed, state.OptimizerCallsUsed, state.EvaluationCallsUsed);
var currentSkill = state.CurrentSkill;
var bestSkill = state.BestSkill;
var currentScore = state.CurrentScore;
var bestScore = state.BestScore;
var bestStep = state.BestStep;
var metaSkill = state.MetaSkill;
var completedSteps = state.CompletedSteps;
var completedEpochs = state.CompletedEpochs;
var phase = state.Phase;
var epochStartSkill = state.EpochStartSkill;
var resumeSafe = state.IsResumeSafe;
async ValueTask SaveCheckpointAsync(bool? safe = null)
{
if (safe.HasValue)
{
resumeSafe = safe.Value;
}
await SaveExternalCheckpointAsync(request, CreateRunState(options, currentSkill,
bestSkill, currentScore, bestScore, bestStep, metaSkill, rejected, history,
completedSteps, completedEpochs, phase, epochStartSkill, counters, fingerprint, resumeSafe), cancellationToken);
}
counters.Checkpoint = async token => await SaveCheckpointAsync(false);
async Task CompleteEpochBoundaryAsync()
{
if (phase == SkillOptRunPhase.EpochMetaUpdate)
{
var memoryResponse = await CallOptimizerAsync(request, counters,
OptimizationHelpers.Prompt("meta"), FormatEpochMemory(history, metaSkill), cancellationToken);
if (!string.IsNullOrWhiteSpace(memoryResponse))
{
metaSkill = memoryResponse.Trim();
}
phase = SkillOptRunPhase.EpochSlowUpdate;
await SaveCheckpointAsync(true);
}
if (phase == SkillOptRunPhase.EpochSlowUpdate)
{
var slowPatch = await CallOptimizerAsync(request, counters,
OptimizationHelpers.Prompt("slow_update"), FormatSlowUpdate(epochStartSkill,
currentSkill, history.Where(record => record.Epoch == completedEpochs).ToArray(), metaSkill),
cancellationToken);
var slowUpdate = await ApplySlowUpdateAsync(request, slowPatch, epochStartSkill,
currentSkill, currentScore, bestSkill, bestScore, bestStep, completedSteps,
rejected, history, counters, cancellationToken);
currentSkill = slowUpdate.CurrentSkill;
currentScore = slowUpdate.CurrentScore;
bestSkill = slowUpdate.BestSkill;
bestScore = slowUpdate.BestScore;
bestStep = slowUpdate.BestStep;
phase = SkillOptRunPhase.Training;
epochStartSkill = currentSkill;
await SaveCheckpointAsync(true);
}
}
if (phase is SkillOptRunPhase.EpochMetaUpdate or SkillOptRunPhase.EpochSlowUpdate)
{
await CompleteEpochBoundaryAsync();
}
for (var epoch = completedEpochs; epoch < options.Epochs; epoch++)
{
var epochStart = currentSkill;
var firstStep = epoch == completedEpochs ? completedSteps % options.StepsPerEpoch : 0;
for (var stepInEpoch = firstStep; stepInEpoch < options.StepsPerEpoch; stepInEpoch++)
{
cancellationToken.ThrowIfCancellationRequested();
var step = completedSteps + 1;
var trainCases = SelectBatch(request.TrainingCases, options.BatchSize,
options.RandomSeed + step);
var beforeScore = currentScore;
var observations = await EvaluateCasesAsync(request, trainCases, currentSkill,
counters, cancellationToken);
var rolloutScores = observations.Select(static observation => observation.Score).ToArray();
var outcomeScores = trainCases.Select((item, index) =>
(item, observation: observations[index])).ToArray();
var failures = outcomeScores.Where(static row => row.observation.Score < 1).ToArray();
var successes = outcomeScores.Where(static row => row.observation.Score >= 1).ToArray();
var edits = new List<SkillOptEdit>();
var reasoning = "";
if (options.UpdateMode == SkillUpdateMode.FullRewrite)
{
var response = await CallOptimizerAsync(request, counters, OptimizationHelpers.Prompt("rewrite"),
FormatTrajectories(outcomeScores.Select(static row => (row.item, row.observation)), currentSkill), cancellationToken);
var replacement = response.Trim();
if (replacement.StartsWith("```", StringComparison.Ordinal))
{
replacement = StripCodeFence(replacement);
}
if (replacement.Length > 0)
{
var selectionReport = await EvaluateSplitReportAsync(request, request.SelectionCases,
replacement, "selection", counters, cancellationToken);
var selectedScore = selectionReport.MeanScore!.Value;
var action = Gate(replacement, selectedScore, ref currentSkill, ref currentScore,
ref bestSkill, ref bestScore, ref bestStep, step, options,
request.CandidateGate, selectionReport);
if (action.StartsWith("reject", StringComparison.Ordinal))
{
RememberRejected(rejected, currentSkill, replacement, options.MaxRejectedEdits);
}
history.Add(new SkillOptStepRecord(step, epoch + 1, trainCases.Length,
failures.Length, successes.Length, 0, beforeScore, selectedScore, action, 0,
SummarizeFailures(outcomeScores), "Full skill rewrite"));
}
}
else
{
var failurePatches = await ReflectAsync(request, failures, currentSkill,
SkillOptOutcome.Failure, metaSkill, rejected, counters, cancellationToken);
var successPatches = options.FailureOnly ? [] : await ReflectAsync(request,
successes, currentSkill, SkillOptOutcome.Success, metaSkill, rejected,
counters, cancellationToken);
var allPatches = failurePatches.Concat(successPatches).ToList();
if (allPatches.Count > 0)
{
var aggregate = await AggregateAsync(request, currentSkill, allPatches,
metaSkill, counters, cancellationToken);
reasoning = aggregate.Reasoning;
var budget = GetEditBudget(options, step, options.Epochs * options.StepsPerEpoch);
var selected = await SelectEditsAsync(request, currentSkill, aggregate.Edits,
budget, metaSkill, counters, cancellationToken);
var candidate = OptimizationHelpers.ApplyEdits(currentSkill, selected, out var applied);
if (options.UpdateMode == SkillUpdateMode.RewriteFromSuggestions && applied > 0)
{
candidate = await CallOptimizerAsync(request, counters,
OptimizationHelpers.Prompt("rewrite_suggestions"),
$"## Current Skill\n{currentSkill}\n\n## Selected Suggestions\n{JsonSerializer.Serialize(selected)}\n\n" + metaSkill,
cancellationToken);
}
if (applied > 0 && !string.Equals(candidate, currentSkill, StringComparison.Ordinal))
{
var selectionReport = await EvaluateSplitReportAsync(request,
request.SelectionCases, candidate, "selection", counters, cancellationToken);
var selectedScore = selectionReport.MeanScore!.Value;
var action = Gate(candidate, selectedScore, ref currentSkill, ref currentScore,
ref bestSkill, ref bestScore, ref bestStep, step, options,
request.CandidateGate, selectionReport);
if (action.StartsWith("reject", StringComparison.Ordinal))
{
RememberRejected(rejected, candidate, currentSkill, options.MaxRejectedEdits);
}
history.Add(new SkillOptStepRecord(step, epoch + 1, trainCases.Length,
failures.Length, successes.Length, budget, beforeScore, selectedScore,
action, applied, SummarizeFailures(outcomeScores), reasoning));
}
else
{
history.Add(new SkillOptStepRecord(step, epoch + 1, trainCases.Length,
failures.Length, successes.Length, budget, beforeScore, beforeScore,
"skip_no_patches", 0, SummarizeFailures(outcomeScores), reasoning));
}
}
else
{
history.Add(new SkillOptStepRecord(step, epoch + 1, trainCases.Length,
failures.Length, successes.Length, GetEditBudget(options, step,
options.Epochs * options.StepsPerEpoch), beforeScore, beforeScore,
"skip_no_patches", 0, SummarizeFailures(outcomeScores), "No usable edits."));
}
}
completedSteps++;
completedEpochs = completedSteps / options.StepsPerEpoch;
var completedEpoch = completedSteps % options.StepsPerEpoch == 0;
phase = completedEpoch && completedEpochs >= 2 && !string.Equals(epochStartSkill, currentSkill, StringComparison.Ordinal)
? SkillOptRunPhase.EpochMetaUpdate
: SkillOptRunPhase.Training;
if (completedEpoch && phase == SkillOptRunPhase.Training)
{
epochStartSkill = currentSkill;
}
await SaveCheckpointAsync(true);
}
completedEpochs = epoch + 1;
if (phase is SkillOptRunPhase.EpochMetaUpdate or SkillOptRunPhase.EpochSlowUpdate)
{
await CompleteEpochBoundaryAsync();
}
}
phase = SkillOptRunPhase.FinalReporting;
await SaveCheckpointAsync(false);
var baselineTestCases = await EvaluateCasesAsync(request, request.TestCases,
request.InitialSkill, counters, cancellationToken);
var bestTestCases = await EvaluateCasesAsync(request, request.TestCases,
bestSkill, counters, cancellationToken);
var finalState = new SkillOptRunState
{
Seed = options.RandomSeed,
CompletedSteps = completedSteps,
CompletedEpochs = completedEpochs,
Phase = SkillOptRunPhase.Completed,
IsResumeSafe = true,
EpochStartSkill = currentSkill,
CurrentSkill = currentSkill,
BestSkill = bestSkill,
CurrentScore = currentScore,
BestScore = bestScore,
BestStep = bestStep,
MetaSkill = metaSkill,
RejectedEdits = rejected,
History = history,
RolloutsUsed = counters.Rollouts,
OptimizerCallsUsed = counters.OptimizerCalls,
EvaluationCallsUsed = counters.EvaluationCalls,
DatasetFingerprint = fingerprint
};
await SaveExternalCheckpointAsync(request, finalState, cancellationToken);
return new SkillOptResult(bestSkill, currentSkill,
OptimizationHelpers.Report("test-baseline", request.TestCases, baselineTestCases, options.ScoreMetricName),
OptimizationHelpers.Report("test-best", request.TestCases, bestTestCases, options.ScoreMetricName), finalState);
}
}
/// <summary>Thrown when an explicit optimization, rollout, or evaluation budget is exhausted.</summary>
public sealed class SkillOptBudgetExceededException(string budget)
: InvalidOperationException($"SkillOpt {budget} budget was exhausted.")
{
/// <summary>The budget category that prevented the next operation.</summary>
public string Budget { get; } = budget;
}