Views
No views yet
git clone https://huggingface.co/openbmb/MiniCPM-2B-dpo-bf16tokenizer_config.json"add_eos_token": true1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3import numpy as np
4
5class MiniCPMSentenceEmbedding:
6 def __init__(self, model_path='openbmb/MiniCPM-2B-dpo-bf16', adapter_path=None):
7 self.tokenizer = AutoTokenizer.from_pretrained(model_path)
8 self.model = AutoModelForCausalLM.from_pretrained(model_path,
9 torch_dtype=torch.bfloat16,
10 device_map='cuda',
11 trust_remote_code=True)
12 if adapter_path != None:
13 # Load fine-tuned LoRA
14 self.model.load_adapter(adapter_path)
15
16 def get_last_hidden_state(self, text):
17 inputs = self.tokenizer(text, return_tensors="pt").to('cuda')
18 with torch.no_grad():
19 out = self.model(**inputs, output_hidden_states=True).hidden_states[-1][0, -1, :]
20 return out.squeeze().float().cpu().numpy()
21
22 def encode(self, sentences: list[str], **kwargs) -> list[np.ndarray]:
23 """
24 Returns a list of embeddings for the given sentences.
25
26 Args:
27 sentences: List of sentences to encode
28
29 Returns:
30 List of embeddings for the given sentences
31 """
32
33 out = []
34
35 for s in sentences:
36 out.append(self.get_last_hidden_state(s))
37
38 return out
39
40minicpm_sentence_embedding = PhiSentenceEmbedding(<your-cloned-base-model-path>, 'trapoom555/MiniCPM-2B-Text-Embedding-cft')
41
42example_sentences = ["I don't like apples", "I like apples"]
43
44encoded_sentences = minicpm_sentence_embedding.encode(example_sentences)
45
46print(encoded_sentences)
47| Training Details | Value |
|---|---|
| Loss | InfoNCE |
| Batch Size | 60 |
| InfoNCE Temperature | 0.05 |
| Learning Rate | 5e-05 |
| Warmup Steps | 100 |
| Learning Rate Scheduler | CosineAnnealingLR |
| LoRA Rank | 8 |
| LoRA Alpha | 32 |
| LoRA Dropout | 0.1 |
| Training Precision | bf16 |
| Max Epoch | 1 |
| GPU | RTX3090 |
| Num GPUs | 4 |
| Benchmarks | Before cft | After cft |
|---|---|---|
| STS12 | 7.27 | 76.38 |
| STS13 | 18.38 | 87.61 |
| STS14 | 15.04 | 81.55 |
| STS15 | 32.24 | 87.33 |
| STS16 | 39.79 | 85.25 |
| STS17 | 33.63 | 89.96 |
| STSBenchmark | 33.91 | 86.51 |
| BOISSES | 18.03 | 80.05 |
| SICK-R | 49.30 | 79.87 |
| Overall | 27.51 | 83.84 |