介绍

这个项目主要是为了解决在大语言模型中,张量计算缓慢的问题。

通过资料查阅,有过很多相似的原生方法,比如faiss,这个相对受欢迎,所以以这个为例,我们知道在能够达到可以计算相似之前会将文本向量化,这部分计算在faiss计算较为快速,但是对于大模型的embed来说就尤为吃力了,如何高效的解决这个问题。

我起初的想法是直捣黄龙,但是我真的可以比faiss做的更好吗?

faiss github

方案

我们将专门针对embed方面进行分布式部署,在我们提供文本后,进行计算并且利用非结构化存储,进行存储。

这个方案看上去像这样(简化版)

mindmap

面临问题

非对称计算问题

因为在embed工作的时候,会对数据进行整体加权,所以导致了文本相差一个字也可以导致结果相去甚远的情况,那么我这里引入一个相似度聚类的方案。
在embed表层其实提供了足够的土壤去提供多垂直的比对的可能性,意思就是我们有多个相似度相近的句子,我们可以将这一部分归为一个类。

聚类计算

在直接从缓存获取数据时就可以快速得知结果差值,而避免因为细小改动重新通过向量化,通过统计,这将为10000 tokens(假设5簇n类)那么耗时将会是 (10000/n)/(n/5)

聚类计算

如上的结构可以实现,局部计算而非全局计算,热数据优先加载。

这是我所知的理论,这个想法是从facebook的bart模型的embed结构中得到启发。

数据末端优化-1

数据末端优化-2

bart模型

作为我认为最有可能超越bert的模型,bart在处理注意力的算法上,使用了embed交织的方式,形象描述就是embeddings都会根据训练而回归。

谁规定的embeddings必须是一维的?而且必须是一个方向的?

当然这么做也有问题,这种架构直接导致了模型无法微调,或者微调计算量巨大。(相当于全量计算,那还不如重新跑)

分布式通讯

我在看nvcc的文档,发现他们有提供一个分布式通讯的库,叫做nccl,这个库是专门为gpu设计的,代码并未公开,但是在传值上我觉得很像是用的udp沟通后进行p2p的传输。

。。。这不就是rtc吗?

把音频视频的传输方式搬到数据传输上,这确实是一个不错的想法。

所以我写了一个简单的沟通方法,这个方法用在了 https://weights.chat/ 如果可以各位姥爷可以看看这个产品,这个轮子我会公开的,但是不是现在,我希望它会成为gpu分布式的新的高效的解决方法。

实验日志

初始数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
2024-12-06 02:03:28,439 - test_vectorstore_performance - INFO - 开始测试添加10000个文档的性能...
Batches: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 313/313 [01:19<00:00, 3.96it/s]
2024-12-06 02:04:48,419 - test_vectorstore_performance - INFO - 添加文档总耗时: 79.98秒
2024-12-06 02:04:48,419 - test_vectorstore_performance - INFO - 每秒处理文档数: 125.03
2024-12-06 02:04:48,419 - test_vectorstore_performance - INFO - 成功添加文档数: 56
...
2024-12-06 02:06:11,770 - test_vectorstore_performance - INFO - 平均过滤搜索耗时: 0.0271秒
2024-12-06 02:06:11,770 - test_vectorstore_performance - INFO - 每秒可执行过滤搜索数: 36.95
...
2024-12-06 02:07:30,089 - test_vectorstore_performance - INFO - 重建索引总耗时: 1.61秒
2024-12-06 02:07:30,089 - test_vectorstore_performance - INFO - 每秒处理文档数: 6197.39
...
2024-12-06 02:08:44,278 - test_vectorstore_performance - INFO - 平均搜索耗时: 0.0238秒
2024-12-06 02:08:44,278 - test_vectorstore_performance - INFO - 每秒可执行搜索数: 41.95

聚类数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
2024-12-06 07:37:02,251 - test_clustering - INFO -  测试报告 
2024-12-06 07:37:02,251 - test_clustering - INFO - ==================================================
2024-12-06 07:37:02,251 - test_clustering - INFO -
1. 聚类统计
2024-12-06 07:37:02,251 - test_clustering - INFO - ----------------------------------------
2024-12-06 07:37:02,251 - test_clustering - INFO - 总文档数: 193
2024-12-06 07:37:02,251 - test_clustering - INFO - 聚类数量: 9
2024-12-06 07:37:02,252 - test_clustering - INFO - 离群点数量: 67
2024-12-06 07:37:02,252 - test_clustering - INFO - 平均聚类大小: 21.44
2024-12-06 07:37:02,252 - test_clustering - INFO - 最大聚类大小: 67
2024-12-06 07:37:02,252 - test_clustering - INFO - 最小聚类大小: 3
2024-12-06 07:37:02,255 - test_clustering - INFO -
2. 搜索性能分析
2024-12-06 07:37:02,255 - test_clustering - INFO - ----------------------------------------
2024-12-06 07:37:02,255 - test_clustering - INFO - 总查询数: 9
2024-12-06 07:37:02,255 - test_clustering - INFO - 平均结果重叠度: 96.30%
2024-12-06 07:37:02,255 - test_clustering - INFO -
结果重叠度分布:
2024-12-06 07:37:02,255 - test_clustering - INFO - - 高重叠(>=80%): 8 次
2024-12-06 07:37:02,256 - test_clustering - INFO - - 中重叠(50-80%): 1 次
2024-12-06 07:37:02,256 - test_clustering - INFO - - 低重叠(<50%): 0 次
2024-12-06 07:37:02,256 - test_clustering - INFO -
5. 查询覆盖分析
2024-12-06 07:37:02,256 - test_clustering - INFO - ----------------------------------------
2024-12-06 07:37:02,256 - test_clustering - INFO -
基础信息查询:
2024-12-06 07:37:02,256 - test_clustering - INFO - - 平均结果数: 3.00
2024-12-06 07:37:02,256 - test_clustering - INFO - - 命中率: 100.00%
2024-12-06 07:37:02,256 - test_clustering - INFO - - 相关性得分: 1.00
2024-12-06 07:37:02,256 - test_clustering - INFO -
具体信息查询:
2024-12-06 07:37:02,256 - test_clustering - INFO - - 平均结果数: 3.00
2024-12-06 07:37:02,256 - test_clustering - INFO - - 命中率: 100.00%
2024-12-06 07:37:02,256 - test_clustering - INFO - - 相关性得分: 1.00
2024-12-06 07:37:02,256 - test_clustering - INFO -
模糊查询:
2024-12-06 07:37:02,256 - test_clustering - INFO - - 平均结果数: 3.00
2024-12-06 07:37:02,256 - test_clustering - INFO - - 命中率: 100.00%
2024-12-06 07:37:02,256 - test_clustering - INFO - - 相关性得分: 0.89

综合测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
==================== 测试报告 ====================
2024-12-06 08:02:01,785 - test_vector_operations - INFO -
1. 文档统计
2024-12-06 08:02:01,785 - test_vector_operations - INFO - 总文档数: 193
2024-12-06 08:02:01,785 - test_vector_operations - INFO - 成功添加: 143
2024-12-06 08:02:01,785 - test_vector_operations - INFO - 已删除: 5
2024-12-06 08:02:01,785 - test_vector_operations - INFO -
2. 聚类统计
2024-12-06 08:02:01,786 - test_vector_operations - INFO - 聚类总数: 0
2024-12-06 08:02:01,786 - test_vector_operations - INFO - 离群点数: 0
2024-12-06 08:02:01,786 - test_vector_operations - INFO -
3. 搜索性能
2024-12-06 08:02:01,786 - test_vector_operations - INFO - 聚类搜索命中率: 100.00%
2024-12-06 08:02:01,786 - test_vector_operations - INFO - 全局搜索命中率: 100.00%
----------------------------------------------------------------------