From 6cb42f901ab99b1ca7a10ef34cae8aa3f0e9f7b2 Mon Sep 17 00:00:00 2001 From: Nam Vu Date: Mon, 9 May 2022 16:16:31 -0500 Subject: [PATCH] Fixes builds for cpp examples. - Upgrade to latest libedgetpu with matching tensorflow library - Use bazel and share WORKSPACE for both examples - Added docker with cross compile supports --- .gitignore | 1 + cpp/examples/.gitignore | 7 ++ cpp/examples/Makefile | 82 ++++++++++++++++++++++ cpp/examples/README.md | 14 ++++ cpp/examples/WORKSPACE | 49 +++++++++++++ cpp/examples/classification/BUILD | 23 ++++++ cpp/examples/classification/Makefile | 46 ------------ cpp/examples/classification/classify.cc | 28 +++----- cpp/examples/docker/Dockerfile | 54 ++++++++++++++ cpp/examples/docker/docker.mk | 39 ++++++++++ cpp/examples/docker/include/BUILD | 0 cpp/examples/docker/include/glibc_compat.h | 20 ++++++ cpp/examples/docker/update_sources.sh | 26 +++++++ cpp/examples/lstpu/BUILD | 6 +- cpp/examples/lstpu/Makefile | 33 --------- cpp/examples/lstpu/README.md | 38 ---------- cpp/examples/lstpu/WORKSPACE | 66 ----------------- cpp/examples/lstpu/lstpu.cc | 2 +- 18 files changed, 330 insertions(+), 204 deletions(-) create mode 100644 .gitignore create mode 100644 cpp/examples/.gitignore create mode 100644 cpp/examples/Makefile create mode 100644 cpp/examples/README.md create mode 100644 cpp/examples/WORKSPACE create mode 100644 cpp/examples/classification/BUILD delete mode 100644 cpp/examples/classification/Makefile create mode 100644 cpp/examples/docker/Dockerfile create mode 100644 cpp/examples/docker/docker.mk create mode 100644 cpp/examples/docker/include/BUILD create mode 100644 cpp/examples/docker/include/glibc_compat.h create mode 100755 cpp/examples/docker/update_sources.sh delete mode 100644 cpp/examples/lstpu/Makefile delete mode 100644 cpp/examples/lstpu/README.md delete mode 100644 cpp/examples/lstpu/WORKSPACE diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ffd2a4a --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.idea diff --git a/cpp/examples/.gitignore b/cpp/examples/.gitignore new file mode 100644 index 0000000..f088430 --- /dev/null +++ b/cpp/examples/.gitignore @@ -0,0 +1,7 @@ +.bazelrc +/bazel-* +/out + +** Editors ** +*.swp +*.idea diff --git a/cpp/examples/Makefile b/cpp/examples/Makefile new file mode 100644 index 0000000..f5a07aa --- /dev/null +++ b/cpp/examples/Makefile @@ -0,0 +1,82 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +SHELL := /bin/bash +MAKEFILE_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) +OS := $(shell uname -s) + +# Allowed CPU values: k8, armv7a, aarch64 +ifeq ($(OS),Linux) +CPU ?= k8 +else +$(error $(OS) is not supported) +endif +ifeq ($(filter $(CPU),k8 armv7a aarch64),) +$(error CPU must be k8, armv7a, aarch64) +endif + +# Allowed COMPILATION_MODE values: opt, dbg +COMPILATION_MODE ?= opt +ifeq ($(filter $(COMPILATION_MODE),opt dbg),) +$(error COMPILATION_MODE must be opt or dbg) +endif + +BAZEL_OUT_DIR := $(MAKEFILE_DIR)/bazel-out/$(CPU)-$(COMPILATION_MODE)/bin +BAZEL_BUILD_FLAGS := --crosstool_top=@crosstool//:toolchains \ + --compiler=gcc \ + --compilation_mode=$(COMPILATION_MODE) \ + --copt=-DNPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION \ + --copt=-std=c++14 \ + --verbose_failures \ + --cpu=$(CPU) \ + --experimental_repo_remote_exec + +ifeq ($(COMPILATION_MODE), opt) +BAZEL_BUILD_FLAGS += --linkopt=-Wl,--strip-all +else ifeq ($(COMPILATION_MODE), dbg) +# for now, disable arm_neon in dbg. +# see: https://github.com/tensorflow/tensorflow/issues/33360 +BAZEL_BUILD_FLAGS += --cxxopt -DTF_LITE_DISABLE_X86_NEON +endif + +ifeq ($(CPU),k8) +BAZEL_BUILD_FLAGS += --copt=-includeglibc_compat.h +else ifeq ($(CPU),aarch64) +BAZEL_BUILD_FLAGS += --copt=-ffp-contract=off +else ifeq ($(CPU),armv7a) +BAZEL_BUILD_FLAGS += --copt=-ffp-contract=off +endif + + +DEMO_OUT_DIR := $(MAKEFILE_DIR)/out/$(CPU)/ + +.PHONY: all lstpu classify clean + + +examples: + bazel build $(BAZEL_BUILD_FLAGS) //classification:classify + bazel build $(BAZEL_BUILD_FLAGS) //lstpu:lstpu + mkdir -p $(DEMO_OUT_DIR) + cp -f $(BAZEL_OUT_DIR)/classification/classify \ + $(DEMO_OUT_DIR) + cp -f $(BAZEL_OUT_DIR)/lstpu/lstpu \ + $(DEMO_OUT_DIR) + rm -rf $(MAKEFILE_DIR)/bazel-* + +clean: + rm -rf $(MAKEFILE_DIR)/out + +DOCKER_WORKSPACE=$(MAKEFILE_DIR) +DOCKER_CPUS=k8 armv7a aarch64 armv6 +DOCKER_TAG_BASE=tflite-coral-examples +include $(MAKEFILE_DIR)/docker/docker.mk diff --git a/cpp/examples/README.md b/cpp/examples/README.md new file mode 100644 index 0000000..2089a9d --- /dev/null +++ b/cpp/examples/README.md @@ -0,0 +1,14 @@ +# Simple C++ code example + +These examples show how to build a simple C++ program that uses the EdgeTPU +runtime library. This subdirectory contains 2 different examples: + +- lstpu: + - lists available Edge TPU devices (It does not perform inference). +- classification: + - classify a bmp image using a classification model. + +## Compile the examples +```bash +$ make DOCKER_TARGET=examples DOCKER_CPUS=k8 docker-build +``` \ No newline at end of file diff --git a/cpp/examples/WORKSPACE b/cpp/examples/WORKSPACE new file mode 100644 index 0000000..a0e1fdc --- /dev/null +++ b/cpp/examples/WORKSPACE @@ -0,0 +1,49 @@ +# Copyright 2020 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +workspace(name = "tflite_examples") + +load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") + +TENSORFLOW_COMMIT = "48c3bae94a8b324525b45f157d638dfd4e8c3be1" +TENSORFLOW_SHA256 = "363420a67b4cfa271cd21e5c8fac0d7d91b18b02180671c3f943c887122499d8" + +# These values come from the Tensorflow workspace. If the TF commit is updated, +# these should be updated to match. +IO_BAZEL_RULES_CLOSURE_COMMIT = "308b05b2419edb5c8ee0471b67a40403df940149" +IO_BAZEL_RULES_CLOSURE_SHA256 = "5b00383d08dd71f28503736db0500b6fb4dda47489ff5fc6bed42557c07c6ba9" + +CORAL_CROSSTOOL_COMMIT = "142e930ac6bf1295ff3ba7ba2b5b6324dfb42839" +CORAL_CROSSTOOL_SHA256 = "088ef98b19a45d7224be13636487e3af57b1564880b67df7be8b3b7eee4a1bfc" + +# Configure libedgetpu and downstream libraries (TF and Crosstool). +http_archive( + name = "libedgetpu", + sha256 = "d76d18c5a96758dd620057028cdd4e129bd885480087a5c7334070bba3797e58", + strip_prefix = "libedgetpu-14eee1a076aa1af7ec1ae3c752be79ae2604a708", + urls = [ + "https://github.com/google-coral/libedgetpu/archive/14eee1a076aa1af7ec1ae3c752be79ae2604a708.tar.gz" + ], +) + +load("@libedgetpu//:workspace.bzl", "libedgetpu_dependencies") +libedgetpu_dependencies(TENSORFLOW_COMMIT, TENSORFLOW_SHA256, + IO_BAZEL_RULES_CLOSURE_COMMIT,IO_BAZEL_RULES_CLOSURE_SHA256, + CORAL_CROSSTOOL_COMMIT,CORAL_CROSSTOOL_SHA256) + + +load("@org_tensorflow//tensorflow:workspace.bzl", "tf_workspace") +tf_workspace(tf_repo_name = "org_tensorflow") + +load("@coral_crosstool//:configure.bzl", "cc_crosstool") +cc_crosstool(name = "crosstool", additional_system_include_directories=["//docker/include"]) diff --git a/cpp/examples/classification/BUILD b/cpp/examples/classification/BUILD new file mode 100644 index 0000000..bde0650 --- /dev/null +++ b/cpp/examples/classification/BUILD @@ -0,0 +1,23 @@ +# Copyright 2019 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +cc_binary( + name = "classify", + srcs = ["classify.cc"], + deps = [ + "@libedgetpu//tflite/public:oss_edgetpu_direct_all", + "@org_tensorflow//tensorflow/lite:builtin_op_data", + "@org_tensorflow//tensorflow/lite:framework", + "@org_tensorflow//tensorflow/lite/kernels:builtin_ops", + ], +) diff --git a/cpp/examples/classification/Makefile b/cpp/examples/classification/Makefile deleted file mode 100644 index ee12d65..0000000 --- a/cpp/examples/classification/Makefile +++ /dev/null @@ -1,46 +0,0 @@ -# This is a Makefile to cross-compile classify.cc example. -# 1. Download latest Edge TPU runtime archive from https://coral.ai/software/ -# and extract next to the Makefile: -# $ wget https://dl.google.com/coral/edgetpu_api/edgetpu_runtime_20200710.zip -# $ unzip edgetpu_runtime_20200710.zip -# 2. Download TensorFlow to the Linux machine: -# $ git clone https://github.com/tensorflow/tensorflow.git -# 3. Download external dependencies for TensorFlow Lite: -# $ tensorflow/tensorflow/lite/tools/make/download_dependencies.sh -# 4. Cross-compile TensorFlow Lite for aarch64: -# $ tensorflow/tensorflow/lite/tools/make/build_aarch64_lib.sh -# 5. Cross-compile classify.cc example for aarch64: -# $ TENSORFLOW_DIR= make -# 6. Copy the following files to Coral Dev board: -# * Generated 'classify' binary -# * Model file 'mobilenet_v1_1.0_224_quant_edgetpu.tflite' from 'test_data' -# * Label file 'imagenet_labels.txt ' from 'test_data' -# * Image file 'resized_cat.bmp' from 'test_data' -# and finally run 'classify' binary on the board: -# $ classify mobilenet_v1_1.0_224_quant_edgetpu.tflite \ -# imagenet_labels.txt \ -# resized_cat.bmp \ -# 0.0001 -# INFO: Initialized TensorFlow Lite runtime. -# INFO: Replacing 1 node(s) with delegate (EdgeTpuDelegateForCustomOp) node, yielding 1 partitions. -# 0.81641 286 Egyptian cat -# 0.10938 283 tiger cat -# 0.03516 282 tabby, tabby cat -# 0.01172 812 space heater -# 0.00781 754 radiator -# 0.00391 540 doormat, welcome mat -# 0.00391 285 Siamese cat, Siamese -MAKEFILE_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) -TENSORFLOW_DIR ?= - -classify: classify.cc - aarch64-linux-gnu-g++ -std=c++11 -o classify classify.cc \ - -I$(MAKEFILE_DIR)/edgetpu_runtime/libedgetpu/ \ - -I$(TENSORFLOW_DIR) \ - -I$(TENSORFLOW_DIR)/tensorflow/lite/tools/make/downloads/flatbuffers/include \ - -L$(TENSORFLOW_DIR)/tensorflow/lite/tools/make/gen/linux_aarch64/lib \ - -L$(MAKEFILE_DIR)/edgetpu_runtime/libedgetpu/direct/aarch64/ \ - -ltensorflow-lite -l:libedgetpu.so.1.0 -lpthread -lm -ldl - -clean: - rm -f classify diff --git a/cpp/examples/classification/classify.cc b/cpp/examples/classification/classify.cc index 69dfe04..25ae9d1 100644 --- a/cpp/examples/classification/classify.cc +++ b/cpp/examples/classification/classify.cc @@ -8,10 +8,11 @@ #include #include -#include "edgetpu_c.h" +#include "tensorflow/lite/builtin_op_data.h" #include "tensorflow/lite/interpreter.h" #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" +#include "tflite/public/edgetpu.h" namespace { constexpr size_t kBmpFileHeaderSize = 14; @@ -130,17 +131,6 @@ int main(int argc, char* argv[]) { const std::string image_file = argv[3]; const float threshold = std::stof(argv[4]); - // Find TPU device. - size_t num_devices; - std::unique_ptr devices( - edgetpu_list_devices(&num_devices), &edgetpu_free_devices); - - if (num_devices == 0) { - std::cerr << "No connected TPU found" << std::endl; - return 1; - } - const auto& device = devices.get()[0]; - // Load labels. auto labels = ReadLabels(label_file); if (labels.empty()) { @@ -164,17 +154,19 @@ int main(int argc, char* argv[]) { return 1; } - // Create interpreter. + // Get edgetpu context. + auto edgetpu_context = edgetpu::EdgeTpuManager::GetSingleton()->OpenDevice(); tflite::ops::builtin::BuiltinOpResolver resolver; + resolver.AddCustom(edgetpu::kCustomOp, edgetpu::RegisterCustomOp()); + + // Create interpreter. std::unique_ptr interpreter; if (tflite::InterpreterBuilder(*model, resolver)(&interpreter) != kTfLiteOk) { std::cerr << "Cannot create interpreter" << std::endl; - return 1; + exit(EXIT_FAILURE); } - - auto* delegate = - edgetpu_create_delegate(device.type, device.path, nullptr, 0); - interpreter->ModifyGraphWithDelegate({delegate, edgetpu_free_delegate}); + interpreter->SetExternalContext(kTfLiteEdgeTpuContext, edgetpu_context.get()); + interpreter->SetNumThreads(1); // Allocate tensors. if (interpreter->AllocateTensors() != kTfLiteOk) { diff --git a/cpp/examples/docker/Dockerfile b/cpp/examples/docker/Dockerfile new file mode 100644 index 0000000..9afc6d0 --- /dev/null +++ b/cpp/examples/docker/Dockerfile @@ -0,0 +1,54 @@ +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +ARG IMAGE +FROM ${IMAGE} + +COPY update_sources.sh / +RUN /update_sources.sh + +RUN dpkg --add-architecture armhf +RUN dpkg --add-architecture arm64 +RUN apt-get update && apt-get install -y \ + sudo \ + debhelper \ + build-essential \ + crossbuild-essential-armhf \ + crossbuild-essential-arm64 \ + libusb-1.0-0-dev \ + libusb-1.0-0-dev:arm64 \ + libusb-1.0-0-dev:armhf \ + python \ + python3-all \ + python3-six \ + zlib1g-dev \ + zlib1g-dev:armhf \ + zlib1g-dev:arm64 \ + pkg-config \ + zip \ + unzip \ + curl \ + wget \ + git \ + subversion \ + vim \ + python3-numpy + +ARG BAZEL_VERSION=2.1.0 +RUN wget -O /bazel https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-installer-linux-x86_64.sh && \ + bash /bazel && \ + rm -f /bazel + +RUN svn export https://github.com/google-coral/edgetpu/trunk/libedgetpu /libedgetpu + diff --git a/cpp/examples/docker/docker.mk b/cpp/examples/docker/docker.mk new file mode 100644 index 0000000..fbcd7ff --- /dev/null +++ b/cpp/examples/docker/docker.mk @@ -0,0 +1,39 @@ +DOCKER_MK_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) + +# Docker +DOCKER_CPUS ?= k8 armv7a armv6 aarch64 +DOCKER_TARGETS ?= +DOCKER_IMAGE ?= debian:buster +DOCKER_TAG_BASE ?= "bazel-cross" +DOCKER_TAG := "$(DOCKER_TAG_BASE)-$(subst :,-,$(DOCKER_IMAGE))" +DOCKER_SHELL_COMMAND ?= + +ifndef DOCKER_WORKSPACE +$(error DOCKER_WORKSPACE is not defined) +endif + +WORKSPACE := /workspace +MAKE_COMMAND := \ +for cpu in $(DOCKER_CPUS); do \ + make CPU=\$${cpu} COMPILATION_MODE=$(COMPILATION_MODE) -C /workspace $(DOCKER_TARGETS) || exit 1; \ +done + +define run_command +chmod a+w /; \ +groupadd --gid $(shell id -g) $(shell id -g -n); \ +useradd -m -e '' -s /bin/bash --gid $(shell id -g) --uid $(shell id -u) $(shell id -u -n); \ +echo '$(shell id -u -n) ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers; \ +su $(shell id -u -n) $(if $(1),-c '$(1)',) +endef + +docker-image: + docker build $(DOCKER_IMAGE_OPTIONS) -t $(DOCKER_TAG) \ + --build-arg IMAGE=$(DOCKER_IMAGE) $(DOCKER_MK_DIR) + +docker-shell: docker-image + docker run --rm -i --tty -v $(DOCKER_WORKSPACE):$(WORKSPACE) --workdir $(WORKSPACE) \ + $(DOCKER_TAG) /bin/bash -c "$(call run_command,$(DOCKER_SHELL_COMMAND))" + +docker-build: docker-image + docker run --rm -i $(shell tty -s && echo --tty) -v $(DOCKER_WORKSPACE):$(WORKSPACE) \ + $(DOCKER_TAG) /bin/bash -c "$(call run_command,$(MAKE_COMMAND))" diff --git a/cpp/examples/docker/include/BUILD b/cpp/examples/docker/include/BUILD new file mode 100644 index 0000000..e69de29 diff --git a/cpp/examples/docker/include/glibc_compat.h b/cpp/examples/docker/include/glibc_compat.h new file mode 100644 index 0000000..a690c2a --- /dev/null +++ b/cpp/examples/docker/include/glibc_compat.h @@ -0,0 +1,20 @@ +/* + * Copyright 2021 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// These symbols are from older GLIBC than installed on 18.04. +__asm__(".symver powf,powf@GLIBC_2.2.5"); +__asm__(".symver logf,logf@GLIBC_2.2.5"); +__asm__(".symver expf,expf@GLIBC_2.2.5"); diff --git a/cpp/examples/docker/update_sources.sh b/cpp/examples/docker/update_sources.sh new file mode 100755 index 0000000..dc60719 --- /dev/null +++ b/cpp/examples/docker/update_sources.sh @@ -0,0 +1,26 @@ +#!/bin/bash +# Copyright 2021 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +. /etc/os-release + +[[ "${NAME}" == "Ubuntu" ]] || exit 0 + +sed -i "s/deb\ /deb \[arch=amd64\]\ /g" /etc/apt/sources.list + +cat <> /etc/apt/sources.list +deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME} main universe +deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME}-updates main universe +deb [arch=arm64,armhf] http://ports.ubuntu.com/ubuntu-ports ${UBUNTU_CODENAME}-security main universe +EOT diff --git a/cpp/examples/lstpu/BUILD b/cpp/examples/lstpu/BUILD index 04e3194..e5db21f 100644 --- a/cpp/examples/lstpu/BUILD +++ b/cpp/examples/lstpu/BUILD @@ -15,7 +15,9 @@ cc_binary( name = "lstpu", srcs = ["lstpu.cc"], deps = [ - "@edgetpu//libedgetpu:header", + "@libedgetpu//tflite/public:oss_edgetpu_direct_all", + "@org_tensorflow//tensorflow/lite:builtin_op_data", + "@org_tensorflow//tensorflow/lite:framework", + "@org_tensorflow//tensorflow/lite/kernels:builtin_ops", ], - copts = ["-Iexternal/edgetpu/libedgetpu"], ) diff --git a/cpp/examples/lstpu/Makefile b/cpp/examples/lstpu/Makefile deleted file mode 100644 index 0f8bc80..0000000 --- a/cpp/examples/lstpu/Makefile +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright 2019 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -SHELL := /bin/bash -MAKEFILE_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST)))) -# Allowed CPU values: k8, armv7a, aarch64 -CPU ?= k8 -# Allowed COMPILATION_MODE values: opt, dbg -COMPILATION_MODE ?= opt - -BAZEL_OUT_DIR := $(MAKEFILE_DIR)/bazel-out/$(CPU)-$(COMPILATION_MODE)/bin -BAZEL_BUILD_FLAGS := --crosstool_top=@crosstool//:toolchains \ - --compilation_mode=$(COMPILATION_MODE) \ - --compiler=gcc \ - --cpu=$(CPU) \ - --linkopt=-L$(shell bazel info output_base)/external/edgetpu/libedgetpu/direct/$(CPU) \ - --linkopt=-l:libedgetpu.so.1 - -lstpu: - bazel build $(BAZEL_BUILD_FLAGS) //:lstpu - -clean: - bazel clean diff --git a/cpp/examples/lstpu/README.md b/cpp/examples/lstpu/README.md deleted file mode 100644 index c161db8..0000000 --- a/cpp/examples/lstpu/README.md +++ /dev/null @@ -1,38 +0,0 @@ -# Simple C++ code example - -This example shows how to build a simple C++ program that uses the Edge TPU -runtime library to lists available Edge TPU devices. (It does not perform an -inference.) - -## Requirements - -You need to install [Bazel](https://bazel.build/) in order to build the binary. -Follow the [Bazel install -instructions](https://docs.bazel.build/versions/master/install.html) - -The example is configured to use the cross-compilation toolchain definition from -[crosstool](https://github.com/google-coral/crosstool), but you don't need -to download that repo. - -## Compile the example - -For the native compilation you need to install at least `build-essential` -package: - -``` -sudo apt-get install -y build-essential -``` - -Then run `make` command. - -For cross-compilation you need to install `build-essential` packages for -the corresponding architectures: - -``` -sudo apt-get install -y crossbuild-essential-armhf \ - crossbuild-essential-arm64 -``` - -Then run `make CPU=armv7a` or `make CPU=aarch64`. - -Find the output binary file inside `blaze-out` directory. diff --git a/cpp/examples/lstpu/WORKSPACE b/cpp/examples/lstpu/WORKSPACE deleted file mode 100644 index dcbef1d..0000000 --- a/cpp/examples/lstpu/WORKSPACE +++ /dev/null @@ -1,66 +0,0 @@ -# Copyright 2019 Google LLC -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# https://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -workspace(name = "lstpu") - -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -http_archive( - name = "io_bazel_rules_closure", - sha256 = "5b00383d08dd71f28503736db0500b6fb4dda47489ff5fc6bed42557c07c6ba9", - strip_prefix = "rules_closure-308b05b2419edb5c8ee0471b67a40403df940149", - urls = [ - "https://storage.googleapis.com/mirror.tensorflow.org/github.com/bazelbuild/rules_closure/archive/308b05b2419edb5c8ee0471b67a40403df940149.tar.gz", - "https://github.com/bazelbuild/rules_closure/archive/308b05b2419edb5c8ee0471b67a40403df940149.tar.gz", # 2019-06-13 - ], -) - -http_archive( - name = "bazel_skylib", - sha256 = "1dde365491125a3db70731e25658dfdd3bc5dbdfd11b840b3e987ecf043c7ca0", - urls = ["https://github.com/bazelbuild/bazel-skylib/releases/download/0.9.0/bazel_skylib-0.9.0.tar.gz"], -) - -TENSORFLOW_COMMIT = "d855adfc5a0195788bf5f92c3c7352e638aa1109"; -TENSORFLOW_SHA256 = "b8a691dbea2bb028fa8f7ce407b70ad236dae0a8705c8010dc7bad8af7e93bac" -http_archive( - name = "org_tensorflow", - sha256 = TENSORFLOW_SHA256, - strip_prefix = "tensorflow-" + TENSORFLOW_COMMIT, - urls = [ - "https://github.com/tensorflow/tensorflow/archive/" + TENSORFLOW_COMMIT + ".tar.gz", - ], -) - -load("@org_tensorflow//tensorflow:workspace.bzl", "tf_workspace") -tf_workspace(tf_repo_name = "org_tensorflow") - -http_archive( - name = "edgetpu", - sha256 = "dc5eb443fa1b4132f6828fc0796169e0595643d415b585351839d3c4f796e6a8", - strip_prefix = "edgetpu-14237f65ba07b7b1d8287e9f60dd20c88562871a", - urls = [ - "https://github.com/google-coral/edgetpu/archive/14237f65ba07b7b1d8287e9f60dd20c88562871a.tar.gz", - ] -) - -http_archive( - name = "coral_crosstool", - sha256 = "cb31b1417ccdcf7dd9fca5ec63e1571672372c30427730255997a547569d2feb", - strip_prefix = "crosstool-9e00d5be43bf001f883b5700f5d04882fea00229", - urls = [ - "https://github.com/google-coral/crosstool/archive/9e00d5be43bf001f883b5700f5d04882fea00229.tar.gz", - ], -) -load("@coral_crosstool//:configure.bzl", "cc_crosstool") -cc_crosstool(name = "crosstool") diff --git a/cpp/examples/lstpu/lstpu.cc b/cpp/examples/lstpu/lstpu.cc index 6755840..e502026 100644 --- a/cpp/examples/lstpu/lstpu.cc +++ b/cpp/examples/lstpu/lstpu.cc @@ -14,7 +14,7 @@ #include #include -#include "edgetpu_c.h" +#include "tflite/public/edgetpu_c.h" std::string ToString(edgetpu_device_type type) { switch (type) {