From 263cf43ccdda1019ffa60e2bbf7a60b6bef3b44b Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Thu, 15 Jun 2023 12:06:06 +0100 Subject: [PATCH 01/12] Use turbo --- .github/scripts/setup-env.sh | 3 ++- packaging/pre_build_script.sh | 4 ++-- packaging/torchvision/meta.yaml | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.github/scripts/setup-env.sh b/.github/scripts/setup-env.sh index d102735909e..b47902838f3 100755 --- a/.github/scripts/setup-env.sh +++ b/.github/scripts/setup-env.sh @@ -40,9 +40,10 @@ conda create \ --quiet --yes \ python="${PYTHON_VERSION}" pip \ ninja cmake \ - libpng jpeg \ + libpng \ 'ffmpeg<4.3' conda activate ci +conda install libjpeg-turbo -c pytorch -y pip install --progress-bar=off --upgrade setuptools # See https://github.com/pytorch/vision/issues/6790 diff --git a/packaging/pre_build_script.sh b/packaging/pre_build_script.sh index 9d10738cfa8..43f60e51064 100644 --- a/packaging/pre_build_script.sh +++ b/packaging/pre_build_script.sh @@ -13,8 +13,8 @@ fi if [[ "$(uname)" == Darwin || "$OSTYPE" == "msys" ]]; then # Install libpng from Anaconda (defaults) - conda install libpng "jpeg<=9b" -yq - conda install -yq ffmpeg=4.2 -c pytorch + conda install libpng -yq + conda install -yq ffmpeg=4.2 libjpeg-turbo -c pytorch # Copy binaries to be included in the wheel distribution if [[ "$OSTYPE" == "msys" ]]; then diff --git a/packaging/torchvision/meta.yaml b/packaging/torchvision/meta.yaml index c75b37f947a..9adc13b558b 100644 --- a/packaging/torchvision/meta.yaml +++ b/packaging/torchvision/meta.yaml @@ -10,7 +10,7 @@ requirements: build: - {{ compiler('c') }} # [win] - libpng - - jpeg + - libjpeg-turbo # NOTE: The only ffmpeg version that we build is actually 4.2 - ffmpeg >=4.2 # [not win] @@ -28,7 +28,7 @@ requirements: - requests - libpng - ffmpeg >=4.2 # [not win] - - jpeg + - libjpeg-turbo - pillow >=5.3.0, !=8.3.* - pytorch-mutex 1.0 {{ build_variant }} # [not osx ] {{ environ.get('CONDA_PYTORCH_CONSTRAINT') }} @@ -62,7 +62,7 @@ test: requires: - pytest - scipy - - jpeg + - libjpeg-turbo - ca-certificates From 9917b49dfcfffea4b8aa8522559e59b9717e21b7 Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Mon, 19 Jun 2023 09:30:30 +0200 Subject: [PATCH 02/12] fix installation in setup-env.sh script --- .github/scripts/setup-env.sh | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/scripts/setup-env.sh b/.github/scripts/setup-env.sh index b47902838f3..8e6f5d4dcfa 100755 --- a/.github/scripts/setup-env.sh +++ b/.github/scripts/setup-env.sh @@ -34,16 +34,17 @@ if [[ "${OS_TYPE}" == "macos" && $(uname -m) == x86_64 ]]; then fi echo '::group::Create build environment' -# See https://github.com/pytorch/vision/issues/7296 for ffmpeg +# 1. `libjpeg-turbo` is only available for macOS and Windows on the `defaults` conda channel. Thus, we need to pull it +# in from the `pytorch-nightly` channel on Linux +# 2. See https://github.com/pytorch/vision/issues/7296 for ffmpeg conda create \ --name ci \ --quiet --yes \ + --channel=defaults --channel=pytorch-nightly python="${PYTHON_VERSION}" pip \ ninja cmake \ - libpng \ + libpng libjpeg-turbo \ 'ffmpeg<4.3' -conda activate ci -conda install libjpeg-turbo -c pytorch -y pip install --progress-bar=off --upgrade setuptools # See https://github.com/pytorch/vision/issues/6790 From 3ee8f611e7d95adaeb5dc89291d192fde235eef7 Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Mon, 19 Jun 2023 10:27:53 +0200 Subject: [PATCH 03/12] fix --- .github/scripts/setup-env.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/scripts/setup-env.sh b/.github/scripts/setup-env.sh index 8e6f5d4dcfa..6990220e359 100755 --- a/.github/scripts/setup-env.sh +++ b/.github/scripts/setup-env.sh @@ -40,7 +40,7 @@ echo '::group::Create build environment' conda create \ --name ci \ --quiet --yes \ - --channel=defaults --channel=pytorch-nightly + --channel=defaults --channel=pytorch-nightly \ python="${PYTHON_VERSION}" pip \ ninja cmake \ libpng libjpeg-turbo \ From fd39e13437c69f8741523c44071ebcd0cc1e4aac Mon Sep 17 00:00:00 2001 From: Philip Meier Date: Mon, 19 Jun 2023 10:40:08 +0200 Subject: [PATCH 04/12] another fix --- .github/scripts/setup-env.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/scripts/setup-env.sh b/.github/scripts/setup-env.sh index 6990220e359..51f80d5b89a 100755 --- a/.github/scripts/setup-env.sh +++ b/.github/scripts/setup-env.sh @@ -45,6 +45,7 @@ conda create \ ninja cmake \ libpng libjpeg-turbo \ 'ffmpeg<4.3' +conda activate ci pip install --progress-bar=off --upgrade setuptools # See https://github.com/pytorch/vision/issues/6790 From f4a3c2d35dacee7c2029e7512efe32aaf70fcc18 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Mon, 19 Jun 2023 12:30:44 +0100 Subject: [PATCH 05/12] Revert "another fix" This reverts commit fd39e13437c69f8741523c44071ebcd0cc1e4aac. --- .github/scripts/setup-env.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/scripts/setup-env.sh b/.github/scripts/setup-env.sh index 51f80d5b89a..6990220e359 100755 --- a/.github/scripts/setup-env.sh +++ b/.github/scripts/setup-env.sh @@ -45,7 +45,6 @@ conda create \ ninja cmake \ libpng libjpeg-turbo \ 'ffmpeg<4.3' -conda activate ci pip install --progress-bar=off --upgrade setuptools # See https://github.com/pytorch/vision/issues/6790 From d02d2c87c7712bc5d8986e1fd18f41eb03cd8a62 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Mon, 19 Jun 2023 12:30:53 +0100 Subject: [PATCH 06/12] Revert "fix" This reverts commit 3ee8f611e7d95adaeb5dc89291d192fde235eef7. --- .github/scripts/setup-env.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/scripts/setup-env.sh b/.github/scripts/setup-env.sh index 6990220e359..8e6f5d4dcfa 100755 --- a/.github/scripts/setup-env.sh +++ b/.github/scripts/setup-env.sh @@ -40,7 +40,7 @@ echo '::group::Create build environment' conda create \ --name ci \ --quiet --yes \ - --channel=defaults --channel=pytorch-nightly \ + --channel=defaults --channel=pytorch-nightly python="${PYTHON_VERSION}" pip \ ninja cmake \ libpng libjpeg-turbo \ From 421c7a7d4a69ef23c72db732133b51b6e7a79c07 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Mon, 19 Jun 2023 12:31:02 +0100 Subject: [PATCH 07/12] Revert "fix installation in setup-env.sh script" This reverts commit 9917b49dfcfffea4b8aa8522559e59b9717e21b7. --- .github/scripts/setup-env.sh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/scripts/setup-env.sh b/.github/scripts/setup-env.sh index 8e6f5d4dcfa..b47902838f3 100755 --- a/.github/scripts/setup-env.sh +++ b/.github/scripts/setup-env.sh @@ -34,17 +34,16 @@ if [[ "${OS_TYPE}" == "macos" && $(uname -m) == x86_64 ]]; then fi echo '::group::Create build environment' -# 1. `libjpeg-turbo` is only available for macOS and Windows on the `defaults` conda channel. Thus, we need to pull it -# in from the `pytorch-nightly` channel on Linux -# 2. See https://github.com/pytorch/vision/issues/7296 for ffmpeg +# See https://github.com/pytorch/vision/issues/7296 for ffmpeg conda create \ --name ci \ --quiet --yes \ - --channel=defaults --channel=pytorch-nightly python="${PYTHON_VERSION}" pip \ ninja cmake \ - libpng libjpeg-turbo \ + libpng \ 'ffmpeg<4.3' +conda activate ci +conda install libjpeg-turbo -c pytorch -y pip install --progress-bar=off --upgrade setuptools # See https://github.com/pytorch/vision/issues/6790 From 7efe97c8aa373f7f44f87c483c1a492ed1128495 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Thu, 3 Aug 2023 16:49:09 +0100 Subject: [PATCH 08/12] Some hacks --- .github/scripts/unittest.sh | 3 ++- test/test_image.py | 6 ++++++ torchvision/csrc/io/image/image.cpp | 25 ++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/.github/scripts/unittest.sh b/.github/scripts/unittest.sh index 2a0b7154200..f1b6d9ee2a9 100755 --- a/.github/scripts/unittest.sh +++ b/.github/scripts/unittest.sh @@ -11,4 +11,5 @@ echo '::group::Install testing utilities' pip install --progress-bar=off pytest pytest-mock pytest-cov echo '::endgroup::' -pytest --junit-xml="${RUNNER_TEST_RESULTS_DIR}/test-results.xml" -v --durations=25 +# pytest --junit-xml="${RUNNER_TEST_RESULTS_DIR}/test-results.xml" -v --durations=25 +pytest test/test_image.py -k turbo --junit-xml="${RUNNER_TEST_RESULTS_DIR}/test-results.xml" -v -s --durations=25 diff --git a/test/test_image.py b/test/test_image.py index b08dc2026d4..e9c96e72bda 100644 --- a/test/test_image.py +++ b/test/test_image.py @@ -37,6 +37,12 @@ PILLOW_VERSION = tuple(int(x) for x in PILLOW_VERSION.split(".")) +def test_turbo(): + print() + print(f"{torch.ops.image.jpeg_version() = }") + assert torch.ops.image.is_turbo() + + def _get_safe_image_name(name): # Used when we need to change the pytest "id" for an "image path" parameter. # If we don't, the test id (i.e. its name) will contain the whole path to the image, which is machine-specific, diff --git a/torchvision/csrc/io/image/image.cpp b/torchvision/csrc/io/image/image.cpp index 3c9d632f030..1e456ba5d6c 100644 --- a/torchvision/csrc/io/image/image.cpp +++ b/torchvision/csrc/io/image/image.cpp @@ -19,6 +19,26 @@ PyMODINIT_FUNC PyInit_image(void) { namespace vision { namespace image { +#ifdef JPEG_FOUND + #include +#endif + +int64_t jpeg_version() { +#ifdef JPEG_FOUND + return JPEG_LIB_VERSION; +#else + return -1; +#endif +} + +int64_t is_turbo() { +#ifdef LIBJPEG_TURBO_VERSION + return 1; +#else + return 0; +#endif +} + static auto registry = torch::RegisterOperators() .op("image::decode_png", &decode_png) .op("image::encode_png", &encode_png) @@ -27,7 +47,10 @@ static auto registry = torch::RegisterOperators() .op("image::read_file", &read_file) .op("image::write_file", &write_file) .op("image::decode_image", &decode_image) - .op("image::decode_jpeg_cuda", &decode_jpeg_cuda); + .op("image::decode_jpeg_cuda", &decode_jpeg_cuda) + .op("image::jpeg_version", &jpeg_version) + .op("image::is_turbo", &is_turbo); } // namespace image } // namespace vision + From b391a6ab923ea9b9fcee92c29ad244e1e4e01df3 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Thu, 3 Aug 2023 17:03:12 +0100 Subject: [PATCH 09/12] test all of test_image.py? --- .github/scripts/unittest.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/scripts/unittest.sh b/.github/scripts/unittest.sh index f1b6d9ee2a9..4b4d6a54379 100755 --- a/.github/scripts/unittest.sh +++ b/.github/scripts/unittest.sh @@ -12,4 +12,4 @@ pip install --progress-bar=off pytest pytest-mock pytest-cov echo '::endgroup::' # pytest --junit-xml="${RUNNER_TEST_RESULTS_DIR}/test-results.xml" -v --durations=25 -pytest test/test_image.py -k turbo --junit-xml="${RUNNER_TEST_RESULTS_DIR}/test-results.xml" -v -s --durations=25 +pytest test/test_image.py --junit-xml="${RUNNER_TEST_RESULTS_DIR}/test-results.xml" -v -s --durations=25 From 4fdc43f0a28767bb87008cdbe54e187b3d932923 Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Thu, 3 Aug 2023 17:20:06 +0100 Subject: [PATCH 10/12] Add to smoke test --- test/smoke_test.py | 2 ++ test/test_image.py | 4 ++-- torchvision/csrc/io/image/image.cpp | 34 ++++++++++++++--------------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/test/smoke_test.py b/test/smoke_test.py index 8037183e122..a157f1c9150 100644 --- a/test/smoke_test.py +++ b/test/smoke_test.py @@ -78,6 +78,8 @@ def smoke_test_torchvision_resnet50_classify(device: str = "cpu") -> None: def main() -> None: print(f"torchvision: {torchvision.__version__}") print(f"torch.cuda.is_available: {torch.cuda.is_available()}") + print(f"{torch.ops.image._jpeg_version() = }") + assert torch.ops.image._is_compiled_against_turbo() smoke_test_torchvision() smoke_test_torchvision_read_decode() smoke_test_torchvision_resnet50_classify() diff --git a/test/test_image.py b/test/test_image.py index e9c96e72bda..0b9185e6025 100644 --- a/test/test_image.py +++ b/test/test_image.py @@ -39,8 +39,8 @@ def test_turbo(): print() - print(f"{torch.ops.image.jpeg_version() = }") - assert torch.ops.image.is_turbo() + print(f"{torch.ops.image._jpeg_version() = }") + assert torch.ops.image._is_compiled_against_turbo() def _get_safe_image_name(name): diff --git a/torchvision/csrc/io/image/image.cpp b/torchvision/csrc/io/image/image.cpp index 1e456ba5d6c..9b861e39bf5 100644 --- a/torchvision/csrc/io/image/image.cpp +++ b/torchvision/csrc/io/image/image.cpp @@ -20,10 +20,10 @@ namespace vision { namespace image { #ifdef JPEG_FOUND - #include +#include #endif -int64_t jpeg_version() { +int64_t _jpeg_version() { #ifdef JPEG_FOUND return JPEG_LIB_VERSION; #else @@ -31,26 +31,26 @@ int64_t jpeg_version() { #endif } -int64_t is_turbo() { +bool _is_compiled_against_turbo() { #ifdef LIBJPEG_TURBO_VERSION - return 1; + return true; #else - return 0; + return false; #endif } -static auto registry = torch::RegisterOperators() - .op("image::decode_png", &decode_png) - .op("image::encode_png", &encode_png) - .op("image::decode_jpeg", &decode_jpeg) - .op("image::encode_jpeg", &encode_jpeg) - .op("image::read_file", &read_file) - .op("image::write_file", &write_file) - .op("image::decode_image", &decode_image) - .op("image::decode_jpeg_cuda", &decode_jpeg_cuda) - .op("image::jpeg_version", &jpeg_version) - .op("image::is_turbo", &is_turbo); +static auto registry = + torch::RegisterOperators() + .op("image::decode_png", &decode_png) + .op("image::encode_png", &encode_png) + .op("image::decode_jpeg", &decode_jpeg) + .op("image::encode_jpeg", &encode_jpeg) + .op("image::read_file", &read_file) + .op("image::write_file", &write_file) + .op("image::decode_image", &decode_image) + .op("image::decode_jpeg_cuda", &decode_jpeg_cuda) + .op("image::_jpeg_version", &_jpeg_version) + .op("image::_is_compiled_against_turbo", &_is_compiled_against_turbo); } // namespace image } // namespace vision - From 70e87791b6225872b63eb180e85fa17e7703f7ff Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 4 Aug 2023 11:23:25 +0100 Subject: [PATCH 11/12] cleanups --- .github/scripts/unittest.sh | 4 ++-- test/test_image.py | 6 ------ torchvision/csrc/io/image/cpu/decode_jpeg.cpp | 15 ++++++++++++++ torchvision/csrc/io/image/cpu/decode_jpeg.h | 3 +++ torchvision/csrc/io/image/image.cpp | 20 ------------------- 5 files changed, 20 insertions(+), 28 deletions(-) diff --git a/.github/scripts/unittest.sh b/.github/scripts/unittest.sh index 4b4d6a54379..bb2ad73715a 100755 --- a/.github/scripts/unittest.sh +++ b/.github/scripts/unittest.sh @@ -11,5 +11,5 @@ echo '::group::Install testing utilities' pip install --progress-bar=off pytest pytest-mock pytest-cov echo '::endgroup::' -# pytest --junit-xml="${RUNNER_TEST_RESULTS_DIR}/test-results.xml" -v --durations=25 -pytest test/test_image.py --junit-xml="${RUNNER_TEST_RESULTS_DIR}/test-results.xml" -v -s --durations=25 +python test/smoke_test.py +pytest --junit-xml="${RUNNER_TEST_RESULTS_DIR}/test-results.xml" -v --durations=25 diff --git a/test/test_image.py b/test/test_image.py index 0b9185e6025..b08dc2026d4 100644 --- a/test/test_image.py +++ b/test/test_image.py @@ -37,12 +37,6 @@ PILLOW_VERSION = tuple(int(x) for x in PILLOW_VERSION.split(".")) -def test_turbo(): - print() - print(f"{torch.ops.image._jpeg_version() = }") - assert torch.ops.image._is_compiled_against_turbo() - - def _get_safe_image_name(name): # Used when we need to change the pytest "id" for an "image path" parameter. # If we don't, the test id (i.e. its name) will contain the whole path to the image, which is machine-specific, diff --git a/torchvision/csrc/io/image/cpu/decode_jpeg.cpp b/torchvision/csrc/io/image/cpu/decode_jpeg.cpp index 6ec644d003e..d07844a5e27 100644 --- a/torchvision/csrc/io/image/cpu/decode_jpeg.cpp +++ b/torchvision/csrc/io/image/cpu/decode_jpeg.cpp @@ -152,8 +152,23 @@ torch::Tensor decode_jpeg(const torch::Tensor& data, ImageReadMode mode) { jpeg_destroy_decompress(&cinfo); return tensor.permute({2, 0, 1}); } +#endif // #if !JPEG_FOUND +int64_t _jpeg_version() { +#ifdef JPEG_FOUND + return JPEG_LIB_VERSION; +#else + return -1; +#endif +} + +bool _is_compiled_against_turbo() { +#ifdef LIBJPEG_TURBO_VERSION + return true; +#else + return false; #endif +} } // namespace image } // namespace vision diff --git a/torchvision/csrc/io/image/cpu/decode_jpeg.h b/torchvision/csrc/io/image/cpu/decode_jpeg.h index 97ed3d51a54..254e94680b6 100644 --- a/torchvision/csrc/io/image/cpu/decode_jpeg.h +++ b/torchvision/csrc/io/image/cpu/decode_jpeg.h @@ -10,5 +10,8 @@ C10_EXPORT torch::Tensor decode_jpeg( const torch::Tensor& data, ImageReadMode mode = IMAGE_READ_MODE_UNCHANGED); +C10_EXPORT int64_t _jpeg_version(); +C10_EXPORT bool _is_compiled_against_turbo(); + } // namespace image } // namespace vision diff --git a/torchvision/csrc/io/image/image.cpp b/torchvision/csrc/io/image/image.cpp index 9b861e39bf5..b5952739a7a 100644 --- a/torchvision/csrc/io/image/image.cpp +++ b/torchvision/csrc/io/image/image.cpp @@ -19,26 +19,6 @@ PyMODINIT_FUNC PyInit_image(void) { namespace vision { namespace image { -#ifdef JPEG_FOUND -#include -#endif - -int64_t _jpeg_version() { -#ifdef JPEG_FOUND - return JPEG_LIB_VERSION; -#else - return -1; -#endif -} - -bool _is_compiled_against_turbo() { -#ifdef LIBJPEG_TURBO_VERSION - return true; -#else - return false; -#endif -} - static auto registry = torch::RegisterOperators() .op("image::decode_png", &decode_png) From a0680daa551312999b5f6ca5c1f87b76f3f0d6af Mon Sep 17 00:00:00 2001 From: Nicolas Hug Date: Fri, 4 Aug 2023 12:02:14 +0100 Subject: [PATCH 12/12] Update .github/scripts/setup-env.sh Co-authored-by: Philip Meier --- .github/scripts/setup-env.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/scripts/setup-env.sh b/.github/scripts/setup-env.sh index 10f7e4ef2e3..4b852efd9b7 100755 --- a/.github/scripts/setup-env.sh +++ b/.github/scripts/setup-env.sh @@ -43,7 +43,7 @@ conda create \ libpng \ 'ffmpeg<4.3' conda activate ci -conda install libjpeg-turbo -c pytorch -y +conda install --quiet --yes libjpeg-turbo -c pytorch pip install --progress-bar=off --upgrade setuptools # See https://github.com/pytorch/vision/issues/6790