Skip to content

Initialize parameterized test attributes #1551

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 3 additions & 11 deletions tests/attr/test_llm_attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,6 @@ def generate(
assert mock_response, "must mock response to use DummyLLM to generate"
response = self.tokenizer.encode(mock_response)[1:]
return torch.cat(
# pyre-fixme[6]: In call `torch._C._VariableFunctions.cat`,
# for 1st positional argument, expected `Union[List[Tensor],
# typing.Tuple[Tensor, ...]]` but got `List[Union[List[int], Tensor]]`.
[input_ids, torch.tensor([response], device=self.device)], # type: ignore
dim=1,
)
Expand Down Expand Up @@ -236,13 +233,9 @@ def device(self) -> torch.device:
else [("cpu", True), ("cpu", False)]
),
)
# pyre-fixme[13]: Attribute `device` is never initialized.
# pyre-fixme[13]: Attribute `use_cached_outputs` is never initialized.
class TestLLMAttr(BaseTest):
# pyre-fixme[13]: Attribute `device` is never initialized.
device: str
# pyre-fixme[13]: Attribute `use_cached_outputs` is never initialized.
use_cached_outputs: bool
device: Optional[str] = None
use_cached_outputs: bool = False

# pyre-fixme[56]: Pyre was not able to infer the type of argument `comprehension
@parameterized.expand(
Expand Down Expand Up @@ -509,8 +502,7 @@ def test_llm_attr_with_skip_tensor_target(self) -> None:
("device",), [("cpu",), ("cuda",)] if torch.cuda.is_available() else [("cpu",)]
)
class TestLLMGradAttr(BaseTest):
# pyre-fixme[13]: Attribute `device` is never initialized.
device: str
device: Optional[str] = None

@parameterized.expand(
[
Expand Down
6 changes: 2 additions & 4 deletions tests/attr/test_llm_attr_hf_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@
),
)
class TestLLMAttrHFCompatibility(BaseTest):
# pyre-fixme[13]: Attribute `device` is never initialized.
device: str
# pyre-fixme[13]: Attribute `use_cached_outputs` is never initialized.
use_cached_outputs: bool
device: Optional[str] = None
use_cached_outputs: bool = False

def setUp(self) -> None:
if not HAS_HF:
Expand Down
42 changes: 2 additions & 40 deletions tests/module/test_binary_concrete_stochastic_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# pyre-strict

import unittest
from typing import Optional

import torch
from captum.module.binary_concrete_stochastic_gates import BinaryConcreteStochasticGates
Expand All @@ -18,21 +19,16 @@
]
)
class TestBinaryConcreteStochasticGates(BaseTest):
# pyre-fixme[13]: Attribute `testing_device` is never initialized.
testing_device: str
testing_device: Optional[str] = None

def setUp(self) -> None:
super().setUp()
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
if self.testing_device == "cuda" and not torch.cuda.is_available():
raise unittest.SkipTest("Skipping GPU test since CUDA not available.")

def test_bcstg_1d_input(self) -> None:

dim = 3
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
bcstg = BinaryConcreteStochasticGates(dim).to(self.testing_device)
input_tensor = torch.tensor(
[
Expand All @@ -57,8 +53,6 @@ def test_bcstg_1d_input_with_reg_reduction(self) -> None:

dim = 3
mean_bcstg = BinaryConcreteStochasticGates(dim, reg_reduction="mean").to(
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
self.testing_device
)
none_bcstg = BinaryConcreteStochasticGates(dim, reg_reduction="none").to(
Expand All @@ -82,8 +76,6 @@ def test_bcstg_1d_input_with_reg_reduction(self) -> None:
def test_bcstg_1d_input_with_n_gates_error(self) -> None:

dim = 3
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
bcstg = BinaryConcreteStochasticGates(dim).to(self.testing_device)
input_tensor = torch.tensor([0.0, 0.1, 0.2]).to(self.testing_device)

Expand All @@ -95,14 +87,10 @@ def test_bcstg_num_mask_not_equal_dim_error(self) -> None:
mask = torch.tensor([0, 0, 1]) # only two distinct masks, but given dim is 3

with self.assertRaises(AssertionError):
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
BinaryConcreteStochasticGates(dim, mask=mask).to(self.testing_device)

def test_gates_values_matching_dim_when_eval(self) -> None:
dim = 3
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
bcstg = BinaryConcreteStochasticGates(dim).to(self.testing_device)
input_tensor = torch.tensor(
[
Expand All @@ -118,8 +106,6 @@ def test_gates_values_matching_dim_when_eval(self) -> None:
def test_bcstg_1d_input_with_mask(self) -> None:

dim = 2
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
mask = torch.tensor([0, 0, 1]).to(self.testing_device)
bcstg = BinaryConcreteStochasticGates(dim, mask=mask).to(self.testing_device)
input_tensor = torch.tensor(
Expand All @@ -144,8 +130,6 @@ def test_bcstg_1d_input_with_mask(self) -> None:
def test_bcstg_2d_input(self) -> None:

dim = 3 * 2
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
bcstg = BinaryConcreteStochasticGates(dim).to(self.testing_device)

# shape(2,3,2)
Expand Down Expand Up @@ -185,8 +169,6 @@ def test_bcstg_2d_input(self) -> None:
def test_bcstg_2d_input_with_n_gates_error(self) -> None:

dim = 5
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
bcstg = BinaryConcreteStochasticGates(dim).to(self.testing_device)
input_tensor = torch.tensor(
[
Expand All @@ -210,8 +192,6 @@ def test_bcstg_2d_input_with_mask(self) -> None:
[1, 1],
[0, 2],
]
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
).to(self.testing_device)
bcstg = BinaryConcreteStochasticGates(dim, mask=mask).to(self.testing_device)

Expand Down Expand Up @@ -252,8 +232,6 @@ def test_bcstg_2d_input_with_mask(self) -> None:
def test_get_gate_values_1d_input(self) -> None:

dim = 3
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
bcstg = BinaryConcreteStochasticGates(dim).to(self.testing_device)
input_tensor = torch.tensor(
[
Expand All @@ -273,8 +251,6 @@ def test_get_gate_values_1d_input_with_mask(self) -> None:

dim = 2
mask = torch.tensor([0, 1, 1])
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
bcstg = BinaryConcreteStochasticGates(dim, mask=mask).to(self.testing_device)
input_tensor = torch.tensor(
[
Expand All @@ -293,8 +269,6 @@ def test_get_gate_values_1d_input_with_mask(self) -> None:
def test_get_gate_values_2d_input(self) -> None:

dim = 3 * 2
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
bcstg = BinaryConcreteStochasticGates(dim).to(self.testing_device)

# shape(2,3,2)
Expand Down Expand Up @@ -326,8 +300,6 @@ def test_get_gate_values_clamp(self) -> None:
torch.tensor([10.0, -10.0, 10.0]),
lower_bound=-2,
upper_bound=2,
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
).to(self.testing_device)

clamped_gate_values = bcstg.get_gate_values().cpu().tolist()
Expand All @@ -350,8 +322,6 @@ def test_get_gate_values_2d_input_with_mask(self) -> None:
[0, 2],
]
)
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
bcstg = BinaryConcreteStochasticGates(dim, mask=mask).to(self.testing_device)

input_tensor = torch.tensor(
Expand Down Expand Up @@ -379,8 +349,6 @@ def test_get_gate_values_2d_input_with_mask(self) -> None:
def test_get_gate_active_probs_1d_input(self) -> None:

dim = 3
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
bcstg = BinaryConcreteStochasticGates(dim).to(self.testing_device)
input_tensor = torch.tensor(
[
Expand All @@ -402,8 +370,6 @@ def test_get_gate_active_probs_1d_input_with_mask(self) -> None:

dim = 2
mask = torch.tensor([0, 1, 1])
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
bcstg = BinaryConcreteStochasticGates(dim, mask=mask).to(self.testing_device)
input_tensor = torch.tensor(
[
Expand All @@ -424,8 +390,6 @@ def test_get_gate_active_probs_1d_input_with_mask(self) -> None:
def test_get_gate_active_probs_2d_input(self) -> None:

dim = 3 * 2
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
bcstg = BinaryConcreteStochasticGates(dim).to(self.testing_device)

# shape(2,3,2)
Expand Down Expand Up @@ -463,8 +427,6 @@ def test_get_gate_active_probs_2d_input_with_mask(self) -> None:
[0, 2],
]
)
# pyre-fixme[16]: `TestBinaryConcreteStochasticGates` has no attribute
# `testing_device`.
bcstg = BinaryConcreteStochasticGates(dim, mask=mask).to(self.testing_device)

input_tensor = torch.tensor(
Expand Down
Loading