Skip to content

Fix time-lag UnitConversionError under dask.distributed process workers - #350

Open
CharlesXmf wants to merge 4 commits into
sunpy:mainfrom
CharlesXmf:cursor/fix-dask-distributed-timelag-b978
Open

CharlesXmf wants to merge 4 commits into
sunpy:mainfrom
CharlesXmf:cursor/fix-dask-distributed-timelag-b978

Conversation

@CharlesXmf

Copy link
Copy Markdown

PR Description

time_lag used to multiply the selected lags by their time unit inside the lazy Dask graph. With the default scheduler that works. With process-based dask.distributed workers (dask v2025.4.0 and later), some chunks come back dimensionless, and concatenating mixed dimensionless/s chunks raises UnitConversionError. A thread-based / inproc client does not reproduce the failure. See #276.

This keeps the Dask graph unitless and re-attaches the unit through __dask_postcompute__, so both array.compute() and dask.compute() still return a Quantity, while time_lag(...) itself remains a dask.array.Array. Because HLGFinalizeCompute runs finalize on the worker, a Quantity would be stripped by distributed's numpy serializer on the way back to the client. Finalize therefore returns a small pickle proxy only on process workers (addresses that are not inproc://); inproc workers and the default scheduler multiply by the unit in-process and return a real Quantity.

The regression covers Client(processes=True) and Client(processes=False), and asserts both compute paths. The tests extra now depends on dask[distributed] so that case is actually installed.

Fixes #276

AI Assistance Disclosure

AI tools were used for:

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding
  • No AI tools were used

A Cursor cloud agent (Grok 4.6) was used to implement the change, write the regression tests, and draft the changelog fragment. The resulting patch was reviewed before this pull request was opened.

Regardless of AI use, the human contributor remains fully responsible for correctness, design choices, licensing compatibility, and long-term maintainability.

cursoragent and others added 4 commits November 19, 2026 06:30
Keep lag selection unitless in the Dask graph and re-attach the
time unit after client-side compute. Multiplying by a unit inside
the lazy graph put Quantity chunks on distributed workers, which
dropped units and raised UnitConversionError from dask v2025.4.0.

Co-authored-by: Charles <CharlesXmf@users.noreply.github.com>
Hook __dask_postcompute__ so array.compute() and dask.compute() both
return a Quantity. On distributed process workers, Quantity is serialized
as a bare ndarray, so finalize returns a pickle proxy that rehydrates
the unit on the client instead of putting Quantity chunks in the graph.

Co-authored-by: Charles <CharlesXmf@users.noreply.github.com>
Only use the pickle proxy when finalize runs on a process worker
whose address is not inproc://. Inproc workers share the result by
reference, so multiplying by the unit in finalize already yields a
Quantity. Cover Client(processes=False) in the regression.

Co-authored-by: Charles <CharlesXmf@users.noreply.github.com>
Rename the changelog fragment to 350.bugfix.rst. Quote
dask.distributed as a literal so the docs build does not warn
under -W. Add an in-process pickle round-trip for the unit proxy
that distributed process workers return from finalize.

Co-authored-by: Charles <CharlesXmf@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

Time lag computation fails starting with dask v2025.4.0

2 participants