RotationScaleMerge: a scaling loop is stalled only when its steps stop falling, and ends on its best round (RESULT-CHANGING)
The stall rule ('step not below 0.9 x its best for five rounds') called a steady slow fall a stall -
82 of the 245 loops that ended unsettled in the rc176 full battery were still falling at < 2% a round -
and every unsettled loop returned its LAST state: a fulls loop that diverged from round 1 handed on
the state after five rounds of walking. Now a loop is stalled when the median of the pairwise slopes
of log(step) over its last six rounds (Theil-Sen) is not negative - a median, because a least-squares
slope let one round's jump (the smoothness re-chosen five decades away) end a loop falling by a
quarter a round - and however a loop stops (settled, stalled or at the cap) it ends on the plain round
with the smallest step. On a settled loop that is the last round, so nothing changes there; on any
other it is the state closest to a fixed point the loop reached. The partials' loop restores it
through the round's own rescale (ratio fitted G / best G), so the corr bookkeeping of the CPU and GPU
paths is unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PXc1zD1yxHqaR1DGdCTHL4
This commit is contained in:
1 parent
dd75c6eb2b
commit
b5d9488ceb
1 file changed
+57
-22
@@ -68,6 +68,10 @@ namespace {
|
||||
// The loops are accelerated only once their step is within this of the tolerance: Anderson mixing
|
||||
// models the map near its fixed point, and a loop still further out is as likely to be walking.
|
||||
constexpr double SCALING_ANDERSON_ONSET = 10.0 * SCALING_TOLERANCE;
|
||||
// A scaling loop has stopped settling when the trend of log(step) over its last this many rounds is
|
||||
// not falling (StepsStoppedFalling): a steady slow fall is a loop still contracting, a flat or
|
||||
// rising trend is not.
|
||||
constexpr int SCALING_STALL_WINDOW = 6;
|
||||
// The per-frame scale is smoothed over at least this many rocking curves (see Run).
|
||||
constexpr int SMOOTH_G_MIN_ROCKING_EVENTS = 6;
|
||||
|
||||
@@ -304,6 +308,21 @@ namespace {
|
||||
std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end());
|
||||
return v[v.size() / 2];
|
||||
}
|
||||
|
||||
// Whether the steps of a scaling loop have stopped falling: the median of the pairwise slopes of
|
||||
// log(step) over the last SCALING_STALL_WINDOW rounds (Theil-Sen) is not negative. A median, so that
|
||||
// one round's jump - the smoothness re-chosen five decades away, on a loop that was falling by a
|
||||
// quarter a round - does not end a loop that is still contracting.
|
||||
bool StepsStoppedFalling(const std::vector<double> &steps) {
|
||||
const int n = SCALING_STALL_WINDOW;
|
||||
if (static_cast<int>(steps.size()) < n) return false;
|
||||
const double *s = steps.data() + steps.size() - n;
|
||||
std::vector<double> slopes;
|
||||
for (int i = 0; i < n; ++i)
|
||||
for (int j = i + 1; j < n; ++j)
|
||||
slopes.push_back(std::log(s[j] / s[i]) / (j - i));
|
||||
return median_of(slopes) >= 0.0;
|
||||
}
|
||||
}
|
||||
|
||||
RotationScaleMerge::RotationScaleMerge(const DiffractionExperiment &experiment,
|
||||
@@ -1867,10 +1886,14 @@ RotationScaleMerge::ScalingLoopOutcome RotationScaleMerge::RunScalingLoop(
|
||||
//
|
||||
// The loop stops when the relative scales stop moving - the rms of |log(G_new/G_old)| over the
|
||||
// frames, each weighted by its observation count and nothing else (a weight that vanishes with G
|
||||
// is what let a collapsing sweep report itself settled) - or when they stop settling: a step that
|
||||
// has not fallen below nine tenths of its smallest value for five rounds is a loop on a flat or
|
||||
// rising valley, and more rounds of it can only walk further. Both leave the frames where they are
|
||||
// and say so. The step is the residual of the round, F(x) - x at the state the round started from,
|
||||
// is what let a collapsing sweep report itself settled) - or when they stop settling: steps that
|
||||
// have not fallen over the last SCALING_STALL_WINDOW rounds, by the median trend of their logarithm,
|
||||
// are a loop on a flat or rising valley, and more rounds of it can only walk further. A steady slow
|
||||
// fall is not a stall (a rule of 'not below nine tenths of the best for five rounds' stopped a third
|
||||
// of the unsettled loops of one battery while they were still falling at 2% a round). However the
|
||||
// loop stops, it ends on the plain round with the smallest step - on a settled loop that is the
|
||||
// last round, on any other the state closest to a fixed point it reached, not wherever its walk
|
||||
// had got to. The step is the residual of the round, F(x) - x at the state the round started from,
|
||||
// so the test reads the same whether that state was a plain round's or an accelerated one.
|
||||
ScalingLoopOutcome out;
|
||||
std::vector<uint8_t> scaled_any(n_frames, 0);
|
||||
@@ -1878,7 +1901,7 @@ RotationScaleMerge::ScalingLoopOutcome RotationScaleMerge::RunScalingLoop(
|
||||
std::vector<int64_t> n_obs(n_frames, 0);
|
||||
std::vector<double> ratio(n_frames, 1.0);
|
||||
double best_step = INFINITY;
|
||||
int rounds_since_best = 0;
|
||||
std::vector<double> steps, g_best;
|
||||
AndersonMixing anderson(SCALING_ANDERSON_DEPTH);
|
||||
std::vector<double> log_g_prev(n_frames), log_g(n_frames), weight(n_frames);
|
||||
for (int it = 0; it < scaling_iter; ++it) {
|
||||
@@ -1939,17 +1962,22 @@ RotationScaleMerge::ScalingLoopOutcome RotationScaleMerge::RunScalingLoop(
|
||||
logger->Debug("Per-frame scaling ({}): iteration {}, rms |dlogG| {:.2e} over {} frames, "
|
||||
"gauge {:.4f}, {} restarts{}", what, it + 1, out.step, n, g_ref, anderson.Restarts(),
|
||||
anderson.Stopped() ? ", plain" : "");
|
||||
steps.push_back(out.step);
|
||||
if (out.step < best_step) {
|
||||
best_step = out.step;
|
||||
g_best = g;
|
||||
}
|
||||
if (out.step < SCALING_TOLERANCE) {
|
||||
out.converged = true;
|
||||
stop = true;
|
||||
} else if (out.step < 0.9 * best_step) {
|
||||
best_step = out.step;
|
||||
rounds_since_best = 0;
|
||||
} else if (++rounds_since_best >= 5) {
|
||||
} else if (StepsStoppedFalling(steps)) {
|
||||
out.stalled = true;
|
||||
stop = true;
|
||||
}
|
||||
if (!stop && out.step < SCALING_ANDERSON_ONSET) {
|
||||
if (stop || it + 1 == scaling_iter) {
|
||||
for (int f = 0; f < n_frames; ++f)
|
||||
if (usable[f]) g[f] = g_best[f];
|
||||
} else if (out.step < SCALING_ANDERSON_ONSET) {
|
||||
// The next round starts from the accelerated scales, gauge-pinned again.
|
||||
const std::vector<double> next = anderson.Next(log_g_prev, log_g, weight);
|
||||
for (int f = 0; f < n_frames; ++f)
|
||||
@@ -1977,14 +2005,16 @@ void RotationScaleMerge::LogScalingOutcome(const char *what, const ScalingLoopOu
|
||||
out.iterations, out.step);
|
||||
else if (out.stalled)
|
||||
logger->Warning("Per-frame scaling ({}): stopped after {} iterations - the scales stopped settling "
|
||||
"(rms |dlogG| {:.1e}, not below {:.1e} for five rounds); more rounds would only walk "
|
||||
"them, so everything read off this merge is read off an unsettled state", what,
|
||||
out.iterations, out.step, out.best_step);
|
||||
"(rms |dlogG| {:.1e}, not falling over the last {} rounds); they are left at the "
|
||||
"round with the smallest step ({:.1e}), and everything read off this merge is read "
|
||||
"off an unsettled state", what, out.iterations, out.step, SCALING_STALL_WINDOW,
|
||||
out.best_step);
|
||||
else
|
||||
logger->Warning("Per-frame scaling ({}): NOT settled after the cap of {} iterations - the scales "
|
||||
"were still moving by rms |dlogG| {:.1e} (tolerance {:.0e}); everything read off "
|
||||
"this merge is read off an unsettled state", what, out.iterations, out.step,
|
||||
SCALING_TOLERANCE);
|
||||
"were still moving by rms |dlogG| {:.1e} (tolerance {:.0e}); they are left at the "
|
||||
"round with the smallest step ({:.1e}), and everything read off this merge is read "
|
||||
"off an unsettled state", what, out.iterations, out.step, SCALING_TOLERANCE,
|
||||
out.best_step);
|
||||
}
|
||||
|
||||
RotationScaleMerge::ScalingLoopOutcome RotationScaleMerge::RunFullsScalingLoop(
|
||||
@@ -2028,7 +2058,7 @@ RotationScaleMerge::ScalingLoopOutcome RotationScaleMerge::RunFullsScalingLoop(
|
||||
}
|
||||
double best_step = INFINITY, lambda = 0.0;
|
||||
bool lambda_kept = false;
|
||||
int rounds_since_best = 0;
|
||||
std::vector<double> steps, g_best;
|
||||
AndersonMixing anderson(SCALING_ANDERSON_DEPTH);
|
||||
std::vector<double> log_g(n_frames), log_g_new(n_frames), weight(n_frames);
|
||||
for (int it = 0; it < scaling_iter; ++it) {
|
||||
@@ -2072,18 +2102,23 @@ RotationScaleMerge::ScalingLoopOutcome RotationScaleMerge::RunFullsScalingLoop(
|
||||
"{} restarts{}", it + 1, out.step, lambda, g_ref, anderson.Restarts(),
|
||||
anderson.Stopped() ? ", plain" : "");
|
||||
if (out.step < SCALING_ANDERSON_ONSET) lambda_kept = true;
|
||||
// Stopped and ended on the best round as RunScalingLoop's loop is.
|
||||
steps.push_back(out.step);
|
||||
if (out.step < best_step) {
|
||||
best_step = out.step;
|
||||
g_best = g_new;
|
||||
}
|
||||
bool stop = false;
|
||||
if (out.step < SCALING_TOLERANCE) {
|
||||
out.converged = true;
|
||||
stop = true;
|
||||
} else if (out.step < 0.9 * best_step) {
|
||||
best_step = out.step;
|
||||
rounds_since_best = 0;
|
||||
} else if (++rounds_since_best >= 5) {
|
||||
} else if (StepsStoppedFalling(steps)) {
|
||||
out.stalled = true;
|
||||
stop = true;
|
||||
}
|
||||
if (!stop && out.step < SCALING_ANDERSON_ONSET) {
|
||||
if (stop || it + 1 == scaling_iter) {
|
||||
g_new = g_best;
|
||||
} else if (out.step < SCALING_ANDERSON_ONSET) {
|
||||
// The next round starts from the accelerated scales, gauge-pinned again.
|
||||
const std::vector<double> next = anderson.Next(log_g, log_g_new, weight);
|
||||
for (int f = 0; f < n_frames; ++f)
|
||||
|
||||
Reference in new issue
Block a user