Skip to content

BUG: np.matmul crashes in multi-threaded settings on windows with large matrices #27989

Closed
OpenMathLib/OpenBLAS
#6132

Description

@vladoovtcharov

Describe the issue:

When multiplying large matrices in a multi-threaded case (using ThreadPoolExecutor for example) the program crashes without any error. The crash does not occur if the matrix is small enough, only a single thread is used, on linux, or if OPENBLAS_NUM_THREADS is set to 1, so perhaps this is an openblas issue.

Reproduce the code example:

import numpy as np
from concurrent.futures import ThreadPoolExecutor

def crash_test(size_a):
    a = np.random.random((size_a,4))
    b = np.random.random((4,4))
    c = np.matmul(a,b)
    return c.mean()
    
num_threads = 4
counts = 128
futures = []
total = 0
size_a = 1024*1024

with ThreadPoolExecutor(max_workers=num_threads) as executor:
    for i in range(counts):
        futures.append(executor.submit(crash_test, size_a))
    for i in range(counts):    
        total += futures[i].result()
print(total)

Error message:

No response

Python and NumPy Versions:

1.26.4
3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]

and
2.2.0
3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]

Runtime Environment:

[{'numpy_version': '2.2.0',
'python': '3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]',
'uname': uname_result(system='Windows', node='vlado-laptop', release='10', version='10.0.22621', machine='AMD64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2'],
'not_found': ['AVX512F',
'AVX512CD',
'AVX512_SKX',
'AVX512_CLX',
'AVX512_CNL',
'AVX512_ICL']}}]

Context for the issue:

No response

Activity

  1. isatyamks commented on Dec 13, 2024

    @isatyamks

    @vladoovtcharov We could consider replacing ThreadPoolExecutor with ProcessPoolExecutor. Process-based parallelism creates separate processes for each computation, which effectively avoids threading conflicts and ensures stability when working with openblas.

  2. seberg commented on Dec 13, 2024

    @seberg
    Member

    Would seem like a blas issue, yes. Can you please say how you installed NumPy and what blas is being used? Installing threadpoolctl and running np.show_runtime() again should print it out.

  3. vladoovtcharov commented on Dec 13, 2024

    @vladoovtcharov
    Author

    With numpy 2.2.0:

    [{'numpy_version': '2.2.0',
      'python': '3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]',
      'uname': uname_result(system='Windows', node='vlado-laptop', release='10', version='10.0.22621', machine='AMD64')},
     {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                          'found': ['SSSE3',
                                    'SSE41',
                                    'POPCNT',
                                    'SSE42',
                                    'AVX',
                                    'F16C',
                                    'FMA3',
                                    'AVX2'],
                          'not_found': ['AVX512F',
                                        'AVX512CD',
                                        'AVX512_SKX',
                                        'AVX512_CLX',
                                        'AVX512_CNL',
                                        'AVX512_ICL']}},
     {'architecture': 'Haswell',
      'filepath': 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\numpy.libs\\libscipy_openblas64_-43e11ff0749b8cbe0a615c9cf6737e0e.dll',
      'internal_api': 'openblas',
      'num_threads': 4,
      'prefix': 'libscipy_openblas',
      'threading_layer': 'pthreads',
      'user_api': 'blas',
      'version': '0.3.28'}]
    

    With numpy 1.26.4 I get the following:

    [{'numpy_version': '1.26.4',
      'python': '3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]',
      'uname': uname_result(system='Windows', node='vlado-laptop', release='10', version='10.0.22621', machine='AMD64')},
     {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
                          'found': ['SSSE3',
                                    'SSE41',
                                    'POPCNT',
                                    'SSE42',
                                    'AVX',
                                    'F16C',
                                    'FMA3',
                                    'AVX2'],
                          'not_found': ['AVX512F',
                                        'AVX512CD',
                                        'AVX512_SKX',
                                        'AVX512_CLX',
                                        'AVX512_CNL',
                                        'AVX512_ICL']}},
     {'architecture': 'Zen',
      'filepath': 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python311\\Lib\\site-packages\\numpy.libs\\libopenblas64__v0.3.23-293-gc2f4bdbb-gcc_10_3_0-2bde3a66a51006b2b53eb373ff767a3f.dll',
      'internal_api': 'openblas',
      'num_threads': 4,
      'prefix': 'libopenblas',
      'threading_layer': 'pthreads',
      'user_api': 'blas',
      'version': '0.3.23.dev'}]
    

    I installed both version of numpy through pip no custom compilation.

  4. seberg commented on Dec 13, 2024

    @seberg
    Member

    When I test with numpy 2.2.0 just the above "runtime environment" is printed.

    Yeah, weird... I might suspect threadpoolctl doesn't understand the way we pull in openblas anymore, would that make sense @mattip?

    Anyway, since these are the pip install, we know what is used... Ping @mreineck maybe you have some quick insight on why it might crash?

    (Although, oversubscription seems probable in this context and setting at least some lower value for the number of openblas threads seems likely advisable)

  5. vladoovtcharov commented on Dec 13, 2024

    @vladoovtcharov
    Author

    Yeah, weird... I might suspect threadpoolctl doesn't understand the way we pull in openblas anymore, would that make sense @mattip?

    Mistake on my part, updating to the latest threadpoolctl does show the rest of the information, edited answer above.

  6. seberg commented on Dec 13, 2024

    @seberg
    Member

    updating to the latest threadpoolctl

    Ah, thanks, good to know. We changed the library things a bit so threadpoolctl had to adapt. (I have seen this missing a few times recently, so good to know we should just ask to update.)

  7. seberg commented on Dec 16, 2024

    @seberg
    Member

    Ack, sorry, I pinged the wrong person... @martin-frbg do you immediately happen to know if this is an OpenBLAS issue?

  8. martin-frbg commented on Dec 16, 2024

    @martin-frbg

    not a known one at least, and both 0.3.23 and 0.3.28 should use the same old-and-assumed-stable multithreading code on Windows

  9. mreineck commented on Dec 19, 2024

    @mreineck
    Contributor

    Ack, sorry, I pinged the wrong person

    Good to know, I was a bit surprised at first ;-) While I was involved a bit in the multithreading support for scipy.fft, this is not based on ThreadPoolExecutor at all, so I was not sure how I could help.

  10. yadonpapa commented on Oct 12, 2026

    @yadonpapa

    This still reproduces with the current wheels (numpy 2.5.3, OpenBLAS 0.3.34.106.0), and here it happens much faster and with small operands. The trigger is a gemm in one thread running at the same time as a gemv in another thread. With two threads it crashes within about a second (0xC0000005, no traceback) or sometimes deadlocks. gemm alone or gemv alone in both threads does not fail.

    I could reproduce it without NumPy against the scipy-openblas64 wheel DLL, but not against the official OpenBLAS release binaries (0.3.27 and later). So I reported it to the wheel builds: MacPython/openblas-libs#283 (C reproducer, results by version, and the build-flag differences).

    NumPy reproducer:

    import threading, time
    import numpy as np
    
    stop = time.perf_counter() + 30
    
    def work(i):
        rng = np.random.default_rng(i)
        m = rng.random((3, 3))
        w = np.array([0.2627, 0.6780, 0.0593])
        while time.perf_counter() < stop:
            a = rng.random((int(rng.integers(1_000, 270_000)), 3))
            a @ m.T          # gemm: (N, 3) @ (3, 3)
            a @ w            # gemv: (N, 3) @ (3,)
    
    threads = [threading.Thread(target=work, args=(i,)) for i in range(2)]
    for t in threads: t.start()
    for t in threads: t.join()
    print("no crash")

    Each case was run 3 times, 30 s per run, with Python 3.12.12:

    numpy (bundled OpenBLAS) result
    1.26.4 (0.3.23.dev) ok, ok, ok
    2.0.2 (0.3.27) crash 0.5 s, hang, crash 0.8 s
    2.1.3 (0.3.27) crash 0.7 s, 0.3 s, 0.2 s
    2.2.0 (0.3.28) crash 0.4 s, 0.6 s, 0.3 s
    2.2.6 (0.3.29) crash 0.4 s, 0.2 s, 0.4 s
    2.4.6 (0.3.31.188.0) crash 0.5 s, 0.7 s, hang
    2.5.3 (0.3.34.106.0) crash 0.5 s, 1.8 s, 0.7 s

    More results with numpy 2.5.3:

    • 1 caller thread: no failure.
    • OPENBLAS_NUM_THREADS=1: no failure.
    • OPENBLAS_NUM_THREADS=2: crash or hang.
    • OPENBLAS_NUM_THREADS=4: hang in all 3 runs.

    Environment: Windows 11 Pro 10.0.26300; Intel Core Ultra 9 275HX (24 cores, no SMT, no AVX-512). OpenBLAS reports Haswell, 24 threads.

    Workaround: set OPENBLAS_NUM_THREADS=1 before importing numpy. This is what the OpenBLAS FAQ recommends for multi-threaded applications.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions