NumPy 2.4+: einsum with optimize=True returns wrong results for bool and small-integer arrays

np.einsum('ij,k->k', mask, w)                 # [3.  1.5]
np.einsum('ij,k->k', mask, w, optimize=True)  # [0.5  0.25]

Short answer: Since NumPy 2.4.0, np.einsum with optimize=True can return wrong numbers, with no error, when operands have different dtypes and one is bool or a small integer type. Drop the optimize argument, or cast every operand to the common result dtype first. Passing dtype= does not fix it.

No time to dig in? I find the affected calls, fix them and send you a pull request within 48 hours: $49, card, no account. Still failing, full refund. The free steps are below.

Checked on 16 November 2026 against the sources below.

Why it happens

With optimize on, einsum splits the work into steps. Since 2.4.0 (20 December 2025), an index that appears in only one operand and not in the output is summed away first, in that operand's own dtype, before the operands are combined. Summing a bool array in bool is a logical OR, so six True values become True (1) instead of 6. Summing uint8 or int8 values wraps around at 255 or 127. The default einsum (no optimize) does all its arithmetic in the common result dtype, so it does not have this problem. NumPy issue #33324 (13 November 2026) reports it, a NumPy contributor confirmed it as a valid bug the same day, and the first fix pull request was closed without being merged, so no release has a fix yet. We reproduced it on 14 November 2026 in clean virtual environments (Python 3.11, Windows): NumPy 2.3.5 gave the right answer for the two examples above, 2.4.4 and 2.4.6 gave the wrong one, with optimize=True, 'greedy', 'optimal' and an explicit einsum_path. We then ran 600 random contractions over bool, int8, uint8, int16, int32, int64, float32 and float64 on each version. On 2.4.4 and 2.4.6, 156 of 600 differed from the default einsum. On 2.3.5, 30 of 600 still differed, all with three operands, so going back to 2.3 fixes two-operand calls only. The issue says 2.5.0 and 2.5.4 are affected too. On 16 November 2026 we tested NumPy 2.5.5 (released 15 November) on Python 3.13: both examples above still give the wrong result, with optimize=True, 'greedy' and 'optimal'; in a new set of 600 random contractions, 173 differed from the default einsum and the cast-first helper below matched it in all 600. The 2.5.5 release notes do not mention einsum.

You may also see

np.einsum('i,j->j', a, b)                 # [  0 400 800]
np.einsum('i,j->j', a, b, optimize=True)  # [  0 144 288]
einsum result changes with optimize=True, optimize='greedy' or optimize='optimal'

Fix

  1. Check if you are exposed: search your code for einsum( calls that pass optimize= (True, 'greedy', 'optimal' or a path from np.einsum_path). The default is optimize=False, so calls without the argument are not affected. Then look at the operands: the risk is a boolean mask, or an int8, uint8, int16 or int32 array, combined with an array of a wider dtype.
  2. Confirm on your data: run the call once with and once without optimize and compare with np.allclose. If they differ, the result without optimize is the correct one.
  3. Simplest fix: remove the optimize argument. For two operands, or small arrays, the speed difference is usually small.
  4. Need the speed of optimize? Cast every operand to the common result dtype before the call. This helper gives the same answer as the default einsum on NumPy 2.3.5, 2.4.4 and 2.4.6 in all 600 of our random cases:
    import numpy as np
    
    def einsum_cast(subscripts, *operands, **kwargs):
        """np.einsum with every operand cast to the common result dtype first."""
        dt = np.result_type(*operands)
        return np.einsum(subscripts, *[np.asarray(o, dtype=dt) for o in operands], **kwargs)
    
    einsum_cast('ij,k->k', mask, w, optimize=True)   # [3.  1.5]
    For one call, mask.astype(float) on the bool or small-integer operand does the same job. The cast copies that operand once, so memory use goes up by its size in the wider dtype.
  5. Do not rely on dtype=. In our test, np.einsum('i,j->j', a, b, optimize=True, dtype=np.int64) still returned the wrapped values [0 144 288] on 2.4.6.
  6. Pinning numpy<2.4 is a partial fix only: it restores two-operand calls, but contractions with three or more operands of mixed small dtypes already differed on 2.3.5 in our test. Prefer the cast.
  7. Add a regression test for each fixed call that compares the optimized result with the default one on a boolean or uint8 input, so a later NumPy upgrade can't bring the problem back silently.
Build still red, or no time to dig in? I fix it for you: send a GitHub invite or a zip, and you get a tested pull request that makes the build pass on the new version. No passwords or production keys needed.

Get it fixed: $49

Pay by card, no account needed. Pull request within 48 hours; build still failing, full refund.

How it works: 1. Pay on Gumroad; the checkout asks for the repository. 2. Invite my GitHub user, Donrulezz, to that repository only (private is fine), or email a zip with the failing log to hello@deadlinefix.com. 3. You get a pull request with a list of every change; you review, merge and deploy. I never ask for cloud passwords or production keys.

Who does the work: me, Ashish, the owner of this site, based in India. My public code and tools are on GitHub: github.com/Donrulezz.

Watch out

Sources

Did this fix it?

What's still not working?

Describe what happens and paste any error message. We read every report and fix the page.

Other errors breaking soon

All "breaking soon" fixes