iamgroot42 commited on
Commit
ae72c9e
·
verified ·
1 Parent(s): e0c2821

Create mimir.py

Browse files
Files changed (1) hide show
  1. mimir.py +98 -0
mimir.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Data used for experiments with MIMIR. Processed train/test splits for models trained on the Pile (for now).
3
+ Processing data at HF end.
4
+ """
5
+
6
+ from datasets import GeneratorBasedBuilder, SplitGenerator, DownloadManager, BuilderConfig
7
+ import json
8
+ import os
9
+
10
+ import datasets
11
+
12
+
13
+ _HOMEPAGE = "http://github.com/iamgroot42/mimir"
14
+
15
+ _DESCRIPTION = """\
16
+ Member and non-member splits for our MI experiments using MIMIR. Data is available for each source.
17
+ We also cache neighbors (generated for the NE attack).
18
+ """
19
+
20
+ _CITATION = """\
21
+ @article{duan2024do,
22
+ title={Do Membership Inference Attacks Work on Large Language Models?},
23
+ author={Duan*, Michael and \textbf{A. Suri*} and Mireshghallah, Niloofar and Min, Sewon and Shi, Weijia and Zettlemoyer, Luke and Tsvetkov, Yulia and Choi, Yejin and Evans, David and Hajishirzi, Hannaneh},
24
+ journal={arXiv preprint arXiv:???},
25
+ year={2024}
26
+ }
27
+ """
28
+
29
+ class MimirConfig(BuilderConfig):
30
+ """BuilderConfig for Mimir dataset."""
31
+
32
+ def __init__(self, **kwargs):
33
+ """Constructs a MimirConfig.
34
+
35
+ Args:
36
+ **kwargs: keyword arguments forwarded to super.
37
+ """
38
+ super(MimirConfig, self).__init__(**kwargs)
39
+
40
+
41
+ class MimirDataset(GeneratorBasedBuilder):
42
+ # Assuming 'VERSION' is defined
43
+ VERSION = datasets.Version("1.0.0")
44
+
45
+ # Define the builder configs
46
+ BUILDER_CONFIG_CLASS = MimirConfig
47
+ BUILDER_CONFIGS = [
48
+ MimirConfig(name="the_pile_arxiv", description="This split contains data from Arxiv, truncated with 7-gram overlap threshold < 0.2."),
49
+ ]
50
+
51
+ def _info(self):
52
+ return datasets.DatasetInfo(
53
+ # This is the description that will appear on the datasets page.
54
+ description=_DESCRIPTION,
55
+ # This defines the different columns of the dataset and their types
56
+ features=datasets.Features(
57
+ {
58
+ "text": datasets.Value("string"), # Each example is a piece of text
59
+ }
60
+ ),
61
+ # If there's a common (input, target) tuple from the features,
62
+ # specify them here. They'll be used if as_supervised=True in
63
+ # builder.as_dataset.
64
+ supervised_keys=None,
65
+ # Homepage of the dataset for documentation
66
+ homepage=_HOMEPAGE,
67
+ # Citation for the dataset
68
+ # citation=_CITATION,
69
+ )
70
+
71
+ def _split_generators(self, dl_manager: DownloadManager):
72
+ """Returns SplitGenerators."""
73
+ # Path to the data files
74
+ NEIGHBOR_SUFFIX = "_neighbors_25_bert_in_place_swap"
75
+ parent_dir = "cache_100_200_1000_512"
76
+
77
+ file_paths = {
78
+ "member": os.path.join(parent_dir, "train", self.config.name + ".jsonl"),
79
+ "nonmember": os.path.join(parent_dir, "test", self.config.name + ".jsonl")
80
+ }
81
+ # Load neighbor splits if they exist
82
+ if os.path.exists(os.path.join(parent_dir, "train_neighbors", self.config.name + f"{NEIGHBOR_SUFFIX}.jsonl")):
83
+ # Assume if train nieghbors exist, test neighbors also exist
84
+ file_paths["member_neighbors"] = os.path.join("cache_100_200_1000_512", "train_neighbors", self.config.name + f"{NEIGHBOR_SUFFIX}.jsonl"),
85
+ file_paths["nonmember_neighbors"] = os.path.join("cache_100_200_1000_512", "test_neighbors", self.config.name + f"{NEIGHBOR_SUFFIX}.jsonl")
86
+
87
+ splits = []
88
+ for k, v in file_paths.items():
89
+ splits.append(SplitGenerator(name=k, gen_kwargs={"file_path": v}))
90
+ return splits
91
+
92
+ def _generate_examples(self, file_path):
93
+ """Yields examples."""
94
+ # Open the specified .jsonl file and read each line
95
+ with open(file_path, 'r') as f:
96
+ for id, line in enumerate(f):
97
+ data = json.loads(line)
98
+ yield id, {"text": data}