Skip to content

Starmap hangs for Numpy matmul operator called for matrix vector products #268

Open

Description

@JoKra1

Hey there,

the following code hangs on MacOS (15.6.1 (24G90)):

import multiprocess as mp
import numpy as np
from itertools import repeat

n_c = 1
n_rows = 5000

shapes = [(n_rows,23),(n_rows,1)]
Xs = [np.zeros(shapes[i]) for i in range(len(shapes))]

bs = [np.random.rand(Xs[i].shape[1]).reshape(-1,1) for i in range(len(Xs))]

a = [1,2,3,4]

args = zip(repeat(Xs),
           repeat(bs),
           a)

def matmul(Xs, bs, a):
    return a, [Xs[i] @ bs[i] for i in range(len(Xs))]


with mp.Pool(processes=n_c) as pool:
    import dill
    dill.detect.trace(True)
    (
        a,
        matmul
    ) = zip(*pool.starmap(matmul, args))

Happens in fresh coda env with:

appnope                 0.1.4
asttokens               3.0.2
comm                    0.2.3
debugpy                 1.8.11
dill                    0.4.1
executing               2.2.1
ipykernel               7.3.0
ipython                 9.16.1
ipython_pygments_lexers 1.1.1
jedi                    0.20.0
jupyter_client          8.9.1
jupyter_core            5.9.1
matplotlib-inline       0.2.2
multiprocess            0.70.19
nest-asyncio2           1.7.2
numpy                   2.5.2
packaging               26.3
parso                   0.8.7
pexpect                 4.9.0
pip                     26.1.2
platformdirs            4.11.3
prompt_toolkit          3.0.53
psutil                  7.2.2
ptyprocess              0.7.0
pure_eval               0.2.3
Pygments                2.20.0
python-dateutil         2.9.0.post0
pyzmq                   26.2.0
setuptools              78.1.1
six                     1.17.0
stack_data              0.6.3
tornado                 6.5.1
traitlets               5.16.1
typing_extensions       4.16.0
wcwidth                 0.8.2
wheel                   0.45.1

I have narrowed it down to the use of @ to evaluate the matrix vector product and the size matters: the code succeeds with n_rows=1000. Dill traces suggest that lookup for starmap gets stuck for some reasons for matrices with ~5000 rows:

Dill Trace for n_rows=1000 (Success):

F2: <function starmapstar at 0x1176d2480>
# F2
F1: <function matmul at 0x111dac040>
F2: <function _create_function at 0x111e52b60>
# F2
Co: <code object matmul at 0x111d13340, file "/var/folders/qf/5y9hm2q10014lf3frvjgdk8c0000gn/T/ipykernel_91653/1207331203.py", line 19>
F2: <function _create_code at 0x111e53060>
# F2
# Co
D3: <dict object at 0x0111d5e200>
# D3
D2: <dict object at 0x0116144640>
# D2
D2: <dict object at 0x0111d5f000>
D2: <dict object at 0x0111dc6380>
# D2
# D2
# F1
Nu: ((1000, 23), float64)
F2: <function _create_array at 0x111e53ba0>
# F2
T4: <class 'numpy.ndarray'>
# T4
T4: <class 'numpy.dtype'>
# T4
# Nu
Nu: ((1000, 1), float64)
# Nu
Nu: ((23, 1), float64)
# Nu
Nu: ((1, 1), float64)
# Nu
D2: <dict object at 0x0111e87480>
# D2
F2: <function starmapstar at 0x1176d2480>
# F2
F1: <function matmul at 0x111dac040>
F2: <function _create_function at 0x111e52b60>
# F2
Co: <code object matmul at 0x111d13340, file "/var/folders/qf/5y9hm2q10014lf3frvjgdk8c0000gn/T/ipykernel_91653/1207331203.py", line 19>
F2: <function _create_code at 0x111e53060>
# F2
# Co
D3: <dict object at 0x0111d5e200>
# D3
D2: <dict object at 0x0116144640>
# D2
D2: <dict object at 0x0117729e00>
D2: <dict object at 0x0111dc6380>
# D2
# D2
# F1
Nu: ((1000, 23), float64)
F2: <function _create_array at 0x111e53ba0>
# F2
T4: <class 'numpy.ndarray'>
# T4
T4: <class 'numpy.dtype'>
# T4
# Nu
Nu: ((1000, 1), float64)
# Nu
Nu: ((23, 1), float64)
# Nu
Nu: ((1, 1), float64)
# Nu
D2: <dict object at 0x0111fb42c0>
# D2
F2: <function starmapstar at 0x1176d2480>
# F2
F1: <function matmul at 0x111dac040>
F2: <function _create_function at 0x111e52b60>
# F2
Co: <code object matmul at 0x111d13340, file "/var/folders/qf/5y9hm2q10014lf3frvjgdk8c0000gn/T/ipykernel_91653/1207331203.py", line 19>
F2: <function _create_code at 0x111e53060>
# F2
# Co
D3: <dict object at 0x0111d5e200>
# D3
D2: <dict object at 0x0116144640>
# D2
D2: <dict object at 0x011771a000>
D2: <dict object at 0x0111dc6380>
# D2
# D2
# F1
Nu: ((1000, 23), float64)
F2: <function _create_array at 0x111e53ba0>
# F2
T4: <class 'numpy.ndarray'>
# T4
T4: <class 'numpy.dtype'>
# T4
# Nu
Nu: ((1000, 1), float64)
# Nu
Nu: ((23, 1), float64)
# Nu
Nu: ((1, 1), float64)
# Nu
D2: <dict object at 0x01176d9680>
# D2
F2: <function starmapstar at 0x1176d2480>
# F2
F1: <function matmul at 0x111dac040>
F2: <function _create_function at 0x111e52b60>
# F2
Co: <code object matmul at 0x111d13340, file "/var/folders/qf/5y9hm2q10014lf3frvjgdk8c0000gn/T/ipykernel_91653/1207331203.py", line 19>
F2: <function _create_code at 0x111e53060>
# F2
# Co
D3: <dict object at 0x0111d5e200>
# D3
D2: <dict object at 0x0116144640>
# D2
D2: <dict object at 0x0111fb42c0>
D2: <dict object at 0x0111dc6380>
# D2
# D2
# F1
Nu: ((1000, 23), float64)
F2: <function _create_array at 0x111e53ba0>
# F2
T4: <class 'numpy.ndarray'>
# T4
T4: <class 'numpy.dtype'>
# T4
# Nu
Nu: ((1000, 1), float64)
# Nu
Nu: ((23, 1), float64)
# Nu
Nu: ((1, 1), float64)
# Nu
D2: <dict object at 0x0117738a80>
# D2

Dill Trace for n_rows=5000 (Failure):

F2: <function starmapstar at 0x114b4a2a0>
# F2
F1: <function matmul at 0x1102e42c0>
F2: <function _create_function at 0x11038e980>
# F2
Co: <code object matmul at 0x11024b340, file "/var/folders/qf/5y9hm2q10014lf3frvjgdk8c0000gn/T/ipykernel_91766/3092000593.py", line 19>
F2: <function _create_code at 0x11038ee80>
# F2
# Co
D3: <dict object at 0x0110295b00>
# D3
D2: <dict object at 0x011063c800>
# D2
D2: <dict object at 0x01106ed440>
D2: <dict object at 0x010faacb00>
# D2
# D2
# F1
Nu: ((5000, 23), float64)
F2: <function _create_array at 0x11038f9c0>
# F2
T4: <class 'numpy.ndarray'>
# T4
T4: <class 'numpy.dtype'>
# T4
# Nu
Nu: ((5000, 1), float64)
# Nu
Nu: ((23, 1), float64)
# Nu
Nu: ((1, 1), float64)
# Nu
D2: <dict object at 0x01103bf400>
# D2
F2: <function starmapstar at 0x114b4a2a0>
# F2
F1: <function matmul at 0x1102e42c0>
F2: <function _create_function at 0x11038e980>
# F2
Co: <code object matmul at 0x11024b340, file "/var/folders/qf/5y9hm2q10014lf3frvjgdk8c0000gn/T/ipykernel_91766/3092000593.py", line 19>
F2: <function _create_code at 0x11038ee80>
# F2
# Co
D3: <dict object at 0x0110295b00>
# D3
D2: <dict object at 0x011063c800>
# D2
D2: <dict object at 0x0114b7e2c0>
D2: <dict object at 0x010faacb00>
# D2
# D2
# F1
Nu: ((5000, 23), float64)
F2: <function _create_array at 0x11038f9c0>
# F2
T4: <class 'numpy.ndarray'>
# T4
T4: <class 'numpy.dtype'>
# T4
# Nu
Nu: ((5000, 1), float64)
# Nu
Nu: ((23, 1), float64)
# Nu
Nu: ((1, 1), float64)
# Nu
D2: <dict object at 0x0114b8a440>
# D2

Any ideas and help would be appreciated!

Activity

  1. JoKra1 commented on Aug 17, 2026

    @JoKra1
    Author

    Follow up: this seems to happen only when evaluating code in Jupyter notebooks.

    Consider the following file structure:

    src/
        __init__.py
        matmul.py
    main.py
    main.ipynb
    

    with matmul.py

    import numpy as np
    
    def matmul(Xs, bs, a):
        return a, [Xs[i] @ bs[i] for i in range(len(Xs))]
    

    main.py:

    import multiprocess as mp
    import numpy as np
    from itertools import repeat
    from src.matmul import matmul
    
    if __name__ == "__main__":
    
        n_c = 1
        n_rows = 5000
    
        shapes = [(n_rows,23),(n_rows,1)]
        Xs = [np.zeros(shapes[i]) for i in range(len(shapes))]
    
        bs = [np.random.rand(Xs[i].shape[1]).reshape(-1,1) for i in range(len(Xs))]
    
        a = [1,2,3,4]
    
        args = zip(repeat(Xs),
                repeat(bs),
                a)
    
        def matmul2(Xs, bs, a):
            return a, [Xs[i] @ bs[i] for i in range(len(Xs))]
    
        with mp.Pool(processes=n_c) as pool:
            import dill
            dill.detect.trace(True)
            (
                a,
                matmul
            ) = zip(*pool.starmap(matmul, args))
            print(a)
    

    and main.ipynb:

    import multiprocess as mp
    import numpy as np
    from itertools import repeat
    from src.matmul import matmul
    
    n_c = 1
    n_rows = 5000
    
    shapes = [(n_rows,23),(n_rows,1)]
    Xs = [np.zeros(shapes[i]) for i in range(len(shapes))]
    
    bs = [np.random.rand(Xs[i].shape[1]).reshape(-1,1) for i in range(len(Xs))]
    
    a = [1,2,3,4]
    
    args = zip(repeat(Xs),
            repeat(bs),
            a)
    
    def matmul2(Xs, bs, a):
        return a, [Xs[i] @ bs[i] for i in range(len(Xs))]
    
    with mp.Pool(processes=n_c) as pool:
        import dill
        dill.detect.trace(True)
        (
            a,
            matmul
        ) = zip(*pool.starmap(matmul, args))
        print(a)
    

    Then main.py succeeds with matmul, and matmul2. main.ipynb fails with both. main.ipynb succeeds with matmul when importing standard multiprocessing.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions