-
Notifications
You must be signed in to change notification settings - Fork 456
Expand file tree
/
Copy pathvalidators.py
More file actions
129 lines (108 loc) 路 4.96 KB
/
Copy pathvalidators.py
File metadata and controls
129 lines (108 loc) 路 4.96 KB
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
"""
Misc Validators
=================
Validators ensure compatibility between search methods, transformations, constraints, and goal functions.
"""
import re
import textattack
from textattack.goal_functions import (
InputReduction,
MinimizeBleu,
NonOverlappingOutput,
TargetedClassification,
UntargetedClassification,
)
from . import logger
# A list of goal functions and the corresponding available models.
MODELS_BY_GOAL_FUNCTIONS = {
(TargetedClassification, UntargetedClassification, InputReduction): [
r"^textattack.models.helpers.lstm_for_classification.*",
r"^textattack.models.helpers.word_cnn_for_classification.*",
r"^transformers.modeling_\w*\.\w*ForSequenceClassification$",
],
(NonOverlappingOutput, MinimizeBleu,): [
r"^textattack.models.helpers.t5_for_text_to_text.*",
],
}
# Unroll the `MODELS_BY_GOAL_FUNCTIONS` dictionary into a dictionary that has
# a key for each goal function. (Note the plurality here that distinguishes
# the two variables from one another.)
MODELS_BY_GOAL_FUNCTION = {}
for goal_functions, matching_model_globs in MODELS_BY_GOAL_FUNCTIONS.items():
for goal_function in goal_functions:
MODELS_BY_GOAL_FUNCTION[goal_function] = matching_model_globs
def validate_model_goal_function_compatibility(goal_function_class, model_class):
"""Determines if ``model_class`` is task-compatible with
``goal_function_class``.
For example, a text-generative model like one intended for
translation or summarization would not be compatible with a goal
function that requires probability scores, like the
UntargetedGoalFunction.
"""
# Verify that this is a valid goal function.
try:
matching_model_globs = MODELS_BY_GOAL_FUNCTION[goal_function_class]
except KeyError:
matching_model_globs = []
logger.warn(f"No entry found for goal function {goal_function_class}.")
# Get options for this goal function.
# model_module = model_class.__module__
model_module_path = ".".join((model_class.__module__, model_class.__name__))
# Ensure the model matches one of these options.
for glob in matching_model_globs:
if re.match(glob, model_module_path):
logger.info(
f"Goal function {goal_function_class} compatible with model {model_class.__name__}."
)
return
# If we got here, the model does not match the intended goal function.
for goal_functions, globs in MODELS_BY_GOAL_FUNCTIONS.items():
for glob in globs:
if re.match(glob, model_module_path):
logger.warn(
f"Unknown if model {model_class.__name__} compatible with provided goal function {goal_function_class}."
f" Found match with other goal functions: {goal_functions}."
)
return
# If it matches another goal function, warn user.
# Otherwise, this is an unknown model鈥損erhaps user-provided, or we forgot to
# update the corresponding dictionary. Warn user and return.
logger.warn(
f"Unknown if model of class {model_class} compatible with goal function {goal_function_class}."
)
def validate_model_gradient_word_swap_compatibility(model):
"""Determines if ``model`` is task-compatible with
``GradientBasedWordSwap``.
We can only take the gradient with respect to an individual word if
the model uses a word-based tokenizer.
"""
if isinstance(model, textattack.models.helpers.LSTMForClassification):
return True
else:
raise ValueError(f"Cannot perform GradientBasedWordSwap on model {model}.")
def transformation_consists_of(transformation, transformation_classes):
"""Determines if ``transformation`` is or consists only of instances of a
class in ``transformation_classes``"""
from textattack.transformations import CompositeTransformation
if isinstance(transformation, CompositeTransformation):
for t in transformation.transformations:
if not transformation_consists_of(t, transformation_classes):
return False
return True
else:
for transformation_class in transformation_classes:
if isinstance(transformation, transformation_class):
return True
return False
def transformation_consists_of_word_swaps(transformation):
"""Determines if ``transformation`` is a word swap or consists of only word
swaps."""
from textattack.transformations import WordSwap, WordSwapGradientBased
return transformation_consists_of(transformation, [WordSwap, WordSwapGradientBased])
def transformation_consists_of_word_swaps_and_deletions(transformation):
"""Determines if ``transformation`` is a word swap or consists of only word
swaps and deletions."""
from textattack.transformations import WordDeletion, WordSwap, WordSwapGradientBased
return transformation_consists_of(
transformation, [WordDeletion, WordSwap, WordSwapGradientBased]
)