Skip to content

Commit 7c6e16d

Browse files
committed
variable names more general; remove use_cuda
1 parent bef423a commit 7c6e16d

12 files changed

+104
-108
lines changed

interpretdl/interpreter/abc_interpreter.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Interpreter(ABC):
3434
.. warning:: ``use_cuda`` would be deprecated soon. Use ``device`` directly.
3535
"""
3636

37-
def __init__(self, model: callable, device: str, use_cuda: bool = None, **kwargs):
37+
def __init__(self, model: callable, device: str, **kwargs):
3838
"""
3939
4040
Args:
@@ -46,9 +46,9 @@ def __init__(self, model: callable, device: str, use_cuda: bool = None, **kwargs
4646
self.model = model
4747
self.predict_fn = None
4848

49-
if use_cuda in [True, False]:
49+
if 'use_cuda' in kwargs and kwargs['use_cuda'] in [True, False]:
5050
warnings.warn('``use_cuda`` would be deprecated soon. Use ``device`` directly.', stacklevel=2)
51-
self.device = 'gpu:0' if use_cuda and device[:3] == 'gpu' else 'cpu'
51+
self.device = 'gpu:0' if kwargs['use_cuda'] and device[:3] == 'gpu' else 'cpu'
5252

5353
assert self.device[:3] in ['cpu', 'gpu']
5454

@@ -90,15 +90,15 @@ class InputGradientInterpreter(Interpreter):
9090
This Interpreter implements :py:func:`_build_predict_fn` that returns input gradient given an input.
9191
"""
9292

93-
def __init__(self, model: callable, device: str, use_cuda: bool = None, **kwargs):
93+
def __init__(self, model: callable, device: str, **kwargs):
9494
"""
9595
9696
Args:
9797
model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
9898
device (str): The device used for running ``model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
9999
etc.
100100
"""
101-
Interpreter.__init__(self, model, device, use_cuda, **kwargs)
101+
Interpreter.__init__(self, model, device, **kwargs)
102102
assert hasattr(model, 'forward'), \
103103
"model has to be " \
104104
"an instance of paddle.nn.Layer or a compatible one."
@@ -195,15 +195,15 @@ class InputOutputInterpreter(Interpreter):
195195
196196
"""
197197

198-
def __init__(self, model: callable, device: str, use_cuda: bool = None, **kwargs):
198+
def __init__(self, model: callable, device: str, **kwargs):
199199
"""
200200
201201
Args:
202202
model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
203203
device (str): The device used for running ``model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
204204
etc.
205205
"""
206-
Interpreter.__init__(self, model, device, use_cuda, **kwargs)
206+
Interpreter.__init__(self, model, device, **kwargs)
207207
assert hasattr(model, 'forward'), \
208208
"model has to be " \
209209
"an instance of paddle.nn.Layer or a compatible one."
@@ -271,7 +271,7 @@ class IntermediateLayerInterpreter(Interpreter):
271271
input.
272272
"""
273273

274-
def __init__(self, model: callable, device: str, use_cuda: bool = None, **kwargs):
274+
def __init__(self, model: callable, device: str, **kwargs):
275275
"""
276276
277277
Args:
@@ -280,7 +280,7 @@ def __init__(self, model: callable, device: str, use_cuda: bool = None, **kwargs
280280
etc.
281281
"""
282282

283-
Interpreter.__init__(self, model, device, use_cuda, **kwargs)
283+
Interpreter.__init__(self, model, device, **kwargs)
284284
assert hasattr(model, 'forward'), \
285285
"model has to be " \
286286
"an instance of paddle.nn.Layer or a compatible one."
@@ -361,15 +361,15 @@ class TransformerInterpreter(Interpreter):
361361
This Interpreter implements :py:func:`_build_predict_fn` that returns servral variables and gradients in each layer.
362362
"""
363363

364-
def __init__(self, model: callable, device: str, use_cuda: bool = None, **kwargs):
364+
def __init__(self, model: callable, device: str, **kwargs):
365365
"""
366366
367367
Args:
368368
model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
369369
device (str): The device used for running ``model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
370370
etc.
371371
"""
372-
Interpreter.__init__(self, model, device, use_cuda, **kwargs)
372+
Interpreter.__init__(self, model, device, **kwargs)
373373
assert hasattr(model, 'forward'), \
374374
"model has to be " \
375375
"an instance of paddle.nn.Layer or a compatible one."

interpretdl/interpreter/bidirectional_transformer.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ class BTCVInterpreter(TransformerInterpreter):
1616
The following implementation is specially designed for Vision Transformer.
1717
"""
1818

19-
def __init__(self, paddle_model: callable, device: str = 'gpu:0', use_cuda=None) -> None:
19+
def __init__(self, model: callable, device: str = 'gpu:0') -> None:
2020
"""
2121
2222
Args:
23-
paddle_model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
24-
device (str): The device used for running ``paddle_model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
23+
model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
24+
device (str): The device used for running ``model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
2525
etc.
2626
"""
27-
TransformerInterpreter.__init__(self, paddle_model, device, use_cuda)
27+
TransformerInterpreter.__init__(self, model, device)
2828

2929
def interpret(self,
3030
inputs: str or list(str) or np.ndarray,
@@ -130,7 +130,7 @@ def interpret(self,
130130
# gradient mean over heads.
131131
grad_head_mean = np.mean((total_gradients / steps).clip(min=0), axis=1) # [b, s, s]
132132

133-
if hasattr(self.paddle_model, 'global_pool') and self.paddle_model.global_pool:
133+
if hasattr(self.model, 'global_pool') and self.model.global_pool:
134134
# For MAE ViT.
135135
explanation = (R * grad_head_mean)[:, 1:, :].mean(axis=1)
136136
else:
@@ -157,15 +157,15 @@ class BTNLPInterpreter(TransformerInterpreter):
157157
The following implementation is specially designed for Ernie.
158158
"""
159159

160-
def __init__(self, paddle_model: callable, device: str = 'gpu:0', use_cuda=None) -> None:
160+
def __init__(self, model: callable, device: str = 'gpu:0') -> None:
161161
"""
162162
163163
Args:
164-
paddle_model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
165-
device (str): The device used for running ``paddle_model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
164+
model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
165+
device (str): The device used for running ``model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
166166
etc.
167167
"""
168-
TransformerInterpreter.__init__(self, paddle_model, device, use_cuda)
168+
TransformerInterpreter.__init__(self, model, device)
169169

170170
def interpret(self,
171171
raw_text: str,

interpretdl/interpreter/forgetting_events.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ class ForgettingEventsInterpreter(Interpreter):
1919
https://arxiv.org/abs/1812.05159.
2020
"""
2121

22-
def __init__(self, paddle_model: callable, device: str = 'gpu:0', use_cuda=None):
22+
def __init__(self, model: callable, device: str = 'gpu:0'):
2323
"""
2424
2525
Args:
26-
paddle_model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
27-
device (str): The device used for running ``paddle_model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
26+
model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
27+
device (str): The device used for running ``model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
2828
etc.
2929
"""
30-
Interpreter.__init__(self, paddle_model, device, use_cuda)
30+
Interpreter.__init__(self, model, device)
3131

3232
def interpret(self,
3333
train_reader: callable,
@@ -73,7 +73,7 @@ def interpret(self,
7373
y_train = [t[2] for t in data_train]
7474
x_train = paddle.to_tensor(x_train)
7575
y_train = paddle.to_tensor(np.array(y_train).reshape((-1, 1)))
76-
logits = self.paddle_model(x_train)
76+
logits = self.model(x_train)
7777
predicted = paddle.argmax(logits, axis=1).numpy()
7878
bsz = len(predicted)
7979

interpretdl/interpreter/generic_attention.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ class GAInterpreter(InputGradientInterpreter):
2222
2323
"""
2424

25-
def __init__(self, paddle_model: callable, device: str = 'gpu:0') -> None:
25+
def __init__(self, model: callable, device: str = 'gpu:0') -> None:
2626
"""
2727
2828
Args:
29-
paddle_model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
30-
device (str): The device used for running ``paddle_model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
29+
model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
30+
device (str): The device used for running ``model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
3131
etc.
3232
"""
33-
Interpreter.__init__(self, paddle_model, device)
33+
Interpreter.__init__(self, model, device)
3434

3535
def interpret(self,
3636
image_input: str or np.ndarray,
@@ -140,7 +140,7 @@ def _build_predict_fn(self, vis_attn_layer_pattern: str, txt_attn_layer_pattern:
140140

141141
if self.predict_fn is None or rebuild:
142142
import paddle
143-
self._paddle_env_setup() # inherit from InputGradientInterpreter
143+
self._env_setup() # inherit from InputGradientInterpreter
144144

145145
def predict_fn(image, text_tokenized):
146146
image = paddle.to_tensor(image)
@@ -158,7 +158,7 @@ def txt_hook(layer, input, output):
158158
txt_attns.append(output)
159159

160160
hooks = [] # for remove.
161-
for n, v in self.paddle_model.named_sublayers():
161+
for n, v in self.model.named_sublayers():
162162
if re.match(vis_attn_layer_pattern, n):
163163
h = v.register_forward_post_hook(img_hook)
164164
hooks.append(h)
@@ -167,7 +167,7 @@ def txt_hook(layer, input, output):
167167
h = v.register_forward_post_hook(txt_hook)
168168
hooks.append(h)
169169

170-
logits_per_image, logits_per_text = self.paddle_model(image, text_tokenized)
170+
logits_per_image, logits_per_text = self.model(image, text_tokenized)
171171

172172
for h in hooks:
173173
h.remove()
@@ -180,7 +180,7 @@ def txt_hook(layer, input, output):
180180
one_hot[paddle.arange(logits_per_image.shape[0]), index] = 1
181181
one_hot = paddle.to_tensor(one_hot)
182182
one_hot = paddle.sum(one_hot * logits_per_image)
183-
self.paddle_model.clear_gradients()
183+
self.model.clear_gradients()
184184
one_hot.backward()
185185

186186
img_attns_grads = []
@@ -218,15 +218,15 @@ class GANLPInterpreter(TransformerInterpreter):
218218
219219
"""
220220

221-
def __init__(self, paddle_model: callable, device: str = 'gpu:0', use_cuda=None) -> None:
221+
def __init__(self, model: callable, device: str = 'gpu:0') -> None:
222222
"""
223223
224224
Args:
225-
paddle_model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
226-
device (str): The device used for running ``paddle_model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
225+
model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
226+
device (str): The device used for running ``model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
227227
etc.
228228
"""
229-
TransformerInterpreter.__init__(self, paddle_model, device, use_cuda)
229+
TransformerInterpreter.__init__(self, model, device)
230230

231231
def interpret(self,
232232
raw_text: str,
@@ -315,15 +315,15 @@ class GACVInterpreter(TransformerInterpreter):
315315
The following implementation is specially designed for Vision Transformer.
316316
"""
317317

318-
def __init__(self, paddle_model: callable, device: str = 'gpu:0', use_cuda=None) -> None:
318+
def __init__(self, model: callable, device: str = 'gpu:0') -> None:
319319
"""
320320
321321
Args:
322-
paddle_model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
323-
device (str): The device used for running ``paddle_model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
322+
model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
323+
device (str): The device used for running ``model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
324324
etc.
325325
"""
326-
TransformerInterpreter.__init__(self, paddle_model, device, use_cuda)
326+
TransformerInterpreter.__init__(self, model, device)
327327

328328
def interpret(self,
329329
inputs: str or list(str) or np.ndarray,
@@ -381,7 +381,7 @@ def interpret(self,
381381

382382
R = R + np.matmul(attn, R)
383383

384-
if hasattr(self.paddle_model, 'global_pool') and self.paddle_model.global_pool:
384+
if hasattr(self.model, 'global_pool') and self.model.global_pool:
385385
# For MAE ViT, but GA does not work well.
386386
R = R[:, 1:, :].mean(axis=1)
387387
else:

interpretdl/interpreter/glime.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ class GLIMECVInterpreter(LIMECVInterpreter):
2020
2121
"""
2222

23-
def __init__(self, paddle_model: callable, device: str = 'gpu:0') -> None:
23+
def __init__(self, model: callable, device: str = 'gpu:0') -> None:
2424
"""
2525
2626
Args:
27-
paddle_model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
28-
device (str): The device used for running ``paddle_model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
27+
model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
28+
device (str): The device used for running ``model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
2929
etc.
3030
"""
31-
LIMECVInterpreter.__init__(self, paddle_model, device)
31+
LIMECVInterpreter.__init__(self, model, device)
3232
self.global_weights = None
3333

3434
def set_global_weights(self, global_weights_info: str or dict):

interpretdl/interpreter/gradient_cam.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ class GradCAMInterpreter(Interpreter):
2424
https://arxiv.org/abs/1610.02391.
2525
"""
2626

27-
def __init__(self, paddle_model: callable, device: str = 'gpu:0', use_cuda=None):
27+
def __init__(self, model: callable, device: str = 'gpu:0'):
2828
"""
2929
3030
Args:
31-
paddle_model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
32-
device (str): The device used for running ``paddle_model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
31+
model (callable): A model with :py:func:`forward` and possibly :py:func:`backward` functions.
32+
device (str): The device used for running ``model``, options: ``"cpu"``, ``"gpu:0"``, ``"gpu:1"``
3333
etc.
3434
"""
35-
Interpreter.__init__(self, paddle_model, device, use_cuda)
35+
Interpreter.__init__(self, model, device)
3636
self.paddle_prepared = False
3737

3838
# init for usages during the interpretation.
@@ -76,9 +76,9 @@ def interpret(self,
7676
bsz = len(data) # batch size
7777
save_path = preprocess_save_path(save_path, bsz)
7878

79-
assert target_layer_name in [n for n, v in self.paddle_model.named_sublayers()], \
79+
assert target_layer_name in [n for n, v in self.model.named_sublayers()], \
8080
f"target_layer_name {target_layer_name} does not exist in the given model, " \
81-
f"please check all valid layer names by [n for n, v in paddle_model.named_sublayers()]"
81+
f"please check all valid layer names by [n for n, v in model.named_sublayers()]"
8282

8383
if self._target_layer_name != target_layer_name:
8484
self._target_layer_name = target_layer_name
@@ -119,12 +119,12 @@ def _paddle_prepare(self, predict_fn=None):
119119
paddle.set_device(self.device)
120120
# to get gradients, the ``train`` mode must be set.
121121
# we cannot set v.training = False for the same reason.
122-
self.paddle_model.train()
122+
self.model.train()
123123

124124
def hook(layer, input, output):
125125
self._feature_maps[layer._layer_name_for_hook] = output
126126

127-
for n, v in self.paddle_model.named_sublayers():
127+
for n, v in self.model.named_sublayers():
128128
if n == self._target_layer_name:
129129
v._layer_name_for_hook = n
130130
v.register_forward_post_hook(hook)
@@ -137,7 +137,7 @@ def hook(layer, input, output):
137137
def predict_fn(data, label):
138138
data = paddle.to_tensor(data)
139139
data.stop_gradient = False
140-
out = self.paddle_model(data)
140+
out = self.model(data)
141141
out = paddle.nn.functional.softmax(out, axis=1)
142142
preds = paddle.argmax(out, axis=1)
143143
if label is None:

0 commit comments

Comments
 (0)