tomaszki commited on
Commit
16fc6d2
·
1 Parent(s): a7dfbaf

First commit

Browse files
Files changed (2) hide show
  1. app.py +61 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForCausalLM
4
+ import plotly.express as px
5
+
6
+
7
+ model_name = 'meta-llama/Llama-2-7b-hf'
8
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
9
+
10
+ @st.cache_resource
11
+ def load_model():
12
+ return AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16).to(device)
13
+
14
+ @st.cache_resource
15
+ def load_tokenizer():
16
+ return AutoTokenizer.from_pretrained(model_name)
17
+
18
+ @torch.no_grad()
19
+ @st.cache_data()
20
+ def get_attention_weights_and_tokens(text):
21
+ tokenized = tokenizer(text, return_tensors='pt')
22
+ tokens = [tokenizer.decode(token) for token in tokenized.input_ids[0]]
23
+ tokenized = tokenized.to(device)
24
+ output = model(**tokenized, output_attentions=True)
25
+ return output.attentions, tokens
26
+
27
+ model = load_model()
28
+ tokenizer = load_tokenizer()
29
+
30
+ st.title('Attention visualizer')
31
+ text = st.text_area('Write your text here and see attention weights.')
32
+ layer = st.slider(
33
+ 'Which layer do you want to see?',
34
+ min_value=1,
35
+ max_value=model.config.num_hidden_layers
36
+ ) - 1
37
+
38
+ head = st.select_slider(
39
+ 'Which head do you want to see?',
40
+ options = ['Average'] + list(range(1, model.config.num_attention_heads + 1))
41
+ )
42
+ if text:
43
+ attentions, tokens = get_attention_weights_and_tokens(text)
44
+ if head == 'Average':
45
+ weights = attentions[layer].cpu()[0].mean(dim=0)
46
+ else:
47
+ weights = attentions[layer].cpu()[0][head - 1]
48
+ fig = px.imshow(
49
+ weights,
50
+ )
51
+ fig.update_layout(xaxis={
52
+ 'ticktext': tokens,
53
+ 'tickvals': list(range(len(tokens))),
54
+ }, yaxis={
55
+ 'ticktext': tokens,
56
+ 'tickvals': list(range(len(tokens))),
57
+ },
58
+ height=800,
59
+ )
60
+
61
+ st.plotly_chart(fig)
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ plotly
3
+ streamlit