numpy/einsum-optimize
| Pack | Severity | Fix | Versions |
|---|---|---|---|
| NumPy | warning | unsafe, with xpd check --fix --unsafe-fixes |
All versions |
Without optimize=, einsum loops over every index at once, which was 339 times slower for three 100 by 100 matrices. Add optimize=True.
chain = np.einsum("ij,jk,kl->il", a, b, c)chain = np.einsum("ij,jk,kl->il", a, b, c, optimize=True)What the rule reports
Section titled “What the rule reports”The rule reports np.einsum with three or more 2-D operands and a 2-D result, such as "ij,jk,kl->il", and no optimize=.
How to fix it
Section titled “How to fix it”xpd check --fix --unsafe-fixes applies the fixes of this rule. Check the result, because an unsafe fix can change what the code does.
The unsafe fix adds optimize=True.
The contraction order can change the last bits. The rule leaves a scalar result such as "ij,jk,ki->" alone, since optimizing that contraction can be slower.
Diagnostic message
Section titled “Diagnostic message”Without optimize= einsum loops over every index at once, which ran 339 times slower on three 100 by 100 matrices. Add optimize=True.See all rules in the NumPy pack.