Skip to content

Use Python properties to cleanly apply rules to checkpoints #1249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions captum/influence/_core/tracincp.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,9 @@ def __init__(
Default: None
"""

self.model = model
self.model: Module = model

if isinstance(checkpoints, str):
self.checkpoints = AV.sort_files(glob.glob(join(checkpoints, "*")))
elif isinstance(checkpoints, List) and isinstance(checkpoints[0], str):
self.checkpoints = AV.sort_files(checkpoints)
else:
self.checkpoints = list(checkpoints) # cast to avoid mypy error
if isinstance(self.checkpoints, List):
assert len(self.checkpoints) > 0, "No checkpoints saved!"
self.checkpoints = checkpoints # type: ignore

self.checkpoints_load_func = checkpoints_load_func
self.loss_fn = loss_fn
Expand Down Expand Up @@ -181,6 +174,24 @@ def __init__(
"percentage completion of the computation, nor any time estimates."
)

@property
def checkpoints(self) -> List[str]:
return self._checkpoints

@checkpoints.setter
def checkpoints(self, checkpoints: Union[str, List[str], Iterator]) -> None:
if isinstance(checkpoints, str):
self._checkpoints = AV.sort_files(glob.glob(join(checkpoints, "*")))
elif isinstance(checkpoints, List) and isinstance(checkpoints[0], str):
self._checkpoints = AV.sort_files(checkpoints)
else:
self._checkpoints = list(checkpoints) # cast to avoid mypy error

if len(self._checkpoints) <= 0:
raise ValueError(
f"Invalid checkpoints provided for TracIn class: {checkpoints}!"
)

@abstractmethod
def self_influence(
self,
Expand Down
4 changes: 2 additions & 2 deletions captum/influence/_core/tracincp_fast_rand_proj.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class TracInCPFast(TracInCPBase):
def __init__(
self,
model: Module,
final_fc_layer: Module,
final_fc_layer: Union[Module, str],
train_dataset: Union[Dataset, DataLoader],
checkpoints: Union[str, List[str], Iterator],
checkpoints_load_func: Callable = _load_flexible_state_dict,
Expand Down Expand Up @@ -183,7 +183,7 @@ def __init__(
self.vectorize = vectorize

# TODO: restore prior state
self.final_fc_layer = final_fc_layer
self.final_fc_layer = final_fc_layer # type: ignore
for param in self.final_fc_layer.parameters():
param.requires_grad = True

Expand Down
37 changes: 36 additions & 1 deletion tests/influence/_core/test_tracin_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class TestTracinValidator(BaseTest):
)
def test_tracin_require_inputs_dataset(
self,
reduction,
reduction: str,
tracin_constructor: Callable,
) -> None:
"""
Expand Down Expand Up @@ -64,6 +64,10 @@ def test_tracin_require_inputs_dataset(
tracin.influence(None, k=None)

def test_tracincp_fast_rand_proj_inputs(self) -> None:
"""
This test verifies that TracInCPFast should be initialized
with a valid `final_fc_layer`.
"""
with tempfile.TemporaryDirectory() as tmpdir:
(
net,
Expand All @@ -83,3 +87,34 @@ def test_tracincp_fast_rand_proj_inputs(self) -> None:
loss_fn=nn.MSELoss(),
batch_size=1,
)

@parameterized.expand(
param_list,
name_func=build_test_name_func(),
)
def test_tracincp_input_checkpoints(
self, reduction: str, tracin_constructor: Callable
) -> None:
"""
This test verifies that tracinCP and tracinCPFast
class should be initialized with valid `checkpoints`.
"""
with tempfile.TemporaryDirectory() as invalid_tmpdir:
with tempfile.TemporaryDirectory() as tmpdir:
(
net,
train_dataset,
test_samples,
test_labels,
) = get_random_model_and_data(tmpdir, unpack_inputs=False)

with self.assertRaisesRegex(
ValueError, "Invalid checkpoints provided for TracIn class: "
):
tracin_constructor(
net,
train_dataset,
invalid_tmpdir,
loss_fn=nn.MSELoss(),
batch_size=1,
)