Skip to content

Adding test for output that is a tensor of integers. Updating passthrough layer. #1526

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 2 commits into from
Closed
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
32 changes: 21 additions & 11 deletions captum/testing/helpers/basic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# pyre-strict

from typing import no_type_check, Optional, Tuple, Union
from typing import Dict, no_type_check, Optional, Tuple, Union

import torch
import torch.nn as nn
Expand Down Expand Up @@ -418,7 +418,7 @@ def forward(self, input1, input2, input3=None):
return self.linear2(self.relu(self.linear1(embeddings))).sum(1)


class GradientUnsupportedLayerOutput(nn.Module):
class PassThroughLayerOutput(nn.Module):
"""
This layer is used to test the case where the model returns a layer that
is not supported by the gradient computation.
Expand All @@ -428,10 +428,8 @@ def __init__(self) -> None:
super().__init__()

@no_type_check
def forward(
self, unsupported_layer_output: PassThroughOutputType
) -> PassThroughOutputType:
return unsupported_layer_output
def forward(self, output: PassThroughOutputType) -> PassThroughOutputType:
return output


class BasicModel_GradientLayerAttribution(nn.Module):
Expand All @@ -456,7 +454,7 @@ def __init__(

self.relu = nn.ReLU(inplace=inplace)
self.relu_alt = nn.ReLU(inplace=False)
self.unsupportedLayer = GradientUnsupportedLayerOutput()
self.unsupported_layer = PassThroughLayerOutput()

self.linear2 = nn.Linear(4, 2)
self.linear2.weight = nn.Parameter(torch.ones(2, 4))
Expand All @@ -466,15 +464,19 @@ def __init__(
self.linear3.weight = nn.Parameter(torch.ones(2, 4))
self.linear3.bias = nn.Parameter(torch.tensor([-1.0, 1.0]))

self.int_layer = PassThroughLayerOutput() # sample layer with an int ouput

@no_type_check
def forward(self, x: Tensor, add_input: Optional[Tensor] = None) -> Tensor:
def forward(
self, x: Tensor, add_input: Optional[Tensor] = None
) -> Dict[str, Tensor]:
input = x if add_input is None else x + add_input
lin0_out = self.linear0(input)
lin1_out = self.linear1(lin0_out)
lin1_out_alt = self.linear1_alt(lin0_out)

if self.unsupported_layer_output is not None:
self.unsupportedLayer(self.unsupported_layer_output)
self.unsupported_layer(self.unsupported_layer_output)
# unsupportedLayer is unused in the forward func.
self.relu_alt(
lin1_out_alt
Expand All @@ -483,9 +485,17 @@ def forward(self, x: Tensor, add_input: Optional[Tensor] = None) -> Tensor:
relu_out = self.relu(lin1_out)
lin2_out = self.linear2(relu_out)

lin3_out = self.linear3(lin1_out_alt).to(torch.int64)
lin3_out = self.linear3(lin1_out_alt)
int_output = self.int_layer(lin3_out.to(torch.int64))

output_tensors = torch.cat((lin2_out, int_output), dim=1)

return torch.cat((lin2_out, lin3_out), dim=1)
# we return a dictionary of tensors as an output to test the case
# where an output accessor is required
return {
"task {}".format(i + 1): output_tensors[:, i]
for i in range(output_tensors.shape[1])
}


class MultiRelu(nn.Module):
Expand Down