Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/pip/sphinx-lt-9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
samet-akcay committed Aug 20, 2024
2 parents 3c92360 + 2bd2842 commit c37e415
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 112 deletions.
35 changes: 32 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,46 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Changed

- 🔨 Replace "./dtasets/BTech" to "./dtasets/BTech"
### Deprecated

### Fixed

### New Contributors

**Full Changelog**:

## [v1.1.1]

### Added

- 📚Ppipelines how-to guide by @ashwinvaidya17 in https://github.com/openvinotoolkit/anomalib/pull/2109

### Changed

- Set permissions for github workflows by @djdameln in https://github.com/openvinotoolkit/anomalib/pull/2127
- Update timm requirement from <=1.0.3,>=0.5.4 to >=0.5.4,<=1.0.7 by @dependabot in https://github.com/openvinotoolkit/anomalib/pull/2151
- 🚀 Use gh actions runners for pre-commit checks by @ashwinvaidya17 in https://github.com/openvinotoolkit/anomalib/pull/2160
- Bump AlexanderDokuchaev/md-dead-link-check from 0.8 to 0.9 by @dependabot in https://github.com/openvinotoolkit/anomalib/pull/2162
- Added accuracy control quantization by @adrianboguszewski in https://github.com/openvinotoolkit/anomalib/pull/2070

### Deprecated

- 🔨Remove device postfix by @ashwinvaidya17 in https://github.com/openvinotoolkit/anomalib/pull/2233

### Fixed

- 🔨 Fix uncorrect download path of btech dataset
- Fix: get MLFLOW_TRACKING_UTI from env variables as default by @CarlosNacher in https://github.com/openvinotoolkit/anomalib/pull/2107
- Fix normalization by @alexriedel1 in https://github.com/openvinotoolkit/anomalib/pull/2130
- Fix image-level heatmap normalization by @djdameln in https://github.com/openvinotoolkit/anomalib/pull/2131
- Fix: efficient ad model_size str fixes by @Gornoka in https://github.com/openvinotoolkit/anomalib/pull/2159
- Fix the CI by @samet-akcay in https://github.com/openvinotoolkit/anomalib/pull/2178
- Fix BTech Dataset by @samet-akcay in https://github.com/openvinotoolkit/anomalib/pull/2180

### New Contributors

**Full Changelog**:
- @CarlosNacher made their first contribution in https://github.com/openvinotoolkit/anomalib/pull/2107

**Full Changelog**: https://github.com/openvinotoolkit/anomalib/compare/v1.1.0...v1.1.1

## [v1.1.0]

Expand Down
85 changes: 2 additions & 83 deletions src/anomalib/cli/utils/installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,81 +276,6 @@ def get_hardware_suffix(with_available_torch_build: bool = False, torch_version:
return hardware_suffix


def add_hardware_suffix_to_torch(
requirement: Requirement,
hardware_suffix: str | None = None,
with_available_torch_build: bool = False,
) -> str:
"""Add hardware suffix to the torch requirement.
Args:
requirement (Requirement): Requirement object comprising requirement
details.
hardware_suffix (str | None): Hardware suffix. If None, it will be set
to the correct hardware suffix. Defaults to None.
with_available_torch_build (bool): To check whether the installed
CUDA version is supported by the latest available PyTorch build.
Defaults to False.
Examples:
>>> from pkg_resources import Requirement
>>> req = "torch>=1.13.0, <=2.0.1"
>>> requirement = Requirement.parse(req)
>>> requirement.name, requirement.specs
('torch', [('>=', '1.13.0'), ('<=', '2.0.1')])
>>> add_hardware_suffix_to_torch(requirement)
'torch>=1.13.0+cu121, <=2.0.1+cu121'
``with_available_torch_build=True`` will use the latest available PyTorch build.
>>> req = "torch==2.0.1"
>>> requirement = Requirement.parse(req)
>>> add_hardware_suffix_to_torch(requirement, with_available_torch_build=True)
'torch==2.0.1+cu118'
It is possible to pass the ``hardware_suffix`` manually.
>>> req = "torch==2.0.1"
>>> requirement = Requirement.parse(req)
>>> add_hardware_suffix_to_torch(requirement, hardware_suffix="cu121")
'torch==2.0.1+cu111'
Raises:
ValueError: When the requirement has more than two version criterion.
Returns:
str: Updated torch package with the right cuda suffix.
"""
name = requirement.unsafe_name
updated_specs: list[str] = []

for operator, version in requirement.specs:
hardware_suffix = hardware_suffix or get_hardware_suffix(with_available_torch_build, version)
updated_version = version + f"+{hardware_suffix}" if not version.startswith(("2.1", "2.2")) else version

# ``specs`` contains operators and versions as follows:
# These are to be concatenated again for the updated version.
updated_specs.append(operator + updated_version)

updated_requirement: str = ""

if updated_specs:
# This is the case when specs are e.g. ['<=1.9.1+cu111']
if len(updated_specs) == 1:
updated_requirement = name + updated_specs[0]
# This is the case when specs are e.g., ['<=1.9.1+cu111', '>=1.8.1+cu111']
elif len(updated_specs) == 2:
updated_requirement = name + updated_specs[0] + ", " + updated_specs[1]
else:
msg = (
"Requirement version can be a single value or a range. \n"
"For example it could be torch>=1.8.1 "
"or torch>=1.8.1, <=1.9.1\n"
f"Got {updated_specs} instead."
)
raise ValueError(msg)
return updated_requirement


def get_torch_install_args(requirement: str | Requirement) -> list[str]:
"""Get the install arguments for Torch requirement.
Expand All @@ -368,7 +293,7 @@ def get_torch_install_args(requirement: str | Requirement) -> list[str]:
>>> requriment = "torch>=1.13.0"
>>> get_torch_install_args(requirement)
['--extra-index-url', 'https://download.pytorch.org/whl/cpu',
'torch==1.13.0+cpu', 'torchvision==0.14.0+cpu']
'torch>=1.13.0', 'torchvision==0.14.0']
Returns:
list[str]: The install arguments.
Expand Down Expand Up @@ -401,21 +326,15 @@ def get_torch_install_args(requirement: str | Requirement) -> list[str]:
# Create the PyTorch Index URL to download the correct wheel.
index_url = f"https://download.pytorch.org/whl/{hardware_suffix}"

# Create the PyTorch version depending on the CUDA version. For example,
# If CUDA version is 11.2, then the PyTorch version is 1.8.0+cu112.
# If CUDA version is None, then the PyTorch version is 1.8.0+cpu.
torch_version = add_hardware_suffix_to_torch(requirement, hardware_suffix, with_available_torch_build=True)
torch_version = f"{requirement.name}{operator}{version}" # eg: torch==1.13.0

# Get the torchvision version depending on the torch version.
torchvision_version = AVAILABLE_TORCH_VERSIONS[version]["torchvision"]
torchvision_requirement = f"torchvision{operator}{torchvision_version}"
if isinstance(torchvision_version, str) and not torchvision_version.startswith("0.16"):
torchvision_requirement += f"+{hardware_suffix}"

# Return the install arguments.
install_args += [
"--extra-index-url",
# "--index-url",
index_url,
torch_version,
torchvision_requirement,
Expand Down
28 changes: 2 additions & 26 deletions tests/unit/cli/test_installation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from pytest_mock import MockerFixture

from anomalib.cli.utils.installation import (
add_hardware_suffix_to_torch,
get_cuda_suffix,
get_cuda_version,
get_hardware_suffix,
Expand Down Expand Up @@ -122,29 +121,6 @@ def test_get_hardware_suffix(mocker: MockerFixture) -> None:
assert get_hardware_suffix() == "cpu"


def test_add_hardware_suffix_to_torch(mocker: MockerFixture) -> None:
"""Test that add_hardware_suffix_to_torch returns the expected updated requirement."""
mocker.patch("anomalib.cli.utils.installation.get_hardware_suffix", return_value="cu121")
requirement = Requirement.parse("torch>=1.13.0, <=2.0.1")
updated_requirement = add_hardware_suffix_to_torch(requirement)
assert "torch" in updated_requirement
assert ">=1.13.0+cu121" in updated_requirement
assert "<=2.0.1+cu121" in updated_requirement

requirement = Requirement.parse("torch==2.0.1")
mocker.patch("anomalib.cli.utils.installation.get_hardware_suffix", return_value="cu118")
updated_requirement = add_hardware_suffix_to_torch(requirement, with_available_torch_build=True)
assert updated_requirement == "torch==2.0.1+cu118"

requirement = Requirement.parse("torch==2.0.1")
updated_requirement = add_hardware_suffix_to_torch(requirement, hardware_suffix="cu111")
assert updated_requirement == "torch==2.0.1+cu111"

requirement = Requirement.parse("torch>=1.13.0, <=2.0.1, !=1.14.0")
with pytest.raises(ValueError, match="Requirement version can be a single value or a range."):
add_hardware_suffix_to_torch(requirement)


def test_get_torch_install_args(mocker: MockerFixture) -> None:
"""Test that get_torch_install_args returns the expected install arguments."""
requirement = Requirement.parse("torch>=2.1.1")
Expand Down Expand Up @@ -174,8 +150,8 @@ def test_get_torch_install_args(mocker: MockerFixture) -> None:
expected_args = [
"--extra-index-url",
"https://download.pytorch.org/whl/cu111",
"torch==2.0.1+cu111",
"torchvision==0.15.2+cu111",
"torch==2.0.1",
"torchvision==0.15.2",
]
install_args = get_torch_install_args(requirement)
for arg in expected_args:
Expand Down

0 comments on commit c37e415

Please sign in to comment.