Enable benchmarking in ml-models-public
Supports running benchmarks (defined in sw/kelvin/benchmarks)
on simulator (ISS or HW) and FPGA.
Change-Id: I23a9ffe98a45d55e558b1399689c2cfa58ead68e
diff --git a/.bazelrc b/.bazelrc
new file mode 100644
index 0000000..842e3bb
--- /dev/null
+++ b/.bazelrc
@@ -0,0 +1,21 @@
+build --action_env=BAZEL_CXXOPTS="-std=c++17"
+build --cxxopt='-std=c++17'
+build --conlyopt='-std=c11'
+
+# Bazel embedded enables the following feature which along with -std=c11 and
+# our codebase generates a lot of warnings by setting the -Wpedantic flag
+build --copt='-Wno-pedantic'
+
+# Enable toolchain resolution with cc
+build --incompatible_enable_cc_toolchain_resolution
+
+build:kelvin --platforms=//platforms/riscv32:kelvin
+
+# Set preprocessor defines for tflite-micro.
+build --copt=-DTF_LITE_USE_GLOBAL_CMATH_FUNCTIONS
+build --copt=-DTF_LITE_USE_GLOBAL_MIN
+build --copt=-DTF_LITE_USE_GLOBAL_MAX
+build --copt=-DTF_LITE_MCU_DEBUG_LOG
+build:opt --copt=-DTF_LITE_STRIP_ERROR_STRINGS
+
+build --use_top_level_targets_for_symlinks=false
diff --git a/.bazelversion b/.bazelversion
new file mode 100644
index 0000000..024b066
--- /dev/null
+++ b/.bazelversion
@@ -0,0 +1 @@
+6.2.1
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..ac51a05
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+bazel-*
diff --git a/BUILD b/BUILD
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/BUILD
diff --git a/PREUPLOAD.cfg b/PREUPLOAD.cfg
new file mode 100644
index 0000000..f88c10e
--- /dev/null
+++ b/PREUPLOAD.cfg
@@ -0,0 +1,17 @@
+# Per-project `repo upload` hook settings.
+# https://android.googlesource.com/platform/tools/repohooks
+
+[Options]
+ignore_merged_commits = true
+
+[Builtin Hooks]
+pylint3 = true
+cpplint = true
+clang_format = true
+
+[Builtin Hooks Options]
+clang_format = --commit ${PREUPLOAD_COMMIT} --style file --extensions c,h,cc,cpp
+
+[Hook Scripts]
+buildifier = ${REPO_ROOT}/scripts/preupload-hooks/run_buildifier.sh ${PREUPLOAD_COMMIT}
+yapf-diff = ${REPO_ROOT}/scripts/preupload-hooks/yapf-diff.sh ${PREUPLOAD_COMMIT}
diff --git a/WORKSPACE b/WORKSPACE
new file mode 100644
index 0000000..3d18b46
--- /dev/null
+++ b/WORKSPACE
@@ -0,0 +1,98 @@
+workspace(name = "ml_models_public")
+
+local_repository(
+ name = "kelvin_sw",
+ path = "../../sw/kelvin",
+)
+
+local_repository(
+ name = "matcha",
+ path = "../../hw/matcha",
+)
+
+local_repository(
+ name = "lowrisc_opentitan",
+ path = "../../hw/opentitan-upstream",
+)
+
+load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
+http_archive(
+ name = "rules_python",
+ sha256 = "9d04041ac92a0985e344235f5d946f71ac543f1b1565f2cdbc9a2aaee8adf55b",
+ strip_prefix = "rules_python-0.26.0",
+ url = "https://github.com/bazelbuild/rules_python/releases/download/0.26.0/rules_python-0.26.0.tar.gz",
+)
+
+load("@rules_python//python:repositories.bzl", "py_repositories")
+
+py_repositories()
+
+load("@rules_python//python:repositories.bzl", "python_register_toolchains")
+
+python_register_toolchains(
+ name = "python3",
+ python_version = "3.9",
+)
+
+load("@kelvin_sw//build_tools/bazel:repos.bzl", "kelvin_sw_repos")
+
+kelvin_sw_repos()
+
+# Register Kelvin toolchain
+load("@kelvin_sw//platforms:registration.bzl", "kelvin_register_toolchain")
+
+kelvin_register_toolchain()
+
+load("@lowrisc_opentitan//third_party/crt:repos.bzl", lowrisc_crt_repos = "crt_repos")
+
+lowrisc_crt_repos()
+
+load("@crt//:repos.bzl", "crt_repos")
+
+crt_repos()
+
+load("@crt//config:registration.bzl", "crt_register_toolchains")
+
+crt_register_toolchains(riscv32 = True)
+
+load("@kelvin_sw//build_tools/bazel:repos.bzl", "tflm_repos")
+
+tflm_repos()
+
+load("@tflite-micro//tensorflow:workspace.bzl", "tf_repositories")
+
+tf_repositories()
+
+load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
+
+rules_pkg_dependencies()
+
+load("@rules_python//python:pip.bzl", "pip_parse")
+
+pip_parse(
+ name = "ot_python_deps",
+ requirements_lock = "//:opentitan_sanitized_requirements.txt",
+ python_interpreter_target = "@python3_x86_64-unknown-linux-gnu//:python"
+)
+
+load("@ot_python_deps//:requirements.bzl", install_ot_python_deps = "install_deps")
+
+install_ot_python_deps()
+
+load("@lowrisc_opentitan//third_party/google:repos.bzl", "google_repos")
+
+google_repos()
+
+load("@lowrisc_opentitan//third_party/google:deps.bzl", "google_deps")
+
+google_deps()
+
+pip_parse(
+ name = "tflm_pip_deps",
+ requirements_lock = "@tflite-micro//third_party:python_requirements.txt",
+ python_interpreter_target = "@python3_x86_64-unknown-linux-gnu//:python"
+)
+
+load("@tflm_pip_deps//:requirements.bzl", install_tflm_pip_deps = "install_deps")
+
+install_tflm_pip_deps()
diff --git a/benchmarks/BUILD b/benchmarks/BUILD
new file mode 100644
index 0000000..4b5f51c
--- /dev/null
+++ b/benchmarks/BUILD
@@ -0,0 +1,29 @@
+# Copyright 2024 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.
+
+load("@kelvin_sw//benchmarks:benchmarks.bzl", "kelvin_benchmark_simulator", "kelvin_benchmark_fpga")
+
+kelvin_benchmark_simulator(
+ name = "person_detection_benchmark_simulator",
+ model = "//quant_models:person_detection.tflite",
+ iterations = 2,
+ hw_test_size = "medium",
+ iss_test_size = "medium",
+)
+
+kelvin_benchmark_fpga(
+ name = "person_detection_benchmark_fpga",
+ model = "//quant_models:person_detection.tflite",
+ iterations = 2,
+)
diff --git a/opentitan_sanitized_requirements.txt b/opentitan_sanitized_requirements.txt
new file mode 100644
index 0000000..9cd67e7
--- /dev/null
+++ b/opentitan_sanitized_requirements.txt
@@ -0,0 +1,78 @@
+anytree==2.8.0
+attrs==23.1.0
+blessed==1.20.0
+cachetools==5.3.2
+certifi==2023.11.17
+charset_normalizer==3.3.2
+chipwhisperer==5.6.1
+click==8.1.7
+commonmark==0.9.1
+cssselect==1.2.0
+cssutils==2.9.0
+Cython==3.0.6
+distro==1.8.0
+edalize==0.2.4
+enlighten==1.10.2
+flake8==5.0.4
+fusesoc==0.1
+gitdb==4.0.11
+GitPython==3.1.40
+hjson==3.1.0
+idna==3.6
+iniconfig==2.0.0
+isort==5.10.1
+Jinja2==3.1.2
+jsonschema==4.17.3
+libcst==0.4.9
+libusb1==3.1.0
+lizard==1.17.10
+lxml==4.9.3
+Mako==1.1.6
+MarkupSafe==2.1.3
+mccabe==0.7.0
+mistletoe==0.9.0
+msgpack_python==0.5.6
+mypy==0.971
+mypy_extensions==1.0.0
+okonomiyaki==1.4.0
+packaging==23.2
+Pillow==10.0.1
+pluggy==1.3.0
+pluralizer==1.2.0
+prefixed==0.7.0
+premailer==3.8.0
+py==1.11.0
+pycodestyle==2.9.1
+pycryptodome==3.15.0
+PyDriller==2.1
+pyelftools==0.29
+pyfinite==1.9.1
+pyflakes==2.5.0
+pygments==2.17.2
+pyparsing==3.1.1
+pyrsistent==0.20.0
+pyserial==3.5
+pytest==7.0.1
+pytest_timeout==2.1.0
+pytz==2023.3.post1
+PyYAML==6.0
+requests==2.31.0
+rich==12.5.1
+simplesat==0.8.2
+six==1.16.0
+smmap==5.0.1
+tabulate==0.8.10
+termcolor==1.1.0
+tomli==2.0.1
+typer==0.6.1
+types_dataclasses==0.6.6
+types_pkg_resources==0.1.3
+types_pytz==2023.3.1.1
+types_PyYAML==6.0.11
+types_tabulate==0.8.11
+typing_extensions==4.8.0
+typing_inspect==0.9.0
+urllib3==1.26.15
+wcwidth==0.2.12
+yapf==0.32.0
+zipfile2==0.0.12
diff --git a/quant_models/BUILD b/quant_models/BUILD
new file mode 100644
index 0000000..1190650
--- /dev/null
+++ b/quant_models/BUILD
@@ -0,0 +1,19 @@
+# Copyright 2024 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.
+
+package(default_visibility = ["//visibility:public"])
+
+exports_files(
+ srcs = glob(["*.tflite"]),
+)