Instructions to use Musyysysy/bge-m3-dynamic-int8 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use Musyysysy/bge-m3-dynamic-int8 with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("Musyysysy/bge-m3-dynamic-int8") sentences = [ "The weather is lovely today.", "It's so sunny outside!", "He drove to the stadium." ] embeddings = model.encode(sentences) similarities = model.similarity(embeddings, embeddings) print(similarities.shape) # [3, 3] - Notebooks
- Google Colab
- Kaggle
File size: 2,094 Bytes
d9674cb | 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | from pathlib import Path
import torch
from sentence_transformers import SentenceTransformer
BASE_MODEL_NAME = "BAAI/bge-m3"
CHECKPOINT_FILENAME = "bge_m3_quantized_state_dict.pt"
MAX_SEQ_LENGTH = 128
def _quantize_model(model):
try:
from torch.ao.quantization import quantize_dynamic
except ImportError:
from torch.quantization import quantize_dynamic
result = quantize_dynamic(
model,
{torch.nn.Linear},
dtype=torch.qint8,
inplace=True,
)
return result if result is not None else model
def load_model(checkpoint_path=None):
if checkpoint_path is None:
checkpoint_path = (
Path(__file__).resolve().parent
/ CHECKPOINT_FILENAME
)
else:
checkpoint_path = Path(checkpoint_path)
if not checkpoint_path.is_file():
raise FileNotFoundError(
f"Checkpoint не найден: {checkpoint_path}"
)
model = SentenceTransformer(
BASE_MODEL_NAME,
device="cpu",
)
model.max_seq_length = MAX_SEQ_LENGTH
model.eval()
model = _quantize_model(model)
model.max_seq_length = MAX_SEQ_LENGTH
model.eval()
try:
state_dict = torch.load(
checkpoint_path,
map_location="cpu",
weights_only=False,
)
except TypeError:
state_dict = torch.load(
checkpoint_path,
map_location="cpu",
)
model.load_state_dict(
state_dict,
strict=True,
)
model.eval()
return model
if __name__ == "__main__":
model = load_model()
embeddings = model.encode(
["Первый текст", "Второй текст"],
batch_size=2,
show_progress_bar=False,
normalize_embeddings=True,
convert_to_numpy=True,
device="cpu",
)
print("Модель успешно загружена.")
print("Размер эмбеддингов:", embeddings.shape)
|