Skip to content

Commit

Permalink
refactor: enhance Dockerfile and Makefile for platform-specific Tenso…
Browse files Browse the repository at this point in the history
…rFlow Lite support

- Removed the precompiled TensorFlow Lite C library download step from the Dockerfile, streamlining the build process.
- Updated the Dockerfile to set the TFLITE_LIB_DIR based on the architecture, improving compatibility for linux/arm64 and linux/amd64.
- Modified the Makefile to define TFLITE_LIB_DIR conditionally based on the architecture, ensuring correct library paths for both x86_64 and aarch64 targets.
- Improved clarity and maintainability of both files by explicitly handling platform-specific configurations.
  • Loading branch information
tphakala committed Jan 21, 2025
1 parent 2877a75 commit ed2ed25
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
18 changes: 11 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ COPY --chown=dev-user ./reset_auth.sh ./
# Download TensorFlow headers
RUN make check-tensorflow

# Download and configure precompiled TensorFlow Lite C library
RUN PLATFORM=$(echo ${TARGETPLATFORM} | tr '/' '_') && \
echo "Building for platform: ${TARGETPLATFORM}" && \
echo "Using library archive: tflite_c_v${TENSORFLOW_VERSION}_${PLATFORM}.tar.gz" && \
make download-tflite TARGET=${PLATFORM}

FROM --platform=$TARGETPLATFORM buildenv AS build

Check warning on line 33 in Dockerfile

View workflow job for this annotation

GitHub Actions / docker (linux/amd64)

Setting platform to predefined $TARGETPLATFORM in FROM is redundant as this is the default behavior

RedundantTargetPlatform: Setting platform to predefined $TARGETPLATFORM in FROM is redundant as this is the default behavior More info: https://docs.docker.com/go/dockerfile/rule/redundant-target-platform/
WORKDIR /home/dev-user/src/BirdNET-Go

Expand Down Expand Up @@ -64,8 +58,18 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
sox \
&& rm -rf /var/lib/apt/lists/*

# Set TFLITE_LIB_DIR based on architecture
ARG TARGETPLATFORM
ARG TFLITE_LIB_DIR
COPY --from=build ${TFLITE_LIB_DIR}/libtensorflowlite_c.so ${TFLITE_LIB_DIR}
RUN if [ "$TARGETPLATFORM" = "linux/arm64" ]; then \
export TFLITE_LIB_DIR=/usr/aarch64-linux-gnu/lib; \
else \
export TFLITE_LIB_DIR=/usr/lib; \
fi && \
echo "Using TFLITE_LIB_DIR=$TFLITE_LIB_DIR"

# Use the TFLITE_LIB_DIR for the library copy
COPY --from=build ${TFLITE_LIB_DIR}/libtensorflowlite_c.so ${TFLITE_LIB_DIR}/
RUN ldconfig

# Include reset_auth tool from build stage
Expand Down
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,13 @@ $(strip \
endef

ifeq ($(UNAME_S),Linux)
NATIVE_TARGET := linux_$(if $(filter x86_64,$(UNAME_M)),amd64,arm64)
TFLITE_LIB_DIR := /usr/lib
ifeq ($(UNAME_M),x86_64)
NATIVE_TARGET := linux_amd64
TFLITE_LIB_DIR := /usr/lib
else ifeq ($(UNAME_M),aarch64)
NATIVE_TARGET := linux_arm64
TFLITE_LIB_DIR := /usr/aarch64-linux-gnu/lib
endif
TFLITE_LIB_EXT := .so
else ifeq ($(UNAME_S),Darwin)
NATIVE_TARGET := darwin_$(if $(filter x86_64,$(UNAME_M)),amd64,arm64)
Expand Down Expand Up @@ -123,16 +128,20 @@ check-tensorflow:
define ensure_tflite_symlinks
@if [ "$(suffix $(2))" = ".dll" ] && [ ! -f "$(1)/tensorflowlite_c.dll" ]; then \
echo "Creating symbolic link for Windows DLL..."; \
echo "Linking $(2) to tensorflowlite_c.dll"; \
sudo ln -sf "$(1)/tensorflowlite_c-$(patsubst v%,%,$(TFLITE_VERSION)).dll" "$(1)/tensorflowlite_c.dll"; \
elif [ "$(UNAME_S)" = "Linux" ] && [ ! -f "$(1)/libtensorflowlite_c.so" ]; then \
echo "Creating symbolic links for Linux library..."; \
cd $(1) && \
echo "Linking $(2) to libtensorflowlite_c.so.2"; \
sudo ln -sf $(2) libtensorflowlite_c.so.2 && \
echo "Linking libtensorflowlite_c.so.2 to libtensorflowlite_c.so"; \
sudo ln -sf libtensorflowlite_c.so.2 libtensorflowlite_c.so && \
sudo ldconfig; \
elif [ "$(UNAME_S)" = "Darwin" ] && [ ! -f "$(1)/libtensorflowlite_c.dylib" ]; then \
echo "Creating symbolic links for macOS library..."; \
cd $(1) && \
echo "Linking $(2) to libtensorflowlite_c.dylib"; \
ln -sf $(2) libtensorflowlite_c.dylib; \
fi
endef
Expand Down

0 comments on commit ed2ed25

Please sign in to comment.