Skip to content

Commit

Permalink
Reapply "Fix memory leak in decode_webp (#8712)" (#8723) (#8724)
Browse files Browse the repository at this point in the history
Co-authored-by: atalman <[email protected]>
  • Loading branch information
NicolasHug and atalman authored Dec 2, 2024
1 parent cd839f1 commit eedca60
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion packaging/pre_build_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ else
conda install -yq ffmpeg=4.2 libjpeg-turbo -c pytorch-nightly
fi

yum install -y libjpeg-turbo-devel libwebp-devel freetype gnutls
conda install libwebp -yq
conda install libjpeg-turbo -c pytorch
yum install -y freetype gnutls
pip install auditwheel
fi

Expand Down
2 changes: 2 additions & 0 deletions test/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
IS_MACOS = sys.platform == "darwin"
PILLOW_VERSION = tuple(int(x) for x in PILLOW_VERSION.split("."))
WEBP_TEST_IMAGES_DIR = os.environ.get("WEBP_TEST_IMAGES_DIR", "")
# See https://github.com/pytorch/vision/pull/8724#issuecomment-2503964558
ROCM_WEBP_MESSAGE = "ROCM not built with webp support."

# Hacky way of figuring out whether we compiled with libavif/libheif (those are
# currenlty disabled by default)
Expand Down
5 changes: 4 additions & 1 deletion torchvision/csrc/io/image/cpu/decode_webp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#if WEBP_FOUND
#include "webp/decode.h"
#include "webp/types.h"
#endif // WEBP_FOUND

namespace vision {
Expand Down Expand Up @@ -44,10 +45,12 @@ torch::Tensor decode_webp(

auto decoded_data =
decoding_func(encoded_data_p, encoded_data_size, &width, &height);

TORCH_CHECK(decoded_data != nullptr, "WebPDecodeRGB[A] failed.");

auto deleter = [decoded_data](void*) { WebPFree(decoded_data); };
auto out = torch::from_blob(
decoded_data, {height, width, num_channels}, torch::kUInt8);
decoded_data, {height, width, num_channels}, deleter, torch::kUInt8);

return out.permute({2, 0, 1});
}
Expand Down

0 comments on commit eedca60

Please sign in to comment.