-
Notifications
You must be signed in to change notification settings - Fork 33
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
1 parent
98a8dea
commit d48519d
Showing
6 changed files
with
77 additions
and
23 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
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,53 @@ | ||
from datasets import load_dataset | ||
from transformers import AutoTokenizer | ||
|
||
from elk.extraction import Extract, get_encodings | ||
|
||
|
||
def test_get_encodings(): | ||
dataset_name = "imdb" | ||
model_path = "sshleifer/tiny-gpt2" | ||
|
||
seed = 42 | ||
cfg = Extract( | ||
model=model_path, | ||
datasets=(dataset_name,), | ||
max_examples=(10, 10), | ||
template_path="_default", | ||
get_lm_preds=True, | ||
statement_column="text", | ||
balance=False, | ||
seed=seed, | ||
) | ||
split_type = "train" | ||
encodings = get_encodings(cfg, split_type) | ||
|
||
tokenizer = AutoTokenizer.from_pretrained(model_path, truncation_side="left") | ||
ds = load_dataset(dataset_name, split=split_type) | ||
ds = ds.add_column("row_id", range(len(ds))) # type: ignore | ||
ds = ds.shuffle(seed=seed).select(range(10)) # type: ignore | ||
|
||
def map_fn(ex: dict) -> dict: | ||
out_record = { | ||
"row_id": ex["row_id"], | ||
"label": ex["label"], | ||
"variant_id": "_default", | ||
"text": ex["text"], | ||
"num_suffix_tokens": 0, | ||
"output_hidden_states": True, # TODO: we might remove this | ||
} | ||
input_ids = [tokenizer(ex["text"], add_special_tokens=True)["input_ids"]] | ||
out_record["input_ids"] = input_ids | ||
answer_ids = [ | ||
tokenizer.encode(s, add_special_tokens=False)[0] for s in ["False", "True"] | ||
] | ||
out_record["answer_ids"] = answer_ids | ||
return out_record | ||
|
||
ds = ds.map(map_fn, batched=False, remove_columns=ds.column_names, num_proc=1) | ||
gt_ds = ds.filter(lambda ex: len(ex["input_ids"]) <= tokenizer.model_max_length) | ||
|
||
assert len(encodings) == len(gt_ds) | ||
assert set(encodings.column_names) == set(gt_ds.column_names) | ||
for col in encodings.column_names: | ||
assert encodings[col] == gt_ds[col] |