说明文档
<h1 align="center">Snowflake Arctic-embed-m-v1.5</h1> <h4 align="center"> <p> <a href=#news>新闻</a> | <a href=#this-model>模型介绍</a> | <a href=#usage>使用方法</a> | <a href="#faq">常见问题</a> | <a href="#contact">联系我们</a> | <a href="#license">许可证</a> | <a href="#acknowledgement">致谢</a> <p> </h4> <img referrerpolicy="no-referrer-when-downgrade" src="https://static.scarf.sh/a.png?x-pxid=8ab1f2d9-8425-4212-9bf3-717f7ac637e4" />
新闻
2024/12/11: 发布 2.0模型技术报告
2024/07/26: 在arXiv上发布预印本 [2407.18887] Embedding And Clustering Your Data Can Improve Contrastive Pretraining。
2024/07/18: 发布 snowflake-arctic-embed-m-v1.5,能够生成高度可压缩的嵌入向量,即使压缩到每个向量仅128字节仍能保持质量。关于该模型的开发详情请查看 Snowflake工程博客发布文章。
2024/05/10: 发布 Arctic Embed技术报告
2024/04/16: 原始发布 snowflake-arctic-embed 系列文本嵌入模型。
模型介绍
该模型是 snowflake-arctic-embed-m 的更新版本,旨在提高嵌入向量的可压缩性。该模型在未压缩时整体性能略有提升,同时通过结合 Matryoshka表示学习(MRL) 和统一标量量化,能够在嵌入向量压缩至128字节时仍保留大部分检索质量。
| 模型名称 | MTEB检索得分 (NDCG @ 10) |
|---|---|
| snowflake-arctic-embed-m-v1.5 | 55.14 |
| snowflake-arctic-embed-m | 54.91 |
与使用MRL训练生成256维嵌入向量的其他几个模型相比,snowflake-arctic-embed-m-v1.5 保留了更高程度的原始模型质量,并在MTEB检索基准上提供更好的检索质量。
| 模型 | 模型参数 | 256维时的MTEB检索得分 (arctic-embed-m-v1.5的占比) |
|---|---|---|
| Snowflake arctic-embed-m-v1.5 | 109M | 54.2 (100%) |
| Google gecko | 1200M | 52.4 (97%) |
| OpenAI text-embedding-3-large | 未公开 | 51.7 (95%) |
| Nomic nomic-embed-text-v1.5 | 138M | 50.8 (94%) |
此外,该模型设计用于与语料无关的标量量化方案配合使用,即使每个向量仅需128字节也能实现出色性能(与768维float32向量存储相比,压缩率达24倍)。
| 模型版本 | 维度 | 标量量化 | 每向量字节数 (相对于基线) | MTEB检索得分 (相对于基线) | 每GB向量数 (相对于基线提升) |
|---|---|---|---|---|---|
| v1 | 768 | 无 (float32) | 3072 (100%) | 54.9 (100%) | 0.33M (1.0x) |
| v1 | 768 | int8 | 768 (25%) | 54.9 (100%) | 1.3M (4x) |
| v1.5 | 768 | int8 | 768 (25%) | 55.1 (100%) | 1.3M (4x) |
| v1.5 | 256 | int8 | 256 (8.3%) | 54.2 (99%) | 3.9M (12x) |
| v1.5 | 256 | int4 | 128 (4.2%) | 53.7 (98%) | 7.8M (24x) |
注意:适用于该模型的良好统一标量量化范围(上述评估中使用的范围)为:4位量化使用-0.18到+0.18,8位量化使用-0.3到+0.3。关于使用整数量化与snowflake-arctic-embed-m-v1.5的详细教程,请参阅我们GitHub上的 示例笔记本。
使用方法
使用Sentence Transformers
您可以使用sentence-transformers包来使用任何snowflake-arctic-embed模型。以下是snowflake-arctic-embed-m-v1.5的示例。
import torch
from sentence_transformers import SentenceTransformer
from torch.nn.functional import normalize
# 模型常量。
MODEL_ID = "Snowflake/snowflake-arctic-embed-m-v1.5"
# 您的查询和文档。
queries = ['what is snowflake?', 'Where can I get the best tacos?']
documents = ['The Data Cloud!', 'Mexico City of Course!']
# 加载模型。
model = SentenceTransformer(MODEL_ID)
# 生成文本嵌入。
query_embeddings = model.encode(queries, prompt_name="query")
document_embeddings = model.encode(documents)
# 通过点积计算分数。
scores = query_embeddings @ document_embeddings.T
# 打印结果。
for query, query_scores in zip(queries, scores):
doc_score_pairs = list(zip(documents, query_scores))
doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)
print(f'Query: "{query}"')
for document, score in doc_score_pairs:
print(f'Score: {score:.4f} | Document: "{document}"')
print()
#### 输出 ####
# Query: "what is snowflake?"
# Score: 0.3521 | Document: "The Data Cloud!"
# Score: 0.2358 | Document: "Mexico City of Course!"
# Query: "Where can I get the best tacos?"
# Score: 0.3884 | Document: "Mexico City of Course!"
# Score: 0.2389 | Document: "The Data Cloud!"
#
#### 变体:截断嵌入 ####
query_embeddings_256 = normalize(torch.from_numpy(query_embeddings)[:, :256])
document_embeddings_256 = normalize(torch.from_numpy(document_embeddings)[:, :256])
scores_256 = query_embeddings_256 @ document_embeddings_256.T
# 打印结果。
for query, query_scores in zip(queries, scores_256):
doc_score_pairs = sorted(zip(documents, query_scores), key=lambda x: x[1], reverse=True)
print(f'Query: "{query}"')
for document, score in doc_score_pairs:
print(f'Score: {score:.4f} | Document: "{document}"')
print()
#### 输出 ####
# Query: "what is snowflake?"
# Score: 0.3852 | Document: "The Data Cloud!"
# Score: 0.2721 | Document: "Mexico City of Course!"
# Query: "Where can I get the best tacos?"
# Score: 0.4337 | Document: "Mexico City of Course!"
# Score: 0.2886 | Document: "The Data Cloud!"
#
使用Huggingface transformers
您也可以使用transformers包来使用snowflake-arctic-embed模型。为了获得最佳检索质量,请记得使用CLS token进行嵌入,并在查询上使用下面的查询前缀。
import torch
from torch.nn.functional import normalize
from transformers import AutoModel, AutoTokenizer
# 模型常量。
MODEL_ID = "Snowflake/snowflake-arctic-embed-m-v1.5"
QUERY_PREFIX = 'Represent this sentence for searching relevant passages: '
# 您的查询和文档。
queries = ['what is snowflake?', 'Where can I get the best tacos?']
documents = ['The Data Cloud!', 'Mexico City of Course!']
# 加载模型和分词器。
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModel.from_pretrained(MODEL_ID, add_pooling_layer=False)
model.eval()
# 添加查询前缀并对查询和文档进行分词。
queries_with_prefix = [f"{QUERY_PREFIX}{q}" for q in queries]
query_tokens = tokenizer(queries_with_prefix, padding=True, truncation=True, return_tensors='pt', max_length=512)
document_tokens = tokenizer(documents, padding=True, truncation=True, return_tensors='pt', max_length=512)
# 使用模型生成文本嵌入。
with torch.inference_mode():
query_embeddings = model(**query_tokens)[0][:, 0]
document_embeddings = model(**document_tokens)[0][:, 0]
# 记得对嵌入进行归一化。
query_embeddings = normalize(query_embeddings)
document_embeddings = normalize(document_embeddings)
# 通过点积计算分数。
scores = query_embeddings @ document_embeddings.T
# 打印结果。
for query, query_scores in zip(queries, scores):
doc_score_pairs = list(zip(documents, query_scores))
doc_score_pairs = sorted(doc_score_pairs, key=lambda x: x[1], reverse=True)
print(f'Query: "{query}"')
for document, score in doc_score_pairs:
print(f'Score: {score:.4f} | Document: "{document}"')
print()
#### 输出 ####
# Query: "what is snowflake?"
# Score: 0.3521 | Document: "The Data Cloud!"
# Score: 0.2358 | Document: "Mexico City of Course!"
# Query: "Where can I get the best tacos?"
# Score: 0.3884 | Document: "Mexico City of Course!"
# Score: 0.2389 | Document: "The Data Cloud!"
#
#### 变体:截断嵌入 ####
query_embeddings_256 = normalize(query_embeddings[:, :256])
document_embeddings_256 = normalize(document_embeddings[:, :256])
scores_256 = query_embeddings_256 @ document_embeddings_256.T
# 打印结果。
for query, query_scores in zip(queries, scores_256):
doc_score_pairs = sorted(zip(documents, query_scores), key=lambda x: x[1], reverse=True)
print(f'Query: "{query}"')
for document, score in doc_score_pairs:
print(f'Score: {score:.4f} | Document: "{document}"')
print()
#### 输出 ####
# Query: "what is snowflake?"
# Score: 0.3852 | Document: "The Data Cloud!"
# Score: 0.2721 | Document: "Mexico City of Course!"
# Query: "Where can I get the best tacos?"
# Score: 0.4337 | Document: "Mexico City of Course!"
# Score: 0.2886 | Document: "The Data Cloud!"
#
使用Transformers.js
如果您还没有安装,可以运行以下命令从 NPM 安装 Transformers.js JavaScript库:
npm i @xenova/transformers
您可以按照以下方式使用该模型计算嵌入:
import { pipeline, dot } from '@xenova/transformers';
// 创建特征提取管道
const extractor = await pipeline('feature-extraction', 'Snowflake/snowflake-arctic-embed-m-v1.5', {
quantized: false, // 注释掉此行以使用量化版本
});
// 生成句子嵌入
const sentences = [
'Represent this sentence for searching relevant passages: Where can I get the best tacos?',
'The Data Cloud!',
'Mexico City of Course!',
]
const output = await extractor(sentences, { normalize: true, pooling: 'cls' });
// 计算相似度分数
const [source_embeddings, ...document_embeddings ] = output.tolist();
const similarities = document_embeddings.map(x => dot(source_embeddings, x));
console.log(similarities); // [0.15664823859882132, 0.24481869975470627]
压缩至128字节
该模型设计用于通过两步压缩方案将嵌入压缩至128字节:
- 截断并重新归一化至256维(参考Matryoshka表示学习,详见 原始论文)。
- 对所有256个值进行4位统一标量量化,范围相同(-0.18到+0.18)。
- 对于8位统一标量量化,考虑到8位量化的细粒度更高,稍微宽一些的范围-0.3到+0.3效果通常更好。
详细示例,请查看我们的 arctic-embed GitHub仓库。
常见问题
暂无
联系我们
如果您对此项目有任何问题或建议,请随时提交issue或pull request。 您也可以发送邮件至Daniel Campos (daniel.campos@snowflake.com)。
许可证
Arctic基于 Apache-2 许可证发布。发布的模型可免费用于商业目的。
致谢
我们要感谢开源社区,是他们提供了伟大的基础构件,让我们得以构建我们的模型。 感谢我们的建模工程师Danmei Xu、Luke Merrick、Gaurav Nuti和Daniel Campos,是他们让这些出色的模型成为可能。 感谢我们的领导Himabindu Pucha、Kelvin So、Vivek Raghunathan和Sridhar Ramaswamy对这项工作的支持。 我们还要感谢开源社区,是他们提供了我们能够构建的伟大模型,使这些发布成为可能。 最后,感谢创建BEIR和MTEB基准的研究人员。 正是由于他们不懈努力定义什么是更好的模型,我们才能改进模型性能。
Snowflake/snowflake-arctic-embed-m-v1.5
作者 Snowflake
创建时间: 2024-07-03 18:46:29+00:00
更新时间: 2025-04-24 17:36:32+00:00
在 Hugging Face 上查看