Skip to content

add optional requirements to setuptools installation #4978

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
20 changes: 14 additions & 6 deletions .circleci/config.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions .circleci/config.yml.in
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,24 @@ commands:
editable:
type: boolean
default: true
optional_dependencies:
type: boolean
default: true
steps:
- pip_install:
args: --pre torch -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
descr: Install PyTorch from nightly releases
args: --pre torch -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html

- pip_install:
args: --no-build-isolation <<# parameters.editable >> --editable <</ parameters.editable >> .
descr: Install torchvision <<# parameters.editable >> in editable mode <</ parameters.editable >>
descr: >
Install torchvision
<<# parameters.editable >> in editable mode <</ parameters.editable >>
<<# parameters.optional_dependencies >> with optional dependencies <</ parameters.optional_dependencies >>
args: >
--no-build-isolation
<<# parameters.editable >> --editable <</ parameters.editable >>
.<<# parameters.optional_dependencies >>[all]<</ parameters.optional_dependencies >>


install_prototype_dependencies:
steps:
Expand Down Expand Up @@ -350,9 +361,6 @@ jobs:
| parallel -j0 'wget --no-verbose -O ~/.cache/torch/hub/checkpoints/`basename {}` {}\?source=ci'
- install_torchvision
- install_prototype_dependencies
- pip_install:
args: scipy pycocotools
descr: Install optional dependencies
- run:
name: Enable prototype tests
command: echo 'export PYTORCH_TEST_WITH_PROTOTYPE=1' >> $BASH_ENV
Expand Down
3 changes: 0 additions & 3 deletions .circleci/unittest/linux/scripts/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,3 @@ dependencies:
- ca-certificates
- pip:
- future
- pillow >=5.3.0, !=8.3.*
- scipy
- av
3 changes: 3 additions & 0 deletions .circleci/unittest/linux/scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@ if [ $PYTHON_VERSION == "3.6" ]; then
pip install "pillow>=5.3.0,!=8.3.*"
fi

printf "* Installing optional dependencies\n"
pip install -r optional-requirements.txt

printf "* Installing torchvision\n"
python setup.py develop
3 changes: 0 additions & 3 deletions .circleci/unittest/windows/scripts/environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,4 @@ dependencies:
- ca-certificates
- pip:
- future
- pillow >=5.3.0, !=8.3.*
- scipy
- av
- dataclasses
3 changes: 3 additions & 0 deletions .circleci/unittest/windows/scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@ fi

source "$this_dir/set_cuda_envs.sh"

printf "* Installing optional dependencies\n"
pip install -r optional-requirements.txt

printf "* Installing torchvision\n"
"$this_dir/vc_env_helper.bat" python setup.py develop
8 changes: 8 additions & 0 deletions optional-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# scipy
scipy
Comment on lines +1 to +2
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is only kept for BC, because previously pip install torchvision[scipy] would also install scipy. Happy to remove it in case we don't want to keep BC here.

# io
av
# datasets
scipy
lmdb
pycocotools
30 changes: 27 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import subprocess
import sys
from collections import defaultdict

import torch
from pkg_resources import parse_version, get_distribution, DistributionNotFound
Expand Down Expand Up @@ -444,6 +445,31 @@ def run(self):
distutils.command.clean.clean.run(self)


def get_optional_requirements(file_name="optional-requirements.txt"):
extras_require = defaultdict(lambda: set())
with open(os.path.join(cwd, file_name)) as file:
extra = None
for line in file:
if line.startswith("#"):
extra = line[1:].strip()
continue

extras_require[extra].add(line.strip())

unknown_extra = extras_require.pop(None, None)
if unknown_extra:
raise RuntimeError(
f"Requirement(s) {sorted(unknown_extra)} do not belong to any extra. "
f"Please place them in a existing extra or create new one by adding a comment with the name of the extra."
)

extras_require = {extra: sorted(requirements) for extra, requirements in extras_require.items()}
extras_require["all"] = sorted(
{requirement for requirements in extras_require.values() for requirement in requirements}
)
return extras_require


if __name__ == "__main__":
print(f"Building wheel {package_name}-{version}")

Expand All @@ -467,9 +493,7 @@ def run(self):
package_data={package_name: ["*.dll", "*.dylib", "*.so", "prototype/datasets/_builtin/*.categories"]},
zip_safe=False,
install_requires=requirements,
extras_require={
"scipy": ["scipy"],
},
extras_require=get_optional_requirements(),
ext_modules=get_extensions(),
python_requires=">=3.6",
cmdclass={
Expand Down
29 changes: 29 additions & 0 deletions test/test_prototype_builtin_datasets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import pathlib

import builtin_dataset_mocks
import pytest
Expand Down Expand Up @@ -51,6 +52,34 @@ def dataset_parametrization(*names, decoder=to_bytes):
)


def test_optional_dependencies():
required_dependencies = set()
for name in datasets.list():
required_dependencies.update(datasets.info(name).dependecies)

with open(pathlib.Path(__file__).parents[1] / "optional-requirements.txt") as file:
for line in file:
if line.startswith("#") and line[1:].strip() == "datasets":
break

registered_dependencies = set()
for line in file:
if line.startswith("#"):
break

registered_dependencies.add(line.strip())

# TODO: Until we have feature parity between the stable and prototype datasets, we check if the registered
# dependencies are a superset of the requires ones. Afterwards we should check for equality.
missing_dependencies = required_dependencies - registered_dependencies
if missing_dependencies:
raise AssertionError(
f"The datasets depend on the third-party packages {sequence_to_str(sorted(missing_dependencies))}, "
f"but they are not listed in the '# datasets' section in the file $TORCHVISION/optional-requirements.txt. "
"Please add them."
)


class TestCommon:
@dataset_parametrization()
def test_smoke(self, dataset, mock_info):
Expand Down