From f8cac1044e3ca32b6a9c8712ac6d063e58f19fe1 Mon Sep 17 00:00:00 2001 From: Dmitry Kovalev Date: Fri, 19 Jun 2020 02:15:13 -0700 Subject: [PATCH] Add Docker-based build process --- .gitignore | 3 + CONTRIBUTING.md | 2 +- Makefile | 64 +++++++++++++++---- README.md | 26 +++++++- WORKSPACE | 75 ----------------------- api/watchdog.cc | 15 +++-- api/watchdog.h | 10 ++- build.bat | 5 +- docker/Dockerfile | 38 ++++++++++++ docker/Dockerfile.windows | 42 +++++++++++++ docker/update_sources.sh | 12 ++++ driver/config/beagle/beagle_csr_offsets.h | 18 ++++++ driver/config/scalar_core_csr_offsets.h | 14 +++++ driver/config/tile_csr_offsets.h | 4 ++ driver/driver.cc | 10 ++- driver/interrupt/BUILD | 6 +- driver/interrupt/interrupt_controller.cc | 33 +++++++--- port/default/builddata.h | 2 +- rename_library.py | 2 +- 19 files changed, 269 insertions(+), 112 deletions(-) create mode 100644 .gitignore delete mode 100644 WORKSPACE create mode 100644 docker/Dockerfile create mode 100644 docker/Dockerfile.windows create mode 100755 docker/update_sources.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e6c73c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bazel-* +out/ +WORKSPACE diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3414802..200f0ca 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -25,4 +25,4 @@ information on using pull requests. ## Community Guidelines This project follows -[Google's Open Source Community Guidelines](https://opensource.google/conduct/). \ No newline at end of file +[Google's Open Source Community Guidelines](https://opensource.google/conduct/). diff --git a/Makefile b/Makefile index 4e1e782..b6f8edf 100644 --- a/Makefile +++ b/Makefile @@ -18,8 +18,10 @@ 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 @@ -37,12 +39,9 @@ BAZEL_OUT_DIR := $(MAKEFILE_DIR)/bazel-out/$(CPU)-$(COMPILATION_MODE)/bin # Linux-specific parameters BAZEL_BUILD_TARGET_Linux := //tflite/public:libedgetpu_direct_all.so -# --experimental_repo_remote_exec for remotable parameter used in -# --`repository_rule` from TF. BAZEL_BUILD_FLAGS_Linux := --crosstool_top=@crosstool//:toolchains \ --compiler=gcc \ - --linkopt=-l:libusb-1.0.so \ - --experimental_repo_remote_exec + --linkopt=-l:libusb-1.0.so BAZEL_BUILD_OUTPUT_FILE_Linux := libedgetpu.so.1.0 BAZEL_BUILD_OUTPUT_SYMLINK_Linux := libedgetpu.so.1 @@ -63,10 +62,10 @@ BAZEL_BUILD_OUTPUT_SYMLINK_Darwin := libedgetpu.1.dylib # Common parameters BAZEL_BUILD_FLAGS := --sandbox_debug --subcommands \ + --experimental_repo_remote_exec \ --compilation_mode=$(COMPILATION_MODE) \ --define darwinn_portable=1 \ --copt=-DSTRIP_LOG=1 \ - --copt=-DEDGETPU_EXTERNAL_RELEASE_RUNTIME \ --copt=-fno-rtti \ --copt=-fno-exceptions \ --copt='-D__FILE__=""' \ @@ -93,23 +92,66 @@ endef endif endif +.PHONY: libedgetpu \ + libedgetpu-direct \ + libedgetpu-throttled \ + workspace \ + clean + libedgetpu: libedgetpu-direct libedgetpu-throttled -libedgetpu-direct: +libedgetpu-direct: workspace bazel build $(BAZEL_BUILD_FLAGS) $(BAZEL_BUILD_TARGET) $(call copy_out,direct) $(call strip_out,direct) -libedgetpu-throttled: +libedgetpu-throttled: workspace 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 + clean: rm -rf $(OUT_DIR) -ifdef DOCKER_MK +################################################################################ +# Docker commands +################################################################################ +DOCKER_CONTEXT_DIR := $(MAKEFILE_DIR)/docker DOCKER_WORKSPACE := $(MAKEFILE_DIR) -DOCKER_TAG_BASE=coral-libedgetpu -include $(DOCKER_MK) -endif +DOCKER_CONTAINER_WORKSPACE := /workspace +DOCKER_CPUS ?= k8 armv7a armv6 aarch64 +DOCKER_TARGETS ?= +DOCKER_IMAGE ?= debian:stretch +DOCKER_TAG_BASE ?= libedgetpu-cross +DOCKER_TAG := "$(DOCKER_TAG_BASE)-$(subst :,-,$(DOCKER_IMAGE))" +DOCKER_SHELL_COMMAND ?= + +DOCKER_MAKE_COMMAND := \ +for cpu in $(DOCKER_CPUS); do \ + make CPU=\$${cpu} -C $(DOCKER_CONTAINER_WORKSPACE) $(DOCKER_TARGETS) || exit 1; \ +done + +define docker_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_CONTEXT_DIR) + +docker-shell: docker-image + docker run --rm -i --tty --workdir $(DOCKER_CONTAINER_WORKSPACE) \ + -v $(DOCKER_WORKSPACE):$(DOCKER_CONTAINER_WORKSPACE) \ + $(DOCKER_TAG) /bin/bash -c "$(call docker_run_command,$(DOCKER_SHELL_COMMAND))" + +docker-build: docker-image + docker run --rm -i $(shell tty -s && echo --tty) \ + -v $(DOCKER_WORKSPACE):$(DOCKER_CONTAINER_WORKSPACE) \ + $(DOCKER_TAG) /bin/bash -c "$(call docker_run_command,$(DOCKER_MAKE_COMMAND))" diff --git a/README.md b/README.md index 16da0e5..5e40afb 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,31 @@ This software is distributed in the binary form at [coral.ai/software](https://c ## Building -At present only Bazel build system is supported, but it can be invoked from the Makefile. +At present only [Bazel](https://bazel.build/) build system is supported. For +proper environment setup check `docker` directory. + +Build native binaries on Linux and macOS: +``` +$ make +``` + +Build native binaries on Windows: +``` +$ build.bat +``` + +Cross-compile for ARMv7-A (32 bit), and ARMv8-A (64 bit) on Linux: +``` +$ CPU=armv7a make +$ CPU=aarch64 make +``` + +Build Linux binaries inside Docker container (works on Linux and macOS): +``` +$ DOCKER_CPUS="k8 armv7a aarch64" DOCKER_TARGETS=libedgetpu make docker-build +``` + +All built binaries go to the `out` directory. ## Support diff --git a/WORKSPACE b/WORKSPACE deleted file mode 100644 index 7060b32..0000000 --- a/WORKSPACE +++ /dev/null @@ -1,75 +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 = "libedgetpu") - -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 = [ - "http://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 - ], -) - -# Be consistent with tensorflow/WORKSPACE. -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"], -) # https://github.com/bazelbuild/bazel-skylib/releases - -# The TF commit # here must be in sync with that specified under Gob edgetpu -# repo WORKSPACE file. -# TODO: figure out a way to keep single source of truth of the -# TF commit # used. -TENSORFLOW_COMMIT = "f394a768719a55b5c351ed1ecab2ec6f16f99dd4"; -# Command to calculate: curl -OL | sha256sum | awk '{print $1}' -TENSORFLOW_SHA256 = "cb286abee7ee9cf5c8701d85fcc88f0fd59e72492ec4f254156de486e3e905c1" -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 = "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") -new_local_repository( - name = "libusb", - path = "/usr/include/", - build_file_content = """ -cc_library( - name = "headers", - includes = ["."], - hdrs = ["libusb-1.0/libusb.h"], - visibility = ["//visibility:public"], -) -""" -) diff --git a/api/watchdog.cc b/api/watchdog.cc index 0f5bf5a..81508e3 100644 --- a/api/watchdog.cc +++ b/api/watchdog.cc @@ -86,8 +86,11 @@ util::StatusOr TimerFdWatchdog::Activate() { case WatchdogState::ACTIVE: break; // Already active: Return old activation_id. case WatchdogState::BARKING: - return util::FailedPreconditionError( - "Cannot activate a barking watchdog."); + VLOG(1) << "A barking watchdog was re-activated."; + RETURN_IF_ERROR(timer_->Set(timeout_ns_)); + state_ = WatchdogState::ACTIVE; + activation_id_ = GetNextActivationId(activation_id_); + break; case WatchdogState::INACTIVE: VLOG(5) << "Activating the watchdog."; RETURN_IF_ERROR(timer_->Set(timeout_ns_)); @@ -190,9 +193,11 @@ void TimerFdWatchdog::Watcher() { // Acquire lock again to update shared state after calling expire_. { StdMutexLock lock(&mutex_); - // Watchdog might be destroyed while callback was running. - // In that case, retain the 'DESTROYED' state. - if (state_ != WatchdogState::DESTROYED) { + // While the watchdog was executing the expire_ callback (ie BARKING): + // If ~Watchdog was called, retain DESTROYED state. + // If Activate was called (re-activated), retain ACTIVE state. + // If Deactivate was called, state will change to INACTIVE now. + if (state_ == WatchdogState::BARKING) { state_ = WatchdogState::INACTIVE; } } diff --git a/api/watchdog.h b/api/watchdog.h index 695f58f..4a5ea4a 100644 --- a/api/watchdog.h +++ b/api/watchdog.h @@ -162,7 +162,9 @@ class TimerFdWatchdog : public Watchdog { // The timer to be used for keeping track of expiration deadlines. std::unique_ptr timer_; - // A single mutex to protect mutable fields in this class. + // A single mutex to protect mutable fields in this class. This mutex is not + // held while the watchdog callback is being executed. It is safe to signal, + // activate, deactivate or destroy a barking watchdog. std::mutex mutex_; // Watchdog state machine: @@ -280,9 +282,11 @@ class CascadeWatchdog : public Watchdog { // A mutex to protect mutable class fields. std::mutex mutex_; - // Specifies which child watchdog is currently active. It can be used as an - // index to configs_ or watchdogs_. -1 means all watchdogs are inactive. + // Value that indicates all watchdogs are inactive. static constexpr int kNoneActive = -1; + + // Specifies which child watchdog is currently active. A value other than + // kNoneActive can be used as an index to configs_ or watchdogs_. int currently_active_ GUARDED_BY(mutex_){kNoneActive}; // The current/last generated activation ID for the caller of Activate on this diff --git a/build.bat b/build.bat index 12d0acc..af53532 100644 --- a/build.bat +++ b/build.bat @@ -15,6 +15,8 @@ echo off setlocal +type bazel\WORKSPACE bazel\WORKSPACE.windows > WORKSPACE 2>NUL + set THROTTLED=0 set COMPILATION_MODE=opt set OUT_DIR=%~dp0\out @@ -48,7 +50,8 @@ set BAZEL_BUILD_FLAGS= ^ --define darwinn_portable=1 ^ --copt=/DSTRIP_LOG=1 ^ --copt=/DABSL_FLAGS_STRIP_NAMES ^ ---copt=/DEDGETPU_EXTERNAL_RELEASE_RUNTIME ^ +--copt=/D_HAS_DEPRECATED_RESULT_OF ^ +--copt=/D_HAS_DEPRECATED_ADAPTOR_TYPEDEFS ^ --copt=/GR- ^ --copt=/DWIN32_LEAN_AND_MEAN ^ --copt=/D_WINSOCKAPI_ ^ diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..1c5fe0e --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,38 @@ +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 \ + python \ + python-future \ + python-numpy \ + 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 \ + zlib1g-dev \ + zlib1g-dev:armhf \ + zlib1g-dev:arm64 \ + sudo \ + pkg-config \ + zip \ + unzip \ + curl \ + wget \ + git \ + $(grep Ubuntu /etc/os-release > /dev/null && echo vim-common || echo xxd) + +RUN git clone https://github.com/raspberrypi/tools.git && \ + cd tools && \ + git reset --hard 4a335520900ce55e251ac4f420f52bf0b2ab6b1f + +ARG BAZEL_VERSION=3.2.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 diff --git a/docker/Dockerfile.windows b/docker/Dockerfile.windows new file mode 100644 index 0000000..7f417c6 --- /dev/null +++ b/docker/Dockerfile.windows @@ -0,0 +1,42 @@ +FROM mcr.microsoft.com/windows/servercore:1903 +SHELL ["powershell", "-command"] + +# Install chocolatey + Win10 SDK + VS build tools +RUN "Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" +RUN choco install -y windows-sdk-10 +RUN choco install -y visualstudio2019buildtools +RUN choco install -y visualstudio2019-workload-vctools +RUN choco install -y python3 +RUN python -m pip install six numpy wheel + +# Install 7-zip +RUN choco install -y 7zip +RUN setx /M PATH $($Env:PATH + ';C:\Program Files\7-Zip') + +# Install msys2 +ARG MSYS_VERSION=20200602 +ADD http://repo.msys2.org/distrib/x86_64/msys2-base-x86_64-${MSYS_VERSION}.tar.xz c:/windows/temp +RUN 7z.exe x c:\windows\temp\msys2-base-x86_64-$env:MSYS_VERSION.tar.xz +RUN 7z.exe x c:\msys2-base-x86_64-$env:MSYS_VERSION.tar -o"c:\\" +RUN setx /M PATH $($Env:PATH + ';C:\msys64\usr\bin') + +# Install patch +ARG PATCH_VERSION=2.7.6-1 +ADD http://repo.msys2.org/msys/x86_64/patch-${PATCH_VERSION}-x86_64.pkg.tar.xz c:/windows/temp +RUN 7z.exe x -y c:\windows\temp\patch-$env:PATCH_VERSION-x86_64.pkg.tar.xz +RUN 7z.exe x -y c:\patch-$env:PATCH_VERSION-x86_64.pkg.tar -o"c:\\msys64" + +# Install vim (for xxd) +ARG VIM_VERSION=8.2.0592-1 +ADD http://repo.msys2.org/msys/x86_64/vim-${VIM_VERSION}-x86_64.pkg.tar.xz c:/windows/temp +RUN 7z.exe x -y c:\windows\temp\vim-$env:VIM_VERSION-x86_64.pkg.tar.xz +RUN 7z.exe x -y c:\vim-$env:VIM_VERSION-x86_64.pkg.tar -o"c:\\msys64" + +# Install libusb release package +ARG LIBUSB_VERSION=1.0.22 +ADD https://github.com/libusb/libusb/releases/download/v${LIBUSB_VERSION}/libusb-${LIBUSB_VERSION}.7z c:/windows/temp +RUN 7z x -oc:\libusb-1.0.22 c:\windows\temp\libusb-1.0.22.7z + +# Install Bazel +ARG BAZEL_VERSION=3.2.0 +ADD https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel-${BAZEL_VERSION}-windows-x86_64.exe c:/windows/system32/bazel.exe \ No newline at end of file diff --git a/docker/update_sources.sh b/docker/update_sources.sh new file mode 100755 index 0000000..50fc6cb --- /dev/null +++ b/docker/update_sources.sh @@ -0,0 +1,12 @@ +#!/bin/bash +. /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/driver/config/beagle/beagle_csr_offsets.h b/driver/config/beagle/beagle_csr_offsets.h index 73a1bed..a482450 100644 --- a/driver/config/beagle/beagle_csr_offsets.h +++ b/driver/config/beagle/beagle_csr_offsets.h @@ -1496,6 +1496,22 @@ const ScalarCoreCsrOffsets kBeagleScalarCoreCsrOffsets = { kCsrRegisterSpaceInvalidOffset, // UNUSED, outfeed_3_0RunControl kCsrRegisterSpaceInvalidOffset, // UNUSED, infeed_3_1RunControl kCsrRegisterSpaceInvalidOffset, // UNUSED, outfeed_3_1RunControl + kCsrRegisterSpaceInvalidOffset, // UNUSED, + // ScalarCoreRingBusCreditSender_0Reset + kCsrRegisterSpaceInvalidOffset, // UNUSED, + // ScalarCoreRingBusCreditReceiver_0Reset + kCsrRegisterSpaceInvalidOffset, // UNUSED, + // ScalarCoreRingBusCreditSender_1Reset + kCsrRegisterSpaceInvalidOffset, // UNUSED, + // ScalarCoreRingBusCreditReceiver_1Reset + kCsrRegisterSpaceInvalidOffset, // UNUSED, + // ScalarCoreRingBusCreditSender_2Reset + kCsrRegisterSpaceInvalidOffset, // UNUSED, + // ScalarCoreRingBusCreditReceiver_2Reset + kCsrRegisterSpaceInvalidOffset, // UNUSED, + // ScalarCoreRingBusCreditSender_3Reset + kCsrRegisterSpaceInvalidOffset, // UNUSED, + // ScalarCoreRingBusCreditReceiver_3Reset kCsrRegisterSpaceInvalidOffset, // UNUSED, TilePowerInterval kCsrRegisterSpaceInvalidOffset, // UNUSED, peakPowerSampleInterval kCsrRegisterSpaceInvalidOffset, // UNUSED, tdpPowerSampleInterval @@ -1586,6 +1602,8 @@ const TileCsrOffsets kBeagleTileCsrOffsets = { kCsrRegisterSpaceInvalidOffset, // UNUSED, narrowMemoryContext_1 kCsrRegisterSpaceInvalidOffset, // UNUSED, narrowMemoryContext_2 kCsrRegisterSpaceInvalidOffset, // UNUSED, narrowMemoryContext_3 + kCsrRegisterSpaceInvalidOffset, // UNUSED, TileRingBusCreditSenderReset + kCsrRegisterSpaceInvalidOffset, // UNUSED, TileRingBusCreditReceiverReset }; const WireCsrOffsets kBeagleWireCsrOffsets = { diff --git a/driver/config/scalar_core_csr_offsets.h b/driver/config/scalar_core_csr_offsets.h index bf872d0..902a3da 100644 --- a/driver/config/scalar_core_csr_offsets.h +++ b/driver/config/scalar_core_csr_offsets.h @@ -84,6 +84,20 @@ struct ScalarCoreCsrOffsets { uint64 infeed_3_1RunControl; uint64 outfeed_3_1RunControl; + // Ring bus credit control + // Context 0 + uint64 ScalarCoreRingBusCreditSender_0Reset; + uint64 ScalarCoreRingBusCreditReceiver_0Reset; + // Context 1 + uint64 ScalarCoreRingBusCreditSender_1Reset; + uint64 ScalarCoreRingBusCreditReceiver_1Reset; + // Context 2 + uint64 ScalarCoreRingBusCreditSender_2Reset; + uint64 ScalarCoreRingBusCreditReceiver_2Reset; + // Context 3 + uint64 ScalarCoreRingBusCreditSender_3Reset; + uint64 ScalarCoreRingBusCreditReceiver_3Reset; + // Power related. uint64 TilePowerInterval; uint64 peakPowerSampleInterval; diff --git a/driver/config/tile_csr_offsets.h b/driver/config/tile_csr_offsets.h index d234ede..823c274 100644 --- a/driver/config/tile_csr_offsets.h +++ b/driver/config/tile_csr_offsets.h @@ -88,6 +88,10 @@ struct TileCsrOffsets { uint64 narrowMemoryContext_1; uint64 narrowMemoryContext_2; uint64 narrowMemoryContext_3; + + // Ring bus credit control + uint64 TileRingBusCreditSenderReset; + uint64 TileRingBusCreditReceiverReset; }; } // namespace config diff --git a/driver/driver.cc b/driver/driver.cc index a3b377f..8da3eb0 100644 --- a/driver/driver.cc +++ b/driver/driver.cc @@ -392,8 +392,11 @@ util::Status Driver::SubmitInferenceRequest(std::shared_ptr request) { DoCreateRequest(request, &request->MainExecutableReference(), TpuRequest::RequestType::INFERENCE)); RETURN_IF_ERROR(request->PrepareTpuRequest(tpu_request)); - RETURN_IF_ERROR(DoSubmit(std::move(tpu_request))); + + // Record the submission time before actually submitting the workload. This + // avoids race conditions where the completion is notified before submission. request->NotifySubmission(TpuRequest::RequestType::INFERENCE); + RETURN_IF_ERROR(DoSubmit(std::move(tpu_request))); return util::OkStatus(); } @@ -429,8 +432,11 @@ util::Status Driver::SubmitParameterCachingRequest( DoCreateRequest(request, parameter_caching_ref, TpuRequest::RequestType::PARAMETER_CACHING)); RETURN_IF_ERROR(tpu_request->SetDone([](int, const util::Status&) {})); - RETURN_IF_ERROR(DoSubmit(std::move(tpu_request))); + + // Record the submission time before actually submitting the workload. This + // avoids race conditions where the completion is notified before submission. request->NotifySubmission(TpuRequest::RequestType::PARAMETER_CACHING); + RETURN_IF_ERROR(DoSubmit(std::move(tpu_request))); return util::OkStatus(); } diff --git a/driver/interrupt/BUILD b/driver/interrupt/BUILD index 66e95d4..ff6a461 100644 --- a/driver/interrupt/BUILD +++ b/driver/interrupt/BUILD @@ -43,7 +43,10 @@ cc_library( cc_library( name = "interrupt_controller_interface", hdrs = ["interrupt_controller_interface.h"], - deps = ["//port"], + deps = [ + "//driver/config:register_constants", + "//port", + ], ) cc_library( @@ -62,6 +65,7 @@ cc_library( deps = [ ":interrupt_controller_interface", "//driver/config", + "//driver/config:register_constants", "//driver/registers", "//port", ], diff --git a/driver/interrupt/interrupt_controller.cc b/driver/interrupt/interrupt_controller.cc index bffbc05..d966211 100644 --- a/driver/interrupt/interrupt_controller.cc +++ b/driver/interrupt/interrupt_controller.cc @@ -14,6 +14,7 @@ #include "driver/interrupt/interrupt_controller.h" +#include "driver/config/register_constants.h" #include "driver/registers/registers.h" #include "port/logging.h" @@ -31,23 +32,35 @@ InterruptController::InterruptController( } util::Status InterruptController::EnableInterrupts() { - const uint64 enable_all = (1ULL << NumInterrupts()) - 1; - return registers_->Write(csr_offsets_.control, enable_all); + if (csr_offsets_.control != kCsrRegisterSpaceInvalidOffset) { + const uint64 enable_all = (1ULL << NumInterrupts()) - 1; + return registers_->Write(csr_offsets_.control, enable_all); + } else { + return util::OkStatus(); + } } util::Status InterruptController::DisableInterrupts() { - constexpr uint64 kDisableAll = 0; - return registers_->Write(csr_offsets_.control, kDisableAll); + if (csr_offsets_.control != kCsrRegisterSpaceInvalidOffset) { + constexpr uint64 kDisableAll = 0; + return registers_->Write(csr_offsets_.control, kDisableAll); + } else { + return util::OkStatus(); + } } util::Status InterruptController::ClearInterruptStatus(int id) { - // Interrupt status register has W0C policy meaning that writing 0 clears the - // bit, while writing 1 does not have any effect. - const uint64 clear_bit = ~(1ULL << id); + if (csr_offsets_.status != kCsrRegisterSpaceInvalidOffset) { + // Interrupt status register has W0C policy meaning that writing 0 + // clears the bit, while writing 1 does not have any effect. + const uint64 clear_bit = ~(1ULL << id); - uint64 value = (1ULL << NumInterrupts()) - 1; - value &= clear_bit; - return registers_->Write(csr_offsets_.status, value); + uint64 value = (1ULL << NumInterrupts()) - 1; + value &= clear_bit; + return registers_->Write(csr_offsets_.status, value); + } else { + return util::OkStatus(); + } } } // namespace driver diff --git a/port/default/builddata.h b/port/default/builddata.h index 335b17c..8a10819 100644 --- a/port/default/builddata.h +++ b/port/default/builddata.h @@ -27,7 +27,7 @@ struct BuildData { static const char* BuildLabel() { - return "COMPILER=" COMPILER_VERSION ",DATE=" __DATE__ ",TIME=" __TIME__ ",CL_NUMBER=315372822"; + return "COMPILER=" COMPILER_VERSION ",DATE=" __DATE__ ",TIME=" __TIME__ ",CL_NUMBER=317268237"; } }; diff --git a/rename_library.py b/rename_library.py index 776adb3..55b8c8d 100644 --- a/rename_library.py +++ b/rename_library.py @@ -65,7 +65,7 @@ def main(): cwd=os.path.dirname(args.output_dll)) # Move original DLL to new name. - os.rename(args.input_dll, args.output_dll) + os.replace(args.input_dll, args.output_dll) # Clean up intermediates. os.remove(def_file_name)