Upload code/ifeval.py with huggingface_hub
Browse files- code/ifeval.py +128 -0
code/ifeval.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""IFEval (Zhou et al. 2023) verifiable-instruction following, re-implemented for the subset of
|
| 2 |
+
instruction types we can check exactly. `lm-evaluation-harness` is not installed here, so the
|
| 3 |
+
verifiers below follow the reference implementation's semantics
|
| 4 |
+
(github.com/google-research/google-research/tree/master/instruction_following_eval).
|
| 5 |
+
|
| 6 |
+
We keep only prompts whose EVERY instruction is in the supported set, and report strict
|
| 7 |
+
prompt-level accuracy (all instructions satisfied) plus instruction-level accuracy. Chance is ~0:
|
| 8 |
+
these are generation-time constraints, not multiple choice, so a model that has not acquired
|
| 9 |
+
instruction-following scores near the floor set by accidental satisfaction.
|
| 10 |
+
"""
|
| 11 |
+
from __future__ import annotations
|
| 12 |
+
import re, json, os
|
| 13 |
+
from datasets import load_dataset
|
| 14 |
+
|
| 15 |
+
CACHE = os.environ.get("MA_DATA_CACHE", "/root/hf_cache_mergeacc/datasets")
|
| 16 |
+
_CMP = {"less than": lambda a, b: a < b, "at least": lambda a, b: a >= b,
|
| 17 |
+
"at most": lambda a, b: a <= b, "exactly": lambda a, b: a == b,
|
| 18 |
+
None: lambda a, b: a >= b}
|
| 19 |
+
|
| 20 |
+
def _words(t): return re.findall(r"\b\w+\b", t)
|
| 21 |
+
def _sentences(t):
|
| 22 |
+
s = re.split(r"(?<=[.!?])\s+", t.strip())
|
| 23 |
+
return [x for x in s if x.strip()]
|
| 24 |
+
def _paras(t): return [p for p in re.split(r"\n\n+", t.strip()) if p.strip()]
|
| 25 |
+
|
| 26 |
+
def _v(iid, kw, r, prompt):
|
| 27 |
+
k = lambda n: kw.get(n)
|
| 28 |
+
if iid == "punctuation:no_comma": return "," not in r
|
| 29 |
+
if iid == "change_case:english_lowercase": return r == r.lower()
|
| 30 |
+
if iid == "change_case:english_capital": return r == r.upper()
|
| 31 |
+
if iid == "change_case:capital_word_frequency":
|
| 32 |
+
n = sum(1 for w in _words(r) if w.isupper() and len(w) > 1)
|
| 33 |
+
return _CMP[k("capital_relation")](n, k("capital_frequency"))
|
| 34 |
+
if iid == "keywords:existence":
|
| 35 |
+
return all(re.search(re.escape(w), r, re.I) for w in (k("keywords") or []))
|
| 36 |
+
if iid == "keywords:frequency":
|
| 37 |
+
n = len(re.findall(re.escape(k("keyword")), r, re.I))
|
| 38 |
+
return _CMP[k("relation")](n, k("frequency"))
|
| 39 |
+
if iid == "keywords:forbidden_words":
|
| 40 |
+
return not any(re.search(r"\b" + re.escape(w) + r"\b", r, re.I) for w in (k("forbidden_words") or []))
|
| 41 |
+
if iid == "keywords:letter_frequency":
|
| 42 |
+
n = r.lower().count((k("letter") or "").lower())
|
| 43 |
+
return _CMP[k("let_relation")](n, k("let_frequency"))
|
| 44 |
+
if iid == "length_constraints:number_sentences":
|
| 45 |
+
return _CMP[k("relation")](len(_sentences(r)), k("num_sentences"))
|
| 46 |
+
if iid == "length_constraints:number_words":
|
| 47 |
+
return _CMP[k("relation")](len(_words(r)), k("num_words"))
|
| 48 |
+
if iid == "length_constraints:number_paragraphs":
|
| 49 |
+
return len(_paras(r)) == k("num_paragraphs")
|
| 50 |
+
if iid == "length_constraints:nth_paragraph_first_word":
|
| 51 |
+
ps = _paras(r); n = k("nth_paragraph")
|
| 52 |
+
if not n or len(ps) < n: return False
|
| 53 |
+
w = _words(ps[n - 1])
|
| 54 |
+
return bool(w) and w[0].lower() == str(k("first_word")).lower()
|
| 55 |
+
if iid == "detectable_format:number_highlighted_sections":
|
| 56 |
+
n = len(re.findall(r"\*[^\*\n]+\*", r))
|
| 57 |
+
return n >= (k("num_highlights") or 0)
|
| 58 |
+
if iid == "detectable_format:title":
|
| 59 |
+
return bool(re.search(r"<<[^\n]+>>", r))
|
| 60 |
+
if iid == "detectable_format:number_bullet_lists":
|
| 61 |
+
return len(re.findall(r"^\s*\*\s+", r, re.M)) == k("num_bullets")
|
| 62 |
+
if iid == "detectable_format:json_format":
|
| 63 |
+
t = re.sub(r"^```(json)?|```$", "", r.strip(), flags=re.M).strip()
|
| 64 |
+
try: json.loads(t); return True
|
| 65 |
+
except Exception: return False
|
| 66 |
+
if iid == "detectable_format:multiple_sections":
|
| 67 |
+
sp = k("section_spliter") or ""
|
| 68 |
+
return len(re.findall(re.escape(sp) + r"\s*\d+", r)) >= (k("num_sections") or 0)
|
| 69 |
+
if iid == "detectable_format:constrained_response":
|
| 70 |
+
return any(o in r for o in ("My answer is yes.", "My answer is no.", "My answer is maybe."))
|
| 71 |
+
if iid == "detectable_content:number_placeholders":
|
| 72 |
+
return len(re.findall(r"\[[^\]\n]*\]", r)) >= (k("num_placeholders") or 0)
|
| 73 |
+
if iid == "detectable_content:postscript":
|
| 74 |
+
m = (k("postscript_marker") or "P.S.")
|
| 75 |
+
return m.lower() in r.lower()
|
| 76 |
+
if iid == "startend:end_checker":
|
| 77 |
+
return r.strip().lower().endswith(str(k("end_phrase") or "").strip().lower())
|
| 78 |
+
if iid == "startend:quotation":
|
| 79 |
+
t = r.strip()
|
| 80 |
+
return len(t) >= 2 and t.startswith('"') and t.endswith('"')
|
| 81 |
+
if iid == "combination:repeat_prompt":
|
| 82 |
+
p = (k("prompt_to_repeat") or "").strip()
|
| 83 |
+
return bool(p) and r.strip().lower().startswith(p.lower()[:min(len(p), 120)])
|
| 84 |
+
if iid == "combination:two_responses":
|
| 85 |
+
return len(re.split(r"\*\*\*+", r)) >= 2
|
| 86 |
+
return None # unsupported
|
| 87 |
+
|
| 88 |
+
SUPPORTED = {"punctuation:no_comma", "change_case:english_lowercase", "change_case:english_capital",
|
| 89 |
+
"change_case:capital_word_frequency", "keywords:existence", "keywords:frequency",
|
| 90 |
+
"keywords:forbidden_words", "keywords:letter_frequency",
|
| 91 |
+
"length_constraints:number_sentences", "length_constraints:number_words",
|
| 92 |
+
"length_constraints:number_paragraphs", "length_constraints:nth_paragraph_first_word",
|
| 93 |
+
"detectable_format:number_highlighted_sections", "detectable_format:title",
|
| 94 |
+
"detectable_format:number_bullet_lists", "detectable_format:json_format",
|
| 95 |
+
"detectable_format:multiple_sections", "detectable_format:constrained_response",
|
| 96 |
+
"detectable_content:number_placeholders", "detectable_content:postscript",
|
| 97 |
+
"startend:end_checker", "startend:quotation", "combination:repeat_prompt",
|
| 98 |
+
"combination:two_responses"}
|
| 99 |
+
|
| 100 |
+
_D = None
|
| 101 |
+
def docs(n=None, seed=1234):
|
| 102 |
+
global _D
|
| 103 |
+
if _D is None:
|
| 104 |
+
ds = load_dataset("google/IFEval", split="train", cache_dir=CACHE)
|
| 105 |
+
out = []
|
| 106 |
+
for r in ds:
|
| 107 |
+
ids = list(r["instruction_id_list"])
|
| 108 |
+
if not ids or any(i not in SUPPORTED for i in ids): continue
|
| 109 |
+
kws = [{k: v for k, v in kw.items() if v is not None} for kw in r["kwargs"]]
|
| 110 |
+
out.append({"prompt": r["prompt"], "ids": ids, "kwargs": kws})
|
| 111 |
+
_D = out
|
| 112 |
+
rows = list(_D)
|
| 113 |
+
if n and len(rows) > n:
|
| 114 |
+
import random; random.Random(seed).shuffle(rows); rows = rows[:n]
|
| 115 |
+
return rows
|
| 116 |
+
|
| 117 |
+
def score(rows, responses):
|
| 118 |
+
"""strict prompt-level and instruction-level accuracy."""
|
| 119 |
+
ok_p, ok_i, tot_i = 0, 0, 0
|
| 120 |
+
for d, r in zip(rows, responses):
|
| 121 |
+
good = True
|
| 122 |
+
for iid, kw in zip(d["ids"], d["kwargs"]):
|
| 123 |
+
v = _v(iid, kw, r, d["prompt"])
|
| 124 |
+
if v is None: continue
|
| 125 |
+
tot_i += 1; ok_i += int(v); good &= bool(v)
|
| 126 |
+
ok_p += int(good)
|
| 127 |
+
return {"ifeval_prompt": ok_p / max(len(rows), 1),
|
| 128 |
+
"ifeval_inst": ok_i / max(tot_i, 1), "n": len(rows)}
|