|
| 1 | +import pickle |
| 2 | +import matplotlib.pyplot as plt |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +models = ["counterfactuals2/wiki_Meta-Llama-3-8B-Instruct->mimic_gender_llama3_instruct_prompt:first_k_sents:500_prompt_first_k:5_max_new_tokens:25.pkl", |
| 6 | + "counterfactuals2/wiki_Meta-Llama-3-8B-Instruct->honest_steering_llama3_instruct_prompt:first_k_sents:500_prompt_first_k:5_max_new_tokens:25.pkl", |
| 7 | + #"counterfactuals2/wiki_gpt2-xl->rome_louvre_gpt2_xl_prompt:first_k_sents:500_prompt_first_k:5_max_new_tokens:25.pkl", |
| 8 | + "counterfactuals2/wiki_Meta-Llama-3-8B->chat_llama3_prompt:first_k_sents:500_prompt_first_k:5_max_new_tokens:25.pkl", |
| 9 | + "counterfactuals2/wiki_gpt2-xl->mimic_gender_gpt2_instruct_prompt:first_k_sents:500_prompt_first_k:5_max_new_tokens:25.pkl", |
| 10 | + "counterfactuals2/wiki_gpt2-xl->GPT2-memit-louvre-rome_prompt:first_k_sents:500_prompt_first_k:5_max_new_tokens:25.pkl", |
| 11 | + "counterfactuals2/wiki_gpt2-xl->GPT2-memit-koalas-new_zealand_prompt:first_k_sents:500_prompt_first_k:5_max_new_tokens:25.pkl"] |
| 12 | + #]#, |
| 13 | + #"gpt2-xl_rome-edits-louvre-rome_prompt:True.pkl", |
| 14 | + #"Llama-2-7b-hf_Llama-2-7b-chat-hf_prompt:True.pkl"] |
| 15 | +names = ["LLaMA3-Steering-Gender", "LLaMA3-Steering-Honest", "LLaMA3-Instruct", "GPT2-XL-Steering-Gender", "GPT2-XL-MEMIT-Louvre", "GPT2-XL-MEMIT-Koalas"] #["Honest-LLama", "GPT-XL-ROME", "LLama2-Chat", "GPT2-XL-MEMIT"] |
| 16 | + |
| 17 | +name2data = {} |
| 18 | +colors = ['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', "cyan", "purple", "red"] #['blue', 'orange', 'green', "red", "cyan"] # Define colors for consistency |
| 19 | + |
| 20 | +plt.rcParams["font.family"] = "serif" |
| 21 | +plt.rcParams.update({'font.size': 15}) |
| 22 | +plt.figure(figsize=(8, 6)) |
| 23 | +for idx, (name, model) in enumerate(zip(names, models)): |
| 24 | + print(name) |
| 25 | + with open(model, "rb") as f: |
| 26 | + data = pickle.load(f) |
| 27 | + orig, count = data["original"], data["counter"] |
| 28 | + #counter = [d["counter"] for d in counter] |
| 29 | + #orig = [o.split(" ") for o in original] |
| 30 | + #count = [c.split(" ") for c in counter] |
| 31 | + |
| 32 | + orig = [d["tokens"] for d in orig] |
| 33 | + count = [d["tokens"][1:] for d in count] |
| 34 | + name2data[name] = (orig, count) |
| 35 | + |
| 36 | + |
| 37 | + diffs=[] |
| 38 | + #print(len(orig), len(count)) |
| 39 | + for o,c in zip(orig, count): |
| 40 | + |
| 41 | + #print(o,c) |
| 42 | + i=0 |
| 43 | + for oo,cc in zip(o,c): |
| 44 | + #print("try", cc,oo) |
| 45 | + if cc != oo: |
| 46 | + #print(i, len(oo)) |
| 47 | + diffs.append(i/len(o)) |
| 48 | + break |
| 49 | + i+=1 |
| 50 | + #print(diffs) |
| 51 | + |
| 52 | + |
| 53 | + plt.hist( |
| 54 | + diffs, |
| 55 | + density=False, |
| 56 | + bins=15, |
| 57 | + alpha=0.5, |
| 58 | + label=name, |
| 59 | + color=colors[idx] |
| 60 | + ) |
| 61 | + |
| 62 | + # Calculate and plot median |
| 63 | + median_diff = np.median(diffs) |
| 64 | + plt.axvline( |
| 65 | + median_diff, |
| 66 | + color=colors[idx], |
| 67 | + linestyle='dashed', |
| 68 | + linewidth=2 |
| 69 | + ) |
| 70 | + """ |
| 71 | + plt.text( |
| 72 | + median_diff, |
| 73 | + plt.ylim()[1]*0.9 - idx*plt.ylim()[1]*0.08, # Adjust y-position for each label |
| 74 | + f'Median {name}: {median_diff:.2f}', |
| 75 | + rotation=0, |
| 76 | + color=colors[idx], |
| 77 | + verticalalignment='top', |
| 78 | + horizontalalignment='center', |
| 79 | + fontsize=20, # Increase font size of median labels |
| 80 | + bbox=dict(facecolor='white', alpha=0.5, edgecolor='none') |
| 81 | + ) |
| 82 | + """ |
| 83 | +plt.xlabel("Normalized Length of Longest Common Prefix", fontsize=14) |
| 84 | +plt.ylabel("Counts", fontsize=14) |
| 85 | +plt.grid() |
| 86 | +plt.legend(fontsize=13) |
| 87 | +plt.savefig("common_prefix.pdf", dpi=800) |
| 88 | + |
| 89 | + |
| 90 | + |
| 91 | +#### cosine sim |
| 92 | +#### |
| 93 | + |
| 94 | + |
| 95 | +import torch.nn.functional as F |
| 96 | + |
| 97 | +from torch import Tensor |
| 98 | +from transformers import AutoTokenizer, AutoModel |
| 99 | +import torch |
| 100 | +import sklearn |
| 101 | +from sklearn.metrics.pairwise import cosine_similarity |
| 102 | + |
| 103 | +def average_pool(last_hidden_states: Tensor, |
| 104 | + attention_mask: Tensor) -> Tensor: |
| 105 | + last_hidden = last_hidden_states.masked_fill(~attention_mask[..., None].bool(), 0.0) |
| 106 | + return last_hidden.sum(dim=1) / attention_mask.sum(dim=1)[..., None] |
| 107 | + |
| 108 | + |
| 109 | +# Each input text should start with "query: " or "passage: ". |
| 110 | +# For tasks other than retrieval, you can simply use the "query: " prefix. |
| 111 | +input_texts = ['query: how much protein should a female eat', |
| 112 | + 'query: summit define', |
| 113 | + "passage: 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.", |
| 114 | + "passage: 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."] |
| 115 | + |
| 116 | +tokenizer = AutoTokenizer.from_pretrained('intfloat/e5-base-v2') |
| 117 | +model = AutoModel.from_pretrained('intfloat/e5-base-v2') |
| 118 | + |
| 119 | +for idx, (name, model_path) in enumerate(zip(names, models)): |
| 120 | + print(name) |
| 121 | + with open(model_path, "rb") as f: |
| 122 | + data = pickle.load(f) |
| 123 | + orig, count = data["original"], data["counter"] |
| 124 | + original = [d["text"] for d in orig] |
| 125 | + counter = [d["text"] for d in count] |
| 126 | + with torch.no_grad(): |
| 127 | + batch_dict_original = tokenizer(original, max_length=512, padding=True, truncation=True, return_tensors='pt') |
| 128 | + outputs_original = model(**batch_dict_original) |
| 129 | + embeddings_original = average_pool(outputs_original.last_hidden_state, batch_dict_original['attention_mask']) |
| 130 | + |
| 131 | + batch_dict_counter = tokenizer(counter, max_length=512, padding=True, truncation=True, return_tensors='pt') |
| 132 | + outputs_counter = model(**batch_dict_counter) |
| 133 | + embeddings_counter = average_pool(outputs_counter.last_hidden_state, batch_dict_counter['attention_mask']) |
| 134 | + |
| 135 | + print(name, np.diag(cosine_similarity(embeddings_original,embeddings_counter)).mean()) |
0 commit comments