torchcodec 0.10.0__cp312-cp312-manylinux_2_28_x86_64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- torchcodec/__init__.py +27 -0
- torchcodec/_core/AVIOContextHolder.cpp +60 -0
- torchcodec/_core/AVIOContextHolder.h +64 -0
- torchcodec/_core/AVIOFileLikeContext.cpp +98 -0
- torchcodec/_core/AVIOFileLikeContext.h +55 -0
- torchcodec/_core/AVIOTensorContext.cpp +130 -0
- torchcodec/_core/AVIOTensorContext.h +44 -0
- torchcodec/_core/BetaCudaDeviceInterface.cpp +849 -0
- torchcodec/_core/BetaCudaDeviceInterface.h +196 -0
- torchcodec/_core/CMakeLists.txt +295 -0
- torchcodec/_core/CUDACommon.cpp +330 -0
- torchcodec/_core/CUDACommon.h +51 -0
- torchcodec/_core/Cache.h +124 -0
- torchcodec/_core/CpuDeviceInterface.cpp +509 -0
- torchcodec/_core/CpuDeviceInterface.h +141 -0
- torchcodec/_core/CudaDeviceInterface.cpp +602 -0
- torchcodec/_core/CudaDeviceInterface.h +79 -0
- torchcodec/_core/DeviceInterface.cpp +117 -0
- torchcodec/_core/DeviceInterface.h +191 -0
- torchcodec/_core/Encoder.cpp +1054 -0
- torchcodec/_core/Encoder.h +192 -0
- torchcodec/_core/FFMPEGCommon.cpp +684 -0
- torchcodec/_core/FFMPEGCommon.h +314 -0
- torchcodec/_core/FilterGraph.cpp +159 -0
- torchcodec/_core/FilterGraph.h +59 -0
- torchcodec/_core/Frame.cpp +47 -0
- torchcodec/_core/Frame.h +72 -0
- torchcodec/_core/Metadata.cpp +124 -0
- torchcodec/_core/Metadata.h +92 -0
- torchcodec/_core/NVCUVIDRuntimeLoader.cpp +320 -0
- torchcodec/_core/NVCUVIDRuntimeLoader.h +14 -0
- torchcodec/_core/NVDECCache.cpp +60 -0
- torchcodec/_core/NVDECCache.h +102 -0
- torchcodec/_core/SingleStreamDecoder.cpp +1586 -0
- torchcodec/_core/SingleStreamDecoder.h +391 -0
- torchcodec/_core/StreamOptions.h +70 -0
- torchcodec/_core/Transform.cpp +128 -0
- torchcodec/_core/Transform.h +86 -0
- torchcodec/_core/ValidationUtils.cpp +35 -0
- torchcodec/_core/ValidationUtils.h +21 -0
- torchcodec/_core/__init__.py +46 -0
- torchcodec/_core/_metadata.py +262 -0
- torchcodec/_core/custom_ops.cpp +1090 -0
- torchcodec/_core/fetch_and_expose_non_gpl_ffmpeg_libs.cmake +169 -0
- torchcodec/_core/nvcuvid_include/cuviddec.h +1374 -0
- torchcodec/_core/nvcuvid_include/nvcuvid.h +610 -0
- torchcodec/_core/ops.py +605 -0
- torchcodec/_core/pybind_ops.cpp +50 -0
- torchcodec/_frame.py +146 -0
- torchcodec/_internally_replaced_utils.py +68 -0
- torchcodec/_samplers/__init__.py +7 -0
- torchcodec/_samplers/video_clip_sampler.py +419 -0
- torchcodec/decoders/__init__.py +12 -0
- torchcodec/decoders/_audio_decoder.py +185 -0
- torchcodec/decoders/_decoder_utils.py +113 -0
- torchcodec/decoders/_video_decoder.py +601 -0
- torchcodec/encoders/__init__.py +2 -0
- torchcodec/encoders/_audio_encoder.py +149 -0
- torchcodec/encoders/_video_encoder.py +196 -0
- torchcodec/libtorchcodec_core4.so +0 -0
- torchcodec/libtorchcodec_core5.so +0 -0
- torchcodec/libtorchcodec_core6.so +0 -0
- torchcodec/libtorchcodec_core7.so +0 -0
- torchcodec/libtorchcodec_core8.so +0 -0
- torchcodec/libtorchcodec_custom_ops4.so +0 -0
- torchcodec/libtorchcodec_custom_ops5.so +0 -0
- torchcodec/libtorchcodec_custom_ops6.so +0 -0
- torchcodec/libtorchcodec_custom_ops7.so +0 -0
- torchcodec/libtorchcodec_custom_ops8.so +0 -0
- torchcodec/libtorchcodec_pybind_ops4.so +0 -0
- torchcodec/libtorchcodec_pybind_ops5.so +0 -0
- torchcodec/libtorchcodec_pybind_ops6.so +0 -0
- torchcodec/libtorchcodec_pybind_ops7.so +0 -0
- torchcodec/libtorchcodec_pybind_ops8.so +0 -0
- torchcodec/samplers/__init__.py +2 -0
- torchcodec/samplers/_common.py +84 -0
- torchcodec/samplers/_index_based.py +287 -0
- torchcodec/samplers/_time_based.py +358 -0
- torchcodec/share/cmake/TorchCodec/TorchCodecConfig.cmake +76 -0
- torchcodec/share/cmake/TorchCodec/ffmpeg_versions.cmake +122 -0
- torchcodec/transforms/__init__.py +12 -0
- torchcodec/transforms/_decoder_transforms.py +375 -0
- torchcodec/version.py +2 -0
- torchcodec-0.10.0.dist-info/METADATA +286 -0
- torchcodec-0.10.0.dist-info/RECORD +88 -0
- torchcodec-0.10.0.dist-info/WHEEL +5 -0
- torchcodec-0.10.0.dist-info/licenses/LICENSE +28 -0
- torchcodec-0.10.0.dist-info/top_level.txt +2 -0
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
#include "CUDACommon.h"
|
|
8
|
+
#include "Cache.h" // for PerGpuCache
|
|
9
|
+
|
|
10
|
+
namespace facebook::torchcodec {
|
|
11
|
+
|
|
12
|
+
namespace {
|
|
13
|
+
|
|
14
|
+
// Set to -1 to have an infinitely sized cache. Set it to 0 to disable caching.
|
|
15
|
+
// Set to a positive number to have a cache of that size.
|
|
16
|
+
const int MAX_CONTEXTS_PER_GPU_IN_CACHE = -1;
|
|
17
|
+
|
|
18
|
+
PerGpuCache<NppStreamContext> g_cached_npp_ctxs(
|
|
19
|
+
MAX_CUDA_GPUS,
|
|
20
|
+
MAX_CONTEXTS_PER_GPU_IN_CACHE);
|
|
21
|
+
|
|
22
|
+
} // namespace
|
|
23
|
+
|
|
24
|
+
void initializeCudaContextWithPytorch(const torch::Device& device) {
|
|
25
|
+
// It is important for pytorch itself to create the cuda context. If ffmpeg
|
|
26
|
+
// creates the context it may not be compatible with pytorch.
|
|
27
|
+
// This is a dummy tensor to initialize the cuda context.
|
|
28
|
+
torch::Tensor dummyTensorForCudaInitialization = torch::zeros(
|
|
29
|
+
{1}, torch::TensorOptions().dtype(torch::kUInt8).device(device));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/* clang-format off */
|
|
33
|
+
// Note: [YUV -> RGB Color Conversion, color space and color range]
|
|
34
|
+
//
|
|
35
|
+
// The frames we get from the decoder (FFmpeg decoder, or NVCUVID) are in YUV
|
|
36
|
+
// format. We need to convert them to RGB. This note attempts to describe this
|
|
37
|
+
// process. There may be some inaccuracies and approximations that experts will
|
|
38
|
+
// notice, but our goal is only to provide a good enough understanding of the
|
|
39
|
+
// process for torchcodec developers to implement and maintain it.
|
|
40
|
+
// On CPU, filtergraph and swscale handle everything for us. With CUDA, we have
|
|
41
|
+
// to do a lot of the heavy lifting ourselves.
|
|
42
|
+
//
|
|
43
|
+
// Color space and color range
|
|
44
|
+
// ---------------------------
|
|
45
|
+
// Two main characteristics of a frame will affect the conversion process:
|
|
46
|
+
// 1. Color space: This basically defines what YUV values correspond to which
|
|
47
|
+
// physical wavelength. No need to go into details here,the point is that
|
|
48
|
+
// videos can come in different color spaces, the most common ones being
|
|
49
|
+
// BT.601 and BT.709, but there are others.
|
|
50
|
+
// In FFmpeg this is represented with AVColorSpace:
|
|
51
|
+
// https://ffmpeg.org/doxygen/4.0/pixfmt_8h.html#aff71a069509a1ad3ff54d53a1c894c85
|
|
52
|
+
// 2. Color range: This defines the range of YUV values. There is:
|
|
53
|
+
// - full range, also called PC range: AVCOL_RANGE_JPEG
|
|
54
|
+
// - and the "limited" range, also called studio or TV range: AVCOL_RANGE_MPEG
|
|
55
|
+
// https://ffmpeg.org/doxygen/4.0/pixfmt_8h.html#a3da0bf691418bc22c4bcbe6583ad589a
|
|
56
|
+
//
|
|
57
|
+
// Color space and color range are independent concepts, so we can have a BT.709
|
|
58
|
+
// with full range, and another one with limited range. Same for BT.601.
|
|
59
|
+
//
|
|
60
|
+
// In the first version of this note we'll focus on the full color range. It
|
|
61
|
+
// will later be updated to account for the limited range.
|
|
62
|
+
//
|
|
63
|
+
// Color conversion matrix
|
|
64
|
+
// -----------------------
|
|
65
|
+
// YUV -> RGB conversion is defined as the reverse process of the RGB -> YUV,
|
|
66
|
+
// So this is where we'll start.
|
|
67
|
+
// At the core of a RGB -> YUV conversion are the "luma coefficients", which are
|
|
68
|
+
// specific to a given color space and defined by the color space standard. In
|
|
69
|
+
// FFmpeg they can be found here:
|
|
70
|
+
// https://github.com/FFmpeg/FFmpeg/blob/7d606ef0ccf2946a4a21ab1ec23486cadc21864b/libavutil/csp.c#L46-L56
|
|
71
|
+
//
|
|
72
|
+
// For example, the BT.709 coefficients are: kr=0.2126, kg=0.7152, kb=0.0722
|
|
73
|
+
// Coefficients must sum to 1.
|
|
74
|
+
//
|
|
75
|
+
// Conventionally Y is in [0, 1] range, and U and V are in [-0.5, 0.5] range
|
|
76
|
+
// (that's mathematically, in practice they are represented in integer range).
|
|
77
|
+
// The conversion is defined as:
|
|
78
|
+
// https://en.wikipedia.org/wiki/YCbCr#R'G'B'_to_Y%E2%80%B2PbPr
|
|
79
|
+
// Y = kr*R + kg*G + kb*B
|
|
80
|
+
// U = (B - Y) * 0.5 / (1 - kb) = (B - Y) / u_scale where u_scale = 2 * (1 - kb)
|
|
81
|
+
// V = (R - Y) * 0.5 / (1 - kr) = (R - Y) / v_scale where v_scale = 2 * (1 - kr)
|
|
82
|
+
//
|
|
83
|
+
// Putting all this into matrix form, we get:
|
|
84
|
+
// [Y] = [kr kg kb ] [R]
|
|
85
|
+
// [U] [-kr/u_scale -kg/u_scale (1-kb)/u_scale] [G]
|
|
86
|
+
// [V] [(1-kr)/v_scale -kg/v_scale -kb)/v_scale ] [B]
|
|
87
|
+
//
|
|
88
|
+
//
|
|
89
|
+
// Now, to convert YUV to RGB, we just need to invert this matrix:
|
|
90
|
+
// ```py
|
|
91
|
+
// import torch
|
|
92
|
+
// kr, kg, kb = 0.2126, 0.7152, 0.0722 # BT.709 luma coefficients
|
|
93
|
+
// u_scale = 2 * (1 - kb)
|
|
94
|
+
// v_scale = 2 * (1 - kr)
|
|
95
|
+
//
|
|
96
|
+
// rgb_to_yuv = torch.tensor([
|
|
97
|
+
// [kr, kg, kb],
|
|
98
|
+
// [-kr/u_scale, -kg/u_scale, (1-kb)/u_scale],
|
|
99
|
+
// [(1-kr)/v_scale, -kg/v_scale, -kb/v_scale]
|
|
100
|
+
// ])
|
|
101
|
+
//
|
|
102
|
+
// yuv_to_rgb_full = torch.linalg.inv(rgb_to_yuv)
|
|
103
|
+
// print("YUV->RGB matrix (Full Range):")
|
|
104
|
+
// print(yuv_to_rgb_full)
|
|
105
|
+
// ```
|
|
106
|
+
// And we get:
|
|
107
|
+
// tensor([[ 1.0000e+00, -3.3142e-09, 1.5748e+00],
|
|
108
|
+
// [ 1.0000e+00, -1.8732e-01, -4.6812e-01],
|
|
109
|
+
// [ 1.0000e+00, 1.8556e+00, 4.6231e-09]])
|
|
110
|
+
//
|
|
111
|
+
// Which matches https://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.709_conversion
|
|
112
|
+
//
|
|
113
|
+
// Color conversion in NPP
|
|
114
|
+
// -----------------------
|
|
115
|
+
// https://docs.nvidia.com/cuda/npp/image_color_conversion.html.
|
|
116
|
+
//
|
|
117
|
+
// NPP provides different ways to convert YUV to RGB:
|
|
118
|
+
// - pre-defined color conversion functions like
|
|
119
|
+
// nppiNV12ToRGB_709CSC_8u_P2C3R_Ctx and nppiNV12ToRGB_709HDTV_8u_P2C3R_Ctx
|
|
120
|
+
// which are for BT.709 limited and full range, respectively.
|
|
121
|
+
// - generic color conversion functions that accept a custom color conversion
|
|
122
|
+
// matrix, called ColorTwist, like nppiNV12ToRGB_8u_ColorTwist32f_P2C3R_Ctx
|
|
123
|
+
//
|
|
124
|
+
// We use the pre-defined functions or the color twist functions depending on
|
|
125
|
+
// which one we find to be closer to the CPU results.
|
|
126
|
+
//
|
|
127
|
+
// The color twist functionality is *partially* described in a section named
|
|
128
|
+
// "YUVToRGBColorTwist". Importantly:
|
|
129
|
+
//
|
|
130
|
+
// - The `nppiNV12ToRGB_8u_ColorTwist32f_P2C3R_Ctx` function takes the YUV data
|
|
131
|
+
// and the color-conversion matrix as input. The function itself and the
|
|
132
|
+
// matrix assume different ranges for YUV values:
|
|
133
|
+
// - The **matrix coefficient** must assume that Y is in [0, 1] and U,V are in
|
|
134
|
+
// [-0.5, 0.5]. That's how we defined our matrix above.
|
|
135
|
+
// - The function `nppiNV12ToRGB_8u_ColorTwist32f_P2C3R_Ctx` however expects all
|
|
136
|
+
// of the input Y, U, V to be in [0, 255]. That's how the data comes out of
|
|
137
|
+
// the decoder.
|
|
138
|
+
// - But *internally*, `nppiNV12ToRGB_8u_ColorTwist32f_P2C3R_Ctx` needs U and V to
|
|
139
|
+
// be centered around 0, i.e. in [-128, 127]. So we need to apply a -128
|
|
140
|
+
// offset to U and V. Y doesn't need to be offset. The offset can be applied
|
|
141
|
+
// by adding a 4th column to the matrix.
|
|
142
|
+
//
|
|
143
|
+
//
|
|
144
|
+
// So our conversion matrix becomes the following, with new offset column:
|
|
145
|
+
// tensor([[ 1.0000e+00, -3.3142e-09, 1.5748e+00, 0]
|
|
146
|
+
// [ 1.0000e+00, -1.8732e-01, -4.6812e-01, -128]
|
|
147
|
+
// [ 1.0000e+00, 1.8556e+00, 4.6231e-09 , -128]])
|
|
148
|
+
//
|
|
149
|
+
// And that's what we need to pass for BT701, full range.
|
|
150
|
+
/* clang-format on */
|
|
151
|
+
|
|
152
|
+
// BT.709 full range color conversion matrix for YUV to RGB conversion.
|
|
153
|
+
// See Note [YUV -> RGB Color Conversion, color space and color range]
|
|
154
|
+
const Npp32f bt709FullRangeColorTwist[3][4] = {
|
|
155
|
+
{1.0f, 0.0f, 1.5748f, 0.0f},
|
|
156
|
+
{1.0f, -0.187324273f, -0.468124273f, -128.0f},
|
|
157
|
+
{1.0f, 1.8556f, 0.0f, -128.0f}};
|
|
158
|
+
|
|
159
|
+
torch::Tensor convertNV12FrameToRGB(
|
|
160
|
+
UniqueAVFrame& avFrame,
|
|
161
|
+
const torch::Device& device,
|
|
162
|
+
const UniqueNppContext& nppCtx,
|
|
163
|
+
at::cuda::CUDAStream nvdecStream,
|
|
164
|
+
std::optional<torch::Tensor> preAllocatedOutputTensor) {
|
|
165
|
+
auto frameDims = FrameDims(avFrame->height, avFrame->width);
|
|
166
|
+
torch::Tensor dst;
|
|
167
|
+
if (preAllocatedOutputTensor.has_value()) {
|
|
168
|
+
dst = preAllocatedOutputTensor.value();
|
|
169
|
+
} else {
|
|
170
|
+
dst = allocateEmptyHWCTensor(frameDims, device);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// We need to make sure NVDEC has finished decoding a frame before
|
|
174
|
+
// color-converting it with NPP.
|
|
175
|
+
// So we make the NPP stream wait for NVDEC to finish.
|
|
176
|
+
at::cuda::CUDAStream nppStream =
|
|
177
|
+
at::cuda::getCurrentCUDAStream(device.index());
|
|
178
|
+
at::cuda::CUDAEvent nvdecDoneEvent;
|
|
179
|
+
nvdecDoneEvent.record(nvdecStream);
|
|
180
|
+
nvdecDoneEvent.block(nppStream);
|
|
181
|
+
|
|
182
|
+
nppCtx->hStream = nppStream.stream();
|
|
183
|
+
cudaError_t err = cudaStreamGetFlags(nppCtx->hStream, &nppCtx->nStreamFlags);
|
|
184
|
+
TORCH_CHECK(
|
|
185
|
+
err == cudaSuccess,
|
|
186
|
+
"cudaStreamGetFlags failed: ",
|
|
187
|
+
cudaGetErrorString(err));
|
|
188
|
+
|
|
189
|
+
NppiSize oSizeROI = {frameDims.width, frameDims.height};
|
|
190
|
+
Npp8u* yuvData[2] = {avFrame->data[0], avFrame->data[1]};
|
|
191
|
+
|
|
192
|
+
NppStatus status;
|
|
193
|
+
|
|
194
|
+
// For background, see
|
|
195
|
+
// Note [YUV -> RGB Color Conversion, color space and color range]
|
|
196
|
+
if (avFrame->colorspace == AVColorSpace::AVCOL_SPC_BT709) {
|
|
197
|
+
if (avFrame->color_range == AVColorRange::AVCOL_RANGE_JPEG) {
|
|
198
|
+
// NPP provides a pre-defined color conversion function for BT.709 full
|
|
199
|
+
// range: nppiNV12ToRGB_709HDTV_8u_P2C3R_Ctx. But it's not closely
|
|
200
|
+
// matching the results we have on CPU. So we're using a custom color
|
|
201
|
+
// conversion matrix, which provides more accurate results. See the note
|
|
202
|
+
// mentioned above for details, and headaches.
|
|
203
|
+
|
|
204
|
+
int srcStep[2] = {avFrame->linesize[0], avFrame->linesize[1]};
|
|
205
|
+
|
|
206
|
+
status = nppiNV12ToRGB_8u_ColorTwist32f_P2C3R_Ctx(
|
|
207
|
+
yuvData,
|
|
208
|
+
srcStep,
|
|
209
|
+
static_cast<Npp8u*>(dst.data_ptr()),
|
|
210
|
+
dst.stride(0),
|
|
211
|
+
oSizeROI,
|
|
212
|
+
bt709FullRangeColorTwist,
|
|
213
|
+
*nppCtx);
|
|
214
|
+
} else {
|
|
215
|
+
// If not full range, we assume studio limited range.
|
|
216
|
+
// The color conversion matrix for BT.709 limited range should be:
|
|
217
|
+
// static const Npp32f bt709LimitedRangeColorTwist[3][4] = {
|
|
218
|
+
// {1.16438356f, 0.0f, 1.79274107f, -16.0f},
|
|
219
|
+
// {1.16438356f, -0.213248614f, -0.5329093290f, -128.0f},
|
|
220
|
+
// {1.16438356f, 2.11240179f, 0.0f, -128.0f}
|
|
221
|
+
// };
|
|
222
|
+
// We get very close results to CPU with that, but using the pre-defined
|
|
223
|
+
// nppiNV12ToRGB_709CSC_8u_P2C3R_Ctx seems to be even more accurate.
|
|
224
|
+
status = nppiNV12ToRGB_709CSC_8u_P2C3R_Ctx(
|
|
225
|
+
yuvData,
|
|
226
|
+
avFrame->linesize[0],
|
|
227
|
+
static_cast<Npp8u*>(dst.data_ptr()),
|
|
228
|
+
dst.stride(0),
|
|
229
|
+
oSizeROI,
|
|
230
|
+
*nppCtx);
|
|
231
|
+
}
|
|
232
|
+
} else {
|
|
233
|
+
// TODO we're assuming BT.601 color space (and probably limited range) by
|
|
234
|
+
// calling nppiNV12ToRGB_8u_P2C3R_Ctx. We should handle BT.601 full range,
|
|
235
|
+
// and other color-spaces like 2020.
|
|
236
|
+
status = nppiNV12ToRGB_8u_P2C3R_Ctx(
|
|
237
|
+
yuvData,
|
|
238
|
+
avFrame->linesize[0],
|
|
239
|
+
static_cast<Npp8u*>(dst.data_ptr()),
|
|
240
|
+
dst.stride(0),
|
|
241
|
+
oSizeROI,
|
|
242
|
+
*nppCtx);
|
|
243
|
+
}
|
|
244
|
+
TORCH_CHECK(status == NPP_SUCCESS, "Failed to convert NV12 frame.");
|
|
245
|
+
|
|
246
|
+
return dst;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
UniqueNppContext getNppStreamContext(const torch::Device& device) {
|
|
250
|
+
int deviceIndex = getDeviceIndex(device);
|
|
251
|
+
|
|
252
|
+
UniqueNppContext nppCtx = g_cached_npp_ctxs.get(device);
|
|
253
|
+
if (nppCtx) {
|
|
254
|
+
return nppCtx;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// From 12.9, NPP recommends using a user-created NppStreamContext and using
|
|
258
|
+
// the `_Ctx()` calls:
|
|
259
|
+
// https://docs.nvidia.com/cuda/cuda-toolkit-release-notes/index.html#npp-release-12-9-update-1
|
|
260
|
+
// And the nppGetStreamContext() helper is deprecated. We are explicitly
|
|
261
|
+
// supposed to create the NppStreamContext manually from the CUDA device
|
|
262
|
+
// properties:
|
|
263
|
+
// https://github.com/NVIDIA/CUDALibrarySamples/blob/d97803a40fab83c058bb3d68b6c38bd6eebfff43/NPP/README.md?plain=1#L54-L72
|
|
264
|
+
|
|
265
|
+
nppCtx = std::make_unique<NppStreamContext>();
|
|
266
|
+
cudaDeviceProp prop{};
|
|
267
|
+
cudaError_t err = cudaGetDeviceProperties(&prop, deviceIndex);
|
|
268
|
+
TORCH_CHECK(
|
|
269
|
+
err == cudaSuccess,
|
|
270
|
+
"cudaGetDeviceProperties failed: ",
|
|
271
|
+
cudaGetErrorString(err));
|
|
272
|
+
|
|
273
|
+
nppCtx->nCudaDeviceId = deviceIndex;
|
|
274
|
+
nppCtx->nMultiProcessorCount = prop.multiProcessorCount;
|
|
275
|
+
nppCtx->nMaxThreadsPerMultiProcessor = prop.maxThreadsPerMultiProcessor;
|
|
276
|
+
nppCtx->nMaxThreadsPerBlock = prop.maxThreadsPerBlock;
|
|
277
|
+
nppCtx->nSharedMemPerBlock = prop.sharedMemPerBlock;
|
|
278
|
+
nppCtx->nCudaDevAttrComputeCapabilityMajor = prop.major;
|
|
279
|
+
nppCtx->nCudaDevAttrComputeCapabilityMinor = prop.minor;
|
|
280
|
+
|
|
281
|
+
return nppCtx;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
void returnNppStreamContextToCache(
|
|
285
|
+
const torch::Device& device,
|
|
286
|
+
UniqueNppContext nppCtx) {
|
|
287
|
+
if (nppCtx) {
|
|
288
|
+
g_cached_npp_ctxs.addIfCacheHasCapacity(device, std::move(nppCtx));
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
void validatePreAllocatedTensorShape(
|
|
293
|
+
const std::optional<torch::Tensor>& preAllocatedOutputTensor,
|
|
294
|
+
const UniqueAVFrame& avFrame) {
|
|
295
|
+
// Note that CUDA does not yet support transforms, so the only possible
|
|
296
|
+
// frame dimensions are the raw decoded frame's dimensions.
|
|
297
|
+
auto frameDims = FrameDims(avFrame->height, avFrame->width);
|
|
298
|
+
|
|
299
|
+
if (preAllocatedOutputTensor.has_value()) {
|
|
300
|
+
auto shape = preAllocatedOutputTensor.value().sizes();
|
|
301
|
+
TORCH_CHECK(
|
|
302
|
+
(shape.size() == 3) && (shape[0] == frameDims.height) &&
|
|
303
|
+
(shape[1] == frameDims.width) && (shape[2] == 3),
|
|
304
|
+
"Expected tensor of shape ",
|
|
305
|
+
frameDims.height,
|
|
306
|
+
"x",
|
|
307
|
+
frameDims.width,
|
|
308
|
+
"x3, got ",
|
|
309
|
+
shape);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
int getDeviceIndex(const torch::Device& device) {
|
|
314
|
+
// PyTorch uses int8_t as its torch::DeviceIndex, but FFmpeg and CUDA
|
|
315
|
+
// libraries use int. So we use int, too.
|
|
316
|
+
int deviceIndex = static_cast<int>(device.index());
|
|
317
|
+
TORCH_CHECK(
|
|
318
|
+
deviceIndex >= -1 && deviceIndex < MAX_CUDA_GPUS,
|
|
319
|
+
"Invalid device index = ",
|
|
320
|
+
deviceIndex);
|
|
321
|
+
|
|
322
|
+
if (deviceIndex == -1) {
|
|
323
|
+
TORCH_CHECK(
|
|
324
|
+
cudaGetDevice(&deviceIndex) == cudaSuccess,
|
|
325
|
+
"Failed to get current CUDA device.");
|
|
326
|
+
}
|
|
327
|
+
return deviceIndex;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
} // namespace facebook::torchcodec
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include <ATen/cuda/CUDAEvent.h>
|
|
10
|
+
#include <c10/cuda/CUDAStream.h>
|
|
11
|
+
#include <npp.h>
|
|
12
|
+
#include <torch/types.h>
|
|
13
|
+
|
|
14
|
+
#include "FFMPEGCommon.h"
|
|
15
|
+
#include "Frame.h"
|
|
16
|
+
|
|
17
|
+
extern "C" {
|
|
18
|
+
#include <libavutil/hwcontext_cuda.h>
|
|
19
|
+
#include <libavutil/pixdesc.h>
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
namespace facebook::torchcodec {
|
|
23
|
+
|
|
24
|
+
// Pytorch can only handle up to 128 GPUs.
|
|
25
|
+
// https://github.com/pytorch/pytorch/blob/e30c55ee527b40d67555464b9e402b4b7ce03737/c10/cuda/CUDAMacros.h#L44
|
|
26
|
+
constexpr int MAX_CUDA_GPUS = 128;
|
|
27
|
+
|
|
28
|
+
void initializeCudaContextWithPytorch(const torch::Device& device);
|
|
29
|
+
|
|
30
|
+
// Unique pointer type for NPP stream context
|
|
31
|
+
using UniqueNppContext = std::unique_ptr<NppStreamContext>;
|
|
32
|
+
|
|
33
|
+
torch::Tensor convertNV12FrameToRGB(
|
|
34
|
+
UniqueAVFrame& avFrame,
|
|
35
|
+
const torch::Device& device,
|
|
36
|
+
const UniqueNppContext& nppCtx,
|
|
37
|
+
at::cuda::CUDAStream nvdecStream,
|
|
38
|
+
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt);
|
|
39
|
+
|
|
40
|
+
UniqueNppContext getNppStreamContext(const torch::Device& device);
|
|
41
|
+
void returnNppStreamContextToCache(
|
|
42
|
+
const torch::Device& device,
|
|
43
|
+
UniqueNppContext nppCtx);
|
|
44
|
+
|
|
45
|
+
void validatePreAllocatedTensorShape(
|
|
46
|
+
const std::optional<torch::Tensor>& preAllocatedOutputTensor,
|
|
47
|
+
const UniqueAVFrame& avFrame);
|
|
48
|
+
|
|
49
|
+
int getDeviceIndex(const torch::Device& device);
|
|
50
|
+
|
|
51
|
+
} // namespace facebook::torchcodec
|
torchcodec/_core/Cache.h
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
// All rights reserved.
|
|
3
|
+
//
|
|
4
|
+
// This source code is licensed under the BSD-style license found in the
|
|
5
|
+
// LICENSE file in the root directory of this source tree.
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include <torch/types.h>
|
|
10
|
+
#include <memory>
|
|
11
|
+
#include <mutex>
|
|
12
|
+
|
|
13
|
+
namespace facebook::torchcodec {
|
|
14
|
+
|
|
15
|
+
// This header defines simple cache class primitives to store reusable objects
|
|
16
|
+
// across TorchCodec stream instances. Intended usage is to store hardware
|
|
17
|
+
// contexts creation of which is expensive. The cache mechanism is as follows:
|
|
18
|
+
// 1. 'PerGpuCache' provides a dynamic cache with the specified maximum capacity
|
|
19
|
+
// for the given number of GPUs.
|
|
20
|
+
// 2. When stream object (e.g. SingleStreamDecoder) is destoyed cachable object
|
|
21
|
+
// must be released to the cache. Cache will accept the object if it is not
|
|
22
|
+
// full.
|
|
23
|
+
// 3. When stream object (e.g. SingleStreamDecoder) is created cachable object
|
|
24
|
+
// must be first queried from the cache. If the cache is empty then new
|
|
25
|
+
// object must be created.
|
|
26
|
+
|
|
27
|
+
template <typename T, typename D = std::default_delete<T>>
|
|
28
|
+
class Cache {
|
|
29
|
+
public:
|
|
30
|
+
using element_type = std::unique_ptr<T, D>;
|
|
31
|
+
|
|
32
|
+
explicit Cache(int capacity) : capacity_(capacity) {}
|
|
33
|
+
|
|
34
|
+
// Adds an object to the cache if the cache has capacity. Returns true
|
|
35
|
+
// if object was added and false otherwise.
|
|
36
|
+
bool addIfCacheHasCapacity(element_type&& obj);
|
|
37
|
+
|
|
38
|
+
// Returns an object from the cache. Cache does not hold a reference
|
|
39
|
+
// to the object after this call.
|
|
40
|
+
element_type get();
|
|
41
|
+
|
|
42
|
+
private:
|
|
43
|
+
int capacity_;
|
|
44
|
+
std::mutex mutex_;
|
|
45
|
+
std::vector<element_type> cache_;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
template <typename T, typename D>
|
|
49
|
+
bool Cache<T, D>::addIfCacheHasCapacity(element_type&& obj) {
|
|
50
|
+
std::scoped_lock lock(mutex_);
|
|
51
|
+
if (capacity_ >= 0 && cache_.size() >= static_cast<size_t>(capacity_)) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
cache_.push_back(std::move(obj));
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
template <typename T, typename D>
|
|
59
|
+
typename Cache<T, D>::element_type Cache<T, D>::get() {
|
|
60
|
+
std::scoped_lock lock(mutex_);
|
|
61
|
+
if (cache_.empty()) {
|
|
62
|
+
return nullptr;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
element_type obj = std::move(cache_.back());
|
|
66
|
+
cache_.pop_back();
|
|
67
|
+
return obj;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
template <typename T, typename D = std::default_delete<T>>
|
|
71
|
+
class PerGpuCache {
|
|
72
|
+
public:
|
|
73
|
+
using element_type = typename Cache<T, D>::element_type;
|
|
74
|
+
|
|
75
|
+
// Initializes 'maxGpus' number of caches. Each cache can hold no
|
|
76
|
+
// more than 'capacity' items. If 'capacity' <0 cache size is unlimited.
|
|
77
|
+
PerGpuCache(int maxGpus, int capacity) {
|
|
78
|
+
TORCH_CHECK(maxGpus > 0, "maxGpus for PerGpuCache must be >0");
|
|
79
|
+
for (int i = 0; i < maxGpus; ++i) {
|
|
80
|
+
cache_.emplace_back(std::make_unique<Cache<T, D>>(capacity));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Adds an object to the specified device cache if the cache has
|
|
85
|
+
// capacity. Returns true if object was added and false otherwise.
|
|
86
|
+
bool addIfCacheHasCapacity(const torch::Device& device, element_type&& obj);
|
|
87
|
+
|
|
88
|
+
// Returns an object from the cache of the specified device. Cache
|
|
89
|
+
// does not hold a reference to the object after this call.
|
|
90
|
+
element_type get(const torch::Device& device);
|
|
91
|
+
|
|
92
|
+
private:
|
|
93
|
+
// 'Cache' class implementation contains mutex which makes it non-movable
|
|
94
|
+
// and non-copyable, so we need to wrap it in std::unique_ptr.
|
|
95
|
+
std::vector<std::unique_ptr<Cache<T, D>>> cache_;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// Forward declaration of getDeviceIndex which exists in CUDACommon.h
|
|
99
|
+
// This avoids circular dependency between Cache.h and CUDACommon.cpp which also
|
|
100
|
+
// needs to include Cache.h
|
|
101
|
+
int getDeviceIndex(const torch::Device& device);
|
|
102
|
+
|
|
103
|
+
template <typename T, typename D>
|
|
104
|
+
bool PerGpuCache<T, D>::addIfCacheHasCapacity(
|
|
105
|
+
const torch::Device& device,
|
|
106
|
+
element_type&& obj) {
|
|
107
|
+
int deviceIndex = getDeviceIndex(device);
|
|
108
|
+
TORCH_CHECK(
|
|
109
|
+
static_cast<size_t>(deviceIndex) < cache_.size(),
|
|
110
|
+
"Device index out of range");
|
|
111
|
+
return cache_[deviceIndex]->addIfCacheHasCapacity(std::move(obj));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
template <typename T, typename D>
|
|
115
|
+
typename PerGpuCache<T, D>::element_type PerGpuCache<T, D>::get(
|
|
116
|
+
const torch::Device& device) {
|
|
117
|
+
int deviceIndex = getDeviceIndex(device);
|
|
118
|
+
TORCH_CHECK(
|
|
119
|
+
static_cast<size_t>(deviceIndex) < cache_.size(),
|
|
120
|
+
"Device index out of range");
|
|
121
|
+
return cache_[deviceIndex]->get();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
} // namespace facebook::torchcodec
|