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
| 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) | |