Faiss: std::bad_alloc

Created on 29 Mar 2019  ·  3Comments  ·  Source: facebookresearch/faiss

Summary

Platform

OS: Ubuntu Server 16.04

Faiss version: GPU version

Faiss compilation options: conda install faiss-gpu -c pytorch

Running on:

  • [ ] GPU

Interface:

  • [ ] Python

minimal code to reproduce the issue:

import faiss
import numpy as np

d = 2048
index = faiss.IndexFlatIP(d)

img_feature_list is a numpy array with dimension of 30000 * 2048

img_feature_list = np.load("feature.npy")

for i in range(40): index.add(img_feature_list)

Reproduction instructions


RAM size is 32G

Run this code, It throws the following error:
“terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)”

But the memory hasn't ran out. The memory is only occupied about 67%.
what to do with this ?

enhancement question

Most helpful comment

Sorry, I missed the "minimal code" section.
Memory allocation is done with the std::vector insert function
https://github.com/facebookresearch/faiss/blob/master/IndexFlat.cpp#L32
the memory reallocation policy of std::vector is to increment by powers of 2. So if you already use 67% RAM, it will go to 2*67% > 100% RAM.
There is no clear workaround here unfortunately. Giving up the geometric reallocation would have an impact on performance.
One thing we should look at is catching bad_alloc and translating it into a Python exception.

All 3 comments

@linghuang please provide a proper bug report
https://github.com/facebookresearch/faiss/wiki/Troubleshooting#reporting-bugs

Sorry, I missed the "minimal code" section.
Memory allocation is done with the std::vector insert function
https://github.com/facebookresearch/faiss/blob/master/IndexFlat.cpp#L32
the memory reallocation policy of std::vector is to increment by powers of 2. So if you already use 67% RAM, it will go to 2*67% > 100% RAM.
There is no clear workaround here unfortunately. Giving up the geometric reallocation would have an impact on performance.
One thing we should look at is catching bad_alloc and translating it into a Python exception.

Implemented in 1.5.3

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ljferrer picture Ljferrer  ·  3Comments

hashyong picture hashyong  ·  3Comments

0DF0Arc picture 0DF0Arc  ·  3Comments

maozezhong picture maozezhong  ·  3Comments

daniellevy picture daniellevy  ·  3Comments