Google Coral M.2 on Raspberry Pi 5

Good day,

I have procured a raspberry pi 5 and in pursuit of a superior form factor/packaging for my project have purchased a pcie to m.2 board (the pineboard hat AI drive) + Google Coral m.2 TPU.

I need to get the Coral tpu m.2 working, and it requires some kernel modules (the Google gasket and apex modules).

I’m pursuing a customisation of your suggested pattern for such things ( GitHub - balena-os/kernel-module-build: Example project for building an OOT kernel module in balena ) , but it introduces a level of complexity and risk with running my application.

My question/request is if the kernel modules could be baked into the raspberry 5 Balena OS, or if it already is and there’s just some flag I need to switch on - as I should imagine there will be a number of others also hoping to use this combination of hardware and it would be great if it were more plug and play.

1 Like

Hello @hcpainter first of all, welcome to the balena community!

Let me ask internally to the devices team and we will keep you posted!

BTW could you please share what use case are you running to need a Raspberry Pi 5 with a Coral TPU m2 working? Thanks!

Hi @mpous ,

Certainly, I’m developing a computer vision at edge project. It needs to be reasonably small, low powered and inexpensive. It needs to be at the edge as streaming >1080p video over a SIM is not going to work.

Up to now it’s used the Coral USB which has its fair share of challenges regarding up to date libraries, but mercifully the actual hardware side is quite plug and play with just the need for some UDEV rules.

The m.2 option is cheaper, more performant, and offers a better packaging for the device.

Just to confuse matters:
I am also likely to try the Raspberry pi AI kit once it’s in stock, and this might also become quite popular: https://www.raspberrypi.com/documentation/accessories/ai-kit.html < I haven’t looked into it in depth but suspect it also involves some kernel modules which if run from docker alone become non-trivial to install.

1 Like

Thanks for sharing @hcpainter

Could you please share more details of the logs and errors do you get when using the M.2 Coral TPU?

I’ve so far adapted the nvidia example in the balena-os/kernel-module-build , the gist is here:

I’m baking this into the application itself run in an entrypoint script before starting the python application, and exiting if mod needs to be loaded to effectively reboot.

I’m now just following the remainder of the instructions here: Get started with the M.2 or Mini PCIe Accelerator | Coral and trying to debug.

In my application this bit is currently failing:

from tflite_runtime.interpreter import Interpreter, load_delegate

delegate=load_delegate('libedgetpu.so.1.0')
self.interpreter = Interpreter(model_path=self.model_path,experimental_delegates=[delegate])

With following error:

 detect    File "/usr/local/lib/python3.10/site-packages/tflite_runtime/interpreter.py", line 168, in load_delegate
 detect      raise ValueError('Failed to load delegate from {}\n{}'.format(
 detect  ValueError: Failed to load delegate from libedgetpu.so.1.0

The same code has worked for the USB Edge TPU.

I’m currently troubleshooting, roughly following the various things this poor chap went through: Failed to load delegate from libedgetpu.so.1 M.2 Accelerator with Dual Edge TPU · Issue #611 · google-coral/edgetpu · GitHub

So far, actually think the bit I thought was going to be the most tricky, the kernel modules, seems to be working because:

ls /dev/apex_0
/dev/apex_0

and

lsmod
Module                  Size  Used by
apex                   20480  28
gasket                102400  29 apex

Currently believe it’s down to permissions somewhere and the UDEV rule not quite kicking in correctly.

Permissions are ending up like:

ls -la dev/apex_0 
crw------- 1 root root 120, 0 Jul 23 13:13 dev/apex_0

When I believe desired is:

ls -l /dev/apex_0 
crw-rw---- 1 root apex 120, 0 Jul 23 13:13 /dev/apex_0

It may also be an issue to do with MSI-X (whatever on earth that is):

lspci -vvv|grep -i MSI-X
lspci: Unable to load libkmod resources: error -2
        Capabilities: [d0] MSI-X: Enable- Count=128 Masked-
        Capabilities: [d0] MSI-X: Enable+ Count=9 Masked-
        Capabilities: [b0] MSI-X: Enable+ Count=61 Masked-

When desired is perhaps Enable+ everywhere? (It looks like that unable to load error can be ignored)

I should mention, the service in question has, amongst other things, this in docker-compose:

    environment:
      - UDEV=on
    network_mode: host
    privileged: true

No labels, I understand these to be unnecessary with privileged: true.

Output from dmesg, that’s the same on both the host and the service:

dmesg | grep -E 'apex|gasket'
[  827.581889] gasket: loading out-of-tree module taints kernel.
[  827.592291] apex 0000:03:00.0: enabling device (0000 -> 0002)
[  827.593109] apex 0000:03:00.0: Couldn't initialize interrupts: -28
[  832.661223] apex 0000:03:00.0: Apex performance not throttled due to temperature
[  847.213312] apex 0000:03:00.0: Couldn't reinit interrupts: -28
1 Like

Just circling back to this, I’ve parked it as being a slight distraction from the main task of developing the application, and can continue on USB for now, but I’ll summarise my assessment (though I don’t have much hardware/kernel knowledge to really be able to intelligently know what’s really going on):

I’m using this board and its docs are here: HatDrive! Ai – Pineboards

And that links to these docs (Google Coral Edge TPU on a Raspberry Pi 5 Configuration – Pineboards) which includes this clear step:

Step 2: Update the Kernel

Kernel version 6.6.30 or higher is needed for the Pineboards Hat Ai overlay. This version is only available via rpi-update right now, please use this tutorial to install the Kernel). To check your kernel version use this command:

uname -a

Expected output:

Linux coralpi 6.6.30-v8+ #1761 SMP PREEMPT Thu May  2 16:54:52 BST 2024 aarch64 GNU/Linux

And that’s the only step that I don’t think is in place.

So it looks like support for coral edge TPU m.2 on this board requires dtoverlay=pineboards-hat-ai which is only baked into pi linux kernel 6.6.30 or higher.

As at time of writing Balena OS 5.3.22:

Linux ***** 6.6.22-v8 #1 SMP PREEMPT Tue Mar 19 17:41:59 UTC 2024 aarch64 aarch64 aarch64 GNU/Linux

Would I be right in thinking that eventually 6.6.30+ which includes this: firmware/boot/overlays/pineboards-hat-ai.dtbo at master · raspberrypi/firmware · GitHub will eventually be included in Balena OS and there would be no need for an explicit PR from my end?

Meanwhile I have a hailo-8l variant of the same bundle on order (Ai Bundle (Hailo 8L) – Pineboards) and that looks to not be reliant on that overlay - though the docs DO assume you’re on raspbian and have done a full upgrade which might have some other special sauce yet to be discovered.

@mpous :point_up_2:

BalenaOS should eventually get the update to 6.6.30 or above but that might not automatically include all the new dtbo files and overlays as that is not always done in the yocto board support layer as you can see from this example here: Add support for vc4-kms-dsi-waveshare-panel overlay · Issue #1260 · agherzan/meta-raspberrypi · GitHub

That being said, I also have a RPI AI kit on order with the hailo8L and hope to find some personal time to ensure it gets correctly integrated into balenaOS for the pi5, so hopefully in the next month or so we will have this support. Lets see!

2 Likes