Commit ·
193c97b
1
Parent(s): f35c0ec
the big change
Browse files- mapped_multitask.py +5 -95
- mapped_nq.py +27 -12
- mapped_qampari.py +3 -1
- nq.py +16 -11
mapped_multitask.py
CHANGED
|
@@ -30,18 +30,6 @@ def to_dict_element(el, cols):
|
|
| 30 |
return final_dict
|
| 31 |
|
| 32 |
|
| 33 |
-
|
| 34 |
-
def mega_hash(func,dataset_name,dataset_config,dataset_obj,split):
|
| 35 |
-
hasher = Hasher()
|
| 36 |
-
hasher.update(repr(dataset_obj))
|
| 37 |
-
hasher.update(pickle.dumps(func))
|
| 38 |
-
hasher.update(split)
|
| 39 |
-
hasher.update(dataset_config)
|
| 40 |
-
hasher.update(dataset_name)
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
return hasher.hexdigest()
|
| 44 |
-
|
| 45 |
logger = datasets.logging.get_logger(__name__)
|
| 46 |
|
| 47 |
|
|
@@ -81,57 +69,6 @@ class MappedMultitaskConfig(datasets.BuilderConfig):
|
|
| 81 |
self.feature_format = feature_format
|
| 82 |
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
def to_source_target(example):
|
| 87 |
-
# print(type())
|
| 88 |
-
source = []
|
| 89 |
-
target = []
|
| 90 |
-
meta_list = []
|
| 91 |
-
for ctx, ans_for, title, question, qids, cids, answer_ids, answer_list in zip(
|
| 92 |
-
example["positive_ctxs.text"],
|
| 93 |
-
example["positive_ctxs.pid"],
|
| 94 |
-
example["positive_ctxs.title"],
|
| 95 |
-
example["question_text"],
|
| 96 |
-
example["qid"],
|
| 97 |
-
example["positive_ctxs.chunk_id"],
|
| 98 |
-
example["answer_list.aid"],
|
| 99 |
-
example["answer_list.answer_text"],
|
| 100 |
-
):
|
| 101 |
-
ans_mapping = {idx.split("__")[-1]: ans_str for idx, ans_str in zip(answer_ids, answer_list)}
|
| 102 |
-
for c, a, t, _, _, cid in zip(ctx, ans_for, title, question, qids, cids):
|
| 103 |
-
source.append(f"Title: {t}\nText: {c}\nQuestion: {question}\n")
|
| 104 |
-
target.append(f"Answer: {ans_mapping[a.split('__')[-2]]}")
|
| 105 |
-
meta_list.append({"id": cid, "qid": qids, "question": question, "title": t, "text": c})
|
| 106 |
-
for ctx, title, question, qids, cids in zip(
|
| 107 |
-
example["hard_negative_ctxs.text"],
|
| 108 |
-
example["hard_negative_ctxs.title"],
|
| 109 |
-
example["question_text"],
|
| 110 |
-
example["qid"],
|
| 111 |
-
example["positive_ctxs.chunk_id"],
|
| 112 |
-
):
|
| 113 |
-
for c, t, _, _, cid in zip(ctx, title, question, qids, cids):
|
| 114 |
-
source.append(f"Title: {t}\nText: {c}\nQuestion: {question}\n")
|
| 115 |
-
target.append("Not relevant")
|
| 116 |
-
meta_list.append({"id": cid, "qid": qids, "question": question, "title": t, "text": c})
|
| 117 |
-
return {"target": target, "source": source, "meta": meta_list}
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
def transform_dpr(dataset,dataset_name,dataset_config):
|
| 121 |
-
|
| 122 |
-
for split in dataset.column_names:
|
| 123 |
-
_split_ds = dataset[split].flatten()
|
| 124 |
-
fingerprint = mega_hash(to_source_target,dataset_name,
|
| 125 |
-
dataset_config,_split_ds,split)
|
| 126 |
-
dataset[split] = _split_ds.map(
|
| 127 |
-
to_source_target,
|
| 128 |
-
batched=True,
|
| 129 |
-
remove_columns=_split_ds.column_names,
|
| 130 |
-
new_fingerprint = fingerprint
|
| 131 |
-
)
|
| 132 |
-
return dataset
|
| 133 |
-
|
| 134 |
-
|
| 135 |
class MappedMultitask(datasets.GeneratorBasedBuilder):
|
| 136 |
|
| 137 |
BUILDER_CONFIGS = [
|
|
@@ -189,21 +126,16 @@ class MappedMultitask(datasets.GeneratorBasedBuilder):
|
|
| 189 |
|
| 190 |
def _generate_examples(self, split):
|
| 191 |
"""This function returns the examples in the raw (text) form."""
|
| 192 |
-
qampari_path = "/home/ohadr/ssd/dalle-mini/qampari/mapped_qampari.py"
|
| 193 |
-
nq_path = "/home/ohadr/ssd/dalle-mini/qampari/mapped_nq.py"
|
| 194 |
dataset_list = []
|
| 195 |
-
|
| 196 |
-
|
|
|
|
| 197 |
dataset_list.append(qampari[split].flatten())
|
| 198 |
-
|
| 199 |
nq = load_dataset(nq_path, self.info.config_name)
|
| 200 |
if split in get_config_splits(nq_path)[self.info.config_name] and split in nq:
|
| 201 |
-
dataset_list.append(nq[split].flatten())
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
flattened_dataset = interleave_datasets(datasets=dataset_list).flatten()
|
| 206 |
-
# print(flattened_dataset)
|
| 207 |
|
| 208 |
for i,element in enumerate(flattened_dataset):
|
| 209 |
new_element = dict(source=element['source'],target=element['target'])
|
|
@@ -213,31 +145,9 @@ class MappedMultitask(datasets.GeneratorBasedBuilder):
|
|
| 213 |
title=element['meta.title'],
|
| 214 |
text=element['meta.text'],
|
| 215 |
)
|
| 216 |
-
|
| 217 |
-
# element = to_dict_element(element,cols=flattened_dataset.column_names)
|
| 218 |
-
# print(element)
|
| 219 |
yield i, new_element
|
| 220 |
|
| 221 |
|
| 222 |
-
# if self.feature_format=="reranking":
|
| 223 |
-
# fingerprint = mega_hash(to_source_target,"multitask",
|
| 224 |
-
# self.info.config_name,flattened_dataset,split)
|
| 225 |
-
# transformed_dataset = flattened_dataset.map(
|
| 226 |
-
# to_source_target,
|
| 227 |
-
# batched=True,
|
| 228 |
-
# remove_columns=flattened_dataset.column_names,
|
| 229 |
-
# new_fingerprint = fingerprint
|
| 230 |
-
# )
|
| 231 |
-
# for i,element in enumerate(transformed_dataset):
|
| 232 |
-
# yield i,element
|
| 233 |
-
# elif self.feature_format=="inference":
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
# else:
|
| 237 |
-
# assert False
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
|
| 242 |
|
| 243 |
|
|
|
|
| 30 |
return final_dict
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
logger = datasets.logging.get_logger(__name__)
|
| 34 |
|
| 35 |
|
|
|
|
| 69 |
self.feature_format = feature_format
|
| 70 |
|
| 71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
class MappedMultitask(datasets.GeneratorBasedBuilder):
|
| 73 |
|
| 74 |
BUILDER_CONFIGS = [
|
|
|
|
| 126 |
|
| 127 |
def _generate_examples(self, split):
|
| 128 |
"""This function returns the examples in the raw (text) form."""
|
|
|
|
|
|
|
| 129 |
dataset_list = []
|
| 130 |
+
qo_path = "/home/joberant/home/ohadr/qampari/mapped_qampari.py"
|
| 131 |
+
qampari = load_dataset(qo_path, self.info.config_name)
|
| 132 |
+
if split in get_config_splits(qo_path)[self.info.config_name] and split in qampari:
|
| 133 |
dataset_list.append(qampari[split].flatten())
|
| 134 |
+
nq_path = "/home/joberant/home/ohadr/qampari/mapped_nq.py"
|
| 135 |
nq = load_dataset(nq_path, self.info.config_name)
|
| 136 |
if split in get_config_splits(nq_path)[self.info.config_name] and split in nq:
|
| 137 |
+
dataset_list.append(nq[split].flatten())
|
|
|
|
|
|
|
|
|
|
| 138 |
flattened_dataset = interleave_datasets(datasets=dataset_list).flatten()
|
|
|
|
| 139 |
|
| 140 |
for i,element in enumerate(flattened_dataset):
|
| 141 |
new_element = dict(source=element['source'],target=element['target'])
|
|
|
|
| 145 |
title=element['meta.title'],
|
| 146 |
text=element['meta.text'],
|
| 147 |
)
|
|
|
|
|
|
|
|
|
|
| 148 |
yield i, new_element
|
| 149 |
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
|
| 153 |
|
mapped_nq.py
CHANGED
|
@@ -77,7 +77,7 @@ inference_mapped_features = Features(base_features)
|
|
| 77 |
|
| 78 |
|
| 79 |
class MappedNQConfig(datasets.BuilderConfig):
|
| 80 |
-
"""BuilderConfig for
|
| 81 |
|
| 82 |
def __init__(self, features=None, retriever=None, feature_format=None, **kwargs):
|
| 83 |
super(MappedNQConfig, self).__init__(**kwargs)
|
|
@@ -159,6 +159,14 @@ class MappedNQ(datasets.GeneratorBasedBuilder):
|
|
| 159 |
retriever="dprnq",
|
| 160 |
feature_format="inference",
|
| 161 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
]
|
| 163 |
|
| 164 |
def _info(self):
|
|
@@ -175,20 +183,24 @@ class MappedNQ(datasets.GeneratorBasedBuilder):
|
|
| 175 |
)
|
| 176 |
|
| 177 |
def _split_generators(self, dl_manager):
|
| 178 |
-
|
| 179 |
-
|
| 180 |
name=datasets.Split.TRAIN,
|
| 181 |
gen_kwargs={"split": "train"},
|
| 182 |
),
|
| 183 |
-
|
|
|
|
| 184 |
name=datasets.Split.VALIDATION,
|
| 185 |
gen_kwargs={"split": "validation"},
|
| 186 |
),
|
| 187 |
-
|
| 188 |
name=datasets.Split.TEST,
|
| 189 |
gen_kwargs={"split": "test"},
|
| 190 |
-
)
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
def _prepare_split(self, split_generator, **kwargs):
|
| 194 |
self.info.features = self.config.features
|
|
@@ -196,13 +208,17 @@ class MappedNQ(datasets.GeneratorBasedBuilder):
|
|
| 196 |
|
| 197 |
def _generate_examples(self, split):
|
| 198 |
"""This function returns the examples in the raw (text) form."""
|
| 199 |
-
|
| 200 |
-
path = "/home/
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
flattened_dataset = load_dataset(path, self.info.config_name).flatten()
|
| 202 |
if split not in get_config_splits(path)[self.info.config_name] or split not in flattened_dataset:
|
| 203 |
return
|
| 204 |
flattened_dataset = flattened_dataset[split]
|
| 205 |
-
print(self.info)
|
| 206 |
if self.feature_format=="reranking":
|
| 207 |
|
| 208 |
# flattened_dataset = flattened_dataset[split]
|
|
@@ -223,7 +239,6 @@ class MappedNQ(datasets.GeneratorBasedBuilder):
|
|
| 223 |
element = to_dict_element(element,cols=flattened_dataset.column_names)
|
| 224 |
for j,ctx in enumerate(element['ctxs']):
|
| 225 |
qid,ctx,question = element['qid'],ctx,element["question"]
|
| 226 |
-
# ctx.pop("score",None)
|
| 227 |
ctx.pop("score",None)
|
| 228 |
assert "id" in ctx
|
| 229 |
source_element = {"source": f"Title: {ctx['title']}\nText: {ctx['text']}\nQuestion: {question}\n",
|
|
@@ -234,4 +249,4 @@ class MappedNQ(datasets.GeneratorBasedBuilder):
|
|
| 234 |
yield f"{qid}__{ctx['id']}", source_element
|
| 235 |
|
| 236 |
else:
|
| 237 |
-
assert False
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
class MappedNQConfig(datasets.BuilderConfig):
|
| 80 |
+
"""BuilderConfig for MappedNQ."""
|
| 81 |
|
| 82 |
def __init__(self, features=None, retriever=None, feature_format=None, **kwargs):
|
| 83 |
super(MappedNQConfig, self).__init__(**kwargs)
|
|
|
|
| 159 |
retriever="dprnq",
|
| 160 |
feature_format="inference",
|
| 161 |
),
|
| 162 |
+
MappedNQConfig(
|
| 163 |
+
name="inference_bm25",
|
| 164 |
+
version=datasets.Version("1.0.1", ""),
|
| 165 |
+
description="MappedNQ dataset in DPR format with the bm25 retrieval results",
|
| 166 |
+
features=inference_mapped_features,
|
| 167 |
+
retriever="bm25",
|
| 168 |
+
feature_format="inference",
|
| 169 |
+
),
|
| 170 |
]
|
| 171 |
|
| 172 |
def _info(self):
|
|
|
|
| 183 |
)
|
| 184 |
|
| 185 |
def _split_generators(self, dl_manager):
|
| 186 |
+
split_list = dict(
|
| 187 |
+
train=datasets.SplitGenerator(
|
| 188 |
name=datasets.Split.TRAIN,
|
| 189 |
gen_kwargs={"split": "train"},
|
| 190 |
),
|
| 191 |
+
|
| 192 |
+
validation=datasets.SplitGenerator(
|
| 193 |
name=datasets.Split.VALIDATION,
|
| 194 |
gen_kwargs={"split": "validation"},
|
| 195 |
),
|
| 196 |
+
test=datasets.SplitGenerator(
|
| 197 |
name=datasets.Split.TEST,
|
| 198 |
gen_kwargs={"split": "test"},
|
| 199 |
+
)
|
| 200 |
+
)
|
| 201 |
+
if self.info.config_name=="inference_bm25":
|
| 202 |
+
split_list.pop("train")
|
| 203 |
+
return list(split_list.values())
|
| 204 |
|
| 205 |
def _prepare_split(self, split_generator, **kwargs):
|
| 206 |
self.info.features = self.config.features
|
|
|
|
| 208 |
|
| 209 |
def _generate_examples(self, split):
|
| 210 |
"""This function returns the examples in the raw (text) form."""
|
| 211 |
+
# datasets.inspect_dataset("iohadrubin/nq","tmp_nq")
|
| 212 |
+
path = "/home/joberant/home/ohadr/qampari/nq.py"
|
| 213 |
+
flattened_dataset = load_dataset(path,
|
| 214 |
+
self.info.config_name,
|
| 215 |
+
split=split).flatten()
|
| 216 |
+
|
| 217 |
flattened_dataset = load_dataset(path, self.info.config_name).flatten()
|
| 218 |
if split not in get_config_splits(path)[self.info.config_name] or split not in flattened_dataset:
|
| 219 |
return
|
| 220 |
flattened_dataset = flattened_dataset[split]
|
| 221 |
+
# print(self.info)
|
| 222 |
if self.feature_format=="reranking":
|
| 223 |
|
| 224 |
# flattened_dataset = flattened_dataset[split]
|
|
|
|
| 239 |
element = to_dict_element(element,cols=flattened_dataset.column_names)
|
| 240 |
for j,ctx in enumerate(element['ctxs']):
|
| 241 |
qid,ctx,question = element['qid'],ctx,element["question"]
|
|
|
|
| 242 |
ctx.pop("score",None)
|
| 243 |
assert "id" in ctx
|
| 244 |
source_element = {"source": f"Title: {ctx['title']}\nText: {ctx['text']}\nQuestion: {question}\n",
|
|
|
|
| 249 |
yield f"{qid}__{ctx['id']}", source_element
|
| 250 |
|
| 251 |
else:
|
| 252 |
+
assert False
|
mapped_qampari.py
CHANGED
|
@@ -200,7 +200,9 @@ class MappedQampari(datasets.GeneratorBasedBuilder):
|
|
| 200 |
|
| 201 |
def _generate_examples(self, split):
|
| 202 |
"""This function returns the examples in the raw (text) form."""
|
| 203 |
-
|
|
|
|
|
|
|
| 204 |
self.info.config_name,
|
| 205 |
split=split).flatten()
|
| 206 |
if self.feature_format=="reranking":
|
|
|
|
| 200 |
|
| 201 |
def _generate_examples(self, split):
|
| 202 |
"""This function returns the examples in the raw (text) form."""
|
| 203 |
+
# datasets.inspect_dataset("iohadrubin/qampari","tmp_qampari")
|
| 204 |
+
|
| 205 |
+
flattened_dataset = load_dataset("/home/joberant/home/ohadr/qampari/qampari.py",
|
| 206 |
self.info.config_name,
|
| 207 |
split=split).flatten()
|
| 208 |
if self.feature_format=="reranking":
|
nq.py
CHANGED
|
@@ -115,7 +115,8 @@ RETDPR_INF_URLS = {
|
|
| 115 |
}
|
| 116 |
|
| 117 |
RETBM25_INF_URLS = {
|
| 118 |
-
|
|
|
|
| 119 |
}
|
| 120 |
RETBM25_RERANKING_features = Features(
|
| 121 |
{
|
|
@@ -196,7 +197,9 @@ RETDPR_INF_features = Features(
|
|
| 196 |
)
|
| 197 |
URL_DICT = {"reranking_dprnq":RETDPR_RERANKING_URLS,
|
| 198 |
"reranking_bm25":RETBM25_RERANKING_URLS,
|
| 199 |
-
"inference_dprnq":RETDPR_INF_URLS
|
|
|
|
|
|
|
| 200 |
|
| 201 |
class NatQuestions(datasets.GeneratorBasedBuilder):
|
| 202 |
|
|
@@ -228,15 +231,16 @@ class NatQuestions(datasets.GeneratorBasedBuilder):
|
|
| 228 |
feature_format="inference",
|
| 229 |
url=URL_DICT,
|
| 230 |
),
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
|
|
|
| 240 |
]
|
| 241 |
|
| 242 |
def _info(self):
|
|
@@ -263,6 +267,7 @@ class NatQuestions(datasets.GeneratorBasedBuilder):
|
|
| 263 |
# filepath = "/home/joberant/home/ohadr/testbed/notebooks/NatQuestions_retrievers"
|
| 264 |
|
| 265 |
result = []
|
|
|
|
| 266 |
if "train" in filepath[self.info.config_name]:
|
| 267 |
result.append(
|
| 268 |
datasets.SplitGenerator(
|
|
|
|
| 115 |
}
|
| 116 |
|
| 117 |
RETBM25_INF_URLS = {
|
| 118 |
+
split:f"https://www.cs.tau.ac.il/~ohadr/nq-{split}.json.gz" for split in ["dev","test"]
|
| 119 |
+
|
| 120 |
}
|
| 121 |
RETBM25_RERANKING_features = Features(
|
| 122 |
{
|
|
|
|
| 197 |
)
|
| 198 |
URL_DICT = {"reranking_dprnq":RETDPR_RERANKING_URLS,
|
| 199 |
"reranking_bm25":RETBM25_RERANKING_URLS,
|
| 200 |
+
"inference_dprnq":RETDPR_INF_URLS,
|
| 201 |
+
"inference_bm25":RETBM25_INF_URLS,
|
| 202 |
+
}
|
| 203 |
|
| 204 |
class NatQuestions(datasets.GeneratorBasedBuilder):
|
| 205 |
|
|
|
|
| 231 |
feature_format="inference",
|
| 232 |
url=URL_DICT,
|
| 233 |
),
|
| 234 |
+
NatQuestionsConfig(
|
| 235 |
+
name="inference_bm25",
|
| 236 |
+
version=datasets.Version("1.0.1", ""),
|
| 237 |
+
description="NatQuestions dataset in a format accepted by the inference model, performing reranking on the bm25 retrieval results",
|
| 238 |
+
features=RETDPR_INF_features,
|
| 239 |
+
retriever="bm25",
|
| 240 |
+
feature_format="inference",
|
| 241 |
+
url=URL_DICT,
|
| 242 |
+
),
|
| 243 |
+
|
| 244 |
]
|
| 245 |
|
| 246 |
def _info(self):
|
|
|
|
| 267 |
# filepath = "/home/joberant/home/ohadr/testbed/notebooks/NatQuestions_retrievers"
|
| 268 |
|
| 269 |
result = []
|
| 270 |
+
|
| 271 |
if "train" in filepath[self.info.config_name]:
|
| 272 |
result.append(
|
| 273 |
datasets.SplitGenerator(
|