Fix RuntimeError not caught when torchcodec fails to load#3987
Conversation
When datasets library has torchcodec installed but FFmpeg libraries are missing, torchcodec raises a RuntimeError during import. The exception handler only caught ImportError and AttributeError, causing the error to propagate and crash Unsloth imports in environments like Colab where FFmpeg may not be installed.
Summary of ChangesHello @danielhanchen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request improves the stability of the system by preventing crashes caused by Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request correctly addresses a RuntimeError that occurs when torchcodec fails to load by adding it to the exceptions caught. The change is simple and effective. I have one suggestion to improve code clarity and maintainability.
|
|
||
| _patch() | ||
| except (ImportError, AttributeError): | ||
| except (ImportError, AttributeError, RuntimeError): |
There was a problem hiding this comment.
For improved clarity and consistency with other exception handlers in this file (e.g., line 1045), consider adding a brief comment to the pass statement on the next line. This would explain why these exceptions are being caught and ignored, which is helpful for future maintainers. For example:
pass # unsloth_zoo not installed or torchcodec failed to load…3987) When datasets library has torchcodec installed but FFmpeg libraries are missing, torchcodec raises a RuntimeError during import. The exception handler only caught ImportError and AttributeError, causing the error to propagate and crash Unsloth imports in environments like Colab where FFmpeg may not be installed. Co-authored-by: Daniel Han <danielhanchen@users.noreply.github.com>
Summary
RuntimeErrorinpatch_torchcodec_audio_decoder()exception handlerdisable_torchcodec_if_broken()to disable torchcodec in transformers when FFmpeg is missingProblem
When torchcodec is installed but FFmpeg shared libraries (libavutil.so) are not available:
Import-time crash:
patch_torchcodec_audio_decoder()tries to import from datasets which imports torchcodec, raisingRuntimeErrorRuntime crash: transformers'
is_torchcodec_available()returns True (package exists via find_spec), soload_audio()tries to use torchcodec which fails:Solution
Add
RuntimeErrorto the exception tuple inpatch_torchcodec_audio_decoder()Add
disable_torchcodec_if_broken()which:transformers.utils.import_utils._torchcodec_available = FalseRelated
Testing
Verified Unsloth imports and audio loading works on systems without FFmpeg installed.