Skip to content

Fix non-deterministic gvar compilation#1960

Merged
anthrotype merged 4 commits intomainfrom
fix-1873-deterministic-gvar
Apr 16, 2026
Merged

Fix non-deterministic gvar compilation#1960
anthrotype merged 4 commits intomainfrom
fix-1873-deterministic-gvar

Conversation

@anthrotype
Copy link
Copy Markdown
Member

Fixes #1873

ensure_composite_defined_at_component_locations and ensure_component_has_consistent_layers incrementally inserted interpolated instances into a glyph's source map, mutating the VariationModel between iterations. Combined with non-deterministic HashMap iteration order, this produced different interpolation results across builds.

The fix is to collect all missing locations first, interpolate all from the unmodified source map, and insert them at the end after the loop. Each interpolation now sees the same VariationModel regardless of processing order.

I derived two reproducers from Georama's topology (4 corner masters + 5 intermediates including an interior off-axis location) that verify each interpolated instance returned from these functions matches an independent interpolate_instance call on the original glyph.

Note that because of the non-determinism, the test may pass the first time the CI runs if the random iteration order happens to be the lucky one.

I also confirmed that Georama deterministically builds the same gvar after the fix.

Derived from Georama's uni03060300 (breve + grave combining mark).
The composite has 4 corner masters; its component uni0306 has 5
intermediate layers (4 on-axis edges + 1 interior at wght=0.5,
wdth=0.4). When edge intermediates are inserted incrementally before
the interior point, they narrow the VariationModel's support regions,
causing the interior interpolation to produce a mathematically
different result (up to ~19 units off).

The test verifies that ensure_composite_defined_at_component_locations
produces results matching independent interpolation from the original
(unmodified) model at every missing location.

If you are lucky and the test passes, that is still the non-determinism
at play. Run it again until it will fail :)
@anthrotype
Copy link
Copy Markdown
Member Author

gladly both failed at first shot:

https://github.com/googlefonts/fontc/actions/runs/24506123258/job/71624978566?pr=1960#step:5:244

---- glyph::tests::component_consistent_layers_independent_of_insertion_order stdout ----

thread 'glyph::tests::component_consistent_layers_independent_of_insertion_order' (4068) panicked at fontir/src/glyph.rs:2560:13:
assertion `left == right` failed: instance at Normalized {wdth: 0.40, wght: 0.50} differs from independent interpolation on the original component
  left: GlyphInstance { width: 0.0, height: None, vertical_origin: None, contours: [BezPath([MoveTo((-384.1, 740.9000000000001)), LineTo((-269.40000000000003, 740.9000000000001)), LineTo((-347.40000000000003, 912.4000000000001)), LineTo((-493.8000000000001, 912.4000000000001)), ClosePath])], components: [] }
 right: GlyphInstance { width: 0.0, height: None, vertical_origin: None, contours: [BezPath([MoveTo((-399.90000000000003, 744.7)), LineTo((-300.6, 744.7)), LineTo((-399.6, 925.2)), LineTo((-525.6000000000001, 925.2)), ClosePath])], components: [] }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- glyph::tests::interpolated_instances_depend_only_on_original_sources stdout ----

thread 'glyph::tests::interpolated_instances_depend_only_on_original_sources' (4097) panicked at fontir/src/glyph.rs:2459:13:
assertion `left == right` failed: instance at Normalized {wdth: 0.40, wght: 0.50} differs from independent interpolation on the original glyph
  left: GlyphInstance { width: 0.0, height: None, vertical_origin: None, contours: [], components: [Component { base: uni0306, transform: Affine([1.0, 0.0, 0.0, 1.0, 0.0, 0.0]), anchor: None }, Component { base: gravecomb.case, transform: Affine([0.95, 0.0, 0.0, 0.95, 51.69999999999999, 72.2]), anchor: None }] }
 right: GlyphInstance { width: 0.0, height: None, vertical_origin: None, contours: [], components: [Component { base: uni0306, transform: Affine([1.0, 0.0, 0.0, 1.0, 0.0, 0.0]), anchor: None }, Component { base: gravecomb.case, transform: Affine([0.95, 0.0, 0.0, 0.95, 51.699999999999996, 72.2]), anchor: None }] }

…c gvar

Two functions in fontir/src/glyph.rs (ensure_composite_defined_at_component_locations
and ensure_component_has_consistent_layers) incrementally added interpolated instances
to composite glyphs while iterating HashSet/HashMap keys. Each insertion changed the
VariationModel used for subsequent interpolations, creating a feedback loop where
HashMap iteration order (non-deterministic per process) produced different delta values.

The fix is to compute all interpolations from the original (unmodified) source set,
then insert all the interpolated instances at the end. This makes the computation
order-independent.

Fixes #1873
…_component_locations

ensure_composite_defined_at_component_locations now returns
Result<Glyph, BadGlyph>; both interpolation sites can call
instantiate_instance directly since missing locations are pre-filtered.
Deduplicates the batch-interpolate-then-insert pattern from
ensure_composite_defined_at_component_locations and
ensure_component_has_consistent_layers into a single function.
Copy link
Copy Markdown
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, this has been annoying me :)

@anthrotype anthrotype added this pull request to the merge queue Apr 16, 2026
Merged via the queue into main with commit 1d519a1 Apr 16, 2026
13 checks passed
@anthrotype anthrotype deleted the fix-1873-deterministic-gvar branch April 16, 2026 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

gvar compilation can be non-deterministic

2 participants