vaibhavad commited on
Commit
395a6ce
1 Parent(s): be76331

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +27 -14
README.md CHANGED
@@ -44,31 +44,44 @@ import torch
44
  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 +92,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
 
 
44
  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.
48
+ tokenizer = AutoTokenizer.from_pretrained(
49
+ "McGill-NLP/LLM2Vec-Sheared-LLaMA-mntp"
50
+ )
51
+ config = AutoConfig.from_pretrained(
52
+ "McGill-NLP/LLM2Vec-Sheared-LLaMA-mntp", trust_remote_code=True
53
+ )
54
+ model = AutoModel.from_pretrained(
55
+ "McGill-NLP/LLM2Vec-Sheared-LLaMA-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
 
62
+ # Loading MNTP (Masked Next Token Prediction) model.
63
+ model = PeftModel.from_pretrained(
64
+ model,
65
+ "McGill-NLP/LLM2Vec-Sheared-LLaMA-mntp",
66
+ )
67
 
68
  # Wrapper for encoding and pooling operations
69
  l2v = LLM2Vec(model, tokenizer, pooling_mode="mean", max_length=512)
70
 
71
  # Encoding queries using instructions
72
+ instruction = (
73
+ "Given a web search query, retrieve relevant passages that answer the query:"
74
+ )
75
  queries = [
76
+ [instruction, "how much protein should a female eat"],
77
+ [instruction, "summit define"],
78
  ]
79
  q_reps = l2v.encode(queries)
80
 
81
  # Encoding documents. Instruction are not required for documents
82
  documents = [
83
  "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.",
84
+ "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.",
85
  ]
86
  d_reps = l2v.encode(documents)
87
 
 
92
 
93
  print(cos_sim)
94
  """
95
+ tensor([[0.8180, 0.5825],
96
+ [0.1069, 0.1931]])
97
  """
98
  ```
99