Skip to content
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

Fix device handling in tests #52

Merged
merged 6 commits into from
Jan 10, 2024
Merged

Fix device handling in tests #52

merged 6 commits into from
Jan 10, 2024

Conversation

dtch1997
Copy link
Owner

@dtch1997 dtch1997 commented Dec 23, 2023

Modifies ModelPatcher so that target activations are moved onto the appropriate device before performing operator.

closes #51

@@ -185,7 +185,8 @@ def _create_additive_hook(

def hook_fn(_m: Any, _inputs: Any, outputs: Any) -> Any:
original_tensor = untuple_tensor(outputs)
original_tensor[None] = operator(original_tensor, target_activation)
act = target_activation.to(original_tensor.device)
Copy link
Owner Author

@dtch1997 dtch1997 Dec 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting the device here allows us to skip setting the activation devices.

E.g.:

activations = {
    1: torch.randn(1, 512), # can be on cpu
}
... 
model_patcher.patch_activations(
    activations, layer_type=layer_type, operator="piecewise_addition"
)

@dtch1997 dtch1997 requested a review from chanind December 23, 2023 23:18
@dtch1997
Copy link
Owner Author

As of 16426e4, all the tests pass for me locally (with GPU enabled):

$ python -m pytest tests/util/test_model_patcher.py 
...
12 passed, 9 warnings in 15.54s

Copy link
Collaborator

@chanind chanind left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense, LGTM!

@dtch1997
Copy link
Owner Author

dtch1997 commented Jan 10, 2024

@chanind after merging in your latest changes from main, one of the tests isn't working any more.

Looking at the error logs, it seems to be in this code chunk:

def _create_additive_hook(
    target_activation: torch.Tensor, operator: PatchOperator
) -> Any:
    """Create a hook function that adds the given target_activation to the model output"""

    def hook_fn(_m: Any, _inputs: Any, outputs: Any) -> Any:
        original_tensor = untuple_tensor(outputs)
        act = target_activation.to(original_tensor.device) # This line raises an error
        original_tensor[None] = operator(original_tensor, act)
        return outputs

    return hook_fn

Looking at the CI logs here, this line fails:

act = target_activation.to(original_tensor.device)

with this error:

AttributeError: 'numpy.ndarray' object has no attribute 'to'

I.e. the target_activation seems to be a numpy array. Is this behaviour intended? The type hint indicates that target_activation should be a torch.Tensor

@chanind
Copy link
Collaborator

chanind commented Jan 10, 2024

Good catch! Fixed in #55

@dtch1997 dtch1997 merged commit f46940a into main Jan 10, 2024
2 checks passed
@dtch1997 dtch1997 deleted the model_patcher_device branch January 31, 2024 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ModelPatcher doesn't handle device correctly
2 participants