Skip to content

特征检索可换用faiss-gpu以大幅提升速度 #18

@cpuopt

Description

@cpuopt

conda-forge有针对win编译好的faiss-gpu库,可以直接用conda安装,应该是要提前卸载掉faiss-cpu和python环境里的faiss相关库,再用conda装出来就可以。
我实测了faiss-gpu 1.7.4版本正常使用(1.8 1.9也许可以,刚开始报错以为是版本问题就换回以前用过的老版本了)。
只改动了两个地方(大概)
一是加载的地方

if os.path.exists(cluster_model_path):
if self.feature_retrieval:
with open(cluster_model_path, "rb") as f:
self.cluster_model = pickle.load(f)
self.big_npy = None
self.now_spk_id = -1

class Svc加个属性self.gpu_res = faiss.StandardGpuResources()

self.cluster_model = {}
for spk_id, index_cpu in cluster_model_cpu.items():
    index_gpu = faiss.index_cpu_to_gpu(self.gpu_res, 0, index_cpu)
    self.cluster_model[spk_id] = index_gpu

然后是重建这块,gpu上不支持重建,还得转回cpu重建

if self.big_npy is None or self.now_spk_id != speaker_id:
self.big_npy = feature_index.reconstruct_n(0, feature_index.ntotal)
self.now_spk_id = speaker_id

if self.big_npy is None or self.now_spk_id != speaker_id:
    cpu_index = faiss.index_gpu_to_cpu(feature_index)
    dim = cpu_index.d
    ntotal = cpu_index.ntotal
    self.big_npy = np.zeros((ntotal, dim), dtype="float32")
    chunk_size = 10000  # 可根据显存调整
    cpu_index.make_direct_map()
    for i in range(0, ntotal, chunk_size):
        end = min(i + chunk_size, ntotal)
        cpu_index.reconstruct_batch(np.arange(i, end), self.big_npy[i:end])
    feature_index = faiss.index_cpu_to_gpu(self.gpu_res, 0, cpu_index)
    del cpu_index                     
    self.now_spk_id = speaker_id

这块分块应该是没必要,可以去掉。

然后好像还有个缺失mkl_intel_thread.2.dll的报错,因为没法改整合包.conda环境的路径,我是覆盖着改环境的,完整conda环境装依赖的时候是有这个dll的,直接复制补上也行。

以供参考,希望能让特征检索不再是个残废功能。😂

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions