tensorflow lite cmake compilation failed to allocate memory
I ran it again and got this `In file included from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/xnnpack/src/amalgam/gen/fma3.c:7:
/usr/include/math.h:37:10: fatal error: /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/xnnpack/src/bits/types.h: Cannot allocate memory
   37 | #include <bits/types.h>` Hi @Luca-Stefanescu 

Have you tried [these](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/examples/minimal#tensorflow-lite-c-minimal-example) steps for minimal example and hitting the errors?

In the minimal_build directory, after build we need to run the executable by 

`./minimal <path/to/tflite/model>`

Thanks. > Hi @Luca-Stefanescu
> 
> Have you tried [these](https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/examples/minimal#tensorflow-lite-c-minimal-example) steps for minimal example and hitting the errors?
> 
> In the minimal_build directory, after build we need to run the executable by
> 
> `./minimal <path/to/tflite/model>`
> 
> Thanks.

Yep I followed those instructions and got "failed to allocate memory" when compiling. It didn't complete compilation @pkgoogle I was able to reproduce this issue on both master and 2.13 branches. Please find this [gist](https://colab.research.google.com/gist/pjpratik/0bc677213e4e457cb44a7622c5bbe89d/61485.ipynb).

Could you please look into this issue?

Thanks. Hi @Luca-Stefanescu, thanks for reporting the issue.

You don't need to run ./configure, that is mainly for building with bazel. Where did you make your build directory? it should be a sibling of your tensorflow_src directory. Can you confirm you are following the directions here? https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/examples/minimal Are you in the build directory when you run
```sh
cmake ../tensorflow_src/tensorflow/lite/examples/minimal
```
?

Also please let us know the errors you are getting when you say "I get quite a lot of errors" (like the first page if possible)

It's possible the commit you are cloning is unstable, you can also try switching to the nightly or r2.14 (or earlier) branch. > Hi @Luca-Stefanescu, thanks for reporting the issue.
> 
> You don't need to run ./configure, that is mainly for building with bazel. Where did you make your build directory? it should be a sibling of your tensorflow_src directory. Can you confirm you are following the directions here? https://github.com/tensorflow/tensorflow/tree/master/tensorflow/lite/examples/minimal Are you in the build directory when you run
> 
> ```shell
> cmake ../tensorflow_src/tensorflow/lite/examples/minimal
> ```
> 
> ?
> 
> Also please let us know the errors you are getting when you say "I get quite a lot of errors" (like the first page if possible)
> 
> It's possible the commit you are cloning is unstable, you can also try switching to the nightly or r2.14 (or earlier) branch.

Build directory is a sibling of tensorflow_src/ so I go into build directory and if I do `ls ../tensorflow_src/tensorflow/lite/examples/minimal` it shows the CMakeLists.txt file and the other files in the minimal/ folder. And then I run cmake from the build directory. 

This is the first page with fatal error. I am getting high memory usage by WSL, more than 11GB during this compilation.

```[ 92%] Building CXX object tensorflow-lite/CMakeFiles/tensorflow-lite.dir/kernels/internal/utils/sparsity_format_converter.cc.o
In file included from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/core/async/c/async_signature_runner.cc:17:
/mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/c/c_api_internal.h:31:10: fatal error: /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/cpuinfo/tensorflow/lite/profiling/telemetry/c/profiler.h: Cannot allocate memory
   31 | #include "tensorflow/lite/profiling/telemetry/c/profiler.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
In file included from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/third_party/eigen3/Eigen/Core:1,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/kernels/dequantize.h:20,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/kernels/numeric_verify.cc:26:
/mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/eigen/Eigen/Core:50:10: fatal error: /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/xnnpack/include/complex: Cannot allocate memory
   50 | #include <complex>
      |          ^~~~~~~~~
In file included from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/kernels/internal/optimized/optimized_ops.h:41,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/kernels/detection_postprocess.cc:27:
/mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/third_party/eigen3/Eigen/Core:1:10: fatal error: /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/xnnpack/include/Eigen/Core: Cannot allocate memory
    1 | #include "Eigen/Core"
      |          ^~~~~~~~~~~~
compilation terminated.
compilation terminated.
make[2]: *** [tensorflow-lite/CMakeFiles/tensorflow-lite.dir/build.make:412: tensorflow-lite/CMakeFiles/tensorflow-lite.dir/core/async/c/async_signature_runner.cc.o] Error 1
make[2]: *** Waiting for unfinished jobs....
make[2]: *** [tensorflow-lite/CMakeFiles/tensorflow-lite.dir/build.make:1434: tensorflow-lite/CMakeFiles/tensorflow-lite.dir/kernels/detection_postprocess.cc.o] Error 1
make[2]: *** [tensorflow-lite/CMakeFiles/tensorflow-lite.dir/build.make:2008: tensorflow-lite/CMakeFiles/tensorflow-lite.dir/kernels/numeric_verify.cc.o] Error 1
In file included from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/third_party/eigen3/Eigen/Core:1,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/kernels/internal/optimized/optimized_ops.h:41,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/kernels/gru_cell.cc:20:
/mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/eigen/Eigen/Core:93:10: fatal error: /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/flatbuffers/include/climits: Input/output error
compilation terminated.
In file included from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/c/c_api_internal.h:28,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/c/c_api_for_testing.cc:17:
/mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/core/interpreter.h:58:10: fatal error: /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/stderr_reporter.h: Input/output error
   58 | #include "tensorflow/lite/stderr_reporter.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/third_party/eigen3/Eigen/Core:1,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/kernels/internal/reference/reference_ops.h:29,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/kernels/logical.cc:19:
/mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/eigen/Eigen/Core:287:10: fatal error: /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/eigen/Eigen/src/Core/ArithmeticSequence.h: Input/output error
compilation terminated.
In file included from /usr/lib/gcc/x86_64-linux-gnu/9/include/xmmintrin.h:34,
                 from /usr/lib/gcc/x86_64-linux-gnu/9/include/emmintrin.h:31,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/eigen/Eigen/src/Core/util/ConfigureVectorization.h:337,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/eigen/Eigen/Core:22,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/third_party/eigen3/Eigen/Core:1,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/kernels/internal/optimized/optimized_ops.h:41,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/tensorflow_src/tensorflow/lite/kernels/sub.cc:29:
/usr/lib/gcc/x86_64-linux-gnu/9/include/mm_malloc.h:27:10: fatal error: /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/FP16-source/include/stdlib.h: Cannot allocate memory
   27 | #include <stdlib.h>
      |          ^~~~~~~~~~
```



I am using the master branch, that's the default after git cloning. I'll try the nightly branch now. I got the same issue in the nightly branch, cannot allocate memory but it continues compilation. This is after I introduced a memory limit in my .wslconfig file to 1GB: 

```
[ 30%] Built target absl_base
In file included from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/xnnpack/src/xnnpack/assembler.h:8,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/xnnpack/src/xnnpack/aarch32-assembler.h:12,
                 from /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/xnnpack/src/jit/aarch32-assembler.cc:9:
/mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/xnnpack/src/xnnpack/memory.h:12:10: fatal error: /mnt/c/Users/Lucas/Documents/WWP/ML/edge_inference/build/xnnpack/include/xnnpack/common.h: Cannot allocate memory
   12 | #include <xnnpack/common.h>
      |          ^~~~~~~~~~~~~~~~~~
compilation terminated.
[ 30%] Built target mutex
```

I tried it in my docker container

```
33.21 [ 92%] Building CXX object tensorflow-lite/CMakeFiles/tensorflow-lite.dir/kernels/internal/utils/sparsity_format_converter.cc.o
33.21 [ 92%] Building CXX object tensorflow-lite/CMakeFiles/tensorflow-lite.dir/schema/schema_utils.cc.o
124.2 tensorflow-lite/CMakeFiles/tensorflow-lite.dir/build.make:2007: recipe for target 'tensorflow-lite/CMakeFiles/tensorflow-lite.dir/kernels/numeric_verify.cc.o' failed      
275.8 c++: internal compiler error: Killed (program cc1plus)
275.8 Please submit a full bug report,
275.8 with preprocessed source if appropriate.
275.8 See <file:///usr/share/doc/gcc-7/README.Bugs> for instructions.
275.8 make[2]: *** [tensorflow-lite/CMakeFiles/tensorflow-lite.dir/kernels/numeric_verify.cc.o] Error 4
302.1 make[2]: *** Waiting for unfinished jobs....
302.1 c++: internal compiler error: Killed (program cc1plus)
302.1 Please submit a full bug report,
302.1 with preprocessed source if appropriate.
302.1 See <file:///usr/share/doc/gcc-7/README.Bugs> for instructions.
```

I am not sure how to access that file for instructions, is it in the docker container I think. One thing to note is that the minimal.cc code is not complete but I still expect it to compile or at least compile until getting to the incomplete `Interpreter->Invoke()` line and then fail with the appropriate error. Hi @Luca-Stefanescu, it looks like you cloned everything under /mnt/c which is technically in the in the Windows FS. It's possible the code can't allocate memory because it's coming from Windows in a sense, can you redo everything completely within the WSL FS? i.e. follow the directions from the home directory let's say or make a git folder that you can do everything in as long as it's not a subtree of /mnt/c.

Let me know how that goes.

Also, can you test with this limit removed? 

>This is after I introduced a memory limit in my .wslconfig file to 1GB: > Hi @Luca-Stefanescu, it looks like you cloned everything under /mnt/c which is technically in the in the Windows FS. It's possible the code can't allocate memory because it's coming from Windows in a sense, can you redo everything completely within the WSL FS? i.e. follow the directions from the home directory let's say or make a git folder that you can do everything in as long as it's not a subtree of /mnt/c.
> 
> Let me know how that goes.
> 
> Also, can you test with this limit removed?
> 
> > This is after I introduced a memory limit in my .wslconfig file to 1GB:

Yep I can try but I have tried it in the official tensorflow docker image and in another image that my company uses and I get the same issues. Although sometimes it's input/output error rather than failed to allocate memory. And it's always issues with many various include statements in the tensorflow source code.
 I tried it in the wsl file system with the memory limit removed:
```
[ 92%] Building CXX object tensorflow-lite/CMakeFiles/tensorflow-lite.dir/schema/schema_utils.cc.o
c++: fatal error: Killed signal terminated program cc1plus
compilation terminated.
make[2]: *** [tensorflow-lite/CMakeFiles/tensorflow-lite.dir/build.make:2960: tensorflow-lite/CMakeFiles/tensorflow-lite.dir/optional_debug_tools.cc.o] Error 1
make[2]: *** Waiting for unfinished jobs....
``` Hi @Luca-Stefanescu, I am unable to replicate locally, my cmake version is 3.25.1 but I doubt that's the different factor. I am not using WSL, so it's likely an interaction with your OS and WSL. How much free memory do you have on your hard drive? Is your OS installed on your hard drive or do you have multiple hard drives / partitions? What does that distribution look like? Which hard drive (if multiple) does WSL use? Is there some WSL restriction imposed by your OS? Can you check what configurations your WSL is under? How much RAM do you have? Can you check Task manager while the build is running and see how much Memory WSL is using? Does it reach your RAM limit? Does it ever reach your pagefile (i.e. is the program swapping out memory from RAM to disk)?

Apologies for the question storm but the more information we have the better chance I have of helping you more immediately. > Hi @Luca-Stefanescu, I am unable to replicate locally, my cmake version is 3.25.1 but I doubt that's the different factor. I am not using WSL, so it's likely an interaction with your OS and WSL. How much free memory do you have on your hard drive? Is your OS installed on your hard drive or do you have multiple hard drives / partitions? What does that distribution look like? Which hard drive (if multiple) does WSL use? Is there some WSL restriction imposed by your OS? Can you check what configurations your WSL is under? How much RAM do you have? Can you check Task manager while the build is running and see how much Memory WSL is using? Does it reach your RAM limit? Does it ever reach your pagefile (i.e. is the program swapping out memory from RAM to disk)?
> 
> Apologies for the question storm but the more information we have the better chance I have of helping you more immediately.

Hard drive has hundreds of GB free. I have standard C and D partitions (D is a different hard drive that I bought) but OS and WSL are both on C. Not sure about WSL restrictions, I'll do some research. I am not sure how to check what configurations wsl is under, can you be more specific? I have 16GB RAM, looking at task manager, WSL will typically use as much RAM as I allow it to. I use a .wslconfig file to set this limit but other than that it is empty. WSL can easily reach the ram limit if I allow it to, I did try with the limit set to 16GB and it will exhaust all memory.

Just had another go now, I set the RAM limit to 16GB and tried `cmake --build . -j` and looking at the performance monitor it does show ~10% of the page file being used. I will have a look for WSL restrictions and configurations and get back to you. 

I am in wsl file system rather than /mnt/c and somehow it has just finished compiling. Last time I tried it failed so I am not sure what I am doing differently... I did remove the RAM limit and see that the page file was being used. It is still at more than 5% now after compilation is finished, but task manager shows 50% of RAM being used and vmmemWSL is using 1.7GB. I will try it on my company docker image and see if we can move forward with the project, thanks for assistance. Np, @Luca-Stefanescu sounds like Schrödinger's memory allocation failure :). If I had to guess it sounds like it is reaching capacity in RAM and starts to try to use the pagefile but Windows doesn't like that and limits its RAM usage sometimes in which case the allocation request gets denied. I'm guessing more RAM would help but 16GB should be enough. I'm wondering if there is a way to not restrict WSL RAM/pagefile Usage. I'm thinking reducing simultaneous programs that are using other RAM or unleashing WSL is probably your best way to consistently compile. This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61485">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61485">No</a>
 Any update with this issue? I'm having the same problem.

I am building Tensorflow Lite with cmake following the instruction given on the minimal example. I am building in a docker container with ubuntu using WSL 2 with docker desktop. The build seems to work until 91%. Then it will start to allocate all the memory (16gb of ram + 8gb of swap) until it fails to allocate throwing an allocation error or sometimes an input/output error.
I think that this error message could be helpful:
```
In file included from /workspaces/tfl-dev-env/tie/../tensorflow_src/tensorflow/lite/kernels/internal/runtime_shape.h:22,
                 from /workspaces/tfl-dev-env/tie/../tensorflow_src/tensorflow/lite/kernels/internal/types.h:24,
                 from /workspaces/tfl-dev-env/tie/../tensorflow_src/tensorflow/lite/kernels/internal/tensor_ctypes.h:22,
                 from /workspaces/tfl-dev-env/tensorflow_src/tensorflow/lite/kernels/embedding_lookup_sparse.cc:72:
/usr/include/c++/13/memory:81:12: fatal error: /workspaces/tfl-dev-env/tie/../tensorflow_src/bits/shared_ptr_atomic.h: Cannot allocate memory
   81 | #  include <bits/shared_ptr_atomic.h>
      |            ^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
```

However trying to follow the same steps on a ubuntu VM using VMWare (and with less memory) seems to work. > Any update with this issue? I'm having the same problem.
> 
> I am building Tensorflow Lite with cmake following the instruction given on the minimal example. I am building in a docker container with ubuntu using WSL 2 with docker desktop. The build seems to work until 91%. Then it will start to allocate all the memory (16gb of ram + 8gb of swap) until it fails to allocate throwing an allocation error or sometimes an input/output error. I think that this error message could be helpful:
> 
> ```
> In file included from /workspaces/tfl-dev-env/tie/../tensorflow_src/tensorflow/lite/kernels/internal/runtime_shape.h:22,
>                  from /workspaces/tfl-dev-env/tie/../tensorflow_src/tensorflow/lite/kernels/internal/types.h:24,
>                  from /workspaces/tfl-dev-env/tie/../tensorflow_src/tensorflow/lite/kernels/internal/tensor_ctypes.h:22,
>                  from /workspaces/tfl-dev-env/tensorflow_src/tensorflow/lite/kernels/embedding_lookup_sparse.cc:72:
> /usr/include/c++/13/memory:81:12: fatal error: /workspaces/tfl-dev-env/tie/../tensorflow_src/bits/shared_ptr_atomic.h: Cannot allocate memory
>    81 | #  include <bits/shared_ptr_atomic.h>
>       |            ^~~~~~~~~~~~~~~~~~~~~~~~~~
> compilation terminated.
> ```
> 
> However trying to follow the same steps on a ubuntu VM using VMWare (and with less memory) seems to work.

there was some way to disable memory usage limits which allowed it to work by using more memory in the pagefile (SWAP momey) but utlimately i just found a workaround using the keras2c github repo which converts tensorflow model into a C file. > there was some way to disable memory usage limits which allowed it to work by using more memory in the pagefile (SWAP momey)

So I just need to give WSL more memory? I thought 16+8gb would be enough.

> but utlimately i just found a workaround using the keras2c github repo which converts tensorflow model into a C file.

Can you give me a link or more infos? I am building a tensorflow lite C++ app with cmake, I don't know if this could be a suitable solution.

Can you re-open the issue so that maybe @pjpratik or @pkgoogle can help me finding a solution?

Thanks for the reply. > > there was some way to disable memory usage limits which allowed it to work by using more memory in the pagefile (SWAP momey)
> 
> So I just need to give WSL more memory? I thought 16+8gb would be enough.
> 
> > but utlimately i just found a workaround using the keras2c github repo which converts tensorflow model into a C file.
> 
> Can you give me a link or more infos? I am building a tensorflow lite C++ app with cmake, I don't know if this could be a suitable solution.
> 
> Can you re-open the issue so that maybe @pjpratik or @pkgoogle can help me finding a solution?
> 
> Thanks for the reply.

https://github.com/f0uriest/keras2c this is the github repo. I would reopen this issue but I don't know how. Ok, thanks. Maybe it cannot be re-opened. If they don't reply I'll open a new issue. Thanks for your help btw
### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

Yes

### Source

source

### TensorFlow version

cloned the master branch

### Custom code

Yes

### OS platform and distribution

windows 11 wsl 2 with Ubuntu 20.04.6 LTS

### Mobile device

_No response_

### Python version

_No response_

### Bazel version

6.3.1

### GCC/compiler version

9.4.0

### CUDA/cuDNN version

Cuda compilation tools, release 10.1, V10.1.243

### GPU model and memory

rtx 2060 6GB dedicated 

### Current behavior?

I am using cmake 3.22.2
I cloned tensorflow into a directory tensorflow_src, then I ran ./configure and set ROCm and CUDA support to none, because I am building tflite. Then I made and moved into a build directory. I run `cmake ../tensorflow_src/tensorflow/lite` and `cmake --build . -j` I get quite a lot of errors, the first is "failed to allocate memory". But there's not enough space to paste the entire log here. 

I also tried `cmake ../tensorflow_src/tensorflow/lite/examples/minimal` and `cmake --build . -j` and the error I get is that 

I am trying to instantiate the tflite interpreter object in the easiest possible way.

### Standalone code to reproduce the issue

I also tried bazel build in the tensorflow_sec directory, which seems like it executed fine. then I used `g++ -std=c++17 inference.cpp model.cc -Ltensorflow_src/bazel-bin/tensorflow/lite -ltensorflowlite -o inference -Itensorflow_src/tensorflow/lite`

```shell
In file included from inference.cpp:6:
tensorflow_src/tensorflow/lite/interpreter.h:21:10: fatal error: tensorflow/lite/core/interpreter.h: No such file or directory
   21 | #include "tensorflow/lite/core/interpreter.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
```

I am following exactly the instructions on tensorflolw website to set up tflite for C++, it seems like there is missing information

### Relevant log output

_No response_