Skip to content

Generalize pybind_ops module name #759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/torchcodec/_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,15 @@ function(make_torchcodec_libraries
PUBLIC
"-fvisibility=hidden"
)
# The value we use here must match the value we return from
# _get_pybind_ops_module_name() on the Python side. If the values do not
# match, then we will be unable to import the C++ shared library as a
# Python module at runtime.
target_compile_definitions(
${pybind_ops_library_name}
PRIVATE
PYBIND_OPS_MODULE_NAME=decoder_core_pybind_ops
)
# If we don't make sure this flag is set, we run into segfauls at import
# time on Mac. See:
# https://github.com/pybind/pybind11/issues/3907#issuecomment-1170412764
Expand Down
3 changes: 2 additions & 1 deletion src/torchcodec/_core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from torchcodec._internally_replaced_utils import ( # @manual=//pytorch/torchcodec/src:internally_replaced_utils
_get_extension_path,
_get_pybind_ops_module_name,
_load_pybind11_module,
)

Expand Down Expand Up @@ -43,8 +44,8 @@ def load_torchcodec_shared_libraries():
# libraries do not meet those conditions.

exceptions = []
pybind_ops_module_name = "decoder_core_pybind_ops"
for ffmpeg_major_version in (7, 6, 5, 4):
pybind_ops_module_name = _get_pybind_ops_module_name(ffmpeg_major_version)
decoder_library_name = f"libtorchcodec_decoder{ffmpeg_major_version}"
custom_ops_library_name = f"libtorchcodec_custom_ops{ffmpeg_major_version}"
pybind_ops_library_name = f"libtorchcodec_pybind_ops{ffmpeg_major_version}"
Expand Down
6 changes: 5 additions & 1 deletion src/torchcodec/_core/pybind_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ int64_t create_from_file_like(
return reinterpret_cast<int64_t>(decoder);
}

PYBIND11_MODULE(decoder_core_pybind_ops, m) {
#ifndef PYBIND_OPS_MODULE_NAME
#error PYBIND_OPS_MODULE_NAME must be defined!
#endif

PYBIND11_MODULE(PYBIND_OPS_MODULE_NAME, m) {
m.def("create_from_file_like", &create_from_file_like);
}

Expand Down
11 changes: 11 additions & 0 deletions src/torchcodec/_internally_replaced_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,14 @@ def _load_pybind11_module(module_name: str, library_path: str) -> ModuleType:
)

return importlib.util.module_from_spec(spec)


def _get_pybind_ops_module_name(ffmpeg_major_version: str) -> str:
# Note that this value must match the value used as PYBIND_OPS_MODULE_NAME
# when we compile _core/pybind_ops.cpp. If the values do not match, we will
# not be able to import the C++ shared library as a Python module at
# runtime.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just add a comment clarifying that the ffmpeg_major_version is only required for the internal version of this util

#
# The parameter ffmpeg_major_version is unused externally, but used
# internally.
return "decoder_core_pybind_ops"
Loading