Better support for symbolic (e.g. jax tracer) values in non-index coordinates
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Idoneità per principianti
- 42/100
Direzione di ricerca
Inizia da xarray/core/variable.py, intorno al confronto di compatibilità menzionato nell'issue, quindi segui la gestione delle coordinate aritmetiche, xr.align e le impostazioni xarray.set_options esistenti. Esamina la discussione per determinare quale dei tre approcci alla compatibilità è accettato; il lavoro è completo quando il comportamento scelto è implementato in modo coerente e coperto per i valori delle coordinate simboliche.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Is your feature request related to a problem?
Hello! xarray_jax maintainer here.
I am trying to decide whether it is feasible to rely on using symbolic arrays (such as jax tracers used during JIT compilation) as coordinate data in xarray.
For index coordinates this doesn't work at all, because xarray requires dynamic access to the data in the index to do alignment. So our approach in xarray_jax is to treat index coordinates as static data. I think this is acceptable: even if we could do index alignment with tracers, the result would be variable-shape which jax.jit can't handle without re-tracing anyway.
For non-index coordinates though, it is possible to use jax tracers! However these coordinates have a habit of disappearing due to failed alignment checks, for example in arithmetic operations:
import xarray as xr
import jax
import jax.numpy as jnp
def foo(coord):
a = xr.DataArray(data=jnp.ones(3), dims=['x'], coords={'foo': (['x'], coord)})
b = xr.DataArray(data=jnp.ones(3), dims=['x'], coords={'foo': (['x'], coord)})
print((a + b).coords)
foo(jnp.arange(3))
# prints:
# Coordinates:
# foo (x) int32 12B ...
jax.jit(foo)(jnp.arange(3))
# prints:
# Coordinates:
# *empty*
The coordinate is (correctly) preserved when not JIT-ing, because xarray is able to do an equality comparison to check that the coordinate is compatible.
When JITing however, the coordinate array will be a jax Tracer, and the equality comparison will fail with a jax.errors.TracerBoolConversionError (or similar). xarray catches this under the hood here since TracerBoolConversionError subclasses TypeError. xarray then treats the failed comparison as not-equal resulting in the coordinate being dropped.
This isn't entirely consistent with behaviour elsewhere in xarray.
For example when doing an explicit merge it is possible to set compat='override' which ensures that coordinates aren't tested for equality and so jax tracers survive. And I believe compat='override' is to become the default soon.
xr.align also lets jax Tracers survive:
def foo(coord):
a = xr.DataArray(data=jnp.ones(3), dims=['x'], coords={'foo': (['x'], coord)})
b = xr.DataArray(data=jnp.ones(3), dims=['x'], coords={'foo': (['x'], coord)})
a, b = xr.align(a, b)
print(a.coords)
print(b.coords)
jax.jit(foo)(jnp.arange(3))
# prints:
# Coordinates:
# foo (x) int32 12B ...
# Coordinates:
# foo (x) int32 12B ...
Overall, while I can see why some might want them, these compatibility checks on non-index coordinates seem a bit of an awkward niche behaviour which is configurable in some places but not others, and (at least after the compat='override' default takes over) will have different defaults in different places too IIUC, which is not ideal. And of course they throw a bit of a spanner in the works of using jax here.
Describe the solution you'd like
Three suggestions:
-
Add a
xarray.set_options(arithmetic_compat='override')setting which controls this, and make 'override' the default for it at the same timecompat='override'becomes the default more widely. Note there is already anarithmetic_joinsetting which controls how index coordinates are joined/aligned, but this doesn't give any control over these compatibility checks on non-index coordinates.
One might also consider exposing thecompatoption onxr.aligntoo and any other places where compatibility checks are happening or for consistency ought to happen, to give control over this. -
Allow
jax.errors.TracerBoolConversionErrorto bubble up from generic equality-testing code (or perhaps catch and reraise it as something less jax-specific likexr.SymbolicComparisonError) rather than catching it and treating it as unequal. Then in coordinate compatibility-checking code, catch this error and fall back oncompat='override'behavior in this specific case. This has the advantage that non-index-coord compatibility checks don't have to be disabled across the board, only for jax Tracers. It would require some jax-specific special case in xarray though. -
Just to get rid of alignment checks on non-index coordinates entirely, if noone feels strongly about them. They are already somewhat inconsistent both in configurability and in default behaviour. In many cases it's a safe assumption that any non-index coordinates are a fixed function of the index coordinates and so it's sufficient to check for alignment of the index coordinates. And perhaps other cases it would be OK to make any checking the user's responsibility. I realise this would be backwards-incompatible though.
Describe alternatives you've considered
See above.
Additional context
No response
- Lingua principale
- Python
- Stelle
- 4.2k
- Fork
- 1.5k
- Merge medio
- 2g 21h
- PR unite (30g)
- 74
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pydata/xarray
-
xarray removes axis grid from pcolormeshForse già presa @Sealdot l’ha presa oggi. Apertabug needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
I maintainer di solito rispondono entro 1 giorno
-
`DataTree.map_over_datasets` behavior can be obscure sometimesForse già presa @mohansree14 l’ha presa 24 giorni fa. Apertaneeds triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
pydata/xarray#11813 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Comparing attrs that contain a dict of NumPy arrays raises ValueError in identical / assert_identicalForse già presa @SohamXYZDev l’ha presa 29 giorni fa. Apertabug needs triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 73/100
pydata/xarray#11777 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
RangeIndex` with datetime64 bounds cannot be printed (`Unknown format code 'g'`)Forse già presa @d-chambers l’ha presa 60 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
pydata/xarray#11598 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
DataTree constructor error message says dict even though dict is not acceptedForse già presa @aryansk l’ha presa 100 giorni fa. Apertabug contrib-good-first-issue topic-DataTree topic-error reporting
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
pydata/xarray#11514 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di pydata/xarray
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
abetlen/llama-cpp-python#2424 ·
I maintainer di solito rispondono entro 1 giorno
-
Bug IronPython Python 3
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
pyrevitlabs/pyRevit#4184 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 74/100
jqueryscript/awesome-claude-code#1009 ·
-
tests
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
I maintainer di solito rispondono entro 3 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
I maintainer di solito rispondono entro 1 giorno