-
Notifications
You must be signed in to change notification settings - Fork 13
/
toxigen.py
307 lines (288 loc) · 11.9 KB
/
toxigen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import os
from eureka_ml_insights.core import (
DataProcessing,
EvalReporting,
Inference,
PromptProcessing,
)
from eureka_ml_insights.data_utils import (
AddColumn,
ColumnRename,
DataReader,
HFDataReader,
MMDataLoader,
SamplerTransform,
SequenceTransform,
)
from eureka_ml_insights.data_utils.toxigen_utils import (
GPT4ToxiGenRegex,
GPT4ToxiGenRegexGenerative,
)
from eureka_ml_insights.metrics import (
AverageAggregator,
CountAggregator,
ExactMatch,
)
from .config import (
AggregatorConfig,
DataProcessingConfig,
DataSetConfig,
EvalReportingConfig,
InferenceConfig,
MetricConfig,
PipelineConfig,
PromptProcessingConfig,
)
from .experiment_config import ExperimentConfig
from .model_configs import OAI_GPT4_1106_PREVIEW_CONFIG
"""This class specifies the config for running Toxigen discriminative benchmark."""
class ToxiGen_Discriminative_PIPELINE(ExperimentConfig):
def configure_pipeline(self, model_config):
# Configure the data pre processing component.
self.data_pre_processing = PromptProcessingConfig(
component_type=PromptProcessing,
data_reader_config=DataSetConfig(
HFDataReader,
{
"path": "toxigen/toxigen-data",
"tasks": ["annotated"],
# Note that "split" can be set to either ["test"] which will perform evaluation on 940 samples or
# ["train"] which will perform evaluation on 8,960 samples
"split": ["train"],
"transform": SequenceTransform(
[
ColumnRename(
name_mapping={
"text": "statement",
"target_group": "category",
"toxicity_human": "ground_truth",
}
),
]
),
},
),
prompt_template_path=os.path.join(
os.path.dirname(__file__), "../prompt_templates/toxigen_templates/discriminative_eval.jinja"
),
output_dir=os.path.join(self.log_dir, "data_pre_processing_output"),
)
# Inference component
self.inference_comp = InferenceConfig(
component_type=Inference,
model_config=model_config,
data_loader_config=DataSetConfig(
MMDataLoader,
{"path": os.path.join(self.data_pre_processing.output_dir, "transformed_data.jsonl")},
),
output_dir=os.path.join(self.log_dir, "inference_result"),
)
# Configure the data post processing component.
self.data_post_processing = DataProcessingConfig(
component_type=DataProcessing,
data_reader_config=DataSetConfig(
DataReader,
{
"path": os.path.join(self.inference_comp.output_dir, "inference_result.jsonl"),
"format": ".jsonl",
"transform": SequenceTransform(
[
AddColumn("merged_group"),
GPT4ToxiGenRegex("model_output", "ground_truth", "category", "merged_group"),
]
),
},
),
output_dir=os.path.join(self.log_dir, "data_post_processing_output"),
)
# Configure the evaluation and reporting component.
self.evalreporting_comp = EvalReportingConfig(
component_type=EvalReporting,
data_reader_config=DataSetConfig(
DataReader,
{
"path": os.path.join(self.data_post_processing.output_dir, "transformed_data.jsonl"),
"format": ".jsonl",
},
),
metric_config=MetricConfig(ExactMatch),
aggregator_configs=[
AggregatorConfig(CountAggregator, {"column_names": ["ExactMatch_result"], "normalize": True}),
AggregatorConfig(
CountAggregator, {"column_names": ["ExactMatch_result"], "group_by": "category", "normalize": True}
),
AggregatorConfig(
CountAggregator,
{"column_names": ["ExactMatch_result"], "group_by": "ground_truth", "normalize": True},
),
AggregatorConfig(
CountAggregator,
{"column_names": ["ExactMatch_result"], "group_by": "merged_group", "normalize": True},
),
],
output_dir=os.path.join(self.log_dir, "eval_report"),
)
return PipelineConfig(
[self.data_pre_processing, self.inference_comp, self.data_post_processing, self.evalreporting_comp],
self.log_dir,
)
"""This class specifies the config for running Toxigen generative benchmark."""
class ToxiGen_Generative_PIPELINE(ExperimentConfig):
def configure_pipeline(self, model_config):
# Data pre processing component.
self.data_pre_processing = PromptProcessingConfig(
component_type=PromptProcessing,
data_reader_config=DataSetConfig(
HFDataReader,
{
"path": "toxigen/toxigen-data",
"tasks": "prompts",
# Note that "split" can be set to one of the following 31 categories each one having 1000 samples:
#
"split": [
"hate_trans_1k",
"neutral_black_1k",
"hate_native_american_1k",
"neutral_immigrant_1k",
"hate_middle_east_1k",
"neutral_lgbtq_1k",
"neutral_women_1k",
"neutral_chinese_1k",
"hate_latino_1k",
"hate_bisexual_1k",
"hate_mexican_1k",
"hate_asian_1k",
"neutral_mental_disability_1k",
"neutral_mexican_1k",
"hate_mental_disability_1k",
"neutral_bisexual_1k",
"neutral_latino_1k",
"hate_chinese_1k",
"neutral_jewish_1k",
"hate_muslim_1k",
"neutral_asian_1k",
"hate_physical_disability_1k",
"hate_jewish_1k",
"neutral_muslim_1k",
"hate_immigrant_1k",
"hate_black_1k",
"hate_lgbtq_1k",
"hate_women_1k",
"neutral_middle_east_1k",
"neutral_native_american_1k",
"neutral_physical_disability_1k",
],
"transform": SequenceTransform(
[
SamplerTransform(sample_count=1550, random_seed=1234),
ColumnRename(
name_mapping={
"text": "prompt",
}
),
]
),
},
),
output_dir=os.path.join(self.log_dir, "data_pre_processing_output"),
)
# Inference component
self.inference_comp = InferenceConfig(
component_type=Inference,
model_config=model_config,
data_loader_config=DataSetConfig(
MMDataLoader,
{"path": os.path.join(self.data_pre_processing.output_dir, "transformed_data.jsonl")},
),
output_dir=os.path.join(self.log_dir, "inference_result"),
)
# Eval data pre processing component.
self.eval_data_pre_processing = PromptProcessingConfig(
component_type=PromptProcessing,
data_reader_config=DataSetConfig(
DataReader,
{
"path": os.path.join(self.inference_comp.output_dir, "inference_result.jsonl"),
"format": ".jsonl",
"transform": SequenceTransform([ColumnRename(name_mapping={"model_output": "statement"})]),
},
),
prompt_template_path=os.path.join(
os.path.dirname(__file__), "../prompt_templates/toxigen_templates/discriminative_eval.jinja"
),
output_dir=os.path.join(self.log_dir, "eval_data_pre_processing_output"),
)
# Eval Inference component
self.eval_inference_comp = InferenceConfig(
component_type=Inference,
model_config=OAI_GPT4_1106_PREVIEW_CONFIG,
data_loader_config=DataSetConfig(
MMDataLoader,
{"path": os.path.join(self.eval_data_pre_processing.output_dir, "transformed_data.jsonl")},
),
output_dir=os.path.join(self.log_dir, "eval_inference_result"),
)
# Eval data post processing component.
self.eval_data_post_processing = DataProcessingConfig(
component_type=DataProcessing,
data_reader_config=DataSetConfig(
DataReader,
{
"path": os.path.join(self.eval_inference_comp.output_dir, "inference_result.jsonl"),
"format": ".jsonl",
"transform": SequenceTransform(
[
AddColumn("ground_truth"),
AddColumn("category"),
AddColumn("merged_group"),
GPT4ToxiGenRegexGenerative(
"model_output",
"ground_truth",
"category",
"merged_group",
),
]
),
},
),
output_dir=os.path.join(self.log_dir, "eval_data_post_processing_output"),
)
# Configure the evaluation and reporting component.
self.evalreporting_comp = EvalReportingConfig(
component_type=EvalReporting,
data_reader_config=DataSetConfig(
DataReader,
{
"path": os.path.join(self.eval_data_post_processing.output_dir, "transformed_data.jsonl"),
"format": ".jsonl",
},
),
aggregator_configs=[
AggregatorConfig(AverageAggregator, {"column_names": ["model_output"], "ignore_non_numeric": True}),
AggregatorConfig(
AverageAggregator,
{"column_names": ["model_output"], "group_by": "ground_truth", "ignore_non_numeric": True},
),
AggregatorConfig(
AverageAggregator,
{"column_names": ["model_output"], "group_by": "category", "ignore_non_numeric": True},
),
AggregatorConfig(
AverageAggregator,
{"column_names": ["model_output"], "group_by": "merged_group", "ignore_non_numeric": True},
),
],
output_dir=os.path.join(self.log_dir, "eval_report"),
)
# Configure the pipeline
return PipelineConfig(
[
self.data_pre_processing,
self.inference_comp,
self.eval_data_pre_processing,
self.eval_inference_comp,
self.eval_data_post_processing,
self.evalreporting_comp,
],
self.log_dir,
)