vaibhavad commited on
Commit
1a99574
1 Parent(s): c4b4f97

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +29 -12
README.md CHANGED
@@ -45,30 +45,47 @@ from transformers import AutoTokenizer, AutoModel, AutoConfig
45
  from peft import PeftModel
46
 
47
  # 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.
48
- .tokenizer = AutoTokenizer.from_pretrained("McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp")
49
- config = AutoConfig.from_pretrained("McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp", trust_remote_code=True)
50
- model = AutoModel.from_pretrained("McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp", trust_remote_code=True, config=config, torch_dtype=torch.bfloat16)
51
- model = PeftModel.from_pretrained(model, "McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp")
52
- model = model.merge_and_unload() # This can take several minutes
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
  # 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).
55
- model = PeftModel.from_pretrained(model, "McGill-NLP/LLM2Vec-Mistral-7B-Instruct-v2-mntp-supervised")
 
 
56
 
57
  # Wrapper for encoding and pooling operations
58
  l2v = LLM2Vec(model, tokenizer, pooling_mode="mean", max_length=512)
59
 
60
  # Encoding queries using instructions
61
- instruction = 'Given a web search query, retrieve relevant passages that answer the query:'
 
 
62
  queries = [
63
- [instruction, 'how much protein should a female eat'],
64
- [instruction, 'summit define']
65
  ]
66
  q_reps = l2v.encode(queries)
67
 
68
  # Encoding documents. Instruction are not required for documents
69
  documents = [
70
  "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.",
71
- "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."
72
  ]
73
  d_reps = l2v.encode(documents)
74
 
@@ -79,8 +96,8 @@ cos_sim = torch.mm(q_reps_norm, d_reps_norm.transpose(0, 1))
79
 
80
  print(cos_sim)
81
  """
82
- tensor([[0.5486, 0.0554],
83
- [0.0567, 0.5437]])
84
  """
85
  ```
86
 
 
45
  from peft import PeftModel
46
 
47
  # 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.
48
+ tokenizer = AutoTokenizer.from_pretrained(
49
+ "McGill-NLP/LLM2Vec-Llama-2-7b-chat-hf-mntp"
50
+ )
51
+ config = AutoConfig.from_pretrained(
52
+ "McGill-NLP/LLM2Vec-Llama-2-7b-chat-hf-mntp", trust_remote_code=True
53
+ )
54
+ model = AutoModel.from_pretrained(
55
+ "McGill-NLP/LLM2Vec-Llama-2-7b-chat-hf-mntp",
56
+ trust_remote_code=True,
57
+ config=config,
58
+ torch_dtype=torch.bfloat16,
59
+ device_map="cuda" if torch.cuda.is_available() else "cpu",
60
+ )
61
+ model = PeftModel.from_pretrained(
62
+ model,
63
+ "McGill-NLP/LLM2Vec-Llama-2-7b-chat-hf-mntp",
64
+ )
65
+ model = model.merge_and_unload() # This can take several minutes on cpu
66
 
67
  # 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).
68
+ model = PeftModel.from_pretrained(
69
+ model, "McGill-NLP/LLM2Vec-Llama-2-7b-chat-hf-mntp-supervised"
70
+ )
71
 
72
  # Wrapper for encoding and pooling operations
73
  l2v = LLM2Vec(model, tokenizer, pooling_mode="mean", max_length=512)
74
 
75
  # Encoding queries using instructions
76
+ instruction = (
77
+ "Given a web search query, retrieve relevant passages that answer the query:"
78
+ )
79
  queries = [
80
+ [instruction, "how much protein should a female eat"],
81
+ [instruction, "summit define"],
82
  ]
83
  q_reps = l2v.encode(queries)
84
 
85
  # Encoding documents. Instruction are not required for documents
86
  documents = [
87
  "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.",
88
+ "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.",
89
  ]
90
  d_reps = l2v.encode(documents)
91
 
 
96
 
97
  print(cos_sim)
98
  """
99
+ tensor([[0.5417, 0.0780],
100
+ [0.0627, 0.5726]])
101
  """
102
  ```
103