Skip to content

Conversation

@NicolasHug
Copy link
Contributor

In #1138 we decided to provide the full traceback to the user when import torchcodec fails. This leads to the printed debug info to be really long and difficult to parse.

This PR is a first attempt at condensing the error log to make them more informative and actionable. I think what I wrote is mostly specific to Linux for now. This is just a temporary fix, the long term solution would be to have a log system with verbosity level.

The main principle in this PR is that the new logic should not hide any information that was previously printed - only condense it. I think this principle is respected, but would appreciate more eyes on it.

New logs:

[start of libtorchcodec loading traceback]
FFmpeg version 8:
Got the following exception: OSError: libavutil.so.60: cannot open shared object file: No such file or directory
FFmpeg version 8 is likely not installed or its libraries cannot be found on this system.

FFmpeg version 7:
Got the following exception: OSError: libavcodec.so.61: cannot open shared object file: No such file or directory
FFmpeg version 7 is likely not installed or its libraries cannot be found on this system.

FFmpeg version 6:
Got the following exception: OSError: libavutil.so.58: cannot open shared object file: No such file or directory
FFmpeg version 6 is likely not installed or its libraries cannot be found on this system.

FFmpeg version 5:
Got the following exception: OSError: libavutil.so.57: cannot open shared object file: No such file or directory
FFmpeg version 5 is likely not installed or its libraries cannot be found on this system.

FFmpeg version 4:
Got the following exception: OSError: libavutil.so.56: cannot open shared object file: No such file or directory
FFmpeg version 4 is likely not installed or its libraries cannot be found on this system.
[end of libtorchcodec loading traceback].

Previous logs:



[start of libtorchcodec loading traceback]
FFmpeg version 8:
Traceback (most recent call last):
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torch/_ops.py", line 1487, in load_library
    ctypes.CDLL(path)
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/ctypes/__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: libavutil.so.60: cannot open shared object file: No such file or directory

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torchcodec/_core/ops.py", line 57, in load_torchcodec_shared_libraries
    torch.ops.load_library(core_library_path)
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torch/_ops.py", line 1489, in load_library
    raise OSError(f"Could not load this library: {path}") from e
OSError: Could not load this library: /home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torchcodec/libtorchcodec_core8.so

FFmpeg version 7:
Traceback (most recent call last):
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torch/_ops.py", line 1487, in load_library
    ctypes.CDLL(path)
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/ctypes/__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: libavcodec.so.61: cannot open shared object file: No such file or directory

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torchcodec/_core/ops.py", line 57, in load_torchcodec_shared_libraries
    torch.ops.load_library(core_library_path)
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torch/_ops.py", line 1489, in load_library
    raise OSError(f"Could not load this library: {path}") from e
OSError: Could not load this library: /home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torchcodec/libtorchcodec_core7.so

FFmpeg version 6:
Traceback (most recent call last):
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torch/_ops.py", line 1487, in load_library
    ctypes.CDLL(path)
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/ctypes/__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: libavutil.so.58: cannot open shared object file: No such file or directory

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torchcodec/_core/ops.py", line 57, in load_torchcodec_shared_libraries
    torch.ops.load_library(core_library_path)
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torch/_ops.py", line 1489, in load_library
    raise OSError(f"Could not load this library: {path}") from e
OSError: Could not load this library: /home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torchcodec/libtorchcodec_core6.so

FFmpeg version 5:
Traceback (most recent call last):
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torch/_ops.py", line 1487, in load_library
    ctypes.CDLL(path)
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/ctypes/__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: libavutil.so.57: cannot open shared object file: No such file or directory

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torchcodec/_core/ops.py", line 57, in load_torchcodec_shared_libraries
    torch.ops.load_library(core_library_path)
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torch/_ops.py", line 1489, in load_library
    raise OSError(f"Could not load this library: {path}") from e
OSError: Could not load this library: /home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torchcodec/libtorchcodec_core5.so

FFmpeg version 4:
Traceback (most recent call last):
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torch/_ops.py", line 1487, in load_library
    ctypes.CDLL(path)
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/ctypes/__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: libavutil.so.56: cannot open shared object file: No such file or directory

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torchcodec/_core/ops.py", line 57, in load_torchcodec_shared_libraries
    torch.ops.load_library(core_library_path)
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torch/_ops.py", line 1489, in load_library
    raise OSError(f"Could not load this library: {path}") from e
OSError: Could not load this library: /home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torchcodec/libtorchcodec_core4.so
[end of libtorchcodec loading traceback].

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Jan 7, 2026
f"FFmpeg version {ffmpeg_major_version} is likely not installed or its libraries cannot be found on this system.\n",
)
)
else:
Copy link
Contributor

Choose a reason for hiding this comment

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

Were you able to test any import fails to hit this else case?

I do not recall the particulars of the stack trace, but if a stack trace begins with an OSError, but later lists a root cause after The above exception was the direct cause of the following exception, we will append the condensed OSError exception instead of the potentially more informative root cause.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In the scenario you mention above where we have an OSError and then an another exception stacked on top of it, we will only condense the error message if that other exception is of the form

OSError: libavcodec.so.60: cannot open shared object file: No such file or directory

i.e. only if it directly relates to libav* libraries.

In other words, we're only condensing this

FFmpeg version 7:
Traceback (most recent call last):
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torch/_ops.py", line 1487, in load_library
    ctypes.CDLL(path)
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/ctypes/__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: libavcodec.so.61: cannot open shared object file: No such file or directory

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torchcodec/_core/ops.py", line 57, in load_torchcodec_shared_libraries
    torch.ops.load_library(core_library_path)
  File "/home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torch/_ops.py", line 1489, in load_library
    raise OSError(f"Could not load this library: {path}") from e
OSError: Could not load this library: /home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torchcodec/libtorchcodec_core7.so

into this:

FFmpeg version 7:
Got the following exception: OSError: libavcodec.so.61: cannot open shared object file: No such file or directory
FFmpeg version 7 is likely not installed or its libraries cannot be found on this system.

Any other import error will not be condensed and will hit this else branch, showing the full logs.

if (
isinstance(e, OSError)
and ("Could not load this library") in str(e)
and "libtorchcodec" in (str(e))
Copy link
Contributor

Choose a reason for hiding this comment

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

To define missing_ffmpeg_lib above, we search the error message line by line, but here we are searching the entire string for the two elements of the error message. Is there some reason for that?

It seems Could not load this library and libtorchcodec appear on the same line, so reusing the pattern might help to clarify the conditions we check to add a condensed error message.

...
    raise OSError(f"Could not load this library: {path}") from e
OSError: Could not load this library: /home/nicolashug/.opt/miniconda3/envs/codec/lib/python3.11/site-packages/torchcodec/libtorchcodec_core6.so

Copy link
Contributor

@Dan-Flores Dan-Flores left a comment

Choose a reason for hiding this comment

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

This change looks good! I believe the failed CPP tests are unrelated, but please double check

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants