test_trace sporadically fails under numpy

Abierto
#455 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
68/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
numpy, python
Área
testing

Línea de trabajo

Comienza con array_api_tests/test_linalg.py::test_trace y reproduce el fallo usando el ejemplo de Hypothesis indicado. Comprueba cómo llegan los valores y dtypes generados a xp.linalg.trace y, después, ajusta las entradas de prueba o la estrategia de dtype para que la prueba evite casos de overflow no válidos y siga siendo estable entre versiones de NumPy; se considera terminado cuando test_trace pasa de forma fiable.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Caught in https://github.com/data-apis/array-api-compat/actions/runs/31951617577/job/95175816104?pr=464 : the values are very different:

FAILED array_api_tests/test_linalg.py::test_trace - AssertionError: The input arrays have different values (array(2048, dtype=uint32) != np.uint32(0)) (x_idxes = [(slice(None, None, None), slice(None, None, None))], res_idx = ())

adding @reproduce_failure('6.165.9', b'AEEIAEEEQQQoQfAAABAQAAAAAAAAAUEC'), as suggested by hypothesis shows this:

(Pdb) p x
array([[4.294968e+09, 4.294968e+09, 4.294968e+09, 4.294968e+09],
       [4.294968e+09, 4.294968e+09, 4.294968e+09, 4.294968e+09],
       [4.294968e+09, 4.294968e+09, 4.294968e+09, 4.294968e+09],
       [4.294968e+09, 4.294968e+09, 4.294968e+09, 4.294968e+09]],
      dtype=float32)
(Pdb) kw
{'dtype': <class 'numpy.uint32'>}
(Pdb) p xp.linalg.trace(x)
array(1.7179871e+10, dtype=float32)   # correct value
(Pdb) p xp.linalg.trace(x, **kw)
array(2048, dtype=uint32)       # incorrect
(Pdb) p xp.sum(np.diag(x), **kw)   # trace matches sum
np.uint32(2048)

So what happens is that the summation is forced to occur in int32 and it overflows. The behavior comes straight from numpy.
The test effectively casts a float32 array to int32, which is not a very reasonable thing to do, and the (wrong) results depend on the order of operations in fairly unpredictable manner:

(Pdb) p xp.linalg.trace(xp.astype(x, xp.int32))
array(-8589934592)
(Pdb) p xp.linalg.trace(xp.astype(x, xp.int32), dtype=xp.int32)
array(0, dtype=int32)

What to do about it at the test suite level? Not entirely sure. We can either limit the range of values of x or only draw flowing-point dtype for floating-point x, for example.

Lenguaje dominante
Python
Estrellas
74
Forks
54
Merge medio
4 d 15 h
PR fusionados (30 d)
6

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de data-apis/array-api-tests

Todos los issues de data-apis/array-api-tests

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.