This change drastically modifies the way memory is used in TF Micro. Currently, large blocks of persistent memory are allocated for TfLiteTensor structs and any associated quantization data. Instead of this pattern, those TfLiteTensor structs and quantization data will be allocated from the "temp" section of the memory arena. Instead of allocating a large block of TfLiteTensor structs - a minimal TfLiteEval struct is allocated. This new struct will serve as the source of truth for all buffers in the graph. Everything works in the kernel implementations with this change - they are just temporarily slower. All TfLiteTensor structs fetched from GetInput()/GetOutput()/etc are now allocated on the fly through the temp allocation. Each kernel should be updated to fetch the TfLiteEval struct in the Eval() block in each kernel. Additionally, quantization data should be cached in those op kernels. This CL saves up to 50% on the arena for larger conv-based models. PiperOrigin-RevId: 322224278 Change-Id: Id32509a75c9f68177f5bb6b850ea11907afcbb1d
60 lines
2.4 KiB
C++
60 lines
2.4 KiB
C++
/* 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_MEMORY_HELPERS_H_
|
|
#define TENSORFLOW_LITE_MICRO_MEMORY_HELPERS_H_
|
|
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
|
|
#include "tensorflow/lite/c/common.h"
|
|
#include "tensorflow/lite/core/api/error_reporter.h"
|
|
#include "tensorflow/lite/schema/schema_generated.h"
|
|
|
|
namespace tflite {
|
|
|
|
// Returns the next pointer address aligned to the given alignment.
|
|
uint8_t* AlignPointerUp(uint8_t* data, size_t alignment);
|
|
|
|
// Returns the previous pointer address aligned to the given alignment.
|
|
uint8_t* AlignPointerDown(uint8_t* data, size_t alignment);
|
|
|
|
// Returns an increased size that's a multiple of alignment.
|
|
size_t AlignSizeUp(size_t size, size_t alignment);
|
|
|
|
// Returns size in bytes for a given TfLiteType.
|
|
TfLiteStatus TfLiteTypeSizeOf(TfLiteType type, size_t* size);
|
|
|
|
// How many bytes are needed to hold a tensor's contents.
|
|
TfLiteStatus BytesRequiredForTensor(const tflite::Tensor& flatbuffer_tensor,
|
|
size_t* bytes, size_t* type_size,
|
|
ErrorReporter* error_reporter);
|
|
|
|
// How many bytes are used in a TfLiteEvalTensor instance. The byte length is
|
|
// returned in out_bytes.
|
|
TfLiteStatus TfLiteEvalTensorByteLength(const TfLiteEvalTensor* eval_tensor,
|
|
size_t* out_bytes);
|
|
|
|
// Deduce output dimensions from input and allocate given size.
|
|
// Useful for operators with two inputs where the largest input should equal the
|
|
// output dimension.
|
|
TfLiteStatus AllocateOutputDimensionsFromInput(TfLiteContext* context,
|
|
const TfLiteTensor* input1,
|
|
const TfLiteTensor* input2,
|
|
TfLiteTensor* output);
|
|
|
|
} // namespace tflite
|
|
|
|
#endif // TENSORFLOW_LITE_MICRO_MEMORY_HELPERS_H_
|