diff --git a/tensorflow/lite/kernels/internal/BUILD b/tensorflow/lite/kernels/internal/BUILD index 51b58f92de1..4bbf6704622 100644 --- a/tensorflow/lite/kernels/internal/BUILD +++ b/tensorflow/lite/kernels/internal/BUILD @@ -362,7 +362,11 @@ cc_test( cc_library( name = "cppmath", srcs = [], - hdrs = ["cppmath.h"], + hdrs = [ + "cppmath.h", + "max.h", + "min.h", + ], build_for_embedded = True, copts = tflite_copts(), ) diff --git a/tensorflow/lite/kernels/internal/cppmath.h b/tensorflow/lite/kernels/internal/cppmath.h index 611a8d2588a..24a3aec82e3 100644 --- a/tensorflow/lite/kernels/internal/cppmath.h +++ b/tensorflow/lite/kernels/internal/cppmath.h @@ -19,8 +19,9 @@ limitations under the License. namespace tflite { -#if defined(TF_LITE_USE_GLOBAL_CMATH_FUNCTIONS) || \ - (defined(__ANDROID__) && !defined(__NDK_MAJOR__)) || defined(ARDUINO) +#if defined(TF_LITE_USE_GLOBAL_CMATH_FUNCTIONS) || \ + (defined(__ANDROID__) && !defined(__NDK_MAJOR__)) || defined(ARDUINO) || \ + defined(__ZEPHYR__) #define TF_LITE_GLOBAL_STD_PREFIX #else #define TF_LITE_GLOBAL_STD_PREFIX std diff --git a/tensorflow/lite/kernels/internal/max.h b/tensorflow/lite/kernels/internal/max.h new file mode 100644 index 00000000000..c18100272db --- /dev/null +++ b/tensorflow/lite/kernels/internal/max.h @@ -0,0 +1,35 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +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. +==============================================================================*/ +#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_ +#define TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_ + +#include + +namespace tflite { + +#if defined(TF_LITE_USE_GLOBAL_MAX) || defined(__ZEPHYR__) +inline float TfLiteMax(const float& x, const float& y) { + return std::max(x, y); +} +#else +template +inline T TfLiteMax(const T& x, const T& y) { + return std::fmax(x, y); +} +#endif + +} // namespace tflite + +#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_MAX_H_ diff --git a/tensorflow/lite/kernels/internal/min.h b/tensorflow/lite/kernels/internal/min.h new file mode 100644 index 00000000000..62035dccd89 --- /dev/null +++ b/tensorflow/lite/kernels/internal/min.h @@ -0,0 +1,35 @@ +/* Copyright 2020 The TensorFlow Authors. All Rights Reserved. + +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. +==============================================================================*/ +#ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_ +#define TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_ + +#include + +namespace tflite { + +#if defined(TF_LITE_USE_GLOBAL_MIN) || defined(__ZEPHYR__) +inline float TfLiteMin(const float& x, const float& y) { + return std::min(x, y); +} +#else +template +inline T TfLiteMin(const T& x, const T& y) { + return std::fmin(x, y); +} +#endif + +} // namespace tflite + +#endif // TENSORFLOW_LITE_KERNELS_INTERNAL_MIN_H_ diff --git a/tensorflow/lite/kernels/internal/reference/reduce.h b/tensorflow/lite/kernels/internal/reference/reduce.h index 17dfd8557ae..fbad266e843 100644 --- a/tensorflow/lite/kernels/internal/reference/reduce.h +++ b/tensorflow/lite/kernels/internal/reference/reduce.h @@ -18,6 +18,8 @@ limitations under the License. #include "ruy/profiler/instrumentation.h" // from @ruy #include "tensorflow/lite/kernels/internal/common.h" #include "tensorflow/lite/kernels/internal/cppmath.h" +#include "tensorflow/lite/kernels/internal/max.h" +#include "tensorflow/lite/kernels/internal/min.h" #include "tensorflow/lite/kernels/internal/quantization_util.h" #include "tensorflow/lite/kernels/internal/types.h" @@ -381,11 +383,11 @@ inline bool QuantizedMeanOrSum(const T* input_data, int32 input_zero_point, for (size_t idx = 0; idx < num_outputs; ++idx) { float float_mean = static_cast(temp_sum[idx]) / static_cast(num_elements_in_axis); - float result = - std::min(TfLiteRound(float_mean * scale + bias) + output_zero_point, - static_cast(std::numeric_limits::max())); - result = - std::max(result, static_cast(std::numeric_limits::min())); + float result = TfLiteMin( + TfLiteRound(float_mean * scale + bias) + output_zero_point, + static_cast(std::numeric_limits::max())); + result = TfLiteMax(result, + static_cast(std::numeric_limits::min())); output_data[idx] = static_cast(result); } } diff --git a/tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h b/tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h index ed87863a7e5..e76fc8b6931 100644 --- a/tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h +++ b/tensorflow/lite/kernels/internal/reference/resize_nearest_neighbor.h @@ -17,6 +17,7 @@ limitations under the License. #include +#include "tensorflow/lite/kernels/internal/cppmath.h" #include "tensorflow/lite/kernels/internal/types.h" namespace tflite { @@ -34,7 +35,7 @@ inline int32 GetNearestNeighbor(const int input_value, const int32 input_size, const float offset = half_pixel_centers ? 0.5f : 0.0f; int32 output_value = std::min( align_corners - ? static_cast(std::round((input_value + offset) * scale)) + ? static_cast(TfLiteRound((input_value + offset) * scale)) : static_cast(std::floor((input_value + offset) * scale)), input_size - 1); if (half_pixel_centers) { diff --git a/tensorflow/lite/micro/examples/hello_world/zephyr_riscv/Makefile.inc b/tensorflow/lite/micro/examples/hello_world/zephyr_riscv/Makefile.inc new file mode 100644 index 00000000000..292adbba732 --- /dev/null +++ b/tensorflow/lite/micro/examples/hello_world/zephyr_riscv/Makefile.inc @@ -0,0 +1,28 @@ +ifeq ($(TARGET), zephyr_vexriscv) + export ZEPHYR_TOOLCHAIN_VARIANT?=zephyr + export TOOLCHAIN_BASE=${ZEPHYR_SDK_INSTALL_DIR}/riscv64-zephyr-elf/riscv64-zephyr-elf + export TOOLCHAIN_VERSION=9.2.0 + PROJECT_INCLUDES += ${CURDIR} ${TOOLCHAIN_BASE}/include/c++/${TOOLCHAIN_VERSION} ${TOOLCHAIN_BASE}/include ${TOOLCHAIN_BASE}/include/c++/${TOOLCHAIN_VERSION}/riscv64-zephyr-elf/rv32i/ilp32 + ZEPHYR_HELLO_WORLD_SRCS = \ +tensorflow/lite/micro/examples/hello_world/zephyr_riscv/src/assert.cc \ +tensorflow/lite/micro/examples/hello_world/main.cc \ +tensorflow/lite/micro/examples/hello_world/main_functions.cc \ +tensorflow/lite/micro/examples/hello_world/constants.cc \ +tensorflow/lite/micro/examples/hello_world/output_handler.cc \ +tensorflow/lite/micro/examples/hello_world/model.cc \ +prj.conf + +$(eval $(call generate_project,cmake,zephyr_cmake_project.cmake,hello_world,$(MICROLITE_CC_SRCS) $(THIRD_PARTY_CC_SRCS) $(ZEPHYR_HELLO_WORLD_SRCS) $(MICROLITE_CC_HDRS) $(THIRD_PARTY_CC_HDRS) $(HELLO_WORLD_HDRS),,$(LDFLAGS) $(MICROLITE_LIBS),$(CXXFLAGS),$(CCFLAGS),)) + +$(PRJDIR)hello_world/cmake/CMakeLists.txt: $(PRJDIR)hello_world/cmake/zephyr_cmake_project.cmake + @sed -E 's#\%\{INCLUDE_DIRS\}\%#$(PROJECT_INCLUDES)#g' $< > $@ + +#We are skipping here copy of `zephyr` third_party repository +#To compile standalone project ZEPHYR_BASE enviroment variable should be set +hello_world_bin: generate_hello_world_cmake_project $(PRJDIR)hello_world/cmake/CMakeLists.txt + ( \ + . ${ZEPHYR_BASE}/venv-zephyr/bin/activate; \ + cmake -B${GENDIR}hello_world/build -DBOARD="litex_vexriscv" -H${PRJDIR}hello_world/cmake/ -DPython_ROOT_DIR=${ZEPHYR_BASE}/venv-zephyr/bin/; \ + make -C ${GENDIR}hello_world/build; \ + ) +endif diff --git a/tensorflow/lite/micro/examples/hello_world/zephyr_riscv/prj.conf b/tensorflow/lite/micro/examples/hello_world/zephyr_riscv/prj.conf new file mode 100644 index 00000000000..e36145c332a --- /dev/null +++ b/tensorflow/lite/micro/examples/hello_world/zephyr_riscv/prj.conf @@ -0,0 +1,16 @@ +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. +# +# 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. +# ============================================================================== +CONFIG_CPLUSPLUS=y +CONFIG_NEWLIB_LIBC=y diff --git a/tensorflow/lite/micro/examples/hello_world/zephyr_riscv/src/assert.cc b/tensorflow/lite/micro/examples/hello_world/zephyr_riscv/src/assert.cc new file mode 100644 index 00000000000..2141c091149 --- /dev/null +++ b/tensorflow/lite/micro/examples/hello_world/zephyr_riscv/src/assert.cc @@ -0,0 +1,19 @@ +/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. + +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. +==============================================================================*/ + +extern "C" { + +void __assert_func(const char *, int, const char *, const char *) {} +} diff --git a/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/Makefile.inc b/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/Makefile.inc new file mode 100644 index 00000000000..e257e6620db --- /dev/null +++ b/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/Makefile.inc @@ -0,0 +1,31 @@ +ifeq ($(TARGET), zephyr_vexriscv) + export ZEPHYR_TOOLCHAIN_VARIANT?=zephyr + export TOOLCHAIN_BASE=${ZEPHYR_SDK_INSTALL_DIR}/riscv64-zephyr-elf/riscv64-zephyr-elf + export TOOLCHAIN_VERSION=9.2.0 + PROJECT_INCLUDES += ${CURDIR} ${TOOLCHAIN_BASE}/include/c++/${TOOLCHAIN_VERSION} ${TOOLCHAIN_BASE}/include ${TOOLCHAIN_BASE}/include/c++/${TOOLCHAIN_VERSION}/riscv64-zephyr-elf/rv32i/ilp32 + ZEPHYR_MAGIC_WAND_SRCS = \ +tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/src/assert.cc \ +tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/src/accelerometer_handler.cc \ +tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/src/accelerometer_handler.h \ +tensorflow/lite/micro/examples/magic_wand/main.cc \ +tensorflow/lite/micro/examples/magic_wand/main_functions.cc \ +tensorflow/lite/micro/examples/magic_wand/magic_wand_model_data.cc \ +tensorflow/lite/micro/examples/magic_wand/gesture_predictor.cc \ +tensorflow/lite/micro/examples/magic_wand/output_handler.cc \ +boards/litex_vexriscv.overlay \ +prj.conf + +$(eval $(call generate_project,cmake,zephyr_cmake_project.cmake,magic_wand,$(MICROLITE_CC_SRCS) $(THIRD_PARTY_CC_SRCS) $(ZEPHYR_MAGIC_WAND_SRCS) $(MICROLITE_CC_HDRS) $(THIRD_PARTY_CC_HDRS) $(magic_wand_HDRS),,$(LDFLAGS) $(MICROLITE_LIBS),$(CXXFLAGS),$(CCFLAGS),)) + +$(PRJDIR)magic_wand/cmake/CMakeLists.txt: $(PRJDIR)magic_wand/cmake/zephyr_cmake_project.cmake + @sed -E 's#\%\{INCLUDE_DIRS\}\%#$(PROJECT_INCLUDES)#g' $< > $@ + +#We are skipping here copy of `zephyr` third_party repository +#To compile standalone project ZEPHYR_BASE enviroment variable should be set +magic_wand_bin: generate_magic_wand_cmake_project $(PRJDIR)magic_wand/cmake/CMakeLists.txt + ( \ + . ${ZEPHYR_BASE}/venv-zephyr/bin/activate; \ + cmake -B${GENDIR}magic_wand/build -DBOARD="litex_vexriscv" -H${PRJDIR}magic_wand/cmake/ -DPython_ROOT_DIR=${ZEPHYR_BASE}/venv-zephyr/bin/; \ + make -C ${GENDIR}magic_wand/build; \ + ) +endif diff --git a/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/boards/litex_vexriscv.overlay b/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/boards/litex_vexriscv.overlay new file mode 100644 index 00000000000..a75435b3ea6 --- /dev/null +++ b/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/boards/litex_vexriscv.overlay @@ -0,0 +1,38 @@ +/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. + +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. +==============================================================================*/ + +&i2c0 { + label = "I2C0"; + reg = <0xe0003000 0x4 0xe0003004 0x4>; + + adxl@1d { + compatible = "adi,adxl345"; + label = "accel-0"; + reg = <0x1d>; + }; + +}; + +&pwm0 { + status = "disabled"; +}; + +ð0 { + status = "disabled"; +}; + +&prbs0 { + status = "disabled"; +}; diff --git a/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/prj.conf b/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/prj.conf new file mode 100644 index 00000000000..e4152086d5f --- /dev/null +++ b/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/prj.conf @@ -0,0 +1,22 @@ +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. +# +# 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. +# ============================================================================== +CONFIG_CPLUSPLUS=y +CONFIG_NEWLIB_LIBC=y +CONFIG_SENSOR=y +CONFIG_ADXL345=y +CONFIG_PWM=n +CONFIG_PWM_LITEX=n +CONFIG_NETWORKING=n +CONFIG_MAIN_STACK_SIZE=4096 diff --git a/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/src/accelerometer_handler.cc b/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/src/accelerometer_handler.cc new file mode 100644 index 00000000000..f7f019d4d18 --- /dev/null +++ b/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/src/accelerometer_handler.cc @@ -0,0 +1,98 @@ +/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. + +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. +==============================================================================*/ + +#include "tensorflow/lite/micro/examples/magic_wand/accelerometer_handler.h" + +#include +#include +#include +#include +#include + +#define BUFLEN 300 +int begin_index = 0; +struct device* sensor = NULL; +int current_index = 0; + +float bufx[BUFLEN] = {0.0f}; +float bufy[BUFLEN] = {0.0f}; +float bufz[BUFLEN] = {0.0f}; + +bool initial = true; + +TfLiteStatus SetupAccelerometer(tflite::ErrorReporter* error_reporter) { + sensor = device_get_binding(DT_INST_0_ADI_ADXL345_LABEL); + if (sensor == NULL) { + TF_LITE_REPORT_ERROR(error_reporter, + "Failed to get accelerometer, label: %s\n", + DT_INST_0_ADI_ADXL345_LABEL); + } else { + TF_LITE_REPORT_ERROR(error_reporter, "Got accelerometer, label: %s\n", + DT_INST_0_ADI_ADXL345_LABEL); + } + return kTfLiteOk; +} + +bool ReadAccelerometer(tflite::ErrorReporter* error_reporter, float* input, + int length) { + int rc; + struct sensor_value accel[3]; + int samples_count; + + rc = sensor_sample_fetch(sensor); + if (rc < 0) { + TF_LITE_REPORT_ERROR(error_reporter, "Fetch failed\n"); + return false; + } + // skip if there is no data + if (!rc) { + return false; + } + + samples_count = rc; + for (int i = 0; i < samples_count; i++) { + rc = sensor_channel_get(sensor, SENSOR_CHAN_ACCEL_XYZ, accel); + if (rc < 0) { + TF_LITE_REPORT_ERROR(error_reporter, "ERROR: Update failed: %d\n", rc); + return false; + } + bufx[begin_index] = (float)sensor_value_to_double(&accel[0]); + bufy[begin_index] = (float)sensor_value_to_double(&accel[1]); + bufz[begin_index] = (float)sensor_value_to_double(&accel[2]); + begin_index++; + if (begin_index >= BUFLEN) begin_index = 0; + } + + if (initial && begin_index >= 100) { + initial = false; + } + + if (initial) { + return false; + } + + int sample = 0; + for (int i = 0; i < (length - 3); i += 3) { + int ring_index = begin_index + sample - length / 3; + if (ring_index < 0) { + ring_index += BUFLEN; + } + input[i] = bufx[ring_index]; + input[i + 1] = bufy[ring_index]; + input[i + 2] = bufz[ring_index]; + sample++; + } + return true; +} diff --git a/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/src/accelerometer_handler.h b/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/src/accelerometer_handler.h new file mode 100644 index 00000000000..5b3fb54a4e4 --- /dev/null +++ b/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/src/accelerometer_handler.h @@ -0,0 +1,29 @@ +/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. + +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. +==============================================================================*/ + +#ifndef TENSORFLOW_LITE_MICRO_EXAMPLES_MAGIC_WAND_ACCELEROMETER_HANDLER_H_ +#define TENSORFLOW_LITE_MICRO_EXAMPLES_MAGIC_WAND_ACCELEROMETER_HANDLER_H_ + +#define kChannelNumber 3 + +#include "tensorflow/lite/c/c_api_internal.h" +#include "tensorflow/lite/micro/micro_error_reporter.h" + +extern int begin_index; +extern TfLiteStatus SetupAccelerometer(tflite::ErrorReporter* error_reporter); +extern bool ReadAccelerometer(tflite::ErrorReporter* error_reporter, + float* input, int length, bool reset_buffer); + +#endif // TENSORFLOW_LITE_MICRO_EXAMPLES_MAGIC_WAND_ACCELEROMETER_HANDLER_H_ diff --git a/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/src/assert.cc b/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/src/assert.cc new file mode 100644 index 00000000000..2141c091149 --- /dev/null +++ b/tensorflow/lite/micro/examples/magic_wand/zephyr_riscv/src/assert.cc @@ -0,0 +1,19 @@ +/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. + +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. +==============================================================================*/ + +extern "C" { + +void __assert_func(const char *, int, const char *, const char *) {} +} diff --git a/tensorflow/lite/micro/kernels/activation_utils.h b/tensorflow/lite/micro/kernels/activation_utils.h index 7525bc93b0a..a71826211c0 100644 --- a/tensorflow/lite/micro/kernels/activation_utils.h +++ b/tensorflow/lite/micro/kernels/activation_utils.h @@ -21,6 +21,8 @@ limitations under the License. #include "tensorflow/lite/c/builtin_op_data.h" #include "tensorflow/lite/kernels/internal/cppmath.h" +#include "tensorflow/lite/kernels/internal/max.h" +#include "tensorflow/lite/kernels/internal/min.h" namespace tflite { namespace ops { @@ -32,11 +34,11 @@ inline float ActivationValFloat(TfLiteFusedActivation act, float a) { case kTfLiteActNone: return a; case kTfLiteActRelu: - return std::max(0.0f, a); + return TfLiteMax(0.0f, a); case kTfLiteActRelu1: - return std::max(-1.0f, std::min(a, 1.0f)); + return TfLiteMax(-1.0f, TfLiteMin(a, 1.0f)); case kTfLiteActRelu6: - return std::max(0.0f, std::min(a, 6.0f)); + return TfLiteMax(0.0f, TfLiteMin(a, 6.0f)); case kTfLiteActTanh: return std::tanh(a); case kTfLiteActSignBit: diff --git a/tensorflow/lite/micro/tools/make/Makefile b/tensorflow/lite/micro/tools/make/Makefile index a94d643a3d0..c00a8a8ae89 100644 --- a/tensorflow/lite/micro/tools/make/Makefile +++ b/tensorflow/lite/micro/tools/make/Makefile @@ -185,6 +185,8 @@ tensorflow/lite/kernels/internal/reference/logistic.h \ tensorflow/lite/kernels/internal/reference/strided_slice.h \ tensorflow/lite/kernels/internal/reference/tanh.h \ tensorflow/lite/kernels/internal/cppmath.h \ +tensorflow/lite/kernels/internal/max.h \ +tensorflow/lite/kernels/internal/min.h \ tensorflow/lite/kernels/internal/strided_slice_logic.h \ tensorflow/lite/kernels/internal/tensor.h \ tensorflow/lite/kernels/internal/tensor_ctypes.h \ diff --git a/tensorflow/lite/micro/tools/make/download_and_extract.sh b/tensorflow/lite/micro/tools/make/download_and_extract.sh index 2f602ce9d4c..c82b247ef8c 100755 --- a/tensorflow/lite/micro/tools/make/download_and_extract.sh +++ b/tensorflow/lite/micro/tools/make/download_and_extract.sh @@ -93,6 +93,17 @@ build_embarc_mli() { make -j 4 -C ${1}/lib/make TCF_FILE=${2} } +setup_zephyr() { + command -v virtualenv >/dev/null 2>&1 || { + echo >&2 "The required 'virtualenv' tool isn't installed. Try 'pip install virtualenv'."; exit 1; + } + virtualenv -p python3 ${1}/venv-zephyr + . ${1}/venv-zephyr/bin/activate + python ${1}/venv-zephyr/bin/pip install -r ${1}/scripts/requirements.txt + west init -m https://github.com/zephyrproject-rtos/zephyr.git + deactivate +} + # Main function handling the download, verify, extract, and patch process. download_and_extract() { local usage="Usage: download_and_extract URL MD5 DIR [ACTION] [ACTION_PARAM]" @@ -179,6 +190,8 @@ download_and_extract() { else build_embarc_mli ${dir} ${action_param1} fi + elif [[ ${action} == "setup_zephyr" ]]; then + setup_zephyr ${dir} elif [[ ${action} ]]; then echo "Unknown action '${action}'" exit 1 diff --git a/tensorflow/lite/micro/tools/make/helper_functions.inc b/tensorflow/lite/micro/tools/make/helper_functions.inc index 1cf9afa8794..d3403a81ef0 100644 --- a/tensorflow/lite/micro/tools/make/helper_functions.inc +++ b/tensorflow/lite/micro/tools/make/helper_functions.inc @@ -74,6 +74,14 @@ $(PRJDIR)$(3)/$(1)/%: % third_party_downloads @mkdir -p $$(dir $$@) @cp $$< $$@ +$(PRJDIR)$(3)/cmake/boards/%: tensorflow/lite/micro/examples/$(3)/zephyr_riscv/boards/% + @mkdir -p $$(dir $$@) + @cp $$< $$@ + +$(PRJDIR)$(3)/cmake/%: tensorflow/lite/micro/examples/$(3)/zephyr_riscv/% + @mkdir -p $$(dir $$@) + @cp $$< $$@ + $(PRJDIR)$(3)/$(1)/third_party/%: tensorflow/lite/micro/tools/make/downloads/% third_party_downloads @mkdir -p $$(dir $$@) @cp $$< $$@ diff --git a/tensorflow/lite/micro/tools/make/targets/litex_vexriscv_makefile.inc b/tensorflow/lite/micro/tools/make/targets/litex_vexriscv_makefile.inc new file mode 100644 index 00000000000..c9764ed2f26 --- /dev/null +++ b/tensorflow/lite/micro/tools/make/targets/litex_vexriscv_makefile.inc @@ -0,0 +1,6 @@ +ifeq ($(TARGET), zephyr_vexriscv) + $(eval $(call add_third_party_download,$(ZEPHYR_URL),$(ZEPHYR_MD5),zephyr,setup_zephyr)) + export ZEPHYR_SDK_INSTALL_DIR?=/opt/zephyr-sdk + export ZEPHYR_BASE?=$(realpath $(MAKEFILE_DIR)/downloads/zephyr) +endif + diff --git a/tensorflow/lite/micro/tools/make/templates/zephyr_cmake_project.cmake.tpl b/tensorflow/lite/micro/tools/make/templates/zephyr_cmake_project.cmake.tpl new file mode 100644 index 00000000000..d7bb4511f32 --- /dev/null +++ b/tensorflow/lite/micro/tools/make/templates/zephyr_cmake_project.cmake.tpl @@ -0,0 +1,18 @@ +cmake_minimum_required(VERSION 3.13.1) +include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) +project(tf_lite_magic_wand) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} %{CXX_FLAGS}%") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} %{CC_FLAGS}%") +set(CMAKE_EXE_LINKER_FLAGS "%{LINKER_FLAGS}%") + +# -fno-threadsafe-statics -- disables the mutex around initialization of local static variables +target_compile_options(app PRIVATE "-fno-threadsafe-statics") + +target_sources(app PRIVATE + %{SRCS}% + ) + +target_include_directories(app PRIVATE + %{INCLUDE_DIRS}% + ) diff --git a/tensorflow/lite/micro/tools/make/third_party_downloads.inc b/tensorflow/lite/micro/tools/make/third_party_downloads.inc index e4bb4f424ac..ef3176f2617 100644 --- a/tensorflow/lite/micro/tools/make/third_party_downloads.inc +++ b/tensorflow/lite/micro/tools/make/third_party_downloads.inc @@ -77,6 +77,9 @@ EMBARC_MLI_MD5 := "2bf4982a327fdaa9d475803ce014d1ef" EMBARC_MLI_PRE_COMPILED_URL := "https://github.com/foss-for-synopsys-dwc-arc-processors/embarc_mli/releases/download/Release_1.1_RC2/embARC_MLI_package.zip" EMBARC_MLI_PRE_COMPILED_MD5 := "a95ff9e0370434484f14e7e4114327f6" +ZEPHYR_URL := "https://github.com/antmicro/zephyr/archive/55e36b9.zip" +ZEPHYR_MD5 := "755622eb4812fde918a6382b65d50c3b" + XTENSA_HIFI4_URL :="https://github.com/foss-xtensa/nnlib-hifi4/raw/master/archive/xa_nnlib_04_07.zip" XTENSA_HIFI4_MD5 :="f234764928f9a42901df33a27e118c8b"