-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: enable use of TensorFlow XNNPACK delegate for accelerating infe… #303
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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)})) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling when creating the XNNPACK delegate When creating the XNNPACK delegate with Modify the code to handle errors during delegate creation: -if runtime.GOOS == "linux" && CheckXNNPACKLibrary() && bn.Settings.BirdNET.UseXNNPACK {
- options.AddDelegate(xnnpack.New(xnnpack.DelegateOptions{NumThreads: int32(threads)}))
+if runtime.GOOS == "linux" && CheckXNNPACKLibrary() && bn.Settings.BirdNET.UseXNNPACK {
+ delegate, err := xnnpack.New(xnnpack.DelegateOptions{NumThreads: int32(threads)})
+ if err != nil {
+ return fmt.Errorf("failed to create XNNPACK delegate: %w", err)
+ }
+ options.AddDelegate(delegate)
|
||
} | ||
options.SetNumThread(threads) | ||
options.SetErrorReporter(func(msg string, user_data interface{}) { | ||
fmt.Println(msg) | ||
|
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Implement validation logic for the While the
🔗 Analysis chainVerify default configuration and validation for the new field. While the addition of the
To confirm these points, please run the following commands: If these checks don't yield results, consider updating the respective files to include appropriate default and validation logic for the new 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Check for default configuration
grep -n "UseXNNPACK" defaults.go
# Check for validation logic
grep -n "UseXNNPACK" internal/conf/validation.go
Length of output: 187 Script: #!/bin/bash
# Search for defaults.go
fd "defaults.go" .
# Search for validation.go
fd "validation.go" internal/conf/
Length of output: 80 Script: #!/bin/bash
# Search for usage of UseXNNPACK across the entire codebase
rg "UseXNNPACK" -- --type go
Length of output: 118 Script: #!/bin/bash
# Search for usage of UseXNNPACK across the entire codebase within Go files
rg "UseXNNPACK" --type=go
Length of output: 258 |
||
} | ||
|
||
// RangeFilterSettings contains settings for the range filter | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", "") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification birdnet.modelpath is not utilized in the backend code. The
🔗 Analysis chainLGTM. Verify handling of empty model path. The addition of Ensure that the code handling this configuration properly checks for an empty path before attempting to load the model. Run the following script to verify: 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check for proper handling of empty model path
# Test: Search for usage of birdnet.modelpath configuration
rg -A 5 'viper\.Get.*birdnet\.modelpath'
Length of output: 3506 Script: #!/bin/bash
# Description: Search for any references to birdnet.modelpath in the codebase
rg 'birdnet\.modelpath'
Length of output: 283 |
||
viper.SetDefault("birdnet.labelpath", "") | ||
viper.SetDefault("birdnet.usexnnpack", false) | ||
|
||
// Realtime configuration | ||
viper.SetDefault("realtime.interval", 15) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Undefined function
CheckXNNPACKLibrary()
The function
CheckXNNPACKLibrary()
is called but not defined or imported. This will result in a compile-time error due to the undefined function.Consider adding an implementation for
CheckXNNPACKLibrary()
: