RotationScaleMerge: the CPU partial scaling loop works on a compact copy of the partials
Each iteration of the partial scaling loop passes over all partials three times (group means, per-frame G fit, corr update + rescale), each time reading a few fields of the 80-byte Obs, and the group means gathered whole Obs records through the group permutation. The loop now works on ScalingObs (frame, group, I, sigma, partiality, prescaling_corr, zeta, on_ice; 36 bytes, partials' order) with corr in its own float array, and the group means read ScalingRef (I, sigma, partiality) laid out in the CSR's group order, so only corr is gathered. corr is written back to the partials (and kept by the memo) when the loop ends. ReferenceWeight takes the fields, FitPerFrameG is a template over Obs/ScalingObs; the same values, so the same arithmetic in the same order. Transient memory: 52 bytes per partial during the loop (~0.4 GB on 8M partials). Measured (perf, 499 Hz, CPU-only build, cytc): update/rescale lambda 104 -> 35, group means 87 -> 24, FitPerFrameG 63 -> 27 core-s (-167 core-s, of which the copy costs ~10). CPU-only path; the GPU scaling path is untouched. Byte-identical p.hkl, p.mtz, p_P1.mtz, p_unmerged.mtz on myob, cytc, lyso, sparse (CPU) and myob, lyso (GPU); rotation scale/merge tests pass. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01D1G8gJVAy6gp1K5Dz3NE5C
This commit is contained in:
1 parent
59c4d6fe41
commit
2bad92ddd6
2 files changed
+92
-23
No files matched your search
@@ -1336,12 +1336,13 @@ int RotationScaleMerge::ComputeAsuGroups(const HKLKeyGenerator &keygen) {
|
||||
return n_groups;
|
||||
}
|
||||
|
||||
double RotationScaleMerge::ReferenceWeight(const Obs &o, double min_partiality, float &I_corr) {
|
||||
if (o.group < 0) return 0.0;
|
||||
if (!(o.corr > 0.0f) || !std::isfinite(o.corr)) return 0.0;
|
||||
if (o.partiality < min_partiality) return 0.0;
|
||||
const float Ic = o.I * o.corr;
|
||||
const float sigma_corr = o.sigma * o.corr;
|
||||
double RotationScaleMerge::ReferenceWeight(int32_t group, float corr, float partiality, float I, float sigma,
|
||||
double min_partiality, float &I_corr) {
|
||||
if (group < 0) return 0.0;
|
||||
if (!(corr > 0.0f) || !std::isfinite(corr)) return 0.0;
|
||||
if (partiality < min_partiality) return 0.0;
|
||||
const float Ic = I * corr;
|
||||
const float sigma_corr = sigma * corr;
|
||||
if (!std::isfinite(Ic) || !std::isfinite(sigma_corr) || sigma_corr <= 0.0f) return 0.0;
|
||||
I_corr = Ic;
|
||||
return 1.0 / (static_cast<double>(sigma_corr) * sigma_corr);
|
||||
@@ -1383,7 +1384,30 @@ void RotationScaleMerge::ReducePartialGroupMeans(int n_groups, std::vector<doubl
|
||||
});
|
||||
}
|
||||
|
||||
void RotationScaleMerge::FitPerFrameG(std::vector<Obs> &obs, const std::vector<int32_t> &fstart,
|
||||
void RotationScaleMerge::ReduceScalingGroupMeans(int n_groups, const std::vector<ScalingRef> &refs,
|
||||
const std::vector<float> &corr,
|
||||
std::vector<double> &out_mean) const {
|
||||
out_mean.assign(n_groups, NAN);
|
||||
ParallelChunks(n_groups, ThreadsForWork(corr.size(), nthreads), [&](int glo, int ghi) {
|
||||
for (int g = glo; g < ghi; ++g) {
|
||||
double sw = 0.0, swI = 0.0;
|
||||
const int lo = partial_group_start[g], hi = lo + partial_group_count[g];
|
||||
for (int p = lo; p < hi; ++p) {
|
||||
const ScalingRef &r = refs[p];
|
||||
float I_corr = 0.0f;
|
||||
const double w = ReferenceWeight(g, corr[partial_group_perm[p]], r.partiality, r.I, r.sigma,
|
||||
min_partiality, I_corr);
|
||||
if (w <= 0.0) continue;
|
||||
sw += w;
|
||||
swI += w * I_corr;
|
||||
}
|
||||
if (sw > 0.0) out_mean[g] = swI / sw;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
template <class O>
|
||||
void RotationScaleMerge::FitPerFrameG(const std::vector<O> &obs, const std::vector<int32_t> &fstart,
|
||||
const std::vector<int32_t> &fcount,
|
||||
const std::vector<double> &group_mean_in,
|
||||
bool unity, std::vector<double> &g) {
|
||||
@@ -5560,6 +5584,30 @@ RotationScaleMerge::Result RotationScaleMerge::Run(bool for_search, bool full_st
|
||||
logger.Info("Per-frame scaling (partials): the same grouping and settings as an earlier merge, "
|
||||
"so its {} iterations stand", partial_loop.iterations);
|
||||
} else {
|
||||
// The loop works on its own compact copy of the partials (ScalingObs in the header) and hands
|
||||
// corr back to them when it is done.
|
||||
const int n_partials = static_cast<int>(partials.size());
|
||||
const size_t partial_threads = ThreadsForWork(partials.size(), nthreads);
|
||||
std::vector<ScalingObs> scaling_obs(n_partials);
|
||||
std::vector<float> scaling_corr(n_partials);
|
||||
ParallelChunks(n_partials, partial_threads, [&](int lo, int hi) {
|
||||
for (int i = lo; i < hi; ++i) {
|
||||
const Obs &o = partials[i];
|
||||
scaling_obs[i] = {o.frame, o.group, o.I, o.sigma, o.partiality, o.prescaling_corr, o.zeta,
|
||||
o.on_ice};
|
||||
scaling_corr[i] = o.corr;
|
||||
}
|
||||
});
|
||||
std::vector<ScalingRef> scaling_refs(partial_group_start.empty()
|
||||
? 0 : partial_group_start.back() + partial_group_count.back());
|
||||
ParallelChunks(n_groups, partial_threads, [&](int glo, int ghi) {
|
||||
for (int g = glo; g < ghi; ++g)
|
||||
for (int p = partial_group_start[g]; p < partial_group_start[g] + partial_group_count[g]; ++p) {
|
||||
const ScalingObs &o = scaling_obs[partial_group_perm[p]];
|
||||
scaling_refs[p] = {o.I, o.sigma, o.partiality};
|
||||
}
|
||||
});
|
||||
|
||||
// RunScalingLoop rescales after every iteration before anything reads corr, so the iteration's
|
||||
// UpdateCorr is folded into that pass over the partials: the G and the fitted frames as the
|
||||
// iteration left them, then the rescale on top - the same two roundings, one pass instead of two.
|
||||
@@ -5567,34 +5615,32 @@ RotationScaleMerge::Result RotationScaleMerge::Run(bool for_search, bool full_st
|
||||
std::vector<uint8_t> frame_fitted;
|
||||
partial_loop = RunScalingLoop("partials", g_partial, frame_count, smooth_window,
|
||||
/*release_restraint=*/false, [&] {
|
||||
ReducePartialGroupMeans(n_groups, partial_mean);
|
||||
FitPerFrameG(partials, frame_start, frame_count, partial_mean, /*unity=*/false, g_partial);
|
||||
ReduceScalingGroupMeans(n_groups, scaling_refs, scaling_corr, partial_mean);
|
||||
FitPerFrameG(scaling_obs, frame_start, frame_count, partial_mean, /*unity=*/false, g_partial);
|
||||
g_fitted = g_partial;
|
||||
frame_fitted = frame_scaled_scratch;
|
||||
}, [&](const std::vector<uint8_t> &apply, const std::vector<double> &ratio) {
|
||||
ParallelChunks(static_cast<int>(partials.size()), ThreadsForWork(partials.size(), nthreads),
|
||||
[&](int lo, int hi) {
|
||||
ParallelChunks(n_partials, partial_threads, [&](int lo, int hi) {
|
||||
for (int i = lo; i < hi; ++i) {
|
||||
Obs &o = partials[i];
|
||||
const ScalingObs &o = scaling_obs[i];
|
||||
float &corr = scaling_corr[i];
|
||||
if (frame_fitted[o.frame]) { // UpdateCorr
|
||||
const double denom = static_cast<double>(o.partiality) * g_fitted[o.frame];
|
||||
if (std::isfinite(o.prescaling_corr) && std::isfinite(denom) && denom > 0.0)
|
||||
o.corr = static_cast<float>(o.prescaling_corr / denom);
|
||||
corr = static_cast<float>(o.prescaling_corr / denom);
|
||||
else
|
||||
o.corr = NAN;
|
||||
corr = NAN;
|
||||
}
|
||||
if (apply[o.frame] && std::isfinite(o.corr))
|
||||
o.corr = static_cast<float>(o.corr * ratio[o.frame]);
|
||||
if (apply[o.frame] && std::isfinite(corr))
|
||||
corr = static_cast<float>(corr * ratio[o.frame]);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
PartialLoopMemo m{key, partial_loop, frame_scaled_scratch, g_partial, {}};
|
||||
m.corr.resize(partials.size());
|
||||
ParallelChunks(static_cast<int>(partials.size()), ThreadsForWork(partials.size(), nthreads),
|
||||
[&](int lo, int hi) {
|
||||
for (int i = lo; i < hi; ++i) m.corr[i] = partials[i].corr;
|
||||
ParallelChunks(n_partials, partial_threads, [&](int lo, int hi) {
|
||||
for (int i = lo; i < hi; ++i) partials[i].corr = scaling_corr[i];
|
||||
});
|
||||
PartialLoopMemo m{key, partial_loop, frame_scaled_scratch, g_partial, std::move(scaling_corr)};
|
||||
if (partial_loop_memos.size() == MAX_PARTIAL_LOOP_MEMOS)
|
||||
partial_loop_memos.erase(partial_loop_memos.begin());
|
||||
partial_loop_memos.push_back(std::move(m));
|
||||
|
||||
@@ -446,17 +446,40 @@ private:
|
||||
std::vector<int32_t> partial_group_start, partial_group_count;
|
||||
std::unique_ptr<int32_t[]> partial_group_perm;
|
||||
|
||||
// The CPU partial scaling loop's working copy of the partials. It passes over them several times
|
||||
// per iteration and reads a handful of fields each time, so it works on these rather than on the
|
||||
// eighty-byte Obs: ScalingObs holds, in the partials' own order, what FitPerFrameG and the corr
|
||||
// update read; ScalingRef holds, in the CSR's group order, what the group means read besides corr,
|
||||
// so that only corr is gathered. The same values, so the same arithmetic in the same order.
|
||||
struct ScalingObs {
|
||||
int32_t frame, group;
|
||||
float I, sigma, partiality, prescaling_corr, zeta;
|
||||
uint8_t on_ice;
|
||||
};
|
||||
struct ScalingRef {
|
||||
float I, sigma, partiality;
|
||||
};
|
||||
|
||||
// The observation's inverse-variance weight in the scaling reference, and its scaled intensity;
|
||||
// 0 when the observation is not in the reference at all.
|
||||
static double ReferenceWeight(const Obs &o, double min_partiality, float &I_corr);
|
||||
static double ReferenceWeight(int32_t group, float corr, float partiality, float I, float sigma,
|
||||
double min_partiality, float &I_corr);
|
||||
static double ReferenceWeight(const Obs &o, double min_partiality, float &I_corr) {
|
||||
return ReferenceWeight(o.group, o.corr, o.partiality, o.I, o.sigma, min_partiality, I_corr);
|
||||
}
|
||||
// Inverse-variance per-group mean of I*corr over `obs` (the merge reference).
|
||||
void ReduceGroupMeans(const std::vector<Obs> &obs, int n_groups, std::vector<double> &out_mean) const;
|
||||
// The same over the partials, using the CSR above - the same sums in the same order.
|
||||
void ReducePartialGroupMeans(int n_groups, std::vector<double> &out_mean) const;
|
||||
// The same again over the scaling loop's copy: refs in group order, corr in the partials' order.
|
||||
void ReduceScalingGroupMeans(int n_groups, const std::vector<ScalingRef> &refs,
|
||||
const std::vector<float> &corr, std::vector<double> &out_mean) const;
|
||||
|
||||
// Robust per-frame G fit (IRLS, Cauchy k=3), unity=false uses the rotation partiality, unity=true the
|
||||
// scale-fulls (partiality already folded in). Reads out_mean[group] as the reference intensity.
|
||||
void FitPerFrameG(std::vector<Obs> &obs, const std::vector<int32_t> &fstart,
|
||||
// O is Obs or ScalingObs.
|
||||
template <class O>
|
||||
void FitPerFrameG(const std::vector<O> &obs, const std::vector<int32_t> &fstart,
|
||||
const std::vector<int32_t> &fcount, const std::vector<double> &group_mean_in,
|
||||
bool unity, std::vector<double> &g);
|
||||
|
||||
|
||||
Reference in new issue
Block a user