Skip to content

Commit 9689f43

Browse files
committed
test: add cppapi test to give TorchCodecConfig.cmake a try
Signed-off-by: Dmitry Rogozhkin <[email protected]>
1 parent b765629 commit 9689f43

File tree

7 files changed

+151
-4
lines changed

7 files changed

+151
-4
lines changed

.github/workflows/linux_cuda_wheel.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ jobs:
9494
python-version: ${{ matrix.python-version }}
9595
# We install conda packages at the start because otherwise conda may have conflicts with dependencies.
9696
# Note: xorg-libxau was addded to fix a problem with ffmpeg 4. We should consider removing it.
97-
default-packages: "nvidia/label/cuda-${{ matrix.cuda-version }}.0::libnpp nvidia::cuda-nvrtc=${{ matrix.cuda-version }} nvidia::cuda-toolkit=${{ matrix.cuda-version }} nvidia::cuda-cudart=${{ matrix.cuda-version }} nvidia::cuda-driver-dev=${{ matrix.cuda-version }} conda-forge::ffmpeg=${{ matrix.ffmpeg-version-for-tests }} conda-forge::xorg-libxau"
97+
default-packages: "nvidia/label/cuda-${{ matrix.cuda-version }}.0::libnpp nvidia::cuda-nvrtc=${{ matrix.cuda-version }} nvidia::cuda-toolkit=${{ matrix.cuda-version }} nvidia::cuda-cudart=${{ matrix.cuda-version }} nvidia::cuda-driver-dev=${{ matrix.cuda-version }} conda-forge::ffmpeg=${{ matrix.ffmpeg-version-for-tests }} conda-forge::xorg-libxau conda-forge::pkg-config"
9898
- name: Check env, set LD_LIBRARY_PATH
9999
run: |
100100
${CONDA_RUN} env
@@ -142,6 +142,8 @@ jobs:
142142
- name: Run Python tests
143143
run: |
144144
${CONDA_RUN} FAIL_WITHOUT_CUDA=1 pytest --override-ini="addopts=-v" test --tb=short
145+
env:
146+
CUDA_HOME: "/usr/local/cuda"
145147
- name: Run Python benchmark
146148
run: |
147149
${CONDA_RUN} time python benchmarks/decoders/gpu_benchmark.py --devices=cuda:0,cpu --resize_devices=none

.github/workflows/linux_wheel.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ jobs:
103103
source packaging/helpers.sh
104104
assert_ffmpeg_not_installed
105105
106-
conda install "ffmpeg=${{ matrix.ffmpeg-version-for-tests }}" -c conda-forge
106+
conda install "ffmpeg=${{ matrix.ffmpeg-version-for-tests }}" pkg-config -c conda-forge
107107
ffmpeg -version
108108
109109
- name: Install test dependencies

.github/workflows/macos_wheel.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ jobs:
9999

100100
- name: Install ffmpeg
101101
run: |
102-
conda install "ffmpeg=${{ matrix.ffmpeg-version-for-tests }}" -c conda-forge
102+
conda install "ffmpeg=${{ matrix.ffmpeg-version-for-tests }}" pkg-config -c conda-forge
103103
ffmpeg -version
104104
105105
- name: Install test dependencies

.github/workflows/windows_wheel.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ jobs:
109109
# side-effect. It's OK.
110110
source packaging/helpers.sh
111111
assert_ffmpeg_not_installed
112-
conda install "ffmpeg=${{ matrix.ffmpeg-version-for-tests }}" -c conda-forge
112+
conda install "ffmpeg=${{ matrix.ffmpeg-version-for-tests }}" pkg-config -c conda-forge
113113
ffmpeg -version
114114
- name: Test torchcodec import after FFmpeg installation
115115
run: |

test/cppapi/CMakeLists.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
cmake_minimum_required(VERSION 3.18)
2+
project(CppApiTest)
3+
set(CMAKE_CXX_STANDARD 17)
4+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
5+
6+
find_package(Torch REQUIRED)
7+
find_package(TorchCodec REQUIRED)
8+
9+
function(make_torchcodec_targets torchcodec_variant)
10+
set(binname "torchcodec_cppapitest${torchcodec_variant}")
11+
set(sources CppApiTest.cpp)
12+
13+
# Building executable to check the linkage
14+
add_executable(${binname} ${sources})
15+
# Avoid adding the "lib" prefix which we already add explicitly.
16+
set_target_properties(${binname} PROPERTIES PREFIX "")
17+
set_target_properties(${binname} PROPERTIES CXX_STANDARD 17)
18+
target_link_libraries(${binname}
19+
${TORCH_LIBRARIES}
20+
torchcodec::core${torchcodec_variant}
21+
torchcodec::ffmpeg${torchcodec_variant}
22+
)
23+
endfunction()
24+
25+
foreach(variant IN LISTS TORCHCODEC_VARIANTS)
26+
make_torchcodec_targets(${variant})
27+
endforeach()

test/cppapi/CppApiTest.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "DeviceInterface.h"
2+
#include "FilterGraph.h"
3+
4+
namespace facebook::torchcodec {
5+
6+
class DummyDeviceInterface : public DeviceInterface {
7+
public:
8+
DummyDeviceInterface(const torch::Device& device) : DeviceInterface(device) {}
9+
10+
virtual ~DummyDeviceInterface() {}
11+
12+
void initialize(
13+
const AVStream* avStream,
14+
const UniqueDecodingAVFormatContext& avFormatCtx,
15+
const SharedAVCodecContext& codecContext) override {}
16+
17+
void convertAVFrameToFrameOutput(
18+
UniqueAVFrame& avFrame,
19+
FrameOutput& frameOutput,
20+
std::optional<torch::Tensor> preAllocatedOutputTensor =
21+
std::nullopt) override {}
22+
23+
private:
24+
std::unique_ptr<FilterGraph> filterGraphContext_;
25+
};
26+
27+
namespace {
28+
static bool g_dummy = registerDeviceInterface(
29+
DeviceInterfaceKey(torch::kPrivateUse1),
30+
[](const torch::Device& device) {
31+
return new DummyDeviceInterface(device);
32+
});
33+
} // namespace
34+
} // namespace facebook::torchcodec
35+
36+
int main(int argc, char* argv[]) {
37+
return 0;
38+
}

test/test_cppapi.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import os
2+
import subprocess
3+
4+
from pathlib import Path
5+
6+
import torch
7+
import torchcodec
8+
9+
10+
def test_cppapi_pkgconfig(tmp_path):
11+
cmake_args = [
12+
"cmake",
13+
"-DCMAKE_BUILD_TYPE=Debug",
14+
"-DCMAKE_VERBOSE_MAKEFILE=ON",
15+
f"-DCMAKE_PREFIX_PATH={torchcodec.cmake_prefix_path};{torch.utils.cmake_prefix_path}",
16+
Path(__file__).parent / "cppapi",
17+
]
18+
result = subprocess.run(cmake_args, cwd=tmp_path)
19+
assert result.returncode == 0
20+
21+
result = subprocess.run(["cmake", "--build", "."], cwd=tmp_path)
22+
assert result.returncode == 0
23+
24+
ver = f"{torchcodec.ffmpeg_major_version}"
25+
result = subprocess.run([f"./torchcodec_cppapitest{ver}"], cwd=tmp_path)
26+
assert result.returncode == 0
27+
28+
29+
def test_cppapi_no_ffmpeg(tmp_path):
30+
cmake_args = [
31+
"cmake",
32+
"-DCMAKE_BUILD_TYPE=Debug",
33+
"-DCMAKE_VERBOSE_MAKEFILE=ON",
34+
f"-DCMAKE_PREFIX_PATH={torchcodec.cmake_prefix_path};{torch.utils.cmake_prefix_path}",
35+
Path(__file__).parent / "cppapi",
36+
]
37+
ver = f"{torchcodec.ffmpeg_major_version}"
38+
my_env = os.environ.copy()
39+
my_env[f"TORCHCODEC_FFMPEG{ver}_INSTALL_PREFIX"] = (
40+
Path(__file__).parent / "no-such-dir"
41+
)
42+
43+
# cmake config should fail as we've set ffmpeg install prefix to the not existing
44+
# directory
45+
result = subprocess.run(cmake_args, cwd=tmp_path, env=my_env)
46+
assert result.returncode != 0
47+
48+
49+
def test_cppapi_with_prefix(tmp_path):
50+
cmake_args = [
51+
"cmake",
52+
"-DCMAKE_BUILD_TYPE=Debug",
53+
"-DCMAKE_VERBOSE_MAKEFILE=ON",
54+
f"-DCMAKE_PREFIX_PATH={torchcodec.cmake_prefix_path};{torch.utils.cmake_prefix_path}",
55+
Path(__file__).parent / "cppapi",
56+
]
57+
58+
# In this test we are calculating the prefix of installed ffmpeg version from the location
59+
# of its libavcodec.pc file. Potentially, on the custom ffmpeg install with custom layout
60+
# our calculation can be wrong and test might fail.
61+
result = subprocess.run(
62+
["pkg-config", "--path", "libavcodec"], capture_output=True, text=True
63+
)
64+
assert result.returncode == 0
65+
66+
ver = f"{torchcodec.ffmpeg_major_version}"
67+
my_env = os.environ.copy()
68+
my_env[f"TORCHCODEC_FFMPEG{ver}_INSTALL_PREFIX"] = Path(
69+
f"{result.stdout}"
70+
).parent.parent.parent
71+
72+
result = subprocess.run(cmake_args, cwd=tmp_path, env=my_env)
73+
assert result.returncode == 0
74+
75+
result = subprocess.run(["cmake", "--build", "."], cwd=tmp_path)
76+
assert result.returncode == 0
77+
78+
ver = f"{torchcodec.ffmpeg_major_version}"
79+
result = subprocess.run([f"./torchcodec_cppapitest{ver}"], cwd=tmp_path)
80+
assert result.returncode == 0

0 commit comments

Comments
 (0)