Skip to content

Commit 86e952f

Browse files
authored
Generalize pybind_ops module name (#759)
1 parent 5e54c64 commit 86e952f

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

src/torchcodec/_core/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,15 @@ function(make_torchcodec_libraries
151151
PUBLIC
152152
"-fvisibility=hidden"
153153
)
154+
# The value we use here must match the value we return from
155+
# _get_pybind_ops_module_name() on the Python side. If the values do not
156+
# match, then we will be unable to import the C++ shared library as a
157+
# Python module at runtime.
158+
target_compile_definitions(
159+
${pybind_ops_library_name}
160+
PRIVATE
161+
PYBIND_OPS_MODULE_NAME=decoder_core_pybind_ops
162+
)
154163
# If we don't make sure this flag is set, we run into segfauls at import
155164
# time on Mac. See:
156165
# https://github.com/pybind/pybind11/issues/3907#issuecomment-1170412764

src/torchcodec/_core/ops.py

Lines changed: 2 additions & 1 deletion
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,8 +44,8 @@ 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+
pybind_ops_module_name = _get_pybind_ops_module_name(ffmpeg_major_version)
4849
decoder_library_name = f"libtorchcodec_decoder{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}"

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 "decoder_core_pybind_ops"

0 commit comments

Comments
 (0)