ulab, or what you will - numpy on bare metal

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
v923z
Posts: 123
Joined: Mon Dec 28, 2015 6:19 pm

Re: ulab, or what you will - numpy on bare metal

Post by v923z » Tue Nov 05, 2019 7:14 am

Hi all,

I am a bit confused by the specified flash size of the D series. According to the store's web site, https://store.micropython.org/product/PYBD-SF3-W4F2, they all should have at least 2 MB flash (external, with execute capability), yet, I can't compile the firmware with ulab enabled for the SF2, and SF3. Is the 2 MB extra for the file system only? How much leeway has one for SF6?

To put the question in context, in numpy, many methods can be called in two ways. Either as a method of the ndarray, or as a function. So, e.g.,

Code: Select all

a = array([1, 2, 3, 4])
flip(a)
and

Code: Select all

a = array([1, 2, 3, 4])
a.flip()
produce the same result, except that the first creates a copy of a first, and then operates on that, while the second manipulates a in place. If flash space is very tight, I would yank all out-of-place function objects, and keep only the in-place versions. You can always do

Code: Select all

a = array([1, 2, 3, 4])
b = a
b.flip()
if you want to work on the copy.

I would like to stress that these functions/methods haven't got to be implemented twice, it is basically two ways of calling the same internal C method. But there is still an overhead, because the function objects, the QStrings, and the function wrapper are sort of duplicate. I believe, one can save a couple of kB there.

How do you feel about this issue? Can we go with the solution I outlined, or do you think that keeping both options is important?

Cheers,

Zoltán

User avatar
jimmo
Posts: 1678
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ulab, or what you will - numpy on bare metal

Post by jimmo » Tue Nov 05, 2019 7:54 am

v923z wrote:
Tue Nov 05, 2019 7:14 am
Is the 2 MB extra for the file system only? How much leeway has one for SF6?
The firmware for the sf2 and sf3 is spread across the internal flash and the external qspiflash. By default everything goes to internal, except for things explicitly mentioned in the linker script (e.g. nimble and mbedtls).

Especially on the sf2, the firmware is quite close to the limit for the internal flash. So either:
- we move some more core functionality to external (e.g. some more stuff from extmod?)
- we make user modules go in external flash by default (I'm not sure exactly off the top of my head how to specify this in the linker script but I'm sure it's doable)
- you annotate functions explicitly inside the ulab code to specify which section they should end up in. (there's an attribute you can apply to symbols)

I'll have a play around later and get back to you.

Either way, this can definitely be made to work, no need to cut down useful functionality. (Although maybe making it a configuration option is still worth doing for other more constrained boards).

User avatar
jimmo
Posts: 1678
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: ulab, or what you will - numpy on bare metal

Post by jimmo » Tue Nov 05, 2019 7:56 am

I should add, on SF6 the external qspiflash is currently unused for storing code as the main firmware fits entirely in internal flash.

User avatar
rcolistete
Posts: 205
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil

Re: ulab, or what you will - numpy on bare metal

Post by rcolistete » Sun Jun 07, 2020 2:07 pm

Any news about firmware for Pyboard D SF2W/SF3W with ulab ?

User avatar
jcw
Posts: 35
Joined: Sat Dec 21, 2019 12:08 pm
Location: CET/CEST

Re: ulab, or what you will - numpy on bare metal

Post by jcw » Sun Jun 07, 2020 3:30 pm

I don't know what the status of ulab is, but here is some information about how external flash is used on the SF2/SF3/SF6.

From the info at https://store.micropython.org/product/PYBD-SF2-W4F2 :
  • 2MiB external QSPI flash with execute capabilities to extend internal flash
  • Additional 2MiB external QSPI flash for user filesystem and storage
So it appears that there are two external flash chips. The first one is mapped into the address space, and can eXecute code In-Place (XIP). The second one is a more conventional setup, and can be used as file system (it's probably quite a bit faster than an SD card).

By adding entries here, code can be placed in external flash - BT and TLS in this case:

https://github.com/micropython/micropyt ... ld#L48-L57

FWIW, there's plenty of code space left, the entire firmware needs under 1 MB so far:

Code: Select all

LINK build-PYBD_SF2/firmware.elf
   text	   data	    bss	    dec	    hex	filename
 994696	    284	  75840	1070820	 1056e4	build-PYBD_SF2/firmware.elf
And it appears that currently about half of the code ends up in that first QSPI external flash chip at 0x90000000:

Code: Select all

Writing build-PYBD_SF2/firmware.dfu to the board
File: build-PYBD_SF2/firmware.dfu
    b'DfuSe' v1, image size: 995285, targets: 1
    b'Target' 0, alt setting: 0, name: "ST...", size: 995000, elements: 2
      0, address: 0x08008000, size: 450616
      1, address: 0x90000000, size: 544368
Here is the upload output for an SF6 board with more internal flash memory:

Code: Select all

Writing build-PYBD_SF6/firmware.dfu to the board
File: build-PYBD_SF6/firmware.dfu
    b'DfuSe' v1, image size: 983741, targets: 1
    b'Target' 0, alt setting: 0, name: "ST...", size: 983456, elements: 1
      0, address: 0x08008000, size: 983448
As Jimmo mentioned, the SF6 doesn't place any code in external flash (which is slower):

https://github.com/micropython/micropyt ... 767.ld#L23

Adding entries in the load file is perhaps slightly more flexible than adding "__attribute__((section(...)))" in the source code, as it allows placing code differently, depending each board type.

v923z
Posts: 123
Joined: Mon Dec 28, 2015 6:19 pm

Re: ulab, or what you will - numpy on bare metal

Post by v923z » Sun Jun 07, 2020 3:41 pm

rcolistete wrote:
Sun Jun 07, 2020 2:07 pm
Any news about firmware for Pyboard D SF2W/SF3W with ulab ?
You should compile the firmware yourself from the source: https://github.com/v923z/micropython-ulab
Let me know if there are any difficulties.

You can also easily customise your ulab version (exclude sub-modules), if you find that you are running out of space.

I hope this helps,

Zoltán

Post Reply