intfloat/e5-mistral-7b-instructを動かす

初めに

日本語の埋め込みモデルでスコアが高い intfloat/e5-mistral-7b-instructを触ってみます

参考(JapaneseEmbeddingEval)

github.com

環境

  • L4 GPU
  • ubuntu22.04

準備

ライブラリの追加をします

!pip install flash_attn -U
!pip install --upgrade transformers torch

実行

まずはサンプルコードでテキストをベクトル化します

import torch
import torch.nn.functional as F

from torch import Tensor
from transformers import AutoTokenizer, AutoModel


def last_token_pool(last_hidden_states: Tensor,
                 attention_mask: Tensor) -> Tensor:
    left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
    if left_padding:
        return last_hidden_states[:, -1]
    else:
        sequence_lengths = attention_mask.sum(dim=1) - 1
        batch_size = last_hidden_states.shape[0]
        return last_hidden_states[torch.arange(batch_size, device=last_hidden_states.device), sequence_lengths]


def get_detailed_instruct(task_description: str, query: str) -> str:
    return f'Instruct: {task_description}\nQuery: {query}'


# Each query must come with a one-sentence instruction that describes the task
task = 'Given a web search query, retrieve relevant passages that answer the query'
queries = [
    get_detailed_instruct(task, 'how much protein should a female eat'),
    get_detailed_instruct(task, 'summit define')
]
# No need to add instruction for retrieval documents
documents = [
    "As a general guideline, the CDC's average requirement of protein for women ages 19 to 70 is 46 grams per day. But, as you can see from this chart, you'll need to increase that if you're expecting or training for a marathon. Check out the chart below to see how much protein you should be eating each day.",
    "Definition of summit for English Language Learners. : 1  the highest point of a mountain : the top of a mountain. : 2  the highest level. : 3  a meeting or series of meetings between the leaders of two or more governments."
]
input_texts = queries + documents

tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-mistral-7b-instruct')
model = AutoModel.from_pretrained('intfloat/e5-mistral-7b-instruct')

max_length = 4096
# Tokenize the input texts
batch_dict = tokenizer(input_texts, max_length=max_length - 1, return_attention_mask=False, padding=False, truncation=True)
# append eos_token_id to every input_ids
batch_dict['input_ids'] = [input_ids + [tokenizer.eos_token_id] for input_ids in batch_dict['input_ids']]
batch_dict = tokenizer.pad(batch_dict, padding=True, return_attention_mask=True, return_tensors='pt')

outputs = model(**batch_dict)
embeddings = last_token_pool(outputs.last_hidden_state, batch_dict['attention_mask'])

# normalize embeddings
embeddings = F.normalize(embeddings, p=2, dim=1)
scores = (embeddings[:2] @ embeddings[2:].T) * 100
print(scores.tolist())

ログで以下が出ます

[[82.91365814208984, 47.9715690612793], [46.95111083984375, 81.74346923828125]]

embeddingsの中を確認したいので、以下を実行します

print(embeddings)

結果は以下です

tensor([[ 0.0178,  0.0037, -0.0075,  ...,  0.0298, -0.0193,  0.0064],
        [-0.0033,  0.0003,  0.0154,  ...,  0.0310, -0.0047,  0.0013],
        [ 0.0172,  0.0028, -0.0087,  ...,  0.0116, -0.0094,  0.0094],
        [-0.0043, -0.0112, -0.0049,  ...,  0.0124,  0.0003,  0.0134]],
       grad_fn=<DivBackward0>)