Skip to content

BUG: numpy.power result is nondeterministic when AVX512 enabled #26002

Open

Description

@pryvkin10x

Describe the issue:

numpy.power result is nondeterministic when the exponent is a view with certain strides and AVX512 is available. Problem goes away with NPY_DISABLE_CPU_FEATURES="AVX512F"

Reproduce the code example:

import numpy as np

for r in range(1000):
    for c in range(1, 10):
        src = np.linspace(0, 1, num=r * c).reshape(r, c)
        for j in range(0, c):
            x = src[:, j]
            if not np.array_equal(np.power(1.2, x), np.power(1.2, x)):
                print(f"diff! {(r,c,j)=}; {x.__array_interface__=}")
                break

Error message:

Snippet of results:
diff! (r,c,j)=(3, 3, 1); x.__array_interface__={'data': (94083650133912, False), 'strides': (24,), 'descr': [('', '<f8')], 'typestr': '<f8', 'shape': (3,), 'version': 3}
diff! (r,c,j)=(134, 5, 4); x.__array_interface__={'data': (94083654872944, False), 'strides': (40,), 'descr': [('', '<f8')], 'typestr': '<f8', 'shape': (134,), 'version': 3}
diff! (r,c,j)=(134, 8, 2); x.__array_interface__={'data': (94083654872928, False), 'strides': (64,), 'descr': [('', '<f8')], 'typestr': '<f8', 'shape': (134,), 'version': 3}

Python and NumPy Versions:

1.26.4
3.10.13 | packaged by conda-forge | (main, Dec 23 2023, 15:36:39) [GCC 12.3.0]

Runtime Environment:

[{'numpy_version': '1.26.4',
'python': '3.10.13 | packaged by conda-forge | (main, Dec 23 2023, 15:36:39) '
'[GCC 12.3.0]',
'uname': uname_result(system='Linux', node=', release='6.1.0-18-cloud-amd64', version='#1 SMP PREEMPT_DYNAMIC Debian 6.1.76-1 (2024-02-01)', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2',
'AVX512F',
'AVX512CD',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL'],
'not_found': ['AVX512_KNL', 'AVX512_KNM', 'AVX512_SPR']}},
{'filepath': '/conda_package_mkl/lib/libmkl_rt.so.2',
'internal_api': 'mkl',
'num_threads': 48,
'prefix': 'libmkl_rt',
'threading_layer': 'intel',
'user_api': 'blas',
'version': '2023.2-Product'},
{'filepath': '/conda_package_llvm_openmp/lib/libomp.so',
'internal_api': 'openmp',
'num_threads': 96,
'prefix': 'libomp',
'user_api': 'openmp',
'version': None}]

Context for the issue:

The result is unexpectedly nondeterministic and causes flaky tests.

Activity

  1. changed the title [-]BUG: <Please write a comprehensive title after the 'BUG: ' prefix>[/-] [+]BUG: numpy.power result is nondeterministic when AVX512 enabled[/+] on Mar 12, 2024
  2. self-assigned this
    on Mar 12, 2024
  3. added
    component: SIMD
    on Mar 12, 2024
  4. r-devulap commented on Mar 20, 2024

    @r-devulap
    Member

    Occasionally, the strided input array overlaps in memory with the output/destination array, preventing SIMD from being used. The output of SIMD can differ from the scalar version by at most 4 ULP. You could adjust your test to account this for this difference.

  5. r-devulap commented on Mar 20, 2024

    @r-devulap
    Member

    @seberg is it concerning to see this check fail?

    if (!is_mem_overlap(src1, steps[0], dst, steps[2], len) && !is_mem_overlap(src2, steps[1], dst, steps[2], len) &&

    I always assumed we never hit these conditions and they were there just as a sanity check.

  6. seberg commented on Mar 21, 2024

    @seberg
    Member

    I always assumed we never hit these conditions and they were there just as a sanity check.

    Accumulations do hit it, although we know that ahead of time really and it should be a very clear pattern of 1 stride/step difference between inputs.
    What I have been saying about that part is that it would be plausible to explicitly pick a different loop ahead of time for accumulations.

  7. seberg commented on Mar 21, 2024

    @seberg
    Member

    If it fails with the above code, then I would assume npyv_loadable_stride_ is failing, which seems acceptable (not sure about the conditions).

  8. charris commented on Apr 29, 2024

    @charris
    Member

    Can this be solved for 1.26.x?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

00 - Bugcomponent: SIMD

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions