tensorflow: Failed to load delegate from libedgetpu.so.1.0 with tflite_runtime 1.14

System information

  • Have I written code (based on the docs):
from tflite_runtime.interpreter import Interpreter
from tflite_runtime.interpreter import load_delegate
model_path='my_compiled_model.tflite'
interpreter = Interpreter(model_path,
  experimental_delegates=[load_delegate('libedgetpu.so.1.0')])
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
  Operating System: Ubuntu 18.04.3 LTS
            Kernel: Linux 4.15.0-60-generic
      Architecture: x86-64
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device: laptop
  • TensorFlow installed from (source or binary): pip3 install tflite_runtime-1.14.0-cp36-cp36m-linux_x86_64.whl
  • TensorFlow version (use command below): tflite_runtime 1.14
  • Python version: Python 3.6.5 :: Anaconda, Inc.
  • Bazel version (if compiling from source): n/a
  • GCC/Compiler version (if compiling from source): n/a
  • CUDA/cuDNN version: n/a
  • GPU model and memory: n/a

This is the code that I ran:

from tflite_runtime.interpreter import Interpreter
from tflite_runtime.interpreter import load_delegate
model_path='my_compiled_model.tflite'
interpreter = Interpreter(model_path,
  experimental_delegates=[load_delegate('libedgetpu.so.1.0')])

following this tutorial: https://www.tensorflow.org/lite/guide/python

This was working before, but somehow broken with this error:

Traceback (most recent call last):
  File "/home/nam/anaconda3/lib/python3.6/site-packages/tflite_runtime/interpreter.py", line 165, in load_delegate
    delegate = Delegate(library, options)
  File "/home/nam/anaconda3/lib/python3.6/site-packages/tflite_runtime/interpreter.py", line 119, in __init__
    raise ValueError(capture.message)
ValueError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "evaluate_edgetpu_cifar10.py", line 51, in <module>
    interpreter = Interpreter(file_name,experimental_delegates=[load_delegate('libedgetpu.so.1.0')])
  File "/home/nam/anaconda3/lib/python3.6/site-packages/tflite_runtime/interpreter.py", line 168, in load_delegate
    library, str(e)))
ValueError: Failed to load delegate from libedgetpu.so.1.0

I have been messing around a lot with my machine since by installing different versions of tf. But for the purpose of using the tflite_runtime.interpreter’s load_delegate function, shouldn’t just the pip install works? Very weird behavior 😕 also I do have libedgetpu.so.1.0 installed here:

% ls /usr/lib/x86_64-linux-gnu/libedgetpu.so.1.0
/usr/lib/x86_64-linux-gnu/libedgetpu.so.1.0

Thanks in advance for the help!

[EDIT] I guess I’ll update the issue here with a solution so that any body else can reference: ValueError: Failed to load delegate from libedgetpu.so.1.0 really is just due to the delegate library not being able to communicate with the edgetpu. This is a very standard linux problem and has nothing to do with the tensorflow library or libedgetpu. The failures most likely stems from some type of errno from the kernel which returns as failure to the user side.

So the easiest fix is to run with sudo:

$ sudo python your_script.py

But the most permanent fix is to add your linux user to the plugdev group which will allows you to access devices without sudo (this will requires a reboot after):

$ sudo usermod -aG plugdev $USER

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 23
  • Comments: 33 (1 by maintainers)

Most upvoted comments

Hi @Namburger , I had the same issue!

Make sure your Coral USB Accelerator is plugged in when you run your code. If the USB Accelerator isn’t plugged in when you call the ‘load_delegate’ function, it will result in that error. If it IS plugged in, that error won’t occur.

I am facing the same issue , even when the USB accelerator is plugged in and the LED in it is shining bright …

No problems!

After spending many hours on and off thinking about this problem and combing the net, I finally solved this problem with this “Failed to load delegate from libedgetpu.so.1.0 error”. Essentially, the cable that Google provided with my Coral USB TPU stinks. I broke down and purchased a USB 3.1 (10Gbps) (NOT 5Gbps) cable and everything worked perfectly on the USB 3.0 port for the Raspberry Pi 4b (4GB). I hope this helps someone else in the same situation I was in.

rebooting my pi - fixed the issue for me

Thanks @cruzzer for the suggestions!

Does anyone have tried to use the TPU with a program in a Docker container? I am getting the same Failed to load delegate from libedgetpu.so.1.0 error when running my docker container.

I am facing the same issue , even when the USB accelerator is plugged in and the LED in it is shining bright …

@programmer290399 you might need to add your linux user to plugdev group:

$ sudo usermod -aG plugdev [your username]

I plugged in the device, and I did this too, it still is showing the same error. I am using ubuntu installed on a virtualbox in Mac. The USB device is getting attached, but load_delegate is giving error

I got the same problem in native Ubuntu. Reboot helps.

I am facing the same issue , even when the USB accelerator is plugged in and the LED in it is shining bright …

@programmer290399 you might need to add your linux user to plugdev group:

$ sudo usermod -aG plugdev [your username]

(last edit: May 28,2021, to handle situation when lsusb works, but coral doesn’t load the library)

I’m going to leave another finding here (Only applies to intermittent libedgetpu delegate loading issues, unrelated to plugdev/permissions)

  1. Replacing the cable eliminates most problems related to intermittent loading libedgetpu issues
  2. However, there are times this happens, and especially if I interrupt an ongoing TPU operation
  3. When this happens, a reboot usually fixes it, but I’d prefer not to have to reboot. There are two approaches I found.
    • (Works for me) You reset the full USB ecosystem - this script does that - tested on ubuntu 20. Depending on your OS it may require different paths - see comments in that link. I use this approach.

    • (Does not work for me) If you prefer not to reset the entire USB ecosystem, this is a more focussed way (You can get the coral device details by doing lsusb | grep -i google. However, in my case, when the TPU actually fails, this method does not work - it mulls around for a while and then errors out

So the rest of the post is how I go about detecting failure and then I reset the full USB system. Here is what I observed:

  • Sometimes, when coral fails, lsusb also fails showing the google device
  • I realized later, that there are times that coral fails to load, but lsusb continues to show the device

So to make sure the device always works, I do the following:

  • Try lsusb - if it fails, obviously coral is not working, restart usb
  • If lsusb shows google, try to load the delegate using pycoral - if that fails restart usb
  • If that passes, nothing to do

Specifically, I have set up a cron file that checks that the coral device is detected every hour:

pp@homeserver:~$ cat /etc/cron.d/coral-usb-checker 
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# every hour
0 * * * * root usb_reset_all.sh

The modified script (credit above in my notes):

pp@homeserver:~$ cat /usr/local/bin/usb_reset_all.sh 
#!/bin/bash

filename='/tmp/coral_status.txt'
now=$(date)
if [[ $EUID != 0 ]] ; then
  echo "${now}:$0  must be run as root!" 
  echo "${now}:$0  must be run as root!" > ${filename}
  exit 1
fi

# keep log file to 100 lines
if [ -f "${filename}" ] ;then
        echo "$(tail -100 ${filename})" > ${filename}
fi

 [[ "${1}" == "--force" ]] &&  IS_FORCED=true || IS_FORCED=false

is_coral_working() {
        # Note if you have multiple results from this lsusb command
        # You can specifically check with lsusb -d <deviceid>
        # Example: lsusb -d 18d1:9302 in my case which is what lsusb prints as the "ID" for my device

        if [[ -z `lsusb | grep -i google` ]] ; then
                echo "${now}:lsusb check failed" >> ${filename}

                return 0
        fi
        echo "${now}:lsusb check passed, checking load_edgetpu_delegate()" >> ${filename}
        res=`python << HEREDOC
from pycoral.utils.edgetpu import load_edgetpu_delegate
try:
        load_edgetpu_delegate()
        print ('success')
except Exception as e:
        print('error')
HEREDOC
`
        if [[ "${res}" = "success" ]]; then
                return 0
        else
                return 1
        fi
}
restart_usb() {
        # credit: http://billauer.co.il/blog/2013/02/usb-reset-ehci-uhci-linux/
        echo "${now}:Restarting USB" >> ${filename}
        for xhci in /sys/bus/pci/drivers/?hci_hcd ; do
          if ! cd $xhci ; then
            echo "${now}:Weird error. Failed to change directory to $xhci" >> ${filename}
            exit 1
          fi

          echo "${now}:Resetting devices from $xhci..." >> ${filename}

          for i in ????:??:??.? ; do
            echo -n "$i" > unbind 2>/dev/null
            echo -n "$i" > bind 2>/dev/null
          done
        done
        echo "${now}:Completed operation" >> ${filename}
}


echo "${now}--------------------------------------------------------" >> ${filename}

if [[ ${IS_FORCED} == true ]] ; then
        echo "${now}:Forcing restart as user specified --force" >> ${filename}
        restart_usb

elif is_coral_working ; then 
        echo "${now}:Coral working fine" >> ${filename}

else
        echo "${now}:ERROR: Coral detection failed" >> ${filename}
        restart_usb
fi

The nice part is my resident services that depend on coral automatically get restarted (not sure how, but it does). So I don’t need to restart it manually.

I recently moved from a USB 2.0 system to USB 3.0 and had the same issue. It is intermittent, so it’s not a plugdev issue.

As suggested above it does seem to be an issue with Google’s cable. I bought this cable and replaced the google provided cable. That seems to have completely eliminated the issue and I’m getting good inference speed (averaging 19ms for an 800px image using mobiledet and pycoral wrappers)

There were two issues that needed resolving on my setup, coming from a fresh installed Pi 4 + fresh installed edge TPU.

  1. A udev rules needs to be added for the TPU, which according to the “Getting Started” page should have happened when installing the libedgetpu1-* package.
/etc/udev/rules.d/99-edgetpu-accelerator.rules
SUBSYSTEM=="usb",ATTRS{idVendor}=="1a6e",GROUP="plugdev"
SUBSYSTEM=="usb",ATTRS{idVendor}=="18d1",GROUP="plugdev"
  1. The user needs to be part of the plugdev group as mentioned by @Namburger . sudo usermod -aG plugdev [your username]

  2. Reboot This seems to be a bug with the libedgetpu1-* packages. I tried both -std and -max versions (v12-1).

Thanks @Namburger !

Now my program is able to use the TPU from docker, I had to add -v /dev/bus/usb:/dev/bus/usb along with the --priviledge flag to the docker run command so that it works.

@adr-arroyo you can check this out 😃 https://github.com/google-coral/tflite/issues/3#issuecomment-547942348 tl;dr: most likely just have to throw it a --privileged flag