Skip to content

Commit

Permalink
Release Frogfish
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitriykovalev committed Nov 5, 2020
1 parent f8cac10 commit 14eee1a
Show file tree
Hide file tree
Showing 160 changed files with 4,484 additions and 1,145 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
bazel-*
out/
WORKSPACE
7 changes: 7 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ exports_files([
"LICENSE",
])

config_setting(
name = "opt",
values = {
"compilation_mode": "opt",
},
)

# If --define darwinn_portable=1, compile without google3 deps.
config_setting(
name = "darwinn_portable",
Expand Down
39 changes: 19 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,15 @@
# See the License for the specific language governing permissions and
# limitations under the License.
SHELL := /bin/bash
PYTHON3 ?= python3
MAKEFILE_DIR := $(realpath $(dir $(lastword $(MAKEFILE_LIST))))
OUT_DIR := $(MAKEFILE_DIR)/out
OS := $(shell uname -s)

ifeq ($(OS),Linux)
CPU ?= k8
WORKSPACE_PLATFORM_FILE := WORKSPACE.linux
else ifeq ($(OS),Darwin)
CPU ?= darwin
WORKSPACE_PLATFORM_FILE := WORKSPACE.darwin
else
$(error $(OS) is not supported)
endif
Expand All @@ -40,23 +39,16 @@ BAZEL_OUT_DIR := $(MAKEFILE_DIR)/bazel-out/$(CPU)-$(COMPILATION_MODE)/bin
# Linux-specific parameters
BAZEL_BUILD_TARGET_Linux := //tflite/public:libedgetpu_direct_all.so
BAZEL_BUILD_FLAGS_Linux := --crosstool_top=@crosstool//:toolchains \
--compiler=gcc \
--linkopt=-l:libusb-1.0.so
--compiler=gcc
BAZEL_BUILD_OUTPUT_FILE_Linux := libedgetpu.so.1.0
BAZEL_BUILD_OUTPUT_SYMLINK_Linux := libedgetpu.so.1

ifeq ($(COMPILATION_MODE), opt)
BAZEL_BUILD_FLAGS_Linux += --linkopt=-Wl,--strip-all
endif
ifeq ($(CPU), armv6)
BAZEL_BUILD_FLAGS_Linux += --linkopt=-L/usr/lib/arm-linux-gnueabihf/
endif

# Darwin-specific parameters
BAZEL_BUILD_TARGET_Darwin := //tflite/public:libedgetpu_direct_usb.dylib
BAZEL_BUILD_FLAGS_Darwin := --linkopt=-L/opt/local/lib \
--linkopt=-lusb-1.0 \
--copt=-fvisibility=hidden
BAZEL_BUILD_OUTPUT_FILE_Darwin := libedgetpu.1.0.dylib
BAZEL_BUILD_OUTPUT_SYMLINK_Darwin := libedgetpu.1.dylib

Expand All @@ -65,11 +57,10 @@ BAZEL_BUILD_FLAGS := --sandbox_debug --subcommands \
--experimental_repo_remote_exec \
--compilation_mode=$(COMPILATION_MODE) \
--define darwinn_portable=1 \
--copt=-DSTRIP_LOG=1 \
--copt=-fno-rtti \
--copt=-fno-exceptions \
--copt='-D__FILE__=""' \
--cpu=$(CPU)
--action_env PYTHON_BIN_PATH=$(shell which $(PYTHON3)) \
--cpu=$(CPU) \
--embed_label='TENSORFLOW_COMMIT=$(shell grep "TENSORFLOW_COMMIT =" $(MAKEFILE_DIR)/workspace.bzl | grep -o '[0-9a-f]\{40\}')' \
--stamp
BAZEL_BUILD_FLAGS += $(BAZEL_BUILD_FLAGS_$(OS))
BAZEL_BUILD_TARGET := $(BAZEL_BUILD_TARGET_$(OS))
BAZEL_BUILD_OUTPUT_FILE := $(BAZEL_BUILD_OUTPUT_FILE_$(OS))
Expand All @@ -95,23 +86,31 @@ endif
.PHONY: libedgetpu \
libedgetpu-direct \
libedgetpu-throttled \
workspace \
deb \
deb-armhf \
deb-arm64 \
clean

libedgetpu: libedgetpu-direct libedgetpu-throttled

libedgetpu-direct: workspace
libedgetpu-direct:
bazel build $(BAZEL_BUILD_FLAGS) $(BAZEL_BUILD_TARGET)
$(call copy_out,direct)
$(call strip_out,direct)

libedgetpu-throttled: workspace
libedgetpu-throttled:
bazel build $(BAZEL_BUILD_FLAGS) --copt=-DTHROTTLE_EDGE_TPU $(BAZEL_BUILD_TARGET)
$(call copy_out,throttled)
$(call strip_out,throttled)

workspace: bazel/WORKSPACE bazel/$(WORKSPACE_PLATFORM_FILE)
cat $^ > WORKSPACE
deb:
dpkg-buildpackage -rfakeroot -us -uc -tc -b

deb-armhf:
dpkg-buildpackage -rfakeroot -us -uc -tc -b -a armhf -d

deb-arm64:
dpkg-buildpackage -rfakeroot -us -uc -tc -b -a arm64 -d

clean:
rm -rf $(OUT_DIR)
Expand Down
23 changes: 23 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -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.
workspace(name = "libedgetpu")

load(":workspace.bzl", "libedgetpu_dependencies")

libedgetpu_dependencies()
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")
34 changes: 18 additions & 16 deletions api/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

load("//:libedgetpu_cc_rules.bzl", "libedgetpu_cc_library")

# Description:
# Darwinn API headers
load(
Expand All @@ -23,12 +25,12 @@ package(default_visibility = ["//visibility:public"])

licenses(["notice"])

cc_library(
libedgetpu_cc_library(
name = "chip",
hdrs = ["chip.h"],
)

cc_library(
libedgetpu_cc_library(
name = "tensor_util",
srcs = ["tensor_util.cc"],
hdrs = ["tensor_util.h"],
Expand All @@ -39,7 +41,7 @@ cc_library(
],
)

cc_library(
libedgetpu_cc_library(
name = "layer_information",
srcs = ["layer_information.cc"],
hdrs = ["layer_information.h"],
Expand All @@ -51,7 +53,7 @@ cc_library(
],
)

cc_library(
libedgetpu_cc_library(
name = "request",
hdrs = ["request.h"],
deps = [
Expand All @@ -60,7 +62,7 @@ cc_library(
],
)

cc_library(
libedgetpu_cc_library(
name = "driver",
hdrs = ["driver.h"],
deps = [
Expand All @@ -73,7 +75,7 @@ cc_library(
],
)

cc_library(
libedgetpu_cc_library(
name = "driver_options_helper",
srcs = ["driver_options_helper.cc"],
hdrs = ["driver_options_helper.h"],
Expand All @@ -83,20 +85,20 @@ cc_library(
],
)

cc_library(
libedgetpu_cc_library(
name = "allocated_buffer",
srcs = ["allocated_buffer.cc"],
hdrs = ["allocated_buffer.h"],
deps = ["//port"],
)

cc_library(
libedgetpu_cc_library(
name = "dram_buffer",
hdrs = ["dram_buffer.h"],
deps = ["//port"],
)

cc_library(
libedgetpu_cc_library(
name = "buffer",
srcs = ["buffer.cc"],
hdrs = ["buffer.h"],
Expand All @@ -107,7 +109,7 @@ cc_library(
],
)

cc_library(
libedgetpu_cc_library(
name = "driver_factory",
srcs = ["driver_factory.cc"],
hdrs = ["driver_factory.h"],
Expand All @@ -119,7 +121,7 @@ cc_library(
],
)

cc_library(
libedgetpu_cc_library(
name = "package_reference",
hdrs = ["package_reference.h"],
deps = [
Expand All @@ -130,7 +132,7 @@ cc_library(
],
)

cc_library(
libedgetpu_cc_library(
name = "runtime_version",
hdrs = ["runtime_version.h"],
)
Expand All @@ -141,15 +143,15 @@ flatbuffer_cc_library(
flatc_args = [""],
)

cc_library(
libedgetpu_cc_library(
name = "timing",
hdrs = ["timing.h"],
deps = [
"//port",
],
)

cc_library(
libedgetpu_cc_library(
name = "watchdog",
srcs = ["watchdog.cc"],
hdrs = ["watchdog.h"],
Expand All @@ -161,7 +163,7 @@ cc_library(
],
)

cc_library(
libedgetpu_cc_library(
name = "telemeter_interface",
hdrs = [
"telemeter_interface.h",
Expand All @@ -171,7 +173,7 @@ cc_library(
],
)

cc_library(
libedgetpu_cc_library(
name = "execution_context_interface",
hdrs = [
"execution_context_interface.h",
Expand Down
3 changes: 1 addition & 2 deletions api/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ class Driver {
// integer values may be different from those in NNAPI or other APIs. The
// values here are defined in the order of priority when there are multiple
// models requesting different preferences (e.g. sustained speed takes
// priority over low power). For more information, please see:
// http://go/noronha-execution-preference
// priority over low power).
enum class ExecutionPreference {
// Run at the absolute maximum performance.
kSingleFastAnswer = 0,
Expand Down
60 changes: 35 additions & 25 deletions api/layer_information.cc
Original file line number Diff line number Diff line change
Expand Up @@ -203,30 +203,41 @@ bool OutputLayerInformation::NeedsRelayout() const {
// TODO Add unit tests for this method.
util::Status OutputLayerInformation::Relayout(unsigned char* dest,
const unsigned char* src) const {
// TODO: re-use the same buffer and avoid an unnecessary memcopy
// when relayout is not needed.
if (!NeedsRelayout()) {
memcpy(dest, src,
batch_dim() * y_dim() * x_dim() * z_dim() * DataTypeSize());
return util::OkStatus();
}

if (output_layer_->shape_info()) {
// If output shape info exists in the executable, use the new re-layout
// function. Currently, this is only enabled for models with multiple
// batches.
return RelayoutWithShapeInformation(dest, src);
}

const auto data_type_size = DataTypeSize();
const int z_bytes = z_dim() * data_type_size;
const int executions = execution_count_per_inference();

if (executions == 1) {
// Handle case when execution count is equal to 1, since if execution count
// is greater than 1, there might be padding data in-between.

// TODO: re-use the same buffer and avoid an unnecessary
// memcopy when relayout is not needed.
if (!NeedsRelayout()) {
memcpy(dest, src, batch_dim() * y_dim() * x_dim() * z_bytes);
return util::OkStatus();
}

if (output_layer_->shape_info()) {
// If output shape info exists in the executable, use the new re-layout
// function. Currently, this is only enabled for models with multiple
// batches.
return RelayoutWithShapeInformation(dest, src);
}
} else if (PaddedSizeBytes() == ActualSizeBytes() && !NeedsRelayout()) {
// Use memcpy if `executions` is greater than 1 and there is no internal
// padding between iterations.
if (dest != src) {
memcpy(dest, src, ActualSizeBytes());
}
return util::OkStatus();
}

if (y_dim() == 1 && x_dim() == 1) {
// One dimensional output (only z-dimension).
if (src != dest) {
const int padded_size_bytes = PaddedSizeBytes();
const int actual_size_bytes = ActualSizeBytes();
const int executions = execution_count_per_inference();
if (executions == 1 || padded_size_bytes == actual_size_bytes) {
memcpy(dest, src, z_bytes * executions);
} else {
Expand Down Expand Up @@ -276,8 +287,7 @@ util::Status OutputLayerInformation::Relayout(unsigned char* dest,
// provided we have a guaranteed way of ensuring this function would be inlined
// so that the compiler optimizations based on compile-time-constants can kick
// in.
#define RELAYOUT_WITH_Z_BYTES_SPECIALIZATION( \
num_z_bytes, num_z_bytes_padded) \
#define RELAYOUT_WITH_Z_BYTES_SPECIALIZATION(num_z_bytes, num_z_bytes_padded) \
do { \
for (int y = 0; y < y_dim(); ++y) { \
const auto y_buffer_index = GetYBufferIndex(y); \
Expand Down Expand Up @@ -330,6 +340,12 @@ util::Status OutputLayerInformation::Relayout(unsigned char* dest,
active_tile_x_sizes.size() > 1 || first_y_tile != last_y_tile;

if (need_relayout) {
// TODO: If iteration count is more than 1, we need to make
// sure we advance 'src' and 'dest' correctly due to padding issue. We
// don't have test case now.
CHECK_EQ(executions, 1)
<< "Verification is missing if execution count is greater than 1";

// If there's no z padding, copy one xz block on one tile at a time.
for (int y = 0; y < y_dim(); ++y) {
const auto y_buffer_index = GetYBufferIndex(y);
Expand All @@ -347,17 +363,11 @@ util::Status OutputLayerInformation::Relayout(unsigned char* dest,
}
} else {
// TODO: avoid copy and assign in caller directly.
memcpy(dest, src, x_dim() * y_dim() * z_bytes);
memcpy(dest, src, x_dim() * y_dim() * z_bytes * executions);
}
}

#undef RELAYOUT_WITH_Z_BYTES_SPECIALIZATION

// TODO: If iteration count is more than 1, we need to make sure we
// advance 'src' and 'dest' correctly due to padding issue. We don't have
// test case now.
CHECK_EQ(execution_count_per_inference(), 1)
<< "Verification is missing if execution count is greater than 1";
}

return util::OkStatus();
Expand Down
Loading

0 comments on commit 14eee1a

Please sign in to comment.