-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
400 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import logging | ||
|
||
import numpy as np | ||
import torch | ||
import tqdm | ||
from scipy.stats import spearmanr | ||
from transformers import AutoTokenizer | ||
|
||
from examples.wikitext.pipeline import get_wikitext_dataset | ||
from kronfluence.analyzer import Analyzer | ||
|
||
|
||
def evaluate_correlations(data_name: str, scores: torch.Tensor) -> float: | ||
margins = torch.from_numpy(torch.load(open(f"files/{data_name}/margins.pt", "rb"))) | ||
masks = torch.from_numpy(torch.load(open(f"files/{data_name}/masks.pt", "rb"))).float() | ||
|
||
val_indices = np.arange(481) | ||
preds = -masks @ scores.T | ||
|
||
rs = [] | ||
ps = [] | ||
for j in tqdm.tqdm(val_indices): | ||
r, p = spearmanr(preds[:, j], margins[:, j]) | ||
rs.append(r) | ||
ps.append(p) | ||
rs, ps = np.array(rs), np.array(ps) | ||
return rs.mean() | ||
|
||
|
||
def main(): | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
margins = torch.from_numpy(torch.load(open(f"files/margins.pt", "rb"))) | ||
masks = torch.from_numpy(torch.load(open(f"files/masks.pt", "rb"))).float() | ||
|
||
# You might need to change the path. | ||
scores = Analyzer.load_file("influence_results/wikitext/scores_ekfac/pairwise_scores.safetensors")[ | ||
"all_modules" | ||
].to(dtype=torch.float32) | ||
# scores = Analyzer.load_file("influence_results/wikitext/scores_ekfac_half/pairwise_scores.safetensors")[ | ||
# "all_modules" | ||
# ].to(dtype=torch.float32) | ||
# scores = Analyzer.load_file("influence_results/wikitext/scores_ekfac_half_compile/pairwise_scores.safetensors")[ | ||
# "all_modules" | ||
# ].to(dtype=torch.float32) | ||
|
||
corr_mean = evaluate_correlations(scores) | ||
logging.info(f"LDS: {np.mean(corr_mean)}") | ||
|
||
# We can also visualize the top influential sequences. | ||
eval_idx = 0 | ||
train_dataset = get_wikitext_dataset( | ||
split="eval_train", | ||
) | ||
eval_dataset = get_wikitext_dataset( | ||
split="valid", | ||
) | ||
tokenizer = AutoTokenizer.from_pretrained("gpt2", use_fast=True, trust_remote_code=True) | ||
print("Query Data Example:") | ||
print(tokenizer.decode(eval_dataset[eval_idx]["input_ids"])) | ||
|
||
top_idx = int(torch.argsort(scores[eval_idx], descending=True)[0]) | ||
print("Top Influential Example:") | ||
print(tokenizer.decode(train_dataset[top_idx]["input_ids"])) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.