Skip to content

global_evaluate: refuse non-finite query points on every rank, before any collective - #1249

Merged
lmoresi merged 4 commits into
developmentfrom
bugfix/global-evaluate-non-finite-points
Nov 20, 2026
Merged

lmoresi merged 4 commits into
developmentfrom
bugfix/global-evaluate-non-finite-points

Conversation

@lmoresi

@lmoresi lmoresi commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

uw.function.global_evaluate now refuses query points with a NaN or infinite coordinate, with the same ValueError on every rank, before any collective. Before this, a NaN point could hang a parallel run. This is the core evaluation path; the change is confined to that refusal, and nothing else in the evaluation is touched.

The defect

Found by the parallel review desk while checking #1246 (comment 7028008624 there). A NaN coordinate passes the far-outside refusal (#786), because a comparison with NaN is false. It then reaches the swarm migration inside global_evaluate, where the ranks part. Measured on development (9cf3509) at np 2, with a degree-2 field on UnstructuredSimplexBox(cellSize=0.2) and 41 points on a line, one row NaN:

Where the points are Development
NaN row on the last rank, no points on the others (what a plotting helper with viz_rank last arranges) the first rank raises an IndexError with a garbage index; the last waits in the migration's barrier: hang
the same NaN points on every rank different errors per rank (the #960 RuntimeError, and a kd-tree stencil RuntimeError)
one infinite row, check_extrapolated=True (the far-outside refusal is off) different errors per rank

The fix

A new _refuse_non_finite_points in function/functions_unit_system.py, called by global_evaluate on every rank before the far-outside refusal and the call into global_evaluate_nd.

  • Each rank counts its own points with a non-finite coordinate.
  • The count, and the total point count, are reduced over the ranks (allreduce of two integers), so a rank whose own points are finite, or that holds none, raises too.
  • Every rank raises ValueError: global_evaluate: k of n query points have non-finite coordinates (NaN or infinite), over all ranks. ....

It applies whatever check_extrapolated is. As with the far-outside refusal, only expressions that read mesh data are checked: a pure sympy expression is evaluated pointwise and still returns NaN for a NaN point. A rank whose coordinates are still unit-aware tuples (converted later, in global_evaluate_nd) contributes nothing to the count rather than raising alone before the reduction.

What a user's run does now

Case Before Now
parallel, non-finite point on any rank, field data hang, or a different error on each rank the same ValueError on every rank
serial global_evaluate, NaN point, field data RuntimeError from the kd-tree stencil the ValueError above
serial global_evaluate, infinite point, field data far-outside ValueError (#786) the ValueError above
pure sympy expression, NaN or infinite point returns NaN or inf at that point unchanged
uw.function.evaluate (rank-local, no migration) kd-tree RuntimeError for NaN unchanged

evaluate does not reach the swarm migration, so it is left as it is.

Cost

One isfinite pass over the points and two integer reductions per call. UnstructuredSimplexBox(cellSize=0.02), degree-2 field, uniform points, slowest rank:

10,000 points 1,000,000 points
np 1 0.18 ms of 161 ms (0.11 %) 14 ms of 8.1 s (0.17 %)
np 4 0.11 ms of 228 ms (0.05 %) 3.6 ms of 3.5 s (0.10 %)

Tests

tests/parallel/test_1249_global_evaluate_non_finite_points_parallel.py (level 1, tier B). Each case gathers what every rank raised and requires the same ValueError with the right count:

  • the same NaN, inf and -inf points on every rank;
  • a NaN row on the first or the last rank, the others holding no points or finite points;
  • an infinite point with check_extrapolated=True;
  • finite points evaluated exactly as before (the field x).

On development at np 2 the file hangs (pytest's timeout fires). With the fix: 9 passed on every rank at np 2 and 3, and serially.

What was run

On a 96-thread Linux workstation (single system image, Intel Xeon Gold 6240R, MPICH):

What Result
the new file, np 1, 2, 3 9 passed on every rank
15 serial evaluate and function test files and the collective guard scan 174 passed, 1 skipped
parallel evaluate files at np 2: test_0785, test_0786 (outside domain), test_0960, test_1075, test_1076, test_0795, test_1243 all pass on both ranks
scripts/check_test_coverage.py OK, 491 files reachable
scripts/check_deprecated_patterns.py OK

Not run

np 4 and above for the test file; the full suite; macOS.

Checklist (UW3 Style Charter — docs/developer/UW3_STYLE_CHARTER.md)

  • I have read the Style Charter and followed it over the surrounding code (S2)
  • No drive-by refactors, renames, or "while I was here" cleanups (S9)
  • Bug fixes ship the regression test, written first, with level_* and tier_* markers (S8)
  • No hedging names (maybe_ / try_ / do_) and no commented-out code (S3, S4)
  • Every exception swallow states its sanctioned failure mode in a comment (S4)
  • New data access uses .array / mesh.X.coords — no with ....access(...), no mesh.data (S7) — not applicable: no data access added
  • Parallel safety considered — np2/np4 checked where swarm/mesh/solver dispatch is touched (S11)
  • No mesh entity or rank identified by coordinates; any remaining tolerance is named, relative and says what it bounds (S12) — no tolerance added
  • No pixi.toml / pixi.lock or other dependency changes riding in an unrelated PR
  • AI-assisted work carries the attribution line below

Review: the parallel review desk (H1, the session that reviews pull requests and runs the parallel tests).

Underworld development team with AI support from Claude Code

On development at np 2 the file hangs (pytest's timeout fires): with a NaN
row on the last rank and no points on the others, the first rank raises an
IndexError and the last waits in the swarm migration's barrier. With the
same NaN points on every rank the ranks raise different errors.

Underworld development team with AI support from Claude Code
… any collective

Each rank counts its own points with a NaN or infinite coordinate; the
count is reduced over the ranks before the far-outside refusal and the
swarm migration, and every rank raises the same ValueError. Only
expressions that read mesh data are checked; pure sympy expressions keep
returning NaN for NaN. evaluate is rank-local and unchanged.

Underworld development team with AI support from Claude Code
Copilot AI balanced review requested due to automatic review settings November 20, 2026 02:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@lmoresi

lmoresi commented Nov 20, 2026

Copy link
Copy Markdown
Member Author

Review from the Underworld development team with AI support from Claude Code (H1: review and parallel test runs on a 96-thread Linux workstation (Intel Xeon Gold 6240R, AVX-512; MPICH))
(H1 is the session that reviews pull requests and runs the parallel tests.)

930bb74: ready to merge on green from this review. The one finding below is a rename, and it does not block.

How it was run

  • The change is Python-only: function/functions_unit_system.py plus one new test file.
  • It was laid over a build whose compiled sources match the merge base (9cf3509, development).
  • "Development" below means the same build with the merge base's Python.
  • Ranks were pinned to 4 cores, at most 3 ranks, under the MPI supervisor.
  • The field is degree 2 throughout: UnstructuredSimplexBox(cellSize=0.1), with the sample line from the PR's test.
  • "Empty rank" means np 3 with a shell partition that gives the last rank no cells.

Findings

  1. Low: the new test's number collides with another PR's.

  2. Note: unit-aware coordinates that do not convert in np.asarray are not counted.

    • A rank whose coordinates are unit-aware tuples that np.asarray(..., float64) cannot convert contributes 0 to the count. So a NaN given that way still reaches the migration.
    • The docstring says this, and it is the right choice over a rank raising alone before the reduction.
    • I did not test that path. It is no worse than development.
  3. Note: there is one call site, and it covers every public path.

    • _refuse_non_finite_points is called once, in _global_evaluate_impl, ahead of the far-outside refusal and of _global_evaluate_nd.
    • global_evaluate_nd is imported only privately (as _global_evaluate_nd), and that is its only caller. So there is no route around the check.
    • Rank-local evaluate is unchanged and still gives the kd-tree RuntimeError for NaN, as the PR says. It has no migration, so it cannot hang.

Non-finite points: every rank must raise the same ValueError, with no hang

Each case gathers what every rank raised. The 12 cases were run at np 2, at np 3, and at np 3 with the empty rank:

  • NaN, inf and -inf on every rank (the latter two also with check_extrapolated=True);
  • NaN on rank 0 only and on the last rank only, with the other ranks holding finite points or none;
  • one all-NaN row on the (empty) last rank only;
  • a NaN in x among good points;
  • a vector expression.

Results:

  • Every case gave the same ValueError on every rank, with the right count over all ranks. Examples: "1 of 41" when only the NaN rank has points, "3 of 123" at np 3 when every rank has the NaN row.
  • With the empty rank: the last rank owns 0 cells and holds the NaN, and it is still refused everywhere.
  • In every run, a finite evaluation afterwards succeeds, so the communicator is still usable.
  • Development, same probe at np 2: different errors per rank (the kd-tree stencil RuntimeError on one rank, the Mesh-move field transfer: remap_var_set can skip a collective on one rank, and ghosts are not synchronised #960 RuntimeError on the other) for NaN, and for -inf with check_extrapolated=True. The run hung at "NaN only on the last rank, the others empty".

The new test on development and on this PR

  • Development, np 2: 8 cases fail, then the 9th hangs. One rank waits in uw.mpi.barrier() inside Swarm.migrate (called from _global_evaluate_impl), and the other waits in the test's allgather. pytest's 120 s timeout ends it. The hang is confirmed.
  • This PR: 9 passed on every rank at np 1, np 2, np 3, and np 3 with the empty rank.

Finite points: no regression

  • Bitwise equal to development at np 1, 2 and 3: 30 arrays (each rank's output, 5 arrays per rank).
    • 3000 random interior points per rank;
    • the sample line;
    • an empty last rank;
    • 50 points running 0.02 past both walls, with and without check_extrapolated=True.
  • Far-outside refusal: message and outcome are identical to development at np 1, 2 and 3. This covers:
    • points far outside;
    • the same with check_extrapolated=True, which returns values;
    • far points on the last rank only, which is refused on every rank.

Pure sympy expression with a NaN point (np 2)

  • x*y + 1 returns NaN, or inf, at the bad point on every rank that holds it. Other ranks return their own values. There is no hang and no error, the same as development.
  • A mixed expression (x*y + T) reads mesh data and is refused on every rank. Development gave different errors per rank here.
  • So "mesh-data expressions only" is safe: pure sympy is evaluated pointwise and never reaches the migration.

Cost

global_evaluate of a degree-2 field on cellSize=0.03, 1e5 points split over the ranks. Median of 5 runs, slowest rank, two repeats:

development this PR
np 1 0.758 s, 0.754 s 0.759 s, 0.760 s
np 3 0.412 s, 0.417 s 0.413 s, 0.412 s

The difference is within noise: 0.1 to 0.7 % at np 1, and -1.3 to +0.1 % at np 3.

Existing tests

  • Old error messages: no test asserts the kd-tree RuntimeError or the uw.function.evaluate silently returns values for points far outside the mesh domain #786 message for a non-finite point. Every uw.function.evaluate silently returns values for points far outside the mesh domain #786 and units-contract refusal test uses finite far-outside points with match="outside", and those still raise the uw.function.evaluate silently returns values for points far outside the mesh domain #786 message. So no test needed updating.
  • np 2: all 19 parallel files that call global_evaluate pass. They include test_0682, 0760, 0766, 0780, 0783, 0785, 0786, 0795, 0796, 0873, 0960, 0999, 1062, 1075, 1076, 1093, 1123, 1213 and 1243.
  • Serial: 28 files: 300 passed, 1 skipped, and 3 failed, all in test_1086_mmpde_seam_matches_serial.
    • Those runs failed by their own 120 s watchdog, with the ranks in sympy's assumption code.
    • The [5] cases launch 5 ranks onto 4 pinned cores, and the parallel batch was sharing those cores at the time.
    • Rerun alone, the [3] cases pass on this PR and on development (2 passed, 48 s each).
    • I did not rerun [5], since it exceeds my rank budget. The test does not reach a non-finite point, so I attribute the failures to oversubscription, not to this PR.
  • Coverage: scripts/check_test_coverage.py is OK (491 files reachable).
  • Whitespace: git diff --check is clean.

Underworld development team with AI support from Claude Code

Underworld development team with AI support from Claude Code
Underworld development team with AI support from Claude Code
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.

3 participants