|
| 1 | +import torch |
| 2 | +from typing import Union, Optional |
| 3 | +from os import PathLike |
| 4 | + |
| 5 | +from allennlp.fairness.bias_direction import ( |
| 6 | + BiasDirection, |
| 7 | + PCABiasDirection, |
| 8 | + PairedPCABiasDirection, |
| 9 | + TwoMeansBiasDirection, |
| 10 | + ClassificationNormalBiasDirection, |
| 11 | +) |
| 12 | +from allennlp.fairness.bias_utils import load_word_pairs, load_words |
| 13 | + |
| 14 | +from allennlp.common import Registrable |
| 15 | +from allennlp.data.tokenizers.tokenizer import Tokenizer |
| 16 | +from allennlp.data import Vocabulary |
| 17 | + |
| 18 | + |
| 19 | +class BiasDirectionWrapper(Registrable): |
| 20 | + """ |
| 21 | + Parent class for bias direction wrappers. |
| 22 | + """ |
| 23 | + |
| 24 | + def __init__(self): |
| 25 | + self.direction: BiasDirection = None |
| 26 | + self.noise: float = None |
| 27 | + |
| 28 | + def __call__(self, module): |
| 29 | + raise NotImplementedError |
| 30 | + |
| 31 | + def train(self, mode: bool = True): |
| 32 | + """ |
| 33 | +
|
| 34 | + # Parameters |
| 35 | +
|
| 36 | + mode : `bool`, optional (default=`True`) |
| 37 | + Sets `requires_grad` to value of `mode` for bias direction. |
| 38 | + """ |
| 39 | + self.direction.requires_grad = mode |
| 40 | + |
| 41 | + def add_noise(self, t: torch.Tensor): |
| 42 | + """ |
| 43 | +
|
| 44 | + # Parameters |
| 45 | +
|
| 46 | + t : `torch.Tensor` |
| 47 | + Tensor to which to add small amount of Gaussian noise. |
| 48 | + """ |
| 49 | + return t + self.noise * torch.randn(t.size(), device=t.device) |
| 50 | + |
| 51 | + |
| 52 | +@BiasDirectionWrapper.register("pca") |
| 53 | +class PCABiasDirectionWrapper(BiasDirectionWrapper): |
| 54 | + """ |
| 55 | +
|
| 56 | + # Parameters |
| 57 | +
|
| 58 | + seed_words_file : `Union[PathLike, str]` |
| 59 | + Path of file containing seed words. |
| 60 | + tokenizer : `Tokenizer` |
| 61 | + Tokenizer used to tokenize seed words. |
| 62 | + direction_vocab : `Vocabulary`, optional (default=`None`) |
| 63 | + Vocabulary of tokenizer. If `None`, assumes tokenizer is of |
| 64 | + type `PreTrainedTokenizer` and uses tokenizer's `vocab` attribute. |
| 65 | + namespace : `str`, optional (default=`"tokens"`) |
| 66 | + Namespace of direction_vocab to use when tokenizing. |
| 67 | + Disregarded when direction_vocab is `None`. |
| 68 | + requires_grad : `bool`, optional (default=`False`) |
| 69 | + Option to enable gradient calculation for bias direction. |
| 70 | + noise : `float`, optional (default=`1e-10`) |
| 71 | + To avoid numerical instability if embeddings are initialized uniformly. |
| 72 | + """ |
| 73 | + |
| 74 | + def __init__( |
| 75 | + self, |
| 76 | + seed_words_file: Union[PathLike, str], |
| 77 | + tokenizer: Tokenizer, |
| 78 | + direction_vocab: Optional[Vocabulary] = None, |
| 79 | + namespace: str = "tokens", |
| 80 | + requires_grad: bool = False, |
| 81 | + noise: float = 1e-10, |
| 82 | + ): |
| 83 | + self.ids = load_words(seed_words_file, tokenizer, direction_vocab, namespace) |
| 84 | + self.direction = PCABiasDirection(requires_grad=requires_grad) |
| 85 | + self.noise = noise |
| 86 | + |
| 87 | + def __call__(self, module): |
| 88 | + # embed subword token IDs and mean pool to get |
| 89 | + # embedding of original word |
| 90 | + ids_embeddings = [] |
| 91 | + for i in self.ids: |
| 92 | + i = i.to(module.weight.device) |
| 93 | + ids_embeddings.append(torch.mean(module.forward(i), dim=0, keepdim=True)) |
| 94 | + ids_embeddings = torch.cat(ids_embeddings) |
| 95 | + |
| 96 | + # adding trivial amount of noise |
| 97 | + # to eliminate linear dependence amongst all embeddings |
| 98 | + # when training first starts |
| 99 | + ids_embeddings = self.add_noise(ids_embeddings) |
| 100 | + |
| 101 | + return self.direction(ids_embeddings) |
| 102 | + |
| 103 | + |
| 104 | +@BiasDirectionWrapper.register("paired_pca") |
| 105 | +class PairedPCABiasDirectionWrapper(BiasDirectionWrapper): |
| 106 | + """ |
| 107 | +
|
| 108 | + # Parameters |
| 109 | +
|
| 110 | + seed_word_pairs_file : `Union[PathLike, str]` |
| 111 | + Path of file containing seed word pairs. |
| 112 | + tokenizer : `Tokenizer` |
| 113 | + Tokenizer used to tokenize seed words. |
| 114 | + direction_vocab : `Vocabulary`, optional (default=`None`) |
| 115 | + Vocabulary of tokenizer. If `None`, assumes tokenizer is of |
| 116 | + type `PreTrainedTokenizer` and uses tokenizer's `vocab` attribute. |
| 117 | + namespace : `str`, optional (default=`"tokens"`) |
| 118 | + Namespace of direction_vocab to use when tokenizing. |
| 119 | + Disregarded when direction_vocab is `None`. |
| 120 | + requires_grad : `bool`, optional (default=`False`) |
| 121 | + Option to enable gradient calculation for bias direction. |
| 122 | + noise : `float`, optional (default=`1e-10`) |
| 123 | + To avoid numerical instability if embeddings are initialized uniformly. |
| 124 | + """ |
| 125 | + |
| 126 | + def __init__( |
| 127 | + self, |
| 128 | + seed_word_pairs_file: Union[PathLike, str], |
| 129 | + tokenizer: Tokenizer, |
| 130 | + direction_vocab: Optional[Vocabulary] = None, |
| 131 | + namespace: str = "tokens", |
| 132 | + requires_grad: bool = False, |
| 133 | + noise: float = 1e-10, |
| 134 | + ): |
| 135 | + self.ids1, self.ids2 = load_word_pairs( |
| 136 | + seed_word_pairs_file, tokenizer, direction_vocab, namespace |
| 137 | + ) |
| 138 | + self.direction = PairedPCABiasDirection(requires_grad=requires_grad) |
| 139 | + self.noise = noise |
| 140 | + |
| 141 | + def __call__(self, module): |
| 142 | + # embed subword token IDs and mean pool to get |
| 143 | + # embedding of original word |
| 144 | + ids1_embeddings = [] |
| 145 | + for i in self.ids1: |
| 146 | + i = i.to(module.weight.device) |
| 147 | + ids1_embeddings.append(torch.mean(module.forward(i), dim=0, keepdim=True)) |
| 148 | + ids2_embeddings = [] |
| 149 | + for i in self.ids2: |
| 150 | + i = i.to(module.weight.device) |
| 151 | + ids2_embeddings.append(torch.mean(module.forward(i), dim=0, keepdim=True)) |
| 152 | + ids1_embeddings = torch.cat(ids1_embeddings) |
| 153 | + ids2_embeddings = torch.cat(ids2_embeddings) |
| 154 | + |
| 155 | + ids1_embeddings = self.add_noise(ids1_embeddings) |
| 156 | + ids2_embeddings = self.add_noise(ids2_embeddings) |
| 157 | + |
| 158 | + return self.direction(ids1_embeddings, ids2_embeddings) |
| 159 | + |
| 160 | + |
| 161 | +@BiasDirectionWrapper.register("two_means") |
| 162 | +class TwoMeansBiasDirectionWrapper(BiasDirectionWrapper): |
| 163 | + """ |
| 164 | +
|
| 165 | + # Parameters |
| 166 | +
|
| 167 | + seed_word_pairs_file : `Union[PathLike, str]` |
| 168 | + Path of file containing seed word pairs. |
| 169 | + tokenizer : `Tokenizer` |
| 170 | + Tokenizer used to tokenize seed words. |
| 171 | + direction_vocab : `Vocabulary`, optional (default=`None`) |
| 172 | + Vocabulary of tokenizer. If `None`, assumes tokenizer is of |
| 173 | + type `PreTrainedTokenizer` and uses tokenizer's `vocab` attribute. |
| 174 | + namespace : `str`, optional (default=`"tokens"`) |
| 175 | + Namespace of direction_vocab to use when tokenizing. |
| 176 | + Disregarded when direction_vocab is `None`. |
| 177 | + requires_grad : `bool`, optional (default=`False`) |
| 178 | + Option to enable gradient calculation for bias direction. |
| 179 | + noise : `float`, optional (default=`1e-10`) |
| 180 | + To avoid numerical instability if embeddings are initialized uniformly. |
| 181 | + """ |
| 182 | + |
| 183 | + def __init__( |
| 184 | + self, |
| 185 | + seed_word_pairs_file: Union[PathLike, str], |
| 186 | + tokenizer: Tokenizer, |
| 187 | + direction_vocab: Optional[Vocabulary] = None, |
| 188 | + namespace: str = "tokens", |
| 189 | + requires_grad: bool = False, |
| 190 | + noise: float = 1e-10, |
| 191 | + ): |
| 192 | + self.ids1, self.ids2 = load_word_pairs( |
| 193 | + seed_word_pairs_file, tokenizer, direction_vocab, namespace |
| 194 | + ) |
| 195 | + self.direction = TwoMeansBiasDirection(requires_grad=requires_grad) |
| 196 | + self.noise = noise |
| 197 | + |
| 198 | + def __call__(self, module): |
| 199 | + # embed subword token IDs and mean pool to get |
| 200 | + # embedding of original word |
| 201 | + ids1_embeddings = [] |
| 202 | + for i in self.ids1: |
| 203 | + i = i.to(module.weight.device) |
| 204 | + ids1_embeddings.append(torch.mean(module.forward(i), dim=0, keepdim=True)) |
| 205 | + ids2_embeddings = [] |
| 206 | + for i in self.ids2: |
| 207 | + i = i.to(module.weight.device) |
| 208 | + ids2_embeddings.append(torch.mean(module.forward(i), dim=0, keepdim=True)) |
| 209 | + ids1_embeddings = torch.cat(ids1_embeddings) |
| 210 | + ids2_embeddings = torch.cat(ids2_embeddings) |
| 211 | + |
| 212 | + ids1_embeddings = self.add_noise(ids1_embeddings) |
| 213 | + ids2_embeddings = self.add_noise(ids2_embeddings) |
| 214 | + |
| 215 | + return self.direction(ids1_embeddings, ids2_embeddings) |
| 216 | + |
| 217 | + |
| 218 | +@BiasDirectionWrapper.register("classification_normal") |
| 219 | +class ClassificationNormalBiasDirectionWrapper(BiasDirectionWrapper): |
| 220 | + """ |
| 221 | +
|
| 222 | + # Parameters |
| 223 | +
|
| 224 | + seed_word_pairs_file : `Union[PathLike, str]` |
| 225 | + Path of file containing seed word pairs. |
| 226 | + tokenizer : `Tokenizer` |
| 227 | + Tokenizer used to tokenize seed words. |
| 228 | + direction_vocab : `Vocabulary`, optional (default=`None`) |
| 229 | + Vocabulary of tokenizer. If `None`, assumes tokenizer is of |
| 230 | + type `PreTrainedTokenizer` and uses tokenizer's `vocab` attribute. |
| 231 | + namespace : `str`, optional (default=`"tokens"`) |
| 232 | + Namespace of direction_vocab to use when tokenizing. |
| 233 | + Disregarded when direction_vocab is `None`. |
| 234 | + noise : `float`, optional (default=`1e-10`) |
| 235 | + To avoid numerical instability if embeddings are initialized uniformly. |
| 236 | + """ |
| 237 | + |
| 238 | + def __init__( |
| 239 | + self, |
| 240 | + seed_word_pairs_file: Union[PathLike, str], |
| 241 | + tokenizer: Tokenizer, |
| 242 | + direction_vocab: Optional[Vocabulary] = None, |
| 243 | + namespace: str = "tokens", |
| 244 | + noise: float = 1e-10, |
| 245 | + ): |
| 246 | + self.ids1, self.ids2 = load_word_pairs( |
| 247 | + seed_word_pairs_file, tokenizer, direction_vocab, namespace |
| 248 | + ) |
| 249 | + self.direction = ClassificationNormalBiasDirection() |
| 250 | + self.noise = noise |
| 251 | + |
| 252 | + def __call__(self, module): |
| 253 | + # embed subword token IDs and mean pool to get |
| 254 | + # embedding of original word |
| 255 | + ids1_embeddings = [] |
| 256 | + for i in self.ids1: |
| 257 | + i = i.to(module.weight.device) |
| 258 | + ids1_embeddings.append(torch.mean(module.forward(i), dim=0, keepdim=True)) |
| 259 | + ids2_embeddings = [] |
| 260 | + for i in self.ids2: |
| 261 | + i = i.to(module.weight.device) |
| 262 | + ids2_embeddings.append(torch.mean(module.forward(i), dim=0, keepdim=True)) |
| 263 | + ids1_embeddings = torch.cat(ids1_embeddings) |
| 264 | + ids2_embeddings = torch.cat(ids2_embeddings) |
| 265 | + |
| 266 | + ids1_embeddings = self.add_noise(ids1_embeddings) |
| 267 | + ids2_embeddings = self.add_noise(ids2_embeddings) |
| 268 | + |
| 269 | + return self.direction(ids1_embeddings, ids2_embeddings) |
0 commit comments