Skip to content

torch fall back iwth runtime errors#17

Open
tlebryk wants to merge 1 commit intomainfrom
fix/torch-audio-fallback
Open

torch fall back iwth runtime errors#17
tlebryk wants to merge 1 commit intomainfrom
fix/torch-audio-fallback

Conversation

@tlebryk
Copy link
Copy Markdown
Contributor

@tlebryk tlebryk commented Jan 27, 2026

We don't catch runtime errors when trying torchcodex.encoders. This leads to issues like this when ffmeg libraries are incompatible like so:

  ---------------------------------------------------------------------------                                                                                                                                 
  RuntimeError                              Traceback (most recent call last)                                                                                                                                 
  File /usr/local/lib/python3.11/site-packages/IPython/core/formatters.py:984, in IPythonDisplayFormatter.__call__(self, obj)                                                                                 
  982 method = get_real_method(obj, self.print_method)                                                                                                                                                        
  983 if method is not None:                                                                                                                                                                                  
  --> 984     method()                                                                                                                                                                                        
  985     return True                                                                                                                                                                                         
                                                                                                                                                                                                              
  File /usr/local/lib/python3.11/site-packages/wsds/ws_audio.py:178, in AudioReader._ipython_display_(self)                                                                                                   
  174 import base64                                                                                                                                                                                           
  176 from IPython.display import HTML, display                                                                                                                                                               
  --> 178 samples = self.read_segment()                                                                                                                                                                       
  179 mp3_data = base64.b64encode(_audio_to_mp3(samples)).decode("ascii")                                                                                                                                     
  180 display(HTML(f'<audio controls src="data:audio/mp3;base64,{mp3_data}"/>'))                                                                                                                              
                                                                                                                                                                                                              
  File /usr/local/lib/python3.11/site-packages/wsds/ws_audio.py:152, in AudioReader.read_segment(self, start, end, sample_rate)                                                                               
  151 def read_segment(self, start=0, end=None, sample_rate=None):                                                                                                                                            
  --> 152     reader, sample_rate = self.get_reader(sample_rate)                                                                                                                                              
  153     seek_adjustment = self.skip_samples / sample_rate if start > 0 else 0                                                                                                                               
  154     _samples = reader.get_samples_played_in_range(                                                                                                                                                      
  155         start + seek_adjustment, end + seek_adjustment if end is not None else None                                                                                                                     
  156     )                                                                                                                                                                                                   
                                                                                                                                                                                                              
  File /usr/local/lib/python3.11/site-packages/wsds/ws_audio.py:129, in AudioReader.get_reader(self, sample_rate)                                                                                             
  127 if self.reader is None or sample_rate_switch:                                                                                                                                                           
  128     try:                                                                                                                                                                                                
  --> 129         from torchcodec.decoders import AudioDecoder                                                                                                                                                
  130     except ImportError:                                                                                                                                                                                 
  131         AudioDecoder = CompatAudioDecoder                                                                                                                                                               
                                                                                                                                                                                                              
  File /usr/local/lib/python3.11/site-packages/torchcodec/__init__.py:12                                                                                                                                      
  9 # Note: usort wants to put Frame and FrameBatch after decoders and samplers,                                                                                                                              
  10 # but that results in circular import.                                                                                                                                                                   
  11 from ._frame import AudioSamples, Frame, FrameBatch  # usort:skip # noqa                                                                                                                                 
  ---> 12 from . import decoders, encoders, samplers, transforms  # noqa                                                                                                                                      
  14 try:                                                                                                                                                                                                     
  15     # Note that version.py is generated during install.                                                                                                                                                  
  16     from .version import __version__  # noqa: F401                                                                                                                                                       
                                                                                                                                                                                                              
  File /usr/local/lib/python3.11/site-packages/torchcodec/decoders/__init__.py:7                                                                                                                              
  1 # Copyright (c) Meta Platforms, Inc. and affiliates.                                                                                                                                                      
  2 # All rights reserved.                                                                                                                                                                                    
  3 #                                                                                                                                                                                                         
  4 # This source code is licensed under the BSD-style license found in the                                                                                                                                   
  5 # LICENSE file in the root directory of this source tree.                                                                                                                                                 
  ----> 7 from .._core import AudioStreamMetadata, VideoStreamMetadata                                                                                                                                        
  8 from ._audio_decoder import AudioDecoder  # noqa                                                                                                                                                          
  9 from ._decoder_utils import set_cuda_backend  # noqa                                                                                                                                                      
                                                                                                                                                                                                              
  File /usr/local/lib/python3.11/site-packages/torchcodec/_core/__init__.py:8                                                                                                                                 
  1 # Copyright (c) Meta Platforms, Inc. and affiliates.                                                                                                                                                      
  2 # All rights reserved.                                                                                                                                                                                    
  3 #                                                                                                                                                                                                         
  4 # This source code is licensed under the BSD-style license found in the                                                                                                                                   
  5 # LICENSE file in the root directory of this source tree.                                                                                                                                                 
  ----> 8 from ._metadata import (                                                                                                                                                                            
  9     AudioStreamMetadata,                                                                                                                                                                                  
  10     ContainerMetadata,                                                                                                                                                                                   
  11     get_container_metadata,                                                                                                                                                                              
  12     get_container_metadata_from_header,                                                                                                                                                                  
  13     VideoStreamMetadata,                                                                                                                                                                                 
  14 )                                                                                                                                                                                                        
  15 from .ops import (                                                                                                                                                                                       
  16     _add_video_stream,                                                                                                                                                                                   
  17     _get_backend_details,                                                                                                                                                                                
  (...)     45     seek_to_pts,                                                                                                                                                                               
  46 )                                                                                                                                                                                                        
                                                                                                                                                                                                              
  File /usr/local/lib/python3.11/site-packages/torchcodec/_core/_metadata.py:16                                                                                                                               
  12 from fractions import Fraction                                                                                                                                                                           
  14 import torch                                                                                                                                                                                             
  ---> 16 from torchcodec._core.ops import (                                                                                                                                                                  
  17     _get_container_json_metadata,                                                                                                                                                                        
  18     _get_stream_json_metadata,                                                                                                                                                                           
  19     create_from_file,                                                                                                                                                                                    
  20 )                                                                                                                                                                                                        
  23 SPACES = "  "                                                                                                                                                                                            
  26 @dataclass                                                                                                                                                                                               
  27 class StreamMetadata:                                                                                                                                                                                    
                                                                                                                                                                                                              
  File /usr/local/lib/python3.11/site-packages/torchcodec/_core/ops.py:109                                                                                                                                    
  105             return os.add_dll_directory(str(ffmpeg_dir))  # that's the actual CM                                                                                                                        
  108 with expose_ffmpeg_dlls():                                                                                                                                                                              
  --> 109     ffmpeg_major_version, core_library_path = load_torchcodec_shared_libraries()                                                                                                                    
  112 # Note: We use disallow_in_graph because PyTorch does constant propagation of                                                                                                                           
  113 # factory functions.                                                                                                                                                                                    
  114 create_from_file = torch._dynamo.disallow_in_graph(                                                                                                                                                     
  115     torch.ops.torchcodec_ns.create_from_file.default                                                                                                                                                    
  116 )                                                                                                                                                                                                       
                                                                                                                                                                                                              
  File /usr/local/lib/python3.11/site-packages/torchcodec/_core/ops.py:76, in load_torchcodec_shared_libraries()                                                                                              
  69         exceptions.append((ffmpeg_major_version, exc_traceback))                                                                                                                                         
  71 traceback_info = (                                                                                                                                                                                       
  72     "\n[start of libtorchcodec loading traceback]\n"                                                                                                                                                     
  73     + "\n".join(f"FFmpeg version {v}:\n{tb}" for v, tb in exceptions)                                                                                                                                    
  74     + "[end of libtorchcodec loading traceback]."                                                                                                                                                        
  75 )                                                                                                                                                                                                        
  ---> 76 raise RuntimeError(                                                                                                                                                                                 
  77     f"""Could not load libtorchcodec. Likely causes:                                                                                                                                                     
  78       1. FFmpeg is not properly installed in your environment. We support                                                                                                                                
  79          versions 4, 5, 6, 7, and 8, and we attempt to load libtorchcodec                                                                                                                                
  80          for each of those versions. Errors for versions not installed on                                                                                                                                
  81          your system are expected; only the error for your installed FFmpeg                                                                                                                              
  82          version is relevant. On Windows, ensure you've installed the                                                                                                                                    
  83          "full-shared" version which ships DLLs.                                                                                                                                                         
  84       2. The PyTorch version ({torch.__version__}) is not compatible with                                                                                                                                
  85          this version of TorchCodec. Refer to the version compatibility                                                                                                                                  
  86          table:                                                                                                                                                                                          
  87          https://github.com/pytorch/torchcodec?tab=readme-ov-file#installing-torchcodec.                                                                                                                 
  88       3. Another runtime dependency; see exceptions below.                                                                                                                                               
  89                                                                                                                                                                                                          
  90     The following exceptions were raised as we tried to load libtorchcodec:                                                                                                                              
  91     """                                                                                                                                                                                                  
  92     f"{traceback_info}"                                                                                                                                                                                  
  93 )                                                                                                                                                                                                        
                                                                                                                                                                                                              
  RuntimeError: Could not load libtorchcodec. Likely causes:                                                                                                                                                  
  1. FFmpeg is not properly installed in your environment. We support                                                                                                                                         
  versions 4, 5, 6, 7, and 8, and we attempt to load libtorchcodec                                                                                                                                            
  for each of those versions. Errors for versions not installed on                                                                                                                                            
  your system are expected; only the error for your installed FFmpeg                                                                                                                                          
  version is relevant. On Windows, ensure you've installed the                                                                                                                                                
  "full-shared" version which ships DLLs.                                                                                                                                                                     
  2. The PyTorch version (2.8.0+cu128) is not compatible with                                                                                                                                                 
  this version of TorchCodec. Refer to the version compatibility                                                                                                                                              
  table:                                                                                                                                                                                                      
  https://github.com/pytorch/torchcodec?tab=readme-ov-file#installing-torchcodec.                                                                                                                             
  3. Another runtime dependency; see exceptions below.                                                                                                                                                        
                                                                                                                                                                                                              
  The following exceptions were raised as we tried to load libtorchcodec:                                                                                                                                     
                                                                                                                                                                                                              
  [start of libtorchcodec loading traceback]                                                                                                                                                                  
  FFmpeg version 8:                                                                                                                                                                                           
  Traceback (most recent call last):                                                                                                                                                                          
  File "/usr/local/lib/python3.11/site-packages/torchcodec/_core/ops.py", line 57, in load_torchcodec_shared_libraries                                                                                        
  torch.ops.load_library(core_library_path)                                                                                                                                                                   
  File "/usr/local/lib/python3.11/site-packages/torch/_ops.py", line 1478, in load_library                                                                                                                    
  ctypes.CDLL(path)                                                                                                                                                                                           
  File "/usr/local/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                                                                                                                         
                                                                                                                                                                                                              
  FFmpeg version 7:                                                                                                                                                                                           
  Traceback (most recent call last):                                                                                                                                                                          
  File "/usr/local/lib/python3.11/site-packages/torchcodec/_core/ops.py", line 57, in load_torchcodec_shared_libraries                                                                                        
  torch.ops.load_library(core_library_path)                                                                                                                                                                   
  File "/usr/local/lib/python3.11/site-packages/torch/_ops.py", line 1478, in load_library                                                                                                                    
  ctypes.CDLL(path)                                                                                                                                                                                           
  File "/usr/local/lib/python3.11/ctypes/__init__.py", line 376, in __init__                                                                                                                                  
  self._handle = _dlopen(self._name, mode)                                                                                                                                                                    
  ^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                   
  OSError: /usr/local/lib/python3.11/site-packages/torchcodec/libtorchcodec_core7.so: undefined symbol: _ZN3c1013MessageLogger6streamB5cxx11Ev                                                                
                                                                                                                                                                                                              
  FFmpeg version 6:                                                                                                                                                                                           
  Traceback (most recent call last):                                                                                                                                                                          
  File "/usr/local/lib/python3.11/site-packages/torchcodec/_core/ops.py", line 57, in load_torchcodec_shared_libraries                                                                                        
  torch.ops.load_library(core_library_path)                                                                                                                                                                   
  File "/usr/local/lib/python3.11/site-packages/torch/_ops.py", line 1478, in load_library                                                                                                                    
  ctypes.CDLL(path)                                                                                                                                                                                           
  File "/usr/local/lib/python3.11/ctypes/__init__.py", line 376, in __init__                                                                                                                                  
  self._handle = _dlopen(self._name, mode)                                                                                                                                                                    
  ^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                                                                                   
  OSError: /usr/local/lib/python3.11/site-packages/torchcodec/libtorchcodec_core6.so: undefined symbol: _ZN3c1013MessageLogger6streamB5cxx11Ev                                                                
                                                                                                                                                                                                              
  FFmpeg version 5:                                                                                                                                                                                           
  Traceback (most recent call last):                                                                                                                                                                          
  File "/usr/local/lib/python3.11/site-packages/torchcodec/_core/ops.py", line 57, in load_torchcodec_shared_libraries                                                                                        
  torch.ops.load_library(core_library_path)                                                                                                                                                                   
  File "/usr/local/lib/python3.11/site-packages/torch/_ops.py", line 1478, in load_library                                                                                                                    
  ctypes.CDLL(path)                                                                                                                                                                                           
  File "/usr/local/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                                                                                                                         
                                                                                                                                                                                                              
  FFmpeg version 4:                                                                                                                                                                                           
  Traceback (most recent call last):                                                                                                                                                                          
  File "/usr/local/lib/python3.11/site-packages/torchcodec/_core/ops.py", line 57, in load_torchcodec_shared_libraries                                                                                        
  torch.ops.load_library(core_library_path)                                                                                                                                                                   
  File "/usr/local/lib/python3.11/site-packages/torch/_ops.py", line 1478, in load_library                                                                                                                    
  ctypes.CDLL(path)                                                                                                                                                                                           
  File "/usr/local/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                                                                                                                         
  [end of libtorchcodec loading trac

Tested with same code and we correctly handled that exception and fell back to torchaudio.

@jpc
Copy link
Copy Markdown
Member

jpc commented Feb 20, 2026

I am hoping to solve these issues with https://github.com/jpc/humecodec/

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants