|
import os |
|
import json |
|
from random import shuffle, seed |
|
from itertools import permutations |
|
import pandas as pd |
|
from datasets import load_dataset |
|
from lmppl import EncoderDecoderLM, LM, OpenAI |
|
|
|
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", None) |
|
runs = 3 |
|
shots_num = [1, 3] |
|
prompt_dict = { |
|
"friend/ally of": "entities that are friends or allies", |
|
"competitor/rival of": "entities that are competitors or rivals", |
|
"known for": "examples of what entities are known for", |
|
"influenced by": "what has influenced different entities", |
|
"similar to": "examples of entities that are similar" |
|
} |
|
data = load_dataset("cardiffnlp/relentless", split="test") |
|
shots_ref = {} |
|
for shots in shots_num: |
|
all_perms = list(permutations(range(5), shots)) |
|
seed(42) |
|
shuffle(all_perms) |
|
shots_ref[shots] = all_perms |
|
|
|
full_result = [] |
|
for lm, ppl_class, batch, pretty_name in [ |
|
("google/flan-ul2", EncoderDecoderLM, 1, "Flan-UL2"), |
|
("google/flan-t5-xxl", EncoderDecoderLM, 1, "Flan-T5\textsubscript{XXL}"), |
|
("facebook/opt-13b", LM, 1, "OPT\textsubscript{13B}"), |
|
("davinci", OpenAI, None, "GPT-3\textsubscript{davinci}") |
|
]: |
|
scorer = None |
|
for shots in shots_num: |
|
for s in range(runs): |
|
os.makedirs(f"results/lm_lc_{shots}shots_{s}seed/{os.path.basename(lm)}", exist_ok=True) |
|
|
|
for d in data: |
|
ppl_file = f"results/lm_lc_{shots}shots_{s}seed/{os.path.basename(lm)}/ppl.{d['relation_type'].replace(' ', '_').replace('/', '__')}.jsonl" |
|
|
|
if not os.path.exists(ppl_file): |
|
|
|
if scorer is None: |
|
if ppl_class is OpenAI: |
|
scorer = ppl_class(OPENAI_API_KEY, model=lm) |
|
else: |
|
scorer = ppl_class(lm, device_map='auto', low_cpu_mem_usage=True, offload_folder=f"./offload_folder/{os.path.basename(lm)}") |
|
|
|
demo = [d['prototypical_examples'][h] for h in list(shots_ref[shots][s])] |
|
|
|
content = "\n".join([f'* ["{a}", "{b}"]' for a, b in demo]) |
|
prompt_input = f"{prompt_dict[d['relation_type']]}:\n{content}" |
|
if ppl_class is LM: |
|
prompt_input = [f'{prompt_input}\n* ["{x}", "{y}"]' for x, y in d['pairs']] |
|
ppl = scorer.get_perplexity(input_texts=prompt_input, batch=batch) |
|
output = [{"perplexity": p, "input": i, "output": ""} for p, i in zip(ppl, prompt_input)] |
|
elif ppl_class is EncoderDecoderLM: |
|
prompt_output = [f'* ["{x}", "{y}"]' for x, y in d['pairs']] |
|
ppl = scorer.get_perplexity(input_texts=[prompt_input] * len(prompt_output), output_texts=prompt_output, batch=batch) |
|
output = [{"perplexity": p, "input": prompt_input, "output": o} for p, o in zip(ppl, prompt_output)] |
|
else: |
|
prompt_input = [f'{prompt_input}\n* ["{x}", "{y}"]' for x, y in d['pairs']] |
|
ppl = scorer.get_perplexity(input_texts=prompt_input) |
|
output = [{"perplexity": p, "input": i, "output": ""} for p, i in zip(ppl, prompt_input)] |
|
|
|
with open(ppl_file, "w") as f: |
|
f.write("\n".join([json.dumps(i) for i in output])) |
|
|
|
with open(ppl_file) as f: |
|
ppl = [json.loads(i)['perplexity'] for i in f.read().split("\n") if len(i) > 0] |
|
true_rank = d['ranks'] |
|
assert len(true_rank) == len(ppl), f"Mismatch in number of examples: {len(true_rank)} vs {len(ppl)}" |
|
rank_map = {p: n for n, p in enumerate(sorted(ppl), 1)} |
|
prediction = [rank_map[p] for p in ppl] |
|
tmp = pd.DataFrame([true_rank, prediction], index=['true', 'pred']).T |
|
cor = tmp.corr("spearman").values[0, 1] |
|
full_result.append({"model": pretty_name, "shot": shots, "seed": s, "relation_type": d['relation_type'], "correlation": cor}) |
|
|
|
df = pd.DataFrame(full_result) |
|
models = df['model'].unique() |
|
df = df.pivot(columns="relation_type", index=["model", "shot", "seed"], values="correlation") |
|
df = df.T[models].T |
|
df['average'] = df.mean(1) |
|
df.to_csv(f"results/lm_lc_fewshots.csv") |
|
df = (100 * df).round() |
|
print(df) |
|
print(df.to_markdown()) |
|
print(df.to_latex(escape=False)) |
|
|