Skip to content

Commit d6385be

Browse files
dvrogozheromomon
authored andcommitted
Enable Intel GPU support in torchcodec on Linux (xpu device)
This commit enables support for Intel GPUs in torchcodec. It adds: * ffmpeg-vaapi for decoding * VAAPI based color space conversion (decoding output to RGBA) * RGBA surface import as torch tensor (on torch xpu device) * RGBA to RGB24 tensor slicing To build torchcodec with Intel GPU support: * Install pytorch with XPU backend support. For example, with: ``` pip3 install torch --index-url https://download.pytorch.org/whl/xpu ``` * Install oneAPI development environment following https://github.com/pytorch/pytorch?tab=readme-ov-file#intel-gpu-support * Build and install FFmpeg with `--enable-vaapi` * Install torcheval (for tests): `pip3 install torcheval` * Build torchcodec with: `ENABLE_XPU=1 python3 setup.py devel` Notes: * RGB24 is not supported color format on current Intel GPUs (as it is considered to be suboptimal due to odd alignments) * Intel media and compute APIs can't seamlessly work with the memory from each other. For example, Intel computes's Unified Shared Memory pointers are not recognized by media APIs. Thus, lower level sharing via dma fds is needed. This alos makes this part of the solution OS dependent. * Color space conversion algoriths might be quite different as it happens for Intel. This requires to check PSNR values instead of per-pixel atol/rtol differences. * Installing oneAPI environment is neded due to pytorch/pytorch#149075 This commit was primary verfied on Intel Battlemage G21 (0xe20b) and Intel Data Center GPU Flex (0x56c0). Co-authored-by: Edgar Romo Montiel <[email protected]> Signed-off-by: Edgar Romo Montiel <[email protected]> Signed-off-by: Dmitry Rogozhkin <[email protected]>
1 parent b01942c commit d6385be

File tree

9 files changed

+600
-52
lines changed

9 files changed

+600
-52
lines changed

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ def _build_all_extensions_with_cmake(self):
112112
torch_dir = Path(torch.utils.cmake_prefix_path) / "Torch"
113113
cmake_build_type = os.environ.get("CMAKE_BUILD_TYPE", "Release")
114114
enable_cuda = os.environ.get("ENABLE_CUDA", "")
115+
enable_xpu = os.environ.get("ENABLE_XPU", "")
115116
torchcodec_disable_compile_warning_as_error = os.environ.get(
116117
"TORCHCODEC_DISABLE_COMPILE_WARNING_AS_ERROR", "OFF"
117118
)
@@ -123,6 +124,7 @@ def _build_all_extensions_with_cmake(self):
123124
f"-DCMAKE_BUILD_TYPE={cmake_build_type}",
124125
f"-DPYTHON_VERSION={python_version.major}.{python_version.minor}",
125126
f"-DENABLE_CUDA={enable_cuda}",
127+
f"-DENABLE_XPU={enable_xpu}",
126128
f"-DTORCHCODEC_DISABLE_COMPILE_WARNING_AS_ERROR={torchcodec_disable_compile_warning_as_error}",
127129
]
128130

src/torchcodec/_core/CMakeLists.txt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ else()
3434
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic ${TORCHCODEC_WERROR_OPTION} ${TORCH_CXX_FLAGS}")
3535
endif()
3636

37+
if(ENABLE_CUDA)
38+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DENABLE_CUDA")
39+
endif()
40+
if(ENABLE_XPU)
41+
find_package(PkgConfig REQUIRED)
42+
pkg_check_modules(L0 REQUIRED IMPORTED_TARGET level-zero)
43+
pkg_check_modules(LIBVA REQUIRED IMPORTED_TARGET libva)
44+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DENABLE_XPU")
45+
endif()
3746

3847
function(make_torchcodec_sublibrary
3948
library_name
@@ -109,7 +118,11 @@ function(make_torchcodec_libraries
109118
)
110119

111120
if(ENABLE_CUDA)
112-
list(APPEND core_sources CudaDeviceInterface.cpp)
121+
list(APPEND core_sources CudaDeviceInterface.cpp)
122+
endif()
123+
124+
if(ENABLE_XPU)
125+
list(APPEND core_sources XpuDeviceInterface.cpp)
113126
endif()
114127

115128
set(core_library_dependencies
@@ -124,6 +137,11 @@ function(make_torchcodec_libraries
124137
)
125138
endif()
126139

140+
if(ENABLE_XPU)
141+
list(APPEND core_library_dependencies
142+
PkgConfig::L0 PkgConfig::LIBVA)
143+
endif()
144+
127145
make_torchcodec_sublibrary(
128146
"${core_library_name}"
129147
SHARED

0 commit comments

Comments
 (0)