Taykhoom commited on
Commit
d40dbb4
·
verified ·
1 Parent(s): 5aed0ac

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +35 -1
README.md CHANGED
@@ -11,7 +11,41 @@ tags:
11
  # Note:
12
  This model is copied version of mRNABERT which removes the FlashAttention integration with Trition. This allows the model to be installed off HuggingFace without having to uninstall Triton. Running the below example code yields identical output compared to the original verison.
13
  ```
14
- # TODO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  ```
16
 
17
  # Original README:
 
11
  # Note:
12
  This model is copied version of mRNABERT which removes the FlashAttention integration with Trition. This allows the model to be installed off HuggingFace without having to uninstall Triton. Running the below example code yields identical output compared to the original verison.
13
  ```
14
+ import torch
15
+ from transformers import AutoTokenizer, AutoModel
16
+ from transformers.models.bert.configuration_bert import BertConfig
17
+
18
+ config = BertConfig.from_pretrained("Taykhoom/mRNABERT-no-flashattention")
19
+ tokenizer = AutoTokenizer.from_pretrained("Taykhoom/mRNABERT-no-flashattention")
20
+ model = AutoModel.from_pretrained("Taykhoom/mRNABERT-no-flashattention", trust_remote_code=True, config=config)
21
+
22
+ seq = ["A T C G G A GGG CCC TTT",
23
+ "A T C G",
24
+ "TTT CCC GAC ATG"] #Separate the sequences with spaces.
25
+
26
+ encoding = tokenizer.batch_encode_plus(seq, add_special_tokens=True, padding='longest', return_tensors="pt")
27
+
28
+ input_ids = encoding['input_ids']
29
+ attention_mask = encoding['attention_mask']
30
+
31
+ output = model(input_ids=input_ids, attention_mask=attention_mask)
32
+ last_hidden_state = output[0]
33
+
34
+ attention_mask = attention_mask.unsqueeze(-1).expand_as(last_hidden_state) # Shape : [batch_size, seq_length, hidden_size]
35
+
36
+ # Sum embeddings along the batch dimension
37
+ sum_embeddings = torch.sum(last_hidden_state * attention_mask, dim=1)
38
+
39
+ # Also sum the masks along the batch dimension
40
+ sum_masks = attention_mask.sum(1)
41
+
42
+ # Compute mean embedding.
43
+ mean_embedding = sum_embeddings / sum_masks #Shape:[batch_size, hidden_size]
44
+
45
+ print(torch.mean(mean_embedding, dim=1))
46
+ # Should output: tensor([-0.0209, -0.0156, -0.0201], device='cuda:0', grad_fn=<MeanBackward1>)
47
+ # this is the same as the original version of mRNABERT (checked using original installation instructions)
48
+
49
  ```
50
 
51
  # Original README: