Skip to content

Commit

Permalink
feat: enable use of TensorFlow XNNPACK delegate for accelerating infe…
Browse files Browse the repository at this point in the history
…rence performance
  • Loading branch information
tphakala committed Oct 20, 2024
1 parent c1433cb commit a92f065
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/birdnet/birdnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/tphakala/birdnet-go/internal/conf"
"github.com/tphakala/go-tflite"
"github.com/tphakala/go-tflite/delegates/xnnpack"
)

// Embedded TensorFlow Lite model data.
Expand Down Expand Up @@ -95,6 +96,10 @@ func (bn *BirdNET) initializeModel() error {

// Configure interpreter options.
options := tflite.NewInterpreterOptions()
// If OS is Linux and XNNPACK library is available, enable XNNPACK delegate
if runtime.GOOS == "linux" && CheckXNNPACKLibrary() && bn.Settings.BirdNET.UseXNNPACK {
options.AddDelegate(xnnpack.New(xnnpack.DelegateOptions{NumThreads: int32(threads)}))
}
options.SetNumThread(threads)
options.SetErrorReporter(func(msg string, user_data interface{}) {
fmt.Println(msg)
Expand Down
27 changes: 27 additions & 0 deletions internal/birdnet/birdnet_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package birdnet

import (
"os"
"path/filepath"
)

// CheckXNNPACKLibrary checks for the presence of libXNNPACK.a in typical Debian Linux library paths
func CheckXNNPACKLibrary() bool {
libraryPaths := []string{
"/usr/lib",
"/usr/local/lib",
"/lib",
"/lib/x86_64-linux-gnu",
"/usr/lib/x86_64-linux-gnu",
}

for _, path := range libraryPaths {
fullPath := filepath.Join(path, "libXNNPACK.a")
if _, err := os.Stat(fullPath); err == nil {
// XNNPACK library is present
return true
}
}

return false
}
1 change: 1 addition & 0 deletions internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ type BirdNETConfig struct {
RangeFilter RangeFilterSettings // range filter settings
ModelPath string // path to external model file (empty for embedded)
LabelPath string // path to external label file (empty for embedded)
UseXNNPACK bool // true to use XNNPACK delegate for inference acceleration
}

// RangeFilterSettings contains settings for the range filter
Expand Down
3 changes: 3 additions & 0 deletions internal/conf/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ birdnet:
rangefilter:
model: latest # model to use for range filter: "latest" or "legacy" for previous model
threshold: 0.01 # rangefilter species occurrence threshold
modelpath: "" # path to external model file (empty for embedded)
labelpath: "" # path to external label file (empty for embedded)
usexnnpack: false # true to use XNNPACK delegate for inference acceleration

# Realtime processing settings
realtime:
Expand Down
3 changes: 3 additions & 0 deletions internal/conf/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func setDefaultConfig() {
viper.SetDefault("birdnet.longitude", 0.000)
viper.SetDefault("birdnet.rangefilter.model", "latest")
viper.SetDefault("birdnet.rangefilter.threshold", 0.01)
viper.SetDefault("birdnet.modelpath", "")
viper.SetDefault("birdnet.labelpath", "")
viper.SetDefault("birdnet.usexnnpack", false)

// Realtime configuration
viper.SetDefault("realtime.interval", 15)
Expand Down

0 comments on commit a92f065

Please sign in to comment.