Skip to content

Commit

Permalink
Merge branch 'main' into upstream-fix2
Browse files Browse the repository at this point in the history
  • Loading branch information
suleshahid authored Dec 11, 2024
2 parents b52f8c9 + c9e2319 commit bd219ab
Show file tree
Hide file tree
Showing 35 changed files with 3,182 additions and 155 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,27 @@ jobs:
cd ../
tflite-micro/tensorflow/lite/micro/tools/ci_build/test_x86_default.sh tflite-micro/
x86_default_with_compression:
runs-on: ubuntu-latest

name: Makefile x86 with Compression (presubmit)
steps:
- uses: actions/setup-python@v4
with:
python-version: '3.10'
- uses: actions/checkout@v3
with:
ref: ${{ inputs.trigger-sha }}
- name: Install dependencies
run: |
pip3 install Pillow
pip3 install numpy
- name: Test
run: |
tensorflow/lite/micro/tools/ci_build/test_makefile.sh
cd ../
tflite-micro/tensorflow/lite/micro/tools/ci_build/test_x86_default_with_compression.sh tflite-micro/
x86_out_of_tree:
runs-on: ubuntu-latest

Expand Down
30 changes: 30 additions & 0 deletions .github/workflows/xtensa_presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ jobs:
/bin/bash -c \
"cd /opt && tflite-micro/tensorflow/lite/micro/tools/ci_build/test_xtensa_hifi5.sh tflite-micro/"
hifi5_unit_tests_with_compression:
runs-on: ubuntu-latest

name: Hifi5 Unit Tests with Compression (presubmit)
steps:
- uses: actions/checkout@v2
with:
ref: ${{ inputs.trigger-sha }}
- run: |
rm -rf .git
echo ${{ secrets.tflm-bot-token }} | docker login ghcr.io -u tflm-bot --password-stdin
docker run --env XTENSA_TOOLS_VERSION=RI-2022.9-linux --rm -v `pwd`:/opt/tflite-micro ghcr.io/tflm-bot/xtensa_xplorer_hifi5:0.2 \
/bin/bash -c \
"cd /opt && tflite-micro/tensorflow/lite/micro/tools/ci_build/test_xtensa_hifi5_with_compression.sh tflite-micro/"
hifi_3z_unit_tests:
runs-on: ubuntu-latest

Expand All @@ -65,3 +80,18 @@ jobs:
docker run --env XTENSA_TOOLS_VERSION=RI-2020.4-linux --rm -v `pwd`:/opt/tflite-micro ghcr.io/tflm-bot/xtensa_xplorer_13:0.3 \
/bin/bash -c \
"cd /opt && tflite-micro/tensorflow/lite/micro/tools/ci_build/test_xtensa_hifi3z.sh EXTERNAL tflite-micro/"
hifi_3z_unit_tests_with_compression:
runs-on: ubuntu-latest

name: Hifi3z Unit Tests with Compression (presubmit)
steps:
- uses: actions/checkout@v2
with:
ref: ${{ inputs.trigger-sha }}
- run: |
rm -rf .git
echo ${{ secrets.tflm-bot-token }} | docker login ghcr.io -u tflm-bot --password-stdin
docker run --env XTENSA_TOOLS_VERSION=RI-2020.4-linux --rm -v `pwd`:/opt/tflite-micro ghcr.io/tflm-bot/xtensa_xplorer_13:0.3 \
/bin/bash -c \
"cd /opt && tflite-micro/tensorflow/lite/micro/tools/ci_build/test_xtensa_hifi3z_with_compression.sh EXTERNAL tflite-micro/"
21 changes: 20 additions & 1 deletion tensorflow/lite/micro/BUILD
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(
"//tensorflow/lite/micro:build_def.bzl",
"tflm_cc_binary",
"tflm_cc_library",
"tflm_cc_test",
"tflm_copts",
Expand All @@ -28,6 +27,16 @@ tflm_cc_library(
],
)

tflm_cc_library(
name = "compression",
hdrs = [
"compression.h",
],
deps = [
"//tensorflow/lite/c:common",
],
)

tflm_cc_library(
# TODO(b/187093492): Rename to micro_interpreter.
name = "micro_framework",
Expand Down Expand Up @@ -63,10 +72,14 @@ tflm_cc_library(
"micro_context.h",
],
deps = [
":compression",
":micro_common",
":micro_graph",
":micro_log",
":micro_profiler",
"//tensorflow/lite:type_to_tflitetype",
"//tensorflow/lite/c:common",
"//tensorflow/lite/micro/kernels:decompress",
],
)

Expand Down Expand Up @@ -136,6 +149,7 @@ tflm_cc_library(
":memory_helpers",
":micro_allocator",
":micro_common",
":micro_context",
":micro_graph",
":micro_log",
":micro_profiler",
Expand Down Expand Up @@ -171,6 +185,7 @@ tflm_cc_library(
"micro_allocator.h",
],
deps = [
":compression",
":flatbuffer_utils",
":memory_helpers",
":micro_arena_constants",
Expand All @@ -183,6 +198,7 @@ tflm_cc_library(
"//tensorflow/lite/micro/arena_allocator:non_persistent_arena_buffer_allocator",
"//tensorflow/lite/micro/arena_allocator:persistent_arena_buffer_allocator",
"//tensorflow/lite/micro/arena_allocator:simple_memory_allocator",
"//tensorflow/lite/micro/compression:metadata_saved",
"//tensorflow/lite/micro/memory_planner:greedy_memory_planner",
"//tensorflow/lite/micro/memory_planner:linear_memory_planner",
"//tensorflow/lite/micro/memory_planner:micro_memory_planner",
Expand Down Expand Up @@ -236,7 +252,9 @@ tflm_cc_library(
"test_helpers.h",
],
deps = [
":compression",
":memory_helpers",
":micro_log",
":micro_utils",
":op_resolvers",
"//tensorflow/lite:type_to_tflitetype",
Expand Down Expand Up @@ -414,6 +432,7 @@ tflm_cc_library(
"hexdump.h",
],
deps = [
":debug_log",
":span",
":static_vector",
],
Expand Down
2 changes: 2 additions & 0 deletions tensorflow/lite/micro/build_def.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"""TfLite Micro BUILD options."""

def tflm_copts():
"""Returns the default copts for targets in TFLM.
Expand Down
68 changes: 68 additions & 0 deletions tensorflow/lite/micro/compression.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* Copyright 2024 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_MICRO_COMPRESSION_H_
#define TENSORFLOW_LITE_MICRO_MICRO_COMPRESSION_H_

#ifdef USE_TFLM_COMPRESSION

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

namespace tflite {

//
// Compressed tensors
//

static constexpr const char* kCompressionMetadataString =
"COMPRESSION_METADATA";

enum class CompressionScheme : uint8_t {
kBinQuant,
};

struct LookupTableData {
static constexpr size_t kMaxBitWidth = 7;
static constexpr size_t kMaxValueTableChannelStride = 128;

const void* value_table; // Pointer into FlatBuffer Values.
uint8_t value_table_channel_stride; // elements per channel
uint8_t compressed_bit_width : 3; // 1 to 7 bits
bool is_per_channel_quantized : 1; // tensor is per-channel quantized
bool use_alternate_axis : 1; // shape default channel:
// 0 = first, 1 = last
uint8_t reserved : 3;
};

union CompressionData {
LookupTableData* lut_data;
};

struct CompressionTensorData {
CompressionScheme scheme;
CompressionData data;
};

struct CompressedTensorList {
// Sparsely populated array with the same number of elements as there are
// tensors in the Subgraph. An alternative would include a tensor index in
// the struct for each and walk the list on look up. This could be slow.
const CompressionTensorData** tensors;
};

} // namespace tflite

#endif // USE_TFLM_COMPRESSION
#endif // TENSORFLOW_LITE_MICRO_MICRO_COMPRESSION_H_
79 changes: 74 additions & 5 deletions tensorflow/lite/micro/fake_micro_context.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2024 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.
Expand All @@ -23,10 +23,23 @@ limitations under the License.

namespace tflite {

FakeMicroContext::FakeMicroContext(TfLiteTensor* tensors,
SingleArenaBufferAllocator* allocator,
MicroGraph* micro_graph)
: graph_(*micro_graph), tensors_(tensors), allocator_(allocator) {}
FakeMicroContext::FakeMicroContext(
TfLiteTensor* tensors, SingleArenaBufferAllocator* allocator,
MicroGraph* micro_graph
#ifdef USE_TFLM_COMPRESSION
,
const CompressedTensorList* compressed_tensors
#endif // USE_TFLM_COMPRESSION
)
: graph_(*micro_graph),
tensors_(tensors),
allocator_(allocator)
#ifdef USE_TFLM_COMPRESSION
,
compressed_tensors_(compressed_tensors)
#endif // USE_TFLM_COMPRESSION
{
}

TfLiteTensor* FakeMicroContext::AllocateTempTfLiteTensor(int tensor_index) {
allocated_temp_count_++;
Expand Down Expand Up @@ -112,4 +125,60 @@ void* FakeMicroContext::external_context() { return nullptr; }

MicroGraph& FakeMicroContext::graph() { return graph_; }

#ifdef USE_TFLM_COMPRESSION

// Available during Prepare & Eval. Returns false if tensor is not
// compressed.
bool FakeMicroContext::IsTensorCompressed(const TfLiteNode* node,
int tensor_idx) {
if (compressed_tensors_ != nullptr && tensor_idx < node->inputs->size) {
int index = node->inputs->data[tensor_idx];
if (index >= 0 && compressed_tensors_->tensors[index] != nullptr) {
return true;
}
}

return false;
}

// Only available during Prepare. The kernel is responsible for storing the
// scratch buffer handle.
int FakeMicroContext::AllocateDecompressionScratchBuffer(const TfLiteNode* node,
int tensor_idx) {
if (compressed_tensors_ == nullptr || tensor_idx >= node->inputs->size) {
return -1;
}
int index = node->inputs->data[tensor_idx];
if (index < 0 || compressed_tensors_->tensors[index] == nullptr) {
return -1;
}
TfLiteTensor* tensor = &tensors_[index];
int scratch_index = -1;
TfLiteStatus result =
RequestScratchBufferInArena(tensor->bytes, &scratch_index);
if (result != kTfLiteOk) {
return -1;
}

return scratch_index;
}

// Available during Prepare & Eval. Returns nullptr if tensor is not
// compressed.
const CompressionTensorData* FakeMicroContext::GetTensorCompressionData(
const TfLiteNode* node, int tensor_idx) {
if (compressed_tensors_ == nullptr || tensor_idx >= node->inputs->size) {
return nullptr;
}

int index = node->inputs->data[tensor_idx];
if (index < 0) {
return nullptr;
}

return compressed_tensors_->tensors[index];
}

#endif // USE_TFLM_COMPRESSION

} // namespace tflite
36 changes: 34 additions & 2 deletions tensorflow/lite/micro/fake_micro_context.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2024 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.
Expand Down Expand Up @@ -30,7 +30,12 @@ class FakeMicroContext : public MicroContext {
~FakeMicroContext() = default;

FakeMicroContext(TfLiteTensor* tensors, SingleArenaBufferAllocator* allocator,
MicroGraph* micro_graph);
MicroGraph* micro_graph
#ifdef USE_TFLM_COMPRESSION
,
const CompressedTensorList* compressed_tensors = nullptr
#endif // USE_TFLM_COMPRESSION
);

void* AllocatePersistentBuffer(size_t bytes) override;
TfLiteStatus RequestScratchBufferInArena(size_t bytes,
Expand All @@ -50,6 +55,24 @@ class FakeMicroContext : public MicroContext {
void* external_context() override;
MicroGraph& graph() override;

#ifdef USE_TFLM_COMPRESSION

// Available during Prepare & Eval. Returns false if tensor is not
// compressed.
bool IsTensorCompressed(const TfLiteNode* node, int tensor_idx) override;

// Only available during Prepare. The kernel is responsible for storing the
// scratch buffer handle.
int AllocateDecompressionScratchBuffer(const TfLiteNode* node,
int tensor_idx) override;

// Available during Prepare & Eval. Returns nullptr if tensor is not
// compressed.
const CompressionTensorData* GetTensorCompressionData(
const TfLiteNode* node, int tensor_idx) override;

#endif // USE_TFLM_COMPRESSION

private:
static constexpr int kNumScratchBuffers_ = 12;

Expand All @@ -62,6 +85,15 @@ class FakeMicroContext : public MicroContext {

SingleArenaBufferAllocator* allocator_;

#ifdef USE_TFLM_COMPRESSION

//
// Compression
//
const CompressedTensorList* compressed_tensors_;

#endif // USE_TFLM_COMPRESSION

TF_LITE_REMOVE_VIRTUAL_DELETE
};

Expand Down
Loading

0 comments on commit bd219ab

Please sign in to comment.