-
Notifications
You must be signed in to change notification settings - Fork 2
/
task_patching_utils.py
629 lines (533 loc) · 24.8 KB
/
task_patching_utils.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
from __future__ import annotations
import random
from torchtyping import TensorType
from collections import namedtuple
from datasets import Dataset, DatasetDict, load_dataset
from typing import Dict, List, Tuple, TypeVar, Union
from transformers import PreTrainedTokenizerBase
import multiprocessing as mp
import math
T = TypeVar("T", bound=Union[Dataset, DatasetDict])
from torch.utils.data import DataLoader
from torch.nn.functional import kl_div, cross_entropy
from functools import partial
import torch as t
from tqdm import tqdm
from baukit import Trace
from einops import rearrange
class SparseAct():
def __init__(
self,
act: TensorType["batch_size", "n_ctx", "d_dictionary"] = None,
res: TensorType["batch_size", "n_ctx", "d_model"] = None,
resc: TensorType["batch_size", "n_ctx"] = None, # contracted residual
) -> None:
self.act = act
self.res = res
self.resc = resc
def _map(self, f, aux=None) -> 'SparseAct':
kwargs = {}
if isinstance(aux, SparseAct):
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None and getattr(aux, attr) is not None:
kwargs[attr] = f(getattr(self, attr), getattr(aux, attr))
else:
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = f(getattr(self, attr), aux)
return SparseAct(**kwargs)
def __mul__(self, other) -> 'SparseAct':
if isinstance(other, SparseAct):
# Handle SparseAct * SparseAct
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = getattr(self, attr) * getattr(other, attr)
else:
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = getattr(self, attr) * other
return SparseAct(**kwargs)
def __rmul__(self, other) -> 'SparseAct':
# This will handle float/int * SparseAct by reusing the __mul__ logic
return self.__mul__(other)
def __matmul__(self, other: SparseAct) -> SparseAct:
# dot product between two SparseActs, except only the residual is contracted
return SparseAct(act = self.act * other.act, resc=(self.res * other.res).sum(dim=-1, keepdim=True))
def __add__(self, other) -> SparseAct:
if isinstance(other, SparseAct):
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
if getattr(self, attr).shape != getattr(other, attr).shape:
raise ValueError(f"Shapes of {attr} do not match: {getattr(self, attr).shape} and {getattr(other, attr).shape}")
kwargs[attr] = getattr(self, attr) + getattr(other, attr)
else:
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = getattr(self, attr) + other
return SparseAct(**kwargs)
def __radd__(self, other: SparseAct) -> SparseAct:
return self.__add__(other)
def __sub__(self, other: SparseAct) -> SparseAct:
if isinstance(other, SparseAct):
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
if getattr(self, attr).shape != getattr(other, attr).shape:
raise ValueError(f"Shapes of {attr} do not match: {getattr(self, attr).shape} and {getattr(other, attr).shape}")
kwargs[attr] = getattr(self, attr) - getattr(other, attr)
else:
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = getattr(self, attr) - other
return SparseAct(**kwargs)
def __truediv__(self, other) -> SparseAct:
if isinstance(other, SparseAct):
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = getattr(self, attr) / getattr(other, attr)
else:
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = getattr(self, attr) / other
return SparseAct(**kwargs)
def __rtruediv__(self, other) -> SparseAct:
if isinstance(other, SparseAct):
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = other / getattr(self, attr)
else:
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = other / getattr(self, attr)
return SparseAct(**kwargs)
def __neg__(self) -> SparseAct:
sparse_result = -self.act
res_result = -self.res
return SparseAct(act=sparse_result, res=res_result)
def __invert__(self) -> SparseAct:
return self._map(lambda x, _: ~x)
def __gt__(self, other) -> SparseAct:
if isinstance(other, (int, float)):
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = getattr(self, attr) > other
return SparseAct(**kwargs)
raise ValueError("SparseAct can only be compared to a scalar.")
def __lt__(self, other) -> SparseAct:
if isinstance(other, (int, float)):
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = getattr(self, attr) < other
return SparseAct(**kwargs)
raise ValueError("SparseAct can only be compared to a scalar.")
def __getitem__(self, index: int):
return self.act[index]
def __repr__(self):
if self.res is None:
return f"SparseAct(act={self.act}, resc={self.resc})"
if self.resc is None:
return f"SparseAct(act={self.act}, res={self.res})"
else:
raise ValueError("SparseAct has both residual and contracted residual. This is an unsupported state.")
def sum(self, dim=None):
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = getattr(self, attr).sum(dim)
return SparseAct(**kwargs)
def mean(self, dim: int):
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = getattr(self, attr).mean(dim)
return SparseAct(**kwargs)
def nonzero(self):
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = getattr(self, attr).nonzero()
return SparseAct(**kwargs)
def squeeze(self, dim: int):
kwargs = {}
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
kwargs[attr] = getattr(self, attr).squeeze(dim)
return SparseAct(**kwargs)
@property
def grad(self):
kwargs = {}
for attribute in ['act', 'res', 'resc']:
if getattr(self, attribute) is not None:
kwargs[attribute] = getattr(self, attribute).grad
return SparseAct(**kwargs)
def clone(self):
kwargs = {}
for attribute in ['act', 'res', 'resc']:
if getattr(self, attribute) is not None:
kwargs[attribute] = getattr(self, attribute).clone()
return SparseAct(**kwargs)
@property
def value(self):
kwargs = {}
for attribute in ['act', 'res', 'resc']:
if getattr(self, attribute) is not None:
kwargs[attribute] = getattr(self, attribute).value
return SparseAct(**kwargs)
def save(self):
for attribute in ['act', 'res', 'resc']:
if getattr(self, attribute) is not None:
setattr(self, attribute, getattr(self, attribute).save())
return self
def detach(self):
self.act = self.act.detach()
self.res = self.res.detach()
return SparseAct(act=self.act, res=self.res)
def to_tensor(self):
if self.resc is None:
return t.cat([self.act, self.res], dim=-1)
if self.res is None:
return t.cat([self.act, self.resc], dim=-1)
raise ValueError("SparseAct has both residual and contracted residual. This is an unsupported state.")
def to(self, device):
for attr in ['act', 'res', 'resc']:
if getattr(self, attr) is not None:
setattr(self, attr, getattr(self, attr).to(device))
return self
def __gt__(self, other):
return self._map(lambda x, y: x > y, other)
def __lt__(self, other):
return self._map(lambda x, y: x < y, other)
def nonzero(self):
return self._map(lambda x, _: x.nonzero())
def squeeze(self, dim):
return self._map(lambda x, _: x.squeeze(dim=dim))
def expand_as(self, other):
return self._map(lambda x, y: x.expand_as(y), other)
def zeros_like(self):
return self._map(lambda x, _: t.zeros_like(x))
def ones_like(self):
return self._map(lambda x, _: t.ones_like(x))
def abs(self):
return self._map(lambda x, _: x.abs())
EffectOut = namedtuple('EffectOut', ['effects', 'deltas', 'grads', 'total_effect'])
def patching_effect(
clean,
patch,
model,
submodules,
dictionaries,
metric_fn,
tracer_kwargs,
steps=10,
metric_kwargs=dict(),
):
# first run through a test input to figure out which hidden states are tuples
is_tuple = {}
with model.trace("_"):
for submodule in submodules:
is_tuple[submodule] = type(submodule.output.shape) == tuple
hidden_states_clean = {}
with model.trace(clean, **tracer_kwargs), t.no_grad():
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.output
if is_tuple[submodule]:
x = x[0]
f = dictionary.encode(x)
x_hat = dictionary.decode(f)
residual = x - x_hat
hidden_states_clean[submodule] = SparseAct(act=f.save(), res=residual.save())
metric_clean = metric_fn(model, **metric_kwargs).save()
hidden_states_clean = {k : v.value for k, v in hidden_states_clean.items()}
if patch is None:
hidden_states_patch = {
k : SparseAct(act=t.zeros_like(v.act), res=t.zeros_like(v.res)) for k, v in hidden_states_clean.items()
}
total_effect = None
else:
hidden_states_patch = {}
with model.trace(patch, **tracer_kwargs), t.no_grad():
for submodule in submodules:
dictionary = dictionaries[submodule]
x = submodule.output
if is_tuple[submodule]:
x = x[0]
f = dictionary.encode(x)
x_hat = dictionary.decode(f)
residual = x - x_hat
hidden_states_patch[submodule] = SparseAct(act=f.save(), res=residual.save())
metric_patch = metric_fn(model, **metric_kwargs).save()
total_effect = (metric_patch.value - metric_clean.value).detach()
hidden_states_patch = {k : v.value for k, v in hidden_states_patch.items()}
effects = {}
deltas = {}
grads = {}
for submodule in submodules:
dictionary = dictionaries[submodule]
clean_state = hidden_states_clean[submodule]
patch_state = hidden_states_patch[submodule]
with model.trace(**tracer_kwargs) as tracer:
metrics = []
fs = []
for step in range(steps):
alpha = step / steps
f = (1 - alpha) * clean_state + alpha * patch_state
f.act.retain_grad()
f.res.retain_grad()
fs.append(f)
with tracer.invoke(clean, scan=tracer_kwargs['scan']):
if is_tuple[submodule]:
submodule.output[0][:] = dictionary.decode(f.act) + f.res
else:
submodule.output = dictionary.decode(f.act) + f.res
metrics.append(metric_fn(model, **metric_kwargs))
metric = sum([m for m in metrics])
metric.sum().backward(retain_graph=True)
mean_grad = sum([f.act.grad for f in fs]) / steps
mean_residual_grad = sum([f.res.grad for f in fs]) / steps
grad = SparseAct(act=mean_grad, res=mean_residual_grad)
delta = (patch_state - clean_state).detach() if patch_state is not None else -clean_state.detach()
effect = grad @ delta
effects[submodule] = effect
deltas[submodule] = delta
grads[submodule] = grad
return EffectOut(effects, deltas, grads, total_effect)
def sae_ablation(x, features, sae):
# affine ablate all sae features up to their bias-component
# This is iterative, so we remove each feature component one by one
# This is to avoid double-subtracting directions that features have in common
# baukit nonsense to handle both residual stream & mlp/attn_output
if(isinstance(x, tuple)):
second_value = x[1]
internal_activation = x[0]
else:
internal_activation = x
batch, seq_len, hidden_size = internal_activation.shape
int_val = rearrange(internal_activation, "b seq d_model -> (b seq) d_model")
# Encode in features, then remove all features
f = sae.encode(int_val)
residual = int_val - sae.decode(f)
# set f of ablation to zero tensor
f[..., features] = 0
x_hat = sae.decode(f)
x_recon = residual + x_hat
# baukit nonsense to handle both residual stream & mlp/attn_output
reconstruction = rearrange(x_recon, '(b s) h -> b s h', b=batch, s=seq_len)
if(isinstance(x, tuple)):
return_value = (reconstruction, second_value)
else:
return_value = reconstruction
return return_value
def task_kl(model, task_dataset, target_token_position, ae, features, activation_name):
task_kl_losses = t.zeros(len(features))
with t.no_grad():
for batch_ind, batch in enumerate(task_dataset):
batch = batch.to(model.device)
batch_size = batch.shape[0]
batch_global_ind = batch_ind * batch_size
# original_task_logits = model(batch).logits.log_softmax(dim=-1)[t.arange(target_token_position.shape[0]), target_token_position]
original_task_logits = model(batch).logits.log_softmax(dim=-1)[t.arange(batch_size), target_token_position[batch_global_ind:batch_global_ind + batch_size]]
for feature_ind, feature in enumerate(tqdm(features)):
hook_function = partial(sae_ablation, features=[feature.item()], sae=ae)
with Trace(model, activation_name, edit_output = hook_function) as ret:
# Only do KL on the target token pos
# logits = model(batch).logits.log_softmax(dim=-1)[t.arange(target_token_position.shape[0]), target_token_position]
logits = model(batch).logits.log_softmax(dim=-1)[t.arange(batch_size), target_token_position[batch_global_ind:batch_global_ind + batch_size]]
task_kl_losses[feature_ind] = kl_div(original_task_logits, logits, log_target=True, reduction="batchmean").item()
task_kl_losses /= len(task_dataset)
return task_kl_losses
def overall_kl(model, kl_data_dataloader, ae, features, activation_name, total_batches=10):
kl_losses = t.zeros(len(features))
with t.no_grad():
for feature_ind, feature in enumerate(tqdm(features)):
hook_function = partial(sae_ablation, features=[feature.item()], sae=ae)
for batch_ind, batch in enumerate(kl_data_dataloader):
if(batch_ind >= total_batches):
break
with Trace(model, activation_name, edit_output=hook_function) as ret:
edited_logits = model(batch.to(model.device)).logits.log_softmax(dim=-1)
original_logits = model(batch.to(model.device)).logits.log_softmax(dim=-1)
kl_div_value = kl_div(original_logits, edited_logits, log_target=True, reduction="batchmean")
kl_losses[feature_ind] += kl_div_value.item()
kl_losses /= total_batches
return kl_losses
def logit_diff_metric(model, clean_answers, patch_answers):
return t.mean(
model.embed_out.output[:,-1, patch_answers] - model.embed_out.output[:,-1, clean_answers],
dim = -1
)
def load_overall_dataset(dataset_name, tokenizer, ctx_length, batch_size, shuffle=False):
dataset = load_dataset(dataset_name, split="train")
token_dataset, _ = chunk_and_tokenize(dataset, tokenizer, max_length=ctx_length)
return DataLoader(
token_dataset["input_ids"],
batch_size=batch_size,
shuffle=shuffle,
)
def chunk_and_tokenize(
data: T,
tokenizer: PreTrainedTokenizerBase,
*,
format: str = "torch",
num_proc: int = min(mp.cpu_count() // 2, 8),
text_key: str = "text",
max_length: int = 2048,
return_final_batch: bool = False,
load_from_cache_file: bool = True,
add_bos_token: bool = False,
) -> Tuple[T, float]:
"""Perform GPT-style chunking and tokenization on a dataset.
The resulting dataset will consist entirely of chunks exactly `max_length` tokens
long. Long sequences will be split into multiple chunks, and short sequences will
be merged with their neighbors, using `eos_token` as a separator. The fist token
will also always be an `eos_token`.
Args:
data: The dataset to chunk and tokenize.
tokenizer: The tokenizer to use.
format: The format to return the dataset in, passed to `Dataset.with_format`.
num_proc: The number of processes to use for tokenization.
text_key: The key in the dataset to use as the text to tokenize.
max_length: The maximum length of a batch of input ids.
return_final_batch: Whether to return the final batch, which may be smaller
than the others.
load_from_cache_file: Whether to load from the cache file.
add_bos_token: Whether to prepend a BOS token before each sample.
Returns:
* The chunked and tokenized dataset.
* The ratio of nats to bits per byte see https://arxiv.org/pdf/2101.00027.pdf,
section 3.1.
"""
def _tokenize_fn(x: Dict[str, list]):
chunk_size = min(tokenizer.model_max_length, max_length) # tokenizer max length is 1024 for gpt2
if add_bos_token:
# this is already sufficient, as the tokenizer does left-sided padding with 0, which is the EOS token.
# BUT: needs to be checked for non-pythia models.
chunk_size -= 1
sep = tokenizer.eos_token or "<|endoftext|>"
sep_token = tokenizer.encode(sep)[0]
joined_text = sep.join([""] + x[text_key])
output = tokenizer(
# Concatenate all the samples together, separated by the EOS token.
joined_text, # start with an eos token
max_length=chunk_size,
return_attention_mask=False,
return_overflowing_tokens=True,
truncation=True,
)
if overflow := output.pop("overflowing_tokens", None):
# Slow Tokenizers return unnested lists of ints
assert isinstance(output["input_ids"][0], int)
# Chunk the overflow into batches of size `chunk_size`
chunks = [output["input_ids"]] + [
overflow[i * chunk_size : (i + 1) * chunk_size] for i in range(math.ceil(len(overflow) / chunk_size))
]
output = {"input_ids": chunks}
if add_bos_token:
output["input_ids"] = [[sep_token] + c for c in output["input_ids"]]
total_tokens = sum(len(ids) for ids in output["input_ids"])
total_bytes = len(joined_text.encode("utf-8"))
if not return_final_batch:
# We know that the last sample will almost always be less than the max
# number of tokens, and we don't want to pad, so we just drop it.
output = {k: v[:-1] for k, v in output.items()}
output_batch_size = len(output["input_ids"])
if output_batch_size == 0:
raise ValueError(
"Not enough data to create a single batch complete batch."
" Either allow the final batch to be returned,"
" or supply more data."
)
# We need to output this in order to compute the number of bits per byte
div, rem = divmod(total_tokens, output_batch_size)
output["length"] = [div] * output_batch_size
output["length"][-1] += rem
div, rem = divmod(total_bytes, output_batch_size)
output["bytes"] = [div] * output_batch_size
output["bytes"][-1] += rem
return output
data = data.map(
_tokenize_fn,
# Batching is important for ensuring that we don't waste tokens
# since we always throw away the last element of the batch we
# want to keep the batch size as large as possible
batched=True,
batch_size=2048,
num_proc=num_proc,
remove_columns=get_columns_all_equal(data),
load_from_cache_file=load_from_cache_file,
)
total_bytes: float = sum(data["bytes"])
total_tokens: float = sum(data["length"])
return data.with_format(format, columns=["input_ids"]), (total_tokens / total_bytes) / math.log(2)
def get_columns_all_equal(dataset: Union[Dataset, DatasetDict]) -> List[str]:
"""Get a single list of columns in a `Dataset` or `DatasetDict`.
We assert the columms are the same across splits if it's a `DatasetDict`.
Args:
dataset: The dataset to get the columns from.
Returns:
A list of columns.
"""
if isinstance(dataset, DatasetDict):
cols_by_split = dataset.column_names.values()
columns = next(iter(cols_by_split))
if not all(cols == columns for cols in cols_by_split):
raise ValueError("All splits must have the same columns")
return columns
return dataset.column_names
def get_task_function(task):
task_dict = {
"gender": gender_dataset,
}
return task_dict[task]
def tokenize_task_data(text, labels, tokenizer):
tokenized_text = tokenizer(text, padding=True, return_tensors="pt")["input_ids"]
target_token_position = (tokenized_text != tokenizer.pad_token_id).sum(dim=1) - 1
token_labels = tokenizer(labels, return_tensors="pt")["input_ids"][:, 0].tolist()
return tokenized_text, target_token_position, token_labels
def gender_dataset(num_datapoints):
masc_names = [" Alex", " Lee", " John", " Will", " Erik", " Adam", " Juan", " Henry", " Richard", " Mike" , " Ken", " Carlos", " Noah", " Lucas", " Jimmy"]
fem_names = [" Jenny", " Lucy", " Emma", " Daisy", " Chloe", " Betty", " Erin", " Rachel", " Angela", " Maria", " Violet", " Grace", " Ivy", " Anne", " Mary"]
patterns = [
"{} fixed the bike and then",
"{} booked the tickets, and immediately after that",
"{} left the documents on the table before",
"{} finished the presentation, and everyone applauded as",
"{} cooked dinner for the whole family, while",
"{} received the package, and then",
"{} found the keys on the counter before",
"{} wrote a new blog post, and right after publishing it",
"{} played the guitar, and everyone listened until",
"{} called their friends, and soon",
" After the lecture,{} quickly left the room as",
" During the party,{} sang beautifully until",
" When the alarm rang,{} was already prepared because",
" In the meeting,{} gave an opinion and then",
" On the trip,{} took many photos before",
" Under the tree,{} found a quiet spot where",
" Next to the window,{} set up the workspace and soon",
" Before going to bed,{} checked all the doors and then",
" After finishing the book,{} felt quite emotional as",
" Once the game was over,{} shook hands and then"
]
masc_sentences = []
fem_sentences = []
masc_labels = " he"
fem_labels = " she"
for _ in range(num_datapoints):
masc_name = random.choice(masc_names)
fem_name = random.choice(fem_names)
pattern = random.choice(patterns)
masc_sentences.append(pattern.format(masc_name))
fem_sentences.append(pattern.format(fem_name))
return masc_sentences, fem_sentences, masc_labels, fem_labels