Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

The debug binary now show how much memory it will try to allocate on the heap #595

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

antonionevado
Copy link
Contributor

Currently there is no way of knowing how much memory a network model requires for activations on deployment.

Motivation and Context

It is possible to know the required memory from the Tensorflow model but will not match with the actual required amount of memory due to buffer reuse and quantization. This PR will make the debug binary (lm_xxx.elf) to print a message that show how much memory will be allocated on the heap before calling Network::init(). This allows you to have an idea of how much memory will be used and, debug possibly related memory issues.

Description

  • Added a new method to Network class that returns the amount of memory in bytes.
  • Use this method from mains/main.cpp and print how many megabytes will be required by the model.

How has this been tested?

Tested with 2 of the provided examples:

  • classification: lmnet_quantized_cifar10
  • object detection: widerface_v5

Screenshots (if appropriate):

For object detection this will show:

This network will try to allocate 10.2753 MB on the heap
-------------------------------------------------------------
Comparison: Default network test  succeeded!!!
-------------------------------------------------------------

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature / Optimization (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.

@blueoil-butler blueoil-butler bot added the CI: auto-run Run CI automatically label Nov 7, 2019
@antonionevado antonionevado added enhancement New feature or request and removed accuracy_improvement labels Nov 7, 2019
@tkng tkng self-requested a review November 7, 2019 02:46
total_mem_bytes += sizeof(QUANTIZED_PACKED) * max_device_input_elems;
total_mem_bytes += sizeof(BIN_CONV_OUTPUT) * max_device_output_elems;
#endif
return total_mem_bytes;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are several temporary buffer allocated by some operators (ex. kn2row_buf in src/func/impl/generic/quantized_conv2d_kn2row.cpp), but these are not counted yet.
I'm working to allocate these hidden temporary buffers in Network::init() ( #473 ), but it may take a while...
Until it is resolved, we should count these by hand...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah thank you! Didn't know about these buffers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can wait until they are allocated from Network::init(), if you want.
Or we can add it later and count by hand as you said 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These buffers are allocated by std::make_unique.

I found such buffers in following files:

  • src/func/conv2d.cpp
  • src/func/*/batch_normalization.cpp
  • src/func/impl/generic/quantized_conv2d_kn2row.cpp
  • src/func/impl/arm_neon/quantized_conv2d_tiling.cpp
  • src/func/impl/x86_avx/quantized_conv2d_tiling.cpp
  • src/matrix/multiplication.cpp

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes, seems that for changing layout we need these temporaries...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When #473 is fixed, buffers may be unified into single buffer, total buffer size is obvious.
It seems waiting for fixed is reasonable way.

@@ -28,6 +28,7 @@ class SYM_PUBLIC Network
Network();
~Network();

int memory();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep consistency of API, it looks better to use verb for the method name. Considering that the responsibility of this function should output debug information, how about renaming to get_debug_info?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I propose to change this API to return string, (or char*), so that we can see the buffer size layer by layer. (Is it possible to change the behavior of this API?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's true. The name can be better and we should return more detailed information.

@Joeper214 Joeper214 added WIP Work in progress wontfix This will not be worked on labels Jan 23, 2020
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
CI: auto-run Run CI automatically enhancement New feature or request WIP Work in progress wontfix This will not be worked on
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants