Skip to content

BUG: Weird memory corruption bug related to np.divide #29804

@mrakgr

Description

@mrakgr

Describe the issue:

The following code fragment is somehow accessing uninitialized memory when it's run with the print statements left out. This took me by surprise, and it caused errors in an RL algorithm I am working on. I was just sketching it out using Numpy. I'll try using nan_to_num to get around the divide by 0 errors instead.

Reproduce the code example:

import numpy as np

expected_values_nom_values = np.array([
    [0,0,0],
    [0,0,0],
    [0,0,0],
],dtype=np.float32)

expected_values_den_values = np.array([
    [0,0,0],
    [0,0,0],
    [0,0,0],
],dtype=np.float32)

def expected_values():
    a,b = expected_values_nom_values[0,:], expected_values_den_values[0,:]
    # print(f"a: {a}")
    # print(f"b: {b}")
    return np.divide(a, b, where=(b != 0))

print(expected_values())

Error message:

Here is what happens when I run it in the terminal


(mrakgr) mrakgr@Marko:~/Spiral-s-ML-Library$ python /home/mrakgr/Spiral-s-ML-Library/hd-cfr-python/bug.py
[1.8983535e+03 4.5584239e-41 3.9749163e-31]
(mrakgr) mrakgr@Marko:~/Spiral-s-ML-Library$ python /home/mrakgr/Spiral-s-ML-Library/hd-cfr-python/bug.py
[-1.039874e-09  4.579023e-41  5.763683e-10]
(mrakgr) mrakgr@Marko:~/Spiral-s-ML-Library$ python /home/mrakgr/Spiral-s-ML-Library/hd-cfr-python/bug.py
[1.8641260e-37 4.5823861e-41 4.0987058e-20]
(mrakgr) mrakgr@Marko:~/Spiral-s-ML-Library$ python /home/mrakgr/Spiral-s-ML-Library/hd-cfr-python/bug.py
[-9.5116059e+30  4.5835071e-41  2.2177525e-15]
(mrakgr) mrakgr@Marko:~/Spiral-s-ML-Library$ python /home/mrakgr/Spiral-s-ML-Library/hd-cfr-python/bug.py
[-7.6582234e+23  4.5606660e-41  1.4948110e-20]
(mrakgr) mrakgr@Marko:~/Spiral-s-ML-Library$ python /home/mrakgr/Spiral-s-ML-Library/hd-cfr-python/bug.py
[-1.4026275e+29  4.5724369e-41  1.6299158e-21]
(mrakgr) mrakgr@Marko:~/Spiral-s-ML-Library$ python /home/mrakgr/Spiral-s-ML-Library/hd-cfr-python/bug.py
[1.6858093e-10 4.5591246e-41 5.0520033e-10]


Here is what happens when I run it with the print statements uncommented:


(mrakgr) mrakgr@Marko:~/Spiral-s-ML-Library$ python /home/mrakgr/Spiral-s-ML-Library/hd-cfr-python/bug.py
a: [0. 0. 0.]
b: [0. 0. 0.]
[0. 0. 0.]
(mrakgr) mrakgr@Marko:~/Spiral-s-ML-Library$ python /home/mrakgr/Spiral-s-ML-Library/hd-cfr-python/bug.py
a: [0. 0. 0.]
b: [0. 0. 0.]
[0. 0. 0.]
(mrakgr) mrakgr@Marko:~/Spiral-s-ML-Library$ python /home/mrakgr/Spiral-s-ML-Library/hd-cfr-python/bug.py
a: [0. 0. 0.]
b: [0. 0. 0.]
[0. 0. 0.]

Python and NumPy Versions:

2.3.3
3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0]

Runtime Environment:

[{'numpy_version': '2.3.3',
'python': '3.12.3 (main, Aug 14 2025, 17:47:21) [GCC 13.3.0]',
'uname': uname_result(system='Linux', node='Marko', release='5.15.167.4-microsoft-standard-WSL2', version='#1 SMP Tue Nov 5 00:21:55 UTC 2024', 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']}},
{'architecture': 'SkylakeX',
'filepath': '/home/mrakgr/.venv/lib/python3.12/site-packages/numpy.libs/libscipy_openblas64_-8fb3d286.so',
'internal_api': 'openblas',
'num_threads': 16,
'prefix': 'libscipy_openblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.30'}]

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    33 - QuestionQuestion about NumPy usage or development

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions