Skip to content

Commit

Permalink
Update script to handle previous manual changes. Allow for user contr…
Browse files Browse the repository at this point in the history
…ol of ESP_NN document some of the flags that will be required for S3 support
  • Loading branch information
Nick Jugganaikloo committed Apr 25, 2024
1 parent 0f49b0d commit 22be532
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 36 deletions.
16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
# ESP_TF
1. run create.sf to update source this library just restructures the espressif tensorflow repo (https://github.com/espressif/tflite-micro-esp-examples) to be arduino compatible
2. Move private destructors as needed see commit 47ea0fec385a7701ede45d45b3c4a7ccfcf7a120

## examples
https://github.com/Nickjgniklu/esp_mnist

## notes
ESP_NN support
Building with #define ESP_NN will enable espressif ansci layer implementations
### example for PIO
-DCONFIG_IDF_TARGET_ESP32S3 -DCONFIG_NN_OPTIMIZED -DCONFIG_IDF_TARGET_ARCH_XTENSA are currently not working but will be required for esp32s3 optimizations
```
build_flags =
-std=gnu++17
-DCORE_DEBUG_LEVEL=5
-DESP_NN
-DCONFIG_IDF_TARGET_ESP32S3
-DCONFIG_NN_OPTIMIZED
-DCONFIG_IDF_TARGET_ARCH_XTENSA
```
## TODO
# Revalidate arduino versions
# mark versions 1.0.2 1.0.3 as bad releases
24 changes: 21 additions & 3 deletions create.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ find ./src/tensorflow/ ./src/signal/ -type f -exec sed -i -e 's/#include "tools\
echo "Use esp-nn kernals"
#replace standard kernals with esp nn
cp -a ./src/tensorflow/lite/micro/kernels/esp_nn/. ./src/tensorflow/lite/micro/kernels/
#add ESP_NN define
sed -i 's/#define TENSORFLOW_LITE_C_COMMON_H_/#define TENSORFLOW_LITE_C_COMMON_H_\n#define ESP_NN 1/g' ./src/tensorflow/lite/c/common.h

echo "Making esp-nn files structured for PIO/Arduino"
git clone --recurse-submodules https://github.com/espressif/esp-nn.git
Expand All @@ -48,8 +46,28 @@ find ./src/esp-nn/ -type f -exec sed -i -e 's/#include <esp_nn_defs.h>/#include
find ./src/tensorflow/ -type f -exec sed -i -e 's/#include <esp_nn.h>/#include "esp-nn\/esp_nn.h"/g' {} \;
find ./src/esp-nn/ -type f -iname "*esp32s3.S" -exec sed -i '1s/^/#ifdef ARCH_ESP32_S3\n/;$a\\n#endif' {} \;

echo "Clean up"
# ESP32 requires TF_LITE_REMOVE_VIRTUAL_DELETE descrutors to be made public
sed -i '/TF_LITE_REMOVE_VIRTUAL_DELETE/d' ./src/tensorflow/lite/micro/memory_planner/linear_memory_planner.h
sed -i '/TF_LITE_REMOVE_VIRTUAL_DELETE/d' ./src/tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h
sed -i '/TF_LITE_REMOVE_VIRTUAL_DELETE/d' ./src/tensorflow/lite/micro/memory_planner/greedy_memory_planner.h

sed -i 's/private:/TF_LITE_REMOVE_VIRTUAL_DELETE\n&/' ./src/tensorflow/lite/micro/memory_planner/linear_memory_planner.h
sed -i 's/private:/TF_LITE_REMOVE_VIRTUAL_DELETE\n&/' ./src/tensorflow/lite/micro/tflite_bridge/micro_error_reporter.h
sed -i 's/private:/TF_LITE_REMOVE_VIRTUAL_DELETE\n&/' ./src/tensorflow/lite/micro/memory_planner/greedy_memory_planner.h
# change all occurance of #ifndef TF_LITE_STATIC_MEMORY to #ifdef TF_LITE_NOT_STATIC_MEMORY
find ./src/ -type f -exec sed -i -e 's/#ifndef TF_LITE_STATIC_MEMORY/#ifdef TF_LITE_NOT_STATIC_MEMORY/g' {} \;
find ./src/ -type f -exec sed -i -e 's/#if !defined(TF_LITE_STATIC_MEMORY)/#if defined(TF_LITE_NOT_STATIC_MEMORY)/g' {} \;
find ./src/ -type f -exec sed -i -e 's/#ifdef TF_LITE_STATIC_MEMORY/#define TF_LITE_STATIC_MEMORY\n#ifdef TF_LITE_STATIC_MEMORY/g' {} \;




# Create header file for library
echo "// do not delete" > ./src/ESP_TF.h
echo "// placeholder for arduino library rules" >> ./src/ESP_TF.h

#clean up
echo "Clean up"
rm -fr esp-tflite-micro
rm -fr esp-nn

Expand Down
8 changes: 8 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "ESP_TF",
"version": "2.0.0",
"build": {
"srcDir": "src",
"includeDir": "src"
}
}
2 changes: 1 addition & 1 deletion src/ESP_TF.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// do not delete
// placeholder for arduino library rules
// placeholder for arduino library rules
1 change: 0 additions & 1 deletion src/tensorflow/lite/c/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ limitations under the License.

#ifndef TENSORFLOW_LITE_C_COMMON_H_
#define TENSORFLOW_LITE_C_COMMON_H_
#define ESP_NN 1

#include "tensorflow/lite/core/c/common.h"

Expand Down
3 changes: 2 additions & 1 deletion src/tensorflow/lite/core/api/flatbuffer_conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ TfLiteRngAlgorithm ConvertRngAlgorithm(RngAlgorithm algorithm) {
return kTfLiteRngAlgorithmUnknown;
}

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
TfLiteStatus ParseOpDataTfLite(const Operator* op, BuiltinOperator op_type,
ErrorReporter* error_reporter,
BuiltinDataAllocator* allocator,
Expand Down Expand Up @@ -2890,6 +2890,7 @@ TfLiteStatus ParseOpData(const Operator* op, BuiltinOperator op_type,
// * If all the builtin operators were to have their own parse functions, or we
// were ok with some amount of code duplication, then this split of the .cc
// files would be a lot more feasible.
#define TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_STATIC_MEMORY
TF_LITE_REPORT_ERROR(
error_reporter,
Expand Down
8 changes: 4 additions & 4 deletions src/tensorflow/lite/core/c/common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ limitations under the License.

#include "tensorflow/lite/core/c/common.h"

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
#include <cstdlib>
#endif // TF_LITE_STATIC_MEMORY

Expand Down Expand Up @@ -67,7 +67,7 @@ int TfLiteVarArrayEqual(const T* const a, const T* const b) {
return TfLiteVarArrayEqualsArray(a, b->size, b->data);
}

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY

template <class T>
T* TfLiteVarArrayCreate(const int size) {
Expand Down Expand Up @@ -119,7 +119,7 @@ int TfLiteIntArrayEqualsArray(const TfLiteIntArray* a, int b_size,
return TfLiteVarArrayEqualsArray(a, b_size, b_data);
}

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY

TfLiteIntArray* TfLiteIntArrayCreate(int size) {
return TfLiteVarArrayCreate<TfLiteIntArray>(size);
Expand All @@ -137,7 +137,7 @@ int TfLiteFloatArrayGetSizeInBytes(int size) {
return TfLiteVarArrayGetSizeInBytes<TfLiteFloatArray>(size);
}

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY

TfLiteFloatArray* TfLiteFloatArrayCreate(int size) {
return TfLiteVarArrayCreate<TfLiteFloatArray>(size);
Expand Down
12 changes: 6 additions & 6 deletions src/tensorflow/lite/core/c/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ typedef struct TfLiteIntArray {
/// in bytes.
size_t TfLiteIntArrayGetSizeInBytes(int size);

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
/// Create a array of a given `size` (uninitialized entries).
/// This returns a pointer, that you must free using TfLiteIntArrayFree().
TfLiteIntArray* TfLiteIntArrayCreate(int size);
Expand All @@ -139,7 +139,7 @@ int TfLiteIntArrayEqual(const TfLiteIntArray* a, const TfLiteIntArray* b);
int TfLiteIntArrayEqualsArray(const TfLiteIntArray* a, int b_size,
const int b_data[]);

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
/// Create a copy of an array passed as `src`.
/// You are expected to free memory with TfLiteIntArrayFree
TfLiteIntArray* TfLiteIntArrayCopy(const TfLiteIntArray* src);
Expand Down Expand Up @@ -170,7 +170,7 @@ typedef struct TfLiteFloatArray {
/// size in bytes.
int TfLiteFloatArrayGetSizeInBytes(int size);

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
/// Create a array of a given `size` (uninitialized entries).
/// This returns a pointer, that you must free using TfLiteFloatArrayFree().
TfLiteFloatArray* TfLiteFloatArrayCreate(int size);
Expand Down Expand Up @@ -474,7 +474,7 @@ typedef enum TfLiteCustomAllocationFlags {

/// A tensor in the interpreter system which is a wrapper around a buffer of
/// data including a dimensionality (or NULL if not currently defined).
#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
typedef struct TfLiteTensor {
/// The data type specification for data stored in `data`. This affects
/// what member of `data` union should be used.
Expand Down Expand Up @@ -692,7 +692,7 @@ typedef struct TfLiteEvalTensor {
TfLiteType type;
} TfLiteEvalTensor;

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
/// Free data memory of tensor `t`.
void TfLiteTensorDataFree(TfLiteTensor* t);

Expand Down Expand Up @@ -1410,7 +1410,7 @@ typedef struct TfLiteOpaqueDelegateBuilder {
int64_t flags;
} TfLiteOpaqueDelegateBuilder;

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
// See c_api_opaque.h.
// This declaration in common.h is only for backwards compatibility.
// NOTE: This function is part of the TensorFlow Lite Extension APIs, see above.
Expand Down
2 changes: 1 addition & 1 deletion src/tensorflow/lite/core/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ limitations under the License.
#define TFLITE_HAS_ATTRIBUTE_WEAK 0
#endif

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
// maximum size of a valid flatbuffer
inline constexpr unsigned int flatbuffer_size_max = 2147483648;
// If none zero then the buffer is stored outside of the flatbuffers, string
Expand Down
2 changes: 1 addition & 1 deletion src/tensorflow/lite/kernels/internal/compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ limitations under the License.
#define TFLITE_CHECK_LT(x, y) ((x) < (y)) ? (void)0 : TFLITE_ABORT
#endif

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
// TODO(b/162019032): Consider removing these type-aliases.
using int8 = std::int8_t;
using uint8 = std::uint8_t;
Expand Down
9 changes: 5 additions & 4 deletions src/tensorflow/lite/kernels/kernel_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ limitations under the License.
#include <complex>
#include <limits>
#include <memory>
#ifndef TF_LITE_STATIC_MEMORY

#ifdef TF_LITE_NOT_STATIC_MEMORY
#include <string>

#include "tensorflow/lite/array.h"
Expand Down Expand Up @@ -151,7 +152,7 @@ const TfLiteTensor* GetOptionalInputTensor(const TfLiteContext* context,
return GetInput(context, node, index);
}

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
TfLiteTensor* GetTemporary(TfLiteContext* context, const TfLiteNode* node,
int index) {
const int tensor_index = ValidateTensorIndexing(
Expand Down Expand Up @@ -413,7 +414,7 @@ bool HaveSameShapes(const TfLiteTensor* input1, const TfLiteTensor* input2) {
return TfLiteIntArrayEqual(input1->dims, input2->dims);
}

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
TfLiteStatus GetOutputShapeFromInput(TfLiteContext* context,
const TfLiteTensor* input,
TfLiteIntArray** output_shape) {
Expand Down Expand Up @@ -580,7 +581,7 @@ bool IsMobilePlatform() {
}

bool HasUnspecifiedDimension(const TfLiteTensor* tensor) {
#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
if (tensor->dims_signature) {
for (int i : TfLiteIntArrayView(tensor->dims_signature)) {
if (i == -1) return true;
Expand Down
10 changes: 5 additions & 5 deletions src/tensorflow/lite/kernels/kernel_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ limitations under the License.
#include <stdint.h>

#include <limits>
#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
#include <string>
#endif // TF_LITE_STATIC_MEMORY

Expand Down Expand Up @@ -100,7 +100,7 @@ TfLiteStatus GetOutputSafe(const TfLiteContext* context, const TfLiteNode* node,
const TfLiteTensor* GetOptionalInputTensor(const TfLiteContext* context,
const TfLiteNode* node, int index);

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
// Note: You must check if result is not null:
//
// TfLiteTensor* my_tensor = GetTemporary(context, node, kMyTensorIdx);
Expand Down Expand Up @@ -162,7 +162,7 @@ inline int NumOutputs(const TfLiteNode* node) {
return node->outputs == nullptr ? 0 : node->outputs->size;
}

#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
inline int NumIntermediates(const TfLiteNode* node) {
return node->intermediates->size;
}
Expand Down Expand Up @@ -214,7 +214,7 @@ inline bool IsConstantOrPersistentTensor(const TfLiteTensor* tensor) {
inline bool IsDynamicTensor(const TfLiteTensor* tensor) {
return tensor->allocation_type == kTfLiteDynamic;
}
#ifndef TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_NOT_STATIC_MEMORY
// Sets tensor to dynamic.
inline void SetTensorToDynamic(TfLiteTensor* tensor) {
if (tensor->allocation_type != kTfLiteDynamic) {
Expand Down Expand Up @@ -302,7 +302,7 @@ void CalculateActivationRange(TfLiteFusedActivation activation,
// Return true if the given tensors have the same shape.
bool HaveSameShapes(const TfLiteTensor* input1, const TfLiteTensor* input2);

#if !defined(TF_LITE_STATIC_MEMORY)
#if defined(TF_LITE_NOT_STATIC_MEMORY)
// Gets the output shape from the input tensor.
TfLiteStatus GetOutputShapeFromInput(TfLiteContext* context,
const TfLiteTensor* input,
Expand Down
1 change: 1 addition & 0 deletions src/tensorflow/lite/micro/compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
// nothing to avoid linking in ::delete().
// This macro needs to be included in all subclasses of a virtual base class in
// the private section.
#define TF_LITE_STATIC_MEMORY
#ifdef TF_LITE_STATIC_MEMORY
#define TF_LITE_REMOVE_VIRTUAL_DELETE \
void operator delete(void* p) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,9 @@ class GreedyMemoryPlanner : public MicroMemoryPlanner {
// after invocation. Do to the fact that tensors that tensor data for tensors
// that aren't being used during a phase of invocation are overwritten.
bool preserves_all_tensors() const override { return false; }
TF_LITE_REMOVE_VIRTUAL_DELETE


private:
TF_LITE_REMOVE_VIRTUAL_DELETE
private:
// Whether a buffer is active in a given time range.
bool DoesEntryOverlapInTime(const ListEntry* entry, const int first_time_used,
const int last_time_used) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class LinearMemoryPlanner : public MicroMemoryPlanner {
// Returns True because the LinearMemoryPlanner preserves all tensors after
// invocation.
bool preserves_all_tensors() const override { return true; }
TF_LITE_REMOVE_VIRTUAL_DELETE

private:
TF_LITE_REMOVE_VIRTUAL_DELETE
private:
static constexpr int kMaxBufferCount = 1024;
size_t buffer_offsets_[kMaxBufferCount];
int current_buffer_count_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class MicroErrorReporter : public ErrorReporter {
public:
~MicroErrorReporter() override {}
int Report(const char* format, va_list args) override;
TF_LITE_REMOVE_VIRTUAL_DELETE

private:
TF_LITE_REMOVE_VIRTUAL_DELETE
private:
};

} // namespace tflite
Expand Down

0 comments on commit 22be532

Please sign in to comment.