Skip to content

A float32 correlation kernel in residual form for continuous pose refinement

Planned 2026-11-29; ruled by Hans 2026-11-30 (decisions 1-5, 7 and 8 as proposed, decision 6 replaced: ruling R1 in Progress); not launched. It builds on the float32 kernels run (float32_kernels_and_pcg_precond.md, run f32_pcg): its pixel table and array gather.

Base. Master d860f264e (2026-11-30): df7810e2f lands f32_pcg with the Apple-silicon read's gather fix and the PCG chain fix, d860f264e its skill lines. "What the code check found" was re-checked there on 2026-11-30.

Purpose (R1). The kernel is the CPU mirror of the numerics the GPU route will run, so the CPU path can test them at length before the integration (mlx_fortran_division_of_work.md). mlx-fortran's cartft toy (K5) computes the per-pixel terms in float32 with partials added in double on the host, and its correlation does not resolve a pose closer than about 1e-4 rad in float32 (a claim of that repository, not measured in SIMPLE: decision 5 measures it). It is adopted on its numerics, not on its speed.

1. Why

Continuous pose refinement (refine=cont, the pose_cont follow-up) spends most of its search in one kernel, pose_normal_terms in src/main/cftc/simple_cartft_calc.f90. The f32_pcg run gave the Euclidean objective a float32 version of that kernel: a pixel table per particle slot, an array gather, one vectorised pass whose float32 partial sums are added in double. It costs 18-24 % fewer thread-seconds per pixel-evaluation at 24 threads on the Dell and 23-29 % at one thread (measured, f32_pcg Phase 7). The correlation objective (objfun=cc) kept the double kernel, because it forms 1 - cc from large sums: near the optimum the difference cancels, and in float32 a pose stops resolving at about 1e-4 rad against 1e-7 for Euclid (measured on mlx-fortran's reference implementation, not on SIMPLE). The cc kernel is now the larger of the two: 13.1 % of the local regime's thread-seconds and 2.3 % of the capture regime's in cont_refine3D_bench on the Dell (measured, f32_pcg Phase 0 timer tree; Euclid 11.8 % and 2.0 %), 13.2-13.7 % of the local regime after Phase 7.

The residual form removes the cancellation without changing the objective. With unit-norm images x = X/|X| and m = M/|M| over the band (the Friedel mates and the DC pixel counted as the kernel counts them),

1 - cc = (1/2) |m - x|^2,

so the pass can work on the per-pixel residual r = m - x, which is small near the optimum, exactly as the Euclidean pass works on M - X. The gradient follows from dm = (dM - m Re)/|M|: g_a = Re/|M| - (1 - cc) Re/|M|, the second term small by its factor (1 - cc). The Gauss-Newton matrix is the same tangential form the double kernel builds (finalize_cc_normal_terms). What the per-pixel pass needs and the current layout does not give is |M|, the model's norm at the current pose, before the pass.

2. Contract

  • The objective does not change. The new kernel computes the same 1 - cc, gradient and Gauss-Newton matrix as pose_normal_terms_dp on a cc slot, to the tolerances of decision 4, with the same curvature_scale. The score (score), the statuses of cartft_pose_opt and the transaction are untouched.
  • What changes. The last bits of every cc continuous solve: its poses, scores and the iteration counts of its LM solves.
  • What stays byte-identical at one thread with a fixed seed. Every objfun=euclid path (the float32 Euclidean kernel is not edited), the polar matching, refine_shift (shift_normal_terms), the reference files and their materializer.
  • Thread identity. Partial sums are cut by the pixel table's order (blocks of a fixed count), never by the thread; a cc solve's result does not depend on the team.
  • Off-state. None: no parameter, no switch, no compile flag. pose_normal_terms_dp stays as the reference of the testers, as for Euclid. A kernel that fails decision 4 or 7 is restored.
  • Both architectures. Nothing is committed before the Dell and the Mac both pass decisions 4, 5 and 7.

3. Decisions (proposed 2026-11-29, ruled 2026-11-30; numbers tagged by origin)

  1. Norm first, by two passes over gathered arrays. The pass that needs |M| gathers the whole band first into arrays of the table's length (value and three derivatives), sums |M|^2 = sum |wp V|^2 as float32 partials per block of CART_EUCLID_BLOCK added in double (the GPU's form, R1: no per-pixel double), then runs the float32 residual pass over the stored arrays. The gather is the kernel's dominant cost, so it runs once. The arrays are allocatable locals of the call (heap, not stack: macOS gives secondary threads 512 KB), sized by the table: about 0.7 MB per thread at box_crop 256 (guessed, from about 22,600 half-plane pixels at shell 120). Rejected before the run: a norm carried from the previous evaluation (it changes the objective between evaluations, so the LM acceptance would compare two definitions).
  2. The pixel table for cc slots. fill_ptcl builds the table for cc slots too, from the unwhitened observation and transfer; no new field (the f32_pcg Phase 7 record: the table holds every input a cc pass reads). |X| is the slot's power, already computed.
  3. The pass. Terms in float32: r = m - x per pixel with m = wp V / |M|, x = X/|X|; the objective's sum (1/2) sum |r|^2, the gradient's sum Re and the model-gradient sum Re, the 15 Gauss-Newton entries and the two curvature-scale sums. Float32 partials per block of CART_EUCLID_BLOCK pixels added in double, as the Euclidean pass. The DC pixel, the Friedel factor, the normalisation and the finaliser stay double. A second routine beside euclid_pose_pass (the sums differ), not a branch inside it.
  4. Tolerances (decision 5 of f32_pcg, with its 2026-11-29 rulings). Each measure is calibrated on 20 seeds (lesson of the PCG inner product and of the Euclidean gradient, where one calibration seed was too tight for float32's seed-to-seed spread), the tolerance twice their largest error, capped at its ceiling, frozen in Progress, then judged on the tester's two judged seeds; a miss there restores the kernel. Ceilings: the objective's error 1e-7 absolute on 1 - cc (guessed: Euclid's measured objective error is 2-4e-8 relative); the gradient as the Gauss-Newton step it moves, max |H^-1 (g_sp - g_dp)|, 1e-7 radians or pixels at every pose including the truth (ruled for Euclid 2026-11-29: the float32 Euclidean pose resolution); the Gauss-Newton matrix 4e-6 of its diagonal (as for Euclid).
  5. Negative control (pre-registered). In Phase 0, a scratch patch computes the cc kernel naively in float32 (1 - cc from float32 partial sums of the cross term and the power) and runs decision 4's step measure on it. It must fail the step ceiling at the truth pose, by at least a factor of 10 (guessed, from mlx-fortran's 1e-4 against 1e-7). If it passes, the premise does not hold for SIMPLE's operator: stop, and the plan reduces to the naive form, which the maintainer rules on.
  6. Speed, reported, not gated (R1). On cont_refine3D_bench objfun=cc, the search's thread-nanoseconds per pixel-evaluation in the local and capture regimes, at the full team on the Dell (24 threads) and on the Mac (its core count), six interleaved rounds each way, and at one pinned thread, beside the A-A spread. A slower kernel is kept and its cost stated in the report. Proposed before the ruling: a 10 % bar in the local regime.
  7. Behaviour. lib_cart_align3D (cc at 8 and 4 Angstrom, gated on the truth) and cont_refine3D_1jxy (its stage b is objfun=cc: median rotation error 0.229 degrees on the base, floor 1.5) pass on both machines; on cont_refine3D_bench objfun=cc, seeds 0-2, every solve commits as on the base and the passes per solve stay within the base's range over those seeds (measured in Phase 0).
  8. No experimental data. The run reads none; the maintainer's bgal or PfCRT read under objfun=cc follows the commit.

Defaults taken without a question: the routine names pose_normal_terms_cc_sp and cc_pose_pass; the block is CART_EUCLID_BLOCK (renamed CART_POSE_BLOCK if both kernels use it); the tester extends test_euclid_kernel_single_precision's fixture rather than copying it.

4. Not in this plan

The Euclidean kernel and its tester's tolerances; shift_normal_terms and refine_shift; the polar cc kernels of pftc; the reference layout; a carried norm (decision 1); any new parameter. Every file not in the table.

5. What the code check found (at the base)

  • pose_normal_terms dispatches on the slot's objective: Euclid to pose_normal_terms_sp, cc to pose_normal_terms_dp, which accumulates prediction_power, cross_real, model_gradient and the gradient against the observation, then finalize_cc_normal_terms forms 1 - cc as 1 - cross_real/sqrt(P_X P_M).
  • The pixel table (tab_h, tab_k, tab_shell, tab_fh, tab_fk, tab_observed, tab_transfer) is built in fill_ptcl for Euclidean slots only.
  • The double kernel's per-point gather is gather_packed_bspline3_window_grad again (restored at the Apple-silicon read, a thin public form over the same private body as the array gather). On the Mac the cc route's local regime is then master's (+1.0 %) and its capture regime 10 % slower per pixel (measured 2026-11-30): the body inlines into neither gather, so a point costs one more call than on master. Phase 0 measures this base, not master.
  • The float32 Euclidean tester judges the gradient as a Gauss-Newton step (H^-1 dg via matinv), tolerance 1e-7.

6. Change types and gates

phase type gate
0 measurement builds, fast gate, the base's cc A-A on both machines, the negative control
1 tests and a new kernel, not dispatched decision 4 on the tester; Euclid byte-identical
2 result-changing (cc only) decision 7 on both machines, decision 6 reported; Euclid byte-identical
3 documents the report

7. Rules and stops

  • Common rules of f32_pcg section 7: no new parameter, no relaxed tolerance without a ruling, a file-table row before an edit, experimental variants as patches under scratch, !$omp simd loops checked vectorised in a scratch build before timing.
  • Stops: the negative control passes (decision 5); an Euclidean output changes; a file outside the table; the base fails decision 7 on either machine.
  • Gates no ruling may waive: Euclid byte identity, cont_refine3D_1jxy, lib_cart_align3D, both architectures.

8. Phases

Phase 0: the base and the control

On the Dell and the Mac: Release build, fast gate; cont_refine3D_bench objfun=cc at the full team on seeds 0-2 and seed 0 twice (A-A), at one pinned thread with nptcls=500 twice; lib_cart_align3D; cont_refine3D_1jxy. The negative control of decision 5 on the Mac and the Dell (local_inplace_preserved_update_history passes at the base since df7810e2f). Exit: the A-A spreads, the passes per solve per seed, the control's step error at the truth pose, in Progress.

Phase 1: the kernel and its tester

The table for cc slots (decision 2), pose_normal_terms_cc_sp with cc_pose_pass (decisions 1 and 3), a tester check beside the Euclidean one (objective, step, Gauss-Newton matrix against pose_normal_terms_dp on cc slots, the production block and three others, the 20-seed calibration in a scratch run, then the two judged seeds). Not dispatched yet. Exit: decision 4 met on both machines; unit_cart_align3D passes; the Euclidean benchmark's poses and metrics byte-identical to the base at seed 0.

Phase 2: dispatch and judgement

pose_normal_terms dispatches cc slots to the new kernel. Decision 6 measured on both machines (frozen builds, interleaved, the load beside each number); decision 7. Exit: decision 7 met and decision 6 recorded, or the kernel restored and recorded with its numbers.

Phase 3: documents and report

refine3D_policy.md (the continuous kernel's precision paragraph), the skill line proposed for simple-main-cftc, the README row, the report beside this plan (doc/how2s/how2write_agent_plans.md section 8) with a section "For mlx-fortran" (mlx_fortran_division_of_work.md section 5): the residual form, the routine, the commit, the control's and the kernel's step errors at the truth pose. Exit: the report.

File table

file what phases
src/main/cftc/simple_cartft_calc.f90 the table for cc slots; pose_normal_terms_cc_sp, cc_pose_pass; the dispatch 1, 2
src/main/cftc/simple_cartft_calc_tester.f90 the kernel's check against the double kernel 1
src/main/commanders/test/simple_commanders_test_highlevel_cont.f90 the benchmark's rows, if needed 0, 2
doc/policies/3D/refine3D_policy.md the continuous kernel's precision 3
doc/implementation_notes/README.md the row 3
doc/implementation_notes/planned/cont_cc_residual_kernel.md Progress each
doc/implementation_notes/planned/cont_cc_residual_kernel_report.md the report 3

Progress

  • Rulings (Hans, 2026-11-30, before launch). Decisions 1-5, 7 and 8 adopted as proposed. R1, on decision 6: "I think it is important for robustness testing that we mirror the numerics that we will have in place on the GPU side of things, so that we can test it extensively before integration using the CPU code path." Taken as: the kernel's purpose is the GPU's numerics on the CPU path; speed is reported, not gated (decision 6); the norm's sum takes the GPU's form, float32 partials added in double (decision 1); the report carries the form back to mlx-fortran (Phase 3). Base re-pinned to d860f264e.