-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo_run.py
66 lines (51 loc) · 3.07 KB
/
demo_run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import torch
from args import DeepArgs
from utils import set_gpu
from demo_representation_vocb import assert_FFNandproduction_gpt2xl,show_each_layer_vocb,assert_attentionmlp_equal_output,assert_circuits_equal_output,show_vocabulary_circuit
from transformers import HfArgumentParser,AutoTokenizer,GPT2LMHeadModel
hf_parser = HfArgumentParser((DeepArgs,))
args: DeepArgs = hf_parser.parse_args_into_dataclasses()[0]
set_gpu(args.gpu)
if args.task_name=='general_discovery':
if args.model_name=='gpt2xl':
tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
input_text="The Space Needle is in downtown"
inputs = tokenizer(input_text, return_tensors="pt")
#run following functions to check the distance between FFN and matrix production
print('######### Now assert the distance of logits between original forward and matrix production. ########')
model=assert_FFNandproduction_gpt2xl(args)
model()
print('######### Assert Completes. ########')
#test the original output of complete LLMs
print('######### Now generating the original output of complete LLMs ########')
orig_model = GPT2LMHeadModel.from_pretrained("openai-community/gpt2")
outputs = orig_model(**inputs, labels=inputs["input_ids"])
print('logits is', outputs.logits, outputs.logits.size())
_,predicted_indices=torch.topk(outputs.logits[0][-1],10)
print('max probability token_ids are:', predicted_indices)
print('max probability tokens are:', tokenizer.decode(predicted_indices))
generation_output = orig_model.generate(input_ids=inputs["input_ids"], max_new_tokens=2)
print(tokenizer.decode(generation_output[0]))
print('######### Finished ########')
#check FFN and production of layer output belonging
print('######### Now checking logits of forward and matrix in each layer ########')
model=show_each_layer_vocb(args)
model(inputs)
print('######### Check Completes. ########')
#check residual stream equal to layer output
print('######### Now checking logits of residual stream and original forward in each layer ########')
model=assert_attentionmlp_equal_output(args)
model(inputs)
print('######### Check Completes. ########')
orig_model = GPT2LMHeadModel.from_pretrained("openai-community/gpt2").cuda()
outputs = orig_model(**inputs, labels=inputs["input_ids"])
#check circuits sum equal to layer output
print('######### Now checking logits of circuits sum and original forward in each layer ########')
model=assert_circuits_equal_output(args)
model(inputs)
print('######### Check Completes. ########')
#show each circuit's vocabulary mapping
print('######### Now showing the vocabulary of each circuit in each layer ########')
model=show_vocabulary_circuit(args)
model(inputs)
print('######### Completes. ########')