Python for the Zynq and the PYNQ-Z1

Being a big fan of Python, for ages I’ve wanted to explore the possibilities of running Python on the Zynq. Thankfully Xilinx and Digilent saw the value in this too and they developed the PYNQ-Z1 and more importantly the PYNQ libraries for Python. The PYNQ-Z1 is basically a single board computer based on the Zynq-7020 device from Xilinx. So thats got a dual core ARM plus integrated FPGA or programmable logic. The board runs Ubuntu Linux, it’s got Python installed and it has a file system on the micro SD card. The board’s got Gigabit Ethernet so you can connect this to your network and to the Internet. That’s useful for adding packages to Linux and the like but we also use the network interface to develop and run Python applications on the board. You see, the board runs the Jupyter web application. Jupyter allows us to program and run Python scripts through a web interface using a web browser (see screenshot below). This is pretty handy when you’re developing code for a single board computer because you typically don’t have a screen. With Jupyter, you’ve got an interactive web interface, so it’s got features like code completion, you can step through code blocks, display images and heaps of other things.

I can run Python on the Raspberry Pi! What’s so special about this?

Yes you can, but the Zynq has FPGA programmable logic that you can use to accelerate your programs and the PYNQ Python libraries allow you to interface with the FPGA from within your Python code. So the PYNQ-Z1 is able to do things that the Raspberry Pi can’t do because it’s able to offload compute intensive tasks to the FPGA. Think of trying to run image filters or a neural network on a Raspberry Pi, it would run so slowly that it wouldn’t be very practical.

So let’s look at an example design flow. Let’s say you want the PYNQ-Z1 to read video frames from the HDMI input, run them through a neural network that’s trained to detect people, highlight any detected people and send the new frames to the HDMI output. First you’d write this code in Python without using any acceleration. For the reading and writing of HDMI video frames, the PYNQ-Z1 comes with code examples that you can just copy. For the neural network, you’d use one of the many existing Python libraries for machine learning. Once your Python code is working and the hardware is doing what you want, then you’d look at how you can use the FPGA to make things run faster. In this example, obviously most of the processor’s time would be spent running the neural network, so that’s what we’d try to accelerate first. To do this, we’d develop IP to implement the neural network on the FPGA fabric. In PYNQ terminology, this is called a PYNQ overlay - just another way of describing the FPGA configuration or the bitstream. Once our PYNQ overlay has been designed, we can upload it to our PYNQ board and test it out. We’d first have to write functions that interface with our IP, then we’d want to write functions to replace the machine learning library functions that we used before. If everything is done right, the accelerated functions should run a lot faster than the non-accelerated functions, and we should achieve a much higher frame rate, and hopefully one that is practical.

So in my opinion, what makes this board a great dev platform are two things: one, you can prototype quickly in Python and leverage all of the existing Python packages and two, you can accelerate algorithms by offloading to the FPGA, and creating compute intensive designs that are not possible on other embedded devices.


See also