Feature Extraction
Transformers
Safetensors
sentence-transformers
Chinese
English
mteb
custom_code
Eval Results (legacy)
Run this model with HF Jobs
Run openbmb/MiniCPM-Embedding on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/openbmb/MiniCPM-Embedding. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/openbmb/MiniCPM-Embedding on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use openbmb/MiniCPM-Embedding with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use openbmb/MiniCPM-Embedding with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="openbmb/MiniCPM-Embedding", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import MiniCPM model = MiniCPM.from_pretrained("openbmb/MiniCPM-Embedding", trust_remote_code=True, device_map="auto") - sentence-transformers
How to use openbmb/MiniCPM-Embedding with sentence-transformers:
from sentence_transformers import SentenceTransformer model = SentenceTransformer("openbmb/MiniCPM-Embedding", trust_remote_code=True) 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
Kaguya-19 commited on
Commit ·
143fca0
1
Parent(s): 09daf17
fit for sentence transformers
Browse filesFiles changed (7)
- 1_Pool/config.jsonAdded+10
- README.md+31-5
- config.json+1-1
- config_sentence_transformers.jsonAdded+9
- configuration.jsonAdded+1
- modeling_minicpm.py+9
- modules.jsonAdded+14
12345678910{ "word_embedding_dimension": 2304, "pooling_mode_cls_token": false, "pooling_mode_mean_tokens": true, "pooling_mode_max_tokens": false, "pooling_mode_mean_sqrt_len_tokens": false, "pooling_mode_weightedmean_tokens": false, "pooling_mode_lasttoken": false, "include_prompt": false }346 unmodified lines3473483493503513525 unmodified lines3583593603613623633643653663675 unmodified lines37337437537637737837911 unmodified lines391392393394395396346 unmodified lines### 示例脚本 Demo```pythonfrom transformers import AutoModel, AutoTokenizer5 unmodified linesmodel = AutoModel.from_pretrained(model_name, trust_remote_code=True, attn_implementation="flash_attention_2", torch_dtype=torch.float16).to("cuda")model.eval()def weighted_mean_pooling(hidden, attention_mask):attention_mask_ = attention_mask * attention_mask.cumsum(dim=1)s = torch.sum(hidden * attention_mask_.unsqueeze(-1).float(), dim=1)d = attention_mask_.sum(dim=1, keepdim=True).float()reps = s / dreturn reps5 unmodified linesattention_mask = batch_dict["attention_mask"]hidden = outputs.last_hidden_statereps = weighted_mean_pooling(hidden, attention_mask)embeddings = F.normalize(reps, p=2, dim=1).detach().cpu().numpy()return embeddings11 unmodified linesprint(scores.tolist()) # [[0.3535913825035095, 0.18596848845481873]]```## 实验结果 Evaluation Results### 中文与英文检索结果 CN/EN Retrieval Results346 unmodified lines3473483493503513523535 unmodified lines3593603613623633643653663673683695 unmodified lines37537637737837938038111 unmodified lines393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422346 unmodified lines### 示例脚本 Demo#### Huggingface Transformers```pythonfrom transformers import AutoModel, AutoTokenizer5 unmodified linesmodel = AutoModel.from_pretrained(model_name, trust_remote_code=True, attn_implementation="flash_attention_2", torch_dtype=torch.float16).to("cuda")model.eval()# 事实上我们用的是weighted mean pooling,但为了部署方便,我们将一部分pooling步骤集成在model.forward中# In fact, we will use weighted mean pooling, but we will integrate some pooling steps into model.forward for deployment conveniencedef mean_pooling(hidden,attention_mask):s = torch.sum(hidden * attention_mask.unsqueeze(-1).float(), dim=1)d = attention_mask.sum(dim=1, keepdim=True).float()reps = s / dreturn reps5 unmodified linesattention_mask = batch_dict["attention_mask"]hidden = outputs.last_hidden_statereps = mean_pooling(hidden, attention_mask)embeddings = F.normalize(reps, p=2, dim=1).detach().cpu().numpy()return embeddings11 unmodified linesprint(scores.tolist()) # [[0.3535913825035095, 0.18596848845481873]]```#### Sentence Transformers```pythonimport torchfrom sentence_transformers import SentenceTransformermodel_name = "openbmb/MiniCPM-Embedding"model = SentenceTransformer(model_name, trust_remote_code=True, model_kwargs={"attn_implementation":"flash_attention_2", "torch_dtype":torch.float16})model.max_seq_length = 512model.tokenizer.padding_side="right"queries = ["中国的首都是哪里?"]passages = ["beijing", "shanghai"]INSTRUCTION = "Query: "embeddings_query = model.encode(queries, prompt=INSTRUCTION, normalize_embeddings=True)embeddings_doc = model.encode(passages, normalize_embeddings=True)scores = (embeddings_query @ embeddings_doc.T)print(scores.tolist()) # [[0.3535913825035095, 0.18596848845481873]]```## 实验结果 Evaluation Results### 中文与英文检索结果 CN/EN Retrieval Results
12345{"_name_or_path": "openbmb/RankCPM-E","architectures": ["MiniCPM"],12345{"_name_or_path": "openbmb/MiniCPM-Embedding","architectures": ["MiniCPM"],
123456789{ "__version__": { "sentence_transformers": "2.7.0", "transformers": "4.37.2", "pytorch": "2.0.1+cu121" }, "prompts": {}, "default_prompt_name": null }1{"task":"sentence-embedding"}1042 unmodified lines10431044104510461047104858 unmodified lines1107110811091110111111121042 unmodified linesif inputs_embeds is None:inputs_embeds = self.embed_tokens(input_ids) * self.config.scale_embif self._use_flash_attention_2:# 2d mask is passed through the layersattention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None58 unmodified linesif output_hidden_states:all_hidden_states += (hidden_states,)next_cache = Noneif use_cache:next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache1042 unmodified lines1043104410451046104710481049105058 unmodified lines11091110111111121113111411151116111711181119112011211042 unmodified linesif inputs_embeds is None:inputs_embeds = self.embed_tokens(input_ids) * self.config.scale_emb_attention_mask = attention_maskif self._use_flash_attention_2:# 2d mask is passed through the layersattention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None58 unmodified linesif output_hidden_states:all_hidden_states += (hidden_states,)# gen weight before mean poolingattention_mask_ = _attention_mask * _attention_mask.cumsum(dim=1)s = hidden_states * attention_mask_.unsqueeze(-1).float()d = attention_mask_.sum(dim=1, keepdim=True).unsqueeze(1).float() /_attention_mask.sum(dim=1, keepdim=True).unsqueeze(1).float()hidden_states = s / dnext_cache = Noneif use_cache:next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache
1234567891011121314[ { "idx": 0, "name": "0", "path": "", "type": "sentence_transformers.models.Transformer" }, { "idx": 1, "name": "1", "path": "1_Pooling", "type": "sentence_transformers.models.Pooling" } ]1_Pool/config.jsonAdded+10
README.md+31-5
config.json+1-1
config_sentence_transformers.jsonAdded+9
configuration.jsonAdded+1
modeling_minicpm.py+9
modules.jsonAdded+14