-
Notifications
You must be signed in to change notification settings - Fork 7.1k
[WIP] nvJPEG support #2786
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
Closed
Closed
[WIP] nvJPEG support #2786
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
f878b36
Initial stab at nvJPEG support #2742
jamt9000 5eb6d73
Init nvjpeg once on first call
jamt9000 afd4a2e
Merge remote-tracking branch 'origin' into nvjpeg
jamt9000 8abe4a5
Add io/image/cuda search path
jamt9000 8ae0751
Update test
jamt9000 9a2510f
Building CUDA ext should mean nvjpeg exists
jamt9000 ac3330b
Check if nvjpeg.h is actually there
jamt9000 e798157
Add ImageReadMode support for nvjpeg
jamt9000 a07f53a
lint
jamt9000 e485656
Call nvjpegJpegStateDestroy when bailing out
jamt9000 1c1e471
Use at::cuda::getCurrentCUDAStream()
jamt9000 3e7486e
Merge branch 'master' into nvjpeg
jamt9000 5bc5e21
Changes to match #3312
jamt9000 4d4cd45
Move includes outside namespace
jamt9000 dd3e445
Lint
jamt9000 f560eeb
Guard includes so cpu builds work
jamt9000 ab90893
Add device argument
jamt9000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
#include "readjpeg_cuda.h" | ||
|
||
#include <string> | ||
|
||
#if !NVJPEG_FOUND | ||
|
||
torch::Tensor decodeJPEG_cuda(const torch::Tensor& data, ImageReadMode mode) { | ||
TORCH_CHECK( | ||
false, "decodeJPEG_cuda: torchvision not compiled with nvJPEG support"); | ||
} | ||
|
||
#else | ||
|
||
#include <ATen/ATen.h> | ||
#include <ATen/cuda/CUDAContext.h> | ||
#include <nvjpeg.h> | ||
|
||
static nvjpegHandle_t nvjpeg_handle = nullptr; | ||
|
||
void init_nvjpegImage(nvjpegImage_t& img) { | ||
for (int c = 0; c < NVJPEG_MAX_COMPONENT; c++) { | ||
img.channel[c] = nullptr; | ||
img.pitch[c] = 0; | ||
} | ||
} | ||
|
||
torch::Tensor decodeJPEG_cuda(const torch::Tensor& data, ImageReadMode mode) { | ||
// Check that the input tensor dtype is uint8 | ||
TORCH_CHECK(data.dtype() == torch::kU8, "Expected a torch.uint8 tensor"); | ||
// Check that the input tensor is 1-dimensional | ||
TORCH_CHECK( | ||
data.dim() == 1 && data.numel() > 0, | ||
"Expected a non empty 1-dimensional tensor"); | ||
|
||
auto datap = data.data_ptr<uint8_t>(); | ||
|
||
// Create nvJPEG handle | ||
if (nvjpeg_handle == nullptr) { | ||
nvjpegStatus_t create_status = nvjpegCreateSimple(&nvjpeg_handle); | ||
|
||
TORCH_CHECK( | ||
create_status == NVJPEG_STATUS_SUCCESS, | ||
"nvjpegCreateSimple failed: ", | ||
create_status); | ||
} | ||
|
||
// Create nvJPEG state (should this be persistent or not?) | ||
nvjpegJpegState_t nvjpeg_state; | ||
nvjpegStatus_t state_status = | ||
nvjpegJpegStateCreate(nvjpeg_handle, &nvjpeg_state); | ||
|
||
TORCH_CHECK( | ||
state_status == NVJPEG_STATUS_SUCCESS, | ||
"nvjpegJpegStateCreate failed: ", | ||
state_status); | ||
|
||
// Get the image information | ||
int components; | ||
nvjpegChromaSubsampling_t subsampling; | ||
int widths[NVJPEG_MAX_COMPONENT]; | ||
int heights[NVJPEG_MAX_COMPONENT]; | ||
|
||
nvjpegStatus_t info_status = nvjpegGetImageInfo( | ||
nvjpeg_handle, | ||
datap, | ||
data.numel(), | ||
&components, | ||
&subsampling, | ||
widths, | ||
heights); | ||
|
||
if (info_status != NVJPEG_STATUS_SUCCESS) { | ||
nvjpegJpegStateDestroy(nvjpeg_state); | ||
TORCH_CHECK(false, "nvjpegGetImageInfo failed: ", info_status); | ||
} | ||
|
||
if (subsampling == NVJPEG_CSS_UNKNOWN) { | ||
nvjpegJpegStateDestroy(nvjpeg_state); | ||
TORCH_CHECK(false, "Unknown NVJPEG chroma subsampling"); | ||
} | ||
|
||
int width = widths[0]; | ||
int height = heights[0]; | ||
|
||
nvjpegOutputFormat_t outputFormat; | ||
int outputComponents; | ||
|
||
switch (mode) { | ||
case IMAGE_READ_MODE_UNCHANGED: | ||
if (components == 1) { | ||
outputFormat = NVJPEG_OUTPUT_Y; | ||
outputComponents = 1; | ||
} else if (components == 3) { | ||
outputFormat = NVJPEG_OUTPUT_RGB; | ||
outputComponents = 3; | ||
} else { | ||
nvjpegJpegStateDestroy(nvjpeg_state); | ||
TORCH_CHECK( | ||
false, "The provided mode is not supported for JPEG files on GPU"); | ||
} | ||
break; | ||
case IMAGE_READ_MODE_GRAY: | ||
// This will do 0.299*R + 0.587*G + 0.114*B like opencv | ||
// TODO check if that is the same as libjpeg | ||
outputFormat = NVJPEG_OUTPUT_Y; | ||
outputComponents = 1; | ||
break; | ||
case IMAGE_READ_MODE_RGB: | ||
outputFormat = NVJPEG_OUTPUT_RGB; | ||
outputComponents = 3; | ||
break; | ||
default: | ||
// CMYK as input might work with nvjpegDecodeParamsSetAllowCMYK() | ||
nvjpegJpegStateDestroy(nvjpeg_state); | ||
TORCH_CHECK( | ||
false, "The provided mode is not supported for JPEG files on GPU"); | ||
} | ||
|
||
// nvjpegImage_t is a struct with | ||
// - an array of pointers to each channel | ||
// - the pitch for each channel | ||
// which must be filled in manually | ||
nvjpegImage_t outImage; | ||
init_nvjpegImage(outImage); | ||
|
||
// TODO device selection | ||
auto tensor = torch::empty( | ||
{int64_t(outputComponents), int64_t(height), int64_t(width)}, | ||
torch::dtype(torch::kU8).device(torch::kCUDA)); | ||
|
||
for (int c = 0; c < outputComponents; c++) { | ||
outImage.channel[c] = tensor[c].data_ptr<uint8_t>(); | ||
outImage.pitch[c] = width; | ||
} | ||
|
||
cudaStream_t stream = at::cuda::getCurrentCUDAStream(); | ||
|
||
nvjpegStatus_t decode_status = nvjpegDecode( | ||
nvjpeg_handle, | ||
nvjpeg_state, | ||
datap, | ||
data.numel(), | ||
outputFormat, | ||
&outImage, | ||
stream); | ||
|
||
// Destroy the state | ||
nvjpegJpegStateDestroy(nvjpeg_state); | ||
|
||
TORCH_CHECK( | ||
decode_status == NVJPEG_STATUS_SUCCESS, | ||
"nvjpegDecode failed: ", | ||
decode_status); | ||
|
||
return tensor; | ||
} | ||
|
||
#endif // NVJPEG_FOUND |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
#include <torch/types.h> | ||
#include "../image_read_mode.h" | ||
|
||
C10_EXPORT torch::Tensor decodeJPEG_cuda( | ||
const torch::Tensor& data, | ||
ImageReadMode mode = IMAGE_READ_MODE_UNCHANGED); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there device-specific state associated with the nvjpegHandle? i.e. is it safe/optimal to create a nvjpeg_handle on one device, then switch CUDA devices and use the previously created nvjpeg_handle?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It isn't completely obvious...although these parts of DALI give some clues
https://github.com/NVIDIA/DALI/blob/25ef92a7f9d48f1c895dcd7cceb9d98655aae279/dali/operators/decoder/nvjpeg/nvjpeg_decoder_gpu.h#L36-L37
https://github.com/NVIDIA/DALI/blob/25ef92a7f9d48f1c895dcd7cceb9d98655aae279/dali/operators/decoder/nvjpeg/nvjpeg_decoder_gpu.h#L166-L172
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The docs say:
With the links above where the struct is just defined as a global variable, and the fact that they call it a "library handle", I'd say it's fair to assume that it's not device-specific.
From a quick test with 2 GPUs, this passes fine: