Commit Graph

18 Commits

Author SHA1 Message Date
Tiezhen WANG
81cff8fc90 TFLM: Rename FinishTensorAllocation to FinishModelAllocation in the comments.
The name of this function has been changed.

PiperOrigin-RevId: 316790532
Change-Id: I31a77d3c2e7fe5af3138c99317ccd075ecbdf4ff
2020-06-16 17:37:23 -07:00
Jens Elofsson
708ecda43e Merge remote-tracking branch 'upstream/master' into offline_memory_planner 2020-06-15 10:06:36 +02:00
Nick Kreeger
cd6a929e30 Track variable tensor buffer allocation in the "recording" MicroAllocator.
The RecordingMicroAllocator class currently doesn't track variable tensor allocations. This was noted why the measured allocations had a missing ~10kb of tail space unaccounted for in the keyword model. This change tracks variable tensor allocation for the keyword model (the test conv model does not have any variable tensors).

Total and tail allocation creep up a bit here to handle the additional fields in RecordingMicroAllocator:

TestKeywordModelMemoryThreshold:
-------------------------------
[RecordingMicroAllocator] Arena allocation total 21472 bytes
[RecordingMicroAllocator] Arena allocation head 672 bytes
[RecordingMicroAllocator] Arena allocation tail 20800 bytes
[RecordingMicroAllocator] 'TfLiteTensor struct' used 6048 bytes with alignment overhead (requested 6048 bytes for 54 tensors)
[RecordingMicroAllocator] 'TfLiteTensor quantization data' used 2160 bytes with alignment overhead (requested 2160 bytes for 162 allocations)
[RecordingMicroAllocator] 'TfLiteTensor variable buffer data' used 10240 bytes with alignment overhead (requested 10240 bytes for 7 allocations)
[RecordingMicroAllocator] 'NodeAndRegistration struct' used 1200 bytes with alignment overhead (requested 1200 bytes for 15 NodeAndRegistration structs)
[RecordingMicroAllocator] 'Operator runtime data' used 148 bytes with alignment overhead (requested 148 bytes for 13 OpData structs)

TestConvModelMemoryThreshold:
-----------------------------
[RecordingMicroAllocator] Arena allocation total 12128 bytes
[RecordingMicroAllocator] Arena allocation head 7744 bytes
[RecordingMicroAllocator] Arena allocation tail 4384 bytes
[RecordingMicroAllocator] 'TfLiteTensor struct' used 1680 bytes with alignment overhead (requested 1680 bytes for 15 tensors)
[RecordingMicroAllocator] 'TfLiteTensor quantization data' used 1216 bytes with alignment overhead (requested 1216 bytes for 36 allocations)
[RecordingMicroAllocator] 'TfLiteTensor variable buffer data' used 0 bytes with alignment overhead (requested 0 bytes for 0 allocations)
[RecordingMicroAllocator] 'NodeAndRegistration struct' used 560 bytes with alignment overhead (requested 560 bytes for 7 NodeAndRegistration structs)
[RecordingMicroAllocator] 'Operator runtime data' used 136 bytes with alignment overhead (requested 136 bytes for 5 OpData structs)
PiperOrigin-RevId: 316166016
Change-Id: I7d806f901b39e5d6a73c3baaf11d85fa7f6e17b1
2020-06-12 13:39:35 -07:00
Jens Elofsson
2f9642602d Adapt to changes in micro_allocator. 2020-06-12 10:49:57 +02:00
Nick Kreeger
d8881eb71d Add a memory threshold allocation test for the Keyword model.
This new test ensures that TF Micro does not regress current allocations (on x86-64 systems) for a canonical model. As RAM reduction changes are introduced, the values in this test can be updated from the console log of this test.

Current output for the keyword model:
Testing TestKeywordModelMemoryThreshold
[RecordingMicroAllocator] Arena allocation total 21440 bytes
[RecordingMicroAllocator] Arena allocation head 672 bytes
[RecordingMicroAllocator] Arena allocation tail 20768 bytes
[RecordingMicroAllocator] 'TfLiteTensor struct allocation' used 6048 bytes (requested 6048 bytes 54 times)
[RecordingMicroAllocator] 'TfLiteTensor quantization data allocations' used 2160 bytes (requested 2160 bytes 162 times)
[RecordingMicroAllocator] 'NodeAndRegistration struct allocation' used 1200 bytes (requested 1200 bytes 15 times)
[RecordingMicroAllocator] 'Operator runtime data allocation' used 148 bytes (requested 148 bytes 13 times)

PiperOrigin-RevId: 315958032
Change-Id: I226f6a01aa555970805388632559241a41ff8342
2020-06-11 12:54:09 -07:00
Nick Kreeger
26ee75e596 Decouple the model and TfLiteContext instance from the allocator and interpreter.
This change simplifies the interaction between the MicroInterpreter and MicroAllocator. All allocation for a given model is staged in MicroAllocator.StartModelAllocation() and MicroAllocator.FinishModelAllocation().

This change prepares for two upcoming features:
1.) Multi-tenant memory arena
2.) An easy-to-use RecordingMicroInterpreter to allow auditing of recorded memory arena allocations.

PiperOrigin-RevId: 315736762
Change-Id: Ia9da1f6edcd1001e3aad975c117905054f172e18
2020-06-10 12:12:35 -07:00
Nick Kreeger
1a90749db9 Enable the ability to pass a MicroAllocator instance into a MicroInterpreter instance.
This change is a stepping stone to enable users to:
1.) Enable users to use a single MicroAllocator/arena for multiple models.
2.) Enable users to use the new recording allocation APIs for auditing arena allocations.

PiperOrigin-RevId: 315414448
Change-Id: Ied1ea56deb73c09bb64b3e41fd3502b5a4cd5bb8
2020-06-08 21:27:47 -07:00
Nick Kreeger
9d572b8d5e Introduce a "recording" MicroAllocator class.
This new class enables TFLM to measure, audit, and report memory usage in the shared tensor arena. Users may opt into this class by simply passing this class into a MicroInterpreter instance.

PiperOrigin-RevId: 314995667
Change-Id: I6a451944d55b0498a98f1cfd54244f9008e578d2
2020-06-05 14:25:46 -07:00
Advait Jain
33689c48ad Add MicroOpResolver interface class.
This will allow us to implement selective registration of the builtin parse
functions without changing the OpResolver base class in TFLite.

* MicroOpResolver is now an interface (matching the OpResolver name in TFLite).
* MicroMutableOpResolver is the implementation of the MicroOpResolver
  interface that should be used by applications that do not want to use
  AllOpsResolver.

PiperOrigin-RevId: 313691276
Change-Id: I0a9f51f6584326a3b3dd645cde083ba42116083d
2020-05-28 17:38:37 -07:00
Nick Kreeger
8c8dc2699b Cleanup and refactor all allocations in MicroAllocator to function calls.
This change is a precursor to adding a new memory logging MicroAllocator subclass that will enable TFLM to keep track of tensor arena tail allocations. Outside of moving all arena allocations to utility methods - I also cleaned up the organization of the methods inside of the cc file.

PiperOrigin-RevId: 313242666
Change-Id: Icddcc07187419fe314bc57708170cda8cd35690a
2020-05-26 12:17:39 -07:00
Advait Jain
f1471bd25c Include what you use for the micro_framework bazel target.
PiperOrigin-RevId: 311808278
Change-Id: I2869b7c191c71461d44edc77dc1cd3999c6376d9
2020-05-15 15:09:06 -07:00
Nick Kreeger
a3c55c2437 Reduce space required for TFLM by dropping two non-needed pointers from MicroAllocator.
PiperOrigin-RevId: 310965121
Change-Id: I1d7abe758ef14bfe722c24bfe8878a37a4c738f8
2020-05-11 12:23:53 -07:00
Tiezhen WANG
3564081ab8 TFLM: Print out the buffer usage.
Head space is reusable while the tail space is persistent.
This gives some guidance on how much ram could be saved by using multi-tenant TFLM.

Check test `TestFinishTensorAllocation` in micro_allocator_test.cc for the usage.

PiperOrigin-RevId: 310271606
Change-Id: I5ad75fb4a01504ba584d2af036f474869270bba1
2020-05-06 18:46:28 -07:00
Tiezhen WANG
298b24151e TFLM: Add an interpreter API to inspect the actual number of bytes used in the arena.
This helps to choose the optimal arena size.

- I've also used this tool to adjust the arena size for a few test cases.
- This CL changes the GreedyMemoryPlanner by exposing the per buffer size requirement so that we can estimate if the remaining arena is enough for planning the entire buffer.

PiperOrigin-RevId: 307628733
Change-Id: Id47f578a0bd0b67a3bbbd2a2ef7103d2336b17aa
2020-04-21 10:16:58 -07:00
Tiezhen WANG
9cc03a222c TFLM: implement new memory planning API.
A few things to notice.
- ContextHelper is a helper class reducing the overload on the interpreter. It forwards the request to the allocator while keep tracking the latest node ID.
- Buffers have are located in different areas due to their different lifespan. persistent buffers and scratch buffer handles need to be allocated from the persistent area (tail). Scratch buffers sit together with other tensors (head).
- Buffer handles are located in a "stack". The most recent buffer handle has the lowest address. This optimization saves us from reversing the order of buffer handles list.

PiperOrigin-RevId: 298288221
Change-Id: I7fa55d2b9acf837eafa1c4eedc6d7d339100af95
2020-03-02 00:25:24 -08:00
Tiezhen WANG
61ee33c063 TFLM: Refactor micro allocator, including:
- SplitFinishTensorAllocation method into several stateless functions.
- Rename TensorInfo to allocationInfo so that later we can use it for buffers as well
- Move tensor initialization to the constructor
- Move InitializeRuntimeTensor out of MicroAllocator since it shouldn't be called by clients.
- Make MicroArena aligned by default.

PiperOrigin-RevId: 290262535
Change-Id: I2a4d06cb749368919038b17ba18727f7babdc322
2020-01-17 07:01:48 -08:00
Advait Jain
86fe4669b9 Remove code that was only used in tests.
PiperOrigin-RevId: 285023314
Change-Id: I40ee3e14ecb634830e128b4453cbf27589da3a40
2019-12-11 11:18:42 -08:00
Pete Warden
59c06b9016 Moved TensorFlow Lite Micro out of experimental folder
PiperOrigin-RevId: 284600414
Change-Id: I62238882b1447e53fe5c5c36d021f42eaf56486a
2019-12-09 11:43:28 -08:00