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

attempt fixing unit test version incompatibility #207

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
4 changes: 1 addition & 3 deletions .github/workflows/unit_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ jobs:
shell: bash -l {0}
run: |
set -eux
conda activate test
conda install pytorch torchaudio torchvision cpuonly -c pytorch-nightly
pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cpu
pip install -r requirements.txt
pip install -r dev-requirements.txt
pip install --no-build-isolation -e ".[dev]"
- name: Run unit tests with coverage
shell: bash -l {0}
run: |
set -eux
conda activate test
pytest --cov=. --cov-report xml tests -vv
- name: Upload Coverage to Codecov
uses: codecov/codecov-action@v2
Expand Down
4 changes: 3 additions & 1 deletion tests/metrics/aggregation/test_cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

# pyre-strict

from typing import List

import torch
from torcheval.metrics import Covariance
from torcheval.utils.test_utils.metric_class_tester import MetricClassTester


class TestCovariance(MetricClassTester):
def _test_covariance_with_input(self, batching: list[int]) -> None:
def _test_covariance_with_input(self, batching: List[int]) -> None:
gen = torch.Generator()
gen.manual_seed(3)
X = torch.randn(sum(batching), 4, generator=gen)
Expand Down
10 changes: 4 additions & 6 deletions torcheval/metrics/aggregation/cov.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@

# pyre-strict

from collections.abc import Iterable
from typing import Tuple, TypeVar
from typing import Iterable, Optional, Tuple, TypeVar, Union

import torch
from torcheval.metrics.metric import Metric
from typing_extensions import Self, TypeAlias

# TODO: use a NamedTuple?
_T = TypeVar("_T", bound=torch.Tensor | int)
_T = TypeVar("_T", bound=Union[torch.Tensor, int])
_Output: TypeAlias = Tuple[torch.Tensor, torch.Tensor] # mean, cov


class Covariance(Metric[_Output]):
"""Fit sample mean + covariance to empirical distribution"""

def __init__(self, *, device: torch.device | None = None) -> None:
def __init__(self, *, device: Optional[torch.device] = None) -> None:
super().__init__(device=device)
self.sum: torch.Tensor = self._add_state_and_return(
"sum", default=torch.as_tensor(0.0)
Expand All @@ -31,9 +30,8 @@ def __init__(self, *, device: torch.device | None = None) -> None:
)
self.n: int = self._add_state_and_return("n", default=0)

# pyre-fixme[31]: Expression `_T` is not a valid type.
def _add_state_and_return(self, name: str, default: _T) -> _T:
# Helper funcction for pyre
# Helper function for pyre
self._add_state(name, default)
return getattr(self, name)

Expand Down
Loading