-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattr_config.py
More file actions
70 lines (60 loc) · 2.29 KB
/
attr_config.py
File metadata and controls
70 lines (60 loc) · 2.29 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
from collections.abc import Callable
from typing import Any, cast
from captum.attr import Attribution
from captum._utils.typing import Module, TensorOrTupleOfTensorsGeneric, TargetType
_Callback = Callable[[TensorOrTupleOfTensorsGeneric], TensorOrTupleOfTensorsGeneric]
_RuntimeKwargsFn = Callable[
[TensorOrTupleOfTensorsGeneric, TargetType],
dict[str, Any],
]
class AttributionConfig:
def __init__(
self,
attribution_class: type[Attribution],
callback: _Callback | None = None,
runtime_kwargs_fn: _RuntimeKwargsFn | None = None,
suffix: str | None = None,
**kwargs: Any,
) -> None:
self.attribution_class = attribution_class
self.config = kwargs
self.callback: _Callback = callback if callback is not None else cast(_Callback, lambda x: x)
self.suffix = suffix
self.runtime_kwargs_fn = runtime_kwargs_fn
self.layer = self.config.pop("layer", None)
self._attributor: Attribution | None = None
self._bound_model_id: int | None = None
def _get_attributor(self, model: Module) -> Attribution:
mid = id(model)
if self._attributor is None or self._bound_model_id != mid:
self._attributor = self.attribution_class(
model,
**({"layer": self.layer} if self.layer else {}),
)
self._bound_model_id = mid
return self._attributor
def attribute(
self,
model: Module,
inputs: TensorOrTupleOfTensorsGeneric,
target: TargetType,
) -> TensorOrTupleOfTensorsGeneric:
attributor = self._get_attributor(model)
runtime_kwargs = (
self.runtime_kwargs_fn(inputs, target)
if self.runtime_kwargs_fn is not None
else {}
)
return self.callback(
attributor.attribute(
inputs=inputs,
target=target,
**self.config,
**runtime_kwargs,
)
)
def __str__(self) -> str:
suffix = f" {self.suffix}" if self.suffix else ""
return f"{self.attribution_class.__name__}{suffix}"
def __repr__(self) -> str:
return f"AttributionConfig(attribution_class={self.attribution_class}, config={self.config}, suffix={self.suffix})"