Skip to content

Commit 7eea6d8

Browse files
authored
Merge branch 'main' into fakerelease
2 parents cc20b9c + d444567 commit 7eea6d8

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

src/torchcodec/_core/CMakeLists.txt

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,55 +48,53 @@ function(make_torchcodec_libraries
4848
# We create three shared libraries per version of FFmpeg, where the version
4949
# is denoted by N:
5050
#
51-
# 1. libtorchcodec_decoderN.{ext}: Base library which contains the
51+
# 1. libtorchcodec_coreN.{ext}: Base library which contains the
5252
# implementation of VideoDecoder and everything VideoDecoder needs. On
5353
# Linux, {ext} is so. On Mac, it is dylib.
5454
#
5555
# 2. libtorchcodec_custom_opsN.{ext}: Implementation of the PyTorch custom
56-
# ops. Depends on libtorchcodec_decoderN.{ext}. On Linux, {ext} is so.
56+
# ops. Depends on libtorchcodec_coreN.{ext}. On Linux, {ext} is so.
5757
# On Mac, it is dylib.
5858
#
5959
# 3. libtorchcodec_pybind_opsN.{ext}: Implementation of the pybind11 ops. We
6060
# keep these separate from the PyTorch custom ops because we have to
6161
# load these libraries separately on the Python side. Depends on
62-
# libtorchcodec_decoderN.{ext}. On BOTH Linux and Mac {ext} is so.
62+
# libtorchcodec_coreN.{ext}. On BOTH Linux and Mac {ext} is so.
6363

64-
# 1. Create libtorchcodec_decoderN.{ext}.
65-
set(decoder_library_name "libtorchcodec_decoder${ffmpeg_major_version}")
66-
set(decoder_sources
64+
# 1. Create libtorchcodec_coreN.{ext}.
65+
set(core_library_name "libtorchcodec_core${ffmpeg_major_version}")
66+
set(core_sources
6767
AVIOContextHolder.cpp
6868
AVIOTensorContext.cpp
6969
FFMPEGCommon.cpp
7070
Frame.cpp
7171
DeviceInterface.cpp
7272
CpuDeviceInterface.cpp
7373
SingleStreamDecoder.cpp
74-
# TODO: lib name should probably not be "*_decoder*" now that it also
75-
# contains an encoder
7674
Encoder.cpp
7775
)
7876

7977
if(ENABLE_CUDA)
80-
list(APPEND decoder_sources CudaDeviceInterface.cpp)
78+
list(APPEND core_sources CudaDeviceInterface.cpp)
8179
endif()
8280

83-
set(decoder_library_dependencies
81+
set(core_library_dependencies
8482
${ffmpeg_target}
8583
${TORCH_LIBRARIES}
8684
)
8785

8886
if(ENABLE_CUDA)
89-
list(APPEND decoder_library_dependencies
87+
list(APPEND core_library_dependencies
9088
${CUDA_nppi_LIBRARY}
9189
${CUDA_nppicc_LIBRARY}
9290
)
9391
endif()
9492

9593
make_torchcodec_sublibrary(
96-
"${decoder_library_name}"
94+
"${core_library_name}"
9795
SHARED
98-
"${decoder_sources}"
99-
"${decoder_library_dependencies}"
96+
"${core_sources}"
97+
"${core_library_dependencies}"
10098
)
10199

102100
# 2. Create libtorchcodec_custom_opsN.{ext}.
@@ -106,7 +104,7 @@ function(make_torchcodec_libraries
106104
custom_ops.cpp
107105
)
108106
set(custom_ops_dependencies
109-
${decoder_library_name}
107+
${core_library_name}
110108
${Python3_LIBRARIES}
111109
)
112110
make_torchcodec_sublibrary(
@@ -123,7 +121,7 @@ function(make_torchcodec_libraries
123121
pybind_ops.cpp
124122
)
125123
set(pybind_ops_dependencies
126-
${decoder_library_name}
124+
${core_library_name}
127125
pybind11::module # This library dependency makes sure we have the right
128126
# Python libraries included as well as all of the right
129127
# settings so that we can successfully load the shared
@@ -151,6 +149,15 @@ function(make_torchcodec_libraries
151149
PUBLIC
152150
"-fvisibility=hidden"
153151
)
152+
# The value we use here must match the value we return from
153+
# _get_pybind_ops_module_name() on the Python side. If the values do not
154+
# match, then we will be unable to import the C++ shared library as a
155+
# Python module at runtime.
156+
target_compile_definitions(
157+
${pybind_ops_library_name}
158+
PRIVATE
159+
PYBIND_OPS_MODULE_NAME=core_pybind_ops
160+
)
154161
# If we don't make sure this flag is set, we run into segfauls at import
155162
# time on Mac. See:
156163
# https://github.com/pybind/pybind11/issues/3907#issuecomment-1170412764
@@ -163,7 +170,7 @@ function(make_torchcodec_libraries
163170
# Install all libraries.
164171
set(
165172
all_libraries
166-
${decoder_library_name}
173+
${core_library_name}
167174
${custom_ops_library_name}
168175
${pybind_ops_library_name}
169176
)
@@ -240,7 +247,7 @@ else()
240247
# Expose these values updwards so that the test compilation does not need
241248
# to re-figure it out. FIXME: it's not great that we just copy-paste the
242249
# library names.
243-
set(libtorchcodec_library_name "libtorchcodec_decoder${ffmpeg_major_version}" PARENT_SCOPE)
250+
set(libtorchcodec_library_name "libtorchcodec_core${ffmpeg_major_version}" PARENT_SCOPE)
244251
set(libtorchcodec_custom_ops_name "libtorchcodec_custom_ops${ffmpeg_major_version}" PARENT_SCOPE)
245252
set(libav_include_dirs ${LIBAV_INCLUDE_DIRS} PARENT_SCOPE)
246253
endif()

src/torchcodec/_core/ops.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from torchcodec._internally_replaced_utils import ( # @manual=//pytorch/torchcodec/src:internally_replaced_utils
1717
_get_extension_path,
18+
_get_pybind_ops_module_name,
1819
_load_pybind11_module,
1920
)
2021

@@ -43,9 +44,9 @@ def load_torchcodec_shared_libraries():
4344
# libraries do not meet those conditions.
4445

4546
exceptions = []
46-
pybind_ops_module_name = "decoder_core_pybind_ops"
4747
for ffmpeg_major_version in (7, 6, 5, 4):
48-
decoder_library_name = f"libtorchcodec_decoder{ffmpeg_major_version}"
48+
pybind_ops_module_name = _get_pybind_ops_module_name(ffmpeg_major_version)
49+
decoder_library_name = f"libtorchcodec_core{ffmpeg_major_version}"
4950
custom_ops_library_name = f"libtorchcodec_custom_ops{ffmpeg_major_version}"
5051
pybind_ops_library_name = f"libtorchcodec_pybind_ops{ffmpeg_major_version}"
5152
try:

src/torchcodec/_core/pybind_ops.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ int64_t create_from_file_like(
3838
return reinterpret_cast<int64_t>(decoder);
3939
}
4040

41-
PYBIND11_MODULE(decoder_core_pybind_ops, m) {
41+
#ifndef PYBIND_OPS_MODULE_NAME
42+
#error PYBIND_OPS_MODULE_NAME must be defined!
43+
#endif
44+
45+
PYBIND11_MODULE(PYBIND_OPS_MODULE_NAME, m) {
4246
m.def("create_from_file_like", &create_from_file_like);
4347
}
4448

src/torchcodec/_internally_replaced_utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,14 @@ def _load_pybind11_module(module_name: str, library_path: str) -> ModuleType:
5151
)
5252

5353
return importlib.util.module_from_spec(spec)
54+
55+
56+
def _get_pybind_ops_module_name(ffmpeg_major_version: str) -> str:
57+
# Note that this value must match the value used as PYBIND_OPS_MODULE_NAME
58+
# when we compile _core/pybind_ops.cpp. If the values do not match, we will
59+
# not be able to import the C++ shared library as a Python module at
60+
# runtime.
61+
#
62+
# The parameter ffmpeg_major_version is unused externally, but used
63+
# internally.
64+
return "core_pybind_ops"

0 commit comments

Comments
 (0)