Skip to content

py/grammar.h: Add matmul operator (@) as per PEP 465. - #4740

Closed
jimmo wants to merge 1 commit into
micropython:masterfrom
jimmo:pep-0465-matmul
Closed

jimmo wants to merge 1 commit into
micropython:masterfrom
jimmo:pep-0465-matmul

Conversation

@jimmo

@jimmo jimmo commented May 2, 2019 •

Copy link
Copy Markdown
Member

Maps to __matmul__, not supported for any built-in type (but intended for unumpy.ndarray).
Same precedence as * and left associative.
Also adds corresponding __imatmul__ and __rmatmul__.

This is +80 bytes on ports/minimal. Also a breaking change to the bytecode as the op table changes (but my understanding is that this is OK between micropython versions?)

@jimmo

jimmo commented May 2, 2019

Copy link
Copy Markdown
Member Author

+20 bytes according to the Travis results.

Appveyor failed due to CPython 3.4 on the Windows build. Will make the test conditional.

@dpgeorge

dpgeorge commented May 2, 2019

Copy link
Copy Markdown
Member

Appveyor failed due to CPython 3.4 on the Windows build. Will make the test conditional.

For such tests a .py.exp file is usually provided (see eg tests/basics/python36.py)

@jimmo
jimmo force-pushed the pep-0465-matmul branch from d5b7a5d to b3fd10d Compare May 2, 2019 04:30
@jimmo

jimmo commented May 2, 2019

Copy link
Copy Markdown
Member Author

Thanks :) Done

@dpgeorge

dpgeorge commented May 2, 2019

Copy link
Copy Markdown
Member

This is the code size change I measure locally:

   bare-arm:   +16 +0.024% 
minimal x86:   +80 +0.052% 
   unix x64:  +176 +0.036%
unix nanbox:  +144 +0.033% 
      stm32:   +32 +0.009% 
     cc3200:   +24 +0.013% 
    esp8266:   +80 +0.012% 
      esp32:   +36 +0.003%

So it might be worth thinking of ways to minimise this impact. It could be made compile-time configurable but that's going to be rather messy.

@dpgeorge

dpgeorge commented May 2, 2019

Copy link
Copy Markdown
Member

Also a breaking change to the bytecode as the op table changes (but my understanding is that this is OK between micropython versions?)

Bytecode version already changed since the last release, so there is scope to make a change again before the next release.

Comment thread py/compile.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be possible to compress this switch statement into a small array with a single byte lookup (if so it should be a separate PR to fully evaluate it).

Comment thread py/compile.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This switch might also be compressed.

@pfalcon

pfalcon commented May 2, 2019

Copy link
Copy Markdown
Contributor

So it might be worth thinking of ways to minimise this impact. It could be made compile-time configurable but that's going to be rather messy.

+1, this is a pet operator of numpy, which I never saw to be used anywhere else. (Then maybe numpy support for MicroPython would be important, but I didn't see someone arguing that. And then support for @ would be again among the last things to care about, after 95% of other functionality.)

@stinos

stinos commented May 2, 2019

Copy link
Copy Markdown
Contributor

numpy support for MicroPython would be important, but I didn't see someone arguing that.

Well, it would be pretty awesome. We actually have more than one usecase here where we have uPy code dump data to a binary format, then launch a CPython process to deal with it in numpy and get results from that back into uPy. However that's just us, and numpy is like the opposite of 'micro', so I can see why it's not there yet.

Maps to __matmul__, not supported for any built-in type (but intended for unumpy.ndarray).
Same precedence as *.
Also adds corresponding imatmul and rmatmul.

Note: also moves some big switches to lookup tables to reduce code size.
@jimmo
jimmo force-pushed the pep-0465-matmul branch from b3fd10d to a78fd28 Compare May 2, 2019 12:48
@jimmo

jimmo commented May 2, 2019

Copy link
Copy Markdown
Member Author

@dpgeorge Good thinking on the switch statements. Especially the non-inplace one benefitted because it could take advantage of the table already used in parse.c.

   bare-arm:    -4 -0.006% 
minimal x86:   +48 +0.031% 
   unix x64:  +128 +0.026% [incl +32(data)]

@pfalcon Yes it's highly specialised for numpy, it's literally called "matmul" for a reason. Actually it's pretty high on the list of things to care about because it's a good feature that people use, it means that existing code will work without modification, and it avoids an error-prone conversion of a @ b to numpy.matmul(a, b).

@dpgeorge

Copy link
Copy Markdown
Member

See #4947 for a reworked version of this.

@dpgeorge

Copy link
Copy Markdown
Member

Superseded by #4947

@dpgeorge dpgeorge closed this Sep 26, 2019
tannewt added a commit to tannewt/circuitpython that referenced this pull request May 10, 2021
modmath: Remove stray "pragma GCC diagnostic pop"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants