jealk commited on
Commit
58f708b
·
verified ·
1 Parent(s): 9d4e345

Added model card

Browse files
Files changed (1) hide show
  1. README.md +119 -0
README.md ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: peft
3
+ license: mit
4
+ language:
5
+ - da
6
+ - sv
7
+ base_model:
8
+ - AI-Sweden-Models/Llama-3-8B-instruct
9
+ pipeline_tag: sentence-similarity
10
+ tags:
11
+ - text-embedding
12
+ - embeddings
13
+ - information-retrieval
14
+ - beir
15
+ - text-classification
16
+ - language-model
17
+ - text-clustering
18
+ - text-semantic-similarity
19
+ - text-evaluation
20
+ - text-reranking
21
+ - feature-extraction
22
+ - sentence-similarity
23
+ - Sentence Similarity
24
+ datasets:
25
+ - jealk/scandi-wiki-combined
26
+ - jealk/wiki40b-da-clean
27
+ - jealk/supervised-da
28
+ ---
29
+ ## The Tech Collective - Supervised Embedding model (Danish)
30
+
31
+ ### Model Description
32
+
33
+ Supervised model for sentence embeddings.
34
+
35
+ - **Developed by:** Jesper Alkestrup, The Tech Collective
36
+ - **Model type:** Embedding model
37
+ - **Language(s) (NLP):** Danish
38
+ - **Finetuned from model :** AI-Sweden-Models/Llama-3-8B-instruct
39
+ - **Finetuning procedure:** LLM2Vec
40
+
41
+
42
+ Trained by using the approach outlined in the paper **LLM2Vec: Large Language Models Are Secretly Powerful Text Encoders**.
43
+
44
+ LoRa Finetuning 1000 steps of MNTP on cleaned Danish Wikipedia https://huggingface.co/datasets/jealk/wiki40b-da-clean
45
+ LoRa Finetuning ~1000 steps of Supervised Contrastive learniing on this dataset: https://huggingface.co/datasets/jealk/supervised-da
46
+
47
+ Credits for code-repo used to finetune this model https://github.com/McGill-NLP/llm2vec
48
+
49
+ Requires the llm2vec package to encode sentences. Credits to https://huggingface.co/McGill-NLP/LLM2Vec-Meta-Llama-3-8B-Instruct-mntp-supervised for the below instructions:
50
+
51
+ ## Installation
52
+ ```bash
53
+ pip install llm2vec
54
+ ```
55
+
56
+ ## Usage
57
+ ```python
58
+ from llm2vec import LLM2Vec
59
+
60
+ import torch
61
+ from transformers import AutoTokenizer, AutoModel, AutoConfig
62
+ from peft import PeftModel
63
+
64
+ # Loading base Mistral model, along with custom code that enables bidirectional connections in decoder-only LLMs. MNTP LoRA weights are merged into the base model.
65
+ tokenizer = AutoTokenizer.from_pretrained(
66
+ "jealk/llm2vec-da-mntp"
67
+ )
68
+ config = AutoConfig.from_pretrained(
69
+ "jealk/llm2vec-da-mntp", trust_remote_code=True
70
+ )
71
+ model = AutoModel.from_pretrained(
72
+ "jealk/llm2vec-da-mntp",
73
+ trust_remote_code=True,
74
+ config=config,
75
+ torch_dtype=torch.bfloat16,
76
+ device_map="cuda" if torch.cuda.is_available() else "cpu",
77
+ )
78
+ model = PeftModel.from_pretrained(
79
+ model,
80
+ "jealk/llm2vec-da-mntp",
81
+ )
82
+ model = model.merge_and_unload() # This can take several minutes on cpu
83
+
84
+ # Loading supervised model. This loads the trained LoRA weights on top of MNTP model. Hence the final weights are -- Base model + MNTP (LoRA) + supervised (LoRA).
85
+ model = PeftModel.from_pretrained(
86
+ model, "jealk/TTC-supervised-1"
87
+ )
88
+
89
+ # Wrapper for encoding and pooling operations
90
+ l2v = LLM2Vec(model, tokenizer, pooling_mode="mean", max_length=8124)
91
+
92
+ # Encoding queries using instructions
93
+ instruction = (
94
+ "Given a web search query, retrieve relevant passages that answer the query:"
95
+ )
96
+ queries = [
97
+ [instruction, "how much protein should a female eat"],
98
+ [instruction, "summit define"],
99
+ ]
100
+ q_reps = l2v.encode(queries)
101
+
102
+ # Encoding documents. Instruction are not required for documents
103
+ documents = [
104
+ "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.",
105
+ "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.",
106
+ ]
107
+ d_reps = l2v.encode(documents)
108
+
109
+ # Compute cosine similarity
110
+ q_reps_norm = torch.nn.functional.normalize(q_reps, p=2, dim=1)
111
+ d_reps_norm = torch.nn.functional.normalize(d_reps, p=2, dim=1)
112
+ cos_sim = torch.mm(q_reps_norm, d_reps_norm.transpose(0, 1))
113
+
114
+ print(cos_sim)
115
+ """
116
+ tensor([[0.6470, 0.1619],
117
+ [0.0786, 0.5844]])
118
+ """
119
+ ```